blob: fe82905c607a8a689c4e7588abbbc2aef4aa8fcf [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"
Chris Lattnera500fc62005-09-09 23:53:39 +000038#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000039using namespace llvm;
40
Chris Lattnercd3245a2006-12-19 22:41:21 +000041STATISTIC(NodesCombined , "Number of dag nodes combined");
42STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
43STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng8b944d32009-05-28 00:35:15 +000044STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Evan Cheng31959b12011-02-02 01:06:55 +000045STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int");
Chris Lattnercd3245a2006-12-19 22:41:21 +000046
Nate Begeman1d4d4142005-09-01 00:19:25 +000047namespace {
Jim Laskey71382342006-10-07 23:37:56 +000048 static cl::opt<bool>
Owen Anderson0dcc8142010-09-19 21:01:26 +000049 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000050 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000051
Jim Laskey07a27092006-10-18 19:08:31 +000052 static cl::opt<bool>
53 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
54 cl::desc("Include global information in alias analysis"));
55
Jim Laskeybc588b82006-10-05 15:07:25 +000056//------------------------------ DAGCombiner ---------------------------------//
57
Nick Lewycky6726b6d2009-10-25 06:33:48 +000058 class DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000059 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000060 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000061 CombineLevel Level;
Bill Wendling98a366d2009-04-29 23:29:43 +000062 CodeGenOpt::Level OptLevel;
Duncan Sands25cf2272008-11-24 14:53:14 +000063 bool LegalOperations;
64 bool LegalTypes;
Nate Begeman1d4d4142005-09-01 00:19:25 +000065
66 // Worklist of all of the nodes that need to be simplified.
James Molloy6660c052012-02-16 09:17:04 +000067 //
68 // This has the semantics that when adding to the worklist,
69 // the item added must be next to be processed. It should
70 // also only appear once. The naive approach to this takes
71 // linear time.
72 //
73 // To reduce the insert/remove time to logarithmic, we use
74 // a set and a vector to maintain our worklist.
75 //
76 // The set contains the items on the worklist, but does not
77 // maintain the order they should be visited.
78 //
79 // The vector maintains the order nodes should be visited, but may
80 // contain duplicate or removed nodes. When choosing a node to
81 // visit, we pop off the order stack until we find an item that is
82 // also in the contents set. All operations are O(log N).
83 SmallPtrSet<SDNode*, 64> WorkListContents;
Benjamin Kramerd5f76902012-03-10 00:23:58 +000084 SmallVector<SDNode*, 64> WorkListOrder;
Nate Begeman1d4d4142005-09-01 00:19:25 +000085
Jim Laskeyc7c3f112006-10-16 20:52:31 +000086 // AA - Used for DAG load/store alias analysis.
87 AliasAnalysis &AA;
88
Nate Begeman1d4d4142005-09-01 00:19:25 +000089 /// AddUsersToWorkList - When an instruction is simplified, add all users of
90 /// the instruction to the work lists because they might get more simplified
91 /// now.
92 ///
93 void AddUsersToWorkList(SDNode *N) {
94 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000095 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000096 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000097 }
98
Dan Gohman389079b2007-10-08 17:57:15 +000099 /// visit - call the node-specific routine that knows how to fold each
100 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +0000101 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000102
Chris Lattner24664722006-03-01 04:53:38 +0000103 public:
James Molloy6afa3f72012-02-16 09:48:07 +0000104 /// AddToWorkList - Add to the work list making sure its instance is at the
James Molloy6660c052012-02-16 09:17:04 +0000105 /// back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000106 void AddToWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000107 WorkListContents.insert(N);
108 WorkListOrder.push_back(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000109 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000110
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000111 /// removeFromWorkList - remove all instances of N from the worklist.
112 ///
113 void removeFromWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000114 WorkListContents.erase(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000115 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000116
Dan Gohman475871a2008-07-27 21:46:04 +0000117 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000118 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000119
Dan Gohman475871a2008-07-27 21:46:04 +0000120 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000121 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000122 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000123
Dan Gohman475871a2008-07-27 21:46:04 +0000124 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000125 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000126 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000127 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000128 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000129
130 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000131
132 private:
133
Chris Lattner012f2412006-02-17 21:58:01 +0000134 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000135 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000136 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000137 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman87862e72009-12-11 21:31:27 +0000138 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
139 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000140 return SimplifyDemandedBits(Op, Demanded);
141 }
142
Dan Gohman475871a2008-07-27 21:46:04 +0000143 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000144
Chris Lattner448f2192006-11-11 00:39:41 +0000145 bool CombineToPreIndexedLoadStore(SDNode *N);
146 bool CombineToPostIndexedLoadStore(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000147
Evan Cheng95c57ea2010-04-24 04:43:44 +0000148 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
149 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
150 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
151 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000152 SDValue PromoteIntBinOp(SDValue Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000153 SDValue PromoteIntShiftOp(SDValue Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000154 SDValue PromoteExtend(SDValue Op);
155 bool PromoteLoad(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000156
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +0000157 void ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000158 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +0000159 ISD::NodeType ExtType);
160
Dan Gohman389079b2007-10-08 17:57:15 +0000161 /// combine - call the node-specific routine that knows how to fold each
162 /// particular type of node. If that doesn't do anything, try the
163 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000164 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000165
166 // Visitation implementation - Implement dag node combining for different
167 // node types. The semantics are as follows:
168 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000169 // SDValue.getNode() == 0 - No change was made
170 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
171 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000172 //
Dan Gohman475871a2008-07-27 21:46:04 +0000173 SDValue visitTokenFactor(SDNode *N);
174 SDValue visitMERGE_VALUES(SDNode *N);
175 SDValue visitADD(SDNode *N);
176 SDValue visitSUB(SDNode *N);
177 SDValue visitADDC(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000178 SDValue visitSUBC(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000179 SDValue visitADDE(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000180 SDValue visitSUBE(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000181 SDValue visitMUL(SDNode *N);
182 SDValue visitSDIV(SDNode *N);
183 SDValue visitUDIV(SDNode *N);
184 SDValue visitSREM(SDNode *N);
185 SDValue visitUREM(SDNode *N);
186 SDValue visitMULHU(SDNode *N);
187 SDValue visitMULHS(SDNode *N);
188 SDValue visitSMUL_LOHI(SDNode *N);
189 SDValue visitUMUL_LOHI(SDNode *N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +0000190 SDValue visitSMULO(SDNode *N);
191 SDValue visitUMULO(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000192 SDValue visitSDIVREM(SDNode *N);
193 SDValue visitUDIVREM(SDNode *N);
194 SDValue visitAND(SDNode *N);
195 SDValue visitOR(SDNode *N);
196 SDValue visitXOR(SDNode *N);
197 SDValue SimplifyVBinOp(SDNode *N);
Craig Topperdd201ff2012-09-11 01:45:21 +0000198 SDValue SimplifyVUnaryOp(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000199 SDValue visitSHL(SDNode *N);
200 SDValue visitSRA(SDNode *N);
201 SDValue visitSRL(SDNode *N);
202 SDValue visitCTLZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000203 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000204 SDValue visitCTTZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000205 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000206 SDValue visitCTPOP(SDNode *N);
207 SDValue visitSELECT(SDNode *N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +0000208 SDValue visitVSELECT(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000209 SDValue visitSELECT_CC(SDNode *N);
210 SDValue visitSETCC(SDNode *N);
211 SDValue visitSIGN_EXTEND(SDNode *N);
212 SDValue visitZERO_EXTEND(SDNode *N);
213 SDValue visitANY_EXTEND(SDNode *N);
214 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
215 SDValue visitTRUNCATE(SDNode *N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000216 SDValue visitBITCAST(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000217 SDValue visitBUILD_PAIR(SDNode *N);
218 SDValue visitFADD(SDNode *N);
219 SDValue visitFSUB(SDNode *N);
220 SDValue visitFMUL(SDNode *N);
Owen Anderson062c0a52012-05-02 22:17:40 +0000221 SDValue visitFMA(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000222 SDValue visitFDIV(SDNode *N);
223 SDValue visitFREM(SDNode *N);
224 SDValue visitFCOPYSIGN(SDNode *N);
225 SDValue visitSINT_TO_FP(SDNode *N);
226 SDValue visitUINT_TO_FP(SDNode *N);
227 SDValue visitFP_TO_SINT(SDNode *N);
228 SDValue visitFP_TO_UINT(SDNode *N);
229 SDValue visitFP_ROUND(SDNode *N);
230 SDValue visitFP_ROUND_INREG(SDNode *N);
231 SDValue visitFP_EXTEND(SDNode *N);
232 SDValue visitFNEG(SDNode *N);
233 SDValue visitFABS(SDNode *N);
Owen Anderson7c626d32012-08-13 23:32:49 +0000234 SDValue visitFCEIL(SDNode *N);
235 SDValue visitFTRUNC(SDNode *N);
236 SDValue visitFFLOOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000237 SDValue visitBRCOND(SDNode *N);
238 SDValue visitBR_CC(SDNode *N);
239 SDValue visitLOAD(SDNode *N);
240 SDValue visitSTORE(SDNode *N);
241 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
242 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
243 SDValue visitBUILD_VECTOR(SDNode *N);
244 SDValue visitCONCAT_VECTORS(SDNode *N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +0000245 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000246 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000247
Dan Gohman475871a2008-07-27 21:46:04 +0000248 SDValue XformToShuffleWithZero(SDNode *N);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000249 SDValue ReassociateOps(unsigned Opc, SDLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000250
Dan Gohman475871a2008-07-27 21:46:04 +0000251 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000252
Dan Gohman475871a2008-07-27 21:46:04 +0000253 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
254 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000255 SDValue SimplifySelect(SDLoc DL, SDValue N0, SDValue N1, SDValue N2);
256 SDValue SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1, SDValue N2,
Scott Michelfdc40a02009-02-17 22:15:04 +0000257 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000258 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000259 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000260 SDLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000261 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000262 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000263 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000264 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000265 SDValue BuildSDIV(SDNode *N);
266 SDValue BuildUDIV(SDNode *N);
Evan Cheng9568e5c2011-06-21 06:01:08 +0000267 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
268 bool DemandHighBits = true);
269 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000270 SDNode *MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000271 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000272 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Cheng31959b12011-02-02 01:06:55 +0000273 SDValue TransformFPLoadStorePair(SDNode *N);
Michael Liaofac14ab2012-10-23 23:06:52 +0000274 SDValue reduceBuildVecExtToExtBuildVec(SDNode *N);
Michael Liao1a5cc712012-10-24 04:14:18 +0000275 SDValue reduceBuildVecConvertToConvertBuildVec(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000276
Dan Gohman475871a2008-07-27 21:46:04 +0000277 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000278
Jim Laskey6ff23e52006-10-04 16:53:27 +0000279 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
280 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000281 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
282 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000283
Jim Laskey096c22e2006-10-18 12:29:57 +0000284 /// isAlias - Return true if there is any possibility that the two addresses
285 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000286 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000287 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000288 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000289 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +0000290 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000291 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000292 unsigned SrcValueAlign2,
293 const MDNode *TBAAInfo2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000294
Nadav Rotem90e11dc2012-11-29 00:00:08 +0000295 /// isAlias - Return true if there is any possibility that the two addresses
296 /// overlap.
297 bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1);
298
Jim Laskey7ca56af2006-10-11 13:47:09 +0000299 /// FindAliasInfo - Extracts the relevant alias information from the memory
300 /// node. Returns true if the operand was a load.
301 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000302 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000303 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000304 unsigned &SrcValueAlignment,
305 const MDNode *&TBAAInfo) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000306
Jim Laskey279f0532006-09-25 16:29:54 +0000307 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000308 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000309 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000310
Nadav Rotemc653de62012-10-03 16:11:15 +0000311 /// Merge consecutive store operations into a wide store.
312 /// This optimization uses wide integers or vectors when possible.
313 /// \return True if some memory operations were changed.
314 bool MergeConsecutiveStores(StoreSDNode *N);
315
Chris Lattner2392ae72010-04-15 04:48:01 +0000316 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000317 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Eli Friedman50185242011-11-12 00:35:34 +0000318 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
Chris Lattner2392ae72010-04-15 04:48:01 +0000319 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000320
Nate Begeman1d4d4142005-09-01 00:19:25 +0000321 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000322 void Run(CombineLevel AtLevel);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000323
Chris Lattner2392ae72010-04-15 04:48:01 +0000324 SelectionDAG &getDAG() const { return DAG; }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000325
Chris Lattner2392ae72010-04-15 04:48:01 +0000326 /// getShiftAmountTy - Returns a type large enough to hold any valid
327 /// shift amount - before type legalization these can be huge.
Owen Anderson95771af2011-02-25 21:41:48 +0000328 EVT getShiftAmountTy(EVT LHSTy) {
Elena Demikhovsky87070fe2013-06-26 10:55:03 +0000329 assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
330 if (LHSTy.isVector())
331 return LHSTy;
332 return LegalTypes ? TLI.getScalarShiftAmountTy(LHSTy) : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000333 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000334
Chris Lattner2392ae72010-04-15 04:48:01 +0000335 /// isTypeLegal - This method returns true if we are running before type
336 /// legalization or if the specified VT is legal.
337 bool isTypeLegal(const EVT &VT) {
338 if (!LegalTypes) return true;
339 return TLI.isTypeLegal(VT);
340 }
Matt Arsenault225ed702013-05-18 00:21:46 +0000341
342 /// getSetCCResultType - Convenience wrapper around
343 /// TargetLowering::getSetCCResultType
344 EVT getSetCCResultType(EVT VT) const {
345 return TLI.getSetCCResultType(*DAG.getContext(), VT);
346 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000347 };
348}
349
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000350
351namespace {
352/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
353/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000354class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000355 DAGCombiner &DC;
356public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000357 explicit WorkListRemover(DAGCombiner &dc)
358 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000359
Duncan Sandsedfcf592008-06-11 11:42:12 +0000360 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000361 DC.removeFromWorkList(N);
362 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000363};
364}
365
Chris Lattner24664722006-03-01 04:53:38 +0000366//===----------------------------------------------------------------------===//
367// TargetLowering::DAGCombinerInfo implementation
368//===----------------------------------------------------------------------===//
369
370void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
371 ((DAGCombiner*)DC)->AddToWorkList(N);
372}
373
Cameron Zwariched3caf92011-04-02 02:40:26 +0000374void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
375 ((DAGCombiner*)DC)->removeFromWorkList(N);
376}
377
Dan Gohman475871a2008-07-27 21:46:04 +0000378SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000379CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
380 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000381}
382
Dan Gohman475871a2008-07-27 21:46:04 +0000383SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000384CombineTo(SDNode *N, SDValue Res, bool AddTo) {
385 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000386}
387
388
Dan Gohman475871a2008-07-27 21:46:04 +0000389SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000390CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
391 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000392}
393
Dan Gohmane5af2d32009-01-29 01:59:02 +0000394void TargetLowering::DAGCombinerInfo::
395CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
396 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
397}
Chris Lattner24664722006-03-01 04:53:38 +0000398
Chris Lattner24664722006-03-01 04:53:38 +0000399//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000400// Helper Functions
401//===----------------------------------------------------------------------===//
402
403/// isNegatibleForFree - Return 1 if we can compute the negated form of the
404/// specified expression for the same cost as the expression itself, or 2 if we
405/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000406static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000407 const TargetLowering &TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000408 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000409 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000410 // fneg is removable even if it has multiple uses.
411 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000412
Chris Lattner29446522007-05-14 22:04:50 +0000413 // Don't allow anything with multiple uses.
414 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000415
Chris Lattner3adf9512007-05-25 02:19:06 +0000416 // Don't recurse exponentially.
417 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000418
Chris Lattner29446522007-05-14 22:04:50 +0000419 switch (Op.getOpcode()) {
420 default: return false;
421 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000422 // Don't invert constant FP values after legalize. The negated constant
423 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000424 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000425 case ISD::FADD:
426 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000427 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000428
Owen Andersonafd3d562012-03-06 00:29:31 +0000429 // After operation legalization, it might not be legal to create new FSUBs.
430 if (LegalOperations &&
431 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
432 return 0;
433
Craig Topper956342b2012-09-09 22:58:45 +0000434 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Andersonafd3d562012-03-06 00:29:31 +0000435 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
436 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000437 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000438 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Andersonafd3d562012-03-06 00:29:31 +0000439 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000440 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000441 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000442 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000443 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000444
Bill Wendlingd34470c2009-01-30 23:10:18 +0000445 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000446 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000447
Chris Lattner29446522007-05-14 22:04:50 +0000448 case ISD::FMUL:
449 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000450 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000451
Bill Wendlingd34470c2009-01-30 23:10:18 +0000452 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Andersonafd3d562012-03-06 00:29:31 +0000453 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
454 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000455 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000456
Owen Andersonafd3d562012-03-06 00:29:31 +0000457 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000458 Depth + 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000459
Chris Lattner29446522007-05-14 22:04:50 +0000460 case ISD::FP_EXTEND:
461 case ISD::FP_ROUND:
462 case ISD::FSIN:
Owen Andersonafd3d562012-03-06 00:29:31 +0000463 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000464 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000465 }
466}
467
468/// GetNegatedExpression - If isNegatibleForFree returns true, this function
469/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000470static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000471 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000472 // fneg is removable even if it has multiple uses.
473 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000474
Chris Lattner29446522007-05-14 22:04:50 +0000475 // Don't allow anything with multiple uses.
476 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000477
Chris Lattner3adf9512007-05-25 02:19:06 +0000478 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000479 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000480 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000481 case ISD::ConstantFP: {
482 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
483 V.changeSign();
484 return DAG.getConstantFP(V, Op.getValueType());
485 }
Chris Lattner29446522007-05-14 22:04:50 +0000486 case ISD::FADD:
487 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000488 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000489
Bill Wendlingd34470c2009-01-30 23:10:18 +0000490 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000491 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000492 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000493 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000494 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000495 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000496 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000497 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000498 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000499 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000500 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000501 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000502 Op.getOperand(0));
503 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000504 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000505 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000506
Bill Wendlingd34470c2009-01-30 23:10:18 +0000507 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000508 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000509 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000510 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000511
Bill Wendlingd34470c2009-01-30 23:10:18 +0000512 // fold (fneg (fsub A, B)) -> (fsub B, A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000513 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Bill Wendling35247c32009-01-30 00:45:56 +0000514 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000515
Chris Lattner29446522007-05-14 22:04:50 +0000516 case ISD::FMUL:
517 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000518 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000519
Bill Wendlingd34470c2009-01-30 23:10:18 +0000520 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000521 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000522 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000523 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000524 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000525 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000526 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000527 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000528
Bill Wendlingd34470c2009-01-30 23:10:18 +0000529 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000530 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000531 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000532 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000533 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000534
Chris Lattner29446522007-05-14 22:04:50 +0000535 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000536 case ISD::FSIN:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000537 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000538 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000539 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000540 case ISD::FP_ROUND:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000541 return DAG.getNode(ISD::FP_ROUND, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000542 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000543 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000544 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000545 }
546}
Chris Lattner24664722006-03-01 04:53:38 +0000547
548
Nate Begeman4ebd8052005-09-01 23:24:04 +0000549// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
550// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000551// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000552// nodes based on the type of node we are checking. This simplifies life a
553// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000554static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
555 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000556 if (N.getOpcode() == ISD::SETCC) {
557 LHS = N.getOperand(0);
558 RHS = N.getOperand(1);
559 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000560 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000561 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000562 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000563 N.getOperand(2).getOpcode() == ISD::Constant &&
564 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000565 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000566 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
567 LHS = N.getOperand(0);
568 RHS = N.getOperand(1);
569 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000570 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000571 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000572 return false;
573}
574
Nate Begeman99801192005-09-07 23:25:52 +0000575// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
576// one use. If this is true, it allows the users to invert the operation for
577// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000578static bool isOneUseSetCC(SDValue N) {
579 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000580 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000581 return true;
582 return false;
583}
584
Andrew Trickac6d9be2013-05-25 02:42:55 +0000585SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDLoc DL,
Bill Wendling35247c32009-01-30 00:45:56 +0000586 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000587 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000588 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
589 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000590 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000591 SDValue OpNode =
592 DAG.FoldConstantArithmetic(Opc, VT,
593 cast<ConstantSDNode>(N0.getOperand(1)),
594 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000595 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000596 }
597 if (N0.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000598 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000599 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000600 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000601 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000602 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000603 }
604 }
Bill Wendling35247c32009-01-30 00:45:56 +0000605
Nate Begemancd4d58c2006-02-03 06:46:56 +0000606 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
607 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000608 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000609 SDValue OpNode =
610 DAG.FoldConstantArithmetic(Opc, VT,
611 cast<ConstantSDNode>(N1.getOperand(1)),
612 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000613 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000614 }
615 if (N1.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000616 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000617 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000618 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000619 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000620 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000621 }
622 }
Bill Wendling35247c32009-01-30 00:45:56 +0000623
Dan Gohman475871a2008-07-27 21:46:04 +0000624 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000625}
626
Dan Gohman475871a2008-07-27 21:46:04 +0000627SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
628 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000629 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
630 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000631 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000632 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000633 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000634 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000635 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000636 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000637 assert((!To[i].getNode() ||
638 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000639 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000640 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000641 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000642 if (AddTo) {
643 // Push the new nodes and any users onto the worklist
644 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000645 if (To[i].getNode()) {
646 AddToWorkList(To[i].getNode());
647 AddUsersToWorkList(To[i].getNode());
648 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000649 }
650 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000651
Dan Gohmandbe664a2009-01-19 21:44:21 +0000652 // Finally, if the node is now dead, remove it from the graph. The node
653 // may not be dead if the replacement process recursively simplified to
654 // something else needing this node.
655 if (N->use_empty()) {
656 // Nodes can be reintroduced into the worklist. Make sure we do not
657 // process a node that has been replaced.
658 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000659
Dan Gohmandbe664a2009-01-19 21:44:21 +0000660 // Finally, since the node is now dead, remove it from the graph.
661 DAG.DeleteNode(N);
662 }
Dan Gohman475871a2008-07-27 21:46:04 +0000663 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000664}
665
Evan Chenge5b51ac2010-04-17 06:13:15 +0000666void DAGCombiner::
667CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000668 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000669 // are deleted, make sure to remove them from our worklist.
670 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000671 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000672
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000673 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000674 AddToWorkList(TLO.New.getNode());
675 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000676
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000677 // Finally, if the node is now dead, remove it from the graph. The node
678 // may not be dead if the replacement process recursively simplified to
679 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000680 if (TLO.Old.getNode()->use_empty()) {
681 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000682
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000683 // If the operands of this node are only used by the node, they will now
684 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000685 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
686 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
687 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000688
Gabor Greifba36cb52008-08-28 21:40:38 +0000689 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000690 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000691}
692
693/// SimplifyDemandedBits - Check the specified integer node value to see if
694/// it can be simplified or if things it uses can be simplified by bit
695/// propagation. If so, return true.
696bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000697 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000698 APInt KnownZero, KnownOne;
699 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
700 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000701
Dan Gohmane5af2d32009-01-29 01:59:02 +0000702 // Revisit the node.
703 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000704
Dan Gohmane5af2d32009-01-29 01:59:02 +0000705 // Replace the old value with the new one.
706 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000707 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000708 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000709 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000710 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000711 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000712
Dan Gohmane5af2d32009-01-29 01:59:02 +0000713 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000714 return true;
715}
716
Evan Cheng95c57ea2010-04-24 04:43:44 +0000717void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000718 SDLoc dl(Load);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000719 EVT VT = Load->getValueType(0);
720 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000721
Evan Cheng95c57ea2010-04-24 04:43:44 +0000722 DEBUG(dbgs() << "\nReplacing.9 ";
723 Load->dump(&DAG);
724 dbgs() << "\nWith: ";
725 Trunc.getNode()->dump(&DAG);
726 dbgs() << '\n');
727 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000728 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
729 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng95c57ea2010-04-24 04:43:44 +0000730 removeFromWorkList(Load);
731 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000732 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000733}
734
735SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
736 Replace = false;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000737 SDLoc dl(Op);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000738 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000739 EVT MemVT = LD->getMemoryVT();
740 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000741 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000742 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000743 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000744 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000745 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000746 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000747 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000748 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000749 LD->isNonTemporal(), LD->getAlignment());
750 }
751
Evan Cheng4c26e932010-04-19 19:29:22 +0000752 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000753 switch (Opc) {
754 default: break;
755 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000756 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000757 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000758 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000759 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000760 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000761 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000762 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000763 case ISD::Constant: {
764 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000765 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000766 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000767 }
Evan Chengcaf77402010-04-23 19:10:30 +0000768 }
769
770 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000771 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000772 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000773}
774
Evan Cheng95c57ea2010-04-24 04:43:44 +0000775SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000776 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
777 return SDValue();
778 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000779 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000780 bool Replace = false;
781 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
782 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000783 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000784 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000785
786 if (Replace)
787 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
788 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000789 DAG.getValueType(OldVT));
790}
791
Evan Cheng95c57ea2010-04-24 04:43:44 +0000792SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000793 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000794 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000795 bool Replace = false;
796 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
797 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000798 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000799 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000800
801 if (Replace)
802 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
803 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000804}
805
Evan Cheng64b7bf72010-04-16 06:14:10 +0000806/// PromoteIntBinOp - Promote the specified integer binary operation if the
807/// target indicates it is beneficial. e.g. On x86, it's usually better to
808/// promote i16 operations to i32 since i16 instructions are longer.
809SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
810 if (!LegalOperations)
811 return SDValue();
812
813 EVT VT = Op.getValueType();
814 if (VT.isVector() || !VT.isInteger())
815 return SDValue();
816
Evan Chenge5b51ac2010-04-17 06:13:15 +0000817 // If operation type is 'undesirable', e.g. i16 on x86, consider
818 // promoting it.
819 unsigned Opc = Op.getOpcode();
820 if (TLI.isTypeDesirableForOp(Opc, VT))
821 return SDValue();
822
Evan Cheng64b7bf72010-04-16 06:14:10 +0000823 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000824 // Consult target whether it is a good idea to promote this operation and
825 // what's the right type to promote it to.
826 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000827 assert(PVT != VT && "Don't know what type to promote to!");
828
Evan Cheng95c57ea2010-04-24 04:43:44 +0000829 bool Replace0 = false;
830 SDValue N0 = Op.getOperand(0);
831 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
832 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000833 return SDValue();
834
Evan Cheng95c57ea2010-04-24 04:43:44 +0000835 bool Replace1 = false;
836 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000837 SDValue NN1;
838 if (N0 == N1)
839 NN1 = NN0;
840 else {
841 NN1 = PromoteOperand(N1, PVT, Replace1);
842 if (NN1.getNode() == 0)
843 return SDValue();
844 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000845
Evan Cheng95c57ea2010-04-24 04:43:44 +0000846 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000847 if (NN1.getNode())
848 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000849
850 if (Replace0)
851 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
852 if (Replace1)
853 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000854
Evan Chengac7eae52010-04-27 19:48:13 +0000855 DEBUG(dbgs() << "\nPromoting ";
856 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000857 SDLoc dl(Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000858 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000859 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000860 }
861 return SDValue();
862}
863
864/// PromoteIntShiftOp - Promote the specified integer shift operation if the
865/// target indicates it is beneficial. e.g. On x86, it's usually better to
866/// promote i16 operations to i32 since i16 instructions are longer.
867SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
868 if (!LegalOperations)
869 return SDValue();
870
871 EVT VT = Op.getValueType();
872 if (VT.isVector() || !VT.isInteger())
873 return SDValue();
874
875 // If operation type is 'undesirable', e.g. i16 on x86, consider
876 // promoting it.
877 unsigned Opc = Op.getOpcode();
878 if (TLI.isTypeDesirableForOp(Opc, VT))
879 return SDValue();
880
881 EVT PVT = VT;
882 // Consult target whether it is a good idea to promote this operation and
883 // what's the right type to promote it to.
884 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
885 assert(PVT != VT && "Don't know what type to promote to!");
886
Evan Cheng95c57ea2010-04-24 04:43:44 +0000887 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000888 SDValue N0 = Op.getOperand(0);
889 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000890 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000891 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000892 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000893 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000894 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000895 if (N0.getNode() == 0)
896 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000897
Evan Chenge5b51ac2010-04-17 06:13:15 +0000898 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000899 if (Replace)
900 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000901
Evan Chengac7eae52010-04-27 19:48:13 +0000902 DEBUG(dbgs() << "\nPromoting ";
903 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000904 SDLoc dl(Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000905 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000906 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000907 }
908 return SDValue();
909}
910
Evan Cheng4c26e932010-04-19 19:29:22 +0000911SDValue DAGCombiner::PromoteExtend(SDValue Op) {
912 if (!LegalOperations)
913 return SDValue();
914
915 EVT VT = Op.getValueType();
916 if (VT.isVector() || !VT.isInteger())
917 return SDValue();
918
919 // If operation type is 'undesirable', e.g. i16 on x86, consider
920 // promoting it.
921 unsigned Opc = Op.getOpcode();
922 if (TLI.isTypeDesirableForOp(Opc, VT))
923 return SDValue();
924
925 EVT PVT = VT;
926 // Consult target whether it is a good idea to promote this operation and
927 // what's the right type to promote it to.
928 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
929 assert(PVT != VT && "Don't know what type to promote to!");
930 // fold (aext (aext x)) -> (aext x)
931 // fold (aext (zext x)) -> (zext x)
932 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000933 DEBUG(dbgs() << "\nPromoting ";
934 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000935 return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000936 }
937 return SDValue();
938}
939
940bool DAGCombiner::PromoteLoad(SDValue Op) {
941 if (!LegalOperations)
942 return false;
943
944 EVT VT = Op.getValueType();
945 if (VT.isVector() || !VT.isInteger())
946 return false;
947
948 // If operation type is 'undesirable', e.g. i16 on x86, consider
949 // promoting it.
950 unsigned Opc = Op.getOpcode();
951 if (TLI.isTypeDesirableForOp(Opc, VT))
952 return false;
953
954 EVT PVT = VT;
955 // Consult target whether it is a good idea to promote this operation and
956 // what's the right type to promote it to.
957 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
958 assert(PVT != VT && "Don't know what type to promote to!");
959
Andrew Trickac6d9be2013-05-25 02:42:55 +0000960 SDLoc dl(Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000961 SDNode *N = Op.getNode();
962 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000963 EVT MemVT = LD->getMemoryVT();
964 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000965 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000966 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000967 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000968 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000969 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000970 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000971 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000972 LD->isNonTemporal(), LD->getAlignment());
973 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
974
Evan Cheng95c57ea2010-04-24 04:43:44 +0000975 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000976 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000977 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000978 Result.getNode()->dump(&DAG);
979 dbgs() << '\n');
980 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000981 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
982 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Cheng4c26e932010-04-19 19:29:22 +0000983 removeFromWorkList(N);
984 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000985 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +0000986 return true;
987 }
988 return false;
989}
990
Evan Chenge5b51ac2010-04-17 06:13:15 +0000991
Chris Lattner29446522007-05-14 22:04:50 +0000992//===----------------------------------------------------------------------===//
993// Main DAG Combiner implementation
994//===----------------------------------------------------------------------===//
995
Duncan Sands25cf2272008-11-24 14:53:14 +0000996void DAGCombiner::Run(CombineLevel AtLevel) {
997 // set the instance variables, so that the various visit routines may use it.
998 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +0000999 LegalOperations = Level >= AfterLegalizeVectorOps;
1000 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +00001001
Evan Cheng17a568b2008-08-29 22:21:44 +00001002 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +00001003 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
1004 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +00001005 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +00001006
Evan Cheng17a568b2008-08-29 22:21:44 +00001007 // Create a dummy node (which is not added to allnodes), that adds a reference
1008 // to the root node, preventing it from being deleted, and tracking any
1009 // changes of the root.
1010 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +00001011
Jim Laskey26f7fa72006-10-17 19:33:52 +00001012 // The root of the dag may dangle to deleted nodes until the dag combiner is
1013 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +00001014 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001015
James Molloy6660c052012-02-16 09:17:04 +00001016 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +00001017 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +00001018 while (!WorkListContents.empty()) {
1019 SDNode *N;
1020 // The WorkListOrder holds the SDNodes in order, but it may contain duplicates.
1021 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1022 // worklist *should* contain, and check the node we want to visit is should
1023 // actually be visited.
1024 do {
Benjamin Kramerd5f76902012-03-10 00:23:58 +00001025 N = WorkListOrder.pop_back_val();
James Molloy6660c052012-02-16 09:17:04 +00001026 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001027
Evan Cheng17a568b2008-08-29 22:21:44 +00001028 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1029 // N is deleted from the DAG, since they too may now be dead or may have a
1030 // reduced number of uses, allowing other xforms.
1031 if (N->use_empty() && N != &Dummy) {
1032 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1033 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001034
Evan Cheng17a568b2008-08-29 22:21:44 +00001035 DAG.DeleteNode(N);
1036 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001037 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001038
Evan Cheng17a568b2008-08-29 22:21:44 +00001039 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001040
Evan Cheng17a568b2008-08-29 22:21:44 +00001041 if (RV.getNode() == 0)
1042 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001043
Evan Cheng17a568b2008-08-29 22:21:44 +00001044 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001045
Evan Cheng17a568b2008-08-29 22:21:44 +00001046 // If we get back the same node we passed in, rather than a new node or
1047 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001048 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001049 // mechanics for us, we have no work to do in this case.
1050 if (RV.getNode() == N)
1051 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001052
Evan Cheng17a568b2008-08-29 22:21:44 +00001053 assert(N->getOpcode() != ISD::DELETED_NODE &&
1054 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1055 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001056
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001057 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001058 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001059 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001060 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001061 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001062
Devang Patel9728ea22011-05-23 22:04:42 +00001063 // Transfer debug value.
1064 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001065 WorkListRemover DeadNodes(*this);
1066 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001067 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng17a568b2008-08-29 22:21:44 +00001068 else {
1069 assert(N->getValueType(0) == RV.getValueType() &&
1070 N->getNumValues() == 1 && "Type mismatch");
1071 SDValue OpV = RV;
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001072 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001073 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001074
Evan Cheng17a568b2008-08-29 22:21:44 +00001075 // Push the new node and any users onto the worklist
1076 AddToWorkList(RV.getNode());
1077 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001078
Evan Cheng17a568b2008-08-29 22:21:44 +00001079 // Add any uses of the old node to the worklist in case this node is the
1080 // last one that uses them. They may become dead after this node is
1081 // deleted.
1082 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1083 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001084
Dan Gohmandbe664a2009-01-19 21:44:21 +00001085 // Finally, if the node is now dead, remove it from the graph. The node
1086 // may not be dead if the replacement process recursively simplified to
1087 // something else needing this node.
1088 if (N->use_empty()) {
1089 // Nodes can be reintroduced into the worklist. Make sure we do not
1090 // process a node that has been replaced.
1091 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001092
Dan Gohmandbe664a2009-01-19 21:44:21 +00001093 // Finally, since the node is now dead, remove it from the graph.
1094 DAG.DeleteNode(N);
1095 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001096 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001097
Chris Lattner95038592005-10-05 06:35:28 +00001098 // If the root changed (e.g. it was a dead load, update the root).
1099 DAG.setRoot(Dummy.getValue());
Hal Finkel31490ba2012-04-16 03:33:22 +00001100 DAG.RemoveDeadNodes();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001101}
1102
Dan Gohman475871a2008-07-27 21:46:04 +00001103SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001104 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001105 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001106 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001107 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001108 case ISD::ADD: return visitADD(N);
1109 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001110 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001111 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001112 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001113 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001114 case ISD::MUL: return visitMUL(N);
1115 case ISD::SDIV: return visitSDIV(N);
1116 case ISD::UDIV: return visitUDIV(N);
1117 case ISD::SREM: return visitSREM(N);
1118 case ISD::UREM: return visitUREM(N);
1119 case ISD::MULHU: return visitMULHU(N);
1120 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001121 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1122 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001123 case ISD::SMULO: return visitSMULO(N);
1124 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001125 case ISD::SDIVREM: return visitSDIVREM(N);
1126 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001127 case ISD::AND: return visitAND(N);
1128 case ISD::OR: return visitOR(N);
1129 case ISD::XOR: return visitXOR(N);
1130 case ISD::SHL: return visitSHL(N);
1131 case ISD::SRA: return visitSRA(N);
1132 case ISD::SRL: return visitSRL(N);
1133 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001134 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001135 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001136 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001137 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001138 case ISD::SELECT: return visitSELECT(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00001139 case ISD::VSELECT: return visitVSELECT(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001140 case ISD::SELECT_CC: return visitSELECT_CC(N);
1141 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001142 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1143 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001144 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001145 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1146 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001147 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001148 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001149 case ISD::FADD: return visitFADD(N);
1150 case ISD::FSUB: return visitFSUB(N);
1151 case ISD::FMUL: return visitFMUL(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00001152 case ISD::FMA: return visitFMA(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001153 case ISD::FDIV: return visitFDIV(N);
1154 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001155 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001156 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1157 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1158 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1159 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1160 case ISD::FP_ROUND: return visitFP_ROUND(N);
1161 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1162 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1163 case ISD::FNEG: return visitFNEG(N);
1164 case ISD::FABS: return visitFABS(N);
Owen Anderson7c626d32012-08-13 23:32:49 +00001165 case ISD::FFLOOR: return visitFFLOOR(N);
1166 case ISD::FCEIL: return visitFCEIL(N);
1167 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001168 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001169 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001170 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001171 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001172 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001173 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001174 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1175 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001176 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001177 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 }
Dan Gohman475871a2008-07-27 21:46:04 +00001179 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001180}
1181
Dan Gohman475871a2008-07-27 21:46:04 +00001182SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001183 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001184
1185 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001186 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001187 assert(N->getOpcode() != ISD::DELETED_NODE &&
1188 "Node was deleted but visit returned NULL!");
1189
1190 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1191 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1192
1193 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001194 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +00001195 DagCombineInfo(DAG, Level, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001196
1197 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1198 }
1199 }
1200
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001201 // If nothing happened still, try promoting the operation.
1202 if (RV.getNode() == 0) {
1203 switch (N->getOpcode()) {
1204 default: break;
1205 case ISD::ADD:
1206 case ISD::SUB:
1207 case ISD::MUL:
1208 case ISD::AND:
1209 case ISD::OR:
1210 case ISD::XOR:
1211 RV = PromoteIntBinOp(SDValue(N, 0));
1212 break;
1213 case ISD::SHL:
1214 case ISD::SRA:
1215 case ISD::SRL:
1216 RV = PromoteIntShiftOp(SDValue(N, 0));
1217 break;
1218 case ISD::SIGN_EXTEND:
1219 case ISD::ZERO_EXTEND:
1220 case ISD::ANY_EXTEND:
1221 RV = PromoteExtend(SDValue(N, 0));
1222 break;
1223 case ISD::LOAD:
1224 if (PromoteLoad(SDValue(N, 0)))
1225 RV = SDValue(N, 0);
1226 break;
1227 }
1228 }
1229
Scott Michelfdc40a02009-02-17 22:15:04 +00001230 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001231 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001232 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001233 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1234 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001235 SDValue N0 = N->getOperand(0);
1236 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001237
Evan Cheng08b11732008-03-22 01:55:50 +00001238 // Constant operands are canonicalized to RHS.
1239 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001240 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001241 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1242 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001243 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001244 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001245 }
1246 }
1247
Dan Gohman389079b2007-10-08 17:57:15 +00001248 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001249}
Dan Gohman389079b2007-10-08 17:57:15 +00001250
Chris Lattner6270f682006-10-08 22:57:01 +00001251/// getInputChainForNode - Given a node, return its input chain if it has one,
1252/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001253static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001254 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001255 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001256 return N->getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001258 return N->getOperand(NumOps-1);
1259 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001260 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001261 return N->getOperand(i);
1262 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001263 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001264}
1265
Dan Gohman475871a2008-07-27 21:46:04 +00001266SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001267 // If N has two operands, where one has an input chain equal to the other,
1268 // the 'other' chain is redundant.
1269 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001270 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001271 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001272 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001273 return N->getOperand(1);
1274 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001275
Chris Lattnerc76d4412007-05-16 06:37:59 +00001276 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001277 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001278 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001279 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001280
Jim Laskey6ff23e52006-10-04 16:53:27 +00001281 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001282 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001283
Jim Laskey71382342006-10-07 23:37:56 +00001284 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001285 // encountered.
1286 for (unsigned i = 0; i < TFs.size(); ++i) {
1287 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001288
Jim Laskey6ff23e52006-10-04 16:53:27 +00001289 // Check each of the operands.
1290 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001291 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001292
Jim Laskey6ff23e52006-10-04 16:53:27 +00001293 switch (Op.getOpcode()) {
1294 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001295 // Entry tokens don't need to be added to the list. They are
1296 // rededundant.
1297 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001298 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001299
Jim Laskey6ff23e52006-10-04 16:53:27 +00001300 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001301 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001302 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001303 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001304 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001305 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001306 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001307 Changed = true;
1308 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001309 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001310 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001311
Jim Laskey6ff23e52006-10-04 16:53:27 +00001312 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001313 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001314 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001315 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001316 else
1317 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001318 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001319 }
1320 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001321 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001322
Dan Gohman475871a2008-07-27 21:46:04 +00001323 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001324
1325 // If we've change things around then replace token factor.
1326 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001327 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001328 // The entry token is the only possible outcome.
1329 Result = DAG.getEntryNode();
1330 } else {
1331 // New and improved token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001332 Result = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00001333 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001334 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001335
Jim Laskey274062c2006-10-13 23:32:28 +00001336 // Don't add users to work list.
1337 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001338 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001339
Jim Laskey6ff23e52006-10-04 16:53:27 +00001340 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001341}
1342
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001343/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001344SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001345 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001346 // Replacing results may cause a different MERGE_VALUES to suddenly
1347 // be CSE'd with N, and carry its uses with it. Iterate until no
1348 // uses remain, to ensure that the node can be safely deleted.
Pete Cooper3affd9e2012-06-20 19:35:43 +00001349 // First add the users of this node to the work list so that they
1350 // can be tried again once they have new operands.
1351 AddUsersToWorkList(N);
Dan Gohman00edf392009-08-10 23:43:19 +00001352 do {
1353 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001354 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman00edf392009-08-10 23:43:19 +00001355 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001356 removeFromWorkList(N);
1357 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001358 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001359}
1360
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001361static
Andrew Trickac6d9be2013-05-25 02:42:55 +00001362SDValue combineShlAddConstant(SDLoc DL, SDValue N0, SDValue N1,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001363 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001364 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001365 SDValue N00 = N0.getOperand(0);
1366 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001367 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001368
Gabor Greifba36cb52008-08-28 21:40:38 +00001369 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001370 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001371 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Andrew Trickac6d9be2013-05-25 02:42:55 +00001372 N0 = DAG.getNode(ISD::ADD, SDLoc(N0), VT,
1373 DAG.getNode(ISD::SHL, SDLoc(N00), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001374 N00.getOperand(0), N01),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001375 DAG.getNode(ISD::SHL, SDLoc(N01), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001376 N00.getOperand(1), N01));
1377 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001378 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001379
Dan Gohman475871a2008-07-27 21:46:04 +00001380 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001381}
1382
Dan Gohman475871a2008-07-27 21:46:04 +00001383SDValue DAGCombiner::visitADD(SDNode *N) {
1384 SDValue N0 = N->getOperand(0);
1385 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001386 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1387 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001388 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001389
1390 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001391 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001392 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001393 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001394
1395 // fold (add x, 0) -> x, vector edition
1396 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1397 return N0;
1398 if (ISD::isBuildVectorAllZeros(N0.getNode()))
1399 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001400 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001401
Dan Gohman613e0d82007-07-03 14:03:57 +00001402 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001403 if (N0.getOpcode() == ISD::UNDEF)
1404 return N0;
1405 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001406 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001407 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001408 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001409 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001410 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001411 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001412 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001413 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001414 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001415 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001416 // fold (add Sym, c) -> Sym+c
1417 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001418 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001419 GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001420 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001421 GA->getOffset() +
1422 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001423 // fold ((c1-A)+c2) -> (c1+c2)-A
1424 if (N1C && N0.getOpcode() == ISD::SUB)
1425 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001426 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001427 DAG.getConstant(N1C->getAPIntValue()+
1428 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001429 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001430 // reassociate add
Andrew Trickac6d9be2013-05-25 02:42:55 +00001431 SDValue RADD = ReassociateOps(ISD::ADD, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001432 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001433 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001434 // fold ((0-A) + B) -> B-A
1435 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1436 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001437 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001438 // fold (A + (0-B)) -> A-B
1439 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1440 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001441 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001442 // fold (A+(B-A)) -> B
1443 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001444 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001445 // fold ((B-A)+A) -> B
1446 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1447 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001448 // fold (A+(B-(A+C))) to (B-C)
1449 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001450 N0 == N1.getOperand(1).getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001451 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001452 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001453 // fold (A+(B-(C+A))) to (B-C)
1454 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001455 N0 == N1.getOperand(1).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001456 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001457 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001458 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001459 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1460 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001461 N0 == N1.getOperand(0).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001462 return DAG.getNode(N1.getOpcode(), SDLoc(N), VT,
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001463 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001464
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001465 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1466 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1467 SDValue N00 = N0.getOperand(0);
1468 SDValue N01 = N0.getOperand(1);
1469 SDValue N10 = N1.getOperand(0);
1470 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001471
1472 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001473 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
1474 DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10),
1475 DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001476 }
Chris Lattner947c2892006-03-13 06:51:27 +00001477
Dan Gohman475871a2008-07-27 21:46:04 +00001478 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1479 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001480
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001481 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001482 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001483 APInt LHSZero, LHSOne;
1484 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001485 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001486
Dan Gohman948d8ea2008-02-20 16:33:30 +00001487 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001488 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001489
Chris Lattner947c2892006-03-13 06:51:27 +00001490 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1491 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001492 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001493 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001494 }
1495 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001496
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001497 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001498 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001499 SDValue Result = combineShlAddConstant(SDLoc(N), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001500 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001501 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001502 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001503 SDValue Result = combineShlAddConstant(SDLoc(N), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001504 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001505 }
1506
Dan Gohmancd9e1552010-01-19 23:30:49 +00001507 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1508 if (N1.getOpcode() == ISD::SHL &&
1509 N1.getOperand(0).getOpcode() == ISD::SUB)
1510 if (ConstantSDNode *C =
1511 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1512 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001513 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0,
1514 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001515 N1.getOperand(0).getOperand(1),
1516 N1.getOperand(1)));
1517 if (N0.getOpcode() == ISD::SHL &&
1518 N0.getOperand(0).getOpcode() == ISD::SUB)
1519 if (ConstantSDNode *C =
1520 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1521 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001522 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1,
1523 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001524 N0.getOperand(0).getOperand(1),
1525 N0.getOperand(1)));
1526
Owen Andersonbc146b02010-09-21 20:42:50 +00001527 if (N1.getOpcode() == ISD::AND) {
1528 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001529 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001530 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1531 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001532
Owen Andersonbc146b02010-09-21 20:42:50 +00001533 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1534 // and similar xforms where the inner op is either ~0 or 0.
1535 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001536 SDLoc DL(N);
Owen Andersonbc146b02010-09-21 20:42:50 +00001537 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1538 }
1539 }
1540
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001541 // add (sext i1), X -> sub X, (zext i1)
1542 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1543 N0.getOperand(0).getValueType() == MVT::i1 &&
1544 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001545 SDLoc DL(N);
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001546 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1547 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1548 }
1549
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001550 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001551}
1552
Dan Gohman475871a2008-07-27 21:46:04 +00001553SDValue DAGCombiner::visitADDC(SDNode *N) {
1554 SDValue N0 = N->getOperand(0);
1555 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001556 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1557 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001558 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001559
Chris Lattner91153682007-03-04 20:03:15 +00001560 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001561 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001562 return CombineTo(N, DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001563 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001564 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001565
Chris Lattner91153682007-03-04 20:03:15 +00001566 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001567 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001568 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001569
Chris Lattnerb6541762007-03-04 20:40:38 +00001570 // fold (addc x, 0) -> x + no carry out
1571 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001572 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001573 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001574
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001575 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001576 APInt LHSZero, LHSOne;
1577 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001578 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001579
Dan Gohman948d8ea2008-02-20 16:33:30 +00001580 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001581 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001582
Chris Lattnerb6541762007-03-04 20:40:38 +00001583 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1584 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001585 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001586 return CombineTo(N, DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001587 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001588 SDLoc(N), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001589 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001590
Dan Gohman475871a2008-07-27 21:46:04 +00001591 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001592}
1593
Dan Gohman475871a2008-07-27 21:46:04 +00001594SDValue DAGCombiner::visitADDE(SDNode *N) {
1595 SDValue N0 = N->getOperand(0);
1596 SDValue N1 = N->getOperand(1);
1597 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001598 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1599 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001600
Chris Lattner91153682007-03-04 20:03:15 +00001601 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001602 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001603 return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(),
Bill Wendling14036c02009-01-30 02:38:00 +00001604 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001605
Chris Lattnerb6541762007-03-04 20:40:38 +00001606 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001607 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001608 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001609
Dan Gohman475871a2008-07-27 21:46:04 +00001610 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001611}
1612
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001613// Since it may not be valid to emit a fold to zero for vector initializers
1614// check if we can before folding.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001615static SDValue tryFoldToZero(SDLoc DL, const TargetLowering &TLI, EVT VT,
Owen Anderson95771af2011-02-25 21:41:48 +00001616 SelectionDAG &DAG, bool LegalOperations) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001617 if (!VT.isVector()) {
1618 return DAG.getConstant(0, VT);
Dan Gohman71dc7c92011-05-17 22:20:36 +00001619 }
1620 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001621 // Produce a vector of zeros.
1622 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
1623 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
1624 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
1625 &Ops[0], Ops.size());
1626 }
1627 return SDValue();
1628}
1629
Dan Gohman475871a2008-07-27 21:46:04 +00001630SDValue DAGCombiner::visitSUB(SDNode *N) {
1631 SDValue N0 = N->getOperand(0);
1632 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001633 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1634 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001635 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1636 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001637 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001638
Dan Gohman7f321562007-06-25 16:23:39 +00001639 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001640 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001641 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001642 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001643
1644 // fold (sub x, 0) -> x, vector edition
1645 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1646 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001647 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001648
Chris Lattner854077d2005-10-17 01:07:11 +00001649 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001650 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001651 if (N0 == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001652 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001653 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001654 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001655 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001656 // fold (sub x, c) -> (add x, -c)
1657 if (N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001658 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001659 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001660 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1661 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001662 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001663 // fold A-(A-B) -> B
1664 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1665 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001666 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001667 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001668 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001669 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001670 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001671 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001672 // fold C2-(A+C1) -> (C2-C1)-A
1673 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00001674 SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(),
1675 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001676 return DAG.getNode(ISD::SUB, SDLoc(N), VT, NewC,
Bill Wendling96cb1122012-07-19 00:04:14 +00001677 N1.getOperand(0));
Eric Christopher7332e6e2011-07-14 01:12:15 +00001678 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001679 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001680 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001681 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1682 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001683 N0.getOperand(1).getOperand(0) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001684 return DAG.getNode(N0.getOperand(1).getOpcode(), SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001685 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001686 // fold ((A+(C+B))-B) -> A+C
1687 if (N0.getOpcode() == ISD::ADD &&
1688 N0.getOperand(1).getOpcode() == ISD::ADD &&
1689 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001690 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001691 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001692 // fold ((A-(B-C))-C) -> A-B
1693 if (N0.getOpcode() == ISD::SUB &&
1694 N0.getOperand(1).getOpcode() == ISD::SUB &&
1695 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001696 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001697 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001698
Dan Gohman613e0d82007-07-03 14:03:57 +00001699 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001700 if (N0.getOpcode() == ISD::UNDEF)
1701 return N0;
1702 if (N1.getOpcode() == ISD::UNDEF)
1703 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001704
Dan Gohman6520e202008-10-18 02:06:02 +00001705 // If the relocation model supports it, consider symbol offsets.
1706 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001707 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001708 // fold (sub Sym, c) -> Sym-c
1709 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001710 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001711 GA->getOffset() -
1712 (uint64_t)N1C->getSExtValue());
1713 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1714 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1715 if (GA->getGlobal() == GB->getGlobal())
1716 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1717 VT);
1718 }
1719
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001720 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001721}
1722
Craig Toppercc274522012-01-07 09:06:39 +00001723SDValue DAGCombiner::visitSUBC(SDNode *N) {
1724 SDValue N0 = N->getOperand(0);
1725 SDValue N1 = N->getOperand(1);
1726 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1727 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1728 EVT VT = N0.getValueType();
1729
1730 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001731 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001732 return CombineTo(N, DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1),
1733 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001734 MVT::Glue));
1735
1736 // fold (subc x, x) -> 0 + no borrow
1737 if (N0 == N1)
1738 return CombineTo(N, DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001739 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001740 MVT::Glue));
1741
1742 // fold (subc x, 0) -> x + no borrow
1743 if (N1C && N1C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001744 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001745 MVT::Glue));
1746
1747 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1748 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001749 return CombineTo(N, DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0),
1750 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001751 MVT::Glue));
1752
1753 return SDValue();
1754}
1755
1756SDValue DAGCombiner::visitSUBE(SDNode *N) {
1757 SDValue N0 = N->getOperand(0);
1758 SDValue N1 = N->getOperand(1);
1759 SDValue CarryIn = N->getOperand(2);
1760
1761 // fold (sube x, y, false) -> (subc x, y)
1762 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001763 return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1);
Craig Toppercc274522012-01-07 09:06:39 +00001764
1765 return SDValue();
1766}
1767
Elena Demikhovskyd8026702013-06-26 12:15:53 +00001768/// isConstantSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
1769/// all the same constant or undefined.
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001770static bool isConstantSplatVector(SDNode *N, APInt& SplatValue) {
1771 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(N);
1772 if (!C)
1773 return false;
1774
1775 APInt SplatUndef;
1776 unsigned SplatBitSize;
1777 bool HasAnyUndefs;
1778 EVT EltVT = N->getValueType(0).getVectorElementType();
1779 return (C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1780 HasAnyUndefs) &&
1781 EltVT.getSizeInBits() >= SplatBitSize);
1782}
1783
Dan Gohman475871a2008-07-27 21:46:04 +00001784SDValue DAGCombiner::visitMUL(SDNode *N) {
1785 SDValue N0 = N->getOperand(0);
1786 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001787 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001788
Dan Gohman613e0d82007-07-03 14:03:57 +00001789 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001790 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001791 return DAG.getConstant(0, VT);
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001792
1793 bool N0IsConst = false;
1794 bool N1IsConst = false;
1795 APInt ConstValue0, ConstValue1;
1796 // fold vector ops
1797 if (VT.isVector()) {
1798 SDValue FoldedVOp = SimplifyVBinOp(N);
1799 if (FoldedVOp.getNode()) return FoldedVOp;
1800
1801 N0IsConst = isConstantSplatVector(N0.getNode(), ConstValue0);
1802 N1IsConst = isConstantSplatVector(N1.getNode(), ConstValue1);
1803 } else {
1804 N0IsConst = dyn_cast<ConstantSDNode>(N0) != 0;
1805 ConstValue0 = N0IsConst? (dyn_cast<ConstantSDNode>(N0))->getAPIntValue() : APInt();
1806 N1IsConst = dyn_cast<ConstantSDNode>(N1) != 0;
1807 ConstValue1 = N1IsConst? (dyn_cast<ConstantSDNode>(N1))->getAPIntValue() : APInt();
1808 }
1809
Nate Begeman1d4d4142005-09-01 00:19:25 +00001810 // fold (mul c1, c2) -> c1*c2
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001811 if (N0IsConst && N1IsConst)
1812 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode());
1813
Nate Begeman99801192005-09-07 23:25:52 +00001814 // canonicalize constant to RHS
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001815 if (N0IsConst && !N1IsConst)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001816 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001817 // fold (mul x, 0) -> 0
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001818 if (N1IsConst && ConstValue1 == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001819 return N1;
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001820 // fold (mul x, 1) -> x
1821 if (N1IsConst && ConstValue1 == 1)
1822 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001823 // fold (mul x, -1) -> 0-x
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001824 if (N1IsConst && ConstValue1.isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001825 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001826 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001827 // fold (mul x, (1 << c)) -> x << c
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001828 if (N1IsConst && ConstValue1.isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001829 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001830 DAG.getConstant(ConstValue1.logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001831 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001832 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001833 if (N1IsConst && (-ConstValue1).isPowerOf2()) {
1834 unsigned Log2Val = (-ConstValue1).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001835 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001836 // single-use add), we should put the negate there.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001837 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001838 DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001839 DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001840 DAG.getConstant(Log2Val,
1841 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001842 }
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001843
1844 APInt Val;
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001845 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Stephen Lin155615d2013-07-08 00:37:03 +00001846 if (N1IsConst && N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001847 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1848 isa<ConstantSDNode>(N0.getOperand(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001849 SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001850 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001851 AddToWorkList(C3.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001852 return DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001853 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001854 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001855
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001856 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1857 // use.
1858 {
Dan Gohman475871a2008-07-27 21:46:04 +00001859 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001860 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
Stephen Lin155615d2013-07-08 00:37:03 +00001861 if (N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001862 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1863 isa<ConstantSDNode>(N0.getOperand(1))) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001864 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001865 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001866 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001867 isa<ConstantSDNode>(N1.getOperand(1)) &&
1868 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001869 Sh = N1; Y = N0;
1870 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001871
Gabor Greifba36cb52008-08-28 21:40:38 +00001872 if (Sh.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001873 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001874 Sh.getOperand(0), Y);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001875 return DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001876 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001877 }
1878 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001879
Chris Lattnera1deca32006-03-04 23:33:26 +00001880 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001881 if (N1IsConst && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
1882 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1883 isa<ConstantSDNode>(N0.getOperand(1))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001884 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
1885 DAG.getNode(ISD::MUL, SDLoc(N0), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001886 N0.getOperand(0), N1),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001887 DAG.getNode(ISD::MUL, SDLoc(N1), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001888 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001889
Nate Begemancd4d58c2006-02-03 06:46:56 +00001890 // reassociate mul
Andrew Trickac6d9be2013-05-25 02:42:55 +00001891 SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001892 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001893 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001894
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001895 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001896}
1897
Dan Gohman475871a2008-07-27 21:46:04 +00001898SDValue DAGCombiner::visitSDIV(SDNode *N) {
1899 SDValue N0 = N->getOperand(0);
1900 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001901 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1902 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001903 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001904
Dan Gohman7f321562007-06-25 16:23:39 +00001905 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001906 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001907 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001908 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001909 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001910
Nate Begeman1d4d4142005-09-01 00:19:25 +00001911 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001912 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001913 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001914 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001915 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001916 return N0;
1917 // fold (sdiv X, -1) -> 0-X
1918 if (N1C && N1C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001919 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001920 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001921 // If we know the sign bits of both operands are zero, strength reduce to a
1922 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001923 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001924 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001925 return DAG.getNode(ISD::UDIV, SDLoc(N), N1.getValueType(),
Bill Wendling944d34b2009-01-30 02:52:17 +00001926 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001927 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001928 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001929 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001930 (N1C->getAPIntValue().isPowerOf2() ||
1931 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001932 // If dividing by powers of two is cheap, then don't perform the following
1933 // fold.
1934 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001935 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001936
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001937 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001938
Chris Lattner8f4880b2006-02-16 08:02:36 +00001939 // Splat the sign bit into the register
Andrew Trickac6d9be2013-05-25 02:42:55 +00001940 SDValue SGN = DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
Bill Wendling944d34b2009-01-30 02:52:17 +00001941 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001942 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001943 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001944
Chris Lattner8f4880b2006-02-16 08:02:36 +00001945 // Add (N0 < 0) ? abs2 - 1 : 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001946 SDValue SRL = DAG.getNode(ISD::SRL, SDLoc(N), VT, SGN,
Bill Wendling944d34b2009-01-30 02:52:17 +00001947 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001948 getShiftAmountTy(SGN.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001949 SDValue ADD = DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001950 AddToWorkList(SRL.getNode());
1951 AddToWorkList(ADD.getNode()); // Divide by pow2
Andrew Trickac6d9be2013-05-25 02:42:55 +00001952 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001953 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001954
Nate Begeman405e3ec2005-10-21 00:02:42 +00001955 // If we're dividing by a positive value, we're done. Otherwise, we must
1956 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001957 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001958 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001959
Gabor Greifba36cb52008-08-28 21:40:38 +00001960 AddToWorkList(SRA.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001961 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001962 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001963 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001964
Nate Begeman69575232005-10-20 02:15:44 +00001965 // if integer divide is expensive and we satisfy the requirements, emit an
1966 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001967 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001968 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001969 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001970 }
Dan Gohman7f321562007-06-25 16:23:39 +00001971
Dan Gohman613e0d82007-07-03 14:03:57 +00001972 // undef / X -> 0
1973 if (N0.getOpcode() == ISD::UNDEF)
1974 return DAG.getConstant(0, VT);
1975 // X / undef -> undef
1976 if (N1.getOpcode() == ISD::UNDEF)
1977 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001978
Dan Gohman475871a2008-07-27 21:46:04 +00001979 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001980}
1981
Dan Gohman475871a2008-07-27 21:46:04 +00001982SDValue DAGCombiner::visitUDIV(SDNode *N) {
1983 SDValue N0 = N->getOperand(0);
1984 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001985 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1986 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001987 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001988
Dan Gohman7f321562007-06-25 16:23:39 +00001989 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001990 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001991 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001992 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001993 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001994
Nate Begeman1d4d4142005-09-01 00:19:25 +00001995 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001996 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001997 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001998 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001999 if (N1C && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002000 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002001 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00002002 getShiftAmountTy(N0.getValueType())));
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002003 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002004 if (N1.getOpcode() == ISD::SHL) {
2005 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002006 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002007 EVT ADDVT = N1.getOperand(1).getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002008 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N), ADDVT,
Bill Wendling07d85142009-01-30 02:55:25 +00002009 N1.getOperand(1),
2010 DAG.getConstant(SHC->getAPIntValue()
2011 .logBase2(),
2012 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002013 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002014 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002015 }
2016 }
2017 }
Nate Begeman69575232005-10-20 02:15:44 +00002018 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00002019 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002020 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002021 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00002022 }
Dan Gohman7f321562007-06-25 16:23:39 +00002023
Dan Gohman613e0d82007-07-03 14:03:57 +00002024 // undef / X -> 0
2025 if (N0.getOpcode() == ISD::UNDEF)
2026 return DAG.getConstant(0, VT);
2027 // X / undef -> undef
2028 if (N1.getOpcode() == ISD::UNDEF)
2029 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002030
Dan Gohman475871a2008-07-27 21:46:04 +00002031 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002032}
2033
Dan Gohman475871a2008-07-27 21:46:04 +00002034SDValue DAGCombiner::visitSREM(SDNode *N) {
2035 SDValue N0 = N->getOperand(0);
2036 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002037 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2038 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002039 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002040
Nate Begeman1d4d4142005-09-01 00:19:25 +00002041 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002042 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002043 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002044 // If we know the sign bits of both operands are zero, strength reduce to a
2045 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00002046 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002047 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002048 return DAG.getNode(ISD::UREM, SDLoc(N), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00002049 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002050
Dan Gohman77003042007-11-26 23:46:11 +00002051 // If X/C can be simplified by the division-by-constant logic, lower
2052 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002053 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002054 SDValue Div = DAG.getNode(ISD::SDIV, SDLoc(N), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002055 AddToWorkList(Div.getNode());
2056 SDValue OptimizedDiv = combine(Div.getNode());
2057 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002058 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002059 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002060 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002061 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002062 return Sub;
2063 }
Chris Lattner26d29902006-10-12 20:58:32 +00002064 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002065
Dan Gohman613e0d82007-07-03 14:03:57 +00002066 // undef % X -> 0
2067 if (N0.getOpcode() == ISD::UNDEF)
2068 return DAG.getConstant(0, VT);
2069 // X % undef -> undef
2070 if (N1.getOpcode() == ISD::UNDEF)
2071 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002072
Dan Gohman475871a2008-07-27 21:46:04 +00002073 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002074}
2075
Dan Gohman475871a2008-07-27 21:46:04 +00002076SDValue DAGCombiner::visitUREM(SDNode *N) {
2077 SDValue N0 = N->getOperand(0);
2078 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002079 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2080 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002081 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002082
Nate Begeman1d4d4142005-09-01 00:19:25 +00002083 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002084 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002085 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002086 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002087 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002088 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002089 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002090 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2091 if (N1.getOpcode() == ISD::SHL) {
2092 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002093 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002094 SDValue Add =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002095 DAG.getNode(ISD::ADD, SDLoc(N), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002096 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002097 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002098 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002099 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002100 }
2101 }
2102 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002103
Dan Gohman77003042007-11-26 23:46:11 +00002104 // If X/C can be simplified by the division-by-constant logic, lower
2105 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002106 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002107 SDValue Div = DAG.getNode(ISD::UDIV, SDLoc(N), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002108 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002109 SDValue OptimizedDiv = combine(Div.getNode());
2110 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002111 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002112 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002113 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002114 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002115 return Sub;
2116 }
Chris Lattner26d29902006-10-12 20:58:32 +00002117 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002118
Dan Gohman613e0d82007-07-03 14:03:57 +00002119 // undef % X -> 0
2120 if (N0.getOpcode() == ISD::UNDEF)
2121 return DAG.getConstant(0, VT);
2122 // X % undef -> undef
2123 if (N1.getOpcode() == ISD::UNDEF)
2124 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002125
Dan Gohman475871a2008-07-27 21:46:04 +00002126 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002127}
2128
Dan Gohman475871a2008-07-27 21:46:04 +00002129SDValue DAGCombiner::visitMULHS(SDNode *N) {
2130 SDValue N0 = N->getOperand(0);
2131 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002132 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002133 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002134 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002135
Nate Begeman1d4d4142005-09-01 00:19:25 +00002136 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002137 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002138 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002139 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002140 if (N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002141 return DAG.getNode(ISD::SRA, SDLoc(N), N0.getValueType(), N0,
Bill Wendling326411d2009-01-30 03:00:18 +00002142 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002143 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002144 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002145 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002146 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002147
Chris Lattnerde1c3602010-12-13 08:39:01 +00002148 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2149 // plus a shift.
2150 if (VT.isSimple() && !VT.isVector()) {
2151 MVT Simple = VT.getSimpleVT();
2152 unsigned SimpleSize = Simple.getSizeInBits();
2153 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2154 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2155 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2156 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2157 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002158 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002159 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002160 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2161 }
2162 }
Owen Anderson95771af2011-02-25 21:41:48 +00002163
Dan Gohman475871a2008-07-27 21:46:04 +00002164 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002165}
2166
Dan Gohman475871a2008-07-27 21:46:04 +00002167SDValue DAGCombiner::visitMULHU(SDNode *N) {
2168 SDValue N0 = N->getOperand(0);
2169 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002170 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002171 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002172 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002173
Nate Begeman1d4d4142005-09-01 00:19:25 +00002174 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002175 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002176 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002177 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002178 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002179 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002180 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002181 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002182 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002183
Chris Lattnerde1c3602010-12-13 08:39:01 +00002184 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2185 // plus a shift.
2186 if (VT.isSimple() && !VT.isVector()) {
2187 MVT Simple = VT.getSimpleVT();
2188 unsigned SimpleSize = Simple.getSizeInBits();
2189 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2190 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2191 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2192 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2193 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2194 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002195 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002196 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2197 }
2198 }
Owen Anderson95771af2011-02-25 21:41:48 +00002199
Dan Gohman475871a2008-07-27 21:46:04 +00002200 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002201}
2202
Dan Gohman389079b2007-10-08 17:57:15 +00002203/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2204/// compute two values. LoOp and HiOp give the opcodes for the two computations
2205/// that are being performed. Return true if a simplification was made.
2206///
Scott Michelfdc40a02009-02-17 22:15:04 +00002207SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002208 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002209 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002210 bool HiExists = N->hasAnyUseOfValue(1);
2211 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002212 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002213 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002214 SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002215 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002216 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002217 }
2218
2219 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002220 bool LoExists = N->hasAnyUseOfValue(0);
2221 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002222 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002223 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002224 SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Bill Wendling826d1142009-01-30 03:08:40 +00002225 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002226 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002227 }
2228
Evan Cheng44711942007-11-08 09:25:29 +00002229 // If both halves are used, return as it is.
2230 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002231 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002232
2233 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002234 if (LoExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002235 SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002236 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002237 AddToWorkList(Lo.getNode());
2238 SDValue LoOpt = combine(Lo.getNode());
2239 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002240 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002241 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002242 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002243 }
2244
Evan Cheng44711942007-11-08 09:25:29 +00002245 if (HiExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002246 SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002247 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002248 AddToWorkList(Hi.getNode());
2249 SDValue HiOpt = combine(Hi.getNode());
2250 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002251 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002252 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002253 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002254 }
Bill Wendling826d1142009-01-30 03:08:40 +00002255
Dan Gohman475871a2008-07-27 21:46:04 +00002256 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002257}
2258
Dan Gohman475871a2008-07-27 21:46:04 +00002259SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2260 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002261 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002262
Chris Lattner33e77d32010-12-15 06:04:19 +00002263 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002264 SDLoc DL(N);
Chris Lattner33e77d32010-12-15 06:04:19 +00002265
2266 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2267 // plus a shift.
2268 if (VT.isSimple() && !VT.isVector()) {
2269 MVT Simple = VT.getSimpleVT();
2270 unsigned SimpleSize = Simple.getSizeInBits();
2271 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2272 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2273 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2274 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2275 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2276 // Compute the high part as N1.
2277 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002278 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002279 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2280 // Compute the low part as N0.
2281 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2282 return CombineTo(N, Lo, Hi);
2283 }
2284 }
Owen Anderson95771af2011-02-25 21:41:48 +00002285
Dan Gohman475871a2008-07-27 21:46:04 +00002286 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002287}
2288
Dan Gohman475871a2008-07-27 21:46:04 +00002289SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2290 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002291 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002292
Chris Lattner33e77d32010-12-15 06:04:19 +00002293 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002294 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00002295
Chris Lattner33e77d32010-12-15 06:04:19 +00002296 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2297 // plus a shift.
2298 if (VT.isSimple() && !VT.isVector()) {
2299 MVT Simple = VT.getSimpleVT();
2300 unsigned SimpleSize = Simple.getSizeInBits();
2301 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2302 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2303 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2304 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2305 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2306 // Compute the high part as N1.
2307 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002308 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002309 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2310 // Compute the low part as N0.
2311 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2312 return CombineTo(N, Lo, Hi);
2313 }
2314 }
Owen Anderson95771af2011-02-25 21:41:48 +00002315
Dan Gohman475871a2008-07-27 21:46:04 +00002316 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002317}
2318
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002319SDValue DAGCombiner::visitSMULO(SDNode *N) {
2320 // (smulo x, 2) -> (saddo x, x)
2321 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2322 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002323 return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002324 N->getOperand(0), N->getOperand(0));
2325
2326 return SDValue();
2327}
2328
2329SDValue DAGCombiner::visitUMULO(SDNode *N) {
2330 // (umulo x, 2) -> (uaddo x, x)
2331 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2332 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002333 return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002334 N->getOperand(0), N->getOperand(0));
2335
2336 return SDValue();
2337}
2338
Dan Gohman475871a2008-07-27 21:46:04 +00002339SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2340 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002341 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002342
Dan Gohman475871a2008-07-27 21:46:04 +00002343 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002344}
2345
Dan Gohman475871a2008-07-27 21:46:04 +00002346SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2347 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002348 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002349
Dan Gohman475871a2008-07-27 21:46:04 +00002350 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002351}
2352
Chris Lattner35e5c142006-05-05 05:51:50 +00002353/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2354/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002355SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2356 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002357 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002358 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002359
Dan Gohmanff00a552010-01-14 03:08:49 +00002360 // Bail early if none of these transforms apply.
2361 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2362
Chris Lattner540121f2006-05-05 06:31:05 +00002363 // For each of OP in AND/OR/XOR:
2364 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2365 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2366 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002367 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002368 //
2369 // do not sink logical op inside of a vector extend, since it may combine
2370 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002371 EVT Op0VT = N0.getOperand(0).getValueType();
2372 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002373 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002374 // Avoid infinite looping with PromoteIntBinOp.
2375 (N0.getOpcode() == ISD::ANY_EXTEND &&
2376 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002377 (N0.getOpcode() == ISD::TRUNCATE &&
2378 (!TLI.isZExtFree(VT, Op0VT) ||
2379 !TLI.isTruncateFree(Op0VT, VT)) &&
2380 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002381 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002382 Op0VT == N1.getOperand(0).getValueType() &&
2383 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002384 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002385 N0.getOperand(0).getValueType(),
2386 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002387 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002388 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002389 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002390
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002391 // For each of OP in SHL/SRL/SRA/AND...
2392 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2393 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2394 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002395 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002396 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002397 N0.getOperand(1) == N1.getOperand(1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002398 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002399 N0.getOperand(0).getValueType(),
2400 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002401 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002402 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendlingb74c8672009-01-30 19:25:47 +00002403 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002404 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002405
Nadav Rotem4ac90812012-04-01 19:31:22 +00002406 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2407 // Only perform this optimization after type legalization and before
2408 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2409 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2410 // we don't want to undo this promotion.
2411 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2412 // on scalars.
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002413 if ((N0.getOpcode() == ISD::BITCAST ||
2414 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) &&
2415 Level == AfterLegalizeTypes) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002416 SDValue In0 = N0.getOperand(0);
2417 SDValue In1 = N1.getOperand(0);
2418 EVT In0Ty = In0.getValueType();
2419 EVT In1Ty = In1.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002420 SDLoc DL(N);
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002421 // If both incoming values are integers, and the original types are the
2422 // same.
Nadav Rotem4ac90812012-04-01 19:31:22 +00002423 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002424 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1);
2425 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002426 AddToWorkList(Op.getNode());
2427 return BC;
2428 }
2429 }
2430
2431 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2432 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2433 // If both shuffles use the same mask, and both shuffle within a single
2434 // vector, then it is worthwhile to move the swizzle after the operation.
2435 // The type-legalizer generates this pattern when loading illegal
2436 // vector types from memory. In many cases this allows additional shuffle
2437 // optimizations.
Craig Topperf9204232012-04-09 07:19:09 +00002438 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2439 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2440 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002441 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2442 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topperf9204232012-04-09 07:19:09 +00002443
2444 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2445 "Inputs to shuffles are not the same type");
Nadav Rotem4ac90812012-04-01 19:31:22 +00002446
2447 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotem4ac90812012-04-01 19:31:22 +00002448
2449 // Check that both shuffles use the same mask. The masks are known to be of
2450 // the same length because the result vector type is the same.
2451 bool SameMask = true;
2452 for (unsigned i = 0; i != NumElts; ++i) {
2453 int Idx0 = SVN0->getMaskElt(i);
2454 int Idx1 = SVN1->getMaskElt(i);
2455 if (Idx0 != Idx1) {
2456 SameMask = false;
2457 break;
2458 }
2459 }
2460
Craig Topperf9204232012-04-09 07:19:09 +00002461 if (SameMask) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002462 SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Craig Topperf9204232012-04-09 07:19:09 +00002463 N0.getOperand(0), N1.getOperand(0));
Nadav Rotem4ac90812012-04-01 19:31:22 +00002464 AddToWorkList(Op.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002465 return DAG.getVectorShuffle(VT, SDLoc(N), Op,
Craig Topperf9204232012-04-09 07:19:09 +00002466 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002467 }
2468 }
Craig Topperf9204232012-04-09 07:19:09 +00002469
Dan Gohman475871a2008-07-27 21:46:04 +00002470 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002471}
2472
Dan Gohman475871a2008-07-27 21:46:04 +00002473SDValue DAGCombiner::visitAND(SDNode *N) {
2474 SDValue N0 = N->getOperand(0);
2475 SDValue N1 = N->getOperand(1);
2476 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002477 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2478 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002479 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002480 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002481
Dan Gohman7f321562007-06-25 16:23:39 +00002482 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002483 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002484 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002485 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00002486
2487 // fold (and x, 0) -> 0, vector edition
2488 if (ISD::isBuildVectorAllZeros(N0.getNode()))
2489 return N0;
2490 if (ISD::isBuildVectorAllZeros(N1.getNode()))
2491 return N1;
2492
2493 // fold (and x, -1) -> x, vector edition
2494 if (ISD::isBuildVectorAllOnes(N0.getNode()))
2495 return N1;
2496 if (ISD::isBuildVectorAllOnes(N1.getNode()))
2497 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002498 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002499
Dan Gohman613e0d82007-07-03 14:03:57 +00002500 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002501 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002502 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002503 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002504 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002505 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002506 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002507 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002508 return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002509 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002510 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002511 return N0;
2512 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002513 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002514 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002515 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002516 // reassociate and
Andrew Trickac6d9be2013-05-25 02:42:55 +00002517 SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002518 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002519 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002520 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002521 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002522 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002523 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002524 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002525 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2526 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002527 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002528 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002529 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002530 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002531 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N),
Bill Wendling2627a882009-01-30 20:43:18 +00002532 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002533
Chris Lattner1ec05d12006-03-01 21:47:21 +00002534 // Replace uses of the AND with uses of the Zero extend node.
2535 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002536
Chris Lattner3603cd62006-02-02 07:17:31 +00002537 // We actually want to replace all uses of the any_extend with the
2538 // zero_extend, to avoid duplicating things. This will later cause this
2539 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002540 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002541 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002542 }
2543 }
Stephen Lin155615d2013-07-08 00:37:03 +00002544 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
James Molloy6259dcd2012-02-20 12:02:38 +00002545 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2546 // already be zero by virtue of the width of the base type of the load.
2547 //
2548 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2549 // more cases.
2550 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2551 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2552 N0.getOpcode() == ISD::LOAD) {
2553 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2554 N0 : N0.getOperand(0) );
2555
2556 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2557 // This can be a pure constant or a vector splat, in which case we treat the
2558 // vector as a scalar and use the splat value.
2559 APInt Constant = APInt::getNullValue(1);
2560 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2561 Constant = C->getAPIntValue();
2562 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2563 APInt SplatValue, SplatUndef;
2564 unsigned SplatBitSize;
2565 bool HasAnyUndefs;
2566 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2567 SplatBitSize, HasAnyUndefs);
2568 if (IsSplat) {
2569 // Undef bits can contribute to a possible optimisation if set, so
2570 // set them.
2571 SplatValue |= SplatUndef;
2572
2573 // The splat value may be something like "0x00FFFFFF", which means 0 for
2574 // the first vector value and FF for the rest, repeating. We need a mask
2575 // that will apply equally to all members of the vector, so AND all the
2576 // lanes of the constant together.
2577 EVT VT = Vector->getValueType(0);
2578 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002579
2580 // If the splat value has been compressed to a bitlength lower
2581 // than the size of the vector lane, we need to re-expand it to
2582 // the lane size.
2583 if (BitWidth > SplatBitSize)
2584 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2585 SplatBitSize < BitWidth;
2586 SplatBitSize = SplatBitSize * 2)
2587 SplatValue |= SplatValue.shl(SplatBitSize);
2588
James Molloy6259dcd2012-02-20 12:02:38 +00002589 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002590 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy6259dcd2012-02-20 12:02:38 +00002591 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2592 }
2593 }
2594
2595 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2596 // actually legal and isn't going to get expanded, else this is a false
2597 // optimisation.
2598 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2599 Load->getMemoryVT());
2600
2601 // Resize the constant to the same size as the original memory access before
2602 // extension. If it is still the AllOnesValue then this AND is completely
2603 // unneeded.
2604 Constant =
2605 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2606
2607 bool B;
2608 switch (Load->getExtensionType()) {
2609 default: B = false; break;
2610 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2611 case ISD::ZEXTLOAD:
2612 case ISD::NON_EXTLOAD: B = true; break;
2613 }
2614
2615 if (B && Constant.isAllOnesValue()) {
2616 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2617 // preserve semantics once we get rid of the AND.
2618 SDValue NewLoad(Load, 0);
2619 if (Load->getExtensionType() == ISD::EXTLOAD) {
2620 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002621 Load->getValueType(0), SDLoc(Load),
James Molloy6259dcd2012-02-20 12:02:38 +00002622 Load->getChain(), Load->getBasePtr(),
2623 Load->getOffset(), Load->getMemoryVT(),
2624 Load->getMemOperand());
2625 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkeld65e4632012-06-20 15:42:48 +00002626 if (Load->getNumValues() == 3) {
2627 // PRE/POST_INC loads have 3 values.
2628 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2629 NewLoad.getValue(2) };
2630 CombineTo(Load, To, 3, true);
2631 } else {
2632 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2633 }
James Molloy6259dcd2012-02-20 12:02:38 +00002634 }
2635
2636 // Fold the AND away, taking care not to fold to the old load node if we
2637 // replaced it.
2638 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2639
2640 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2641 }
2642 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002643 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2644 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2645 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2646 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002647
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002648 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002649 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002650 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002651 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002652 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002653 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002654 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002655 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002656 }
Bill Wendling2627a882009-01-30 20:43:18 +00002657 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002658 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002659 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002660 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002661 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002662 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002663 }
Bill Wendling2627a882009-01-30 20:43:18 +00002664 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002665 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002666 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002667 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002668 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002669 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002670 }
2671 }
2672 // canonicalize equivalent to ll == rl
2673 if (LL == RR && LR == RL) {
2674 Op1 = ISD::getSetCCSwappedOperands(Op1);
2675 std::swap(RL, RR);
2676 }
2677 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002678 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002679 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002680 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00002681 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00002682 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
2683 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00002684 getSetCCResultType(N0.getSimpleValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002685 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling2627a882009-01-30 20:43:18 +00002686 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002687 }
2688 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002689
Bill Wendling2627a882009-01-30 20:43:18 +00002690 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002691 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002692 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002693 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002694 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002695
Nate Begemande996292006-02-03 22:24:05 +00002696 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2697 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002698 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002699 SimplifyDemandedBits(SDValue(N, 0)))
2700 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002701
Nate Begemanded49632005-10-13 03:11:28 +00002702 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002703 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002704 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002705 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002706 // If we zero all the possible extended bits, then we can turn this into
2707 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002708 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002709 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002710 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002711 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002712 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002713 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002714 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002715 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002716 LN0->isVolatile(), LN0->isNonTemporal(),
2717 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002718 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002719 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002720 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002721 }
2722 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002723 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002724 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002725 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002726 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002727 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002728 // If we zero all the possible extended bits, then we can turn this into
2729 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002730 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002731 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002732 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002733 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002734 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002735 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002736 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002737 LN0->getBasePtr(), LN0->getPointerInfo(),
2738 MemVT,
David Greene1e559442010-02-15 17:00:31 +00002739 LN0->isVolatile(), LN0->isNonTemporal(),
2740 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002741 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002742 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002743 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002744 }
2745 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002746
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002747 // fold (and (load x), 255) -> (zextload x, i8)
2748 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002749 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2750 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2751 (N0.getOpcode() == ISD::ANY_EXTEND &&
2752 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2753 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2754 LoadSDNode *LN0 = HasAnyExt
2755 ? cast<LoadSDNode>(N0.getOperand(0))
2756 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002757 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Tim Northover5bce67a2013-07-02 09:58:53 +00002758 LN0->isUnindexed() && N0.hasOneUse() && SDValue(LN0, 0).hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002759 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002760 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2761 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2762 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002763
Evan Chengd40d03e2010-01-06 19:38:29 +00002764 if (ExtVT == LoadedVT &&
2765 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002766 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002767
2768 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002769 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002770 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002771 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002772 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2773 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002774 AddToWorkList(N);
2775 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2776 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2777 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002778
Chris Lattneref7634c2010-01-07 21:53:27 +00002779 // Do not change the width of a volatile load.
2780 // Do not generate loads of non-round integer types since these can
2781 // be expensive (and would be wrong if the type is not byte sized).
2782 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2783 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2784 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002785
Chris Lattneref7634c2010-01-07 21:53:27 +00002786 unsigned Alignment = LN0->getAlignment();
2787 SDValue NewPtr = LN0->getBasePtr();
2788
2789 // For big endian targets, we need to add an offset to the pointer
2790 // to load the correct bytes. For little endian systems, we merely
2791 // need to read fewer bytes from the same pointer.
2792 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002793 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2794 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2795 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002796 NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), PtrType,
Chris Lattneref7634c2010-01-07 21:53:27 +00002797 NewPtr, DAG.getConstant(PtrOff, PtrType));
2798 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002799 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002800
2801 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002802
Chris Lattneref7634c2010-01-07 21:53:27 +00002803 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2804 SDValue Load =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002805 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002806 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002807 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002808 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2809 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002810 AddToWorkList(N);
2811 CombineTo(LN0, Load, Load.getValue(1));
2812 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002813 }
Evan Cheng466685d2006-10-09 20:57:25 +00002814 }
Chris Lattner15045b62006-02-28 06:35:35 +00002815 }
2816 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002817
Evan Chenga9e13ba2012-07-17 18:54:11 +00002818 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2819 VT.getSizeInBits() <= 64) {
2820 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2821 APInt ADDC = ADDI->getAPIntValue();
2822 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2823 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2824 // immediate for an add, but it is legal if its top c2 bits are set,
2825 // transform the ADD so the immediate doesn't need to be materialized
2826 // in a register.
2827 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2828 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2829 SRLI->getZExtValue());
2830 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2831 ADDC |= Mask;
2832 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2833 SDValue NewAdd =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002834 DAG.getNode(ISD::ADD, SDLoc(N0), VT,
Evan Chenga9e13ba2012-07-17 18:54:11 +00002835 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2836 CombineTo(N0.getNode(), NewAdd);
2837 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2838 }
2839 }
2840 }
2841 }
2842 }
2843 }
Evan Chenga9e13ba2012-07-17 18:54:11 +00002844
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002845 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002846}
2847
Evan Cheng9568e5c2011-06-21 06:01:08 +00002848/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2849///
2850SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2851 bool DemandHighBits) {
2852 if (!LegalOperations)
2853 return SDValue();
2854
2855 EVT VT = N->getValueType(0);
2856 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2857 return SDValue();
2858 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2859 return SDValue();
2860
2861 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2862 bool LookPassAnd0 = false;
2863 bool LookPassAnd1 = false;
2864 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2865 std::swap(N0, N1);
2866 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2867 std::swap(N0, N1);
2868 if (N0.getOpcode() == ISD::AND) {
2869 if (!N0.getNode()->hasOneUse())
2870 return SDValue();
2871 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2872 if (!N01C || N01C->getZExtValue() != 0xFF00)
2873 return SDValue();
2874 N0 = N0.getOperand(0);
2875 LookPassAnd0 = true;
2876 }
2877
2878 if (N1.getOpcode() == ISD::AND) {
2879 if (!N1.getNode()->hasOneUse())
2880 return SDValue();
2881 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2882 if (!N11C || N11C->getZExtValue() != 0xFF)
2883 return SDValue();
2884 N1 = N1.getOperand(0);
2885 LookPassAnd1 = true;
2886 }
2887
2888 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2889 std::swap(N0, N1);
2890 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2891 return SDValue();
2892 if (!N0.getNode()->hasOneUse() ||
2893 !N1.getNode()->hasOneUse())
2894 return SDValue();
2895
2896 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2897 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2898 if (!N01C || !N11C)
2899 return SDValue();
2900 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2901 return SDValue();
2902
2903 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2904 SDValue N00 = N0->getOperand(0);
2905 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2906 if (!N00.getNode()->hasOneUse())
2907 return SDValue();
2908 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2909 if (!N001C || N001C->getZExtValue() != 0xFF)
2910 return SDValue();
2911 N00 = N00.getOperand(0);
2912 LookPassAnd0 = true;
2913 }
2914
2915 SDValue N10 = N1->getOperand(0);
2916 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2917 if (!N10.getNode()->hasOneUse())
2918 return SDValue();
2919 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2920 if (!N101C || N101C->getZExtValue() != 0xFF00)
2921 return SDValue();
2922 N10 = N10.getOperand(0);
2923 LookPassAnd1 = true;
2924 }
2925
2926 if (N00 != N10)
2927 return SDValue();
2928
2929 // Make sure everything beyond the low halfword is zero since the SRL 16
2930 // will clear the top bits.
2931 unsigned OpSizeInBits = VT.getSizeInBits();
2932 if (DemandHighBits && OpSizeInBits > 16 &&
2933 (!LookPassAnd0 || !LookPassAnd1) &&
2934 !DAG.MaskedValueIsZero(N10, APInt::getHighBitsSet(OpSizeInBits, 16)))
2935 return SDValue();
Eric Christopher7332e6e2011-07-14 01:12:15 +00002936
Andrew Trickac6d9be2013-05-25 02:42:55 +00002937 SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00);
Evan Cheng9568e5c2011-06-21 06:01:08 +00002938 if (OpSizeInBits > 16)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002939 Res = DAG.getNode(ISD::SRL, SDLoc(N), VT, Res,
Evan Cheng9568e5c2011-06-21 06:01:08 +00002940 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2941 return Res;
2942}
2943
2944/// isBSwapHWordElement - Return true if the specified node is an element
2945/// that makes up a 32-bit packed halfword byteswap. i.e.
2946/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2947static bool isBSwapHWordElement(SDValue N, SmallVector<SDNode*,4> &Parts) {
2948 if (!N.getNode()->hasOneUse())
2949 return false;
2950
2951 unsigned Opc = N.getOpcode();
2952 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
2953 return false;
2954
2955 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2956 if (!N1C)
2957 return false;
2958
2959 unsigned Num;
2960 switch (N1C->getZExtValue()) {
2961 default:
2962 return false;
2963 case 0xFF: Num = 0; break;
2964 case 0xFF00: Num = 1; break;
2965 case 0xFF0000: Num = 2; break;
2966 case 0xFF000000: Num = 3; break;
2967 }
2968
2969 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
2970 SDValue N0 = N.getOperand(0);
2971 if (Opc == ISD::AND) {
2972 if (Num == 0 || Num == 2) {
2973 // (x >> 8) & 0xff
2974 // (x >> 8) & 0xff0000
2975 if (N0.getOpcode() != ISD::SRL)
2976 return false;
2977 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2978 if (!C || C->getZExtValue() != 8)
2979 return false;
2980 } else {
2981 // (x << 8) & 0xff00
2982 // (x << 8) & 0xff000000
2983 if (N0.getOpcode() != ISD::SHL)
2984 return false;
2985 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2986 if (!C || C->getZExtValue() != 8)
2987 return false;
2988 }
2989 } else if (Opc == ISD::SHL) {
2990 // (x & 0xff) << 8
2991 // (x & 0xff0000) << 8
2992 if (Num != 0 && Num != 2)
2993 return false;
2994 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2995 if (!C || C->getZExtValue() != 8)
2996 return false;
2997 } else { // Opc == ISD::SRL
2998 // (x & 0xff00) >> 8
2999 // (x & 0xff000000) >> 8
3000 if (Num != 1 && Num != 3)
3001 return false;
3002 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3003 if (!C || C->getZExtValue() != 8)
3004 return false;
3005 }
3006
3007 if (Parts[Num])
3008 return false;
3009
3010 Parts[Num] = N0.getOperand(0).getNode();
3011 return true;
3012}
3013
3014/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
3015/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
3016/// => (rotl (bswap x), 16)
3017SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
3018 if (!LegalOperations)
3019 return SDValue();
3020
3021 EVT VT = N->getValueType(0);
3022 if (VT != MVT::i32)
3023 return SDValue();
3024 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
3025 return SDValue();
3026
3027 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
3028 // Look for either
3029 // (or (or (and), (and)), (or (and), (and)))
3030 // (or (or (or (and), (and)), (and)), (and))
3031 if (N0.getOpcode() != ISD::OR)
3032 return SDValue();
3033 SDValue N00 = N0.getOperand(0);
3034 SDValue N01 = N0.getOperand(1);
3035
Evan Cheng9a65a012012-12-13 01:34:32 +00003036 if (N1.getOpcode() == ISD::OR &&
3037 N00.getNumOperands() == 2 && N01.getNumOperands() == 2) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00003038 // (or (or (and), (and)), (or (and), (and)))
3039 SDValue N000 = N00.getOperand(0);
3040 if (!isBSwapHWordElement(N000, Parts))
3041 return SDValue();
3042
3043 SDValue N001 = N00.getOperand(1);
3044 if (!isBSwapHWordElement(N001, Parts))
3045 return SDValue();
3046 SDValue N010 = N01.getOperand(0);
3047 if (!isBSwapHWordElement(N010, Parts))
3048 return SDValue();
3049 SDValue N011 = N01.getOperand(1);
3050 if (!isBSwapHWordElement(N011, Parts))
3051 return SDValue();
3052 } else {
3053 // (or (or (or (and), (and)), (and)), (and))
3054 if (!isBSwapHWordElement(N1, Parts))
3055 return SDValue();
3056 if (!isBSwapHWordElement(N01, Parts))
3057 return SDValue();
3058 if (N00.getOpcode() != ISD::OR)
3059 return SDValue();
3060 SDValue N000 = N00.getOperand(0);
3061 if (!isBSwapHWordElement(N000, Parts))
3062 return SDValue();
3063 SDValue N001 = N00.getOperand(1);
3064 if (!isBSwapHWordElement(N001, Parts))
3065 return SDValue();
3066 }
3067
3068 // Make sure the parts are all coming from the same node.
3069 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
3070 return SDValue();
3071
Andrew Trickac6d9be2013-05-25 02:42:55 +00003072 SDValue BSwap = DAG.getNode(ISD::BSWAP, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00003073 SDValue(Parts[0],0));
3074
3075 // Result of the bswap should be rotated by 16. If it's not legal, than
3076 // do (x << 16) | (x >> 16).
3077 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
3078 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003079 return DAG.getNode(ISD::ROTL, SDLoc(N), VT, BSwap, ShAmt);
Craig Topper0eb5dad2012-09-29 07:18:53 +00003080 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003081 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, BSwap, ShAmt);
3082 return DAG.getNode(ISD::OR, SDLoc(N), VT,
3083 DAG.getNode(ISD::SHL, SDLoc(N), VT, BSwap, ShAmt),
3084 DAG.getNode(ISD::SRL, SDLoc(N), VT, BSwap, ShAmt));
Evan Cheng9568e5c2011-06-21 06:01:08 +00003085}
3086
Dan Gohman475871a2008-07-27 21:46:04 +00003087SDValue DAGCombiner::visitOR(SDNode *N) {
3088 SDValue N0 = N->getOperand(0);
3089 SDValue N1 = N->getOperand(1);
3090 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00003091 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3092 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003093 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003094
Dan Gohman7f321562007-06-25 16:23:39 +00003095 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003096 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003097 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003098 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003099
3100 // fold (or x, 0) -> x, vector edition
3101 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3102 return N1;
3103 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3104 return N0;
3105
3106 // fold (or x, -1) -> -1, vector edition
3107 if (ISD::isBuildVectorAllOnes(N0.getNode()))
3108 return N0;
3109 if (ISD::isBuildVectorAllOnes(N1.getNode()))
3110 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003111 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003112
Dan Gohman613e0d82007-07-03 14:03:57 +00003113 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00003114 if (!LegalOperations &&
3115 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00003116 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3117 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3118 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003119 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003120 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003121 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003122 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003123 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003124 return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003125 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003126 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003127 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003128 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003129 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003130 return N1;
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003131 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003132 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003133 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00003134
3135 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3136 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3137 if (BSwap.getNode() != 0)
3138 return BSwap;
3139 BSwap = MatchBSwapHWordLow(N, N0, N1);
3140 if (BSwap.getNode() != 0)
3141 return BSwap;
3142
Nate Begemancd4d58c2006-02-03 06:46:56 +00003143 // reassociate or
Andrew Trickac6d9be2013-05-25 02:42:55 +00003144 SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003145 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003146 return ROR;
3147 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003148 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00003149 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00003150 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00003151 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00003152 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003153 return DAG.getNode(ISD::AND, SDLoc(N), VT,
3154 DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003155 N0.getOperand(0), N1),
3156 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00003157 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003158 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3159 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3160 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3161 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003162
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003163 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003164 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00003165 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3166 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00003167 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003168 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003169 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003170 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003171 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003172 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003173 }
Bill Wendling09025642009-01-30 20:59:34 +00003174 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3175 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00003176 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003177 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003178 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003179 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003180 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003181 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003182 }
3183 }
3184 // canonicalize equivalent to ll == rl
3185 if (LL == RR && LR == RL) {
3186 Op1 = ISD::getSetCCSwappedOperands(Op1);
3187 std::swap(RL, RR);
3188 }
3189 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003190 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003191 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00003192 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003193 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00003194 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
3195 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00003196 getSetCCResultType(N0.getValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003197 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling09025642009-01-30 20:59:34 +00003198 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003199 }
3200 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003201
Bill Wendling09025642009-01-30 20:59:34 +00003202 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00003203 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003204 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003205 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003206 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003207
Bill Wendling09025642009-01-30 20:59:34 +00003208 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00003209 if (N0.getOpcode() == ISD::AND &&
3210 N1.getOpcode() == ISD::AND &&
3211 N0.getOperand(1).getOpcode() == ISD::Constant &&
3212 N1.getOperand(1).getOpcode() == ISD::Constant &&
3213 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003214 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00003215 // We can only do this xform if we know that bits from X that are set in C2
3216 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003217 const APInt &LHSMask =
3218 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3219 const APInt &RHSMask =
3220 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003221
Dan Gohmanea859be2007-06-22 14:59:07 +00003222 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3223 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003224 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling09025642009-01-30 20:59:34 +00003225 N0.getOperand(0), N1.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003226 return DAG.getNode(ISD::AND, SDLoc(N), VT, X,
Bill Wendling09025642009-01-30 20:59:34 +00003227 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00003228 }
3229 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003230
Chris Lattner516b9622006-09-14 20:50:57 +00003231 // See if this is some rotate idiom.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003232 if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N)))
Dan Gohman475871a2008-07-27 21:46:04 +00003233 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00003234
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003235 // Simplify the operands using demanded-bits information.
3236 if (!VT.isVector() &&
3237 SimplifyDemandedBits(SDValue(N, 0)))
3238 return SDValue(N, 0);
3239
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003240 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003241}
3242
Chris Lattner516b9622006-09-14 20:50:57 +00003243/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003244static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00003245 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00003246 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00003247 Mask = Op.getOperand(1);
3248 Op = Op.getOperand(0);
3249 } else {
3250 return false;
3251 }
3252 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003253
Chris Lattner516b9622006-09-14 20:50:57 +00003254 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3255 Shift = Op;
3256 return true;
3257 }
Bill Wendling09025642009-01-30 20:59:34 +00003258
Scott Michelfdc40a02009-02-17 22:15:04 +00003259 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00003260}
3261
Chris Lattner516b9622006-09-14 20:50:57 +00003262// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3263// idioms for rotate, and if the target supports rotation instructions, generate
3264// a rot[lr].
Andrew Trickac6d9be2013-05-25 02:42:55 +00003265SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003266 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00003267 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00003268 if (!TLI.isTypeLegal(VT)) return 0;
3269
3270 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003271 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3272 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00003273 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003274
Chris Lattner516b9622006-09-14 20:50:57 +00003275 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003276 SDValue LHSShift; // The shift.
3277 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003278 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3279 return 0; // Not part of a rotate.
3280
Dan Gohman475871a2008-07-27 21:46:04 +00003281 SDValue RHSShift; // The shift.
3282 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003283 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3284 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00003285
Chris Lattner516b9622006-09-14 20:50:57 +00003286 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3287 return 0; // Not shifting the same value.
3288
3289 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3290 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00003291
Chris Lattner516b9622006-09-14 20:50:57 +00003292 // Canonicalize shl to left side in a shl/srl pair.
3293 if (RHSShift.getOpcode() == ISD::SHL) {
3294 std::swap(LHS, RHS);
3295 std::swap(LHSShift, RHSShift);
3296 std::swap(LHSMask , RHSMask );
3297 }
3298
Duncan Sands83ec4b62008-06-06 12:08:01 +00003299 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003300 SDValue LHSShiftArg = LHSShift.getOperand(0);
3301 SDValue LHSShiftAmt = LHSShift.getOperand(1);
3302 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003303
3304 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3305 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003306 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3307 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003308 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3309 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003310 if ((LShVal + RShVal) != OpSizeInBits)
3311 return 0;
3312
Craig Topper32b73432012-09-29 06:54:22 +00003313 SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3314 LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003315
Chris Lattner516b9622006-09-14 20:50:57 +00003316 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003317 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003318 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003319
Gabor Greifba36cb52008-08-28 21:40:38 +00003320 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003321 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3322 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003323 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003324 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003325 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3326 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003327 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003328
Bill Wendling317bd702009-01-30 21:14:50 +00003329 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003330 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003331
Gabor Greifba36cb52008-08-28 21:40:38 +00003332 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003333 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003334
Chris Lattner516b9622006-09-14 20:50:57 +00003335 // If there is a mask here, and we have a variable shift, we can't be sure
3336 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003337 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003338 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003339
Chris Lattner516b9622006-09-14 20:50:57 +00003340 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
3341 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003342 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
3343 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003344 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003345 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003346 if (SUBC->getAPIntValue() == OpSizeInBits) {
Craig Topper32b73432012-09-29 06:54:22 +00003347 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, LHSShiftArg,
3348 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003349 }
Chris Lattner516b9622006-09-14 20:50:57 +00003350 }
3351 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003352
Chris Lattner516b9622006-09-14 20:50:57 +00003353 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
3354 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003355 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
3356 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003357 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003358 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003359 if (SUBC->getAPIntValue() == OpSizeInBits) {
Craig Topper32b73432012-09-29 06:54:22 +00003360 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, LHSShiftArg,
3361 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003362 }
Scott Michelc9dc1142007-04-02 21:36:32 +00003363 }
3364 }
3365
Dan Gohman74feef22008-10-17 01:23:35 +00003366 // Look for sign/zext/any-extended or truncate cases:
Craig Topper0eb5dad2012-09-29 07:18:53 +00003367 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3368 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3369 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3370 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
3371 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3372 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3373 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3374 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003375 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
3376 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00003377 if (RExtOp0.getOpcode() == ISD::SUB &&
3378 RExtOp0.getOperand(1) == LExtOp0) {
3379 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003380 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00003381 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003382 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003383 if (ConstantSDNode *SUBC =
3384 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003385 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003386 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3387 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00003388 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003389 }
3390 }
3391 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3392 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003393 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003394 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00003395 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003396 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003397 if (ConstantSDNode *SUBC =
3398 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003399 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003400 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
3401 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00003402 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003403 }
3404 }
Chris Lattner516b9622006-09-14 20:50:57 +00003405 }
3406 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003407
Chris Lattner516b9622006-09-14 20:50:57 +00003408 return 0;
3409}
3410
Dan Gohman475871a2008-07-27 21:46:04 +00003411SDValue DAGCombiner::visitXOR(SDNode *N) {
3412 SDValue N0 = N->getOperand(0);
3413 SDValue N1 = N->getOperand(1);
3414 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003415 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3416 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003417 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003418
Dan Gohman7f321562007-06-25 16:23:39 +00003419 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003420 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003421 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003422 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003423
3424 // fold (xor x, 0) -> x, vector edition
3425 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3426 return N1;
3427 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3428 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003429 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003430
Evan Cheng26471c42008-03-25 20:08:07 +00003431 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3432 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3433 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003434 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003435 if (N0.getOpcode() == ISD::UNDEF)
3436 return N0;
3437 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003438 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003439 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003440 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003441 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003442 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003443 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003444 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003445 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003446 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003447 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003448 // reassociate xor
Andrew Trickac6d9be2013-05-25 02:42:55 +00003449 SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003450 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003451 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003452
Nate Begeman1d4d4142005-09-01 00:19:25 +00003453 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003454 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003455 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003456 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3457 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003458
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003459 if (!LegalOperations ||
3460 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003461 switch (N0.getOpcode()) {
3462 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003463 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003464 case ISD::SETCC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003465 return DAG.getSetCC(SDLoc(N), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003466 case ISD::SELECT_CC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003467 return DAG.getSelectCC(SDLoc(N), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003468 N0.getOperand(3), NotCC);
3469 }
3470 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003471 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003472
Chris Lattner61c5ff42007-09-10 21:39:07 +00003473 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003474 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003475 N0.getNode()->hasOneUse() &&
3476 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003477 SDValue V = N0.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003478 V = DAG.getNode(ISD::XOR, SDLoc(N0), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003479 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003480 AddToWorkList(V.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003481 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003482 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003483
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003484 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003485 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003486 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003487 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003488 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3489 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003490 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3491 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003492 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003493 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003494 }
3495 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003496 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003497 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003498 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003499 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003500 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3501 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003502 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3503 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003504 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003505 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003506 }
3507 }
David Majnemer363160a2013-05-08 06:44:42 +00003508 // fold (xor (and x, y), y) -> (and (not x), y)
3509 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
3510 N0->getOperand(1) == N1) {
3511 SDValue X = N0->getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003512 SDValue NotX = DAG.getNOT(SDLoc(X), X, VT);
David Majnemer363160a2013-05-08 06:44:42 +00003513 AddToWorkList(NotX.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003514 return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1);
David Majnemer363160a2013-05-08 06:44:42 +00003515 }
Bill Wendling317bd702009-01-30 21:14:50 +00003516 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003517 if (N1C && N0.getOpcode() == ISD::XOR) {
3518 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3519 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3520 if (N00C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003521 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(1),
Bill Wendling317bd702009-01-30 21:14:50 +00003522 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003523 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003524 if (N01C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003525 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(0),
Bill Wendling317bd702009-01-30 21:14:50 +00003526 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003527 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003528 }
3529 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003530 if (N0 == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003531 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations);
Scott Michelfdc40a02009-02-17 22:15:04 +00003532
Chris Lattner35e5c142006-05-05 05:51:50 +00003533 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3534 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003535 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003536 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003537 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003538
Chris Lattner3e104b12006-04-08 04:15:24 +00003539 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003540 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003541 SimplifyDemandedBits(SDValue(N, 0)))
3542 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003543
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003544 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003545}
3546
Chris Lattnere70da202007-12-06 07:33:36 +00003547/// visitShiftByConstant - Handle transforms common to the three shifts, when
3548/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003549SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003550 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003551 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003552
Chris Lattnere70da202007-12-06 07:33:36 +00003553 // We want to pull some binops through shifts, so that we have (and (shift))
3554 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3555 // thing happens with address calculations, so it's important to canonicalize
3556 // it.
3557 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003558
Chris Lattnere70da202007-12-06 07:33:36 +00003559 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003560 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003561 case ISD::OR:
3562 case ISD::XOR:
3563 HighBitSet = false; // We can only transform sra if the high bit is clear.
3564 break;
3565 case ISD::AND:
3566 HighBitSet = true; // We can only transform sra if the high bit is set.
3567 break;
3568 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003569 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003570 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003571 HighBitSet = false; // We can only transform sra if the high bit is clear.
3572 break;
3573 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003574
Chris Lattnere70da202007-12-06 07:33:36 +00003575 // We require the RHS of the binop to be a constant as well.
3576 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003577 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003578
3579 // FIXME: disable this unless the input to the binop is a shift by a constant.
3580 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003581 //
Bill Wendling88103372009-01-30 21:37:17 +00003582 // void foo(int *X, int i) { X[i & 1235] = 1; }
3583 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003584 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003585 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003586 BinOpLHSVal->getOpcode() != ISD::SRA &&
3587 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3588 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003589 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003590
Owen Andersone50ed302009-08-10 22:56:29 +00003591 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003592
Bill Wendling88103372009-01-30 21:37:17 +00003593 // If this is a signed shift right, and the high bit is modified by the
3594 // logical operation, do not perform the transformation. The highBitSet
3595 // boolean indicates the value of the high bit of the constant which would
3596 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003597 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003598 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3599 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003600 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003601 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003602
Chris Lattnere70da202007-12-06 07:33:36 +00003603 // Fold the constants, shifting the binop RHS by the shift amount.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003604 SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)),
Bill Wendling88103372009-01-30 21:37:17 +00003605 N->getValueType(0),
3606 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003607
3608 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003609 SDValue NewShift = DAG.getNode(N->getOpcode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00003610 SDLoc(LHS->getOperand(0)),
Bill Wendling88103372009-01-30 21:37:17 +00003611 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003612
3613 // Create the new binop.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003614 return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003615}
3616
Dan Gohman475871a2008-07-27 21:46:04 +00003617SDValue DAGCombiner::visitSHL(SDNode *N) {
3618 SDValue N0 = N->getOperand(0);
3619 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003620 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3621 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003622 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003623 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003624
Nate Begeman1d4d4142005-09-01 00:19:25 +00003625 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003626 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003627 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003628 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003629 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003630 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003631 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003632 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003633 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003634 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003635 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003636 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003637 // fold (shl undef, x) -> 0
3638 if (N0.getOpcode() == ISD::UNDEF)
3639 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003640 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003641 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003642 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003643 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003644 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003645 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003646 N1.getOperand(0).getOpcode() == ISD::AND &&
3647 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003648 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003649 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003650 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003651 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003652 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003653 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003654 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
3655 DAG.getNode(ISD::AND, SDLoc(N), TruncVT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003656 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003657 SDLoc(N),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003658 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003659 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003660 }
3661 }
3662
Dan Gohman475871a2008-07-27 21:46:04 +00003663 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3664 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003665
3666 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003667 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003668 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003669 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3670 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003671 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003672 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003673 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003674 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003675 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003676
3677 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3678 // For this to be valid, the second form must not preserve any of the bits
3679 // that are shifted out by the inner shift in the first form. This means
3680 // the outer shift size must be >= the number of bits added by the ext.
3681 // As a corollary, we don't care what kind of ext it is.
3682 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3683 N0.getOpcode() == ISD::ANY_EXTEND ||
3684 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3685 N0.getOperand(0).getOpcode() == ISD::SHL &&
3686 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003687 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003688 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3689 uint64_t c2 = N1C->getZExtValue();
3690 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3691 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3692 if (c2 >= OpSizeInBits - InnerShiftSize) {
3693 if (c1 + c2 >= OpSizeInBits)
3694 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003695 return DAG.getNode(ISD::SHL, SDLoc(N0), VT,
3696 DAG.getNode(N0.getOpcode(), SDLoc(N0), VT,
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003697 N0.getOperand(0)->getOperand(0)),
3698 DAG.getConstant(c1 + c2, N1.getValueType()));
3699 }
3700 }
3701
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003702 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3703 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003704 // Only fold this if the inner shift has no other uses -- if it does, folding
3705 // this will increase the total number of instructions.
3706 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003707 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003708 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003709 if (c1 < VT.getSizeInBits()) {
3710 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003711 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3712 VT.getSizeInBits() - c1);
3713 SDValue Shift;
3714 if (c2 > c1) {
3715 Mask = Mask.shl(c2-c1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003716 Shift = DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003717 DAG.getConstant(c2-c1, N1.getValueType()));
3718 } else {
3719 Mask = Mask.lshr(c1-c2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003720 Shift = DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003721 DAG.getConstant(c1-c2, N1.getValueType()));
3722 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003723 return DAG.getNode(ISD::AND, SDLoc(N0), VT, Shift,
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003724 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003725 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003726 }
Bill Wendling88103372009-01-30 21:37:17 +00003727 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003728 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3729 SDValue HiBitsMask =
3730 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3731 VT.getSizeInBits() -
3732 N1C->getZExtValue()),
3733 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003734 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003735 HiBitsMask);
3736 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003737
Evan Chenge5b51ac2010-04-17 06:13:15 +00003738 if (N1C) {
3739 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3740 if (NewSHL.getNode())
3741 return NewSHL;
3742 }
3743
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003744 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003745}
3746
Dan Gohman475871a2008-07-27 21:46:04 +00003747SDValue DAGCombiner::visitSRA(SDNode *N) {
3748 SDValue N0 = N->getOperand(0);
3749 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003750 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3751 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003752 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003753 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003754
Bill Wendling88103372009-01-30 21:37:17 +00003755 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003756 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003757 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003758 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003759 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003760 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003761 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003762 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003763 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003764 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003765 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003766 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003767 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003768 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003769 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003770 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3771 // sext_inreg.
3772 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003773 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003774 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3775 if (VT.isVector())
3776 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3777 ExtVT, VT.getVectorNumElements());
3778 if ((!LegalOperations ||
3779 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003780 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003781 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003782 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003783
Bill Wendling88103372009-01-30 21:37:17 +00003784 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003785 if (N1C && N0.getOpcode() == ISD::SRA) {
3786 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003787 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003788 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003789 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003790 DAG.getConstant(Sum, N1C->getValueType(0)));
3791 }
3792 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003793
Bill Wendling88103372009-01-30 21:37:17 +00003794 // fold (sra (shl X, m), (sub result_size, n))
3795 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003796 // result_size - n != m.
3797 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003798 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003799 if (N0.getOpcode() == ISD::SHL) {
3800 // Get the two constanst of the shifts, CN0 = m, CN = n.
3801 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3802 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003803 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003804 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003805 EVT::getIntegerVT(*DAG.getContext(),
3806 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003807 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003808 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003809
Scott Michelfdc40a02009-02-17 22:15:04 +00003810 // If the shift is not a no-op (in which case this should be just a sign
3811 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003812 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003813 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003814 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003815 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3816 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003817 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003818
Owen Anderson95771af2011-02-25 21:41:48 +00003819 SDValue Amt = DAG.getConstant(ShiftAmt,
3820 getShiftAmountTy(N0.getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003821 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), VT,
Bill Wendling88103372009-01-30 21:37:17 +00003822 N0.getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003823 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), TruncVT,
Bill Wendling88103372009-01-30 21:37:17 +00003824 Shift);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003825 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003826 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003827 }
3828 }
3829 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003830
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003831 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003832 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003833 N1.getOperand(0).getOpcode() == ISD::AND &&
3834 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003835 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003836 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003837 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003838 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003839 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003840 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003841 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
3842 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003843 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003844 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003845 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00003846 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003847 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003848 }
3849 }
3850
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003851 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3852 // if c1 is equal to the number of bits the trunc removes
3853 if (N0.getOpcode() == ISD::TRUNCATE &&
3854 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3855 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3856 N0.getOperand(0).hasOneUse() &&
3857 N0.getOperand(0).getOperand(1).hasOneUse() &&
3858 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3859 EVT LargeVT = N0.getOperand(0).getValueType();
3860 ConstantSDNode *LargeShiftAmt =
3861 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3862
3863 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3864 LargeShiftAmt->getZExtValue()) {
3865 SDValue Amt =
3866 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003867 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003868 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), LargeVT,
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003869 N0.getOperand(0).getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003870 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, SRA);
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003871 }
3872 }
3873
Scott Michelfdc40a02009-02-17 22:15:04 +00003874 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003875 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3876 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003877
3878
Nate Begeman1d4d4142005-09-01 00:19:25 +00003879 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003880 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003881 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003882
Evan Chenge5b51ac2010-04-17 06:13:15 +00003883 if (N1C) {
3884 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3885 if (NewSRA.getNode())
3886 return NewSRA;
3887 }
3888
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003889 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003890}
3891
Dan Gohman475871a2008-07-27 21:46:04 +00003892SDValue DAGCombiner::visitSRL(SDNode *N) {
3893 SDValue N0 = N->getOperand(0);
3894 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003895 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3896 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003897 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003898 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003899
Nate Begeman1d4d4142005-09-01 00:19:25 +00003900 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003901 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003902 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003903 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003904 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003905 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003906 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003907 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003908 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003909 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003910 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003911 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003912 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003913 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003914 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003915 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003916
Bill Wendling88103372009-01-30 21:37:17 +00003917 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003918 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003919 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003920 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3921 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003922 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003923 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003924 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003925 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003926 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003927
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003928 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003929 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
3930 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003931 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003932 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003933 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3934 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003935 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3936 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003937 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003938 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003939 if (c1 + OpSizeInBits == InnerShiftSize) {
3940 if (c1 + c2 >= InnerShiftSize)
3941 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003942 return DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT,
3943 DAG.getNode(ISD::SRL, SDLoc(N0), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003944 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003945 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003946 }
3947 }
3948
Chris Lattnerefcddc32010-04-15 05:28:43 +00003949 // fold (srl (shl x, c), c) -> (and x, cst2)
3950 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
3951 N0.getValueSizeInBits() <= 64) {
3952 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003953 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattnerefcddc32010-04-15 05:28:43 +00003954 DAG.getConstant(~0ULL >> ShAmt, VT));
3955 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003956
Michael Liao2da86392013-06-21 18:45:27 +00003957 // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask)
Chris Lattner06afe072006-05-05 22:53:17 +00003958 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
3959 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00003960 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003961 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00003962 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00003963
Evan Chenge5b51ac2010-04-17 06:13:15 +00003964 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00003965 uint64_t ShiftAmt = N1C->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003966 SDValue SmallShift = DAG.getNode(ISD::SRL, SDLoc(N0), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00003967 N0.getOperand(0),
3968 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00003969 AddToWorkList(SmallShift.getNode());
Michael Liao2da86392013-06-21 18:45:27 +00003970 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits()).lshr(ShiftAmt);
3971 return DAG.getNode(ISD::AND, SDLoc(N), VT,
3972 DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SmallShift),
3973 DAG.getConstant(Mask, VT));
Evan Chenge5b51ac2010-04-17 06:13:15 +00003974 }
Chris Lattner06afe072006-05-05 22:53:17 +00003975 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003976
Chris Lattner3657ffe2006-10-12 20:23:19 +00003977 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
3978 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00003979 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00003980 if (N0.getOpcode() == ISD::SRA)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003981 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00003982 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003983
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003984 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00003985 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003986 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00003987 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003988 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00003989
Chris Lattner350bec02006-04-02 06:11:11 +00003990 // If any of the input bits are KnownOne, then the input couldn't be all
3991 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003992 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003993
Chris Lattner350bec02006-04-02 06:11:11 +00003994 // If all of the bits input the to ctlz node are known to be zero, then
3995 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003996 APInt UnknownBits = ~KnownZero;
Chris Lattner350bec02006-04-02 06:11:11 +00003997 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003998
Chris Lattner350bec02006-04-02 06:11:11 +00003999 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00004000 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00004001 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00004002 // could be set on input to the CTLZ node. If this bit is set, the SRL
4003 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
4004 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00004005 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00004006 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00004007
Chris Lattner350bec02006-04-02 06:11:11 +00004008 if (ShAmt) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004009 Op = DAG.getNode(ISD::SRL, SDLoc(N0), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00004010 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00004011 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00004012 }
Bill Wendling88103372009-01-30 21:37:17 +00004013
Andrew Trickac6d9be2013-05-25 02:42:55 +00004014 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling88103372009-01-30 21:37:17 +00004015 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00004016 }
4017 }
Evan Chengeb9f8922008-08-30 02:03:58 +00004018
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004019 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00004020 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00004021 N1.getOperand(0).getOpcode() == ISD::AND &&
4022 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00004023 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00004024 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004025 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00004026 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004027 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004028 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004029 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
4030 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00004031 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00004032 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004033 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00004034 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00004035 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00004036 }
4037 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004038
Chris Lattner61a4c072007-04-18 03:06:49 +00004039 // fold operands of srl based on knowledge that the low bits are not
4040 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00004041 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4042 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004043
Evan Cheng9ab2b982009-12-18 21:31:31 +00004044 if (N1C) {
4045 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
4046 if (NewSRL.getNode())
4047 return NewSRL;
4048 }
4049
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004050 // Attempt to convert a srl of a load into a narrower zero-extending load.
4051 SDValue NarrowLoad = ReduceLoadWidth(N);
4052 if (NarrowLoad.getNode())
4053 return NarrowLoad;
4054
Evan Cheng9ab2b982009-12-18 21:31:31 +00004055 // Here is a common situation. We want to optimize:
4056 //
4057 // %a = ...
4058 // %b = and i32 %a, 2
4059 // %c = srl i32 %b, 1
4060 // brcond i32 %c ...
4061 //
4062 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004063 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00004064 // %a = ...
4065 // %b = and %a, 2
4066 // %c = setcc eq %b, 0
4067 // brcond %c ...
4068 //
4069 // However when after the source operand of SRL is optimized into AND, the SRL
4070 // itself may not be optimized further. Look for it and add the BRCOND into
4071 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00004072 if (N->hasOneUse()) {
4073 SDNode *Use = *N->use_begin();
4074 if (Use->getOpcode() == ISD::BRCOND)
4075 AddToWorkList(Use);
4076 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
4077 // Also look pass the truncate.
4078 Use = *Use->use_begin();
4079 if (Use->getOpcode() == ISD::BRCOND)
4080 AddToWorkList(Use);
4081 }
4082 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00004083
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004084 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00004085}
4086
Dan Gohman475871a2008-07-27 21:46:04 +00004087SDValue DAGCombiner::visitCTLZ(SDNode *N) {
4088 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004089 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004090
4091 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004092 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004093 return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004094 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004095}
4096
Chandler Carruth63974b22011-12-13 01:56:10 +00004097SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
4098 SDValue N0 = N->getOperand(0);
4099 EVT VT = N->getValueType(0);
4100
4101 // fold (ctlz_zero_undef c1) -> c2
4102 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004103 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004104 return SDValue();
4105}
4106
Dan Gohman475871a2008-07-27 21:46:04 +00004107SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4108 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004109 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004110
Nate Begeman1d4d4142005-09-01 00:19:25 +00004111 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004112 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004113 return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004114 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004115}
4116
Chandler Carruth63974b22011-12-13 01:56:10 +00004117SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4118 SDValue N0 = N->getOperand(0);
4119 EVT VT = N->getValueType(0);
4120
4121 // fold (cttz_zero_undef c1) -> c2
4122 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004123 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004124 return SDValue();
4125}
4126
Dan Gohman475871a2008-07-27 21:46:04 +00004127SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4128 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004129 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004130
Nate Begeman1d4d4142005-09-01 00:19:25 +00004131 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004132 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004133 return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004134 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004135}
4136
Dan Gohman475871a2008-07-27 21:46:04 +00004137SDValue DAGCombiner::visitSELECT(SDNode *N) {
4138 SDValue N0 = N->getOperand(0);
4139 SDValue N1 = N->getOperand(1);
4140 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004141 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4142 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4143 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004144 EVT VT = N->getValueType(0);
4145 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00004146
Bill Wendling34584e62009-01-30 22:02:18 +00004147 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004148 if (N1 == N2)
4149 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004150 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004151 if (N0C && !N0C->isNullValue())
4152 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004153 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00004154 if (N0C && N0C->isNullValue())
4155 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00004156 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004157 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004158 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004159 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00004160 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004161 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00004162 (VT0.isInteger() &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00004163 TLI.getBooleanContents(false) ==
4164 TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004165 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004166 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00004167 if (VT == VT0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004168 return DAG.getNode(ISD::XOR, SDLoc(N), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004169 N0, DAG.getConstant(1, VT0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004170 XORNode = DAG.getNode(ISD::XOR, SDLoc(N0), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004171 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004172 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004173 if (VT.bitsGT(VT0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004174 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode);
4175 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00004176 }
Bill Wendling34584e62009-01-30 22:02:18 +00004177 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004178 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004179 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004180 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004181 return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004182 }
Bill Wendling34584e62009-01-30 22:02:18 +00004183 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004184 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004185 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004186 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004187 return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00004188 }
Bill Wendling34584e62009-01-30 22:02:18 +00004189 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004190 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004191 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Bill Wendling34584e62009-01-30 22:02:18 +00004192 // fold (select X, X, Y) -> (or X, Y)
4193 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004194 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004195 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004196 // fold (select X, Y, X) -> (and X, Y)
4197 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004198 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004199 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004200
Chris Lattner40c62d52005-10-18 06:04:22 +00004201 // If we can fold this based on the true/false value, do so.
4202 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00004203 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004204
Nate Begeman44728a72005-09-19 22:34:01 +00004205 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004206 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004207 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00004208 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00004209 // having to say they don't support SELECT_CC on every type the DAG knows
4210 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00004211 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00004212 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004213 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT,
Bill Wendling34584e62009-01-30 22:02:18 +00004214 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004215 N1, N2, N0.getOperand(2));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004216 return SimplifySelect(SDLoc(N), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004217 }
Bill Wendling34584e62009-01-30 22:02:18 +00004218
Dan Gohman475871a2008-07-27 21:46:04 +00004219 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00004220}
4221
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004222SDValue DAGCombiner::visitVSELECT(SDNode *N) {
4223 SDValue N0 = N->getOperand(0);
4224 SDValue N1 = N->getOperand(1);
4225 SDValue N2 = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004226 SDLoc DL(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004227
4228 // Canonicalize integer abs.
4229 // vselect (setg[te] X, 0), X, -X ->
4230 // vselect (setgt X, -1), X, -X ->
4231 // vselect (setl[te] X, 0), -X, X ->
4232 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4233 if (N0.getOpcode() == ISD::SETCC) {
4234 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
4235 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
4236 bool isAbs = false;
4237 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
4238
4239 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
4240 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) &&
4241 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1))
4242 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode());
4243 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) &&
4244 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1))
4245 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
4246
4247 if (isAbs) {
4248 EVT VT = LHS.getValueType();
4249 SDValue Shift = DAG.getNode(
4250 ISD::SRA, DL, VT, LHS,
4251 DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT));
4252 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
4253 AddToWorkList(Shift.getNode());
4254 AddToWorkList(Add.getNode());
4255 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift);
4256 }
4257 }
4258
4259 return SDValue();
4260}
4261
Dan Gohman475871a2008-07-27 21:46:04 +00004262SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4263 SDValue N0 = N->getOperand(0);
4264 SDValue N1 = N->getOperand(1);
4265 SDValue N2 = N->getOperand(2);
4266 SDValue N3 = N->getOperand(3);
4267 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00004268 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004269
Nate Begeman44728a72005-09-19 22:34:01 +00004270 // fold select_cc lhs, rhs, x, x, cc -> x
4271 if (N2 == N3)
4272 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004273
Chris Lattner5f42a242006-09-20 06:19:26 +00004274 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00004275 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004276 N0, N1, CC, SDLoc(N), false);
Stephen Lin7e6d6202013-06-15 04:03:33 +00004277 if (SCC.getNode()) {
4278 AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00004279
Stephen Lin7e6d6202013-06-15 04:03:33 +00004280 if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) {
4281 if (!SCCC->isNullValue())
4282 return N2; // cond always true -> true val
4283 else
4284 return N3; // cond always false -> false val
4285 }
4286
4287 // Fold to a simpler select_cc
4288 if (SCC.getOpcode() == ISD::SETCC)
4289 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(),
4290 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
4291 SCC.getOperand(2));
Chris Lattner5f42a242006-09-20 06:19:26 +00004292 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004293
Chris Lattner40c62d52005-10-18 06:04:22 +00004294 // If we can fold this based on the true/false value, do so.
4295 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00004296 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00004297
Nate Begeman44728a72005-09-19 22:34:01 +00004298 // fold select_cc into other things, such as min/max/abs
Andrew Trickac6d9be2013-05-25 02:42:55 +00004299 return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00004300}
4301
Dan Gohman475871a2008-07-27 21:46:04 +00004302SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004303 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004304 cast<CondCodeSDNode>(N->getOperand(2))->get(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004305 SDLoc(N));
Nate Begeman452d7be2005-09-16 00:54:12 +00004306}
4307
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004308// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00004309// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004310// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00004311// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00004312static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004313 unsigned ExtOpc,
4314 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00004315 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004316 bool HasCopyToRegUses = false;
4317 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00004318 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4319 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004320 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004321 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004322 if (User == N)
4323 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00004324 if (UI.getUse().getResNo() != N0.getResNo())
4325 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004326 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00004327 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004328 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4329 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4330 // Sign bits will be lost after a zext.
4331 return false;
4332 bool Add = false;
4333 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004334 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004335 if (UseOp == N0)
4336 continue;
4337 if (!isa<ConstantSDNode>(UseOp))
4338 return false;
4339 Add = true;
4340 }
4341 if (Add)
4342 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004343 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004344 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004345 // If truncates aren't free and there are users we can't
4346 // extend, it isn't worthwhile.
4347 if (!isTruncFree)
4348 return false;
4349 // Remember if this value is live-out.
4350 if (User->getOpcode() == ISD::CopyToReg)
4351 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004352 }
4353
4354 if (HasCopyToRegUses) {
4355 bool BothLiveOut = false;
4356 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4357 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004358 SDUse &Use = UI.getUse();
4359 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4360 BothLiveOut = true;
4361 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004362 }
4363 }
4364 if (BothLiveOut)
4365 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004366 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004367 return ExtendNodes.size();
4368 }
4369 return true;
4370}
4371
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004372void DAGCombiner::ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004373 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004374 ISD::NodeType ExtType) {
4375 // Extend SetCC uses if necessary.
4376 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4377 SDNode *SetCC = SetCCs[i];
4378 SmallVector<SDValue, 4> Ops;
4379
4380 for (unsigned j = 0; j != 2; ++j) {
4381 SDValue SOp = SetCC->getOperand(j);
4382 if (SOp == Trunc)
4383 Ops.push_back(ExtLoad);
4384 else
4385 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4386 }
4387
4388 Ops.push_back(SetCC->getOperand(2));
4389 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4390 &Ops[0], Ops.size()));
4391 }
4392}
4393
Dan Gohman475871a2008-07-27 21:46:04 +00004394SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4395 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004396 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004397
Nate Begeman1d4d4142005-09-01 00:19:25 +00004398 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004399 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004400 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004401
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004402 // fold (sext (sext x)) -> (sext x)
4403 // fold (sext (aext x)) -> (sext x)
4404 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004405 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT,
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004406 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004407
Chris Lattner22558872007-02-26 03:13:59 +00004408 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004409 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4410 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004411 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4412 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004413 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4414 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004415 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004416 // CombineTo deleted the truncate, if needed, but not what's under it.
4417 AddToWorkList(oye);
4418 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004419 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004420 }
Evan Chengc88138f2007-03-22 01:54:19 +00004421
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004422 // See if the value being truncated is already sign extended. If so, just
4423 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004424 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004425 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4426 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4427 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004428 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004429
Chris Lattner22558872007-02-26 03:13:59 +00004430 if (OpBits == DestBits) {
4431 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4432 // bits, it is already ready.
4433 if (NumSignBits > DestBits-MidBits)
4434 return Op;
4435 } else if (OpBits < DestBits) {
4436 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4437 // bits, just sext from i32.
4438 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004439 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004440 } else {
4441 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4442 // bits, just truncate to i32.
4443 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004444 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004445 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004446
Chris Lattner22558872007-02-26 03:13:59 +00004447 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004448 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4449 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004450 if (OpBits < DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004451 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004452 else if (OpBits > DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004453 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op);
4454 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004455 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004456 }
Chris Lattner6007b842006-09-21 06:00:20 +00004457 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004458
Evan Cheng110dec22005-12-14 02:19:23 +00004459 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004460 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004461 // on vectors in one instruction. We only perform this transformation on
4462 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004463 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004464 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004465 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004466 bool DoXform = true;
4467 SmallVector<SDNode*, 4> SetCCs;
4468 if (!N0.hasOneUse())
4469 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4470 if (DoXform) {
4471 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004472 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004473 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004474 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004475 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004476 LN0->isVolatile(), LN0->isNonTemporal(),
4477 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004478 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004479 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004480 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004481 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004482 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004483 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004484 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004485 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004486 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004487
4488 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4489 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004490 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4491 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004492 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004493 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004494 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004495 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004496 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004497 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004498 LN0->getBasePtr(), LN0->getPointerInfo(),
4499 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004500 LN0->isVolatile(), LN0->isNonTemporal(),
4501 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004502 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004503 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004504 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004505 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004506 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004507 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004508 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004509 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004510
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004511 // fold (sext (and/or/xor (load x), cst)) ->
4512 // (and/or/xor (sextload x), (sext cst))
4513 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4514 N0.getOpcode() == ISD::XOR) &&
4515 isa<LoadSDNode>(N0.getOperand(0)) &&
4516 N0.getOperand(1).getOpcode() == ISD::Constant &&
4517 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4518 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4519 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4520 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4521 bool DoXform = true;
4522 SmallVector<SDNode*, 4> SetCCs;
4523 if (!N0.hasOneUse())
4524 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4525 SetCCs, TLI);
4526 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004527 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004528 LN0->getChain(), LN0->getBasePtr(),
4529 LN0->getPointerInfo(),
4530 LN0->getMemoryVT(),
4531 LN0->isVolatile(),
4532 LN0->isNonTemporal(),
4533 LN0->getAlignment());
4534 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4535 Mask = Mask.sext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004536 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004537 ExtLoad, DAG.getConstant(Mask, VT));
4538 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004539 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004540 N0.getOperand(0).getValueType(), ExtLoad);
4541 CombineTo(N, And);
4542 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004543 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004544 ISD::SIGN_EXTEND);
4545 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4546 }
4547 }
4548 }
4549
Chris Lattner20a35c32007-04-11 05:32:27 +00004550 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004551 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004552 // Only do this before legalize for now.
Owen Andersoned5707b2013-04-23 18:09:28 +00004553 if (VT.isVector() && !LegalOperations &&
Stephen Lin155615d2013-07-08 00:37:03 +00004554 TLI.getBooleanContents(true) ==
Owen Andersoned5707b2013-04-23 18:09:28 +00004555 TargetLowering::ZeroOrNegativeOneBooleanContent) {
Dan Gohman3ce89f42010-04-30 17:19:19 +00004556 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem2e506192012-04-11 08:26:11 +00004557 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4558 // of the same size as the compared operands. Only optimize sext(setcc())
4559 // if this is the case.
Matt Arsenault225ed702013-05-18 00:21:46 +00004560 EVT SVT = getSetCCResultType(N0VT);
Nadav Rotem2e506192012-04-11 08:26:11 +00004561
4562 // We know that the # elements of the results is the same as the
4563 // # elements of the compare (and the # elements of the compare result
4564 // for that matter). Check to see that they are the same size. If so,
4565 // we know that the element size of the sext'd result matches the
4566 // element size of the compare operands.
4567 if (VT.getSizeInBits() == SVT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004568 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004569 N0.getOperand(1),
4570 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004571
Dan Gohman3ce89f42010-04-30 17:19:19 +00004572 // If the desired elements are smaller or larger than the source
4573 // elements we can use a matching integer vector type and then
4574 // truncate/sign extend
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004575 EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger();
Craig Topper0eb5dad2012-09-29 07:18:53 +00004576 if (SVT == MatchingVectorType) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004577 SDValue VsetCC = DAG.getSetCC(SDLoc(N), MatchingVectorType,
Craig Topper0eb5dad2012-09-29 07:18:53 +00004578 N0.getOperand(0), N0.getOperand(1),
4579 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004580 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Dan Gohman3ce89f42010-04-30 17:19:19 +00004581 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004582 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004583
Chris Lattner2b7a2712009-07-08 00:31:33 +00004584 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004585 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004586 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004587 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004588 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004589 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004590 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004591 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004592 if (SCC.getNode()) return SCC;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00004593 if (!VT.isVector() &&
4594 (!LegalOperations ||
4595 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(VT)))) {
4596 return DAG.getSelect(SDLoc(N), VT,
4597 DAG.getSetCC(SDLoc(N),
4598 getSetCCResultType(VT),
4599 N0.getOperand(0), N0.getOperand(1),
4600 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4601 NegOne, DAG.getConstant(0, VT));
4602 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004603 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004604
Dan Gohman8f0ad582008-04-28 16:58:24 +00004605 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004606 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004607 DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004608 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004609
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004610 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004611}
4612
Rafael Espindoladecbc432012-04-09 16:06:03 +00004613// isTruncateOf - If N is a truncate of some other value, return true, record
4614// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4615// This function computes KnownZero to avoid a duplicated call to
4616// ComputeMaskedBits in the caller.
4617static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4618 APInt &KnownZero) {
4619 APInt KnownOne;
4620 if (N->getOpcode() == ISD::TRUNCATE) {
4621 Op = N->getOperand(0);
4622 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4623 return true;
4624 }
4625
4626 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4627 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4628 return false;
4629
4630 SDValue Op0 = N->getOperand(0);
4631 SDValue Op1 = N->getOperand(1);
4632 assert(Op0.getValueType() == Op1.getValueType());
4633
4634 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4635 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004636 if (COp0 && COp0->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004637 Op = Op1;
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004638 else if (COp1 && COp1->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004639 Op = Op0;
4640 else
4641 return false;
4642
4643 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4644
4645 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4646 return false;
4647
4648 return true;
4649}
4650
Dan Gohman475871a2008-07-27 21:46:04 +00004651SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4652 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004653 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004654
Nate Begeman1d4d4142005-09-01 00:19:25 +00004655 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004656 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004657 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004658 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004659 // fold (zext (aext x)) -> (zext x)
4660 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004661 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004662 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004663
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004664 // fold (zext (truncate x)) -> (zext x) or
4665 // (zext (truncate x)) -> (truncate x)
4666 // This is valid when the truncated bits of x are already zero.
4667 // FIXME: We should extend this to work for vectors too.
Rafael Espindoladecbc432012-04-09 16:06:03 +00004668 SDValue Op;
4669 APInt KnownZero;
4670 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4671 APInt TruncatedBits =
4672 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4673 APInt(Op.getValueSizeInBits(), 0) :
4674 APInt::getBitsSet(Op.getValueSizeInBits(),
4675 N0.getValueSizeInBits(),
4676 std::min(Op.getValueSizeInBits(),
4677 VT.getSizeInBits()));
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004678 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004679 if (VT.bitsGT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004680 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004681 if (VT.bitsLT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004682 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004683
4684 return Op;
4685 }
4686 }
4687
Evan Chengc88138f2007-03-22 01:54:19 +00004688 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4689 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004690 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004691 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4692 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004693 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4694 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004695 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004696 // CombineTo deleted the truncate, if needed, but not what's under it.
4697 AddToWorkList(oye);
4698 }
Eli Friedmane545d382011-04-16 23:25:34 +00004699 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004700 }
Evan Chengc88138f2007-03-22 01:54:19 +00004701 }
4702
Chris Lattner6007b842006-09-21 06:00:20 +00004703 // fold (zext (truncate x)) -> (and x, mask)
4704 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004705 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004706
4707 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4708 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4709 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4710 if (NarrowLoad.getNode()) {
4711 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4712 if (NarrowLoad.getNode() != N0.getNode()) {
4713 CombineTo(N0.getNode(), NarrowLoad);
4714 // CombineTo deleted the truncate, if needed, but not what's under it.
4715 AddToWorkList(oye);
4716 }
4717 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4718 }
4719
Dan Gohman475871a2008-07-27 21:46:04 +00004720 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004721 if (Op.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004722 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004723 AddToWorkList(Op.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004724 } else if (Op.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004725 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004726 AddToWorkList(Op.getNode());
Chris Lattner6007b842006-09-21 06:00:20 +00004727 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00004728 return DAG.getZeroExtendInReg(Op, SDLoc(N),
Dan Gohman87862e72009-12-11 21:31:27 +00004729 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004730 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004731
Dan Gohman97121ba2009-04-08 00:15:30 +00004732 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4733 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004734 if (N0.getOpcode() == ISD::AND &&
4735 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004736 N0.getOperand(1).getOpcode() == ISD::Constant &&
4737 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4738 N0.getValueType()) ||
4739 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004740 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004741 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004742 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004743 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004744 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004745 }
Dan Gohman220a8232008-03-03 23:51:38 +00004746 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004747 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004748 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004749 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004750 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004751
Evan Cheng110dec22005-12-14 02:19:23 +00004752 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004753 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004754 // on vectors in one instruction. We only perform this transformation on
4755 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004756 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004757 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004758 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004759 bool DoXform = true;
4760 SmallVector<SDNode*, 4> SetCCs;
4761 if (!N0.hasOneUse())
4762 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4763 if (DoXform) {
4764 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004765 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004766 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004767 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004768 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004769 LN0->isVolatile(), LN0->isNonTemporal(),
4770 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004771 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004772 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004773 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004774 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004775
Andrew Trickac6d9be2013-05-25 02:42:55 +00004776 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004777 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004778 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004779 }
Evan Cheng110dec22005-12-14 02:19:23 +00004780 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004781
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004782 // fold (zext (and/or/xor (load x), cst)) ->
4783 // (and/or/xor (zextload x), (zext cst))
4784 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4785 N0.getOpcode() == ISD::XOR) &&
4786 isa<LoadSDNode>(N0.getOperand(0)) &&
4787 N0.getOperand(1).getOpcode() == ISD::Constant &&
4788 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4789 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4790 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4791 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4792 bool DoXform = true;
4793 SmallVector<SDNode*, 4> SetCCs;
4794 if (!N0.hasOneUse())
4795 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4796 SetCCs, TLI);
4797 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004798 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004799 LN0->getChain(), LN0->getBasePtr(),
4800 LN0->getPointerInfo(),
4801 LN0->getMemoryVT(),
4802 LN0->isVolatile(),
4803 LN0->isNonTemporal(),
4804 LN0->getAlignment());
4805 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4806 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004807 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004808 ExtLoad, DAG.getConstant(Mask, VT));
4809 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004810 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004811 N0.getOperand(0).getValueType(), ExtLoad);
4812 CombineTo(N, And);
4813 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004814 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004815 ISD::ZERO_EXTEND);
4816 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4817 }
4818 }
4819 }
4820
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004821 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4822 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004823 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4824 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004825 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004826 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004827 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004828 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004829 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004830 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004831 LN0->getBasePtr(), LN0->getPointerInfo(),
4832 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004833 LN0->isVolatile(), LN0->isNonTemporal(),
4834 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004835 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004836 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004837 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004838 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004839 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004840 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004841 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004842 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004843
Chris Lattner20a35c32007-04-11 05:32:27 +00004844 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004845 if (!LegalOperations && VT.isVector()) {
4846 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4847 // Only do this before legalize for now.
4848 EVT N0VT = N0.getOperand(0).getValueType();
4849 EVT EltVT = VT.getVectorElementType();
4850 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4851 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004852 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004853 // We know that the # elements of the results is the same as the
4854 // # elements of the compare (and the # elements of the compare result
4855 // for that matter). Check to see that they are the same size. If so,
4856 // we know that the element size of the sext'd result matches the
4857 // element size of the compare operands.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004858 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4859 DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004860 N0.getOperand(1),
4861 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004862 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Evan Cheng0a942db2010-05-19 01:08:17 +00004863 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004864
4865 // If the desired elements are smaller or larger than the source
4866 // elements we can use a matching integer vector type and then
4867 // truncate/sign extend
4868 EVT MatchingElementType =
4869 EVT::getIntegerVT(*DAG.getContext(),
4870 N0VT.getScalarType().getSizeInBits());
4871 EVT MatchingVectorType =
4872 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4873 N0VT.getVectorNumElements());
4874 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004875 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00004876 N0.getOperand(1),
4877 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004878 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4879 DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT),
4880 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Dan Gohman71dc7c92011-05-17 22:20:36 +00004881 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00004882 }
4883
4884 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004885 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004886 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00004887 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004888 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004889 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004890 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004891
Evan Cheng9818c042009-12-15 03:00:32 +00004892 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00004893 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00004894 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00004895 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4896 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00004897 SDValue ShAmt = N0.getOperand(1);
4898 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00004899 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00004900 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00004901 // If the original shl may be shifting out bits, do not perform this
4902 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00004903 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
4904 InnerZExt.getOperand(0).getValueType().getSizeInBits();
4905 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00004906 return SDValue();
4907 }
Chris Lattnere0751182011-02-13 19:09:16 +00004908
Andrew Trickac6d9be2013-05-25 02:42:55 +00004909 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00004910
4911 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00004912 if (VT.getSizeInBits() >= 256)
4913 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00004914
Chris Lattnere0751182011-02-13 19:09:16 +00004915 return DAG.getNode(N0.getOpcode(), DL, VT,
4916 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
4917 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00004918 }
4919
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004920 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004921}
4922
Dan Gohman475871a2008-07-27 21:46:04 +00004923SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
4924 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004925 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004926
Chris Lattner5ffc0662006-05-05 05:58:59 +00004927 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004928 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004929 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00004930 // fold (aext (aext x)) -> (aext x)
4931 // fold (aext (zext x)) -> (zext x)
4932 // fold (aext (sext x)) -> (sext x)
4933 if (N0.getOpcode() == ISD::ANY_EXTEND ||
4934 N0.getOpcode() == ISD::ZERO_EXTEND ||
4935 N0.getOpcode() == ISD::SIGN_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004936 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004937
Evan Chengc88138f2007-03-22 01:54:19 +00004938 // fold (aext (truncate (load x))) -> (aext (smaller load x))
4939 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
4940 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004941 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4942 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00004943 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4944 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004945 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00004946 // CombineTo deleted the truncate, if needed, but not what's under it.
4947 AddToWorkList(oye);
4948 }
Eli Friedmane545d382011-04-16 23:25:34 +00004949 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004950 }
Evan Chengc88138f2007-03-22 01:54:19 +00004951 }
4952
Chris Lattner84750582006-09-20 06:29:17 +00004953 // fold (aext (truncate x))
4954 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00004955 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00004956 if (TruncOp.getValueType() == VT)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00004957 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004958 if (TruncOp.getValueType().bitsGT(VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004959 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp);
4960 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00004961 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004962
Dan Gohman97121ba2009-04-08 00:15:30 +00004963 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
4964 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00004965 if (N0.getOpcode() == ISD::AND &&
4966 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004967 N0.getOperand(1).getOpcode() == ISD::Constant &&
4968 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4969 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004970 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004971 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004972 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004973 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004974 X = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00004975 }
Dan Gohman220a8232008-03-03 23:51:38 +00004976 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004977 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004978 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling683c9572009-01-30 22:27:33 +00004979 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00004980 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004981
Chris Lattner5ffc0662006-05-05 05:58:59 +00004982 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004983 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004984 // on vectors in one instruction. We only perform this transformation on
4985 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004986 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004987 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004988 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004989 bool DoXform = true;
4990 SmallVector<SDNode*, 4> SetCCs;
4991 if (!N0.hasOneUse())
4992 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
4993 if (DoXform) {
4994 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004995 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004996 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004997 LN0->getBasePtr(), LN0->getPointerInfo(),
Dan Gohman57fc82d2009-04-09 03:51:29 +00004998 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004999 LN0->isVolatile(), LN0->isNonTemporal(),
5000 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00005001 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005002 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Dan Gohman57fc82d2009-04-09 03:51:29 +00005003 N0.getValueType(), ExtLoad);
5004 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005005 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00005006 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00005007 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5008 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00005009 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005010
Chris Lattner5ffc0662006-05-05 05:58:59 +00005011 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
5012 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
5013 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00005014 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005015 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00005016 N0.hasOneUse()) {
5017 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005018 EVT MemVT = LN0->getMemoryVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005019 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(N),
Stuart Hastingsa9011292011-02-16 16:23:55 +00005020 VT, LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005021 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00005022 LN0->isVolatile(), LN0->isNonTemporal(),
5023 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00005024 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00005025 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005026 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling683c9572009-01-30 22:27:33 +00005027 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00005028 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005029 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00005030 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005031
Chris Lattner20a35c32007-04-11 05:32:27 +00005032 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00005033 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
5034 // Only do this before legalize for now.
5035 if (VT.isVector() && !LegalOperations) {
5036 EVT N0VT = N0.getOperand(0).getValueType();
5037 // We know that the # elements of the results is the same as the
5038 // # elements of the compare (and the # elements of the compare result
5039 // for that matter). Check to see that they are the same size. If so,
5040 // we know that the element size of the sext'd result matches the
5041 // element size of the compare operands.
5042 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005043 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005044 N0.getOperand(1),
5045 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00005046 // If the desired elements are smaller or larger than the source
5047 // elements we can use a matching integer vector type and then
5048 // truncate/sign extend
5049 else {
Duncan Sands34727662010-07-12 08:16:59 +00005050 EVT MatchingElementType =
5051 EVT::getIntegerVT(*DAG.getContext(),
5052 N0VT.getScalarType().getSizeInBits());
5053 EVT MatchingVectorType =
5054 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5055 N0VT.getVectorNumElements());
5056 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005057 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005058 N0.getOperand(1),
5059 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005060 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00005061 }
5062 }
5063
5064 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00005065 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005066 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00005067 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00005068 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00005069 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005070 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00005071 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005072
Evan Chengb3a3d5e2010-04-28 07:10:39 +00005073 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00005074}
5075
Chris Lattner2b4c2792007-10-13 06:35:54 +00005076/// GetDemandedBits - See if the specified operand can be simplified with the
5077/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00005078/// simpler operand, otherwise return a null SDValue.
5079SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005080 switch (V.getOpcode()) {
5081 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00005082 case ISD::Constant: {
5083 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
5084 assert(CV != 0 && "Const value should be ConstSDNode.");
5085 const APInt &CVal = CV->getAPIntValue();
5086 APInt NewVal = CVal & Mask;
5087 if (NewVal != CVal) {
5088 return DAG.getConstant(NewVal, V.getValueType());
5089 }
5090 break;
5091 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005092 case ISD::OR:
5093 case ISD::XOR:
5094 // If the LHS or RHS don't contribute bits to the or, drop them.
5095 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
5096 return V.getOperand(1);
5097 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
5098 return V.getOperand(0);
5099 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00005100 case ISD::SRL:
5101 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005102 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00005103 break;
5104 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
5105 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005106 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00005107
Dan Gohmancc91d632009-01-03 19:22:06 +00005108 // Watch out for shift count overflow though.
5109 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005110 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00005111 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00005112 if (SimplifyLHS.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005113 return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00005114 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00005115 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005116 }
Dan Gohman475871a2008-07-27 21:46:04 +00005117 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00005118}
5119
Evan Chengc88138f2007-03-22 01:54:19 +00005120/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
5121/// bits and then truncated to a narrower type and where N is a multiple
5122/// of number of bits of the narrower type, transform it to a narrower load
5123/// from address + N / num of bits of new type. If the result is to be
5124/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00005125SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00005126 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005127
Evan Chengc88138f2007-03-22 01:54:19 +00005128 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00005129 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005130 EVT VT = N->getValueType(0);
5131 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00005132
Dan Gohman7f8613e2008-08-14 20:04:46 +00005133 // This transformation isn't valid for vector loads.
5134 if (VT.isVector())
5135 return SDValue();
5136
Dan Gohmand1996362010-01-09 02:13:55 +00005137 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00005138 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00005139 if (Opc == ISD::SIGN_EXTEND_INREG) {
5140 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00005141 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005142 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00005143 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005144 ExtType = ISD::ZEXTLOAD;
5145 N0 = SDValue(N, 0);
5146 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5147 if (!N01) return SDValue();
5148 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5149 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00005150 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00005151 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5152 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005153
Owen Andersone50ed302009-08-10 22:56:29 +00005154 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00005155
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005156 // Do not generate loads of non-round integer types since these can
5157 // be expensive (and would be wrong if the type is not byte sized).
5158 if (!ExtVT.isRound())
5159 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005160
Evan Chengc88138f2007-03-22 01:54:19 +00005161 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005162 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00005163 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005164 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005165 // Is the shift amount a multiple of size of VT?
5166 if ((ShAmt & (EVTBits-1)) == 0) {
5167 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00005168 // Is the load width a multiple of size of VT?
5169 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005170 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005171 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005172
Chris Lattnercbf68df2010-12-22 08:02:57 +00005173 // At this point, we must have a load or else we can't do the transform.
5174 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005175
Chandler Carruth1c49fda2012-12-11 00:36:57 +00005176 // Because a SRL must be assumed to *need* to zero-extend the high bits
5177 // (as opposed to anyext the high bits), we can't combine the zextload
5178 // lowering of SRL and an sextload.
5179 if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD)
5180 return SDValue();
5181
Chris Lattner2831a192010-10-01 05:36:09 +00005182 // If the shift amount is larger than the input type then we're not
5183 // accessing any of the loaded bytes. If the load was a zextload/extload
5184 // then the result of the shift+trunc is zero/undef (handled elsewhere).
Chris Lattnercbf68df2010-12-22 08:02:57 +00005185 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00005186 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005187 }
5188 }
5189
Dan Gohman394d6292010-11-03 01:47:46 +00005190 // If the load is shifted left (and the result isn't shifted back right),
5191 // we can fold the truncate through the shift.
5192 unsigned ShLeftAmt = 0;
5193 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00005194 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00005195 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5196 ShLeftAmt = N01->getZExtValue();
5197 N0 = N0.getOperand(0);
5198 }
5199 }
Owen Anderson95771af2011-02-25 21:41:48 +00005200
Chris Lattner4c32bc22010-12-22 07:36:50 +00005201 // If we haven't found a load, we can't narrow it. Don't transform one with
5202 // multiple uses, this would require adding a new load.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005203 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse())
5204 return SDValue();
5205
5206 // Don't change the width of a volatile load.
5207 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5208 if (LN0->isVolatile())
Chris Lattner4c32bc22010-12-22 07:36:50 +00005209 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005210
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005211 // Verify that we are actually reducing a load width here.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005212 if (LN0->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00005213 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005214
Bill Schmidt89e88e32013-01-14 22:04:38 +00005215 // For the transform to be legal, the load must produce only two values
5216 // (the value loaded and the chain). Don't transform a pre-increment
Stephen Lin155615d2013-07-08 00:37:03 +00005217 // load, for example, which produces an extra value. Otherwise the
Bill Schmidt89e88e32013-01-14 22:04:38 +00005218 // transformation is not equivalent, and the downstream logic to replace
5219 // uses gets things wrong.
5220 if (LN0->getNumValues() > 2)
5221 return SDValue();
5222
Benjamin Kramerf4eeab42013-07-06 14:05:09 +00005223 // If the load that we're shrinking is an extload and we're not just
5224 // discarding the extension we can't simply shrink the load. Bail.
5225 // TODO: It would be possible to merge the extensions in some cases.
5226 if (LN0->getExtensionType() != ISD::NON_EXTLOAD &&
5227 LN0->getMemoryVT().getSizeInBits() < ExtVT.getSizeInBits() + ShAmt)
5228 return SDValue();
5229
Chris Lattner4c32bc22010-12-22 07:36:50 +00005230 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00005231
Evan Cheng16436df2012-06-26 01:19:33 +00005232 if (PtrType == MVT::Untyped || PtrType.isExtended())
5233 // It's not possible to generate a constant of extended or untyped type.
5234 return SDValue();
5235
Chris Lattner4c32bc22010-12-22 07:36:50 +00005236 // For big endian targets, we need to adjust the offset to the pointer to
5237 // load the correct bytes.
5238 if (TLI.isBigEndian()) {
5239 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5240 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5241 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00005242 }
5243
Chris Lattner4c32bc22010-12-22 07:36:50 +00005244 uint64_t PtrOff = ShAmt / 8;
5245 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005246 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0),
Chris Lattner4c32bc22010-12-22 07:36:50 +00005247 PtrType, LN0->getBasePtr(),
5248 DAG.getConstant(PtrOff, PtrType));
5249 AddToWorkList(NewPtr.getNode());
5250
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005251 SDValue Load;
5252 if (ExtType == ISD::NON_EXTLOAD)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005253 Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005254 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005255 LN0->isVolatile(), LN0->isNonTemporal(),
5256 LN0->isInvariant(), NewAlign);
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005257 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005258 Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005259 LN0->getPointerInfo().getWithOffset(PtrOff),
5260 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
5261 NewAlign);
Chris Lattner4c32bc22010-12-22 07:36:50 +00005262
5263 // Replace the old load's chain with the new load's chain.
5264 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005265 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005266
5267 // Shift the result left, if we've swallowed a left shift.
5268 SDValue Result = Load;
5269 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00005270 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005271 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5272 ShImmTy = VT;
Paul Redmond5c974502013-02-12 15:21:21 +00005273 // If the shift amount is as large as the result size (but, presumably,
5274 // no larger than the source) then the useful bits of the result are
5275 // zero; we can't simply return the shortened shift, because the result
5276 // of that operation is undefined.
5277 if (ShLeftAmt >= VT.getSizeInBits())
5278 Result = DAG.getConstant(0, VT);
5279 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005280 Result = DAG.getNode(ISD::SHL, SDLoc(N0), VT,
Paul Redmond5c974502013-02-12 15:21:21 +00005281 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005282 }
5283
5284 // Return the new loaded value.
5285 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00005286}
5287
Dan Gohman475871a2008-07-27 21:46:04 +00005288SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5289 SDValue N0 = N->getOperand(0);
5290 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005291 EVT VT = N->getValueType(0);
5292 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00005293 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00005294 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005295
Nate Begeman1d4d4142005-09-01 00:19:25 +00005296 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00005297 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005298 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005299
Chris Lattner541a24f2006-05-06 22:43:44 +00005300 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00005301 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00005302 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005303
Nate Begeman646d7e22005-09-02 21:18:40 +00005304 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5305 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00005306 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005307 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005308 N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00005309 }
Chris Lattner4b37e872006-05-08 21:18:59 +00005310
Dan Gohman75dcf082008-07-31 00:50:31 +00005311 // fold (sext_in_reg (sext x)) -> (sext x)
5312 // fold (sext_in_reg (aext x)) -> (sext x)
5313 // if x is small enough.
5314 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5315 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00005316 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5317 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005318 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00005319 }
5320
Chris Lattner95a5e052007-04-17 19:03:21 +00005321 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005322 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005323 return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005324
Chris Lattner95a5e052007-04-17 19:03:21 +00005325 // fold operands of sext_in_reg based on knowledge that the top bits are not
5326 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00005327 if (SimplifyDemandedBits(SDValue(N, 0)))
5328 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005329
Evan Chengc88138f2007-03-22 01:54:19 +00005330 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5331 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00005332 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005333 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00005334 return NarrowLoad;
5335
Bill Wendling8509c902009-01-30 22:33:24 +00005336 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005337 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00005338 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5339 if (N0.getOpcode() == ISD::SRL) {
5340 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00005341 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005342 // We can turn this into an SRA iff the input to the SRL is already sign
Chris Lattner4b37e872006-05-08 21:18:59 +00005343 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00005344 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00005345 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005346 return DAG.getNode(ISD::SRA, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005347 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00005348 }
5349 }
Evan Chengc88138f2007-03-22 01:54:19 +00005350
Nate Begemanded49632005-10-13 03:11:28 +00005351 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00005352 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005353 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005354 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005355 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005356 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005357 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005358 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005359 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005360 LN0->getBasePtr(), LN0->getPointerInfo(),
5361 EVT,
David Greene1e559442010-02-15 17:00:31 +00005362 LN0->isVolatile(), LN0->isNonTemporal(),
5363 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005364 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005365 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Elena Demikhovsky4b977312012-12-19 07:50:20 +00005366 AddToWorkList(ExtLoad.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005367 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005368 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005369 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00005370 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00005371 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005372 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005373 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005374 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005375 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005376 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005377 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005378 LN0->getBasePtr(), LN0->getPointerInfo(),
5379 EVT,
David Greene1e559442010-02-15 17:00:31 +00005380 LN0->isVolatile(), LN0->isNonTemporal(),
5381 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005382 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005383 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005384 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005385 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00005386
5387 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5388 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5389 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5390 N0.getOperand(1), false);
5391 if (BSwap.getNode() != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005392 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00005393 BSwap, N1);
5394 }
5395
Dan Gohman475871a2008-07-27 21:46:04 +00005396 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005397}
5398
Dan Gohman475871a2008-07-27 21:46:04 +00005399SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5400 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005401 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005402 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005403
5404 // noop truncate
5405 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00005406 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00005407 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005408 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005409 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005410 // fold (truncate (truncate x)) -> (truncate x)
5411 if (N0.getOpcode() == ISD::TRUNCATE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005412 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005413 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00005414 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5415 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00005416 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005417 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005418 // if the source is smaller than the dest, we still need an extend
Andrew Trickac6d9be2013-05-25 02:42:55 +00005419 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005420 N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005421 if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005422 // if the source is larger than the dest, than we just need the truncate
Andrew Trickac6d9be2013-05-25 02:42:55 +00005423 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005424 // if the source and dest are the same type, we can drop both the extend
5425 // and the truncate.
5426 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005427 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005428
Nadav Rotemcc870a82012-02-05 11:39:23 +00005429 // Fold extract-and-trunc into a narrow extract. For example:
5430 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5431 // i32 y = TRUNCATE(i64 x)
5432 // -- becomes --
5433 // v16i8 b = BITCAST (v2i64 val)
5434 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5435 //
5436 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005437 // creates this pattern) and before operation legalization after which
5438 // we need to be more careful about the vector instructions that we generate.
5439 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5440 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5441
5442 EVT VecTy = N0.getOperand(0).getValueType();
5443 EVT ExTy = N0.getValueType();
5444 EVT TrTy = N->getValueType(0);
5445
5446 unsigned NumElem = VecTy.getVectorNumElements();
5447 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5448
5449 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5450 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5451
5452 SDValue EltNo = N0->getOperand(1);
5453 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5454 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005455 EVT IndexTy = N0->getOperand(1).getValueType();
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005456 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5457
Andrew Trickac6d9be2013-05-25 02:42:55 +00005458 SDValue V = DAG.getNode(ISD::BITCAST, SDLoc(N),
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005459 NVT, N0.getOperand(0));
5460
5461 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005462 SDLoc(N), TrTy, V,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005463 DAG.getConstant(Index, IndexTy));
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005464 }
5465 }
5466
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005467 // Fold a series of buildvector, bitcast, and truncate if possible.
5468 // For example fold
5469 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to
5470 // (2xi32 (buildvector x, y)).
5471 if (Level == AfterLegalizeVectorOps && VT.isVector() &&
5472 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
5473 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
5474 N0.getOperand(0).hasOneUse()) {
5475
5476 SDValue BuildVect = N0.getOperand(0);
5477 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType();
5478 EVT TruncVecEltTy = VT.getVectorElementType();
5479
5480 // Check that the element types match.
5481 if (BuildVectEltTy == TruncVecEltTy) {
5482 // Now we only need to compute the offset of the truncated elements.
5483 unsigned BuildVecNumElts = BuildVect.getNumOperands();
5484 unsigned TruncVecNumElts = VT.getVectorNumElements();
5485 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
5486
5487 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&
5488 "Invalid number of elements");
5489
5490 SmallVector<SDValue, 8> Opnds;
5491 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset)
5492 Opnds.push_back(BuildVect.getOperand(i));
5493
Andrew Trickac6d9be2013-05-25 02:42:55 +00005494 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0],
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005495 Opnds.size());
5496 }
5497 }
5498
Chris Lattner2b4c2792007-10-13 06:35:54 +00005499 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005500 // only the low bits are being used.
5501 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005502 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005503 // may have different active low bits.
5504 if (!VT.isVector()) {
5505 SDValue Shorter =
5506 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5507 VT.getSizeInBits()));
5508 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005509 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter);
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005510 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005511 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005512 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005513 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5514 SDValue Reduced = ReduceLoadWidth(N);
5515 if (Reduced.getNode())
5516 return Reduced;
5517 }
Michael Liao07edaf32012-10-17 23:45:54 +00005518 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)),
5519 // where ... are all 'undef'.
5520 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) {
5521 SmallVector<EVT, 8> VTs;
5522 SDValue V;
5523 unsigned Idx = 0;
5524 unsigned NumDefs = 0;
5525
5526 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
5527 SDValue X = N0.getOperand(i);
5528 if (X.getOpcode() != ISD::UNDEF) {
5529 V = X;
5530 Idx = i;
5531 NumDefs++;
5532 }
5533 // Stop if more than one members are non-undef.
5534 if (NumDefs > 1)
5535 break;
5536 VTs.push_back(EVT::getVectorVT(*DAG.getContext(),
5537 VT.getVectorElementType(),
5538 X.getValueType().getVectorNumElements()));
5539 }
5540
5541 if (NumDefs == 0)
5542 return DAG.getUNDEF(VT);
5543
5544 if (NumDefs == 1) {
5545 assert(V.getNode() && "The single defined operand is empty!");
5546 SmallVector<SDValue, 8> Opnds;
5547 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
5548 if (i != Idx) {
5549 Opnds.push_back(DAG.getUNDEF(VTs[i]));
5550 continue;
5551 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005552 SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V);
Michael Liao07edaf32012-10-17 23:45:54 +00005553 AddToWorkList(NV.getNode());
5554 Opnds.push_back(NV);
5555 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005556 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Michael Liao07edaf32012-10-17 23:45:54 +00005557 &Opnds[0], Opnds.size());
5558 }
5559 }
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005560
5561 // Simplify the operands using demanded-bits information.
5562 if (!VT.isVector() &&
5563 SimplifyDemandedBits(SDValue(N, 0)))
5564 return SDValue(N, 0);
5565
Evan Chenge5b51ac2010-04-17 06:13:15 +00005566 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005567}
5568
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005569static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005570 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005571 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005572 return Elt.getNode();
5573 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005574}
5575
5576/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005577/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005578SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005579 assert(N->getOpcode() == ISD::BUILD_PAIR);
5580
Nate Begemanabc01992009-06-05 21:37:30 +00005581 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5582 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005583 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5584 LD1->getPointerInfo().getAddrSpace() !=
5585 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005586 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005587 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005588
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005589 if (ISD::isNON_EXTLoad(LD2) &&
5590 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005591 // If both are volatile this would reduce the number of volatile loads.
5592 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005593 !LD1->isVolatile() &&
5594 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005595 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005596 unsigned Align = LD1->getAlignment();
Micah Villmow3574eca2012-10-08 16:38:25 +00005597 unsigned NewAlign = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005598 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005599
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005600 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005601 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005602 return DAG.getLoad(VT, SDLoc(N), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005603 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005604 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005605 }
Bill Wendling67a67682009-01-30 22:44:24 +00005606
Dan Gohman475871a2008-07-27 21:46:04 +00005607 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005608}
5609
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005610SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005611 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005612 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005613
Dan Gohman7f321562007-06-25 16:23:39 +00005614 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5615 // Only do this before legalize, since afterward the target may be depending
5616 // on the bitconvert.
5617 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005618 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005619 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005620 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005621 bool isSimple = true;
5622 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5623 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5624 N0.getOperand(i).getOpcode() != ISD::Constant &&
5625 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005626 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005627 break;
5628 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005629
Owen Andersone50ed302009-08-10 22:56:29 +00005630 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005631 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005632 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005633 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005634 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005635 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005636
Dan Gohman3dd168d2008-09-05 01:58:21 +00005637 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005638 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005639 SDValue Res = DAG.getNode(ISD::BITCAST, SDLoc(N), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005640 if (Res.getNode() != N) {
5641 if (!LegalOperations ||
5642 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5643 return Res;
5644
5645 // Folding it resulted in an illegal node, and it's too late to
5646 // do that. Clean up the old node and forego the transformation.
5647 // Ideally this won't happen very often, because instcombine
5648 // and the earlier dagcombine runs (where illegal nodes are
5649 // permitted) should have folded most of them already.
5650 DAG.DeleteNode(Res.getNode());
5651 }
Chris Lattner94683772005-12-23 05:30:37 +00005652 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005653
Bill Wendling67a67682009-01-30 22:44:24 +00005654 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005655 if (N0.getOpcode() == ISD::BITCAST)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005656 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005657 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005658
Chris Lattner57104102005-12-23 05:44:41 +00005659 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005660 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005661 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005662 // Do not change the width of a volatile load.
5663 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005664 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005665 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Micah Villmow3574eca2012-10-08 16:38:25 +00005666 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005667 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005668 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005669
Evan Cheng59d5b682007-05-07 21:27:48 +00005670 if (Align <= OrigAlign) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005671 SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005672 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005673 LN0->isVolatile(), LN0->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005674 LN0->isInvariant(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00005675 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005676 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005677 DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005678 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005679 Load.getValue(1));
5680 return Load;
5681 }
Chris Lattner57104102005-12-23 05:44:41 +00005682 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005683
Bill Wendling67a67682009-01-30 22:44:24 +00005684 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5685 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005686 // This often reduces constant pool loads.
Owen Anderson29f60f32012-04-02 22:10:29 +00005687 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(VT)) ||
5688 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(VT))) &&
Nadav Rotem91a7e012012-09-13 14:54:28 +00005689 N0.getNode()->hasOneUse() && VT.isInteger() &&
5690 !VT.isVector() && !N0.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005691 SDValue NewConv = DAG.getNode(ISD::BITCAST, SDLoc(N0), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005692 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005693 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005694
Duncan Sands83ec4b62008-06-06 12:08:01 +00005695 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005696 if (N0.getOpcode() == ISD::FNEG)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005697 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005698 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005699 assert(N0.getOpcode() == ISD::FABS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005700 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005701 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005702 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005703
Bill Wendling67a67682009-01-30 22:44:24 +00005704 // fold (bitconvert (fcopysign cst, x)) ->
5705 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5706 // Note that we don't handle (copysign x, cst) because this can always be
5707 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005708 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005709 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005710 VT.isInteger() && !VT.isVector()) {
5711 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005712 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005713 if (isTypeLegal(IntXVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005714 SDValue X = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005715 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005716 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005717
Duncan Sands25cf2272008-11-24 14:53:14 +00005718 // If X has a different width than the result/lhs, sext it or truncate it.
5719 unsigned VTWidth = VT.getSizeInBits();
5720 if (OrigXWidth < VTWidth) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005721 X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005722 AddToWorkList(X.getNode());
5723 } else if (OrigXWidth > VTWidth) {
5724 // To get the sign bit in the right place, we have to shift it right
5725 // before truncating.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005726 X = DAG.getNode(ISD::SRL, SDLoc(X),
Bill Wendling67a67682009-01-30 22:44:24 +00005727 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005728 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5729 AddToWorkList(X.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005730 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005731 AddToWorkList(X.getNode());
5732 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005733
Duncan Sands25cf2272008-11-24 14:53:14 +00005734 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005735 X = DAG.getNode(ISD::AND, SDLoc(X), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005736 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005737 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005738
Andrew Trickac6d9be2013-05-25 02:42:55 +00005739 SDValue Cst = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005740 VT, N0.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005741 Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005742 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005743 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005744
Andrew Trickac6d9be2013-05-25 02:42:55 +00005745 return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005746 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005747 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005748
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005749 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005750 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005751 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5752 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005753 return CombineLD;
5754 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005755
Dan Gohman475871a2008-07-27 21:46:04 +00005756 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005757}
5758
Dan Gohman475871a2008-07-27 21:46:04 +00005759SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005760 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005761 return CombineConsecutiveLoads(N, VT);
5762}
5763
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005764/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005765/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005766/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005767SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005768ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005769 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005770
Chris Lattner6258fb22006-04-02 02:53:43 +00005771 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005772 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005773
Duncan Sands83ec4b62008-06-06 12:08:01 +00005774 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5775 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005776
Chris Lattner6258fb22006-04-02 02:53:43 +00005777 // If this is a conversion of N elements of one type to N elements of another
5778 // type, convert each element. This handles FP<->INT cases.
5779 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005780 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5781 BV->getValueType(0).getVectorNumElements());
5782
5783 // Due to the FP element handling below calling this routine recursively,
5784 // we can end up with a scalar-to-vector node here.
5785 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005786 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
5787 DAG.getNode(ISD::BITCAST, SDLoc(BV),
Nate Begemane0efc212010-07-27 18:02:18 +00005788 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005789
Dan Gohman475871a2008-07-27 21:46:04 +00005790 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005791 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005792 SDValue Op = BV->getOperand(i);
5793 // If the vector element type is not legal, the BUILD_VECTOR operands
5794 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005795 if (Op.getValueType() != SrcEltVT)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005796 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op);
5797 Ops.push_back(DAG.getNode(ISD::BITCAST, SDLoc(BV),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005798 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005799 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005800 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005801 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005802 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005803 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005804
Chris Lattner6258fb22006-04-02 02:53:43 +00005805 // Otherwise, we're growing or shrinking the elements. To avoid having to
5806 // handle annoying details of growing/shrinking FP values, we convert them to
5807 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005808 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005809 // Convert the input float vector to a int vector where the elements are the
5810 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005811 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005812 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005813 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005814 SrcEltVT = IntVT;
5815 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005816
Chris Lattner6258fb22006-04-02 02:53:43 +00005817 // Now we know the input is an integer vector. If the output is a FP type,
5818 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005819 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005820 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005821 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005822 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005823
Chris Lattner6258fb22006-04-02 02:53:43 +00005824 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005825 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005826 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005827
Chris Lattner6258fb22006-04-02 02:53:43 +00005828 // Okay, we know the src/dst types are both integers of differing types.
5829 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005830 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005831 if (SrcBitSize < DstBitSize) {
5832 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005833
Dan Gohman475871a2008-07-27 21:46:04 +00005834 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005835 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005836 i += NumInputsPerOutput) {
5837 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005838 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005839 bool EltIsUndef = true;
5840 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5841 // Shift the previously computed bits over.
5842 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005843 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005844 if (Op.getOpcode() == ISD::UNDEF) continue;
5845 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005846
Jay Foad40f8f622010-12-07 08:25:19 +00005847 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005848 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005849 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005850
Chris Lattner6258fb22006-04-02 02:53:43 +00005851 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005852 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005853 else
5854 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5855 }
5856
Owen Anderson23b9b192009-08-12 00:36:31 +00005857 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005858 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005859 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005860 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005861
Chris Lattner6258fb22006-04-02 02:53:43 +00005862 // Finally, this must be the case where we are shrinking elements: each input
5863 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005864 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005865 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005866 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5867 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005868 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005869
Dan Gohman7f321562007-06-25 16:23:39 +00005870 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005871 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5872 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005873 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005874 continue;
5875 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005876
Jay Foad40f8f622010-12-07 08:25:19 +00005877 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5878 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005879
Chris Lattner6258fb22006-04-02 02:53:43 +00005880 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00005881 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005882 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00005883 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00005884 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005885 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
Bill Wendlingb0162f52009-01-30 22:53:48 +00005886 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00005887 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005888 }
5889
5890 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00005891 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00005892 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
5893 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005894
Andrew Trickac6d9be2013-05-25 02:42:55 +00005895 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005896 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005897}
5898
Dan Gohman475871a2008-07-27 21:46:04 +00005899SDValue DAGCombiner::visitFADD(SDNode *N) {
5900 SDValue N0 = N->getOperand(0);
5901 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005902 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5903 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005904 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005905
Dan Gohman7f321562007-06-25 16:23:39 +00005906 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005907 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005908 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005909 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005910 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005911
Lang Hames01806942012-06-14 20:37:15 +00005912 // fold (fadd c1, c2) -> c1 + c2
Ulrich Weigande669c932012-10-29 18:35:49 +00005913 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005914 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005915 // canonicalize constant to RHS
5916 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005917 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N0);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005918 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005919 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5920 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005921 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005922 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005923 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005924 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005925 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005926 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00005927 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Andersonafd3d562012-03-06 00:29:31 +00005928 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005929 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005930 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00005931 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005932
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005933 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005934 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5935 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
5936 isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005937 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0.getOperand(0),
5938 DAG.getNode(ISD::FADD, SDLoc(N), VT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005939 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005940
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005941 // No FP constant should be created after legalization as Instruction
5942 // Selection pass has hard time in dealing with FP constant.
5943 //
5944 // We don't need test this condition for transformation like following, as
5945 // the DAG being transformed implies it is legal to take FP constant as
5946 // operand.
Stephen Lin155615d2013-07-08 00:37:03 +00005947 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005948 // (fadd (fmul c, x), x) -> (fmul c+1, x)
Stephen Lin155615d2013-07-08 00:37:03 +00005949 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005950 bool AllowNewFpConst = (Level < AfterLegalizeDAG);
5951
Owen Anderson607ebde2012-11-01 02:00:53 +00005952 // If allow, fold (fadd (fneg x), x) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005953 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Owen Anderson607ebde2012-11-01 02:00:53 +00005954 N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1) {
5955 return DAG.getConstantFP(0.0, VT);
5956 }
5957
5958 // If allow, fold (fadd x, (fneg x)) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005959 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Owen Anderson607ebde2012-11-01 02:00:53 +00005960 N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0) {
5961 return DAG.getConstantFP(0.0, VT);
5962 }
5963
Owen Anderson43da6c72012-08-30 23:35:16 +00005964 // In unsafe math mode, we can fold chains of FADD's of the same value
5965 // into multiplications. This transform is not safe in general because
5966 // we are reducing the number of rounding steps.
5967 if (DAG.getTarget().Options.UnsafeFPMath &&
5968 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
5969 !N0CFP && !N1CFP) {
5970 if (N0.getOpcode() == ISD::FMUL) {
5971 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
5972 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
5973
Stephen Lin38103d12013-06-14 18:17:35 +00005974 // (fadd (fmul c, x), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00005975 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005976 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005977 SDValue(CFP00, 0),
5978 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005979 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005980 N1, NewCFP);
5981 }
5982
Stephen Lin38103d12013-06-14 18:17:35 +00005983 // (fadd (fmul x, c), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00005984 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005985 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005986 SDValue(CFP01, 0),
5987 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005988 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005989 N1, NewCFP);
5990 }
5991
Stephen Lin38103d12013-06-14 18:17:35 +00005992 // (fadd (fmul c, x), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00005993 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
5994 N1.getOperand(0) == N1.getOperand(1) &&
5995 N0.getOperand(1) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005996 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00005997 SDValue(CFP00, 0),
5998 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005999 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006000 N0.getOperand(1), NewCFP);
6001 }
6002
Stephen Lin38103d12013-06-14 18:17:35 +00006003 // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00006004 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
6005 N1.getOperand(0) == N1.getOperand(1) &&
6006 N0.getOperand(0) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006007 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006008 SDValue(CFP01, 0),
6009 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006010 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006011 N0.getOperand(0), NewCFP);
6012 }
6013 }
6014
6015 if (N1.getOpcode() == ISD::FMUL) {
6016 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
6017 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
6018
Stephen Lin38103d12013-06-14 18:17:35 +00006019 // (fadd x, (fmul c, x)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006020 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006021 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006022 SDValue(CFP10, 0),
6023 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006024 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006025 N0, NewCFP);
6026 }
6027
Stephen Lin38103d12013-06-14 18:17:35 +00006028 // (fadd x, (fmul x, c)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006029 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006030 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006031 SDValue(CFP11, 0),
6032 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006033 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006034 N0, NewCFP);
6035 }
6036
Owen Anderson43da6c72012-08-30 23:35:16 +00006037
Stephen Lin38103d12013-06-14 18:17:35 +00006038 // (fadd (fadd x, x), (fmul c, x)) -> (fmul x, c+2)
6039 if (CFP10 && !CFP11 && N0.getOpcode() == ISD::FADD &&
6040 N0.getOperand(0) == N0.getOperand(1) &&
6041 N1.getOperand(1) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006042 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006043 SDValue(CFP10, 0),
6044 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006045 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006046 N1.getOperand(1), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006047 }
6048
Stephen Lin38103d12013-06-14 18:17:35 +00006049 // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2)
6050 if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD &&
6051 N0.getOperand(0) == N0.getOperand(1) &&
6052 N1.getOperand(0) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006053 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006054 SDValue(CFP11, 0),
6055 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006056 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006057 N1.getOperand(0), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006058 }
6059 }
6060
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006061 if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006062 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006063 // (fadd (fadd x, x), x) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006064 if (!CFP && N0.getOperand(0) == N0.getOperand(1) &&
6065 (N0.getOperand(0) == N1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006066 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006067 N1, DAG.getConstantFP(3.0, VT));
6068 }
6069 }
6070
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006071 if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006072 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006073 // (fadd x, (fadd x, x)) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006074 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
6075 N1.getOperand(0) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006076 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006077 N0, DAG.getConstantFP(3.0, VT));
6078 }
6079 }
6080
Stephen Lina553bed2013-06-14 21:33:58 +00006081 // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0)
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006082 if (AllowNewFpConst &&
6083 N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
Owen Anderson43da6c72012-08-30 23:35:16 +00006084 N0.getOperand(0) == N0.getOperand(1) &&
6085 N1.getOperand(0) == N1.getOperand(1) &&
6086 N0.getOperand(0) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006087 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006088 N0.getOperand(0),
6089 DAG.getConstantFP(4.0, VT));
6090 }
6091 }
6092
Lang Hamesd693caf2012-06-19 22:51:23 +00006093 // FADD -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006094 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006095 DAG.getTarget().Options.UnsafeFPMath) &&
6096 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006097 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006098
6099 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
6100 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006101 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006102 N0.getOperand(0), N0.getOperand(1), N1);
6103 }
Owen Anderson43da6c72012-08-30 23:35:16 +00006104
Michael Liaob79bff52012-09-01 04:09:16 +00006105 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hamesd693caf2012-06-19 22:51:23 +00006106 // Note: Commutes FADD operands.
6107 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006108 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006109 N1.getOperand(0), N1.getOperand(1), N0);
6110 }
6111 }
6112
Dan Gohman475871a2008-07-27 21:46:04 +00006113 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006114}
6115
Dan Gohman475871a2008-07-27 21:46:04 +00006116SDValue DAGCombiner::visitFSUB(SDNode *N) {
6117 SDValue N0 = N->getOperand(0);
6118 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006119 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6120 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006121 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006122 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00006123
Dan Gohman7f321562007-06-25 16:23:39 +00006124 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006125 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006126 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006127 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006128 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006129
Nate Begemana0e221d2005-10-18 00:28:13 +00006130 // fold (fsub c1, c2) -> c1-c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006131 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006132 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00006133 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006134 if (DAG.getTarget().Options.UnsafeFPMath &&
6135 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00006136 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00006137 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006138 if (DAG.getTarget().Options.UnsafeFPMath &&
6139 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006140 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006141 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00006142 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006143 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00006144 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00006145 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00006146 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006147 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00006148 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00006149
Bill Wendling5a894342012-03-15 05:12:00 +00006150 // If 'unsafe math' is enabled, fold
Owen Anderson713e9532012-05-07 20:51:25 +00006151 // (fsub x, x) -> 0.0 &
Bill Wendling5a894342012-03-15 05:12:00 +00006152 // (fsub x, (fadd x, y)) -> (fneg y) &
6153 // (fsub x, (fadd y, x)) -> (fneg y)
6154 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Anderson713e9532012-05-07 20:51:25 +00006155 if (N0 == N1)
6156 return DAG.getConstantFP(0.0f, VT);
6157
Bill Wendling5a894342012-03-15 05:12:00 +00006158 if (N1.getOpcode() == ISD::FADD) {
6159 SDValue N10 = N1->getOperand(0);
6160 SDValue N11 = N1->getOperand(1);
6161
6162 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
6163 &DAG.getTarget().Options))
6164 return GetNegatedExpression(N11, DAG, LegalOperations);
6165 else if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
6166 &DAG.getTarget().Options))
6167 return GetNegatedExpression(N10, DAG, LegalOperations);
6168 }
6169 }
6170
Lang Hamesd693caf2012-06-19 22:51:23 +00006171 // FSUB -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006172 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006173 DAG.getTarget().Options.UnsafeFPMath) &&
6174 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006175 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006176
6177 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
6178 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006179 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006180 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006181 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hamesd693caf2012-06-19 22:51:23 +00006182 }
6183
6184 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
6185 // Note: Commutes FSUB operands.
6186 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006187 return DAG.getNode(ISD::FMA, dl, VT,
6188 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006189 N1.getOperand(0)),
6190 N1.getOperand(1), N0);
6191 }
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006192
6193 // fold (fsub (-(fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
Stephen Lin155615d2013-07-08 00:37:03 +00006194 if (N0.getOpcode() == ISD::FNEG &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006195 N0.getOperand(0).getOpcode() == ISD::FMUL &&
6196 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
6197 SDValue N00 = N0.getOperand(0).getOperand(0);
6198 SDValue N01 = N0.getOperand(0).getOperand(1);
6199 return DAG.getNode(ISD::FMA, dl, VT,
6200 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
6201 DAG.getNode(ISD::FNEG, dl, VT, N1));
6202 }
Lang Hamesd693caf2012-06-19 22:51:23 +00006203 }
6204
Dan Gohman475871a2008-07-27 21:46:04 +00006205 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006206}
6207
Dan Gohman475871a2008-07-27 21:46:04 +00006208SDValue DAGCombiner::visitFMUL(SDNode *N) {
6209 SDValue N0 = N->getOperand(0);
6210 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006211 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6212 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006213 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006214 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006215
Dan Gohman7f321562007-06-25 16:23:39 +00006216 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006217 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006218 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006219 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006220 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006221
Nate Begeman11af4ea2005-10-17 20:40:11 +00006222 // fold (fmul c1, c2) -> c1*c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006223 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006224 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006225 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00006226 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006227 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N1, N0);
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006228 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006229 if (DAG.getTarget().Options.UnsafeFPMath &&
6230 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00006231 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00006232 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006233 if (DAG.getTarget().Options.UnsafeFPMath &&
6234 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00006235 return N1;
Owen Anderson363e4b92012-05-02 21:32:35 +00006236 // fold (fmul A, 1.0) -> A
6237 if (N1CFP && N1CFP->isExactlyValue(1.0))
6238 return N0;
Nate Begeman11af4ea2005-10-17 20:40:11 +00006239 // fold (fmul X, 2.0) -> (fadd X, X)
6240 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006241 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00006242 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00006243 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00006244 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006245 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006246
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006247 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006248 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006249 &DAG.getTarget().Options)) {
Stephen Lin155615d2013-07-08 00:37:03 +00006250 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006251 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006252 // Both can be negated for free, check to see if at least one is cheaper
6253 // negated.
6254 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006255 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006256 GetNegatedExpression(N0, DAG, LegalOperations),
6257 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006258 }
6259 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006260
Chris Lattnerddae4bd2007-01-08 23:04:05 +00006261 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006262 if (DAG.getTarget().Options.UnsafeFPMath &&
6263 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006264 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006265 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0),
6266 DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Dale Johannesende064702009-02-06 21:50:26 +00006267 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006268
Dan Gohman475871a2008-07-27 21:46:04 +00006269 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006270}
6271
Owen Anderson062c0a52012-05-02 22:17:40 +00006272SDValue DAGCombiner::visitFMA(SDNode *N) {
6273 SDValue N0 = N->getOperand(0);
6274 SDValue N1 = N->getOperand(1);
6275 SDValue N2 = N->getOperand(2);
6276 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6277 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6278 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006279 SDLoc dl(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00006280
Owen Anderson607ebde2012-11-01 02:00:53 +00006281 if (DAG.getTarget().Options.UnsafeFPMath) {
6282 if (N0CFP && N0CFP->isZero())
6283 return N2;
6284 if (N1CFP && N1CFP->isZero())
6285 return N2;
6286 }
Owen Anderson062c0a52012-05-02 22:17:40 +00006287 if (N0CFP && N0CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006288 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006289 if (N1CFP && N1CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006290 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006291
Owen Anderson85ef6f42012-05-30 18:50:39 +00006292 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Andersonf917d202012-05-30 18:54:50 +00006293 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006294 return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
Owen Anderson85ef6f42012-05-30 18:50:39 +00006295
Owen Anderson58d57292012-09-01 06:04:27 +00006296 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6297 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6298 N2.getOpcode() == ISD::FMUL &&
6299 N0 == N2.getOperand(0) &&
6300 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6301 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6302 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6303 }
6304
6305
6306 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6307 if (DAG.getTarget().Options.UnsafeFPMath &&
6308 N0.getOpcode() == ISD::FMUL && N1CFP &&
6309 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6310 return DAG.getNode(ISD::FMA, dl, VT,
6311 N0.getOperand(0),
6312 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6313 N2);
6314 }
6315
6316 // (fma x, 1, y) -> (fadd x, y)
6317 // (fma x, -1, y) -> (fadd (fneg x), y)
6318 if (N1CFP) {
6319 if (N1CFP->isExactlyValue(1.0))
6320 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6321
6322 if (N1CFP->isExactlyValue(-1.0) &&
6323 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6324 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6325 AddToWorkList(RHSNeg.getNode());
6326 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6327 }
6328 }
6329
6330 // (fma x, c, x) -> (fmul x, (c+1))
6331 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2) {
6332 return DAG.getNode(ISD::FMUL, dl, VT,
6333 N0,
6334 DAG.getNode(ISD::FADD, dl, VT,
6335 N1, DAG.getConstantFP(1.0, VT)));
6336 }
6337
6338 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6339 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6340 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0) {
6341 return DAG.getNode(ISD::FMUL, dl, VT,
6342 N0,
6343 DAG.getNode(ISD::FADD, dl, VT,
6344 N1, DAG.getConstantFP(-1.0, VT)));
6345 }
6346
6347
Owen Anderson062c0a52012-05-02 22:17:40 +00006348 return SDValue();
6349}
6350
Dan Gohman475871a2008-07-27 21:46:04 +00006351SDValue DAGCombiner::visitFDIV(SDNode *N) {
6352 SDValue N0 = N->getOperand(0);
6353 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006354 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6355 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006356 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006357 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006358
Dan Gohman7f321562007-06-25 16:23:39 +00006359 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006360 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006361 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006362 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006363 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006364
Nate Begemana148d982006-01-18 22:35:16 +00006365 // fold (fdiv c1, c2) -> c1/c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006366 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006367 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006368
Duncan Sands3ef3fcf2012-04-08 18:08:12 +00006369 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
Ulrich Weigande669c932012-10-29 18:35:49 +00006370 if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands961d6662012-04-07 20:04:00 +00006371 // Compute the reciprocal 1.0 / c2.
6372 APFloat N1APF = N1CFP->getValueAPF();
6373 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6374 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands507bb7a2012-04-10 20:35:27 +00006375 // Only do the transform if the reciprocal is a legal fp immediate that
6376 // isn't too nasty (eg NaN, denormal, ...).
6377 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov999821c2012-04-10 13:22:49 +00006378 (!LegalOperations ||
6379 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6380 // backend)... we should handle this gracefully after Legalize.
6381 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6382 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6383 TLI.isFPImmLegal(Recip, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006384 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0,
Duncan Sands961d6662012-04-07 20:04:00 +00006385 DAG.getConstantFP(Recip, VT));
6386 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006387
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006388 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006389 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006390 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006391 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006392 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006393 // Both can be negated for free, check to see if at least one is cheaper
6394 // negated.
6395 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006396 return DAG.getNode(ISD::FDIV, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006397 GetNegatedExpression(N0, DAG, LegalOperations),
6398 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006399 }
6400 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006401
Dan Gohman475871a2008-07-27 21:46:04 +00006402 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006403}
6404
Dan Gohman475871a2008-07-27 21:46:04 +00006405SDValue DAGCombiner::visitFREM(SDNode *N) {
6406 SDValue N0 = N->getOperand(0);
6407 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006408 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6409 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006410 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00006411
Nate Begemana148d982006-01-18 22:35:16 +00006412 // fold (frem c1, c2) -> fmod(c1,c2)
Ulrich Weigande669c932012-10-29 18:35:49 +00006413 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006414 return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00006415
Dan Gohman475871a2008-07-27 21:46:04 +00006416 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006417}
6418
Dan Gohman475871a2008-07-27 21:46:04 +00006419SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6420 SDValue N0 = N->getOperand(0);
6421 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00006422 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6423 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006424 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00006425
Ulrich Weigande669c932012-10-29 18:35:49 +00006426 if (N0CFP && N1CFP) // Constant fold
Andrew Trickac6d9be2013-05-25 02:42:55 +00006427 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006428
Chris Lattner12d83032006-03-05 05:30:57 +00006429 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00006430 const APFloat& V = N1CFP->getValueAPF();
Sylvestre Ledru94c22712012-09-27 10:14:43 +00006431 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6432 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00006433 if (!V.isNegative()) {
6434 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006435 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00006436 } else {
6437 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006438 return DAG.getNode(ISD::FNEG, SDLoc(N), VT,
6439 DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00006440 }
Chris Lattner12d83032006-03-05 05:30:57 +00006441 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006442
Chris Lattner12d83032006-03-05 05:30:57 +00006443 // copysign(fabs(x), y) -> copysign(x, y)
6444 // copysign(fneg(x), y) -> copysign(x, y)
6445 // copysign(copysign(x,z), y) -> copysign(x, y)
6446 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6447 N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006448 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006449 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00006450
6451 // copysign(x, abs(y)) -> abs(x)
6452 if (N1.getOpcode() == ISD::FABS)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006453 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006454
Chris Lattner12d83032006-03-05 05:30:57 +00006455 // copysign(x, copysign(y,z)) -> copysign(x, z)
6456 if (N1.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006457 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006458 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006459
Chris Lattner12d83032006-03-05 05:30:57 +00006460 // copysign(x, fp_extend(y)) -> copysign(x, y)
6461 // copysign(x, fp_round(y)) -> copysign(x, y)
6462 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006463 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006464 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006465
Dan Gohman475871a2008-07-27 21:46:04 +00006466 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00006467}
6468
Dan Gohman475871a2008-07-27 21:46:04 +00006469SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6470 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006471 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006472 EVT VT = N->getValueType(0);
6473 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00006474
Nate Begeman1d4d4142005-09-01 00:19:25 +00006475 // fold (sint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006476 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006477 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006478 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006479 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006480 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006481
Chris Lattnercda88752008-06-26 00:16:49 +00006482 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6483 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006484 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6485 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006486 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006487 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006488 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006489 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006490
Nadav Rotemed1a3352012-07-23 07:59:50 +00006491 // The next optimizations are desireable only if SELECT_CC can be lowered.
6492 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6493 // having to say they don't support SELECT_CC on every type the DAG knows
6494 // about, since there is no way to mark an opcode illegal at all value types
6495 // (See also visitSELECT)
6496 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6497 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6498 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6499 !VT.isVector() &&
6500 (!LegalOperations ||
6501 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6502 SDValue Ops[] =
6503 { N0.getOperand(0), N0.getOperand(1),
6504 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6505 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006506 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006507 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006508
Nadav Rotemed1a3352012-07-23 07:59:50 +00006509 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6510 // (select_cc x, y, 1.0, 0.0,, cc)
6511 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6512 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6513 (!LegalOperations ||
6514 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6515 SDValue Ops[] =
6516 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6517 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6518 N0.getOperand(0).getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006519 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006520 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006521 }
6522
Dan Gohman475871a2008-07-27 21:46:04 +00006523 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006524}
6525
Dan Gohman475871a2008-07-27 21:46:04 +00006526SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6527 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006528 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006529 EVT VT = N->getValueType(0);
6530 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00006531
Nate Begeman1d4d4142005-09-01 00:19:25 +00006532 // fold (uint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006533 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006534 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006535 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006536 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006537 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006538
Chris Lattnercda88752008-06-26 00:16:49 +00006539 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6540 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006541 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6542 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006543 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006544 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006545 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006546 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006547
Nadav Rotemed1a3352012-07-23 07:59:50 +00006548 // The next optimizations are desireable only if SELECT_CC can be lowered.
6549 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6550 // having to say they don't support SELECT_CC on every type the DAG knows
6551 // about, since there is no way to mark an opcode illegal at all value types
6552 // (See also visitSELECT)
6553 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6554 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond9bf71f2012-07-09 20:31:12 +00006555
Nadav Rotemed1a3352012-07-23 07:59:50 +00006556 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6557 (!LegalOperations ||
6558 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6559 SDValue Ops[] =
6560 { N0.getOperand(0), N0.getOperand(1),
6561 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6562 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006563 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006564 }
6565 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006566
Dan Gohman475871a2008-07-27 21:46:04 +00006567 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006568}
6569
Dan Gohman475871a2008-07-27 21:46:04 +00006570SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6571 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006572 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006573 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006574
Nate Begeman1d4d4142005-09-01 00:19:25 +00006575 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00006576 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006577 return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006578
Dan Gohman475871a2008-07-27 21:46:04 +00006579 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006580}
6581
Dan Gohman475871a2008-07-27 21:46:04 +00006582SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6583 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006584 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006585 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006586
Nate Begeman1d4d4142005-09-01 00:19:25 +00006587 // fold (fp_to_uint c1fp) -> c1
Ulrich Weigande669c932012-10-29 18:35:49 +00006588 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006589 return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006590
Dan Gohman475871a2008-07-27 21:46:04 +00006591 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006592}
6593
Dan Gohman475871a2008-07-27 21:46:04 +00006594SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6595 SDValue N0 = N->getOperand(0);
6596 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006597 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006598 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006599
Nate Begeman1d4d4142005-09-01 00:19:25 +00006600 // fold (fp_round c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006601 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006602 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006603
Chris Lattner79dbea52006-03-13 06:26:26 +00006604 // fold (fp_round (fp_extend x)) -> x
6605 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6606 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006607
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006608 // fold (fp_round (fp_round x)) -> (fp_round x)
6609 if (N0.getOpcode() == ISD::FP_ROUND) {
6610 // This is a value preserving truncation if both round's are.
6611 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006612 N0.getNode()->getConstantOperandVal(1) == 1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006613 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006614 DAG.getIntPtrConstant(IsTrunc));
6615 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006616
Chris Lattner79dbea52006-03-13 06:26:26 +00006617 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00006618 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006619 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006620 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00006621 AddToWorkList(Tmp.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006622 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006623 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00006624 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006625
Dan Gohman475871a2008-07-27 21:46:04 +00006626 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006627}
6628
Dan Gohman475871a2008-07-27 21:46:04 +00006629SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6630 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006631 EVT VT = N->getValueType(0);
6632 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00006633 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006634
Nate Begeman1d4d4142005-09-01 00:19:25 +00006635 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00006636 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00006637 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006638 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006639 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006640
Dan Gohman475871a2008-07-27 21:46:04 +00006641 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006642}
6643
Dan Gohman475871a2008-07-27 21:46:04 +00006644SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6645 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006646 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006647 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006648
Chris Lattner5938bef2007-12-29 06:55:23 +00006649 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00006650 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00006651 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00006652 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00006653
Nate Begeman1d4d4142005-09-01 00:19:25 +00006654 // fold (fp_extend c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006655 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006656 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006657
6658 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6659 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00006660 if (N0.getOpcode() == ISD::FP_ROUND
6661 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00006662 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006663 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006664 if (VT.bitsLT(In.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006665 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006666 In, N0.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006667 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00006668 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006669
Chris Lattner0bd48932008-01-17 07:00:52 +00006670 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00006671 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006672 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00006673 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00006674 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006675 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006676 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00006677 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00006678 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00006679 LN0->isVolatile(), LN0->isNonTemporal(),
6680 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00006681 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006682 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006683 DAG.getNode(ISD::FP_ROUND, SDLoc(N0),
Bill Wendling0225a1d2009-01-30 23:15:49 +00006684 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00006685 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006686 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00006687 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006688
Dan Gohman475871a2008-07-27 21:46:04 +00006689 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006690}
6691
Dan Gohman475871a2008-07-27 21:46:04 +00006692SDValue DAGCombiner::visitFNEG(SDNode *N) {
6693 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006694 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00006695
Craig Topperdd201ff2012-09-11 01:45:21 +00006696 if (VT.isVector()) {
6697 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6698 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper956342b2012-09-09 22:58:45 +00006699 }
6700
Owen Andersonafd3d562012-03-06 00:29:31 +00006701 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6702 &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006703 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00006704
Chris Lattner3bd39d42008-01-27 17:42:27 +00006705 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6706 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006707 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006708 !VT.isVector() &&
6709 N0.getNode()->hasOneUse() &&
6710 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006711 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006712 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006713 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006714 Int = DAG.getNode(ISD::XOR, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006715 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006716 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006717 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006718 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006719 }
6720 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006721
Owen Anderson58d57292012-09-01 06:04:27 +00006722 // (fneg (fmul c, x)) -> (fmul -c, x)
6723 if (N0.getOpcode() == ISD::FMUL) {
6724 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
6725 if (CFP1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006726 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006727 N0.getOperand(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006728 DAG.getNode(ISD::FNEG, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006729 N0.getOperand(1)));
6730 }
6731 }
6732
Dan Gohman475871a2008-07-27 21:46:04 +00006733 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006734}
6735
Owen Anderson7c626d32012-08-13 23:32:49 +00006736SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6737 SDValue N0 = N->getOperand(0);
6738 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6739 EVT VT = N->getValueType(0);
6740
6741 // fold (fceil c1) -> fceil(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006742 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006743 return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006744
6745 return SDValue();
6746}
6747
6748SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6749 SDValue N0 = N->getOperand(0);
6750 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6751 EVT VT = N->getValueType(0);
6752
6753 // fold (ftrunc c1) -> ftrunc(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006754 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006755 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006756
6757 return SDValue();
6758}
6759
6760SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6761 SDValue N0 = N->getOperand(0);
6762 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6763 EVT VT = N->getValueType(0);
6764
6765 // fold (ffloor c1) -> ffloor(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006766 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006767 return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006768
6769 return SDValue();
6770}
6771
Dan Gohman475871a2008-07-27 21:46:04 +00006772SDValue DAGCombiner::visitFABS(SDNode *N) {
6773 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006774 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006775 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006776
Craig Topperdd201ff2012-09-11 01:45:21 +00006777 if (VT.isVector()) {
6778 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6779 if (FoldedVOp.getNode()) return FoldedVOp;
6780 }
6781
Nate Begeman1d4d4142005-09-01 00:19:25 +00006782 // fold (fabs c1) -> fabs(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006783 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006784 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006785 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006786 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00006787 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006788 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006789 // fold (fabs (fcopysign x, y)) -> (fabs x)
6790 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006791 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006792
Chris Lattner3bd39d42008-01-27 17:42:27 +00006793 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6794 // constant pool values.
Stephen Lin155615d2013-07-08 00:37:03 +00006795 if (!TLI.isFAbsFree(VT) &&
Owen Anderson29f60f32012-04-02 22:10:29 +00006796 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006797 N0.getOperand(0).getValueType().isInteger() &&
6798 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006799 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006800 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006801 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006802 Int = DAG.getNode(ISD::AND, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006803 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006804 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006805 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00006806 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006807 }
6808 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006809
Dan Gohman475871a2008-07-27 21:46:04 +00006810 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006811}
6812
Dan Gohman475871a2008-07-27 21:46:04 +00006813SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6814 SDValue Chain = N->getOperand(0);
6815 SDValue N1 = N->getOperand(1);
6816 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006817
Dan Gohmane0f06c72009-11-17 00:47:23 +00006818 // If N is a constant we could fold this into a fallthrough or unconditional
6819 // branch. However that doesn't happen very often in normal code, because
6820 // Instcombine/SimplifyCFG should have handled the available opportunities.
6821 // If we did this folding here, it would be necessary to update the
6822 // MachineBasicBlock CFG, which is awkward.
6823
Nate Begeman750ac1b2006-02-01 07:19:44 +00006824 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6825 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006826 if (N1.getOpcode() == ISD::SETCC &&
Tom Stellard3ef53832013-03-08 15:36:57 +00006827 TLI.isOperationLegalOrCustom(ISD::BR_CC,
6828 N1.getOperand(0).getValueType())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006829 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006830 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00006831 N1.getOperand(0), N1.getOperand(1), N2);
6832 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006833
Evan Cheng2a135ae2010-10-04 22:41:01 +00006834 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6835 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6836 (N1.getOperand(0).hasOneUse() &&
6837 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6838 SDNode *Trunc = 0;
6839 if (N1.getOpcode() == ISD::TRUNCATE) {
6840 // Look pass the truncate.
6841 Trunc = N1.getNode();
6842 N1 = N1.getOperand(0);
6843 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006844
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006845 // Match this pattern so that we can generate simpler code:
6846 //
6847 // %a = ...
6848 // %b = and i32 %a, 2
6849 // %c = srl i32 %b, 1
6850 // brcond i32 %c ...
6851 //
6852 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006853 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006854 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00006855 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006856 // %c = setcc eq %b, 0
6857 // brcond %c ...
6858 //
6859 // This applies only when the AND constant value has one bit set and the
6860 // SRL constant is equal to the log2 of the AND constant. The back-end is
6861 // smart enough to convert the result into a TEST/JMP sequence.
6862 SDValue Op0 = N1.getOperand(0);
6863 SDValue Op1 = N1.getOperand(1);
6864
6865 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006866 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006867 SDValue AndOp1 = Op0.getOperand(1);
6868
6869 if (AndOp1.getOpcode() == ISD::Constant) {
6870 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
6871
6872 if (AndConst.isPowerOf2() &&
6873 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
6874 SDValue SetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00006875 DAG.getSetCC(SDLoc(N),
Matt Arsenault225ed702013-05-18 00:21:46 +00006876 getSetCCResultType(Op0.getValueType()),
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006877 Op0, DAG.getConstant(0, Op0.getValueType()),
6878 ISD::SETNE);
6879
Andrew Trickac6d9be2013-05-25 02:42:55 +00006880 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Chengd40d03e2010-01-06 19:38:29 +00006881 MVT::Other, Chain, SetCC, N2);
6882 // Don't add the new BRCond into the worklist or else SimplifySelectCC
6883 // will convert it back to (X & C1) >> C2.
6884 CombineTo(N, NewBRCond, false);
6885 // Truncate is dead.
6886 if (Trunc) {
6887 removeFromWorkList(Trunc);
6888 DAG.DeleteNode(Trunc);
6889 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006890 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00006891 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006892 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006893 removeFromWorkList(N1.getNode());
6894 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00006895 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006896 }
6897 }
6898 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00006899
6900 if (Trunc)
6901 // Restore N1 if the above transformation doesn't match.
6902 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006903 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006904
Evan Cheng2c755ba2010-02-27 07:36:59 +00006905 // Transform br(xor(x, y)) -> br(x != y)
6906 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
6907 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
6908 SDNode *TheXor = N1.getNode();
6909 SDValue Op0 = TheXor->getOperand(0);
6910 SDValue Op1 = TheXor->getOperand(1);
6911 if (Op0.getOpcode() == Op1.getOpcode()) {
6912 // Avoid missing important xor optimizations.
6913 SDValue Tmp = visitXOR(TheXor);
Evan Cheng78ec0252013-01-09 20:56:40 +00006914 if (Tmp.getNode()) {
6915 if (Tmp.getNode() != TheXor) {
6916 DEBUG(dbgs() << "\nReplacing.8 ";
6917 TheXor->dump(&DAG);
6918 dbgs() << "\nWith: ";
6919 Tmp.getNode()->dump(&DAG);
6920 dbgs() << '\n');
6921 WorkListRemover DeadNodes(*this);
6922 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
6923 removeFromWorkList(TheXor);
6924 DAG.DeleteNode(TheXor);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006925 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng78ec0252013-01-09 20:56:40 +00006926 MVT::Other, Chain, Tmp, N2);
6927 }
6928
Benjamin Kramer0b68b752013-03-30 21:28:18 +00006929 // visitXOR has changed XOR's operands or replaced the XOR completely,
6930 // bail out.
6931 return SDValue(N, 0);
Evan Cheng2c755ba2010-02-27 07:36:59 +00006932 }
6933 }
6934
6935 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
6936 bool Equal = false;
6937 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
6938 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
6939 Op0.getOpcode() == ISD::XOR) {
6940 TheXor = Op0.getNode();
6941 Equal = true;
6942 }
6943
Evan Cheng2a135ae2010-10-04 22:41:01 +00006944 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00006945 if (LegalTypes)
Matt Arsenault225ed702013-05-18 00:21:46 +00006946 SetCCVT = getSetCCResultType(SetCCVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006947 SDValue SetCC = DAG.getSetCC(SDLoc(TheXor),
Evan Cheng2c755ba2010-02-27 07:36:59 +00006948 SetCCVT,
6949 Op0, Op1,
6950 Equal ? ISD::SETEQ : ISD::SETNE);
6951 // Replace the uses of XOR with SETCC
6952 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006953 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Cheng2a135ae2010-10-04 22:41:01 +00006954 removeFromWorkList(N1.getNode());
6955 DAG.DeleteNode(N1.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006956 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng2c755ba2010-02-27 07:36:59 +00006957 MVT::Other, Chain, SetCC, N2);
6958 }
6959 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006960
Dan Gohman475871a2008-07-27 21:46:04 +00006961 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006962}
6963
Chris Lattner3ea0b472005-10-05 06:47:48 +00006964// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
6965//
Dan Gohman475871a2008-07-27 21:46:04 +00006966SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00006967 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006968 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00006969
Dan Gohmane0f06c72009-11-17 00:47:23 +00006970 // If N is a constant we could fold this into a fallthrough or unconditional
6971 // branch. However that doesn't happen very often in normal code, because
6972 // Instcombine/SimplifyCFG should have handled the available opportunities.
6973 // If we did this folding here, it would be necessary to update the
6974 // MachineBasicBlock CFG, which is awkward.
6975
Duncan Sands8eab8a22008-06-09 11:32:28 +00006976 // Use SimplifySetCC to simplify SETCC's.
Matt Arsenault225ed702013-05-18 00:21:46 +00006977 SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006978 CondLHS, CondRHS, CC->get(), SDLoc(N),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006979 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00006980 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00006981
Nate Begemane17daeb2005-10-05 21:43:42 +00006982 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00006983 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006984 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006985 N->getOperand(0), Simp.getOperand(2),
6986 Simp.getOperand(0), Simp.getOperand(1),
6987 N->getOperand(4));
6988
Dan Gohman475871a2008-07-27 21:46:04 +00006989 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006990}
6991
Evan Chengc4b527a2012-01-13 01:37:24 +00006992/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
6993/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng03be3622012-03-06 23:33:32 +00006994/// addressing mode.
Evan Chengc4b527a2012-01-13 01:37:24 +00006995static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
6996 SelectionDAG &DAG,
6997 const TargetLowering &TLI) {
6998 EVT VT;
6999 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
7000 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
7001 return false;
7002 VT = Use->getValueType(0);
7003 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
7004 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
7005 return false;
7006 VT = ST->getValue().getValueType();
7007 } else
7008 return false;
7009
Chandler Carruth56d433d2013-01-07 15:14:13 +00007010 TargetLowering::AddrMode AM;
Evan Chengc4b527a2012-01-13 01:37:24 +00007011 if (N->getOpcode() == ISD::ADD) {
7012 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7013 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00007014 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00007015 AM.BaseOffs = Offset->getSExtValue();
7016 else
Evan Cheng03be3622012-03-06 23:33:32 +00007017 // [reg +/- reg]
7018 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007019 } else if (N->getOpcode() == ISD::SUB) {
7020 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7021 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00007022 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00007023 AM.BaseOffs = -Offset->getSExtValue();
7024 else
Evan Cheng03be3622012-03-06 23:33:32 +00007025 // [reg +/- reg]
7026 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007027 } else
7028 return false;
7029
7030 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
7031}
7032
Duncan Sandsec87aa82008-06-15 20:12:31 +00007033/// CombineToPreIndexedLoadStore - Try turning a load / store into a
7034/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00007035/// and it has other uses besides the load / store. After the
7036/// transformation, the new indexed load / store has effectively folded
7037/// the add / subtract in and all of its other uses are redirected to the
7038/// new load / store.
7039bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007040 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007041 return false;
7042
7043 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007044 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007045 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007046 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007047 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007048 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007049 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00007050 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00007051 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
7052 return false;
7053 Ptr = LD->getBasePtr();
7054 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007055 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007056 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007057 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007058 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
7059 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
7060 return false;
7061 Ptr = ST->getBasePtr();
7062 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007063 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007064 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007065 }
Chris Lattner448f2192006-11-11 00:39:41 +00007066
Chris Lattner9f1794e2006-11-11 00:56:29 +00007067 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
7068 // out. There is no reason to make this a preinc/predec.
7069 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00007070 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007071 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007072
Chris Lattner9f1794e2006-11-11 00:56:29 +00007073 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00007074 SDValue BasePtr;
7075 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007076 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7077 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
7078 return false;
Hal Finkel089a5f82013-02-08 21:35:47 +00007079
7080 // Backends without true r+i pre-indexed forms may need to pass a
7081 // constant base with a variable offset so that constant coercion
7082 // will work with the patterns in canonical form.
7083 bool Swapped = false;
7084 if (isa<ConstantSDNode>(BasePtr)) {
7085 std::swap(BasePtr, Offset);
7086 Swapped = true;
7087 }
7088
Evan Chenga7d4a042007-05-03 23:52:19 +00007089 // Don't create a indexed load / store with zero offset.
7090 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007091 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007092 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007093
Chris Lattner41e53fd2006-11-11 01:00:15 +00007094 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00007095 // 1) The new base ptr is a frame index.
7096 // 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 +00007097 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00007098 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00007099 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00007100 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00007101
Chris Lattner41e53fd2006-11-11 01:00:15 +00007102 // Check #1. Preinc'ing a frame index would require copying the stack pointer
7103 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00007104 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00007105 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007106
Chris Lattner41e53fd2006-11-11 01:00:15 +00007107 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007108 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00007109 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007110 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007111 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007112 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007113
Hal Finkel089a5f82013-02-08 21:35:47 +00007114 // If the offset is a constant, there may be other adds of constants that
7115 // can be folded with this one. We should do this to avoid having to keep
7116 // a copy of the original base pointer.
7117 SmallVector<SDNode *, 16> OtherUses;
7118 if (isa<ConstantSDNode>(Offset))
7119 for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(),
7120 E = BasePtr.getNode()->use_end(); I != E; ++I) {
7121 SDNode *Use = *I;
7122 if (Use == Ptr.getNode())
7123 continue;
7124
7125 if (Use->isPredecessorOf(N))
7126 continue;
7127
7128 if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) {
7129 OtherUses.clear();
7130 break;
7131 }
7132
7133 SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1);
7134 if (Op1.getNode() == BasePtr.getNode())
7135 std::swap(Op0, Op1);
7136 assert(Op0.getNode() == BasePtr.getNode() &&
7137 "Use of ADD/SUB but not an operand");
7138
7139 if (!isa<ConstantSDNode>(Op1)) {
7140 OtherUses.clear();
7141 break;
7142 }
7143
7144 // FIXME: In some cases, we can be smarter about this.
7145 if (Op1.getValueType() != Offset.getValueType()) {
7146 OtherUses.clear();
7147 break;
7148 }
7149
7150 OtherUses.push_back(Use);
7151 }
7152
7153 if (Swapped)
7154 std::swap(BasePtr, Offset);
7155
Evan Chengc843abe2007-05-24 02:35:39 +00007156 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007157 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00007158
7159 // Caches for hasPredecessorHelper
7160 SmallPtrSet<const SDNode *, 32> Visited;
7161 SmallVector<const SDNode *, 16> Worklist;
7162
Gabor Greifba36cb52008-08-28 21:40:38 +00007163 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7164 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007165 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007166 if (Use == N)
7167 continue;
Lang Hames944520f2011-07-07 04:31:51 +00007168 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007169 return false;
7170
Evan Chengc4b527a2012-01-13 01:37:24 +00007171 // If Ptr may be folded in addressing mode of other use, then it's
7172 // not profitable to do this transformation.
7173 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007174 RealUse = true;
7175 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007176
Chris Lattner9f1794e2006-11-11 00:56:29 +00007177 if (!RealUse)
7178 return false;
7179
Dan Gohman475871a2008-07-27 21:46:04 +00007180 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007181 if (isLoad)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007182 Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007183 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007184 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00007185 Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007186 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007187 ++PreIndexedNodes;
7188 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007189 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007190 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007191 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007192 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007193 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007194 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007195 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007196 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7197 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007198 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007199 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007200 }
7201
Chris Lattner9f1794e2006-11-11 00:56:29 +00007202 // Finally, since the node is now dead, remove it from the graph.
7203 DAG.DeleteNode(N);
7204
Hal Finkel089a5f82013-02-08 21:35:47 +00007205 if (Swapped)
7206 std::swap(BasePtr, Offset);
7207
7208 // Replace other uses of BasePtr that can be updated to use Ptr
7209 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) {
7210 unsigned OffsetIdx = 1;
7211 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode())
7212 OffsetIdx = 0;
7213 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() ==
7214 BasePtr.getNode() && "Expected BasePtr operand");
7215
Silviu Baranga730a5702013-04-26 15:52:24 +00007216 // We need to replace ptr0 in the following expression:
7217 // x0 * offset0 + y0 * ptr0 = t0
7218 // knowing that
7219 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store)
Stephen Lin155615d2013-07-08 00:37:03 +00007220 //
Silviu Baranga730a5702013-04-26 15:52:24 +00007221 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the
7222 // indexed load/store and the expresion that needs to be re-written.
7223 //
7224 // Therefore, we have:
7225 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1
Hal Finkel089a5f82013-02-08 21:35:47 +00007226
7227 ConstantSDNode *CN =
7228 cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx));
Silviu Baranga730a5702013-04-26 15:52:24 +00007229 int X0, X1, Y0, Y1;
7230 APInt Offset0 = CN->getAPIntValue();
7231 APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue();
Hal Finkel089a5f82013-02-08 21:35:47 +00007232
Silviu Baranga730a5702013-04-26 15:52:24 +00007233 X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1;
7234 Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1;
7235 X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1;
7236 Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1;
Hal Finkel089a5f82013-02-08 21:35:47 +00007237
Silviu Baranga730a5702013-04-26 15:52:24 +00007238 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD;
7239
7240 APInt CNV = Offset0;
7241 if (X0 < 0) CNV = -CNV;
7242 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1;
7243 else CNV = CNV - Offset1;
7244
7245 // We can now generate the new expression.
7246 SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0));
7247 SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0);
7248
7249 SDValue NewUse = DAG.getNode(Opcode,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007250 SDLoc(OtherUses[i]),
Hal Finkel089a5f82013-02-08 21:35:47 +00007251 OtherUses[i]->getValueType(0), NewOp1, NewOp2);
7252 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse);
7253 removeFromWorkList(OtherUses[i]);
7254 DAG.DeleteNode(OtherUses[i]);
7255 }
7256
Chris Lattner9f1794e2006-11-11 00:56:29 +00007257 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007258 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greifba36cb52008-08-28 21:40:38 +00007259 removeFromWorkList(Ptr.getNode());
7260 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00007261
7262 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007263}
7264
Duncan Sandsec87aa82008-06-15 20:12:31 +00007265/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00007266/// add / sub of the base pointer node into a post-indexed load / store.
7267/// The transformation folded the add / subtract into the new indexed
7268/// load / store effectively and all of its uses are redirected to the
7269/// new load / store.
7270bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007271 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007272 return false;
7273
7274 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007275 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007276 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007277 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007278 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007279 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007280 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007281 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
7282 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
7283 return false;
7284 Ptr = LD->getBasePtr();
7285 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007286 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007287 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007288 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007289 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
7290 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
7291 return false;
7292 Ptr = ST->getBasePtr();
7293 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007294 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007295 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007296 }
Chris Lattner448f2192006-11-11 00:39:41 +00007297
Gabor Greifba36cb52008-08-28 21:40:38 +00007298 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007299 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007300
Gabor Greifba36cb52008-08-28 21:40:38 +00007301 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7302 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007303 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007304 if (Op == N ||
7305 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
7306 continue;
7307
Dan Gohman475871a2008-07-27 21:46:04 +00007308 SDValue BasePtr;
7309 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007310 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7311 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00007312 // Don't create a indexed load / store with zero offset.
7313 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007314 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007315 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007316
Chris Lattner9f1794e2006-11-11 00:56:29 +00007317 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00007318 // 1) All uses are load / store ops that use it as base ptr (and
7319 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00007320 // 2) Op must be independent of N, i.e. Op is neither a predecessor
7321 // nor a successor of N. Otherwise, if Op is folded that would
7322 // create a cycle.
7323
Evan Chengcaab1292009-05-06 18:25:01 +00007324 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
7325 continue;
7326
Chris Lattner9f1794e2006-11-11 00:56:29 +00007327 // Check for #1.
7328 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00007329 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
7330 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00007331 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00007332 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00007333 continue;
7334
Chris Lattner9f1794e2006-11-11 00:56:29 +00007335 // If all the uses are load / store addresses, then don't do the
7336 // transformation.
7337 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
7338 bool RealUse = false;
7339 for (SDNode::use_iterator III = Use->use_begin(),
7340 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00007341 SDNode *UseUse = *III;
Stephen Lin155615d2013-07-08 00:37:03 +00007342 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007343 RealUse = true;
7344 }
Chris Lattner448f2192006-11-11 00:39:41 +00007345
Chris Lattner9f1794e2006-11-11 00:56:29 +00007346 if (!RealUse) {
7347 TryNext = true;
7348 break;
Chris Lattner448f2192006-11-11 00:39:41 +00007349 }
7350 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007351 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007352
Chris Lattner9f1794e2006-11-11 00:56:29 +00007353 if (TryNext)
7354 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007355
Chris Lattner9f1794e2006-11-11 00:56:29 +00007356 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00007357 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00007358 SDValue Result = isLoad
Andrew Trickac6d9be2013-05-25 02:42:55 +00007359 ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007360 BasePtr, Offset, AM)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007361 : DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007362 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007363 ++PostIndexedNodes;
7364 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007365 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007366 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007367 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007368 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007369 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007370 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007371 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007372 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7373 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007374 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007375 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner448f2192006-11-11 00:39:41 +00007376 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007377
Chris Lattner9f1794e2006-11-11 00:56:29 +00007378 // Finally, since the node is now dead, remove it from the graph.
7379 DAG.DeleteNode(N);
7380
7381 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00007382 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007383 Result.getValue(isLoad ? 1 : 0));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007384 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007385 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007386 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007387 }
7388 }
7389 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007390
Chris Lattner448f2192006-11-11 00:39:41 +00007391 return false;
7392}
7393
Dan Gohman475871a2008-07-27 21:46:04 +00007394SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00007395 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007396 SDValue Chain = LD->getChain();
7397 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007398
Evan Cheng45a7ca92007-05-01 00:38:21 +00007399 // If load is not volatile and there are no uses of the loaded value (and
7400 // the updated indexed value in case of indexed loads), change uses of the
7401 // chain value into uses of the chain input (i.e. delete the dead load).
7402 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007403 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00007404 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00007405 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00007406 // It's not safe to use the two value CombineTo variant here. e.g.
7407 // v1, chain2 = load chain1, loc
7408 // v2, chain3 = load chain2, loc
7409 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00007410 // Now we replace use of chain2 with chain1. This makes the second load
7411 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00007412 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007413 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007414 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007415 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007416 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007417 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007418 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendlingc0debad2009-01-30 23:27:35 +00007419
Chris Lattner125991a2008-01-24 07:57:06 +00007420 if (N->use_empty()) {
7421 removeFromWorkList(N);
7422 DAG.DeleteNode(N);
7423 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007424
Dan Gohman475871a2008-07-27 21:46:04 +00007425 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00007426 }
Evan Cheng498f5592007-05-01 08:53:39 +00007427 } else {
7428 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00007429 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00007430 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007431 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00007432 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007433 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007434 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007435 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007436 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007437 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007438 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman475871a2008-07-27 21:46:04 +00007439 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007440 DAG.getUNDEF(N->getValueType(1)));
7441 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng02c42852008-01-16 23:11:54 +00007442 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00007443 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007444 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00007445 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00007446 }
7447 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007448
Chris Lattner01a22022005-10-10 22:04:48 +00007449 // If this load is directly stored, replace the load value with the stored
7450 // value.
7451 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007452 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00007453 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007454 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007455 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7456 if (PrevST->getBasePtr() == Ptr &&
7457 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007458 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00007459 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007460 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007461
Evan Cheng255f20f2010-04-01 06:04:33 +00007462 // Try to infer better alignment information than the load already has.
7463 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007464 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Owen Andersonb48783b2013-02-05 19:24:39 +00007465 if (Align > LD->getMemOperand()->getBaseAlignment()) {
7466 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00007467 DAG.getExtLoad(LD->getExtensionType(), SDLoc(N),
Evan Chenged1c0c72011-11-28 22:37:34 +00007468 LD->getValueType(0),
7469 Chain, Ptr, LD->getPointerInfo(),
7470 LD->getMemoryVT(),
7471 LD->isVolatile(), LD->isNonTemporal(), Align);
Owen Andersonb48783b2013-02-05 19:24:39 +00007472 return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true);
7473 }
Evan Cheng255f20f2010-04-01 06:04:33 +00007474 }
7475 }
7476
Jim Laskey7ca56af2006-10-11 13:47:09 +00007477 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007478 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007479 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007480
Jim Laskey6ff23e52006-10-04 16:53:27 +00007481 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007482 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007483 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007484
Jim Laskey279f0532006-09-25 16:29:54 +00007485 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007486 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007487 ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD),
Chris Lattnerfa459012010-09-21 16:08:50 +00007488 BetterChain, Ptr, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007489 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007490 LD->isInvariant(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007491 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007492 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD),
Stuart Hastingsa9011292011-02-16 16:23:55 +00007493 LD->getValueType(0),
Chris Lattnerfa459012010-09-21 16:08:50 +00007494 BetterChain, Ptr, LD->getPointerInfo(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007495 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007496 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007497 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00007498 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007499 }
Jim Laskey279f0532006-09-25 16:29:54 +00007500
Jim Laskey6ff23e52006-10-04 16:53:27 +00007501 // Create token factor to keep old chain connected.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007502 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00007503 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007504
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007505 // Make sure the new and old chains are cleaned up.
7506 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007507
Jim Laskey274062c2006-10-13 23:32:28 +00007508 // Replace uses with load result and token factor. Don't add users
7509 // to work list.
7510 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007511 }
7512 }
7513
Evan Cheng7fc033a2006-11-03 03:06:21 +00007514 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007515 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007516 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00007517
Dan Gohman475871a2008-07-27 21:46:04 +00007518 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00007519}
7520
Chris Lattner2392ae72010-04-15 04:48:01 +00007521/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
7522/// load is having specific bytes cleared out. If so, return the byte size
7523/// being masked out and the shift amount.
7524static std::pair<unsigned, unsigned>
7525CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
7526 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007527
Chris Lattner2392ae72010-04-15 04:48:01 +00007528 // Check for the structure we're looking for.
7529 if (V->getOpcode() != ISD::AND ||
7530 !isa<ConstantSDNode>(V->getOperand(1)) ||
7531 !ISD::isNormalLoad(V->getOperand(0).getNode()))
7532 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007533
Chris Lattnere6987582010-04-15 06:10:49 +00007534 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00007535 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00007536 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007537
Chris Lattnere6987582010-04-15 06:10:49 +00007538 // The store should be chained directly to the load or be an operand of a
7539 // tokenfactor.
7540 if (LD == Chain.getNode())
7541 ; // ok.
7542 else if (Chain->getOpcode() != ISD::TokenFactor)
7543 return Result; // Fail.
7544 else {
7545 bool isOk = false;
7546 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
7547 if (Chain->getOperand(i).getNode() == LD) {
7548 isOk = true;
7549 break;
7550 }
7551 if (!isOk) return Result;
7552 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007553
Chris Lattner2392ae72010-04-15 04:48:01 +00007554 // This only handles simple types.
7555 if (V.getValueType() != MVT::i16 &&
7556 V.getValueType() != MVT::i32 &&
7557 V.getValueType() != MVT::i64)
7558 return Result;
7559
7560 // Check the constant mask. Invert it so that the bits being masked out are
7561 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
7562 // follow the sign bit for uniformity.
7563 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
Michael J. Spencerc6af2432013-05-24 22:23:49 +00007564 unsigned NotMaskLZ = countLeadingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00007565 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
Michael J. Spencerc6af2432013-05-24 22:23:49 +00007566 unsigned NotMaskTZ = countTrailingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00007567 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
7568 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007569
Chris Lattner2392ae72010-04-15 04:48:01 +00007570 // See if we have a continuous run of bits. If so, we have 0*1+0*
7571 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
7572 return Result;
7573
7574 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
7575 if (V.getValueType() != MVT::i64 && NotMaskLZ)
7576 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007577
Chris Lattner2392ae72010-04-15 04:48:01 +00007578 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
7579 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007580 case 1:
7581 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00007582 case 4: break;
7583 default: return Result; // All one mask, or 5-byte mask.
7584 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007585
Chris Lattner2392ae72010-04-15 04:48:01 +00007586 // Verify that the first bit starts at a multiple of mask so that the access
7587 // is aligned the same as the access width.
7588 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007589
Chris Lattner2392ae72010-04-15 04:48:01 +00007590 Result.first = MaskedBytes;
7591 Result.second = NotMaskTZ/8;
7592 return Result;
7593}
7594
7595
7596/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
7597/// provides a value as specified by MaskInfo. If so, replace the specified
7598/// store with a narrower store of truncated IVal.
7599static SDNode *
7600ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
7601 SDValue IVal, StoreSDNode *St,
7602 DAGCombiner *DC) {
7603 unsigned NumBytes = MaskInfo.first;
7604 unsigned ByteShift = MaskInfo.second;
7605 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007606
Chris Lattner2392ae72010-04-15 04:48:01 +00007607 // Check to see if IVal is all zeros in the part being masked in by the 'or'
7608 // that uses this. If not, this is not a replacement.
7609 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
7610 ByteShift*8, (ByteShift+NumBytes)*8);
7611 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007612
Chris Lattner2392ae72010-04-15 04:48:01 +00007613 // Check that it is legal on the target to do this. It is legal if the new
7614 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
7615 // legalization.
7616 MVT VT = MVT::getIntegerVT(NumBytes*8);
7617 if (!DC->isTypeLegal(VT))
7618 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007619
Chris Lattner2392ae72010-04-15 04:48:01 +00007620 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
7621 // shifted by ByteShift and truncated down to NumBytes.
7622 if (ByteShift)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007623 IVal = DAG.getNode(ISD::SRL, SDLoc(IVal), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00007624 DAG.getConstant(ByteShift*8,
7625 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00007626
7627 // Figure out the offset for the store and the alignment of the access.
7628 unsigned StOffset;
7629 unsigned NewAlign = St->getAlignment();
7630
7631 if (DAG.getTargetLoweringInfo().isLittleEndian())
7632 StOffset = ByteShift;
7633 else
7634 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007635
Chris Lattner2392ae72010-04-15 04:48:01 +00007636 SDValue Ptr = St->getBasePtr();
7637 if (StOffset) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007638 Ptr = DAG.getNode(ISD::ADD, SDLoc(IVal), Ptr.getValueType(),
Chris Lattner2392ae72010-04-15 04:48:01 +00007639 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
7640 NewAlign = MinAlign(NewAlign, StOffset);
7641 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007642
Chris Lattner2392ae72010-04-15 04:48:01 +00007643 // Truncate down to the new size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007644 IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007645
Chris Lattner2392ae72010-04-15 04:48:01 +00007646 ++OpsNarrowed;
Andrew Trickac6d9be2013-05-25 02:42:55 +00007647 return DAG.getStore(St->getChain(), SDLoc(St), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007648 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00007649 false, false, NewAlign).getNode();
7650}
7651
Evan Cheng8b944d32009-05-28 00:35:15 +00007652
7653/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
7654/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
7655/// of the loaded bits, try narrowing the load and store if it would end up
7656/// being a win for performance or code size.
7657SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
7658 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00007659 if (ST->isVolatile())
7660 return SDValue();
7661
Evan Cheng8b944d32009-05-28 00:35:15 +00007662 SDValue Chain = ST->getChain();
7663 SDValue Value = ST->getValue();
7664 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00007665 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00007666
7667 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00007668 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007669
7670 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007671
Chris Lattner2392ae72010-04-15 04:48:01 +00007672 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
7673 // is a byte mask indicating a consecutive number of bytes, check to see if
7674 // Y is known to provide just those bytes. If so, we try to replace the
7675 // load + replace + store sequence with a single (narrower) store, which makes
7676 // the load dead.
7677 if (Opc == ISD::OR) {
7678 std::pair<unsigned, unsigned> MaskedLoad;
7679 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
7680 if (MaskedLoad.first)
7681 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7682 Value.getOperand(1), ST,this))
7683 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007684
Chris Lattner2392ae72010-04-15 04:48:01 +00007685 // Or is commutative, so try swapping X and Y.
7686 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
7687 if (MaskedLoad.first)
7688 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7689 Value.getOperand(0), ST,this))
7690 return SDValue(NewST, 0);
7691 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007692
Evan Cheng8b944d32009-05-28 00:35:15 +00007693 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
7694 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00007695 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007696
7697 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00007698 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
7699 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00007700 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00007701 if (LD->getBasePtr() != Ptr ||
7702 LD->getPointerInfo().getAddrSpace() !=
7703 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00007704 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007705
7706 // Find the type to narrow it the load / op / store to.
7707 SDValue N1 = Value.getOperand(1);
7708 unsigned BitWidth = N1.getValueSizeInBits();
7709 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
7710 if (Opc == ISD::AND)
7711 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00007712 if (Imm == 0 || Imm.isAllOnesValue())
7713 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007714 unsigned ShAmt = Imm.countTrailingZeros();
7715 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
7716 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00007717 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007718 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00007719 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00007720 TLI.isNarrowingProfitable(VT, NewVT))) {
7721 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00007722 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007723 }
Evan Chengcdcecc02009-05-28 18:41:02 +00007724 if (NewBW >= BitWidth)
7725 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007726
7727 // If the lsb changed does not start at the type bitwidth boundary,
7728 // start at the previous one.
7729 if (ShAmt % NewBW)
7730 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
Manman Ren981b9632012-12-12 01:13:50 +00007731 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt,
7732 std::min(BitWidth, ShAmt + NewBW));
Evan Cheng8b944d32009-05-28 00:35:15 +00007733 if ((Imm & Mask) == Imm) {
7734 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
7735 if (Opc == ISD::AND)
7736 NewImm ^= APInt::getAllOnesValue(NewBW);
7737 uint64_t PtrOff = ShAmt / 8;
7738 // For big endian targets, we need to adjust the offset to the pointer to
7739 // load the correct bytes.
7740 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00007741 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00007742
7743 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007744 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00007745 if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00007746 return SDValue();
7747
Andrew Trickac6d9be2013-05-25 02:42:55 +00007748 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD),
Evan Cheng8b944d32009-05-28 00:35:15 +00007749 Ptr.getValueType(), Ptr,
7750 DAG.getConstant(PtrOff, Ptr.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00007751 SDValue NewLD = DAG.getLoad(NewVT, SDLoc(N0),
Evan Cheng8b944d32009-05-28 00:35:15 +00007752 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007753 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007754 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007755 LD->isInvariant(), NewAlign);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007756 SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD,
Evan Cheng8b944d32009-05-28 00:35:15 +00007757 DAG.getConstant(NewImm, NewVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00007758 SDValue NewST = DAG.getStore(Chain, SDLoc(N),
Evan Cheng8b944d32009-05-28 00:35:15 +00007759 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007760 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007761 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007762
7763 AddToWorkList(NewPtr.getNode());
7764 AddToWorkList(NewLD.getNode());
7765 AddToWorkList(NewVal.getNode());
7766 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007767 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Cheng8b944d32009-05-28 00:35:15 +00007768 ++OpsNarrowed;
7769 return NewST;
7770 }
7771 }
7772
Evan Chengcdcecc02009-05-28 18:41:02 +00007773 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007774}
7775
Evan Cheng31959b12011-02-02 01:06:55 +00007776/// TransformFPLoadStorePair - For a given floating point load / store pair,
7777/// if the load value isn't used by any other operations, then consider
7778/// transforming the pair to integer load / store operations if the target
7779/// deems the transformation profitable.
7780SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
7781 StoreSDNode *ST = cast<StoreSDNode>(N);
7782 SDValue Chain = ST->getChain();
7783 SDValue Value = ST->getValue();
7784 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
7785 Value.hasOneUse() &&
7786 Chain == SDValue(Value.getNode(), 1)) {
7787 LoadSDNode *LD = cast<LoadSDNode>(Value);
7788 EVT VT = LD->getMemoryVT();
7789 if (!VT.isFloatingPoint() ||
7790 VT != ST->getMemoryVT() ||
7791 LD->isNonTemporal() ||
7792 ST->isNonTemporal() ||
7793 LD->getPointerInfo().getAddrSpace() != 0 ||
7794 ST->getPointerInfo().getAddrSpace() != 0)
7795 return SDValue();
7796
7797 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
7798 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
7799 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
7800 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
7801 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
7802 return SDValue();
7803
7804 unsigned LDAlign = LD->getAlignment();
7805 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007806 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00007807 unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy);
Evan Cheng31959b12011-02-02 01:06:55 +00007808 if (LDAlign < ABIAlign || STAlign < ABIAlign)
7809 return SDValue();
7810
Andrew Trickac6d9be2013-05-25 02:42:55 +00007811 SDValue NewLD = DAG.getLoad(IntVT, SDLoc(Value),
Evan Cheng31959b12011-02-02 01:06:55 +00007812 LD->getChain(), LD->getBasePtr(),
7813 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007814 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00007815
Andrew Trickac6d9be2013-05-25 02:42:55 +00007816 SDValue NewST = DAG.getStore(NewLD.getValue(1), SDLoc(N),
Evan Cheng31959b12011-02-02 01:06:55 +00007817 NewLD, ST->getBasePtr(),
7818 ST->getPointerInfo(),
7819 false, false, STAlign);
7820
7821 AddToWorkList(NewLD.getNode());
7822 AddToWorkList(NewST.getNode());
7823 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007824 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Cheng31959b12011-02-02 01:06:55 +00007825 ++LdStFP2Int;
7826 return NewST;
7827 }
7828
7829 return SDValue();
7830}
7831
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007832/// Helper struct to parse and store a memory address as base + index + offset.
7833/// We ignore sign extensions when it is safe to do so.
7834/// The following two expressions are not equivalent. To differentiate we need
7835/// to store whether there was a sign extension involved in the index
7836/// computation.
7837/// (load (i64 add (i64 copyfromreg %c)
7838/// (i64 signextend (add (i8 load %index)
7839/// (i8 1))))
7840/// vs
7841///
7842/// (load (i64 add (i64 copyfromreg %c)
7843/// (i64 signextend (i32 add (i32 signextend (i8 load %index))
7844/// (i32 1)))))
7845struct BaseIndexOffset {
7846 SDValue Base;
7847 SDValue Index;
7848 int64_t Offset;
7849 bool IsIndexSignExt;
7850
7851 BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {}
7852
7853 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
7854 bool IsIndexSignExt) :
7855 Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {}
7856
7857 bool equalBaseIndex(const BaseIndexOffset &Other) {
7858 return Other.Base == Base && Other.Index == Index &&
7859 Other.IsIndexSignExt == IsIndexSignExt;
Nadav Rotemc653de62012-10-03 16:11:15 +00007860 }
7861
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007862 /// Parses tree in Ptr for base, index, offset addresses.
7863 static BaseIndexOffset match(SDValue Ptr) {
7864 bool IsIndexSignExt = false;
7865
7866 // Just Base or possibly anything else.
7867 if (Ptr->getOpcode() != ISD::ADD)
7868 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7869
7870 // Base + offset.
7871 if (isa<ConstantSDNode>(Ptr->getOperand(1))) {
7872 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
7873 return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset,
7874 IsIndexSignExt);
7875 }
7876
7877 // Look at Base + Index + Offset cases.
7878 SDValue Base = Ptr->getOperand(0);
7879 SDValue IndexOffset = Ptr->getOperand(1);
7880
7881 // Skip signextends.
7882 if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) {
7883 IndexOffset = IndexOffset->getOperand(0);
7884 IsIndexSignExt = true;
7885 }
7886
7887 // Either the case of Base + Index (no offset) or something else.
7888 if (IndexOffset->getOpcode() != ISD::ADD)
7889 return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt);
7890
7891 // Now we have the case of Base + Index + offset.
7892 SDValue Index = IndexOffset->getOperand(0);
7893 SDValue Offset = IndexOffset->getOperand(1);
7894
7895 if (!isa<ConstantSDNode>(Offset))
7896 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7897
7898 // Ignore signextends.
7899 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
7900 Index = Index->getOperand(0);
7901 IsIndexSignExt = true;
7902 } else IsIndexSignExt = false;
7903
7904 int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue();
7905 return BaseIndexOffset(Base, Index, Off, IsIndexSignExt);
7906 }
7907};
Nadav Rotemc653de62012-10-03 16:11:15 +00007908
7909/// Holds a pointer to an LSBaseSDNode as well as information on where it
7910/// is located in a sequence of memory operations connected by a chain.
7911struct MemOpLink {
7912 MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq):
7913 MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { }
7914 // Ptr to the mem node.
7915 LSBaseSDNode *MemNode;
7916 // Offset from the base ptr.
7917 int64_t OffsetFromBase;
7918 // What is the sequence number of this mem node.
7919 // Lowest mem operand in the DAG starts at zero.
7920 unsigned SequenceNum;
7921};
7922
7923/// Sorts store nodes in a link according to their offset from a shared
7924// base ptr.
7925struct ConsecutiveMemoryChainSorter {
7926 bool operator()(MemOpLink LHS, MemOpLink RHS) {
7927 return LHS.OffsetFromBase < RHS.OffsetFromBase;
7928 }
7929};
7930
7931bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) {
7932 EVT MemVT = St->getMemoryVT();
7933 int64_t ElementSizeBytes = MemVT.getSizeInBits()/8;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00007934 bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes().
7935 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nadav Rotemc653de62012-10-03 16:11:15 +00007936
7937 // Don't merge vectors into wider inputs.
7938 if (MemVT.isVector() || !MemVT.isSimple())
7939 return false;
7940
7941 // Perform an early exit check. Do not bother looking at stored values that
7942 // are not constants or loads.
7943 SDValue StoredVal = St->getValue();
7944 bool IsLoadSrc = isa<LoadSDNode>(StoredVal);
7945 if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) &&
7946 !IsLoadSrc)
7947 return false;
7948
7949 // Only look at ends of store sequences.
7950 SDValue Chain = SDValue(St, 1);
7951 if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE)
7952 return false;
7953
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007954 // This holds the base pointer, index, and the offset in bytes from the base
7955 // pointer.
7956 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00007957
7958 // We must have a base and an offset.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007959 if (!BasePtr.Base.getNode())
Nadav Rotemc653de62012-10-03 16:11:15 +00007960 return false;
7961
7962 // Do not handle stores to undef base pointers.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007963 if (BasePtr.Base.getOpcode() == ISD::UNDEF)
Nadav Rotemc653de62012-10-03 16:11:15 +00007964 return false;
7965
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007966 // Save the LoadSDNodes that we find in the chain.
7967 // We need to make sure that these nodes do not interfere with
7968 // any of the store nodes.
7969 SmallVector<LSBaseSDNode*, 8> AliasLoadNodes;
7970
7971 // Save the StoreSDNodes that we find in the chain.
Nadav Rotemc653de62012-10-03 16:11:15 +00007972 SmallVector<MemOpLink, 8> StoreNodes;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007973
Nadav Rotemc653de62012-10-03 16:11:15 +00007974 // Walk up the chain and look for nodes with offsets from the same
7975 // base pointer. Stop when reaching an instruction with a different kind
7976 // or instruction which has a different base pointer.
7977 unsigned Seq = 0;
7978 StoreSDNode *Index = St;
7979 while (Index) {
7980 // If the chain has more than one use, then we can't reorder the mem ops.
7981 if (Index != St && !SDValue(Index, 1)->hasOneUse())
7982 break;
7983
7984 // Find the base pointer and offset for this memory node.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007985 BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00007986
7987 // Check that the base pointer is the same as the original one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007988 if (!Ptr.equalBaseIndex(BasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00007989 break;
7990
7991 // Check that the alignment is the same.
7992 if (Index->getAlignment() != St->getAlignment())
7993 break;
7994
7995 // The memory operands must not be volatile.
7996 if (Index->isVolatile() || Index->isIndexed())
7997 break;
7998
7999 // No truncation.
8000 if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index))
8001 if (St->isTruncatingStore())
8002 break;
8003
8004 // The stored memory type must be the same.
8005 if (Index->getMemoryVT() != MemVT)
8006 break;
8007
8008 // We do not allow unaligned stores because we want to prevent overriding
8009 // stores.
8010 if (Index->getAlignment()*8 != MemVT.getSizeInBits())
8011 break;
8012
8013 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008014 StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++));
Nadav Rotemc653de62012-10-03 16:11:15 +00008015
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008016 // Find the next memory operand in the chain. If the next operand in the
8017 // chain is a store then move up and continue the scan with the next
8018 // memory operand. If the next operand is a load save it and use alias
8019 // information to check if it interferes with anything.
8020 SDNode *NextInChain = Index->getChain().getNode();
8021 while (1) {
Nadav Rotemdde785c2012-12-06 17:34:13 +00008022 if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) {
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008023 // We found a store node. Use it for the next iteration.
Nadav Rotemdde785c2012-12-06 17:34:13 +00008024 Index = STn;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008025 break;
8026 } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) {
8027 // Save the load node for later. Continue the scan.
8028 AliasLoadNodes.push_back(Ldn);
8029 NextInChain = Ldn->getChain().getNode();
8030 continue;
8031 } else {
8032 Index = NULL;
8033 break;
8034 }
8035 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008036 }
8037
8038 // Check if there is anything to merge.
8039 if (StoreNodes.size() < 2)
8040 return false;
8041
8042 // Sort the memory operands according to their distance from the base pointer.
8043 std::sort(StoreNodes.begin(), StoreNodes.end(),
8044 ConsecutiveMemoryChainSorter());
8045
8046 // Scan the memory operations on the chain and find the first non-consecutive
8047 // store memory address.
8048 unsigned LastConsecutiveStore = 0;
8049 int64_t StartAddress = StoreNodes[0].OffsetFromBase;
Nadav Rotemdde785c2012-12-06 17:34:13 +00008050 for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) {
8051
8052 // Check that the addresses are consecutive starting from the second
8053 // element in the list of stores.
8054 if (i > 0) {
8055 int64_t CurrAddress = StoreNodes[i].OffsetFromBase;
8056 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8057 break;
8058 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008059
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008060 bool Alias = false;
8061 // Check if this store interferes with any of the loads that we found.
8062 for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld)
8063 if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) {
8064 Alias = true;
8065 break;
8066 }
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008067 // We found a load that alias with this store. Stop the sequence.
8068 if (Alias)
8069 break;
8070
Nadav Rotemc653de62012-10-03 16:11:15 +00008071 // Mark this node as useful.
8072 LastConsecutiveStore = i;
8073 }
8074
8075 // The node with the lowest store address.
8076 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
8077
8078 // Store the constants into memory as one consecutive store.
8079 if (!IsLoadSrc) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008080 unsigned LastLegalType = 0;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008081 unsigned LastLegalVectorType = 0;
8082 bool NonZero = false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008083 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8084 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8085 SDValue StoredVal = St->getValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008086
8087 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008088 NonZero |= !C->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008089 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008090 NonZero |= !C->getConstantFPValue()->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008091 } else {
8092 // Non constant.
Nadav Rotemc653de62012-10-03 16:11:15 +00008093 break;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008094 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008095
Nadav Rotemc653de62012-10-03 16:11:15 +00008096 // Find a legal type for the constant store.
8097 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8098 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8099 if (TLI.isTypeLegal(StoreTy))
8100 LastLegalType = i+1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008101 // Or check whether a truncstore is legal.
8102 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8103 TargetLowering::TypePromoteInteger) {
8104 EVT LegalizedStoredValueTy =
8105 TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType());
8106 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy))
8107 LastLegalType = i+1;
8108 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008109
8110 // Find a legal type for the vector store.
8111 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8112 if (TLI.isTypeLegal(Ty))
8113 LastLegalVectorType = i + 1;
Nadav Rotemc653de62012-10-03 16:11:15 +00008114 }
8115
Bob Wilson99d8e762012-12-20 01:36:20 +00008116 // We only use vectors if the constant is known to be zero and the
8117 // function is not marked with the noimplicitfloat attribute.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008118 if (NonZero || NoVectors)
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008119 LastLegalVectorType = 0;
8120
Nadav Rotemc653de62012-10-03 16:11:15 +00008121 // Check if we found a legal integer type to store.
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008122 if (LastLegalType == 0 && LastLegalVectorType == 0)
Nadav Rotemc653de62012-10-03 16:11:15 +00008123 return false;
8124
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008125 bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008126 unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType;
8127
8128 // Make sure we have something to merge.
8129 if (NumElem < 2)
8130 return false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008131
8132 unsigned EarliestNodeUsed = 0;
8133 for (unsigned i=0; i < NumElem; ++i) {
8134 // Find a chain for the new wide-store operand. Notice that some
8135 // of the store nodes that we found may not be selected for inclusion
8136 // in the wide store. The chain we use needs to be the chain of the
8137 // earliest store node which is *used* and replaced by the wide store.
8138 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8139 EarliestNodeUsed = i;
8140 }
8141
8142 // The earliest Node in the DAG.
8143 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008144 SDLoc DL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008145
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008146 SDValue StoredVal;
8147 if (UseVector) {
8148 // Find a legal type for the vector store.
8149 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8150 assert(TLI.isTypeLegal(Ty) && "Illegal vector store");
8151 StoredVal = DAG.getConstant(0, Ty);
8152 } else {
8153 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8154 APInt StoreInt(StoreBW, 0);
8155
8156 // Construct a single integer constant which is made of the smaller
8157 // constant inputs.
8158 bool IsLE = TLI.isLittleEndian();
8159 for (unsigned i = 0; i < NumElem ; ++i) {
8160 unsigned Idx = IsLE ?(NumElem - 1 - i) : i;
8161 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode);
8162 SDValue Val = St->getValue();
8163 StoreInt<<=ElementSizeBytes*8;
8164 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) {
8165 StoreInt|=C->getAPIntValue().zext(StoreBW);
8166 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) {
8167 StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW);
8168 } else {
8169 assert(false && "Invalid constant element type");
8170 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008171 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008172
8173 // Create the new Load and Store operations.
8174 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8175 StoredVal = DAG.getConstant(StoreInt, StoreTy);
Nadav Rotemc653de62012-10-03 16:11:15 +00008176 }
8177
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008178 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal,
Nadav Rotemc653de62012-10-03 16:11:15 +00008179 FirstInChain->getBasePtr(),
8180 FirstInChain->getPointerInfo(),
8181 false, false,
8182 FirstInChain->getAlignment());
8183
8184 // Replace the first store with the new store
8185 CombineTo(EarliestOp, NewStore);
8186 // Erase all other stores.
8187 for (unsigned i = 0; i < NumElem ; ++i) {
8188 if (StoreNodes[i].MemNode == EarliestOp)
8189 continue;
8190 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
Rafael Espindola8e2b8ae2012-11-14 05:08:56 +00008191 // ReplaceAllUsesWith will replace all uses that existed when it was
8192 // called, but graph optimizations may cause new ones to appear. For
8193 // example, the case in pr14333 looks like
8194 //
8195 // St's chain -> St -> another store -> X
8196 //
8197 // And the only difference from St to the other store is the chain.
8198 // When we change it's chain to be St's chain they become identical,
8199 // get CSEed and the net result is that X is now a use of St.
8200 // Since we know that St is redundant, just iterate.
8201 while (!St->use_empty())
8202 DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain());
Nadav Rotemc653de62012-10-03 16:11:15 +00008203 removeFromWorkList(St);
8204 DAG.DeleteNode(St);
8205 }
8206
8207 return true;
8208 }
8209
8210 // Below we handle the case of multiple consecutive stores that
8211 // come from multiple consecutive loads. We merge them into a single
8212 // wide load and a single wide store.
8213
8214 // Look for load nodes which are used by the stored values.
8215 SmallVector<MemOpLink, 8> LoadNodes;
8216
8217 // Find acceptable loads. Loads need to have the same chain (token factor),
8218 // must not be zext, volatile, indexed, and they must be consecutive.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008219 BaseIndexOffset LdBasePtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008220 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8221 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8222 LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue());
8223 if (!Ld) break;
8224
8225 // Loads must only have one use.
8226 if (!Ld->hasNUsesOfValue(1, 0))
8227 break;
8228
8229 // Check that the alignment is the same as the stores.
8230 if (Ld->getAlignment() != St->getAlignment())
8231 break;
8232
8233 // The memory operands must not be volatile.
8234 if (Ld->isVolatile() || Ld->isIndexed())
8235 break;
8236
8237 // We do not accept ext loads.
8238 if (Ld->getExtensionType() != ISD::NON_EXTLOAD)
8239 break;
8240
8241 // The stored memory type must be the same.
8242 if (Ld->getMemoryVT() != MemVT)
8243 break;
8244
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008245 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008246 // If this is not the first ptr that we check.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008247 if (LdBasePtr.Base.getNode()) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008248 // The base ptr must be the same.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008249 if (!LdPtr.equalBaseIndex(LdBasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008250 break;
8251 } else {
8252 // Check that all other base pointers are the same as this one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008253 LdBasePtr = LdPtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008254 }
8255
8256 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008257 LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0));
Nadav Rotemc653de62012-10-03 16:11:15 +00008258 }
8259
8260 if (LoadNodes.size() < 2)
8261 return false;
8262
8263 // Scan the memory operations on the chain and find the first non-consecutive
8264 // load memory address. These variables hold the index in the store node
8265 // array.
8266 unsigned LastConsecutiveLoad = 0;
8267 // This variable refers to the size and not index in the array.
8268 unsigned LastLegalVectorType = 0;
8269 unsigned LastLegalIntegerType = 0;
8270 StartAddress = LoadNodes[0].OffsetFromBase;
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008271 SDValue FirstChain = LoadNodes[0].MemNode->getChain();
8272 for (unsigned i = 1; i < LoadNodes.size(); ++i) {
8273 // All loads much share the same chain.
8274 if (LoadNodes[i].MemNode->getChain() != FirstChain)
8275 break;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008276
Nadav Rotemc653de62012-10-03 16:11:15 +00008277 int64_t CurrAddress = LoadNodes[i].OffsetFromBase;
8278 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8279 break;
8280 LastConsecutiveLoad = i;
8281
8282 // Find a legal type for the vector store.
8283 EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8284 if (TLI.isTypeLegal(StoreTy))
8285 LastLegalVectorType = i + 1;
8286
8287 // Find a legal type for the integer store.
8288 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8289 StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8290 if (TLI.isTypeLegal(StoreTy))
8291 LastLegalIntegerType = i + 1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008292 // Or check whether a truncstore and extload is legal.
8293 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8294 TargetLowering::TypePromoteInteger) {
8295 EVT LegalizedStoredValueTy =
8296 TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy);
8297 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) &&
8298 TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) &&
8299 TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) &&
8300 TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy))
8301 LastLegalIntegerType = i+1;
8302 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008303 }
8304
8305 // Only use vector types if the vector type is larger than the integer type.
8306 // If they are the same, use integers.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008307 bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors;
Nadav Rotemc653de62012-10-03 16:11:15 +00008308 unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType);
8309
8310 // We add +1 here because the LastXXX variables refer to location while
8311 // the NumElem refers to array/index size.
8312 unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1;
8313 NumElem = std::min(LastLegalType, NumElem);
8314
8315 if (NumElem < 2)
8316 return false;
8317
8318 // The earliest Node in the DAG.
8319 unsigned EarliestNodeUsed = 0;
8320 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
8321 for (unsigned i=1; i<NumElem; ++i) {
8322 // Find a chain for the new wide-store operand. Notice that some
8323 // of the store nodes that we found may not be selected for inclusion
8324 // in the wide store. The chain we use needs to be the chain of the
8325 // earliest store node which is *used* and replaced by the wide store.
8326 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8327 EarliestNodeUsed = i;
8328 }
8329
8330 // Find if it is better to use vectors or integers to load and store
8331 // to memory.
8332 EVT JointMemOpVT;
8333 if (UseVectorTy) {
8334 JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8335 } else {
8336 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8337 JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8338 }
8339
Andrew Trickac6d9be2013-05-25 02:42:55 +00008340 SDLoc LoadDL(LoadNodes[0].MemNode);
8341 SDLoc StoreDL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008342
8343 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode);
8344 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL,
8345 FirstLoad->getChain(),
8346 FirstLoad->getBasePtr(),
8347 FirstLoad->getPointerInfo(),
8348 false, false, false,
8349 FirstLoad->getAlignment());
8350
8351 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad,
8352 FirstInChain->getBasePtr(),
8353 FirstInChain->getPointerInfo(), false, false,
8354 FirstInChain->getAlignment());
8355
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008356 // Replace one of the loads with the new load.
8357 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode);
8358 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1),
8359 SDValue(NewLoad.getNode(), 1));
8360
8361 // Remove the rest of the load chains.
8362 for (unsigned i = 1; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008363 // Replace all chain users of the old load nodes with the chain of the new
8364 // load node.
8365 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode);
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008366 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain());
8367 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008368
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008369 // Replace the first store with the new store.
8370 CombineTo(EarliestOp, NewStore);
8371 // Erase all other stores.
8372 for (unsigned i = 0; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008373 // Remove all Store nodes.
8374 if (StoreNodes[i].MemNode == EarliestOp)
8375 continue;
8376 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8377 DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain());
8378 removeFromWorkList(St);
8379 DAG.DeleteNode(St);
8380 }
8381
8382 return true;
8383}
8384
Dan Gohman475871a2008-07-27 21:46:04 +00008385SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00008386 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00008387 SDValue Chain = ST->getChain();
8388 SDValue Value = ST->getValue();
8389 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00008390
Evan Cheng59d5b682007-05-07 21:27:48 +00008391 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00008392 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008393 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008394 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00008395 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008396 EVT SVT = Value.getOperand(0).getValueType();
Micah Villmow3574eca2012-10-08 16:38:25 +00008397 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00008398 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008399 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00008400 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008401 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00008402 return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00008403 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008404 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00008405 }
Owen Andersona34d9362011-04-14 17:30:49 +00008406
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008407 // Turn 'store undef, Ptr' -> nothing.
8408 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
8409 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008410
Nate Begeman2cbba892006-12-11 02:23:46 +00008411 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00008412 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008413 // NOTE: If the original store is volatile, this transform must not increase
8414 // the number of stores. For example, on x86-32 an f64 can be stored in one
8415 // processor operation but an i64 (which is not legal) requires two. So the
8416 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00008417 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00008418 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008419 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008420 default: llvm_unreachable("Unknown FP type");
Pete Cooper438c0402012-06-21 18:00:39 +00008421 case MVT::f16: // We don't do this for these yet.
8422 case MVT::f80:
Owen Anderson825b72b2009-08-11 20:47:22 +00008423 case MVT::f128:
8424 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00008425 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008426 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00008427 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00008428 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00008429 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00008430 bitcastToAPInt().getZExtValue(), MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008431 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008432 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008433 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00008434 }
8435 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008436 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00008437 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008438 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00008439 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00008440 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00008441 getZExtValue(), MVT::i64);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008442 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008443 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008444 ST->isNonTemporal(), ST->getAlignment());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008445 }
Owen Andersona34d9362011-04-14 17:30:49 +00008446
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008447 if (!ST->isVolatile() &&
8448 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00008449 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00008450 // argument passing. Since this is so common, custom legalize the
8451 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00008452 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008453 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
8454 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00008455 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00008456
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00008457 unsigned Alignment = ST->getAlignment();
8458 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00008459 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00008460
Andrew Trickac6d9be2013-05-25 02:42:55 +00008461 SDValue St0 = DAG.getStore(Chain, SDLoc(ST), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008462 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008463 isVolatile, isNonTemporal,
8464 ST->getAlignment());
Andrew Trickac6d9be2013-05-25 02:42:55 +00008465 Ptr = DAG.getNode(ISD::ADD, SDLoc(N), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00008466 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00008467 Alignment = MinAlign(Alignment, 4U);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008468 SDValue St1 = DAG.getStore(Chain, SDLoc(ST), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008469 Ptr, ST->getPointerInfo().getWithOffset(4),
8470 isVolatile, isNonTemporal,
David Greene1e559442010-02-15 17:00:31 +00008471 Alignment);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008472 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00008473 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00008474 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008475
Chris Lattner62be1a72006-12-12 04:16:14 +00008476 break;
Evan Cheng25ece662006-12-11 17:25:19 +00008477 }
Nate Begeman2cbba892006-12-11 02:23:46 +00008478 }
Nate Begeman2cbba892006-12-11 02:23:46 +00008479 }
8480
Evan Cheng255f20f2010-04-01 06:04:33 +00008481 // Try to infer better alignment information than the store already has.
8482 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00008483 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
8484 if (Align > ST->getAlignment())
Andrew Trickac6d9be2013-05-25 02:42:55 +00008485 return DAG.getTruncStore(Chain, SDLoc(N), Value,
Evan Chenged1c0c72011-11-28 22:37:34 +00008486 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
8487 ST->isVolatile(), ST->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00008488 }
8489 }
8490
Evan Cheng31959b12011-02-02 01:06:55 +00008491 // Try transforming a pair floating point load / store ops to integer
8492 // load / store ops.
8493 SDValue NewST = TransformFPLoadStorePair(N);
8494 if (NewST.getNode())
8495 return NewST;
8496
Scott Michelfdc40a02009-02-17 22:15:04 +00008497 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00008498 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008499 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00008500
Jim Laskey6ff23e52006-10-04 16:53:27 +00008501 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00008502 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00008503 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008504
8505 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008506 if (ST->isTruncatingStore()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008507 ReplStore = DAG.getTruncStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008508 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008509 ST->getMemoryVT(), ST->isVolatile(),
8510 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008511 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008512 ReplStore = DAG.getStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008513 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008514 ST->isVolatile(), ST->isNonTemporal(),
8515 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008516 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008517
Jim Laskey279f0532006-09-25 16:29:54 +00008518 // Create token to keep both nodes around.
Andrew Trickac6d9be2013-05-25 02:42:55 +00008519 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00008520 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00008521
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008522 // Make sure the new and old chains are cleaned up.
8523 AddToWorkList(Token.getNode());
8524
Jim Laskey274062c2006-10-13 23:32:28 +00008525 // Don't add users to work list.
8526 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00008527 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00008528 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008529
Evan Cheng33dbedc2006-11-05 09:31:14 +00008530 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00008531 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00008532 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00008533
Chris Lattner3c872852007-12-29 06:26:16 +00008534 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00008535 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00008536 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00008537 // See if we can simplify the input to this truncstore with knowledge that
8538 // only the low bits are being used. For example:
8539 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00008540 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00008541 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00008542 APInt::getLowBitsSet(
8543 Value.getValueType().getScalarType().getSizeInBits(),
8544 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00008545 AddToWorkList(Value.getNode());
8546 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00008547 return DAG.getTruncStore(Chain, SDLoc(N), Shorter,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008548 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00008549 ST->isVolatile(), ST->isNonTemporal(),
8550 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00008551
Chris Lattnere33544c2007-10-13 06:58:48 +00008552 // Otherwise, see if we can simplify the operation with
8553 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00008554 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00008555 APInt::getLowBitsSet(
8556 Value.getValueType().getScalarType().getSizeInBits(),
8557 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00008558 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00008559 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008560
Chris Lattner3c872852007-12-29 06:26:16 +00008561 // If this is a load followed by a store to the same location, then the store
8562 // is dead/noop.
8563 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008564 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008565 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00008566 // There can't be any side effects between the load and store, such as
8567 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00008568 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00008569 // The store is dead, remove it.
8570 return Chain;
8571 }
8572 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008573
Chris Lattnerddf89562008-01-17 19:59:44 +00008574 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
8575 // truncating store. We can do this even if this is already a truncstore.
8576 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00008577 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008578 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008579 ST->getMemoryVT())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008580 return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008581 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00008582 ST->isVolatile(), ST->isNonTemporal(),
8583 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00008584 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008585
Nadav Rotemc653de62012-10-03 16:11:15 +00008586 // Only perform this optimization before the types are legal, because we
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008587 // don't want to perform this optimization on every DAGCombine invocation.
Nadav Rotema569a802012-12-02 17:14:09 +00008588 if (!LegalTypes) {
8589 bool EverChanged = false;
8590
8591 do {
8592 // There can be multiple store sequences on the same chain.
8593 // Keep trying to merge store sequences until we are unable to do so
8594 // or until we merge the last store on the chain.
8595 bool Changed = MergeConsecutiveStores(ST);
8596 EverChanged |= Changed;
8597 if (!Changed) break;
8598 } while (ST->getOpcode() != ISD::DELETED_NODE);
8599
8600 if (EverChanged)
8601 return SDValue(N, 0);
8602 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008603
Evan Cheng8b944d32009-05-28 00:35:15 +00008604 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00008605}
8606
Dan Gohman475871a2008-07-27 21:46:04 +00008607SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
8608 SDValue InVec = N->getOperand(0);
8609 SDValue InVal = N->getOperand(1);
8610 SDValue EltNo = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008611 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00008612
Bob Wilson492fd452010-05-19 23:42:58 +00008613 // If the inserted element is an UNDEF, just use the input vector.
8614 if (InVal.getOpcode() == ISD::UNDEF)
8615 return InVec;
8616
Nadav Rotem609d54e2011-02-12 14:40:33 +00008617 EVT VT = InVec.getValueType();
8618
Owen Anderson95771af2011-02-25 21:41:48 +00008619 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00008620 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
8621 return SDValue();
8622
Eli Friedman9db817f2011-09-09 21:04:06 +00008623 // Check that we know which element is being inserted
8624 if (!isa<ConstantSDNode>(EltNo))
8625 return SDValue();
8626 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008627
Eli Friedman9db817f2011-09-09 21:04:06 +00008628 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
8629 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
8630 // vector elements.
8631 SmallVector<SDValue, 8> Ops;
8632 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
8633 Ops.append(InVec.getNode()->op_begin(),
8634 InVec.getNode()->op_end());
8635 } else if (InVec.getOpcode() == ISD::UNDEF) {
8636 unsigned NElts = VT.getVectorNumElements();
8637 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
8638 } else {
8639 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00008640 }
Eli Friedman9db817f2011-09-09 21:04:06 +00008641
8642 // Insert the element
8643 if (Elt < Ops.size()) {
8644 // All the operands of BUILD_VECTOR must have the same type;
8645 // we enforce that here.
8646 EVT OpVT = Ops[0].getValueType();
8647 if (InVal.getValueType() != OpVT)
8648 InVal = OpVT.bitsGT(InVal.getValueType()) ?
8649 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
8650 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
8651 Ops[Elt] = InVal;
8652 }
8653
8654 // Return the new vector
8655 return DAG.getNode(ISD::BUILD_VECTOR, dl,
8656 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00008657}
8658
Dan Gohman475871a2008-07-27 21:46:04 +00008659SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008660 // (vextract (scalar_to_vector val, 0) -> val
8661 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00008662 EVT VT = InVec.getValueType();
8663 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008664
Duncan Sandsc356f332011-05-09 08:03:33 +00008665 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
8666 // Check if the result type doesn't match the inserted element type. A
8667 // SCALAR_TO_VECTOR may truncate the inserted element and the
8668 // EXTRACT_VECTOR_ELT may widen the extracted vector.
8669 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00008670 if (InOp.getValueType() != NVT) {
8671 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Andrew Trickac6d9be2013-05-25 02:42:55 +00008672 return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT);
Duncan Sandsc356f332011-05-09 08:03:33 +00008673 }
8674 return InOp;
8675 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008676
Nadav Rotemba05c912012-01-17 21:44:01 +00008677 SDValue EltNo = N->getOperand(1);
8678 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
8679
8680 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
8681 // We only perform this optimization before the op legalization phase because
Nadav Rotem6dfabb62012-09-20 08:53:31 +00008682 // we may introduce new vector instructions which are not backed by TD
8683 // patterns. For example on AVX, extracting elements from a wide vector
8684 // without using extract_subvector.
Nadav Rotemba05c912012-01-17 21:44:01 +00008685 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
8686 && ConstEltNo && !LegalOperations) {
8687 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
8688 int NumElem = VT.getVectorNumElements();
8689 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
8690 // Find the new index to extract from.
8691 int OrigElt = SVOp->getMaskElt(Elt);
8692
8693 // Extracting an undef index is undef.
8694 if (OrigElt == -1)
8695 return DAG.getUNDEF(NVT);
8696
8697 // Select the right vector half to extract from.
8698 if (OrigElt < NumElem) {
8699 InVec = InVec->getOperand(0);
8700 } else {
8701 InVec = InVec->getOperand(1);
8702 OrigElt -= NumElem;
8703 }
8704
Jim Grosbacha249f7d2012-05-08 20:56:07 +00008705 EVT IndexTy = N->getOperand(1).getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008706 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00008707 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemba05c912012-01-17 21:44:01 +00008708 }
8709
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008710 // Perform only after legalization to ensure build_vector / vector_shuffle
8711 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00008712 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008713
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008714 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
8715 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
8716 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00008717
Nadav Rotemba05c912012-01-17 21:44:01 +00008718 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00008719 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00008720 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00008721 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00008722 EVT ExtVT = VT.getVectorElementType();
8723 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00008724
Evan Cheng84387ea2012-03-13 22:00:52 +00008725 // If the result of load has to be truncated, then it's not necessarily
8726 // profitable.
Evan Chenga03d3662012-03-13 22:16:11 +00008727 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng84387ea2012-03-13 22:00:52 +00008728 return SDValue();
8729
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008730 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008731 // Don't duplicate a load with other uses.
8732 if (!InVec.hasOneUse())
8733 return SDValue();
8734
Owen Andersone50ed302009-08-10 22:56:29 +00008735 EVT BCVT = InVec.getOperand(0).getValueType();
8736 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008737 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00008738 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
8739 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008740 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00008741 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008742 NewLoad = true;
8743 }
Evan Cheng513da432007-10-06 08:19:55 +00008744
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008745 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00008746 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00008747 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008748 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00008749 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00008750 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00008751 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008752 // Don't duplicate a load with other uses.
8753 if (!InVec.hasOneUse())
8754 return SDValue();
8755
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008756 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00008757 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008758 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
8759 // =>
8760 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00008761
Eli Friedmand6e25602011-12-26 22:49:32 +00008762 // Don't duplicate a load with other uses.
8763 if (!InVec.hasOneUse())
8764 return SDValue();
8765
Mon P Wanga60b5232008-12-11 00:26:16 +00008766 // If the bit convert changed the number of elements, it is unsafe
8767 // to examine the mask.
8768 if (BCNumEltsChanged)
8769 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00008770
8771 // Select the input vector, guarding against out of range extract vector.
8772 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00008773 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00008774 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
8775
Eli Friedmand6e25602011-12-26 22:49:32 +00008776 if (InVec.getOpcode() == ISD::BITCAST) {
8777 // Don't duplicate a load with other uses.
8778 if (!InVec.hasOneUse())
8779 return SDValue();
8780
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008781 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00008782 }
Gabor Greifba36cb52008-08-28 21:40:38 +00008783 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008784 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00008785 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00008786 }
8787 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008788
Eli Friedmand6e25602011-12-26 22:49:32 +00008789 // Make sure we found a non-volatile load and the extractelement is
8790 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00008791 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00008792 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008793
Eric Christopherd81f17a2010-11-03 20:44:42 +00008794 // If Idx was -1 above, Elt is going to be -1, so just return undef.
8795 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00008796 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00008797
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008798 unsigned Align = LN0->getAlignment();
8799 if (NewLoad) {
8800 // Check the resultant load doesn't need a higher alignment than the
8801 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00008802 unsigned NewAlign =
Micah Villmow3574eca2012-10-08 16:38:25 +00008803 TLI.getDataLayout()
Eric Christopher503a64d2010-12-09 04:48:06 +00008804 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00008805
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008806 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00008807 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00008808
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008809 Align = NewAlign;
8810 }
8811
Dan Gohman475871a2008-07-27 21:46:04 +00008812 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00008813 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008814
Eric Christopherd81f17a2010-11-03 20:44:42 +00008815 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00008816 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00008817 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008818 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008819 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008820 NewPtr = DAG.getNode(ISD::ADD, SDLoc(N), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008821 DAG.getConstant(PtrOff, PtrType));
8822 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008823
Eli Friedman4db4add2011-11-16 23:50:22 +00008824 // The replacement we need to do here is a little tricky: we need to
8825 // replace an extractelement of a load with a load.
8826 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00008827 // Note that this replacement assumes that the extractvalue is the only
8828 // use of the load; that's okay because we don't want to perform this
8829 // transformation in other cases anyway.
Evan Cheng84387ea2012-03-13 22:00:52 +00008830 SDValue Load;
Evan Chenga03d3662012-03-13 22:16:11 +00008831 SDValue Chain;
Evan Cheng84387ea2012-03-13 22:00:52 +00008832 if (NVT.bitsGT(LVT)) {
8833 // If the result type of vextract is wider than the load, then issue an
8834 // extending load instead.
8835 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
8836 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008837 Load = DAG.getExtLoad(ExtType, SDLoc(N), NVT, LN0->getChain(),
Evan Cheng84387ea2012-03-13 22:00:52 +00008838 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
8839 LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008840 Chain = Load.getValue(1);
8841 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008842 Load = DAG.getLoad(LVT, SDLoc(N), LN0->getChain(), NewPtr,
Evan Cheng84387ea2012-03-13 22:00:52 +00008843 LN0->getPointerInfo().getWithOffset(PtrOff),
Stephen Lin155615d2013-07-08 00:37:03 +00008844 LN0->isVolatile(), LN0->isNonTemporal(),
Evan Cheng84387ea2012-03-13 22:00:52 +00008845 LN0->isInvariant(), Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008846 Chain = Load.getValue(1);
8847 if (NVT.bitsLT(LVT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00008848 Load = DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00008849 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00008850 Load = DAG.getNode(ISD::BITCAST, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00008851 }
Eli Friedman4db4add2011-11-16 23:50:22 +00008852 WorkListRemover DeadNodes(*this);
8853 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chenga03d3662012-03-13 22:16:11 +00008854 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00008855 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedman4db4add2011-11-16 23:50:22 +00008856 // Since we're explcitly calling ReplaceAllUses, add the new node to the
8857 // worklist explicitly as well.
8858 AddToWorkList(Load.getNode());
Craig Topper0c9da212012-03-20 05:28:39 +00008859 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedman4db4add2011-11-16 23:50:22 +00008860 // Make sure to revisit this node to clean it up; it will usually be dead.
8861 AddToWorkList(N);
8862 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00008863 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008864
Dan Gohman475871a2008-07-27 21:46:04 +00008865 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00008866}
Evan Cheng513da432007-10-06 08:19:55 +00008867
Michael Liaofac14ab2012-10-23 23:06:52 +00008868// Simplify (build_vec (ext )) to (bitcast (build_vec ))
8869SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) {
8870 // We perform this optimization post type-legalization because
8871 // the type-legalizer often scalarizes integer-promoted vectors.
8872 // Performing this optimization before may create bit-casts which
8873 // will be type-legalized to complex code sequences.
8874 // We perform this optimization only before the operation legalizer because we
8875 // may introduce illegal operations.
8876 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes)
8877 return SDValue();
8878
Dan Gohman7f321562007-06-25 16:23:39 +00008879 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008880 SDLoc dl(N);
Owen Andersone50ed302009-08-10 22:56:29 +00008881 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008882
Nadav Rotemb00418a2011-10-29 21:23:04 +00008883 // Check to see if this is a BUILD_VECTOR of a bunch of values
8884 // which come from any_extend or zero_extend nodes. If so, we can create
8885 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00008886 // optimizations. We do not handle sign-extend because we can't fill the sign
8887 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008888 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00008889 bool AllAnyExt = true;
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008890
Craig Topperd3b58892012-01-17 09:09:48 +00008891 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00008892 SDValue In = N->getOperand(i);
8893 // Ignore undef inputs.
8894 if (In.getOpcode() == ISD::UNDEF) continue;
8895
8896 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
8897 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
8898
Nadav Rotemf47368b2011-10-31 20:08:25 +00008899 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008900 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00008901 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008902 break;
8903 }
8904
8905 // The input is a ZeroExt or AnyExt. Check the original type.
8906 EVT InTy = In.getOperand(0).getValueType();
8907
8908 // Check that all of the widened source types are the same.
8909 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00008910 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008911 SourceType = InTy;
8912 else if (InTy != SourceType) {
8913 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00008914 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008915 break;
8916 }
8917
8918 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00008919 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008920 }
8921
Nadav Rotemf47368b2011-10-31 20:08:25 +00008922 // In order to have valid types, all of the inputs must be extended from the
8923 // same source type and all of the inputs must be any or zero extend.
8924 // Scalar sizes must be a power of two.
Michael Liaofac14ab2012-10-23 23:06:52 +00008925 EVT OutScalarTy = VT.getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008926 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00008927 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
8928 isPowerOf2_32(SourceType.getSizeInBits());
8929
Nadav Rotem6431ff92012-03-15 08:49:06 +00008930 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
8931 // turn into a single shuffle instruction.
Michael Liaofac14ab2012-10-23 23:06:52 +00008932 if (!ValidTypes)
8933 return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00008934
Michael Liaofac14ab2012-10-23 23:06:52 +00008935 bool isLE = TLI.isLittleEndian();
8936 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
8937 assert(ElemRatio > 1 && "Invalid element size ratio");
8938 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
8939 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008940
Michael Liaofac14ab2012-10-23 23:06:52 +00008941 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements();
8942 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008943
Michael Liaofac14ab2012-10-23 23:06:52 +00008944 // Populate the new build_vector
Jakub Staszakadf38912012-10-24 00:38:25 +00008945 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Michael Liaofac14ab2012-10-23 23:06:52 +00008946 SDValue Cast = N->getOperand(i);
8947 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
8948 Cast.getOpcode() == ISD::ZERO_EXTEND ||
8949 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
8950 SDValue In;
8951 if (Cast.getOpcode() == ISD::UNDEF)
8952 In = DAG.getUNDEF(SourceType);
8953 else
8954 In = Cast->getOperand(0);
8955 unsigned Index = isLE ? (i * ElemRatio) :
8956 (i * ElemRatio + (ElemRatio - 1));
Nadav Rotemb00418a2011-10-29 21:23:04 +00008957
Michael Liaofac14ab2012-10-23 23:06:52 +00008958 assert(Index < Ops.size() && "Invalid index");
8959 Ops[Index] = In;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008960 }
Chris Lattnerca242442006-03-19 01:27:56 +00008961
Michael Liaofac14ab2012-10-23 23:06:52 +00008962 // The type of the new BUILD_VECTOR node.
8963 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
8964 assert(VecVT.getSizeInBits() == VT.getSizeInBits() &&
8965 "Invalid vector size");
8966 // Check if the new vector type is legal.
8967 if (!isTypeLegal(VecVT)) return SDValue();
8968
8969 // Make the new BUILD_VECTOR.
8970 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size());
8971
8972 // The new BUILD_VECTOR node has the potential to be further optimized.
8973 AddToWorkList(BV.getNode());
8974 // Bitcast to the desired type.
8975 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8976}
8977
Michael Liao1a5cc712012-10-24 04:14:18 +00008978SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) {
8979 EVT VT = N->getValueType(0);
8980
8981 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008982 SDLoc dl(N);
Michael Liao1a5cc712012-10-24 04:14:18 +00008983
8984 EVT SrcVT = MVT::Other;
8985 unsigned Opcode = ISD::DELETED_NODE;
8986 unsigned NumDefs = 0;
8987
8988 for (unsigned i = 0; i != NumInScalars; ++i) {
8989 SDValue In = N->getOperand(i);
8990 unsigned Opc = In.getOpcode();
8991
8992 if (Opc == ISD::UNDEF)
8993 continue;
8994
8995 // If all scalar values are floats and converted from integers.
8996 if (Opcode == ISD::DELETED_NODE &&
8997 (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) {
8998 Opcode = Opc;
Michael Liao1a5cc712012-10-24 04:14:18 +00008999 }
Tom Stellardd40758b2013-01-02 22:13:01 +00009000
Michael Liao1a5cc712012-10-24 04:14:18 +00009001 if (Opc != Opcode)
9002 return SDValue();
9003
9004 EVT InVT = In.getOperand(0).getValueType();
9005
9006 // If all scalar values are typed differently, bail out. It's chosen to
9007 // simplify BUILD_VECTOR of integer types.
9008 if (SrcVT == MVT::Other)
9009 SrcVT = InVT;
9010 if (SrcVT != InVT)
9011 return SDValue();
9012 NumDefs++;
9013 }
9014
9015 // If the vector has just one element defined, it's not worth to fold it into
9016 // a vectorized one.
9017 if (NumDefs < 2)
9018 return SDValue();
9019
9020 assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP)
9021 && "Should only handle conversion from integer to float.");
9022 assert(SrcVT != MVT::Other && "Cannot determine source type!");
9023
9024 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars);
Tom Stellardd40758b2013-01-02 22:13:01 +00009025
9026 if (!TLI.isOperationLegalOrCustom(Opcode, NVT))
9027 return SDValue();
9028
Michael Liao1a5cc712012-10-24 04:14:18 +00009029 SmallVector<SDValue, 8> Opnds;
9030 for (unsigned i = 0; i != NumInScalars; ++i) {
9031 SDValue In = N->getOperand(i);
9032
9033 if (In.getOpcode() == ISD::UNDEF)
9034 Opnds.push_back(DAG.getUNDEF(SrcVT));
9035 else
9036 Opnds.push_back(In.getOperand(0));
9037 }
9038 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT,
9039 &Opnds[0], Opnds.size());
9040 AddToWorkList(BV.getNode());
9041
9042 return DAG.getNode(Opcode, dl, VT, BV);
9043}
9044
Michael Liaofac14ab2012-10-23 23:06:52 +00009045SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
9046 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009047 SDLoc dl(N);
Michael Liaofac14ab2012-10-23 23:06:52 +00009048 EVT VT = N->getValueType(0);
9049
9050 // A vector built entirely of undefs is undef.
9051 if (ISD::allOperandsUndef(N))
9052 return DAG.getUNDEF(VT);
9053
9054 SDValue V = reduceBuildVecExtToExtBuildVec(N);
9055 if (V.getNode())
9056 return V;
9057
Michael Liao1a5cc712012-10-24 04:14:18 +00009058 V = reduceBuildVecConvertToConvertBuildVec(N);
9059 if (V.getNode())
9060 return V;
9061
Dan Gohman7f321562007-06-25 16:23:39 +00009062 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
9063 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
9064 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands00294ca2012-03-19 15:35:44 +00009065
9066 // May only combine to shuffle after legalize if shuffle is legal.
9067 if (LegalOperations &&
9068 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
9069 return SDValue();
9070
Dan Gohman475871a2008-07-27 21:46:04 +00009071 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009072 for (unsigned i = 0; i != NumInScalars; ++i) {
9073 // Ignore undef inputs.
9074 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009075
Dan Gohman7f321562007-06-25 16:23:39 +00009076 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00009077 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00009078 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00009079 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00009080 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009081 break;
9082 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009083
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009084 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00009085 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009086 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
9087 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009088
Gabor Greifba36cb52008-08-28 21:40:38 +00009089 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009090 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00009091 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009092 VecIn2 = ExtractedFromVec;
9093 } else {
9094 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009095 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009096 break;
9097 }
9098 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009099
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009100 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00009101 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009102 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009103 for (unsigned i = 0; i != NumInScalars; ++i) {
9104 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009105 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009106 continue;
9107 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009108
Rafael Espindola15684b22009-04-24 12:40:33 +00009109 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00009110 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00009111 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009112 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00009113 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
9114 if (ExtIndex > VT.getVectorNumElements())
9115 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009116
Nate Begeman5a5ca152009-04-29 05:20:52 +00009117 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009118 continue;
9119 }
9120
9121 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00009122 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00009123 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009124 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009125
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009126 // We can't generate a shuffle node with mismatched input and output types.
9127 // Attempt to transform a single input vector to the correct type.
9128 if ((VT != VecIn1.getValueType())) {
9129 // We don't support shuffeling between TWO values of different types.
9130 if (VecIn2.getNode() != 0)
9131 return SDValue();
9132
9133 // We only support widening of vectors which are half the size of the
9134 // output registers. For example XMM->YMM widening on X86 with AVX.
9135 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
9136 return SDValue();
9137
James Molloy8cd08bf2012-09-10 14:01:21 +00009138 // If the input vector type has a different base type to the output
9139 // vector type, bail out.
9140 if (VecIn1.getValueType().getVectorElementType() !=
9141 VT.getVectorElementType())
9142 return SDValue();
9143
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009144 // Widen the input vector by adding undef values.
Michael Liaofac14ab2012-10-23 23:06:52 +00009145 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009146 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009147 }
9148
9149 // If VecIn2 is unused then change it to undef.
9150 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
9151
Nadav Rotem6dfabb62012-09-20 08:53:31 +00009152 // Check that we were able to transform all incoming values to the same
9153 // type.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009154 if (VecIn2.getValueType() != VecIn1.getValueType() ||
9155 VecIn1.getValueType() != VT)
9156 return SDValue();
9157
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009158 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009159 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00009160 return SDValue();
9161
Dan Gohman7f321562007-06-25 16:23:39 +00009162 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00009163 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00009164 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009165 Ops[1] = VecIn2;
Michael Liaofac14ab2012-10-23 23:06:52 +00009166 return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009167 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009168
Dan Gohman475871a2008-07-27 21:46:04 +00009169 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00009170}
9171
Dan Gohman475871a2008-07-27 21:46:04 +00009172SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00009173 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
9174 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
9175 // inputs come from at most two distinct vectors, turn this into a shuffle
9176 // node.
9177
9178 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00009179 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00009180 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009181
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009182 // Check if all of the operands are undefs.
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009183 if (ISD::allOperandsUndef(N))
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009184 return DAG.getUNDEF(N->getValueType(0));
9185
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009186 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR
9187 // nodes often generate nop CONCAT_VECTOR nodes.
9188 // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that
9189 // place the incoming vectors at the exact same location.
9190 SDValue SingleSource = SDValue();
9191 unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements();
9192
9193 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
9194 SDValue Op = N->getOperand(i);
9195
9196 if (Op.getOpcode() == ISD::UNDEF)
9197 continue;
9198
9199 // Check if this is the identity extract:
9200 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
9201 return SDValue();
9202
9203 // Find the single incoming vector for the extract_subvector.
9204 if (SingleSource.getNode()) {
9205 if (Op.getOperand(0) != SingleSource)
9206 return SDValue();
9207 } else {
9208 SingleSource = Op.getOperand(0);
Michael Kuperstein27202482013-05-06 08:06:13 +00009209
9210 // Check the source type is the same as the type of the result.
9211 // If not, this concat may extend the vector, so we can not
9212 // optimize it away.
9213 if (SingleSource.getValueType() != N->getValueType(0))
9214 return SDValue();
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009215 }
9216
9217 unsigned IdentityIndex = i * PartNumElem;
9218 ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9219 // The extract index must be constant.
9220 if (!CS)
9221 return SDValue();
Stephen Lin155615d2013-07-08 00:37:03 +00009222
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009223 // Check that we are reading from the identity index.
9224 if (CS->getZExtValue() != IdentityIndex)
9225 return SDValue();
9226 }
9227
9228 if (SingleSource.getNode())
9229 return SingleSource;
Stephen Lin155615d2013-07-08 00:37:03 +00009230
Dan Gohman475871a2008-07-27 21:46:04 +00009231 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00009232}
9233
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009234SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
9235 EVT NVT = N->getValueType(0);
9236 SDValue V = N->getOperand(0);
9237
Michael Liao13429e22012-10-17 20:48:33 +00009238 if (V->getOpcode() == ISD::CONCAT_VECTORS) {
9239 // Combine:
9240 // (extract_subvec (concat V1, V2, ...), i)
9241 // Into:
9242 // Vi if possible
Michael Liao9aecdb52012-10-19 03:17:00 +00009243 // Only operand 0 is checked as 'concat' assumes all inputs of the same type.
9244 if (V->getOperand(0).getValueType() != NVT)
9245 return SDValue();
Michael Liao13429e22012-10-17 20:48:33 +00009246 unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9247 unsigned NumElems = NVT.getVectorNumElements();
9248 assert((Idx % NumElems) == 0 &&
9249 "IDX in concat is not a multiple of the result vector length.");
9250 return V->getOperand(Idx / NumElems);
9251 }
9252
Michael Liaob4f98ea2013-03-25 23:47:35 +00009253 // Skip bitcasting
9254 if (V->getOpcode() == ISD::BITCAST)
9255 V = V.getOperand(0);
9256
9257 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009258 SDLoc dl(N);
Michael Liaob4f98ea2013-03-25 23:47:35 +00009259 // Handle only simple case where vector being inserted and vector
9260 // being extracted are of same type, and are half size of larger vectors.
9261 EVT BigVT = V->getOperand(0).getValueType();
9262 EVT SmallVT = V->getOperand(1).getValueType();
9263 if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
9264 return SDValue();
9265
9266 // Only handle cases where both indexes are constants with the same type.
9267 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
9268 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
9269
9270 if (InsIdx && ExtIdx &&
9271 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
9272 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
9273 // Combine:
9274 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
9275 // Into:
9276 // indices are equal or bit offsets are equal => V1
9277 // otherwise => (extract_subvec V1, ExtIdx)
9278 if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() ==
9279 ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits())
9280 return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1));
9281 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT,
9282 DAG.getNode(ISD::BITCAST, dl,
9283 N->getOperand(0).getValueType(),
9284 V->getOperand(0)), N->getOperand(1));
9285 }
9286 }
9287
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009288 return SDValue();
9289}
9290
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009291// Tries to turn a shuffle of two CONCAT_VECTORS into a single concat.
9292static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) {
9293 EVT VT = N->getValueType(0);
9294 unsigned NumElts = VT.getVectorNumElements();
9295
9296 SDValue N0 = N->getOperand(0);
9297 SDValue N1 = N->getOperand(1);
9298 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9299
9300 SmallVector<SDValue, 4> Ops;
9301 EVT ConcatVT = N0.getOperand(0).getValueType();
9302 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements();
9303 unsigned NumConcats = NumElts / NumElemsPerConcat;
9304
9305 // Look at every vector that's inserted. We're looking for exact
9306 // subvector-sized copies from a concatenated vector
9307 for (unsigned I = 0; I != NumConcats; ++I) {
9308 // Make sure we're dealing with a copy.
9309 unsigned Begin = I * NumElemsPerConcat;
Hao Liu3778c042013-05-13 02:07:05 +00009310 bool AllUndef = true, NoUndef = true;
9311 for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) {
9312 if (SVN->getMaskElt(J) >= 0)
9313 AllUndef = false;
9314 else
9315 NoUndef = false;
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009316 }
9317
Hao Liu3778c042013-05-13 02:07:05 +00009318 if (NoUndef) {
Hao Liu3778c042013-05-13 02:07:05 +00009319 if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0)
9320 return SDValue();
9321
9322 for (unsigned J = 1; J != NumElemsPerConcat; ++J)
9323 if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J))
9324 return SDValue();
9325
9326 unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat;
9327 if (FirstElt < N0.getNumOperands())
9328 Ops.push_back(N0.getOperand(FirstElt));
9329 else
9330 Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands()));
9331
9332 } else if (AllUndef) {
9333 Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType()));
9334 } else { // Mixed with general masks and undefs, can't do optimization.
9335 return SDValue();
9336 }
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009337 }
9338
Andrew Trickac6d9be2013-05-25 02:42:55 +00009339 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops.data(),
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009340 Ops.size());
9341}
9342
Dan Gohman475871a2008-07-27 21:46:04 +00009343SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009344 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00009345 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009346
Mon P Wangaeb06d22008-11-10 04:46:22 +00009347 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +00009348 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +00009349
Craig Topperae1bec52012-04-09 05:16:56 +00009350 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wangaeb06d22008-11-10 04:46:22 +00009351
Craig Topper481b79c2012-01-04 08:07:43 +00009352 // Canonicalize shuffle undef, undef -> undef
9353 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
9354 return DAG.getUNDEF(VT);
9355
9356 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9357
9358 // Canonicalize shuffle v, v -> v, undef
9359 if (N0 == N1) {
9360 SmallVector<int, 8> NewMask;
9361 for (unsigned i = 0; i != NumElts; ++i) {
9362 int Idx = SVN->getMaskElt(i);
9363 if (Idx >= (int)NumElts) Idx -= NumElts;
9364 NewMask.push_back(Idx);
9365 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009366 return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +00009367 &NewMask[0]);
9368 }
9369
9370 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
9371 if (N0.getOpcode() == ISD::UNDEF) {
9372 SmallVector<int, 8> NewMask;
9373 for (unsigned i = 0; i != NumElts; ++i) {
9374 int Idx = SVN->getMaskElt(i);
Craig Topper4b206bd2012-04-09 05:55:33 +00009375 if (Idx >= 0) {
9376 if (Idx < (int)NumElts)
9377 Idx += NumElts;
9378 else
9379 Idx -= NumElts;
9380 }
9381 NewMask.push_back(Idx);
Craig Topper481b79c2012-01-04 08:07:43 +00009382 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009383 return DAG.getVectorShuffle(VT, SDLoc(N), N1, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +00009384 &NewMask[0]);
9385 }
9386
9387 // Remove references to rhs if it is undef
9388 if (N1.getOpcode() == ISD::UNDEF) {
9389 bool Changed = false;
9390 SmallVector<int, 8> NewMask;
9391 for (unsigned i = 0; i != NumElts; ++i) {
9392 int Idx = SVN->getMaskElt(i);
9393 if (Idx >= (int)NumElts) {
9394 Idx = -1;
9395 Changed = true;
9396 }
9397 NewMask.push_back(Idx);
9398 }
9399 if (Changed)
Andrew Trickac6d9be2013-05-25 02:42:55 +00009400 return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, &NewMask[0]);
Craig Topper481b79c2012-01-04 08:07:43 +00009401 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00009402
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009403 // If it is a splat, check if the argument vector is another splat or a
9404 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009405 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009406 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00009407
Dan Gohman7f321562007-06-25 16:23:39 +00009408 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00009409 // not the number of vector elements, look through it. Be careful not to
9410 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009411 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +00009412 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00009413 if (ConvInput.getValueType().isVector() &&
9414 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00009415 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00009416 }
9417
Dan Gohman7f321562007-06-25 16:23:39 +00009418 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009419 assert(V->getNumOperands() == NumElts &&
9420 "BUILD_VECTOR has wrong number of operands");
9421 SDValue Base;
9422 bool AllSame = true;
9423 for (unsigned i = 0; i != NumElts; ++i) {
9424 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
9425 Base = V->getOperand(i);
9426 break;
Evan Cheng917ec982006-07-21 08:25:53 +00009427 }
Evan Cheng917ec982006-07-21 08:25:53 +00009428 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009429 // Splat of <u, u, u, u>, return <u, u, u, u>
9430 if (!Base.getNode())
9431 return N0;
9432 for (unsigned i = 0; i != NumElts; ++i) {
9433 if (V->getOperand(i) != Base) {
9434 AllSame = false;
9435 break;
9436 }
9437 }
9438 // Splat of <x, x, x, x>, return <x, x, x, x>
9439 if (AllSame)
9440 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +00009441 }
9442 }
Nadav Rotem4ac90812012-04-01 19:31:22 +00009443
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009444 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
9445 Level < AfterLegalizeVectorOps &&
9446 (N1.getOpcode() == ISD::UNDEF ||
9447 (N1.getOpcode() == ISD::CONCAT_VECTORS &&
9448 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) {
9449 SDValue V = partitionShuffleOfConcats(N, DAG);
9450
9451 if (V.getNode())
9452 return V;
9453 }
9454
Nadav Rotem4ac90812012-04-01 19:31:22 +00009455 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009456 // and it reverses the swizzle of the previous shuffle then we can
9457 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotem4ac90812012-04-01 19:31:22 +00009458 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
9459 N1.getOpcode() == ISD::UNDEF) {
9460
Nadav Rotem4ac90812012-04-01 19:31:22 +00009461 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
9462
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009463 // Shuffle nodes can only reverse shuffles with a single non-undef value.
9464 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
9465 return SDValue();
9466
Craig Topperae1bec52012-04-09 05:16:56 +00009467 // The incoming shuffle must be of the same type as the result of the
9468 // current shuffle.
9469 assert(OtherSV->getOperand(0).getValueType() == VT &&
9470 "Shuffle types don't match");
Nadav Rotem4ac90812012-04-01 19:31:22 +00009471
9472 for (unsigned i = 0; i != NumElts; ++i) {
9473 int Idx = SVN->getMaskElt(i);
Craig Topperae1bec52012-04-09 05:16:56 +00009474 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotem4ac90812012-04-01 19:31:22 +00009475 // Next, this index comes from the first value, which is the incoming
9476 // shuffle. Adopt the incoming index.
9477 if (Idx >= 0)
9478 Idx = OtherSV->getMaskElt(Idx);
9479
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009480 // The combined shuffle must map each index to itself.
Craig Topperae1bec52012-04-09 05:16:56 +00009481 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009482 return SDValue();
Nadav Rotem4ac90812012-04-01 19:31:22 +00009483 }
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009484
9485 return OtherSV->getOperand(0);
Nadav Rotem4ac90812012-04-01 19:31:22 +00009486 }
9487
Dan Gohman475871a2008-07-27 21:46:04 +00009488 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009489}
9490
Evan Cheng44f1f092006-04-20 08:56:16 +00009491/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00009492/// an AND to a vector_shuffle with the destination vector and a zero vector.
9493/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00009494/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00009495SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009496 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009497 SDLoc dl(N);
Dan Gohman475871a2008-07-27 21:46:04 +00009498 SDValue LHS = N->getOperand(0);
9499 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00009500 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009501 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +00009502 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009503 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009504 SmallVector<int, 8> Indices;
9505 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00009506 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00009507 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00009508 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00009509 return SDValue();
Craig Topperb7135e52012-04-09 05:59:53 +00009510
9511 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00009512 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00009513 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00009514 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00009515 else
Dan Gohman475871a2008-07-27 21:46:04 +00009516 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009517 }
9518
9519 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00009520 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00009521 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00009522 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009523
Dan Gohman7f321562007-06-25 16:23:39 +00009524 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00009525 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00009526 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00009527 DAG.getConstant(0, EltVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009528 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Nate Begeman9008ca62009-04-27 18:41:29 +00009529 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009530 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +00009531 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009532 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00009533 }
9534 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009535
Dan Gohman475871a2008-07-27 21:46:04 +00009536 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009537}
9538
Dan Gohman7f321562007-06-25 16:23:39 +00009539/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00009540SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Bob Wilsond7273432010-12-17 23:06:49 +00009541 assert(N->getValueType(0).isVector() &&
9542 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00009543
Dan Gohman475871a2008-07-27 21:46:04 +00009544 SDValue LHS = N->getOperand(0);
9545 SDValue RHS = N->getOperand(1);
9546 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00009547 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00009548
Dan Gohman7f321562007-06-25 16:23:39 +00009549 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00009550 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00009551 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00009552 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00009553 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00009554 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00009555 SDValue LHSOp = LHS.getOperand(i);
9556 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00009557 // If these two elements can't be folded, bail out.
9558 if ((LHSOp.getOpcode() != ISD::UNDEF &&
9559 LHSOp.getOpcode() != ISD::Constant &&
9560 LHSOp.getOpcode() != ISD::ConstantFP) ||
9561 (RHSOp.getOpcode() != ISD::UNDEF &&
9562 RHSOp.getOpcode() != ISD::Constant &&
9563 RHSOp.getOpcode() != ISD::ConstantFP))
9564 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00009565
Evan Cheng7b336a82006-05-31 06:08:35 +00009566 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00009567 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
9568 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00009569 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00009570 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00009571 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00009572 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00009573 break;
9574 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009575
Bob Wilsond7273432010-12-17 23:06:49 +00009576 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009577 EVT RVT = RHSOp.getValueType();
9578 if (RVT != VT) {
9579 // Integer BUILD_VECTOR operands may have types larger than the element
9580 // size (e.g., when the element type is not legal). Prior to type
9581 // legalization, the types may not match between the two BUILD_VECTORS.
9582 // Truncate one of the operands to make them match.
9583 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009584 RHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, RHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009585 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009586 LHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), RVT, LHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009587 VT = RVT;
9588 }
9589 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009590 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(LHS), VT,
Evan Chenga0839882010-05-18 00:03:40 +00009591 LHSOp, RHSOp);
9592 if (FoldOp.getOpcode() != ISD::UNDEF &&
9593 FoldOp.getOpcode() != ISD::Constant &&
9594 FoldOp.getOpcode() != ISD::ConstantFP)
9595 break;
9596 Ops.push_back(FoldOp);
9597 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00009598 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009599
Bob Wilsond7273432010-12-17 23:06:49 +00009600 if (Ops.size() == LHS.getNumOperands())
Andrew Trickac6d9be2013-05-25 02:42:55 +00009601 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Bob Wilsond7273432010-12-17 23:06:49 +00009602 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +00009603 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009604
Dan Gohman475871a2008-07-27 21:46:04 +00009605 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00009606}
9607
Craig Topperdd201ff2012-09-11 01:45:21 +00009608/// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG.
9609SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) {
Craig Topperdd201ff2012-09-11 01:45:21 +00009610 assert(N->getValueType(0).isVector() &&
9611 "SimplifyVUnaryOp only works on vectors!");
9612
9613 SDValue N0 = N->getOperand(0);
9614
9615 if (N0.getOpcode() != ISD::BUILD_VECTOR)
9616 return SDValue();
9617
9618 // Operand is a BUILD_VECTOR node, see if we can constant fold it.
9619 SmallVector<SDValue, 8> Ops;
9620 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
9621 SDValue Op = N0.getOperand(i);
9622 if (Op.getOpcode() != ISD::UNDEF &&
9623 Op.getOpcode() != ISD::ConstantFP)
9624 break;
9625 EVT EltVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009626 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(N0), EltVT, Op);
Craig Topperdd201ff2012-09-11 01:45:21 +00009627 if (FoldOp.getOpcode() != ISD::UNDEF &&
9628 FoldOp.getOpcode() != ISD::ConstantFP)
9629 break;
9630 Ops.push_back(FoldOp);
9631 AddToWorkList(FoldOp.getNode());
9632 }
9633
9634 if (Ops.size() != N0.getNumOperands())
9635 return SDValue();
9636
Andrew Trickac6d9be2013-05-25 02:42:55 +00009637 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topperdd201ff2012-09-11 01:45:21 +00009638 N0.getValueType(), &Ops[0], Ops.size());
9639}
9640
Andrew Trickac6d9be2013-05-25 02:42:55 +00009641SDValue DAGCombiner::SimplifySelect(SDLoc DL, SDValue N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00009642 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00009643 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00009644
Bill Wendling836ca7d2009-01-30 23:59:18 +00009645 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00009646 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009647
Nate Begemanf845b452005-10-08 00:29:44 +00009648 // If we got a simplified select_cc node back from SimplifySelectCC, then
9649 // break it down into a new SETCC node, and a new SELECT node, and then return
9650 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00009651 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00009652 // Check to see if we got a select_cc back (to turn into setcc/select).
9653 // Otherwise, just return whatever node we got back, like fabs.
9654 if (SCC.getOpcode() == ISD::SELECT_CC) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009655 SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009656 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009657 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009658 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00009659 AddToWorkList(SETCC.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009660 return DAG.getSelect(SDLoc(SCC), SCC.getValueType(),
9661 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00009662 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009663
Nate Begemanf845b452005-10-08 00:29:44 +00009664 return SCC;
9665 }
Dan Gohman475871a2008-07-27 21:46:04 +00009666 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00009667}
9668
Chris Lattner40c62d52005-10-18 06:04:22 +00009669/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
9670/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00009671/// select. Callers of this should assume that TheSelect is deleted if this
9672/// returns true. As such, they should return the appropriate thing (e.g. the
9673/// node) back to the top-level of the DAG combiner loop to avoid it being
9674/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00009675bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00009676 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009677
Nadav Rotemf94fdb62011-02-11 19:57:47 +00009678 // Cannot simplify select with vector condition
9679 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
9680
Chris Lattner40c62d52005-10-18 06:04:22 +00009681 // If this is a select from two identical things, try to pull the operation
9682 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +00009683 if (LHS.getOpcode() != RHS.getOpcode() ||
9684 !LHS.hasOneUse() || !RHS.hasOneUse())
9685 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009686
Chris Lattner18061612010-09-21 15:46:59 +00009687 // If this is a load and the token chain is identical, replace the select
9688 // of two loads with a load through a select of the address to load from.
9689 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
9690 // constants have been dropped into the constant pool.
9691 if (LHS.getOpcode() == ISD::LOAD) {
9692 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
9693 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009694
Chris Lattner18061612010-09-21 15:46:59 +00009695 // Token chains must be identical.
9696 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009697 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +00009698 LLD->isVolatile() || RLD->isVolatile() ||
9699 // If this is an EXTLOAD, the VT's must match.
9700 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +00009701 // If this is an EXTLOAD, the kind of extension must match.
9702 (LLD->getExtensionType() != RLD->getExtensionType() &&
9703 // The only exception is if one of the extensions is anyext.
9704 LLD->getExtensionType() != ISD::EXTLOAD &&
9705 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +00009706 // FIXME: this discards src value information. This is
9707 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00009708 // both potential memory locations. Since we are discarding
9709 // src value info, don't do the transformation if the memory
9710 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +00009711 LLD->getPointerInfo().getAddrSpace() != 0 ||
Pete Cooperb0fde6d2013-02-12 03:14:50 +00009712 RLD->getPointerInfo().getAddrSpace() != 0 ||
9713 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(),
9714 LLD->getBasePtr().getValueType()))
Chris Lattner18061612010-09-21 15:46:59 +00009715 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009716
Chris Lattnerf1658062010-09-21 15:58:55 +00009717 // Check that the select condition doesn't reach either load. If so,
9718 // folding this will induce a cycle into the DAG. If not, this is safe to
9719 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +00009720 SDValue Addr;
9721 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +00009722 SDNode *CondNode = TheSelect->getOperand(0).getNode();
9723 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
9724 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
9725 return false;
Nadav Rotem1c5bf3f2012-10-18 18:06:48 +00009726 // The loads must not depend on one another.
9727 if (LLD->isPredecessorOf(RLD) ||
9728 RLD->isPredecessorOf(LLD))
9729 return false;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009730 Addr = DAG.getSelect(SDLoc(TheSelect),
9731 LLD->getBasePtr().getValueType(),
9732 TheSelect->getOperand(0), LLD->getBasePtr(),
9733 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +00009734 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +00009735 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
9736 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
9737
9738 if ((LLD->hasAnyUseOfValue(1) &&
9739 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner77d95212012-03-27 16:27:21 +00009740 (RLD->hasAnyUseOfValue(1) &&
9741 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnerf1658062010-09-21 15:58:55 +00009742 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009743
Andrew Trickac6d9be2013-05-25 02:42:55 +00009744 Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +00009745 LLD->getBasePtr().getValueType(),
9746 TheSelect->getOperand(0),
9747 TheSelect->getOperand(1),
9748 LLD->getBasePtr(), RLD->getBasePtr(),
9749 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +00009750 }
9751
Chris Lattnerf1658062010-09-21 15:58:55 +00009752 SDValue Load;
9753 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
9754 Load = DAG.getLoad(TheSelect->getValueType(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00009755 SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +00009756 // FIXME: Discards pointer info.
9757 LLD->getChain(), Addr, MachinePointerInfo(),
9758 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009759 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +00009760 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +00009761 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
9762 RLD->getExtensionType() : LLD->getExtensionType(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00009763 SDLoc(TheSelect),
Stuart Hastingsa9011292011-02-16 16:23:55 +00009764 TheSelect->getValueType(0),
Chris Lattnerf1658062010-09-21 15:58:55 +00009765 // FIXME: Discards pointer info.
9766 LLD->getChain(), Addr, MachinePointerInfo(),
9767 LLD->getMemoryVT(), LLD->isVolatile(),
9768 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +00009769 }
Chris Lattnerf1658062010-09-21 15:58:55 +00009770
9771 // Users of the select now use the result of the load.
9772 CombineTo(TheSelect, Load);
9773
9774 // Users of the old loads now use the new load's chain. We know the
9775 // old-load value is dead now.
9776 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
9777 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
9778 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +00009779 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009780
Chris Lattner40c62d52005-10-18 06:04:22 +00009781 return false;
9782}
9783
Chris Lattner600fec32009-03-11 05:08:08 +00009784/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
9785/// where 'cond' is the comparison specified by CC.
Andrew Trickac6d9be2013-05-25 02:42:55 +00009786SDValue DAGCombiner::SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00009787 SDValue N2, SDValue N3,
9788 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00009789 // (x ? y : y) -> y.
9790 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009791
Owen Andersone50ed302009-08-10 22:56:29 +00009792 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00009793 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
9794 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
9795 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009796
9797 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00009798 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00009799 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00009800 if (SCC.getNode()) AddToWorkList(SCC.getNode());
9801 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009802
9803 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00009804 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00009805 return N2;
9806 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00009807 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00009808 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00009809
Nate Begemanf845b452005-10-08 00:29:44 +00009810 // Check to see if we can simplify the select into an fabs node
9811 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
9812 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00009813 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00009814 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
9815 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
9816 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
9817 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00009818 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009819
Nate Begemanf845b452005-10-08 00:29:44 +00009820 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
9821 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
9822 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
9823 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00009824 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00009825 }
9826 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009827
Chris Lattner600fec32009-03-11 05:08:08 +00009828 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
9829 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
9830 // in it. This is a win when the constant is not otherwise available because
9831 // it replaces two constant pool loads with one. We only do this if the FP
9832 // type is known to be legal, because if it isn't, then we are before legalize
9833 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00009834 // messing with soft float) and if the ConstantFP is not legal, because if
9835 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00009836 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
9837 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
9838 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00009839 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
9840 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00009841 // If both constants have multiple uses, then we won't need to do an
9842 // extra load, they are likely around in registers for other users.
9843 (TV->hasOneUse() || FV->hasOneUse())) {
9844 Constant *Elts[] = {
9845 const_cast<ConstantFP*>(FV->getConstantFPValue()),
9846 const_cast<ConstantFP*>(TV->getConstantFPValue())
9847 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009848 Type *FPTy = Elts[0]->getType();
Micah Villmow3574eca2012-10-08 16:38:25 +00009849 const DataLayout &TD = *TLI.getDataLayout();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009850
Chris Lattner600fec32009-03-11 05:08:08 +00009851 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +00009852 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +00009853 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
9854 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00009855 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00009856
9857 // Get the offsets to the 0 and 1 element of the array so that we can
9858 // select between them.
9859 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00009860 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00009861 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009862
Chris Lattner600fec32009-03-11 05:08:08 +00009863 SDValue Cond = DAG.getSetCC(DL,
Matt Arsenault225ed702013-05-18 00:21:46 +00009864 getSetCCResultType(N0.getValueType()),
Chris Lattner600fec32009-03-11 05:08:08 +00009865 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +00009866 AddToWorkList(Cond.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009867 SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(),
9868 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +00009869 AddToWorkList(CstOffset.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00009870 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
9871 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +00009872 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00009873 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00009874 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009875 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00009876
9877 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009878 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009879
Nate Begemanf845b452005-10-08 00:29:44 +00009880 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00009881 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00009882 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00009883 (N1C->isNullValue() || // (a < 0) ? b : 0
9884 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00009885 EVT XType = N0.getValueType();
9886 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00009887 if (XType.bitsGE(AType)) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00009888 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00009889 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00009890 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
9891 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00009892 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +00009893 SDValue ShCt = DAG.getConstant(ShCtV,
9894 getShiftAmountTy(N0.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009895 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009896 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00009897 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009898
Duncan Sands8e4eb092008-06-08 20:54:56 +00009899 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009900 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009901 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009902 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009903
9904 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009905 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009906
Andrew Trickac6d9be2013-05-25 02:42:55 +00009907 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009908 XType, N0,
9909 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009910 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00009911 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009912
Duncan Sands8e4eb092008-06-08 20:54:56 +00009913 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009914 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009915 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009916 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009917
9918 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009919 }
9920 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009921
Owen Andersoned1088a2010-09-22 22:58:22 +00009922 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
9923 // where y is has a single bit set.
9924 // A plaintext description would be, we can turn the SELECT_CC into an AND
9925 // when the condition can be materialized as an all-ones register. Any
9926 // single bit-test can be materialized as an all-ones register with
9927 // shift-left and shift-right-arith.
9928 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
9929 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009930 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +00009931 N2C && N2C->isNullValue()) {
9932 SDValue AndLHS = N0->getOperand(0);
9933 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
9934 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
9935 // Shift the tested bit over the sign bit.
9936 APInt AndMask = ConstAndRHS->getAPIntValue();
9937 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00009938 DAG.getConstant(AndMask.countLeadingZeros(),
9939 getShiftAmountTy(AndLHS.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009940 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009941
Owen Andersoned1088a2010-09-22 22:58:22 +00009942 // Now arithmetic right shift it all the way over, so the result is either
9943 // all-ones, or zero.
9944 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00009945 DAG.getConstant(AndMask.getBitWidth()-1,
9946 getShiftAmountTy(Shl.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009947 SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009948
Owen Andersoned1088a2010-09-22 22:58:22 +00009949 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
9950 }
9951 }
9952
Nate Begeman07ed4172005-10-10 21:26:48 +00009953 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00009954 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00009955 TLI.getBooleanContents(N0.getValueType().isVector()) ==
9956 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009957
Chris Lattner1eba01e2007-04-11 06:50:51 +00009958 // If the caller doesn't want us to simplify this into a zext of a compare,
9959 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00009960 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00009961 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009962
Nate Begeman07ed4172005-10-10 21:26:48 +00009963 // Get a SetCC of the condition
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009964 // NOTE: Don't create a SETCC if it's not legal on this target.
9965 if (!LegalOperations ||
9966 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00009967 LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) {
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009968 SDValue Temp, SCC;
9969 // cast from setcc result type to select result type
9970 if (LegalTypes) {
Matt Arsenault225ed702013-05-18 00:21:46 +00009971 SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()),
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009972 N0, N1, CC);
9973 if (N2.getValueType().bitsLT(SCC.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00009974 Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009975 N2.getValueType());
9976 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00009977 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009978 N2.getValueType(), SCC);
9979 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009980 SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC);
9981 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009982 N2.getValueType(), SCC);
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009983 }
9984
9985 AddToWorkList(SCC.getNode());
9986 AddToWorkList(Temp.getNode());
9987
9988 if (N2C->getAPIntValue() == 1)
9989 return Temp;
9990
9991 // shl setcc result by log2 n2c
9992 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
9993 DAG.getConstant(N2C->getAPIntValue().logBase2(),
9994 getShiftAmountTy(Temp.getValueType())));
Nate Begemanb0d04a72006-02-18 02:40:58 +00009995 }
Nate Begeman07ed4172005-10-10 21:26:48 +00009996 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009997
Nate Begemanf845b452005-10-08 00:29:44 +00009998 // Check to see if this is the equivalent of setcc
9999 // FIXME: Turn all of these into setcc if setcc if setcc is legal
10000 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +000010001 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +000010002 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +000010003 if (!LegalOperations ||
Matt Arsenault225ed702013-05-18 00:21:46 +000010004 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) {
10005 SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +000010006 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +000010007 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +000010008 return Res;
10009 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010010
Bill Wendling836ca7d2009-01-30 23:59:18 +000010011 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +000010012 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +000010013 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +000010014 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010015 SDValue Ctlz = DAG.getNode(ISD::CTLZ, SDLoc(N0), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010016 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +000010017 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +000010018 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +000010019 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010020 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +000010021 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010022 SDValue NegN0 = DAG.getNode(ISD::SUB, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010023 XType, DAG.getConstant(0, XType), N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000010024 SDValue NotN0 = DAG.getNOT(SDLoc(N0), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +000010025 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +000010026 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +000010027 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010028 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +000010029 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010030 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +000010031 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010032 SDValue Sign = DAG.getNode(ISD::SRL, SDLoc(N0), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +000010033 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010034 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +000010035 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +000010036 }
10037 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010038
Benjamin Kramercde51102010-07-08 12:09:56 +000010039 // Check to see if this is an integer abs.
10040 // select_cc setg[te] X, 0, X, -X ->
10041 // select_cc setgt X, -1, X, -X ->
10042 // select_cc setl[te] X, 0, -X, X ->
10043 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +000010044 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +000010045 if (N1C) {
10046 ConstantSDNode *SubC = NULL;
10047 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
10048 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
10049 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
10050 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
10051 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
10052 (N1C->isOne() && CC == ISD::SETLT)) &&
10053 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
10054 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
10055
Owen Andersone50ed302009-08-10 22:56:29 +000010056 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +000010057 if (SubC && SubC->isNullValue() && XType.isInteger()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010058 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), XType,
Benjamin Kramercde51102010-07-08 12:09:56 +000010059 N0,
10060 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010061 getShiftAmountTy(N0.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010062 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0),
Benjamin Kramercde51102010-07-08 12:09:56 +000010063 XType, N0, Shift);
10064 AddToWorkList(Shift.getNode());
10065 AddToWorkList(Add.getNode());
10066 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +000010067 }
10068 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010069
Dan Gohman475871a2008-07-27 21:46:04 +000010070 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +000010071}
10072
Evan Chengfa1eb272007-02-08 22:13:59 +000010073/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +000010074SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +000010075 SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +000010076 SDLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010077 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +000010078 DagCombineInfo(DAG, Level, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +000010079 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7be2005-09-16 00:54:12 +000010080}
10081
Nate Begeman69575232005-10-20 02:15:44 +000010082/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
10083/// return a DAG expression to select that will generate the same value by
10084/// multiplying by a magic number. See:
10085/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010086SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010087 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010088 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010089
Andrew Lenharth232c9102006-06-12 16:07:18 +000010090 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010091 ii != ee; ++ii)
10092 AddToWorkList(*ii);
10093 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010094}
10095
10096/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
10097/// return a DAG expression to select that will generate the same value by
10098/// multiplying by a magic number. See:
10099/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010100SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010101 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010102 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +000010103
Andrew Lenharth232c9102006-06-12 16:07:18 +000010104 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010105 ii != ee; ++ii)
10106 AddToWorkList(*ii);
10107 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010108}
10109
Nate Begemancc66cdd2009-09-25 06:05:26 +000010110/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +000010111// to alias with anything but itself. Provides base object and offset as
10112// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010113static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky2943e372012-09-05 22:15:49 +000010114 const GlobalValue *&GV, const void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +000010115 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010116 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010117
Jim Laskey71382342006-10-07 23:37:56 +000010118 // If it's an adding a simple constant then integrate the offset.
10119 if (Base.getOpcode() == ISD::ADD) {
10120 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
10121 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010122 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +000010123 }
10124 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010125
Nate Begemancc66cdd2009-09-25 06:05:26 +000010126 // Return the underlying GlobalValue, and update the Offset. Return false
10127 // for GlobalAddressSDNode since the same GlobalAddress may be represented
10128 // by multiple nodes with different offsets.
10129 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
10130 GV = G->getGlobal();
10131 Offset += G->getOffset();
10132 return false;
10133 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010134
Nate Begemancc66cdd2009-09-25 06:05:26 +000010135 // Return the underlying Constant value, and update the Offset. Return false
10136 // for ConstantSDNodes since the same constant pool entry may be represented
10137 // by multiple nodes with different offsets.
10138 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky2943e372012-09-05 22:15:49 +000010139 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
10140 : (const void *)C->getConstVal();
Nate Begemancc66cdd2009-09-25 06:05:26 +000010141 Offset += C->getOffset();
10142 return false;
10143 }
Jim Laskey71382342006-10-07 23:37:56 +000010144 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010145 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +000010146}
10147
10148/// isAlias - Return true if there is any possibility that the two addresses
10149/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +000010150bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +000010151 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010152 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010153 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +000010154 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010155 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010156 unsigned SrcValueAlign2,
10157 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +000010158 // If they are the same then they must be aliases.
10159 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +000010160
Jim Laskey71382342006-10-07 23:37:56 +000010161 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +000010162 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +000010163 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +000010164 const GlobalValue *GV1, *GV2;
Roman Divacky2943e372012-09-05 22:15:49 +000010165 const void *CV1, *CV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +000010166 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
10167 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010168
Nate Begemancc66cdd2009-09-25 06:05:26 +000010169 // If they have a same base address then check to see if they overlap.
10170 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +000010171 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010172
Owen Anderson4a9f1502010-09-20 20:39:59 +000010173 // It is possible for different frame indices to alias each other, mostly
10174 // when tail call optimization reuses return address slots for arguments.
10175 // To catch this case, look up the actual index of frame indices to compute
10176 // the real alias relationship.
10177 if (isFrameIndex1 && isFrameIndex2) {
10178 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10179 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
10180 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
10181 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
10182 }
10183
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010184 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +000010185 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010186 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
10187 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +000010188
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010189 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
10190 // compared to the size and offset of the access, we may be able to prove they
10191 // do not alias. This check is conservative for now to catch cases created by
10192 // splitting vector types.
10193 if ((SrcValueAlign1 == SrcValueAlign2) &&
10194 (SrcValueOffset1 != SrcValueOffset2) &&
10195 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
10196 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
10197 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010198
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010199 // There is no overlap between these relatively aligned accesses of similar
10200 // size, return no alias.
10201 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
10202 return false;
10203 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010204
Jim Laskey07a27092006-10-18 19:08:31 +000010205 if (CombinerGlobalAA) {
10206 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +000010207 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
10208 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
10209 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +000010210 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010211 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
10212 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +000010213 if (AAResult == AliasAnalysis::NoAlias)
10214 return false;
10215 }
Jim Laskey096c22e2006-10-18 12:29:57 +000010216
10217 // Otherwise we have to assume they alias.
10218 return true;
Jim Laskey71382342006-10-07 23:37:56 +000010219}
10220
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010221bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) {
10222 SDValue Ptr0, Ptr1;
10223 int64_t Size0, Size1;
10224 const Value *SrcValue0, *SrcValue1;
10225 int SrcValueOffset0, SrcValueOffset1;
10226 unsigned SrcValueAlign0, SrcValueAlign1;
10227 const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1;
10228 FindAliasInfo(Op0, Ptr0, Size0, SrcValue0, SrcValueOffset0,
10229 SrcValueAlign0, SrcTBAAInfo0);
10230 FindAliasInfo(Op1, Ptr1, Size1, SrcValue1, SrcValueOffset1,
10231 SrcValueAlign1, SrcTBAAInfo1);
10232 return isAlias(Ptr0, Size0, SrcValue0, SrcValueOffset0,
Nadav Rotemdde785c2012-12-06 17:34:13 +000010233 SrcValueAlign0, SrcTBAAInfo0,
10234 Ptr1, Size1, SrcValue1, SrcValueOffset1,
10235 SrcValueAlign1, SrcTBAAInfo1);
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010236}
10237
Jim Laskey71382342006-10-07 23:37:56 +000010238/// FindAliasInfo - Extracts the relevant alias information from the memory
10239/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +000010240bool DAGCombiner::FindAliasInfo(SDNode *N,
Benjamin Kramerae4746b2012-01-15 11:50:43 +000010241 SDValue &Ptr, int64_t &Size,
10242 const Value *&SrcValue,
10243 int &SrcValueOffset,
10244 unsigned &SrcValueAlign,
10245 const MDNode *&TBAAInfo) const {
10246 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
10247
10248 Ptr = LS->getBasePtr();
10249 Size = LS->getMemoryVT().getSizeInBits() >> 3;
10250 SrcValue = LS->getSrcValue();
10251 SrcValueOffset = LS->getSrcValueOffset();
10252 SrcValueAlign = LS->getOriginalAlignment();
10253 TBAAInfo = LS->getTBAAInfo();
10254 return isa<LoadSDNode>(LS);
Jim Laskey71382342006-10-07 23:37:56 +000010255}
10256
Jim Laskey6ff23e52006-10-04 16:53:27 +000010257/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
10258/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +000010259void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
10260 SmallVector<SDValue, 8> &Aliases) {
10261 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010262 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +000010263
Jim Laskey279f0532006-09-25 16:29:54 +000010264 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +000010265 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010266 int64_t Size;
10267 const Value *SrcValue;
10268 int SrcValueOffset;
10269 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010270 const MDNode *SrcTBAAInfo;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010271 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010272 SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +000010273
Jim Laskey6ff23e52006-10-04 16:53:27 +000010274 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +000010275 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +000010276 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010277
Jim Laskeybc588b82006-10-05 15:07:25 +000010278 // Look at each chain and determine if it is an alias. If so, add it to the
10279 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +000010280 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +000010281 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010282 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +000010283 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010284
10285 // For TokenFactor nodes, look at each operand and only continue up the
10286 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +000010287 // find more and revert to original chain since the xform is unlikely to be
10288 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010289 //
10290 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +000010291 // chain we found before we hit a tokenfactor rather than the original
10292 // chain.
10293 if (Depth > 6 || Aliases.size() == 2) {
10294 Aliases.clear();
10295 Aliases.push_back(OriginalChain);
10296 break;
10297 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010298
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010299 // Don't bother if we've been before.
10300 if (!Visited.insert(Chain.getNode()))
10301 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +000010302
Jim Laskeybc588b82006-10-05 15:07:25 +000010303 switch (Chain.getOpcode()) {
10304 case ISD::EntryToken:
10305 // Entry token is ideal chain operand, but handled in FindBetterChain.
10306 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010307
Jim Laskeybc588b82006-10-05 15:07:25 +000010308 case ISD::LOAD:
10309 case ISD::STORE: {
10310 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +000010311 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010312 int64_t OpSize;
10313 const Value *OpSrcValue;
10314 int OpSrcValueOffset;
10315 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010316 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +000010317 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010318 OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010319 OpSrcValueAlign,
10320 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +000010321
Jim Laskeybc588b82006-10-05 15:07:25 +000010322 // If chain is alias then stop here.
10323 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010324 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010325 SrcTBAAInfo,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010326 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010327 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +000010328 Aliases.push_back(Chain);
10329 } else {
10330 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +000010331 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +000010332 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +000010333 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010334 break;
10335 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010336
Jim Laskeybc588b82006-10-05 15:07:25 +000010337 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010338 // We have to check each of the operands of the token factor for "small"
10339 // token factors, so we queue them up. Adding the operands to the queue
10340 // (stack) in reverse order maintains the original order and increases the
10341 // likelihood that getNode will find a matching token factor (CSE.)
10342 if (Chain.getNumOperands() > 16) {
10343 Aliases.push_back(Chain);
10344 break;
10345 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010346 for (unsigned n = Chain.getNumOperands(); n;)
10347 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +000010348 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +000010349 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010350
Jim Laskeybc588b82006-10-05 15:07:25 +000010351 default:
10352 // For all other instructions we will just have to take what we can get.
10353 Aliases.push_back(Chain);
10354 break;
Jim Laskey279f0532006-09-25 16:29:54 +000010355 }
10356 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000010357}
10358
10359/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
10360/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +000010361SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
10362 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +000010363
Jim Laskey6ff23e52006-10-04 16:53:27 +000010364 // Accumulate all the aliases to this node.
10365 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +000010366
Dan Gohman71dc7c92011-05-17 22:20:36 +000010367 // If no operands then chain to entry token.
10368 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +000010369 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +000010370
10371 // If a single operand then chain to it. We don't need to revisit it.
10372 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +000010373 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010374
Jim Laskey6ff23e52006-10-04 16:53:27 +000010375 // Construct a custom tailored token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +000010376 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010377 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +000010378}
10379
Nate Begeman1d4d4142005-09-01 00:19:25 +000010380// SelectionDAG::Combine - This is the entry point for the file.
10381//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000010382void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +000010383 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +000010384 /// run - This is the main entry point to this class.
10385 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000010386 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +000010387}