blob: 2cfde18e103dea9f2dbd502c6021576c7bed3f60 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
Scott Michelfdc40a02009-02-17 22:15:04 +000012//
Dan Gohman41287002009-04-25 17:09:45 +000013// This pass is not a substitute for the LLVM IR instcombine pass. This pass is
14// primarily intended to handle simplification opportunities that are implicit
15// in the LLVM IR and exposed by the various codegen lowering phases.
16//
Nate Begeman1d4d4142005-09-01 00:19:25 +000017//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000020#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner600fec32009-03-11 05:08:08 +000021#include "llvm/DerivedTypes.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000022#include "llvm/LLVMContext.h"
Chris Lattner00161a62008-01-25 07:20:16 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000026#include "llvm/Target/TargetData.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000027#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000028#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000029#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000030#include "llvm/ADT/SmallPtrSet.h"
31#include "llvm/ADT/Statistic.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000032#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000033#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000035#include "llvm/Support/MathExtras.h"
Chris Lattnerbbbfa992009-08-23 06:35:02 +000036#include "llvm/Support/raw_ostream.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000037#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000038using namespace llvm;
39
Chris Lattnercd3245a2006-12-19 22:41:21 +000040STATISTIC(NodesCombined , "Number of dag nodes combined");
41STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
42STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng8b944d32009-05-28 00:35:15 +000043STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Evan Cheng31959b12011-02-02 01:06:55 +000044STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int");
Chris Lattnercd3245a2006-12-19 22:41:21 +000045
Nate Begeman1d4d4142005-09-01 00:19:25 +000046namespace {
Jim Laskey71382342006-10-07 23:37:56 +000047 static cl::opt<bool>
Owen Anderson0dcc8142010-09-19 21:01:26 +000048 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000049 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000050
Jim Laskey07a27092006-10-18 19:08:31 +000051 static cl::opt<bool>
52 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
53 cl::desc("Include global information in alias analysis"));
54
Jim Laskeybc588b82006-10-05 15:07:25 +000055//------------------------------ DAGCombiner ---------------------------------//
56
Nick Lewycky6726b6d2009-10-25 06:33:48 +000057 class DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000058 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000059 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000060 CombineLevel Level;
Bill Wendling98a366d2009-04-29 23:29:43 +000061 CodeGenOpt::Level OptLevel;
Duncan Sands25cf2272008-11-24 14:53:14 +000062 bool LegalOperations;
63 bool LegalTypes;
Nate Begeman1d4d4142005-09-01 00:19:25 +000064
65 // Worklist of all of the nodes that need to be simplified.
James Molloy6660c052012-02-16 09:17:04 +000066 //
67 // This has the semantics that when adding to the worklist,
68 // the item added must be next to be processed. It should
69 // also only appear once. The naive approach to this takes
70 // linear time.
71 //
72 // To reduce the insert/remove time to logarithmic, we use
73 // a set and a vector to maintain our worklist.
74 //
75 // The set contains the items on the worklist, but does not
76 // maintain the order they should be visited.
77 //
78 // The vector maintains the order nodes should be visited, but may
79 // contain duplicate or removed nodes. When choosing a node to
80 // visit, we pop off the order stack until we find an item that is
81 // also in the contents set. All operations are O(log N).
82 SmallPtrSet<SDNode*, 64> WorkListContents;
Benjamin Kramerd5f76902012-03-10 00:23:58 +000083 SmallVector<SDNode*, 64> WorkListOrder;
Nate Begeman1d4d4142005-09-01 00:19:25 +000084
Jim Laskeyc7c3f112006-10-16 20:52:31 +000085 // AA - Used for DAG load/store alias analysis.
86 AliasAnalysis &AA;
87
Nate Begeman1d4d4142005-09-01 00:19:25 +000088 /// AddUsersToWorkList - When an instruction is simplified, add all users of
89 /// the instruction to the work lists because they might get more simplified
90 /// now.
91 ///
92 void AddUsersToWorkList(SDNode *N) {
93 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000094 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000095 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000096 }
97
Dan Gohman389079b2007-10-08 17:57:15 +000098 /// visit - call the node-specific routine that knows how to fold each
99 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +0000100 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000101
Chris Lattner24664722006-03-01 04:53:38 +0000102 public:
James Molloy6afa3f72012-02-16 09:48:07 +0000103 /// AddToWorkList - Add to the work list making sure its instance is at the
James Molloy6660c052012-02-16 09:17:04 +0000104 /// back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000105 void AddToWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000106 WorkListContents.insert(N);
107 WorkListOrder.push_back(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000108 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000109
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000110 /// removeFromWorkList - remove all instances of N from the worklist.
111 ///
112 void removeFromWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000113 WorkListContents.erase(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000114 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000115
Dan Gohman475871a2008-07-27 21:46:04 +0000116 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000117 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000118
Dan Gohman475871a2008-07-27 21:46:04 +0000119 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000120 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000121 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000122
Dan Gohman475871a2008-07-27 21:46:04 +0000123 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000124 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000125 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000126 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000127 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000128
129 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000130
131 private:
132
Chris Lattner012f2412006-02-17 21:58:01 +0000133 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000134 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000135 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000136 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman87862e72009-12-11 21:31:27 +0000137 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
138 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000139 return SimplifyDemandedBits(Op, Demanded);
140 }
141
Dan Gohman475871a2008-07-27 21:46:04 +0000142 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000143
Chris Lattner448f2192006-11-11 00:39:41 +0000144 bool CombineToPreIndexedLoadStore(SDNode *N);
145 bool CombineToPostIndexedLoadStore(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000146
Evan Cheng95c57ea2010-04-24 04:43:44 +0000147 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
148 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
149 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
150 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000151 SDValue PromoteIntBinOp(SDValue Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000152 SDValue PromoteIntShiftOp(SDValue Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000153 SDValue PromoteExtend(SDValue Op);
154 bool PromoteLoad(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000155
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +0000156 void ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
157 SDValue Trunc, SDValue ExtLoad, DebugLoc DL,
158 ISD::NodeType ExtType);
159
Dan Gohman389079b2007-10-08 17:57:15 +0000160 /// combine - call the node-specific routine that knows how to fold each
161 /// particular type of node. If that doesn't do anything, try the
162 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000163 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000164
165 // Visitation implementation - Implement dag node combining for different
166 // node types. The semantics are as follows:
167 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000168 // SDValue.getNode() == 0 - No change was made
169 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
170 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000171 //
Dan Gohman475871a2008-07-27 21:46:04 +0000172 SDValue visitTokenFactor(SDNode *N);
173 SDValue visitMERGE_VALUES(SDNode *N);
174 SDValue visitADD(SDNode *N);
175 SDValue visitSUB(SDNode *N);
176 SDValue visitADDC(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000177 SDValue visitSUBC(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000178 SDValue visitADDE(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000179 SDValue visitSUBE(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000180 SDValue visitMUL(SDNode *N);
181 SDValue visitSDIV(SDNode *N);
182 SDValue visitUDIV(SDNode *N);
183 SDValue visitSREM(SDNode *N);
184 SDValue visitUREM(SDNode *N);
185 SDValue visitMULHU(SDNode *N);
186 SDValue visitMULHS(SDNode *N);
187 SDValue visitSMUL_LOHI(SDNode *N);
188 SDValue visitUMUL_LOHI(SDNode *N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +0000189 SDValue visitSMULO(SDNode *N);
190 SDValue visitUMULO(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000191 SDValue visitSDIVREM(SDNode *N);
192 SDValue visitUDIVREM(SDNode *N);
193 SDValue visitAND(SDNode *N);
194 SDValue visitOR(SDNode *N);
195 SDValue visitXOR(SDNode *N);
196 SDValue SimplifyVBinOp(SDNode *N);
Craig Topperdd201ff2012-09-11 01:45:21 +0000197 SDValue SimplifyVUnaryOp(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000198 SDValue visitSHL(SDNode *N);
199 SDValue visitSRA(SDNode *N);
200 SDValue visitSRL(SDNode *N);
201 SDValue visitCTLZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000202 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000203 SDValue visitCTTZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000204 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000205 SDValue visitCTPOP(SDNode *N);
206 SDValue visitSELECT(SDNode *N);
207 SDValue visitSELECT_CC(SDNode *N);
208 SDValue visitSETCC(SDNode *N);
209 SDValue visitSIGN_EXTEND(SDNode *N);
210 SDValue visitZERO_EXTEND(SDNode *N);
211 SDValue visitANY_EXTEND(SDNode *N);
212 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
213 SDValue visitTRUNCATE(SDNode *N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000214 SDValue visitBITCAST(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000215 SDValue visitBUILD_PAIR(SDNode *N);
216 SDValue visitFADD(SDNode *N);
217 SDValue visitFSUB(SDNode *N);
218 SDValue visitFMUL(SDNode *N);
Owen Anderson062c0a52012-05-02 22:17:40 +0000219 SDValue visitFMA(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000220 SDValue visitFDIV(SDNode *N);
221 SDValue visitFREM(SDNode *N);
222 SDValue visitFCOPYSIGN(SDNode *N);
223 SDValue visitSINT_TO_FP(SDNode *N);
224 SDValue visitUINT_TO_FP(SDNode *N);
225 SDValue visitFP_TO_SINT(SDNode *N);
226 SDValue visitFP_TO_UINT(SDNode *N);
227 SDValue visitFP_ROUND(SDNode *N);
228 SDValue visitFP_ROUND_INREG(SDNode *N);
229 SDValue visitFP_EXTEND(SDNode *N);
230 SDValue visitFNEG(SDNode *N);
231 SDValue visitFABS(SDNode *N);
Owen Anderson7c626d32012-08-13 23:32:49 +0000232 SDValue visitFCEIL(SDNode *N);
233 SDValue visitFTRUNC(SDNode *N);
234 SDValue visitFFLOOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000235 SDValue visitBRCOND(SDNode *N);
236 SDValue visitBR_CC(SDNode *N);
237 SDValue visitLOAD(SDNode *N);
238 SDValue visitSTORE(SDNode *N);
239 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
240 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
241 SDValue visitBUILD_VECTOR(SDNode *N);
242 SDValue visitCONCAT_VECTORS(SDNode *N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +0000243 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000244 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Jim Grosbach9a526492010-06-23 16:07:42 +0000245 SDValue visitMEMBARRIER(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000246
Dan Gohman475871a2008-07-27 21:46:04 +0000247 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendling35247c32009-01-30 00:45:56 +0000248 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000249
Dan Gohman475871a2008-07-27 21:46:04 +0000250 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000251
Dan Gohman475871a2008-07-27 21:46:04 +0000252 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
253 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Bill Wendling836ca7d2009-01-30 23:59:18 +0000254 SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000255 SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2,
256 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000257 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000258 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000259 DebugLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000260 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000261 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000262 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000263 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000264 SDValue BuildSDIV(SDNode *N);
265 SDValue BuildUDIV(SDNode *N);
Evan Cheng9568e5c2011-06-21 06:01:08 +0000266 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
267 bool DemandHighBits = true);
268 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
Bill Wendling317bd702009-01-30 21:14:50 +0000269 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000270 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000271 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Cheng31959b12011-02-02 01:06:55 +0000272 SDValue TransformFPLoadStorePair(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000273
Dan Gohman475871a2008-07-27 21:46:04 +0000274 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000275
Jim Laskey6ff23e52006-10-04 16:53:27 +0000276 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
277 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000278 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
279 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000280
Jim Laskey096c22e2006-10-18 12:29:57 +0000281 /// isAlias - Return true if there is any possibility that the two addresses
282 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000283 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000284 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000285 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000286 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +0000287 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000288 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000289 unsigned SrcValueAlign2,
290 const MDNode *TBAAInfo2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000291
Jim Laskey7ca56af2006-10-11 13:47:09 +0000292 /// FindAliasInfo - Extracts the relevant alias information from the memory
293 /// node. Returns true if the operand was a load.
294 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000295 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000296 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000297 unsigned &SrcValueAlignment,
298 const MDNode *&TBAAInfo) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000299
Jim Laskey279f0532006-09-25 16:29:54 +0000300 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000301 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000302 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000303
Nadav Rotem72f7b082012-09-29 06:33:25 +0000304 /// Merge consecutive store operations into a wide store.
305 /// \return True if some memory operations were changed.
306 bool MergeConsecutiveStores(StoreSDNode *N);
307
Chris Lattner2392ae72010-04-15 04:48:01 +0000308 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000309 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Eli Friedman50185242011-11-12 00:35:34 +0000310 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
Chris Lattner2392ae72010-04-15 04:48:01 +0000311 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000312
Nate Begeman1d4d4142005-09-01 00:19:25 +0000313 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000314 void Run(CombineLevel AtLevel);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000315
Chris Lattner2392ae72010-04-15 04:48:01 +0000316 SelectionDAG &getDAG() const { return DAG; }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000317
Chris Lattner2392ae72010-04-15 04:48:01 +0000318 /// getShiftAmountTy - Returns a type large enough to hold any valid
319 /// shift amount - before type legalization these can be huge.
Owen Anderson95771af2011-02-25 21:41:48 +0000320 EVT getShiftAmountTy(EVT LHSTy) {
321 return LegalTypes ? TLI.getShiftAmountTy(LHSTy) : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000322 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000323
Chris Lattner2392ae72010-04-15 04:48:01 +0000324 /// isTypeLegal - This method returns true if we are running before type
325 /// legalization or if the specified VT is legal.
326 bool isTypeLegal(const EVT &VT) {
327 if (!LegalTypes) return true;
328 return TLI.isTypeLegal(VT);
329 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000330 };
331}
332
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000333
334namespace {
335/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
336/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000337class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000338 DAGCombiner &DC;
339public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000340 explicit WorkListRemover(DAGCombiner &dc)
341 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000342
Duncan Sandsedfcf592008-06-11 11:42:12 +0000343 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000344 DC.removeFromWorkList(N);
345 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000346};
347}
348
Chris Lattner24664722006-03-01 04:53:38 +0000349//===----------------------------------------------------------------------===//
350// TargetLowering::DAGCombinerInfo implementation
351//===----------------------------------------------------------------------===//
352
353void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
354 ((DAGCombiner*)DC)->AddToWorkList(N);
355}
356
Cameron Zwariched3caf92011-04-02 02:40:26 +0000357void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
358 ((DAGCombiner*)DC)->removeFromWorkList(N);
359}
360
Dan Gohman475871a2008-07-27 21:46:04 +0000361SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000362CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
363 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000364}
365
Dan Gohman475871a2008-07-27 21:46:04 +0000366SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000367CombineTo(SDNode *N, SDValue Res, bool AddTo) {
368 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000369}
370
371
Dan Gohman475871a2008-07-27 21:46:04 +0000372SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000373CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
374 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000375}
376
Dan Gohmane5af2d32009-01-29 01:59:02 +0000377void TargetLowering::DAGCombinerInfo::
378CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
379 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
380}
Chris Lattner24664722006-03-01 04:53:38 +0000381
Chris Lattner24664722006-03-01 04:53:38 +0000382//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000383// Helper Functions
384//===----------------------------------------------------------------------===//
385
386/// isNegatibleForFree - Return 1 if we can compute the negated form of the
387/// specified expression for the same cost as the expression itself, or 2 if we
388/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000389static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000390 const TargetLowering &TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000391 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000392 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000393 // No compile time optimizations on this type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 if (Op.getValueType() == MVT::ppcf128)
Dale Johannesendb44bf82007-10-16 23:38:29 +0000395 return 0;
396
Chris Lattner29446522007-05-14 22:04:50 +0000397 // fneg is removable even if it has multiple uses.
398 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000399
Chris Lattner29446522007-05-14 22:04:50 +0000400 // Don't allow anything with multiple uses.
401 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000402
Chris Lattner3adf9512007-05-25 02:19:06 +0000403 // Don't recurse exponentially.
404 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000405
Chris Lattner29446522007-05-14 22:04:50 +0000406 switch (Op.getOpcode()) {
407 default: return false;
408 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000409 // Don't invert constant FP values after legalize. The negated constant
410 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000411 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000412 case ISD::FADD:
413 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000414 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000415
Owen Andersonafd3d562012-03-06 00:29:31 +0000416 // After operation legalization, it might not be legal to create new FSUBs.
417 if (LegalOperations &&
418 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
419 return 0;
420
Craig Topper956342b2012-09-09 22:58:45 +0000421 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Andersonafd3d562012-03-06 00:29:31 +0000422 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
423 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000424 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000425 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Andersonafd3d562012-03-06 00:29:31 +0000426 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000427 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000428 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000429 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000430 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000431
Bill Wendlingd34470c2009-01-30 23:10:18 +0000432 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000433 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000434
Chris Lattner29446522007-05-14 22:04:50 +0000435 case ISD::FMUL:
436 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000437 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000438
Bill Wendlingd34470c2009-01-30 23:10:18 +0000439 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Andersonafd3d562012-03-06 00:29:31 +0000440 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
441 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000442 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000443
Owen Andersonafd3d562012-03-06 00:29:31 +0000444 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000445 Depth + 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000446
Chris Lattner29446522007-05-14 22:04:50 +0000447 case ISD::FP_EXTEND:
448 case ISD::FP_ROUND:
449 case ISD::FSIN:
Owen Andersonafd3d562012-03-06 00:29:31 +0000450 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000451 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000452 }
453}
454
455/// GetNegatedExpression - If isNegatibleForFree returns true, this function
456/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000457static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000458 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000459 // fneg is removable even if it has multiple uses.
460 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000461
Chris Lattner29446522007-05-14 22:04:50 +0000462 // Don't allow anything with multiple uses.
463 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000464
Chris Lattner3adf9512007-05-25 02:19:06 +0000465 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000466 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000467 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000468 case ISD::ConstantFP: {
469 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
470 V.changeSign();
471 return DAG.getConstantFP(V, Op.getValueType());
472 }
Chris Lattner29446522007-05-14 22:04:50 +0000473 case ISD::FADD:
474 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000475 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000476
Bill Wendlingd34470c2009-01-30 23:10:18 +0000477 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000478 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000479 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000480 &DAG.getTarget().Options, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000481 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000482 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000483 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000484 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000485 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Bill Wendling35247c32009-01-30 00:45:56 +0000486 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000487 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000488 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000489 Op.getOperand(0));
490 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000491 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000492 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000493
Bill Wendlingd34470c2009-01-30 23:10:18 +0000494 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000495 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000496 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000497 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000498
Bill Wendlingd34470c2009-01-30 23:10:18 +0000499 // fold (fneg (fsub A, B)) -> (fsub B, A)
Bill Wendling35247c32009-01-30 00:45:56 +0000500 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
501 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000502
Chris Lattner29446522007-05-14 22:04:50 +0000503 case ISD::FMUL:
504 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000505 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000506
Bill Wendlingd34470c2009-01-30 23:10:18 +0000507 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000508 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000509 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000510 &DAG.getTarget().Options, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000511 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000512 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000513 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000514 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000515
Bill Wendlingd34470c2009-01-30 23:10:18 +0000516 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Bill Wendling35247c32009-01-30 00:45:56 +0000517 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000518 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000519 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000520 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000521
Chris Lattner29446522007-05-14 22:04:50 +0000522 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000523 case ISD::FSIN:
Bill Wendling35247c32009-01-30 00:45:56 +0000524 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), 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 Lattner0bd48932008-01-17 07:00:52 +0000527 case ISD::FP_ROUND:
Bill Wendling35247c32009-01-30 00:45:56 +0000528 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000529 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000530 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000531 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000532 }
533}
Chris Lattner24664722006-03-01 04:53:38 +0000534
535
Nate Begeman4ebd8052005-09-01 23:24:04 +0000536// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
537// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000538// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000539// nodes based on the type of node we are checking. This simplifies life a
540// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000541static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
542 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000543 if (N.getOpcode() == ISD::SETCC) {
544 LHS = N.getOperand(0);
545 RHS = N.getOperand(1);
546 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000547 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000548 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000549 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000550 N.getOperand(2).getOpcode() == ISD::Constant &&
551 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000552 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000553 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
554 LHS = N.getOperand(0);
555 RHS = N.getOperand(1);
556 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000557 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000558 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000559 return false;
560}
561
Nate Begeman99801192005-09-07 23:25:52 +0000562// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
563// one use. If this is true, it allows the users to invert the operation for
564// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000565static bool isOneUseSetCC(SDValue N) {
566 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000567 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000568 return true;
569 return false;
570}
571
Bill Wendling35247c32009-01-30 00:45:56 +0000572SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
573 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000574 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000575 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
576 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000577 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000578 SDValue OpNode =
579 DAG.FoldConstantArithmetic(Opc, VT,
580 cast<ConstantSDNode>(N0.getOperand(1)),
581 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000582 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000583 }
584 if (N0.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000585 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendling35247c32009-01-30 00:45:56 +0000586 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
587 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000588 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000589 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000590 }
591 }
Bill Wendling35247c32009-01-30 00:45:56 +0000592
Nate Begemancd4d58c2006-02-03 06:46:56 +0000593 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
594 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000595 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000596 SDValue OpNode =
597 DAG.FoldConstantArithmetic(Opc, VT,
598 cast<ConstantSDNode>(N1.getOperand(1)),
599 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000600 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000601 }
602 if (N1.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000603 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlingd69c3142009-01-30 02:23:43 +0000604 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000605 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000606 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000607 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000608 }
609 }
Bill Wendling35247c32009-01-30 00:45:56 +0000610
Dan Gohman475871a2008-07-27 21:46:04 +0000611 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000612}
613
Dan Gohman475871a2008-07-27 21:46:04 +0000614SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
615 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000616 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
617 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000618 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000619 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000620 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000621 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000622 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000623 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000624 assert((!To[i].getNode() ||
625 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000626 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000627 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000628 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000629 if (AddTo) {
630 // Push the new nodes and any users onto the worklist
631 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000632 if (To[i].getNode()) {
633 AddToWorkList(To[i].getNode());
634 AddUsersToWorkList(To[i].getNode());
635 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000636 }
637 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000638
Dan Gohmandbe664a2009-01-19 21:44:21 +0000639 // Finally, if the node is now dead, remove it from the graph. The node
640 // may not be dead if the replacement process recursively simplified to
641 // something else needing this node.
642 if (N->use_empty()) {
643 // Nodes can be reintroduced into the worklist. Make sure we do not
644 // process a node that has been replaced.
645 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000646
Dan Gohmandbe664a2009-01-19 21:44:21 +0000647 // Finally, since the node is now dead, remove it from the graph.
648 DAG.DeleteNode(N);
649 }
Dan Gohman475871a2008-07-27 21:46:04 +0000650 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000651}
652
Evan Chenge5b51ac2010-04-17 06:13:15 +0000653void DAGCombiner::
654CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000655 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000656 // are deleted, make sure to remove them from our worklist.
657 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000658 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000659
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000660 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000661 AddToWorkList(TLO.New.getNode());
662 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000663
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000664 // Finally, if the node is now dead, remove it from the graph. The node
665 // may not be dead if the replacement process recursively simplified to
666 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000667 if (TLO.Old.getNode()->use_empty()) {
668 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000669
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000670 // If the operands of this node are only used by the node, they will now
671 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000672 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
673 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
674 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000675
Gabor Greifba36cb52008-08-28 21:40:38 +0000676 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000677 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000678}
679
680/// SimplifyDemandedBits - Check the specified integer node value to see if
681/// it can be simplified or if things it uses can be simplified by bit
682/// propagation. If so, return true.
683bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000684 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000685 APInt KnownZero, KnownOne;
686 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
687 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000688
Dan Gohmane5af2d32009-01-29 01:59:02 +0000689 // Revisit the node.
690 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000691
Dan Gohmane5af2d32009-01-29 01:59:02 +0000692 // Replace the old value with the new one.
693 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000694 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000695 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000696 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000697 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000698 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000699
Dan Gohmane5af2d32009-01-29 01:59:02 +0000700 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000701 return true;
702}
703
Evan Cheng95c57ea2010-04-24 04:43:44 +0000704void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
705 DebugLoc dl = Load->getDebugLoc();
706 EVT VT = Load->getValueType(0);
707 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000708
Evan Cheng95c57ea2010-04-24 04:43:44 +0000709 DEBUG(dbgs() << "\nReplacing.9 ";
710 Load->dump(&DAG);
711 dbgs() << "\nWith: ";
712 Trunc.getNode()->dump(&DAG);
713 dbgs() << '\n');
714 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000715 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
716 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng95c57ea2010-04-24 04:43:44 +0000717 removeFromWorkList(Load);
718 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000719 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000720}
721
722SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
723 Replace = false;
Evan Cheng4c26e932010-04-19 19:29:22 +0000724 DebugLoc dl = Op.getDebugLoc();
Evan Chenge5b51ac2010-04-17 06:13:15 +0000725 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000726 EVT MemVT = LD->getMemoryVT();
727 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000728 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000729 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000730 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000731 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000732 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000733 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000734 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000735 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000736 LD->isNonTemporal(), LD->getAlignment());
737 }
738
Evan Cheng4c26e932010-04-19 19:29:22 +0000739 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000740 switch (Opc) {
741 default: break;
742 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000743 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000744 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000745 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000746 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000747 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000748 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000749 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000750 case ISD::Constant: {
751 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000752 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000753 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000754 }
Evan Chengcaf77402010-04-23 19:10:30 +0000755 }
756
757 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000758 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000759 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000760}
761
Evan Cheng95c57ea2010-04-24 04:43:44 +0000762SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000763 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
764 return SDValue();
765 EVT OldVT = Op.getValueType();
766 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000767 bool Replace = false;
768 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
769 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000770 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000771 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000772
773 if (Replace)
774 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
775 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000776 DAG.getValueType(OldVT));
777}
778
Evan Cheng95c57ea2010-04-24 04:43:44 +0000779SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000780 EVT OldVT = Op.getValueType();
781 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000782 bool Replace = false;
783 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
784 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000785 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000786 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000787
788 if (Replace)
789 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
790 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000791}
792
Evan Cheng64b7bf72010-04-16 06:14:10 +0000793/// PromoteIntBinOp - Promote the specified integer binary operation if the
794/// target indicates it is beneficial. e.g. On x86, it's usually better to
795/// promote i16 operations to i32 since i16 instructions are longer.
796SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
797 if (!LegalOperations)
798 return SDValue();
799
800 EVT VT = Op.getValueType();
801 if (VT.isVector() || !VT.isInteger())
802 return SDValue();
803
Evan Chenge5b51ac2010-04-17 06:13:15 +0000804 // If operation type is 'undesirable', e.g. i16 on x86, consider
805 // promoting it.
806 unsigned Opc = Op.getOpcode();
807 if (TLI.isTypeDesirableForOp(Opc, VT))
808 return SDValue();
809
Evan Cheng64b7bf72010-04-16 06:14:10 +0000810 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000811 // Consult target whether it is a good idea to promote this operation and
812 // what's the right type to promote it to.
813 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000814 assert(PVT != VT && "Don't know what type to promote to!");
815
Evan Cheng95c57ea2010-04-24 04:43:44 +0000816 bool Replace0 = false;
817 SDValue N0 = Op.getOperand(0);
818 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
819 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000820 return SDValue();
821
Evan Cheng95c57ea2010-04-24 04:43:44 +0000822 bool Replace1 = false;
823 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000824 SDValue NN1;
825 if (N0 == N1)
826 NN1 = NN0;
827 else {
828 NN1 = PromoteOperand(N1, PVT, Replace1);
829 if (NN1.getNode() == 0)
830 return SDValue();
831 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000832
Evan Cheng95c57ea2010-04-24 04:43:44 +0000833 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000834 if (NN1.getNode())
835 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000836
837 if (Replace0)
838 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
839 if (Replace1)
840 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000841
Evan Chengac7eae52010-04-27 19:48:13 +0000842 DEBUG(dbgs() << "\nPromoting ";
843 Op.getNode()->dump(&DAG));
Evan Cheng07c4e102010-04-22 20:19:46 +0000844 DebugLoc dl = Op.getDebugLoc();
845 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000846 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000847 }
848 return SDValue();
849}
850
851/// PromoteIntShiftOp - Promote the specified integer shift operation if the
852/// target indicates it is beneficial. e.g. On x86, it's usually better to
853/// promote i16 operations to i32 since i16 instructions are longer.
854SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
855 if (!LegalOperations)
856 return SDValue();
857
858 EVT VT = Op.getValueType();
859 if (VT.isVector() || !VT.isInteger())
860 return SDValue();
861
862 // If operation type is 'undesirable', e.g. i16 on x86, consider
863 // promoting it.
864 unsigned Opc = Op.getOpcode();
865 if (TLI.isTypeDesirableForOp(Opc, VT))
866 return SDValue();
867
868 EVT PVT = VT;
869 // Consult target whether it is a good idea to promote this operation and
870 // what's the right type to promote it to.
871 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
872 assert(PVT != VT && "Don't know what type to promote to!");
873
Evan Cheng95c57ea2010-04-24 04:43:44 +0000874 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000875 SDValue N0 = Op.getOperand(0);
876 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000877 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000878 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000879 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000880 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000881 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000882 if (N0.getNode() == 0)
883 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000884
Evan Chenge5b51ac2010-04-17 06:13:15 +0000885 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000886 if (Replace)
887 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000888
Evan Chengac7eae52010-04-27 19:48:13 +0000889 DEBUG(dbgs() << "\nPromoting ";
890 Op.getNode()->dump(&DAG));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000891 DebugLoc dl = Op.getDebugLoc();
892 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000893 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000894 }
895 return SDValue();
896}
897
Evan Cheng4c26e932010-04-19 19:29:22 +0000898SDValue DAGCombiner::PromoteExtend(SDValue Op) {
899 if (!LegalOperations)
900 return SDValue();
901
902 EVT VT = Op.getValueType();
903 if (VT.isVector() || !VT.isInteger())
904 return SDValue();
905
906 // If operation type is 'undesirable', e.g. i16 on x86, consider
907 // promoting it.
908 unsigned Opc = Op.getOpcode();
909 if (TLI.isTypeDesirableForOp(Opc, VT))
910 return SDValue();
911
912 EVT PVT = VT;
913 // Consult target whether it is a good idea to promote this operation and
914 // what's the right type to promote it to.
915 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
916 assert(PVT != VT && "Don't know what type to promote to!");
917 // fold (aext (aext x)) -> (aext x)
918 // fold (aext (zext x)) -> (zext x)
919 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000920 DEBUG(dbgs() << "\nPromoting ";
921 Op.getNode()->dump(&DAG));
Evan Cheng4c26e932010-04-19 19:29:22 +0000922 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), VT, Op.getOperand(0));
923 }
924 return SDValue();
925}
926
927bool DAGCombiner::PromoteLoad(SDValue Op) {
928 if (!LegalOperations)
929 return false;
930
931 EVT VT = Op.getValueType();
932 if (VT.isVector() || !VT.isInteger())
933 return false;
934
935 // If operation type is 'undesirable', e.g. i16 on x86, consider
936 // promoting it.
937 unsigned Opc = Op.getOpcode();
938 if (TLI.isTypeDesirableForOp(Opc, VT))
939 return false;
940
941 EVT PVT = VT;
942 // Consult target whether it is a good idea to promote this operation and
943 // what's the right type to promote it to.
944 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
945 assert(PVT != VT && "Don't know what type to promote to!");
946
947 DebugLoc dl = Op.getDebugLoc();
948 SDNode *N = Op.getNode();
949 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000950 EVT MemVT = LD->getMemoryVT();
951 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000952 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000953 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000954 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000955 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000956 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000957 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000958 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000959 LD->isNonTemporal(), LD->getAlignment());
960 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
961
Evan Cheng95c57ea2010-04-24 04:43:44 +0000962 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000963 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000964 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000965 Result.getNode()->dump(&DAG);
966 dbgs() << '\n');
967 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000968 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
969 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Cheng4c26e932010-04-19 19:29:22 +0000970 removeFromWorkList(N);
971 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000972 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +0000973 return true;
974 }
975 return false;
976}
977
Evan Chenge5b51ac2010-04-17 06:13:15 +0000978
Chris Lattner29446522007-05-14 22:04:50 +0000979//===----------------------------------------------------------------------===//
980// Main DAG Combiner implementation
981//===----------------------------------------------------------------------===//
982
Duncan Sands25cf2272008-11-24 14:53:14 +0000983void DAGCombiner::Run(CombineLevel AtLevel) {
984 // set the instance variables, so that the various visit routines may use it.
985 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +0000986 LegalOperations = Level >= AfterLegalizeVectorOps;
987 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000988
Evan Cheng17a568b2008-08-29 22:21:44 +0000989 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +0000990 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
991 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +0000992 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000993
Evan Cheng17a568b2008-08-29 22:21:44 +0000994 // Create a dummy node (which is not added to allnodes), that adds a reference
995 // to the root node, preventing it from being deleted, and tracking any
996 // changes of the root.
997 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +0000998
Jim Laskey26f7fa72006-10-17 19:33:52 +0000999 // The root of the dag may dangle to deleted nodes until the dag combiner is
1000 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +00001001 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001002
James Molloy6660c052012-02-16 09:17:04 +00001003 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +00001004 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +00001005 while (!WorkListContents.empty()) {
1006 SDNode *N;
1007 // The WorkListOrder holds the SDNodes in order, but it may contain duplicates.
1008 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1009 // worklist *should* contain, and check the node we want to visit is should
1010 // actually be visited.
1011 do {
Benjamin Kramerd5f76902012-03-10 00:23:58 +00001012 N = WorkListOrder.pop_back_val();
James Molloy6660c052012-02-16 09:17:04 +00001013 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001014
Evan Cheng17a568b2008-08-29 22:21:44 +00001015 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1016 // N is deleted from the DAG, since they too may now be dead or may have a
1017 // reduced number of uses, allowing other xforms.
1018 if (N->use_empty() && N != &Dummy) {
1019 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1020 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001021
Evan Cheng17a568b2008-08-29 22:21:44 +00001022 DAG.DeleteNode(N);
1023 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001024 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001025
Evan Cheng17a568b2008-08-29 22:21:44 +00001026 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001027
Evan Cheng17a568b2008-08-29 22:21:44 +00001028 if (RV.getNode() == 0)
1029 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001030
Evan Cheng17a568b2008-08-29 22:21:44 +00001031 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001032
Evan Cheng17a568b2008-08-29 22:21:44 +00001033 // If we get back the same node we passed in, rather than a new node or
1034 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001035 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001036 // mechanics for us, we have no work to do in this case.
1037 if (RV.getNode() == N)
1038 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001039
Evan Cheng17a568b2008-08-29 22:21:44 +00001040 assert(N->getOpcode() != ISD::DELETED_NODE &&
1041 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1042 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001043
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001044 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001045 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001046 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001047 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001048 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001049
Devang Patel9728ea22011-05-23 22:04:42 +00001050 // Transfer debug value.
1051 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001052 WorkListRemover DeadNodes(*this);
1053 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001054 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng17a568b2008-08-29 22:21:44 +00001055 else {
1056 assert(N->getValueType(0) == RV.getValueType() &&
1057 N->getNumValues() == 1 && "Type mismatch");
1058 SDValue OpV = RV;
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001059 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001060 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001061
Evan Cheng17a568b2008-08-29 22:21:44 +00001062 // Push the new node and any users onto the worklist
1063 AddToWorkList(RV.getNode());
1064 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001065
Evan Cheng17a568b2008-08-29 22:21:44 +00001066 // Add any uses of the old node to the worklist in case this node is the
1067 // last one that uses them. They may become dead after this node is
1068 // deleted.
1069 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1070 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001071
Dan Gohmandbe664a2009-01-19 21:44:21 +00001072 // Finally, if the node is now dead, remove it from the graph. The node
1073 // may not be dead if the replacement process recursively simplified to
1074 // something else needing this node.
1075 if (N->use_empty()) {
1076 // Nodes can be reintroduced into the worklist. Make sure we do not
1077 // process a node that has been replaced.
1078 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001079
Dan Gohmandbe664a2009-01-19 21:44:21 +00001080 // Finally, since the node is now dead, remove it from the graph.
1081 DAG.DeleteNode(N);
1082 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001083 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001084
Chris Lattner95038592005-10-05 06:35:28 +00001085 // If the root changed (e.g. it was a dead load, update the root).
1086 DAG.setRoot(Dummy.getValue());
Hal Finkel31490ba2012-04-16 03:33:22 +00001087 DAG.RemoveDeadNodes();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001088}
1089
Dan Gohman475871a2008-07-27 21:46:04 +00001090SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001091 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001092 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001093 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001094 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001095 case ISD::ADD: return visitADD(N);
1096 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001097 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001098 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001099 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001100 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001101 case ISD::MUL: return visitMUL(N);
1102 case ISD::SDIV: return visitSDIV(N);
1103 case ISD::UDIV: return visitUDIV(N);
1104 case ISD::SREM: return visitSREM(N);
1105 case ISD::UREM: return visitUREM(N);
1106 case ISD::MULHU: return visitMULHU(N);
1107 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001108 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1109 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001110 case ISD::SMULO: return visitSMULO(N);
1111 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001112 case ISD::SDIVREM: return visitSDIVREM(N);
1113 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001114 case ISD::AND: return visitAND(N);
1115 case ISD::OR: return visitOR(N);
1116 case ISD::XOR: return visitXOR(N);
1117 case ISD::SHL: return visitSHL(N);
1118 case ISD::SRA: return visitSRA(N);
1119 case ISD::SRL: return visitSRL(N);
1120 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001121 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001122 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001123 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001124 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7beb2005-09-16 00:54:12 +00001125 case ISD::SELECT: return visitSELECT(N);
1126 case ISD::SELECT_CC: return visitSELECT_CC(N);
1127 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001128 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1129 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001130 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001131 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1132 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001133 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001134 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001135 case ISD::FADD: return visitFADD(N);
1136 case ISD::FSUB: return visitFSUB(N);
1137 case ISD::FMUL: return visitFMUL(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00001138 case ISD::FMA: return visitFMA(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001139 case ISD::FDIV: return visitFDIV(N);
1140 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001141 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001142 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1143 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1144 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1145 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1146 case ISD::FP_ROUND: return visitFP_ROUND(N);
1147 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1148 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1149 case ISD::FNEG: return visitFNEG(N);
1150 case ISD::FABS: return visitFABS(N);
Owen Anderson7c626d32012-08-13 23:32:49 +00001151 case ISD::FFLOOR: return visitFFLOOR(N);
1152 case ISD::FCEIL: return visitFCEIL(N);
1153 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001154 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001155 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001156 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001157 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001158 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001159 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001160 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1161 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001162 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001163 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Jim Grosbach9a526492010-06-23 16:07:42 +00001164 case ISD::MEMBARRIER: return visitMEMBARRIER(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001165 }
Dan Gohman475871a2008-07-27 21:46:04 +00001166 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001167}
1168
Dan Gohman475871a2008-07-27 21:46:04 +00001169SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001170 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001171
1172 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001173 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001174 assert(N->getOpcode() != ISD::DELETED_NODE &&
1175 "Node was deleted but visit returned NULL!");
1176
1177 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1178 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1179
1180 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001181 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00001182 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001183
1184 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1185 }
1186 }
1187
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001188 // If nothing happened still, try promoting the operation.
1189 if (RV.getNode() == 0) {
1190 switch (N->getOpcode()) {
1191 default: break;
1192 case ISD::ADD:
1193 case ISD::SUB:
1194 case ISD::MUL:
1195 case ISD::AND:
1196 case ISD::OR:
1197 case ISD::XOR:
1198 RV = PromoteIntBinOp(SDValue(N, 0));
1199 break;
1200 case ISD::SHL:
1201 case ISD::SRA:
1202 case ISD::SRL:
1203 RV = PromoteIntShiftOp(SDValue(N, 0));
1204 break;
1205 case ISD::SIGN_EXTEND:
1206 case ISD::ZERO_EXTEND:
1207 case ISD::ANY_EXTEND:
1208 RV = PromoteExtend(SDValue(N, 0));
1209 break;
1210 case ISD::LOAD:
1211 if (PromoteLoad(SDValue(N, 0)))
1212 RV = SDValue(N, 0);
1213 break;
1214 }
1215 }
1216
Scott Michelfdc40a02009-02-17 22:15:04 +00001217 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001218 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001219 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001220 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1221 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001222 SDValue N0 = N->getOperand(0);
1223 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001224
Evan Cheng08b11732008-03-22 01:55:50 +00001225 // Constant operands are canonicalized to RHS.
1226 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001227 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001228 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1229 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001230 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001231 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001232 }
1233 }
1234
Dan Gohman389079b2007-10-08 17:57:15 +00001235 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001236}
Dan Gohman389079b2007-10-08 17:57:15 +00001237
Chris Lattner6270f682006-10-08 22:57:01 +00001238/// getInputChainForNode - Given a node, return its input chain if it has one,
1239/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001240static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001241 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001242 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001243 return N->getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001244 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001245 return N->getOperand(NumOps-1);
1246 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001247 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001248 return N->getOperand(i);
1249 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001250 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001251}
1252
Dan Gohman475871a2008-07-27 21:46:04 +00001253SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001254 // If N has two operands, where one has an input chain equal to the other,
1255 // the 'other' chain is redundant.
1256 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001257 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001258 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001259 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001260 return N->getOperand(1);
1261 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001262
Chris Lattnerc76d4412007-05-16 06:37:59 +00001263 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001264 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001265 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001266 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001267
Jim Laskey6ff23e52006-10-04 16:53:27 +00001268 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001269 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001270
Jim Laskey71382342006-10-07 23:37:56 +00001271 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001272 // encountered.
1273 for (unsigned i = 0; i < TFs.size(); ++i) {
1274 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001275
Jim Laskey6ff23e52006-10-04 16:53:27 +00001276 // Check each of the operands.
1277 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001278 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001279
Jim Laskey6ff23e52006-10-04 16:53:27 +00001280 switch (Op.getOpcode()) {
1281 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001282 // Entry tokens don't need to be added to the list. They are
1283 // rededundant.
1284 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001285 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001286
Jim Laskey6ff23e52006-10-04 16:53:27 +00001287 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001288 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001289 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001290 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001291 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001292 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001293 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001294 Changed = true;
1295 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001296 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001297 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001298
Jim Laskey6ff23e52006-10-04 16:53:27 +00001299 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001300 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001301 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001302 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001303 else
1304 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001305 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001306 }
1307 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001308 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001309
Dan Gohman475871a2008-07-27 21:46:04 +00001310 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001311
1312 // If we've change things around then replace token factor.
1313 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001314 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001315 // The entry token is the only possible outcome.
1316 Result = DAG.getEntryNode();
1317 } else {
1318 // New and improved token factor.
Bill Wendling5c71acf2009-01-30 01:13:16 +00001319 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001320 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001321 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001322
Jim Laskey274062c2006-10-13 23:32:28 +00001323 // Don't add users to work list.
1324 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001325 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001326
Jim Laskey6ff23e52006-10-04 16:53:27 +00001327 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001328}
1329
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001330/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001331SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001332 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001333 // Replacing results may cause a different MERGE_VALUES to suddenly
1334 // be CSE'd with N, and carry its uses with it. Iterate until no
1335 // uses remain, to ensure that the node can be safely deleted.
Pete Cooper3affd9e2012-06-20 19:35:43 +00001336 // First add the users of this node to the work list so that they
1337 // can be tried again once they have new operands.
1338 AddUsersToWorkList(N);
Dan Gohman00edf392009-08-10 23:43:19 +00001339 do {
1340 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001341 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman00edf392009-08-10 23:43:19 +00001342 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001343 removeFromWorkList(N);
1344 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001345 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001346}
1347
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001348static
Bill Wendlingd69c3142009-01-30 02:23:43 +00001349SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
1350 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001351 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001352 SDValue N00 = N0.getOperand(0);
1353 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001354 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001355
Gabor Greifba36cb52008-08-28 21:40:38 +00001356 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001357 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001358 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1359 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
1360 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
1361 N00.getOperand(0), N01),
1362 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
1363 N00.getOperand(1), N01));
1364 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001365 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001366
Dan Gohman475871a2008-07-27 21:46:04 +00001367 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001368}
1369
Dan Gohman475871a2008-07-27 21:46:04 +00001370SDValue DAGCombiner::visitADD(SDNode *N) {
1371 SDValue N0 = N->getOperand(0);
1372 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001373 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1374 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001375 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001376
1377 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001378 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001379 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001380 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001381 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001382
Dan Gohman613e0d82007-07-03 14:03:57 +00001383 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001384 if (N0.getOpcode() == ISD::UNDEF)
1385 return N0;
1386 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001387 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001388 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001389 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001390 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001391 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001392 if (N0C && !N1C)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001393 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001394 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001395 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001396 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001397 // fold (add Sym, c) -> Sym+c
1398 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001399 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001400 GA->getOpcode() == ISD::GlobalAddress)
Devang Patel0d881da2010-07-06 22:08:15 +00001401 return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001402 GA->getOffset() +
1403 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001404 // fold ((c1-A)+c2) -> (c1+c2)-A
1405 if (N1C && N0.getOpcode() == ISD::SUB)
1406 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001407 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001408 DAG.getConstant(N1C->getAPIntValue()+
1409 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001410 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001411 // reassociate add
Bill Wendling35247c32009-01-30 00:45:56 +00001412 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001413 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001414 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001415 // fold ((0-A) + B) -> B-A
1416 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1417 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001418 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001419 // fold (A + (0-B)) -> A-B
1420 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1421 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001422 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001423 // fold (A+(B-A)) -> B
1424 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001425 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001426 // fold ((B-A)+A) -> B
1427 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1428 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001429 // fold (A+(B-(A+C))) to (B-C)
1430 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001431 N0 == N1.getOperand(1).getOperand(0))
1432 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001433 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001434 // fold (A+(B-(C+A))) to (B-C)
1435 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001436 N0 == N1.getOperand(1).getOperand(1))
1437 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001438 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001439 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001440 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1441 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001442 N0 == N1.getOperand(0).getOperand(1))
1443 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1444 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001445
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001446 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1447 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1448 SDValue N00 = N0.getOperand(0);
1449 SDValue N01 = N0.getOperand(1);
1450 SDValue N10 = N1.getOperand(0);
1451 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001452
1453 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1454 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1455 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1456 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001457 }
Chris Lattner947c2892006-03-13 06:51:27 +00001458
Dan Gohman475871a2008-07-27 21:46:04 +00001459 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1460 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001461
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001462 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001463 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001464 APInt LHSZero, LHSOne;
1465 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001466 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001467
Dan Gohman948d8ea2008-02-20 16:33:30 +00001468 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001469 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001470
Chris Lattner947c2892006-03-13 06:51:27 +00001471 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1472 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001473 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001474 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001475 }
1476 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001477
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001478 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001479 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001480 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001481 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001482 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001483 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001484 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001485 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001486 }
1487
Dan Gohmancd9e1552010-01-19 23:30:49 +00001488 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1489 if (N1.getOpcode() == ISD::SHL &&
1490 N1.getOperand(0).getOpcode() == ISD::SUB)
1491 if (ConstantSDNode *C =
1492 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1493 if (C->getAPIntValue() == 0)
1494 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0,
1495 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1496 N1.getOperand(0).getOperand(1),
1497 N1.getOperand(1)));
1498 if (N0.getOpcode() == ISD::SHL &&
1499 N0.getOperand(0).getOpcode() == ISD::SUB)
1500 if (ConstantSDNode *C =
1501 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1502 if (C->getAPIntValue() == 0)
1503 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1,
1504 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1505 N0.getOperand(0).getOperand(1),
1506 N0.getOperand(1)));
1507
Owen Andersonbc146b02010-09-21 20:42:50 +00001508 if (N1.getOpcode() == ISD::AND) {
1509 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001510 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001511 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1512 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001513
Owen Andersonbc146b02010-09-21 20:42:50 +00001514 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1515 // and similar xforms where the inner op is either ~0 or 0.
1516 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
1517 DebugLoc DL = N->getDebugLoc();
1518 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1519 }
1520 }
1521
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001522 // add (sext i1), X -> sub X, (zext i1)
1523 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1524 N0.getOperand(0).getValueType() == MVT::i1 &&
1525 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
1526 DebugLoc DL = N->getDebugLoc();
1527 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1528 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1529 }
1530
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001531 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001532}
1533
Dan Gohman475871a2008-07-27 21:46:04 +00001534SDValue DAGCombiner::visitADDC(SDNode *N) {
1535 SDValue N0 = N->getOperand(0);
1536 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001537 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1538 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001539 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001540
Chris Lattner91153682007-03-04 20:03:15 +00001541 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001542 if (!N->hasAnyUseOfValue(1))
Craig Toppercc274522012-01-07 09:06:39 +00001543 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001544 DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001545 N->getDebugLoc(), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001546
Chris Lattner91153682007-03-04 20:03:15 +00001547 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001548 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001549 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001550
Chris Lattnerb6541762007-03-04 20:40:38 +00001551 // fold (addc x, 0) -> x + no carry out
1552 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001553 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001554 N->getDebugLoc(), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001555
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001556 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001557 APInt LHSZero, LHSOne;
1558 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001559 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001560
Dan Gohman948d8ea2008-02-20 16:33:30 +00001561 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001562 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001563
Chris Lattnerb6541762007-03-04 20:40:38 +00001564 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1565 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001566 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Bill Wendling14036c02009-01-30 02:38:00 +00001567 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001568 DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001569 N->getDebugLoc(), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001570 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001571
Dan Gohman475871a2008-07-27 21:46:04 +00001572 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001573}
1574
Dan Gohman475871a2008-07-27 21:46:04 +00001575SDValue DAGCombiner::visitADDE(SDNode *N) {
1576 SDValue N0 = N->getOperand(0);
1577 SDValue N1 = N->getOperand(1);
1578 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001579 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1580 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001581
Chris Lattner91153682007-03-04 20:03:15 +00001582 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001583 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001584 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1585 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001586
Chris Lattnerb6541762007-03-04 20:40:38 +00001587 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001588 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Craig Toppercc274522012-01-07 09:06:39 +00001589 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N0, N1);
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
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001594// Since it may not be valid to emit a fold to zero for vector initializers
1595// check if we can before folding.
1596static SDValue tryFoldToZero(DebugLoc DL, const TargetLowering &TLI, EVT VT,
Owen Anderson95771af2011-02-25 21:41:48 +00001597 SelectionDAG &DAG, bool LegalOperations) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001598 if (!VT.isVector()) {
1599 return DAG.getConstant(0, VT);
Dan Gohman71dc7c92011-05-17 22:20:36 +00001600 }
1601 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001602 // Produce a vector of zeros.
1603 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
1604 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
1605 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
1606 &Ops[0], Ops.size());
1607 }
1608 return SDValue();
1609}
1610
Dan Gohman475871a2008-07-27 21:46:04 +00001611SDValue DAGCombiner::visitSUB(SDNode *N) {
1612 SDValue N0 = N->getOperand(0);
1613 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001614 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1615 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001616 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1617 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001618 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001619
Dan Gohman7f321562007-06-25 16:23:39 +00001620 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001621 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001622 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001623 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001624 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001625
Chris Lattner854077d2005-10-17 01:07:11 +00001626 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001627 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001628 if (N0 == N1)
1629 return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001630 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001631 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001632 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001633 // fold (sub x, c) -> (add x, -c)
1634 if (N1C)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001635 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001636 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001637 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1638 if (N0C && N0C->isAllOnesValue())
1639 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001640 // fold A-(A-B) -> B
1641 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1642 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001643 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001644 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001645 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001646 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001647 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001648 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001649 // fold C2-(A+C1) -> (C2-C1)-A
1650 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00001651 SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(),
1652 VT);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001653 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, NewC,
Bill Wendling96cb1122012-07-19 00:04:14 +00001654 N1.getOperand(0));
Eric Christopher7332e6e2011-07-14 01:12:15 +00001655 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001656 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001657 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001658 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1659 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001660 N0.getOperand(1).getOperand(0) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001661 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1662 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001663 // fold ((A+(C+B))-B) -> A+C
1664 if (N0.getOpcode() == ISD::ADD &&
1665 N0.getOperand(1).getOpcode() == ISD::ADD &&
1666 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001667 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1668 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001669 // fold ((A-(B-C))-C) -> A-B
1670 if (N0.getOpcode() == ISD::SUB &&
1671 N0.getOperand(1).getOpcode() == ISD::SUB &&
1672 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001673 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1674 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001675
Dan Gohman613e0d82007-07-03 14:03:57 +00001676 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001677 if (N0.getOpcode() == ISD::UNDEF)
1678 return N0;
1679 if (N1.getOpcode() == ISD::UNDEF)
1680 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001681
Dan Gohman6520e202008-10-18 02:06:02 +00001682 // If the relocation model supports it, consider symbol offsets.
1683 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001684 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001685 // fold (sub Sym, c) -> Sym-c
1686 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Devang Patel0d881da2010-07-06 22:08:15 +00001687 return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001688 GA->getOffset() -
1689 (uint64_t)N1C->getSExtValue());
1690 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1691 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1692 if (GA->getGlobal() == GB->getGlobal())
1693 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1694 VT);
1695 }
1696
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001697 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001698}
1699
Craig Toppercc274522012-01-07 09:06:39 +00001700SDValue DAGCombiner::visitSUBC(SDNode *N) {
1701 SDValue N0 = N->getOperand(0);
1702 SDValue N1 = N->getOperand(1);
1703 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1704 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1705 EVT VT = N0.getValueType();
1706
1707 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001708 if (!N->hasAnyUseOfValue(1))
Craig Toppercc274522012-01-07 09:06:39 +00001709 return CombineTo(N, DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1),
1710 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1711 MVT::Glue));
1712
1713 // fold (subc x, x) -> 0 + no borrow
1714 if (N0 == N1)
1715 return CombineTo(N, DAG.getConstant(0, VT),
1716 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1717 MVT::Glue));
1718
1719 // fold (subc x, 0) -> x + no borrow
1720 if (N1C && N1C->isNullValue())
1721 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1722 MVT::Glue));
1723
1724 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1725 if (N0C && N0C->isAllOnesValue())
1726 return CombineTo(N, DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0),
1727 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1728 MVT::Glue));
1729
1730 return SDValue();
1731}
1732
1733SDValue DAGCombiner::visitSUBE(SDNode *N) {
1734 SDValue N0 = N->getOperand(0);
1735 SDValue N1 = N->getOperand(1);
1736 SDValue CarryIn = N->getOperand(2);
1737
1738 // fold (sube x, y, false) -> (subc x, y)
1739 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
1740 return DAG.getNode(ISD::SUBC, N->getDebugLoc(), N->getVTList(), N0, N1);
1741
1742 return SDValue();
1743}
1744
Dan Gohman475871a2008-07-27 21:46:04 +00001745SDValue DAGCombiner::visitMUL(SDNode *N) {
1746 SDValue N0 = N->getOperand(0);
1747 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001748 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1749 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001750 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001751
Dan Gohman7f321562007-06-25 16:23:39 +00001752 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001753 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001754 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001755 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001756 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001757
Dan Gohman613e0d82007-07-03 14:03:57 +00001758 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001759 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001760 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001761 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001762 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001763 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001764 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001765 if (N0C && !N1C)
Bill Wendling9c8148a2009-01-30 02:45:56 +00001766 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001767 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001768 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001769 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001770 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001771 if (N1C && N1C->isAllOnesValue())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001772 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1773 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001774 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001775 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001776 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001777 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001778 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001779 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Chris Lattner66b8bc32009-03-09 20:22:18 +00001780 if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) {
1781 unsigned Log2Val = (-N1C->getAPIntValue()).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001782 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001783 // single-use add), we should put the negate there.
Bill Wendling9c8148a2009-01-30 02:45:56 +00001784 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1785 DAG.getConstant(0, VT),
Bill Wendling73e16b22009-01-30 02:49:26 +00001786 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001787 DAG.getConstant(Log2Val,
1788 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001789 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001790 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendling73e16b22009-01-30 02:49:26 +00001791 if (N1C && N0.getOpcode() == ISD::SHL &&
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001792 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001793 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1794 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001795 AddToWorkList(C3.getNode());
Bill Wendling9c8148a2009-01-30 02:45:56 +00001796 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1797 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001798 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001799
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001800 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1801 // use.
1802 {
Dan Gohman475871a2008-07-27 21:46:04 +00001803 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001804 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1805 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001806 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001807 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001808 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001809 isa<ConstantSDNode>(N1.getOperand(1)) &&
1810 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001811 Sh = N1; Y = N0;
1812 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001813
Gabor Greifba36cb52008-08-28 21:40:38 +00001814 if (Sh.getNode()) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001815 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1816 Sh.getOperand(0), Y);
1817 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1818 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001819 }
1820 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001821
Chris Lattnera1deca32006-03-04 23:33:26 +00001822 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Scott Michelfdc40a02009-02-17 22:15:04 +00001823 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendling9c8148a2009-01-30 02:45:56 +00001824 isa<ConstantSDNode>(N0.getOperand(1)))
1825 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1826 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1827 N0.getOperand(0), N1),
1828 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1829 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001830
Nate Begemancd4d58c2006-02-03 06:46:56 +00001831 // reassociate mul
Bill Wendling35247c32009-01-30 00:45:56 +00001832 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001833 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001834 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001835
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001836 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001837}
1838
Dan Gohman475871a2008-07-27 21:46:04 +00001839SDValue DAGCombiner::visitSDIV(SDNode *N) {
1840 SDValue N0 = N->getOperand(0);
1841 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001842 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1843 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001844 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001845
Dan Gohman7f321562007-06-25 16:23:39 +00001846 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001847 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001848 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001849 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001850 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001851
Nate Begeman1d4d4142005-09-01 00:19:25 +00001852 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001853 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001854 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001855 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001856 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001857 return N0;
1858 // fold (sdiv X, -1) -> 0-X
1859 if (N1C && N1C->isAllOnesValue())
Bill Wendling944d34b2009-01-30 02:52:17 +00001860 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1861 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001862 // If we know the sign bits of both operands are zero, strength reduce to a
1863 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001864 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001865 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling944d34b2009-01-30 02:52:17 +00001866 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1867 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001868 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001869 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001870 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001871 (N1C->getAPIntValue().isPowerOf2() ||
1872 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001873 // If dividing by powers of two is cheap, then don't perform the following
1874 // fold.
1875 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001876 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001877
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001878 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001879
Chris Lattner8f4880b2006-02-16 08:02:36 +00001880 // Splat the sign bit into the register
Bill Wendling944d34b2009-01-30 02:52:17 +00001881 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1882 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001883 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001884 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001885
Chris Lattner8f4880b2006-02-16 08:02:36 +00001886 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling944d34b2009-01-30 02:52:17 +00001887 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1888 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001889 getShiftAmountTy(SGN.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001890 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001891 AddToWorkList(SRL.getNode());
1892 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling944d34b2009-01-30 02:52:17 +00001893 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001894 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001895
Nate Begeman405e3ec2005-10-21 00:02:42 +00001896 // If we're dividing by a positive value, we're done. Otherwise, we must
1897 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001898 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001899 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001900
Gabor Greifba36cb52008-08-28 21:40:38 +00001901 AddToWorkList(SRA.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001902 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1903 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001904 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001905
Nate Begeman69575232005-10-20 02:15:44 +00001906 // if integer divide is expensive and we satisfy the requirements, emit an
1907 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001908 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001909 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001910 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001911 }
Dan Gohman7f321562007-06-25 16:23:39 +00001912
Dan Gohman613e0d82007-07-03 14:03:57 +00001913 // undef / X -> 0
1914 if (N0.getOpcode() == ISD::UNDEF)
1915 return DAG.getConstant(0, VT);
1916 // X / undef -> undef
1917 if (N1.getOpcode() == ISD::UNDEF)
1918 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001919
Dan Gohman475871a2008-07-27 21:46:04 +00001920 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001921}
1922
Dan Gohman475871a2008-07-27 21:46:04 +00001923SDValue DAGCombiner::visitUDIV(SDNode *N) {
1924 SDValue N0 = N->getOperand(0);
1925 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001926 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1927 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001928 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001929
Dan Gohman7f321562007-06-25 16:23:39 +00001930 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001931 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001932 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001933 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001934 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001935
Nate Begeman1d4d4142005-09-01 00:19:25 +00001936 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001937 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001938 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001939 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001940 if (N1C && N1C->getAPIntValue().isPowerOf2())
Scott Michelfdc40a02009-02-17 22:15:04 +00001941 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001942 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001943 getShiftAmountTy(N0.getValueType())));
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001944 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001945 if (N1.getOpcode() == ISD::SHL) {
1946 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001947 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001948 EVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendling07d85142009-01-30 02:55:25 +00001949 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1950 N1.getOperand(1),
1951 DAG.getConstant(SHC->getAPIntValue()
1952 .logBase2(),
1953 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001954 AddToWorkList(Add.getNode());
Bill Wendling07d85142009-01-30 02:55:25 +00001955 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001956 }
1957 }
1958 }
Nate Begeman69575232005-10-20 02:15:44 +00001959 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001960 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001961 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001962 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001963 }
Dan Gohman7f321562007-06-25 16:23:39 +00001964
Dan Gohman613e0d82007-07-03 14:03:57 +00001965 // undef / X -> 0
1966 if (N0.getOpcode() == ISD::UNDEF)
1967 return DAG.getConstant(0, VT);
1968 // X / undef -> undef
1969 if (N1.getOpcode() == ISD::UNDEF)
1970 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001971
Dan Gohman475871a2008-07-27 21:46:04 +00001972 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001973}
1974
Dan Gohman475871a2008-07-27 21:46:04 +00001975SDValue DAGCombiner::visitSREM(SDNode *N) {
1976 SDValue N0 = N->getOperand(0);
1977 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001978 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1979 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001980 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001981
Nate Begeman1d4d4142005-09-01 00:19:25 +00001982 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001983 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001984 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001985 // If we know the sign bits of both operands are zero, strength reduce to a
1986 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001987 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001988 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001989 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00001990 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001991
Dan Gohman77003042007-11-26 23:46:11 +00001992 // If X/C can be simplified by the division-by-constant logic, lower
1993 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001994 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001995 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001996 AddToWorkList(Div.getNode());
1997 SDValue OptimizedDiv = combine(Div.getNode());
1998 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001999 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
2000 OptimizedDiv, N1);
2001 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002002 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002003 return Sub;
2004 }
Chris Lattner26d29902006-10-12 20:58:32 +00002005 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002006
Dan Gohman613e0d82007-07-03 14:03:57 +00002007 // undef % X -> 0
2008 if (N0.getOpcode() == ISD::UNDEF)
2009 return DAG.getConstant(0, VT);
2010 // X % undef -> undef
2011 if (N1.getOpcode() == ISD::UNDEF)
2012 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002013
Dan Gohman475871a2008-07-27 21:46:04 +00002014 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002015}
2016
Dan Gohman475871a2008-07-27 21:46:04 +00002017SDValue DAGCombiner::visitUREM(SDNode *N) {
2018 SDValue N0 = N->getOperand(0);
2019 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002020 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2021 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002022 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002023
Nate Begeman1d4d4142005-09-01 00:19:25 +00002024 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002025 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002026 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002027 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002028 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002029 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002030 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002031 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2032 if (N1.getOpcode() == ISD::SHL) {
2033 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002034 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002035 SDValue Add =
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002036 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002037 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002038 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002039 AddToWorkList(Add.getNode());
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002040 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002041 }
2042 }
2043 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002044
Dan Gohman77003042007-11-26 23:46:11 +00002045 // If X/C can be simplified by the division-by-constant logic, lower
2046 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002047 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002048 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002049 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002050 SDValue OptimizedDiv = combine(Div.getNode());
2051 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002052 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
2053 OptimizedDiv, N1);
2054 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002055 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002056 return Sub;
2057 }
Chris Lattner26d29902006-10-12 20:58:32 +00002058 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002059
Dan Gohman613e0d82007-07-03 14:03:57 +00002060 // undef % X -> 0
2061 if (N0.getOpcode() == ISD::UNDEF)
2062 return DAG.getConstant(0, VT);
2063 // X % undef -> undef
2064 if (N1.getOpcode() == ISD::UNDEF)
2065 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002066
Dan Gohman475871a2008-07-27 21:46:04 +00002067 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002068}
2069
Dan Gohman475871a2008-07-27 21:46:04 +00002070SDValue DAGCombiner::visitMULHS(SDNode *N) {
2071 SDValue N0 = N->getOperand(0);
2072 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002073 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002074 EVT VT = N->getValueType(0);
Chris Lattnerde1c3602010-12-13 08:39:01 +00002075 DebugLoc DL = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00002076
Nate Begeman1d4d4142005-09-01 00:19:25 +00002077 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002078 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002079 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002080 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002081 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendling326411d2009-01-30 03:00:18 +00002082 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
2083 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002084 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002085 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002086 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002087 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002088
Chris Lattnerde1c3602010-12-13 08:39:01 +00002089 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2090 // plus a shift.
2091 if (VT.isSimple() && !VT.isVector()) {
2092 MVT Simple = VT.getSimpleVT();
2093 unsigned SimpleSize = Simple.getSizeInBits();
2094 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2095 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2096 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2097 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2098 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002099 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002100 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002101 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2102 }
2103 }
Owen Anderson95771af2011-02-25 21:41:48 +00002104
Dan Gohman475871a2008-07-27 21:46:04 +00002105 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002106}
2107
Dan Gohman475871a2008-07-27 21:46:04 +00002108SDValue DAGCombiner::visitMULHU(SDNode *N) {
2109 SDValue N0 = N->getOperand(0);
2110 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002111 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002112 EVT VT = N->getValueType(0);
Chris Lattnerde1c3602010-12-13 08:39:01 +00002113 DebugLoc DL = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00002114
Nate Begeman1d4d4142005-09-01 00:19:25 +00002115 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002116 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002117 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002118 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002119 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002120 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002121 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002122 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002123 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002124
Chris Lattnerde1c3602010-12-13 08:39:01 +00002125 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2126 // plus a shift.
2127 if (VT.isSimple() && !VT.isVector()) {
2128 MVT Simple = VT.getSimpleVT();
2129 unsigned SimpleSize = Simple.getSizeInBits();
2130 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2131 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2132 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2133 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2134 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2135 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002136 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002137 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2138 }
2139 }
Owen Anderson95771af2011-02-25 21:41:48 +00002140
Dan Gohman475871a2008-07-27 21:46:04 +00002141 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002142}
2143
Dan Gohman389079b2007-10-08 17:57:15 +00002144/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2145/// compute two values. LoOp and HiOp give the opcodes for the two computations
2146/// that are being performed. Return true if a simplification was made.
2147///
Scott Michelfdc40a02009-02-17 22:15:04 +00002148SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002149 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002150 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002151 bool HiExists = N->hasAnyUseOfValue(1);
2152 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002153 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002154 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00002155 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
2156 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002157 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002158 }
2159
2160 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002161 bool LoExists = N->hasAnyUseOfValue(0);
2162 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002163 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002164 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00002165 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
2166 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002167 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002168 }
2169
Evan Cheng44711942007-11-08 09:25:29 +00002170 // If both halves are used, return as it is.
2171 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002172 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002173
2174 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002175 if (LoExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00002176 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
2177 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002178 AddToWorkList(Lo.getNode());
2179 SDValue LoOpt = combine(Lo.getNode());
2180 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002181 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002182 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002183 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002184 }
2185
Evan Cheng44711942007-11-08 09:25:29 +00002186 if (HiExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00002187 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002188 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002189 AddToWorkList(Hi.getNode());
2190 SDValue HiOpt = combine(Hi.getNode());
2191 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002192 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002193 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002194 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002195 }
Bill Wendling826d1142009-01-30 03:08:40 +00002196
Dan Gohman475871a2008-07-27 21:46:04 +00002197 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002198}
2199
Dan Gohman475871a2008-07-27 21:46:04 +00002200SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2201 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002202 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002203
Chris Lattner33e77d32010-12-15 06:04:19 +00002204 EVT VT = N->getValueType(0);
2205 DebugLoc DL = N->getDebugLoc();
2206
2207 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2208 // plus a shift.
2209 if (VT.isSimple() && !VT.isVector()) {
2210 MVT Simple = VT.getSimpleVT();
2211 unsigned SimpleSize = Simple.getSizeInBits();
2212 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2213 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2214 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2215 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2216 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2217 // Compute the high part as N1.
2218 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002219 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002220 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2221 // Compute the low part as N0.
2222 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2223 return CombineTo(N, Lo, Hi);
2224 }
2225 }
Owen Anderson95771af2011-02-25 21:41:48 +00002226
Dan Gohman475871a2008-07-27 21:46:04 +00002227 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002228}
2229
Dan Gohman475871a2008-07-27 21:46:04 +00002230SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2231 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002232 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002233
Chris Lattner33e77d32010-12-15 06:04:19 +00002234 EVT VT = N->getValueType(0);
2235 DebugLoc DL = N->getDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002236
Chris Lattner33e77d32010-12-15 06:04:19 +00002237 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2238 // plus a shift.
2239 if (VT.isSimple() && !VT.isVector()) {
2240 MVT Simple = VT.getSimpleVT();
2241 unsigned SimpleSize = Simple.getSizeInBits();
2242 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2243 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2244 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2245 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2246 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2247 // Compute the high part as N1.
2248 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002249 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002250 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2251 // Compute the low part as N0.
2252 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2253 return CombineTo(N, Lo, Hi);
2254 }
2255 }
Owen Anderson95771af2011-02-25 21:41:48 +00002256
Dan Gohman475871a2008-07-27 21:46:04 +00002257 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002258}
2259
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002260SDValue DAGCombiner::visitSMULO(SDNode *N) {
2261 // (smulo x, 2) -> (saddo x, x)
2262 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2263 if (C2->getAPIntValue() == 2)
2264 return DAG.getNode(ISD::SADDO, N->getDebugLoc(), N->getVTList(),
2265 N->getOperand(0), N->getOperand(0));
2266
2267 return SDValue();
2268}
2269
2270SDValue DAGCombiner::visitUMULO(SDNode *N) {
2271 // (umulo x, 2) -> (uaddo x, x)
2272 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2273 if (C2->getAPIntValue() == 2)
2274 return DAG.getNode(ISD::UADDO, N->getDebugLoc(), N->getVTList(),
2275 N->getOperand(0), N->getOperand(0));
2276
2277 return SDValue();
2278}
2279
Dan Gohman475871a2008-07-27 21:46:04 +00002280SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2281 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002282 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002283
Dan Gohman475871a2008-07-27 21:46:04 +00002284 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002285}
2286
Dan Gohman475871a2008-07-27 21:46:04 +00002287SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2288 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002289 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002290
Dan Gohman475871a2008-07-27 21:46:04 +00002291 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002292}
2293
Chris Lattner35e5c142006-05-05 05:51:50 +00002294/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2295/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002296SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2297 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002298 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002299 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002300
Dan Gohmanff00a552010-01-14 03:08:49 +00002301 // Bail early if none of these transforms apply.
2302 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2303
Chris Lattner540121f2006-05-05 06:31:05 +00002304 // For each of OP in AND/OR/XOR:
2305 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2306 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2307 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002308 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002309 //
2310 // do not sink logical op inside of a vector extend, since it may combine
2311 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002312 EVT Op0VT = N0.getOperand(0).getValueType();
2313 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002314 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002315 // Avoid infinite looping with PromoteIntBinOp.
2316 (N0.getOpcode() == ISD::ANY_EXTEND &&
2317 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002318 (N0.getOpcode() == ISD::TRUNCATE &&
2319 (!TLI.isZExtFree(VT, Op0VT) ||
2320 !TLI.isTruncateFree(Op0VT, VT)) &&
2321 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002322 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002323 Op0VT == N1.getOperand(0).getValueType() &&
2324 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002325 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2326 N0.getOperand(0).getValueType(),
2327 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002328 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002329 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002330 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002331
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002332 // For each of OP in SHL/SRL/SRA/AND...
2333 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2334 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2335 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002336 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002337 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002338 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002339 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2340 N0.getOperand(0).getValueType(),
2341 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002342 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002343 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
2344 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002345 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002346
Nadav Rotem4ac90812012-04-01 19:31:22 +00002347 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2348 // Only perform this optimization after type legalization and before
2349 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2350 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2351 // we don't want to undo this promotion.
2352 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2353 // on scalars.
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002354 if ((N0.getOpcode() == ISD::BITCAST ||
2355 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) &&
2356 Level == AfterLegalizeTypes) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002357 SDValue In0 = N0.getOperand(0);
2358 SDValue In1 = N1.getOperand(0);
2359 EVT In0Ty = In0.getValueType();
2360 EVT In1Ty = In1.getValueType();
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002361 DebugLoc DL = N->getDebugLoc();
2362 // If both incoming values are integers, and the original types are the
2363 // same.
Nadav Rotem4ac90812012-04-01 19:31:22 +00002364 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002365 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1);
2366 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002367 AddToWorkList(Op.getNode());
2368 return BC;
2369 }
2370 }
2371
2372 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2373 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2374 // If both shuffles use the same mask, and both shuffle within a single
2375 // vector, then it is worthwhile to move the swizzle after the operation.
2376 // The type-legalizer generates this pattern when loading illegal
2377 // vector types from memory. In many cases this allows additional shuffle
2378 // optimizations.
Craig Topperf9204232012-04-09 07:19:09 +00002379 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2380 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2381 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002382 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2383 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topperf9204232012-04-09 07:19:09 +00002384
2385 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2386 "Inputs to shuffles are not the same type");
Nadav Rotem4ac90812012-04-01 19:31:22 +00002387
2388 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotem4ac90812012-04-01 19:31:22 +00002389
2390 // Check that both shuffles use the same mask. The masks are known to be of
2391 // the same length because the result vector type is the same.
2392 bool SameMask = true;
2393 for (unsigned i = 0; i != NumElts; ++i) {
2394 int Idx0 = SVN0->getMaskElt(i);
2395 int Idx1 = SVN1->getMaskElt(i);
2396 if (Idx0 != Idx1) {
2397 SameMask = false;
2398 break;
2399 }
2400 }
2401
Craig Topperf9204232012-04-09 07:19:09 +00002402 if (SameMask) {
2403 SDValue Op = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
2404 N0.getOperand(0), N1.getOperand(0));
Nadav Rotem4ac90812012-04-01 19:31:22 +00002405 AddToWorkList(Op.getNode());
Craig Topperf9204232012-04-09 07:19:09 +00002406 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Op,
2407 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002408 }
2409 }
Craig Topperf9204232012-04-09 07:19:09 +00002410
Dan Gohman475871a2008-07-27 21:46:04 +00002411 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002412}
2413
Dan Gohman475871a2008-07-27 21:46:04 +00002414SDValue DAGCombiner::visitAND(SDNode *N) {
2415 SDValue N0 = N->getOperand(0);
2416 SDValue N1 = N->getOperand(1);
2417 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002418 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2419 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002420 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002421 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002422
Dan Gohman7f321562007-06-25 16:23:39 +00002423 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002424 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002425 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002426 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002427 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002428
Dan Gohman613e0d82007-07-03 14:03:57 +00002429 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002430 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002431 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002432 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002433 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002434 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002435 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002436 if (N0C && !N1C)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00002437 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002438 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002439 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002440 return N0;
2441 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002442 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002443 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002444 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002445 // reassociate and
Bill Wendling35247c32009-01-30 00:45:56 +00002446 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002447 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002448 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002449 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002450 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002451 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002452 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002453 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002454 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2455 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002456 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002457 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002458 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002459 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendling2627a882009-01-30 20:43:18 +00002460 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
2461 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002462
Chris Lattner1ec05d12006-03-01 21:47:21 +00002463 // Replace uses of the AND with uses of the Zero extend node.
2464 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002465
Chris Lattner3603cd62006-02-02 07:17:31 +00002466 // We actually want to replace all uses of the any_extend with the
2467 // zero_extend, to avoid duplicating things. This will later cause this
2468 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002469 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002470 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002471 }
2472 }
James Molloy6259dcd2012-02-20 12:02:38 +00002473 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
2474 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2475 // already be zero by virtue of the width of the base type of the load.
2476 //
2477 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2478 // more cases.
2479 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2480 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2481 N0.getOpcode() == ISD::LOAD) {
2482 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2483 N0 : N0.getOperand(0) );
2484
2485 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2486 // This can be a pure constant or a vector splat, in which case we treat the
2487 // vector as a scalar and use the splat value.
2488 APInt Constant = APInt::getNullValue(1);
2489 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2490 Constant = C->getAPIntValue();
2491 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2492 APInt SplatValue, SplatUndef;
2493 unsigned SplatBitSize;
2494 bool HasAnyUndefs;
2495 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2496 SplatBitSize, HasAnyUndefs);
2497 if (IsSplat) {
2498 // Undef bits can contribute to a possible optimisation if set, so
2499 // set them.
2500 SplatValue |= SplatUndef;
2501
2502 // The splat value may be something like "0x00FFFFFF", which means 0 for
2503 // the first vector value and FF for the rest, repeating. We need a mask
2504 // that will apply equally to all members of the vector, so AND all the
2505 // lanes of the constant together.
2506 EVT VT = Vector->getValueType(0);
2507 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002508
2509 // If the splat value has been compressed to a bitlength lower
2510 // than the size of the vector lane, we need to re-expand it to
2511 // the lane size.
2512 if (BitWidth > SplatBitSize)
2513 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2514 SplatBitSize < BitWidth;
2515 SplatBitSize = SplatBitSize * 2)
2516 SplatValue |= SplatValue.shl(SplatBitSize);
2517
James Molloy6259dcd2012-02-20 12:02:38 +00002518 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002519 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy6259dcd2012-02-20 12:02:38 +00002520 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2521 }
2522 }
2523
2524 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2525 // actually legal and isn't going to get expanded, else this is a false
2526 // optimisation.
2527 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2528 Load->getMemoryVT());
2529
2530 // Resize the constant to the same size as the original memory access before
2531 // extension. If it is still the AllOnesValue then this AND is completely
2532 // unneeded.
2533 Constant =
2534 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2535
2536 bool B;
2537 switch (Load->getExtensionType()) {
2538 default: B = false; break;
2539 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2540 case ISD::ZEXTLOAD:
2541 case ISD::NON_EXTLOAD: B = true; break;
2542 }
2543
2544 if (B && Constant.isAllOnesValue()) {
2545 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2546 // preserve semantics once we get rid of the AND.
2547 SDValue NewLoad(Load, 0);
2548 if (Load->getExtensionType() == ISD::EXTLOAD) {
2549 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
2550 Load->getValueType(0), Load->getDebugLoc(),
2551 Load->getChain(), Load->getBasePtr(),
2552 Load->getOffset(), Load->getMemoryVT(),
2553 Load->getMemOperand());
2554 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkeld65e4632012-06-20 15:42:48 +00002555 if (Load->getNumValues() == 3) {
2556 // PRE/POST_INC loads have 3 values.
2557 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2558 NewLoad.getValue(2) };
2559 CombineTo(Load, To, 3, true);
2560 } else {
2561 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2562 }
James Molloy6259dcd2012-02-20 12:02:38 +00002563 }
2564
2565 // Fold the AND away, taking care not to fold to the old load node if we
2566 // replaced it.
2567 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2568
2569 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2570 }
2571 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002572 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2573 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2574 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2575 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002576
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002577 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002578 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002579 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002580 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002581 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2582 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002583 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002584 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002585 }
Bill Wendling2627a882009-01-30 20:43:18 +00002586 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002587 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002588 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
2589 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002590 AddToWorkList(ANDNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002591 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002592 }
Bill Wendling2627a882009-01-30 20:43:18 +00002593 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002594 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendling2627a882009-01-30 20:43:18 +00002595 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2596 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002597 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002598 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002599 }
2600 }
2601 // canonicalize equivalent to ll == rl
2602 if (LL == RR && LR == RL) {
2603 Op1 = ISD::getSetCCSwappedOperands(Op1);
2604 std::swap(RL, RR);
2605 }
2606 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002607 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002608 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002609 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002610 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling2627a882009-01-30 20:43:18 +00002611 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2612 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002613 }
2614 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002615
Bill Wendling2627a882009-01-30 20:43:18 +00002616 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002617 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002618 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002619 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002620 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002621
Nate Begemande996292006-02-03 22:24:05 +00002622 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2623 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002624 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002625 SimplifyDemandedBits(SDValue(N, 0)))
2626 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002627
Nate Begemanded49632005-10-13 03:11:28 +00002628 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002629 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002630 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002631 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002632 // If we zero all the possible extended bits, then we can turn this into
2633 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002634 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002635 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002636 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002637 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002638 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00002639 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002640 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002641 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002642 LN0->isVolatile(), LN0->isNonTemporal(),
2643 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002644 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002645 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002646 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002647 }
2648 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002649 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002650 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002651 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002652 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002653 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002654 // If we zero all the possible extended bits, then we can turn this into
2655 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002656 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002657 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002658 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002659 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002660 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00002661 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002662 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002663 LN0->getBasePtr(), LN0->getPointerInfo(),
2664 MemVT,
David Greene1e559442010-02-15 17:00:31 +00002665 LN0->isVolatile(), LN0->isNonTemporal(),
2666 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002667 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002668 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002669 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002670 }
2671 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002672
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002673 // fold (and (load x), 255) -> (zextload x, i8)
2674 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002675 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2676 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2677 (N0.getOpcode() == ISD::ANY_EXTEND &&
2678 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2679 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2680 LoadSDNode *LN0 = HasAnyExt
2681 ? cast<LoadSDNode>(N0.getOperand(0))
2682 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002683 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerbd1fccf2010-01-07 21:59:23 +00002684 LN0->isUnindexed() && N0.hasOneUse() && LN0->hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002685 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002686 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2687 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2688 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002689
Evan Chengd40d03e2010-01-06 19:38:29 +00002690 if (ExtVT == LoadedVT &&
2691 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002692 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002693
2694 SDValue NewLoad =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002695 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002696 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002697 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002698 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2699 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002700 AddToWorkList(N);
2701 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2702 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2703 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002704
Chris Lattneref7634c2010-01-07 21:53:27 +00002705 // Do not change the width of a volatile load.
2706 // Do not generate loads of non-round integer types since these can
2707 // be expensive (and would be wrong if the type is not byte sized).
2708 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2709 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2710 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002711
Chris Lattneref7634c2010-01-07 21:53:27 +00002712 unsigned Alignment = LN0->getAlignment();
2713 SDValue NewPtr = LN0->getBasePtr();
2714
2715 // For big endian targets, we need to add an offset to the pointer
2716 // to load the correct bytes. For little endian systems, we merely
2717 // need to read fewer bytes from the same pointer.
2718 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002719 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2720 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2721 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Chris Lattneref7634c2010-01-07 21:53:27 +00002722 NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType,
2723 NewPtr, DAG.getConstant(PtrOff, PtrType));
2724 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002725 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002726
2727 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002728
Chris Lattneref7634c2010-01-07 21:53:27 +00002729 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2730 SDValue Load =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002731 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002732 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002733 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002734 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2735 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002736 AddToWorkList(N);
2737 CombineTo(LN0, Load, Load.getValue(1));
2738 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002739 }
Evan Cheng466685d2006-10-09 20:57:25 +00002740 }
Chris Lattner15045b62006-02-28 06:35:35 +00002741 }
2742 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002743
Evan Chenga9e13ba2012-07-17 18:54:11 +00002744 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2745 VT.getSizeInBits() <= 64) {
2746 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2747 APInt ADDC = ADDI->getAPIntValue();
2748 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2749 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2750 // immediate for an add, but it is legal if its top c2 bits are set,
2751 // transform the ADD so the immediate doesn't need to be materialized
2752 // in a register.
2753 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2754 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2755 SRLI->getZExtValue());
2756 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2757 ADDC |= Mask;
2758 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2759 SDValue NewAdd =
2760 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
2761 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2762 CombineTo(N0.getNode(), NewAdd);
2763 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2764 }
2765 }
2766 }
2767 }
2768 }
2769 }
2770
2771
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002772 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002773}
2774
Evan Cheng9568e5c2011-06-21 06:01:08 +00002775/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2776///
2777SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2778 bool DemandHighBits) {
2779 if (!LegalOperations)
2780 return SDValue();
2781
2782 EVT VT = N->getValueType(0);
2783 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2784 return SDValue();
2785 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2786 return SDValue();
2787
2788 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2789 bool LookPassAnd0 = false;
2790 bool LookPassAnd1 = false;
2791 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2792 std::swap(N0, N1);
2793 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2794 std::swap(N0, N1);
2795 if (N0.getOpcode() == ISD::AND) {
2796 if (!N0.getNode()->hasOneUse())
2797 return SDValue();
2798 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2799 if (!N01C || N01C->getZExtValue() != 0xFF00)
2800 return SDValue();
2801 N0 = N0.getOperand(0);
2802 LookPassAnd0 = true;
2803 }
2804
2805 if (N1.getOpcode() == ISD::AND) {
2806 if (!N1.getNode()->hasOneUse())
2807 return SDValue();
2808 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2809 if (!N11C || N11C->getZExtValue() != 0xFF)
2810 return SDValue();
2811 N1 = N1.getOperand(0);
2812 LookPassAnd1 = true;
2813 }
2814
2815 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2816 std::swap(N0, N1);
2817 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2818 return SDValue();
2819 if (!N0.getNode()->hasOneUse() ||
2820 !N1.getNode()->hasOneUse())
2821 return SDValue();
2822
2823 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2824 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2825 if (!N01C || !N11C)
2826 return SDValue();
2827 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2828 return SDValue();
2829
2830 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2831 SDValue N00 = N0->getOperand(0);
2832 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2833 if (!N00.getNode()->hasOneUse())
2834 return SDValue();
2835 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2836 if (!N001C || N001C->getZExtValue() != 0xFF)
2837 return SDValue();
2838 N00 = N00.getOperand(0);
2839 LookPassAnd0 = true;
2840 }
2841
2842 SDValue N10 = N1->getOperand(0);
2843 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2844 if (!N10.getNode()->hasOneUse())
2845 return SDValue();
2846 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2847 if (!N101C || N101C->getZExtValue() != 0xFF00)
2848 return SDValue();
2849 N10 = N10.getOperand(0);
2850 LookPassAnd1 = true;
2851 }
2852
2853 if (N00 != N10)
2854 return SDValue();
2855
2856 // Make sure everything beyond the low halfword is zero since the SRL 16
2857 // will clear the top bits.
2858 unsigned OpSizeInBits = VT.getSizeInBits();
2859 if (DemandHighBits && OpSizeInBits > 16 &&
2860 (!LookPassAnd0 || !LookPassAnd1) &&
2861 !DAG.MaskedValueIsZero(N10, APInt::getHighBitsSet(OpSizeInBits, 16)))
2862 return SDValue();
Eric Christopher7332e6e2011-07-14 01:12:15 +00002863
Evan Cheng9568e5c2011-06-21 06:01:08 +00002864 SDValue Res = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT, N00);
2865 if (OpSizeInBits > 16)
2866 Res = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Res,
2867 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2868 return Res;
2869}
2870
2871/// isBSwapHWordElement - Return true if the specified node is an element
2872/// that makes up a 32-bit packed halfword byteswap. i.e.
2873/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2874static bool isBSwapHWordElement(SDValue N, SmallVector<SDNode*,4> &Parts) {
2875 if (!N.getNode()->hasOneUse())
2876 return false;
2877
2878 unsigned Opc = N.getOpcode();
2879 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
2880 return false;
2881
2882 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2883 if (!N1C)
2884 return false;
2885
2886 unsigned Num;
2887 switch (N1C->getZExtValue()) {
2888 default:
2889 return false;
2890 case 0xFF: Num = 0; break;
2891 case 0xFF00: Num = 1; break;
2892 case 0xFF0000: Num = 2; break;
2893 case 0xFF000000: Num = 3; break;
2894 }
2895
2896 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
2897 SDValue N0 = N.getOperand(0);
2898 if (Opc == ISD::AND) {
2899 if (Num == 0 || Num == 2) {
2900 // (x >> 8) & 0xff
2901 // (x >> 8) & 0xff0000
2902 if (N0.getOpcode() != ISD::SRL)
2903 return false;
2904 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2905 if (!C || C->getZExtValue() != 8)
2906 return false;
2907 } else {
2908 // (x << 8) & 0xff00
2909 // (x << 8) & 0xff000000
2910 if (N0.getOpcode() != ISD::SHL)
2911 return false;
2912 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2913 if (!C || C->getZExtValue() != 8)
2914 return false;
2915 }
2916 } else if (Opc == ISD::SHL) {
2917 // (x & 0xff) << 8
2918 // (x & 0xff0000) << 8
2919 if (Num != 0 && Num != 2)
2920 return false;
2921 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2922 if (!C || C->getZExtValue() != 8)
2923 return false;
2924 } else { // Opc == ISD::SRL
2925 // (x & 0xff00) >> 8
2926 // (x & 0xff000000) >> 8
2927 if (Num != 1 && Num != 3)
2928 return false;
2929 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2930 if (!C || C->getZExtValue() != 8)
2931 return false;
2932 }
2933
2934 if (Parts[Num])
2935 return false;
2936
2937 Parts[Num] = N0.getOperand(0).getNode();
2938 return true;
2939}
2940
2941/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
2942/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2943/// => (rotl (bswap x), 16)
2944SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
2945 if (!LegalOperations)
2946 return SDValue();
2947
2948 EVT VT = N->getValueType(0);
2949 if (VT != MVT::i32)
2950 return SDValue();
2951 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2952 return SDValue();
2953
2954 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
2955 // Look for either
2956 // (or (or (and), (and)), (or (and), (and)))
2957 // (or (or (or (and), (and)), (and)), (and))
2958 if (N0.getOpcode() != ISD::OR)
2959 return SDValue();
2960 SDValue N00 = N0.getOperand(0);
2961 SDValue N01 = N0.getOperand(1);
2962
2963 if (N1.getOpcode() == ISD::OR) {
2964 // (or (or (and), (and)), (or (and), (and)))
2965 SDValue N000 = N00.getOperand(0);
2966 if (!isBSwapHWordElement(N000, Parts))
2967 return SDValue();
2968
2969 SDValue N001 = N00.getOperand(1);
2970 if (!isBSwapHWordElement(N001, Parts))
2971 return SDValue();
2972 SDValue N010 = N01.getOperand(0);
2973 if (!isBSwapHWordElement(N010, Parts))
2974 return SDValue();
2975 SDValue N011 = N01.getOperand(1);
2976 if (!isBSwapHWordElement(N011, Parts))
2977 return SDValue();
2978 } else {
2979 // (or (or (or (and), (and)), (and)), (and))
2980 if (!isBSwapHWordElement(N1, Parts))
2981 return SDValue();
2982 if (!isBSwapHWordElement(N01, Parts))
2983 return SDValue();
2984 if (N00.getOpcode() != ISD::OR)
2985 return SDValue();
2986 SDValue N000 = N00.getOperand(0);
2987 if (!isBSwapHWordElement(N000, Parts))
2988 return SDValue();
2989 SDValue N001 = N00.getOperand(1);
2990 if (!isBSwapHWordElement(N001, Parts))
2991 return SDValue();
2992 }
2993
2994 // Make sure the parts are all coming from the same node.
2995 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
2996 return SDValue();
2997
2998 SDValue BSwap = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT,
2999 SDValue(Parts[0],0));
3000
3001 // Result of the bswap should be rotated by 16. If it's not legal, than
3002 // do (x << 16) | (x >> 16).
3003 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
3004 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
3005 return DAG.getNode(ISD::ROTL, N->getDebugLoc(), VT, BSwap, ShAmt);
3006 else if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
3007 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, BSwap, ShAmt);
3008 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT,
3009 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, BSwap, ShAmt),
3010 DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, BSwap, ShAmt));
3011}
3012
Dan Gohman475871a2008-07-27 21:46:04 +00003013SDValue DAGCombiner::visitOR(SDNode *N) {
3014 SDValue N0 = N->getOperand(0);
3015 SDValue N1 = N->getOperand(1);
3016 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00003017 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3018 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003019 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003020
Dan Gohman7f321562007-06-25 16:23:39 +00003021 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003022 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003023 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003024 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003025 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003026
Dan Gohman613e0d82007-07-03 14:03:57 +00003027 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00003028 if (!LegalOperations &&
3029 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00003030 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3031 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3032 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003033 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003034 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003035 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003036 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003037 if (N0C && !N1C)
Bill Wendling09025642009-01-30 20:59:34 +00003038 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003039 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003040 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003041 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003042 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003043 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003044 return N1;
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003045 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003046 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003047 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00003048
3049 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3050 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3051 if (BSwap.getNode() != 0)
3052 return BSwap;
3053 BSwap = MatchBSwapHWordLow(N, N0, N1);
3054 if (BSwap.getNode() != 0)
3055 return BSwap;
3056
Nate Begemancd4d58c2006-02-03 06:46:56 +00003057 // reassociate or
Bill Wendling35247c32009-01-30 00:45:56 +00003058 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003059 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003060 return ROR;
3061 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003062 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00003063 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00003064 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00003065 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00003066 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003067 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3068 DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
3069 N0.getOperand(0), N1),
3070 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00003071 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003072 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3073 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3074 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3075 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003076
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003077 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003078 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00003079 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3080 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00003081 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003082 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling09025642009-01-30 20:59:34 +00003083 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
3084 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003085 AddToWorkList(ORNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00003086 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003087 }
Bill Wendling09025642009-01-30 20:59:34 +00003088 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3089 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00003090 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003091 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling09025642009-01-30 20:59:34 +00003092 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
3093 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003094 AddToWorkList(ANDNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00003095 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003096 }
3097 }
3098 // canonicalize equivalent to ll == rl
3099 if (LL == RR && LR == RL) {
3100 Op1 = ISD::getSetCCSwappedOperands(Op1);
3101 std::swap(RL, RR);
3102 }
3103 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003104 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003105 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00003106 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003107 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling09025642009-01-30 20:59:34 +00003108 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
3109 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003110 }
3111 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003112
Bill Wendling09025642009-01-30 20:59:34 +00003113 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00003114 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003115 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003116 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003117 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003118
Bill Wendling09025642009-01-30 20:59:34 +00003119 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00003120 if (N0.getOpcode() == ISD::AND &&
3121 N1.getOpcode() == ISD::AND &&
3122 N0.getOperand(1).getOpcode() == ISD::Constant &&
3123 N1.getOperand(1).getOpcode() == ISD::Constant &&
3124 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003125 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00003126 // We can only do this xform if we know that bits from X that are set in C2
3127 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003128 const APInt &LHSMask =
3129 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3130 const APInt &RHSMask =
3131 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003132
Dan Gohmanea859be2007-06-22 14:59:07 +00003133 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3134 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling09025642009-01-30 20:59:34 +00003135 SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
3136 N0.getOperand(0), N1.getOperand(0));
3137 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X,
3138 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00003139 }
3140 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003141
Chris Lattner516b9622006-09-14 20:50:57 +00003142 // See if this is some rotate idiom.
Bill Wendling317bd702009-01-30 21:14:50 +00003143 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman475871a2008-07-27 21:46:04 +00003144 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00003145
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003146 // Simplify the operands using demanded-bits information.
3147 if (!VT.isVector() &&
3148 SimplifyDemandedBits(SDValue(N, 0)))
3149 return SDValue(N, 0);
3150
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003151 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003152}
3153
Chris Lattner516b9622006-09-14 20:50:57 +00003154/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003155static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00003156 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00003157 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00003158 Mask = Op.getOperand(1);
3159 Op = Op.getOperand(0);
3160 } else {
3161 return false;
3162 }
3163 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003164
Chris Lattner516b9622006-09-14 20:50:57 +00003165 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3166 Shift = Op;
3167 return true;
3168 }
Bill Wendling09025642009-01-30 20:59:34 +00003169
Scott Michelfdc40a02009-02-17 22:15:04 +00003170 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00003171}
3172
Chris Lattner516b9622006-09-14 20:50:57 +00003173// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3174// idioms for rotate, and if the target supports rotation instructions, generate
3175// a rot[lr].
Bill Wendling317bd702009-01-30 21:14:50 +00003176SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003177 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00003178 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00003179 if (!TLI.isTypeLegal(VT)) return 0;
3180
3181 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003182 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3183 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00003184 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003185
Chris Lattner516b9622006-09-14 20:50:57 +00003186 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003187 SDValue LHSShift; // The shift.
3188 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003189 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3190 return 0; // Not part of a rotate.
3191
Dan Gohman475871a2008-07-27 21:46:04 +00003192 SDValue RHSShift; // The shift.
3193 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003194 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3195 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00003196
Chris Lattner516b9622006-09-14 20:50:57 +00003197 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3198 return 0; // Not shifting the same value.
3199
3200 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3201 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00003202
Chris Lattner516b9622006-09-14 20:50:57 +00003203 // Canonicalize shl to left side in a shl/srl pair.
3204 if (RHSShift.getOpcode() == ISD::SHL) {
3205 std::swap(LHS, RHS);
3206 std::swap(LHSShift, RHSShift);
3207 std::swap(LHSMask , RHSMask );
3208 }
3209
Duncan Sands83ec4b62008-06-06 12:08:01 +00003210 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003211 SDValue LHSShiftArg = LHSShift.getOperand(0);
3212 SDValue LHSShiftAmt = LHSShift.getOperand(1);
3213 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003214
3215 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3216 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003217 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3218 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003219 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3220 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003221 if ((LShVal + RShVal) != OpSizeInBits)
3222 return 0;
3223
Dan Gohman475871a2008-07-27 21:46:04 +00003224 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00003225 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00003226 Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00003227 else
Bill Wendling317bd702009-01-30 21:14:50 +00003228 Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003229
Chris Lattner516b9622006-09-14 20:50:57 +00003230 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003231 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003232 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003233
Gabor Greifba36cb52008-08-28 21:40:38 +00003234 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003235 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3236 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003237 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003238 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003239 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3240 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003241 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003242
Bill Wendling317bd702009-01-30 21:14:50 +00003243 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003244 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003245
Gabor Greifba36cb52008-08-28 21:40:38 +00003246 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003247 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003248
Chris Lattner516b9622006-09-14 20:50:57 +00003249 // If there is a mask here, and we have a variable shift, we can't be sure
3250 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003251 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003252 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003253
Chris Lattner516b9622006-09-14 20:50:57 +00003254 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
3255 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003256 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
3257 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003258 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003259 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003260 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00003261 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00003262 return DAG.getNode(ISD::ROTL, DL, VT,
3263 LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003264 else
Bill Wendling317bd702009-01-30 21:14:50 +00003265 return DAG.getNode(ISD::ROTR, DL, VT,
3266 LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003267 }
Chris Lattner516b9622006-09-14 20:50:57 +00003268 }
3269 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003270
Chris Lattner516b9622006-09-14 20:50:57 +00003271 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
3272 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003273 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
3274 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003275 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003276 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003277 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00003278 if (HasROTR)
Bill Wendling317bd702009-01-30 21:14:50 +00003279 return DAG.getNode(ISD::ROTR, DL, VT,
3280 LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00003281 else
Bill Wendling317bd702009-01-30 21:14:50 +00003282 return DAG.getNode(ISD::ROTL, DL, VT,
3283 LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003284 }
Scott Michelc9dc1142007-04-02 21:36:32 +00003285 }
3286 }
3287
Dan Gohman74feef22008-10-17 01:23:35 +00003288 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00003289 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
3290 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00003291 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
3292 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00003293 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
3294 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00003295 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
3296 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003297 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
3298 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00003299 if (RExtOp0.getOpcode() == ISD::SUB &&
3300 RExtOp0.getOperand(1) == LExtOp0) {
3301 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003302 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00003303 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003304 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003305 if (ConstantSDNode *SUBC =
3306 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003307 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003308 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3309 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00003310 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003311 }
3312 }
3313 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3314 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003315 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003316 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00003317 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003318 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003319 if (ConstantSDNode *SUBC =
3320 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003321 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003322 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
3323 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00003324 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003325 }
3326 }
Chris Lattner516b9622006-09-14 20:50:57 +00003327 }
3328 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003329
Chris Lattner516b9622006-09-14 20:50:57 +00003330 return 0;
3331}
3332
Dan Gohman475871a2008-07-27 21:46:04 +00003333SDValue DAGCombiner::visitXOR(SDNode *N) {
3334 SDValue N0 = N->getOperand(0);
3335 SDValue N1 = N->getOperand(1);
3336 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003337 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3338 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003339 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003340
Dan Gohman7f321562007-06-25 16:23:39 +00003341 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003342 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003343 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003344 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003345 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003346
Evan Cheng26471c42008-03-25 20:08:07 +00003347 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3348 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3349 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003350 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003351 if (N0.getOpcode() == ISD::UNDEF)
3352 return N0;
3353 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003354 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003355 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003356 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003357 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003358 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003359 if (N0C && !N1C)
Bill Wendling317bd702009-01-30 21:14:50 +00003360 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003361 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003362 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003363 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003364 // reassociate xor
Bill Wendling35247c32009-01-30 00:45:56 +00003365 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003366 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003367 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003368
Nate Begeman1d4d4142005-09-01 00:19:25 +00003369 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003370 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003371 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003372 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3373 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003374
Duncan Sands25cf2272008-11-24 14:53:14 +00003375 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003376 switch (N0.getOpcode()) {
3377 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003378 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003379 case ISD::SETCC:
Bill Wendling317bd702009-01-30 21:14:50 +00003380 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003381 case ISD::SELECT_CC:
Bill Wendling317bd702009-01-30 21:14:50 +00003382 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003383 N0.getOperand(3), NotCC);
3384 }
3385 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003386 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003387
Chris Lattner61c5ff42007-09-10 21:39:07 +00003388 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003389 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003390 N0.getNode()->hasOneUse() &&
3391 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003392 SDValue V = N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003393 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003394 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003395 AddToWorkList(V.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003396 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003397 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003398
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003399 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003400 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003401 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003402 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003403 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3404 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00003405 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
3406 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003407 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003408 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003409 }
3410 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003411 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003412 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003413 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003414 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003415 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3416 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00003417 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
3418 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003419 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003420 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003421 }
3422 }
Bill Wendling317bd702009-01-30 21:14:50 +00003423 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003424 if (N1C && N0.getOpcode() == ISD::XOR) {
3425 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3426 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3427 if (N00C)
Bill Wendling317bd702009-01-30 21:14:50 +00003428 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
3429 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003430 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003431 if (N01C)
Bill Wendling317bd702009-01-30 21:14:50 +00003432 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
3433 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003434 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003435 }
3436 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003437 if (N0 == N1)
3438 return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations);
Scott Michelfdc40a02009-02-17 22:15:04 +00003439
Chris Lattner35e5c142006-05-05 05:51:50 +00003440 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3441 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003442 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003443 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003444 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003445
Chris Lattner3e104b12006-04-08 04:15:24 +00003446 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003447 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003448 SimplifyDemandedBits(SDValue(N, 0)))
3449 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003450
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003451 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003452}
3453
Chris Lattnere70da202007-12-06 07:33:36 +00003454/// visitShiftByConstant - Handle transforms common to the three shifts, when
3455/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003456SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003457 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003458 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003459
Chris Lattnere70da202007-12-06 07:33:36 +00003460 // We want to pull some binops through shifts, so that we have (and (shift))
3461 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3462 // thing happens with address calculations, so it's important to canonicalize
3463 // it.
3464 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003465
Chris Lattnere70da202007-12-06 07:33:36 +00003466 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003467 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003468 case ISD::OR:
3469 case ISD::XOR:
3470 HighBitSet = false; // We can only transform sra if the high bit is clear.
3471 break;
3472 case ISD::AND:
3473 HighBitSet = true; // We can only transform sra if the high bit is set.
3474 break;
3475 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003476 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003477 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003478 HighBitSet = false; // We can only transform sra if the high bit is clear.
3479 break;
3480 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003481
Chris Lattnere70da202007-12-06 07:33:36 +00003482 // We require the RHS of the binop to be a constant as well.
3483 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003484 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003485
3486 // FIXME: disable this unless the input to the binop is a shift by a constant.
3487 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003488 //
Bill Wendling88103372009-01-30 21:37:17 +00003489 // void foo(int *X, int i) { X[i & 1235] = 1; }
3490 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003491 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003492 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003493 BinOpLHSVal->getOpcode() != ISD::SRA &&
3494 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3495 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003496 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003497
Owen Andersone50ed302009-08-10 22:56:29 +00003498 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003499
Bill Wendling88103372009-01-30 21:37:17 +00003500 // If this is a signed shift right, and the high bit is modified by the
3501 // logical operation, do not perform the transformation. The highBitSet
3502 // boolean indicates the value of the high bit of the constant which would
3503 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003504 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003505 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3506 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003507 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003508 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003509
Chris Lattnere70da202007-12-06 07:33:36 +00003510 // Fold the constants, shifting the binop RHS by the shift amount.
Bill Wendling88103372009-01-30 21:37:17 +00003511 SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(),
3512 N->getValueType(0),
3513 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003514
3515 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003516 SDValue NewShift = DAG.getNode(N->getOpcode(),
3517 LHS->getOperand(0).getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003518 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003519
3520 // Create the new binop.
Bill Wendling88103372009-01-30 21:37:17 +00003521 return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003522}
3523
Dan Gohman475871a2008-07-27 21:46:04 +00003524SDValue DAGCombiner::visitSHL(SDNode *N) {
3525 SDValue N0 = N->getOperand(0);
3526 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003527 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3528 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003529 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003530 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003531
Nate Begeman1d4d4142005-09-01 00:19:25 +00003532 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003533 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003534 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003535 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003536 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003537 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003538 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003539 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003540 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003541 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003542 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003543 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003544 // fold (shl undef, x) -> 0
3545 if (N0.getOpcode() == ISD::UNDEF)
3546 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003547 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003548 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003549 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003550 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003551 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003552 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003553 N1.getOperand(0).getOpcode() == ISD::AND &&
3554 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003555 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003556 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003557 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003558 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003559 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003560 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003561 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003562 DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT,
3563 DAG.getNode(ISD::TRUNCATE,
3564 N->getDebugLoc(),
3565 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003566 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003567 }
3568 }
3569
Dan Gohman475871a2008-07-27 21:46:04 +00003570 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3571 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003572
3573 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003574 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003575 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003576 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3577 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003578 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003579 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00003580 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003581 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003582 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003583
3584 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3585 // For this to be valid, the second form must not preserve any of the bits
3586 // that are shifted out by the inner shift in the first form. This means
3587 // the outer shift size must be >= the number of bits added by the ext.
3588 // As a corollary, we don't care what kind of ext it is.
3589 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3590 N0.getOpcode() == ISD::ANY_EXTEND ||
3591 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3592 N0.getOperand(0).getOpcode() == ISD::SHL &&
3593 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003594 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003595 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3596 uint64_t c2 = N1C->getZExtValue();
3597 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3598 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3599 if (c2 >= OpSizeInBits - InnerShiftSize) {
3600 if (c1 + c2 >= OpSizeInBits)
3601 return DAG.getConstant(0, VT);
3602 return DAG.getNode(ISD::SHL, N0->getDebugLoc(), VT,
3603 DAG.getNode(N0.getOpcode(), N0->getDebugLoc(), VT,
3604 N0.getOperand(0)->getOperand(0)),
3605 DAG.getConstant(c1 + c2, N1.getValueType()));
3606 }
3607 }
3608
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003609 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3610 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003611 // Only fold this if the inner shift has no other uses -- if it does, folding
3612 // this will increase the total number of instructions.
3613 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003614 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003615 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003616 if (c1 < VT.getSizeInBits()) {
3617 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003618 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3619 VT.getSizeInBits() - c1);
3620 SDValue Shift;
3621 if (c2 > c1) {
3622 Mask = Mask.shl(c2-c1);
3623 Shift = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
3624 DAG.getConstant(c2-c1, N1.getValueType()));
3625 } else {
3626 Mask = Mask.lshr(c1-c2);
3627 Shift = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
3628 DAG.getConstant(c1-c2, N1.getValueType()));
3629 }
3630 return DAG.getNode(ISD::AND, N0.getDebugLoc(), VT, Shift,
3631 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003632 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003633 }
Bill Wendling88103372009-01-30 21:37:17 +00003634 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003635 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3636 SDValue HiBitsMask =
3637 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3638 VT.getSizeInBits() -
3639 N1C->getZExtValue()),
3640 VT);
Bill Wendling88103372009-01-30 21:37:17 +00003641 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003642 HiBitsMask);
3643 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003644
Evan Chenge5b51ac2010-04-17 06:13:15 +00003645 if (N1C) {
3646 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3647 if (NewSHL.getNode())
3648 return NewSHL;
3649 }
3650
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003651 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003652}
3653
Dan Gohman475871a2008-07-27 21:46:04 +00003654SDValue DAGCombiner::visitSRA(SDNode *N) {
3655 SDValue N0 = N->getOperand(0);
3656 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003657 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3658 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003659 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003660 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003661
Bill Wendling88103372009-01-30 21:37:17 +00003662 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003663 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003664 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003665 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003666 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003667 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003668 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003669 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003670 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003671 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003672 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003673 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003674 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003675 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003676 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003677 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3678 // sext_inreg.
3679 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003680 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003681 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3682 if (VT.isVector())
3683 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3684 ExtVT, VT.getVectorNumElements());
3685 if ((!LegalOperations ||
3686 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Bill Wendling88103372009-01-30 21:37:17 +00003687 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003688 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003689 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003690
Bill Wendling88103372009-01-30 21:37:17 +00003691 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003692 if (N1C && N0.getOpcode() == ISD::SRA) {
3693 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003694 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003695 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Bill Wendling88103372009-01-30 21:37:17 +00003696 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003697 DAG.getConstant(Sum, N1C->getValueType(0)));
3698 }
3699 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003700
Bill Wendling88103372009-01-30 21:37:17 +00003701 // fold (sra (shl X, m), (sub result_size, n))
3702 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003703 // result_size - n != m.
3704 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003705 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003706 if (N0.getOpcode() == ISD::SHL) {
3707 // Get the two constanst of the shifts, CN0 = m, CN = n.
3708 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3709 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003710 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003711 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003712 EVT::getIntegerVT(*DAG.getContext(),
3713 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003714 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003715 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003716
Scott Michelfdc40a02009-02-17 22:15:04 +00003717 // If the shift is not a no-op (in which case this should be just a sign
3718 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003719 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003720 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003721 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003722 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3723 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003724 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003725
Owen Anderson95771af2011-02-25 21:41:48 +00003726 SDValue Amt = DAG.getConstant(ShiftAmt,
3727 getShiftAmountTy(N0.getOperand(0).getValueType()));
Bill Wendling88103372009-01-30 21:37:17 +00003728 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT,
3729 N0.getOperand(0), Amt);
3730 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT,
3731 Shift);
3732 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(),
3733 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003734 }
3735 }
3736 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003737
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003738 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003739 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003740 N1.getOperand(0).getOpcode() == ISD::AND &&
3741 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003742 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003743 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003744 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003745 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003746 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003747 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003748 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003749 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003750 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003751 DAG.getNode(ISD::TRUNCATE,
3752 N->getDebugLoc(),
3753 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003754 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003755 }
3756 }
3757
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003758 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3759 // if c1 is equal to the number of bits the trunc removes
3760 if (N0.getOpcode() == ISD::TRUNCATE &&
3761 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3762 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3763 N0.getOperand(0).hasOneUse() &&
3764 N0.getOperand(0).getOperand(1).hasOneUse() &&
3765 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3766 EVT LargeVT = N0.getOperand(0).getValueType();
3767 ConstantSDNode *LargeShiftAmt =
3768 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3769
3770 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3771 LargeShiftAmt->getZExtValue()) {
3772 SDValue Amt =
3773 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003774 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003775 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), LargeVT,
3776 N0.getOperand(0).getOperand(0), Amt);
3777 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, SRA);
3778 }
3779 }
3780
Scott Michelfdc40a02009-02-17 22:15:04 +00003781 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003782 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3783 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003784
3785
Nate Begeman1d4d4142005-09-01 00:19:25 +00003786 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003787 if (DAG.SignBitIsZero(N0))
Bill Wendling88103372009-01-30 21:37:17 +00003788 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003789
Evan Chenge5b51ac2010-04-17 06:13:15 +00003790 if (N1C) {
3791 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3792 if (NewSRA.getNode())
3793 return NewSRA;
3794 }
3795
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003796 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003797}
3798
Dan Gohman475871a2008-07-27 21:46:04 +00003799SDValue DAGCombiner::visitSRL(SDNode *N) {
3800 SDValue N0 = N->getOperand(0);
3801 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003802 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3803 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003804 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003805 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003806
Nate Begeman1d4d4142005-09-01 00:19:25 +00003807 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003808 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003809 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003810 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003811 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003812 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003813 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003814 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003815 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003816 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003817 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003818 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003819 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003820 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003821 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003822 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003823
Bill Wendling88103372009-01-30 21:37:17 +00003824 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003825 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003826 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003827 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3828 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003829 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003830 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00003831 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003832 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003833 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003834
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003835 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003836 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
3837 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003838 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003839 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003840 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3841 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003842 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3843 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003844 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003845 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003846 if (c1 + OpSizeInBits == InnerShiftSize) {
3847 if (c1 + c2 >= InnerShiftSize)
3848 return DAG.getConstant(0, VT);
3849 return DAG.getNode(ISD::TRUNCATE, N0->getDebugLoc(), VT,
Owen Anderson95771af2011-02-25 21:41:48 +00003850 DAG.getNode(ISD::SRL, N0->getDebugLoc(), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003851 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003852 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003853 }
3854 }
3855
Chris Lattnerefcddc32010-04-15 05:28:43 +00003856 // fold (srl (shl x, c), c) -> (and x, cst2)
3857 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
3858 N0.getValueSizeInBits() <= 64) {
3859 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
3860 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
3861 DAG.getConstant(~0ULL >> ShAmt, VT));
3862 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003863
Scott Michelfdc40a02009-02-17 22:15:04 +00003864
Chris Lattner06afe072006-05-05 22:53:17 +00003865 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
3866 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
3867 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00003868 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003869 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00003870 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00003871
Evan Chenge5b51ac2010-04-17 06:13:15 +00003872 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00003873 uint64_t ShiftAmt = N1C->getZExtValue();
Evan Chenge5b51ac2010-04-17 06:13:15 +00003874 SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00003875 N0.getOperand(0),
3876 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00003877 AddToWorkList(SmallShift.getNode());
3878 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift);
3879 }
Chris Lattner06afe072006-05-05 22:53:17 +00003880 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003881
Chris Lattner3657ffe2006-10-12 20:23:19 +00003882 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
3883 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00003884 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00003885 if (N0.getOpcode() == ISD::SRA)
Bill Wendling88103372009-01-30 21:37:17 +00003886 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00003887 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003888
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003889 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00003890 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003891 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00003892 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003893 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00003894
Chris Lattner350bec02006-04-02 06:11:11 +00003895 // If any of the input bits are KnownOne, then the input couldn't be all
3896 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003897 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003898
Chris Lattner350bec02006-04-02 06:11:11 +00003899 // If all of the bits input the to ctlz node are known to be zero, then
3900 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003901 APInt UnknownBits = ~KnownZero;
Chris Lattner350bec02006-04-02 06:11:11 +00003902 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003903
Chris Lattner350bec02006-04-02 06:11:11 +00003904 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00003905 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00003906 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00003907 // could be set on input to the CTLZ node. If this bit is set, the SRL
3908 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
3909 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003910 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00003911 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00003912
Chris Lattner350bec02006-04-02 06:11:11 +00003913 if (ShAmt) {
Bill Wendling88103372009-01-30 21:37:17 +00003914 Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00003915 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00003916 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00003917 }
Bill Wendling88103372009-01-30 21:37:17 +00003918
3919 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
3920 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00003921 }
3922 }
Evan Chengeb9f8922008-08-30 02:03:58 +00003923
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003924 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003925 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003926 N1.getOperand(0).getOpcode() == ISD::AND &&
3927 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003928 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003929 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003930 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003931 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003932 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003933 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003934 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003935 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003936 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003937 DAG.getNode(ISD::TRUNCATE,
3938 N->getDebugLoc(),
3939 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003940 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003941 }
3942 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003943
Chris Lattner61a4c072007-04-18 03:06:49 +00003944 // fold operands of srl based on knowledge that the low bits are not
3945 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003946 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3947 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003948
Evan Cheng9ab2b982009-12-18 21:31:31 +00003949 if (N1C) {
3950 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
3951 if (NewSRL.getNode())
3952 return NewSRL;
3953 }
3954
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003955 // Attempt to convert a srl of a load into a narrower zero-extending load.
3956 SDValue NarrowLoad = ReduceLoadWidth(N);
3957 if (NarrowLoad.getNode())
3958 return NarrowLoad;
3959
Evan Cheng9ab2b982009-12-18 21:31:31 +00003960 // Here is a common situation. We want to optimize:
3961 //
3962 // %a = ...
3963 // %b = and i32 %a, 2
3964 // %c = srl i32 %b, 1
3965 // brcond i32 %c ...
3966 //
3967 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003968 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00003969 // %a = ...
3970 // %b = and %a, 2
3971 // %c = setcc eq %b, 0
3972 // brcond %c ...
3973 //
3974 // However when after the source operand of SRL is optimized into AND, the SRL
3975 // itself may not be optimized further. Look for it and add the BRCOND into
3976 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00003977 if (N->hasOneUse()) {
3978 SDNode *Use = *N->use_begin();
3979 if (Use->getOpcode() == ISD::BRCOND)
3980 AddToWorkList(Use);
3981 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
3982 // Also look pass the truncate.
3983 Use = *Use->use_begin();
3984 if (Use->getOpcode() == ISD::BRCOND)
3985 AddToWorkList(Use);
3986 }
3987 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00003988
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003989 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00003990}
3991
Dan Gohman475871a2008-07-27 21:46:04 +00003992SDValue DAGCombiner::visitCTLZ(SDNode *N) {
3993 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003994 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003995
3996 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003997 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003998 return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003999 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004000}
4001
Chandler Carruth63974b22011-12-13 01:56:10 +00004002SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
4003 SDValue N0 = N->getOperand(0);
4004 EVT VT = N->getValueType(0);
4005
4006 // fold (ctlz_zero_undef c1) -> c2
4007 if (isa<ConstantSDNode>(N0))
4008 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0);
4009 return SDValue();
4010}
4011
Dan Gohman475871a2008-07-27 21:46:04 +00004012SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4013 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004014 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004015
Nate Begeman1d4d4142005-09-01 00:19:25 +00004016 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004017 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00004018 return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004019 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004020}
4021
Chandler Carruth63974b22011-12-13 01:56:10 +00004022SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4023 SDValue N0 = N->getOperand(0);
4024 EVT VT = N->getValueType(0);
4025
4026 // fold (cttz_zero_undef c1) -> c2
4027 if (isa<ConstantSDNode>(N0))
4028 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0);
4029 return SDValue();
4030}
4031
Dan Gohman475871a2008-07-27 21:46:04 +00004032SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4033 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004034 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004035
Nate Begeman1d4d4142005-09-01 00:19:25 +00004036 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004037 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00004038 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004039 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004040}
4041
Dan Gohman475871a2008-07-27 21:46:04 +00004042SDValue DAGCombiner::visitSELECT(SDNode *N) {
4043 SDValue N0 = N->getOperand(0);
4044 SDValue N1 = N->getOperand(1);
4045 SDValue N2 = N->getOperand(2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004046 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4047 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4048 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004049 EVT VT = N->getValueType(0);
4050 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00004051
Bill Wendling34584e62009-01-30 22:02:18 +00004052 // fold (select C, X, X) -> X
Nate Begeman452d7beb2005-09-16 00:54:12 +00004053 if (N1 == N2)
4054 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004055 // fold (select true, X, Y) -> X
Nate Begeman452d7beb2005-09-16 00:54:12 +00004056 if (N0C && !N0C->isNullValue())
4057 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004058 // fold (select false, X, Y) -> Y
Nate Begeman452d7beb2005-09-16 00:54:12 +00004059 if (N0C && N0C->isNullValue())
4060 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00004061 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004062 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Bill Wendling34584e62009-01-30 22:02:18 +00004063 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
4064 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00004065 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004066 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00004067 (VT0.isInteger() &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00004068 TLI.getBooleanContents(false) ==
4069 TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004070 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004071 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00004072 if (VT == VT0)
Bill Wendling34584e62009-01-30 22:02:18 +00004073 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0,
4074 N0, DAG.getConstant(1, VT0));
4075 XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0,
4076 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004077 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004078 if (VT.bitsGT(VT0))
Bill Wendling34584e62009-01-30 22:02:18 +00004079 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode);
4080 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00004081 }
Bill Wendling34584e62009-01-30 22:02:18 +00004082 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004083 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bill Wendling7581bfa2009-01-30 23:03:19 +00004084 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004085 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00004086 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004087 }
Bill Wendling34584e62009-01-30 22:02:18 +00004088 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004089 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004090 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004091 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00004092 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004093 }
Bill Wendling34584e62009-01-30 22:02:18 +00004094 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004095 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Bill Wendling34584e62009-01-30 22:02:18 +00004096 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
4097 // fold (select X, X, Y) -> (or X, Y)
4098 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004099 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Bill Wendling34584e62009-01-30 22:02:18 +00004100 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
4101 // fold (select X, Y, X) -> (and X, Y)
4102 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004103 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Bill Wendling34584e62009-01-30 22:02:18 +00004104 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004105
Chris Lattner40c62d52005-10-18 06:04:22 +00004106 // If we can fold this based on the true/false value, do so.
4107 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00004108 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004109
Nate Begeman44728a72005-09-19 22:34:01 +00004110 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004111 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004112 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00004113 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00004114 // having to say they don't support SELECT_CC on every type the DAG knows
4115 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00004116 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00004117 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Bill Wendling34584e62009-01-30 22:02:18 +00004118 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT,
4119 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004120 N1, N2, N0.getOperand(2));
Chris Lattner600fec32009-03-11 05:08:08 +00004121 return SimplifySelect(N->getDebugLoc(), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004122 }
Bill Wendling34584e62009-01-30 22:02:18 +00004123
Dan Gohman475871a2008-07-27 21:46:04 +00004124 return SDValue();
Nate Begeman452d7beb2005-09-16 00:54:12 +00004125}
4126
Dan Gohman475871a2008-07-27 21:46:04 +00004127SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4128 SDValue N0 = N->getOperand(0);
4129 SDValue N1 = N->getOperand(1);
4130 SDValue N2 = N->getOperand(2);
4131 SDValue N3 = N->getOperand(3);
4132 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00004133 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004134
Nate Begeman44728a72005-09-19 22:34:01 +00004135 // fold select_cc lhs, rhs, x, x, cc -> x
4136 if (N2 == N3)
4137 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004138
Chris Lattner5f42a242006-09-20 06:19:26 +00004139 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00004140 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004141 N0, N1, CC, N->getDebugLoc(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004142 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00004143
Gabor Greifba36cb52008-08-28 21:40:38 +00004144 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00004145 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00004146 return N2; // cond always true -> true val
4147 else
4148 return N3; // cond always false -> false val
4149 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004150
Chris Lattner5f42a242006-09-20 06:19:26 +00004151 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00004152 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Scott Michelfdc40a02009-02-17 22:15:04 +00004153 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(),
4154 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
Chris Lattner5f42a242006-09-20 06:19:26 +00004155 SCC.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004156
Chris Lattner40c62d52005-10-18 06:04:22 +00004157 // If we can fold this based on the true/false value, do so.
4158 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00004159 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00004160
Nate Begeman44728a72005-09-19 22:34:01 +00004161 // fold select_cc into other things, such as min/max/abs
Bill Wendling836ca7d2009-01-30 23:59:18 +00004162 return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004163}
4164
Dan Gohman475871a2008-07-27 21:46:04 +00004165SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7beb2005-09-16 00:54:12 +00004166 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004167 cast<CondCodeSDNode>(N->getOperand(2))->get(),
4168 N->getDebugLoc());
Nate Begeman452d7beb2005-09-16 00:54:12 +00004169}
4170
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004171// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00004172// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004173// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00004174// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00004175static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004176 unsigned ExtOpc,
4177 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00004178 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004179 bool HasCopyToRegUses = false;
4180 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00004181 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4182 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004183 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004184 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004185 if (User == N)
4186 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00004187 if (UI.getUse().getResNo() != N0.getResNo())
4188 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004189 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00004190 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004191 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4192 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4193 // Sign bits will be lost after a zext.
4194 return false;
4195 bool Add = false;
4196 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004197 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004198 if (UseOp == N0)
4199 continue;
4200 if (!isa<ConstantSDNode>(UseOp))
4201 return false;
4202 Add = true;
4203 }
4204 if (Add)
4205 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004206 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004207 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004208 // If truncates aren't free and there are users we can't
4209 // extend, it isn't worthwhile.
4210 if (!isTruncFree)
4211 return false;
4212 // Remember if this value is live-out.
4213 if (User->getOpcode() == ISD::CopyToReg)
4214 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004215 }
4216
4217 if (HasCopyToRegUses) {
4218 bool BothLiveOut = false;
4219 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4220 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004221 SDUse &Use = UI.getUse();
4222 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4223 BothLiveOut = true;
4224 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004225 }
4226 }
4227 if (BothLiveOut)
4228 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004229 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004230 return ExtendNodes.size();
4231 }
4232 return true;
4233}
4234
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004235void DAGCombiner::ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
4236 SDValue Trunc, SDValue ExtLoad, DebugLoc DL,
4237 ISD::NodeType ExtType) {
4238 // Extend SetCC uses if necessary.
4239 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4240 SDNode *SetCC = SetCCs[i];
4241 SmallVector<SDValue, 4> Ops;
4242
4243 for (unsigned j = 0; j != 2; ++j) {
4244 SDValue SOp = SetCC->getOperand(j);
4245 if (SOp == Trunc)
4246 Ops.push_back(ExtLoad);
4247 else
4248 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4249 }
4250
4251 Ops.push_back(SetCC->getOperand(2));
4252 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4253 &Ops[0], Ops.size()));
4254 }
4255}
4256
Dan Gohman475871a2008-07-27 21:46:04 +00004257SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4258 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004259 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004260
Nate Begeman1d4d4142005-09-01 00:19:25 +00004261 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004262 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004263 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004264
Nate Begeman1d4d4142005-09-01 00:19:25 +00004265 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004266 // fold (sext (aext x)) -> (sext x)
4267 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004268 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT,
4269 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004270
Chris Lattner22558872007-02-26 03:13:59 +00004271 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004272 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4273 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004274 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4275 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004276 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4277 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004278 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004279 // CombineTo deleted the truncate, if needed, but not what's under it.
4280 AddToWorkList(oye);
4281 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004282 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004283 }
Evan Chengc88138f2007-03-22 01:54:19 +00004284
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004285 // See if the value being truncated is already sign extended. If so, just
4286 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004287 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004288 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4289 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4290 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004291 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004292
Chris Lattner22558872007-02-26 03:13:59 +00004293 if (OpBits == DestBits) {
4294 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4295 // bits, it is already ready.
4296 if (NumSignBits > DestBits-MidBits)
4297 return Op;
4298 } else if (OpBits < DestBits) {
4299 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4300 // bits, just sext from i32.
4301 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004302 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004303 } else {
4304 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4305 // bits, just truncate to i32.
4306 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004307 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004308 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004309
Chris Lattner22558872007-02-26 03:13:59 +00004310 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004311 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4312 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004313 if (OpBits < DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004314 Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004315 else if (OpBits > DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004316 Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op);
4317 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004318 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004319 }
Chris Lattner6007b842006-09-21 06:00:20 +00004320 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004321
Evan Cheng110dec22005-12-14 02:19:23 +00004322 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004323 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004324 // on vectors in one instruction. We only perform this transformation on
4325 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004326 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004327 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004328 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004329 bool DoXform = true;
4330 SmallVector<SDNode*, 4> SetCCs;
4331 if (!N0.hasOneUse())
4332 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4333 if (DoXform) {
4334 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004335 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004336 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004337 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004338 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004339 LN0->isVolatile(), LN0->isNonTemporal(),
4340 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004341 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00004342 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4343 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004344 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004345 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4346 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004347 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004348 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004349 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004350
4351 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4352 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004353 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4354 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004355 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004356 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004357 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004358 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004359 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004360 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004361 LN0->getBasePtr(), LN0->getPointerInfo(),
4362 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004363 LN0->isVolatile(), LN0->isNonTemporal(),
4364 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004365 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004366 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004367 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4368 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004369 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004370 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004371 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004372 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004373
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004374 // fold (sext (and/or/xor (load x), cst)) ->
4375 // (and/or/xor (sextload x), (sext cst))
4376 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4377 N0.getOpcode() == ISD::XOR) &&
4378 isa<LoadSDNode>(N0.getOperand(0)) &&
4379 N0.getOperand(1).getOpcode() == ISD::Constant &&
4380 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4381 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4382 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4383 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4384 bool DoXform = true;
4385 SmallVector<SDNode*, 4> SetCCs;
4386 if (!N0.hasOneUse())
4387 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4388 SetCCs, TLI);
4389 if (DoXform) {
4390 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, LN0->getDebugLoc(), VT,
4391 LN0->getChain(), LN0->getBasePtr(),
4392 LN0->getPointerInfo(),
4393 LN0->getMemoryVT(),
4394 LN0->isVolatile(),
4395 LN0->isNonTemporal(),
4396 LN0->getAlignment());
4397 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4398 Mask = Mask.sext(VT.getSizeInBits());
4399 SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4400 ExtLoad, DAG.getConstant(Mask, VT));
4401 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
4402 N0.getOperand(0).getDebugLoc(),
4403 N0.getOperand(0).getValueType(), ExtLoad);
4404 CombineTo(N, And);
4405 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
4406 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4407 ISD::SIGN_EXTEND);
4408 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4409 }
4410 }
4411 }
4412
Chris Lattner20a35c32007-04-11 05:32:27 +00004413 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004414 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004415 // Only do this before legalize for now.
4416 if (VT.isVector() && !LegalOperations) {
4417 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem2e506192012-04-11 08:26:11 +00004418 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4419 // of the same size as the compared operands. Only optimize sext(setcc())
4420 // if this is the case.
4421 EVT SVT = TLI.getSetCCResultType(N0VT);
4422
4423 // We know that the # elements of the results is the same as the
4424 // # elements of the compare (and the # elements of the compare result
4425 // for that matter). Check to see that they are the same size. If so,
4426 // we know that the element size of the sext'd result matches the
4427 // element size of the compare operands.
4428 if (VT.getSizeInBits() == SVT.getSizeInBits())
Duncan Sands28b77e92011-09-06 19:07:46 +00004429 return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004430 N0.getOperand(1),
4431 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Dan Gohman3ce89f42010-04-30 17:19:19 +00004432 // If the desired elements are smaller or larger than the source
4433 // elements we can use a matching integer vector type and then
4434 // truncate/sign extend
4435 else {
Duncan Sands34727662010-07-12 08:16:59 +00004436 EVT MatchingElementType =
4437 EVT::getIntegerVT(*DAG.getContext(),
4438 N0VT.getScalarType().getSizeInBits());
4439 EVT MatchingVectorType =
4440 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4441 N0VT.getVectorNumElements());
Nadav Rotem2e506192012-04-11 08:26:11 +00004442
4443 if (SVT == MatchingVectorType) {
4444 SDValue VsetCC = DAG.getSetCC(N->getDebugLoc(), MatchingVectorType,
4445 N0.getOperand(0), N0.getOperand(1),
4446 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4447 return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT);
4448 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004449 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004450 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004451
Chris Lattner2b7a2712009-07-08 00:31:33 +00004452 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004453 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004454 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004455 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004456 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004457 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004458 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004459 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004460 if (SCC.getNode()) return SCC;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00004461 if (!LegalOperations ||
4462 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(VT)))
4463 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4464 DAG.getSetCC(N->getDebugLoc(),
4465 TLI.getSetCCResultType(VT),
4466 N0.getOperand(0), N0.getOperand(1),
4467 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4468 NegOne, DAG.getConstant(0, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004469 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004470
Dan Gohman8f0ad582008-04-28 16:58:24 +00004471 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004472 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004473 DAG.SignBitIsZero(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004474 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004475
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004476 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004477}
4478
Rafael Espindoladecbc432012-04-09 16:06:03 +00004479// isTruncateOf - If N is a truncate of some other value, return true, record
4480// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4481// This function computes KnownZero to avoid a duplicated call to
4482// ComputeMaskedBits in the caller.
4483static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4484 APInt &KnownZero) {
4485 APInt KnownOne;
4486 if (N->getOpcode() == ISD::TRUNCATE) {
4487 Op = N->getOperand(0);
4488 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4489 return true;
4490 }
4491
4492 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4493 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4494 return false;
4495
4496 SDValue Op0 = N->getOperand(0);
4497 SDValue Op1 = N->getOperand(1);
4498 assert(Op0.getValueType() == Op1.getValueType());
4499
4500 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4501 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004502 if (COp0 && COp0->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004503 Op = Op1;
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004504 else if (COp1 && COp1->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004505 Op = Op0;
4506 else
4507 return false;
4508
4509 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4510
4511 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4512 return false;
4513
4514 return true;
4515}
4516
Dan Gohman475871a2008-07-27 21:46:04 +00004517SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4518 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004519 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004520
Nate Begeman1d4d4142005-09-01 00:19:25 +00004521 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004522 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004523 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004524 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004525 // fold (zext (aext x)) -> (zext x)
4526 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004527 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT,
4528 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004529
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004530 // fold (zext (truncate x)) -> (zext x) or
4531 // (zext (truncate x)) -> (truncate x)
4532 // This is valid when the truncated bits of x are already zero.
4533 // FIXME: We should extend this to work for vectors too.
Rafael Espindoladecbc432012-04-09 16:06:03 +00004534 SDValue Op;
4535 APInt KnownZero;
4536 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4537 APInt TruncatedBits =
4538 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4539 APInt(Op.getValueSizeInBits(), 0) :
4540 APInt::getBitsSet(Op.getValueSizeInBits(),
4541 N0.getValueSizeInBits(),
4542 std::min(Op.getValueSizeInBits(),
4543 VT.getSizeInBits()));
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004544 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004545 if (VT.bitsGT(Op.getValueType()))
4546 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, Op);
4547 if (VT.bitsLT(Op.getValueType()))
4548 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
4549
4550 return Op;
4551 }
4552 }
4553
Evan Chengc88138f2007-03-22 01:54:19 +00004554 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4555 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004556 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004557 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4558 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004559 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4560 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004561 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004562 // CombineTo deleted the truncate, if needed, but not what's under it.
4563 AddToWorkList(oye);
4564 }
Eli Friedmane545d382011-04-16 23:25:34 +00004565 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004566 }
Evan Chengc88138f2007-03-22 01:54:19 +00004567 }
4568
Chris Lattner6007b842006-09-21 06:00:20 +00004569 // fold (zext (truncate x)) -> (and x, mask)
4570 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004571 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004572
4573 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4574 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4575 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4576 if (NarrowLoad.getNode()) {
4577 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4578 if (NarrowLoad.getNode() != N0.getNode()) {
4579 CombineTo(N0.getNode(), NarrowLoad);
4580 // CombineTo deleted the truncate, if needed, but not what's under it.
4581 AddToWorkList(oye);
4582 }
4583 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4584 }
4585
Dan Gohman475871a2008-07-27 21:46:04 +00004586 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004587 if (Op.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004588 Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004589 AddToWorkList(Op.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004590 } else if (Op.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004591 Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004592 AddToWorkList(Op.getNode());
Chris Lattner6007b842006-09-21 06:00:20 +00004593 }
Dan Gohman87862e72009-12-11 21:31:27 +00004594 return DAG.getZeroExtendInReg(Op, N->getDebugLoc(),
4595 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004596 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004597
Dan Gohman97121ba2009-04-08 00:15:30 +00004598 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4599 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004600 if (N0.getOpcode() == ISD::AND &&
4601 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004602 N0.getOperand(1).getOpcode() == ISD::Constant &&
4603 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4604 N0.getValueType()) ||
4605 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004606 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004607 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004608 X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004609 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004610 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004611 }
Dan Gohman220a8232008-03-03 23:51:38 +00004612 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004613 Mask = Mask.zext(VT.getSizeInBits());
Bill Wendling6ce610f2009-01-30 22:23:15 +00004614 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4615 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004616 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004617
Evan Cheng110dec22005-12-14 02:19:23 +00004618 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004619 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004620 // on vectors in one instruction. We only perform this transformation on
4621 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004622 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004623 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004624 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004625 bool DoXform = true;
4626 SmallVector<SDNode*, 4> SetCCs;
4627 if (!N0.hasOneUse())
4628 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4629 if (DoXform) {
4630 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004631 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004632 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004633 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004634 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004635 LN0->isVolatile(), LN0->isNonTemporal(),
4636 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004637 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00004638 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4639 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004640 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004641
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004642 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4643 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004644 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004645 }
Evan Cheng110dec22005-12-14 02:19:23 +00004646 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004647
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004648 // fold (zext (and/or/xor (load x), cst)) ->
4649 // (and/or/xor (zextload x), (zext cst))
4650 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4651 N0.getOpcode() == ISD::XOR) &&
4652 isa<LoadSDNode>(N0.getOperand(0)) &&
4653 N0.getOperand(1).getOpcode() == ISD::Constant &&
4654 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4655 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4656 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4657 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4658 bool DoXform = true;
4659 SmallVector<SDNode*, 4> SetCCs;
4660 if (!N0.hasOneUse())
4661 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4662 SetCCs, TLI);
4663 if (DoXform) {
4664 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), VT,
4665 LN0->getChain(), LN0->getBasePtr(),
4666 LN0->getPointerInfo(),
4667 LN0->getMemoryVT(),
4668 LN0->isVolatile(),
4669 LN0->isNonTemporal(),
4670 LN0->getAlignment());
4671 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4672 Mask = Mask.zext(VT.getSizeInBits());
4673 SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4674 ExtLoad, DAG.getConstant(Mask, VT));
4675 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
4676 N0.getOperand(0).getDebugLoc(),
4677 N0.getOperand(0).getValueType(), ExtLoad);
4678 CombineTo(N, And);
4679 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
4680 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4681 ISD::ZERO_EXTEND);
4682 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4683 }
4684 }
4685 }
4686
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004687 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4688 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004689 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4690 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004691 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004692 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004693 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004694 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004695 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004696 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004697 LN0->getBasePtr(), LN0->getPointerInfo(),
4698 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004699 LN0->isVolatile(), LN0->isNonTemporal(),
4700 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004701 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004702 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004703 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(),
4704 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004705 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004706 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004707 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004708 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004709
Chris Lattner20a35c32007-04-11 05:32:27 +00004710 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004711 if (!LegalOperations && VT.isVector()) {
4712 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4713 // Only do this before legalize for now.
4714 EVT N0VT = N0.getOperand(0).getValueType();
4715 EVT EltVT = VT.getVectorElementType();
4716 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4717 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004718 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004719 // We know that the # elements of the results is the same as the
4720 // # elements of the compare (and the # elements of the compare result
4721 // for that matter). Check to see that they are the same size. If so,
4722 // we know that the element size of the sext'd result matches the
4723 // element size of the compare operands.
4724 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
Duncan Sands28b77e92011-09-06 19:07:46 +00004725 DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004726 N0.getOperand(1),
4727 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4728 DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
4729 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004730
4731 // If the desired elements are smaller or larger than the source
4732 // elements we can use a matching integer vector type and then
4733 // truncate/sign extend
4734 EVT MatchingElementType =
4735 EVT::getIntegerVT(*DAG.getContext(),
4736 N0VT.getScalarType().getSizeInBits());
4737 EVT MatchingVectorType =
4738 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4739 N0VT.getVectorNumElements());
4740 SDValue VsetCC =
Duncan Sands28b77e92011-09-06 19:07:46 +00004741 DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00004742 N0.getOperand(1),
4743 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4744 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4745 DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT),
4746 DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
4747 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00004748 }
4749
4750 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004751 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004752 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00004753 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004754 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004755 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004756 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004757
Evan Cheng9818c042009-12-15 03:00:32 +00004758 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00004759 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00004760 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00004761 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4762 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00004763 SDValue ShAmt = N0.getOperand(1);
4764 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00004765 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00004766 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00004767 // If the original shl may be shifting out bits, do not perform this
4768 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00004769 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
4770 InnerZExt.getOperand(0).getValueType().getSizeInBits();
4771 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00004772 return SDValue();
4773 }
Chris Lattnere0751182011-02-13 19:09:16 +00004774
4775 DebugLoc DL = N->getDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00004776
4777 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00004778 if (VT.getSizeInBits() >= 256)
4779 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00004780
Chris Lattnere0751182011-02-13 19:09:16 +00004781 return DAG.getNode(N0.getOpcode(), DL, VT,
4782 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
4783 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00004784 }
4785
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004786 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004787}
4788
Dan Gohman475871a2008-07-27 21:46:04 +00004789SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
4790 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004791 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004792
Chris Lattner5ffc0662006-05-05 05:58:59 +00004793 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004794 if (isa<ConstantSDNode>(N0))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004795 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00004796 // fold (aext (aext x)) -> (aext x)
4797 // fold (aext (zext x)) -> (zext x)
4798 // fold (aext (sext x)) -> (sext x)
4799 if (N0.getOpcode() == ISD::ANY_EXTEND ||
4800 N0.getOpcode() == ISD::ZERO_EXTEND ||
4801 N0.getOpcode() == ISD::SIGN_EXTEND)
Bill Wendling683c9572009-01-30 22:27:33 +00004802 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004803
Evan Chengc88138f2007-03-22 01:54:19 +00004804 // fold (aext (truncate (load x))) -> (aext (smaller load x))
4805 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
4806 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004807 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4808 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00004809 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4810 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004811 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00004812 // CombineTo deleted the truncate, if needed, but not what's under it.
4813 AddToWorkList(oye);
4814 }
Eli Friedmane545d382011-04-16 23:25:34 +00004815 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004816 }
Evan Chengc88138f2007-03-22 01:54:19 +00004817 }
4818
Chris Lattner84750582006-09-20 06:29:17 +00004819 // fold (aext (truncate x))
4820 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00004821 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00004822 if (TruncOp.getValueType() == VT)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00004823 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004824 if (TruncOp.getValueType().bitsGT(VT))
Bill Wendling683c9572009-01-30 22:27:33 +00004825 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp);
4826 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00004827 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004828
Dan Gohman97121ba2009-04-08 00:15:30 +00004829 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
4830 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00004831 if (N0.getOpcode() == ISD::AND &&
4832 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004833 N0.getOperand(1).getOpcode() == ISD::Constant &&
4834 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4835 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004836 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004837 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004838 X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004839 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004840 X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00004841 }
Dan Gohman220a8232008-03-03 23:51:38 +00004842 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004843 Mask = Mask.zext(VT.getSizeInBits());
Bill Wendling683c9572009-01-30 22:27:33 +00004844 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4845 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00004846 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004847
Chris Lattner5ffc0662006-05-05 05:58:59 +00004848 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004849 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004850 // on vectors in one instruction. We only perform this transformation on
4851 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004852 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004853 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004854 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004855 bool DoXform = true;
4856 SmallVector<SDNode*, 4> SetCCs;
4857 if (!N0.hasOneUse())
4858 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
4859 if (DoXform) {
4860 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004861 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004862 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004863 LN0->getBasePtr(), LN0->getPointerInfo(),
Dan Gohman57fc82d2009-04-09 03:51:29 +00004864 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004865 LN0->isVolatile(), LN0->isNonTemporal(),
4866 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00004867 CombineTo(N, ExtLoad);
4868 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4869 N0.getValueType(), ExtLoad);
4870 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004871 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4872 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004873 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4874 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00004875 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004876
Chris Lattner5ffc0662006-05-05 05:58:59 +00004877 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
4878 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
4879 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00004880 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004881 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00004882 N0.hasOneUse()) {
4883 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004884 EVT MemVT = LN0->getMemoryVT();
Stuart Hastingsa9011292011-02-16 16:23:55 +00004885 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(),
4886 VT, LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004887 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00004888 LN0->isVolatile(), LN0->isNonTemporal(),
4889 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00004890 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00004891 CombineTo(N0.getNode(),
Bill Wendling683c9572009-01-30 22:27:33 +00004892 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4893 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00004894 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004895 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00004896 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004897
Chris Lattner20a35c32007-04-11 05:32:27 +00004898 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004899 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
4900 // Only do this before legalize for now.
4901 if (VT.isVector() && !LegalOperations) {
4902 EVT N0VT = N0.getOperand(0).getValueType();
4903 // We know that the # elements of the results is the same as the
4904 // # elements of the compare (and the # elements of the compare result
4905 // for that matter). Check to see that they are the same size. If so,
4906 // we know that the element size of the sext'd result matches the
4907 // element size of the compare operands.
4908 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Duncan Sands28b77e92011-09-06 19:07:46 +00004909 return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004910 N0.getOperand(1),
4911 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00004912 // If the desired elements are smaller or larger than the source
4913 // elements we can use a matching integer vector type and then
4914 // truncate/sign extend
4915 else {
Duncan Sands34727662010-07-12 08:16:59 +00004916 EVT MatchingElementType =
4917 EVT::getIntegerVT(*DAG.getContext(),
4918 N0VT.getScalarType().getSizeInBits());
4919 EVT MatchingVectorType =
4920 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4921 N0VT.getVectorNumElements());
4922 SDValue VsetCC =
Duncan Sands28b77e92011-09-06 19:07:46 +00004923 DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004924 N0.getOperand(1),
4925 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4926 return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00004927 }
4928 }
4929
4930 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004931 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004932 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004933 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00004934 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004935 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004936 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004937 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004938
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004939 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00004940}
4941
Chris Lattner2b4c2792007-10-13 06:35:54 +00004942/// GetDemandedBits - See if the specified operand can be simplified with the
4943/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00004944/// simpler operand, otherwise return a null SDValue.
4945SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004946 switch (V.getOpcode()) {
4947 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00004948 case ISD::Constant: {
4949 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
4950 assert(CV != 0 && "Const value should be ConstSDNode.");
4951 const APInt &CVal = CV->getAPIntValue();
4952 APInt NewVal = CVal & Mask;
4953 if (NewVal != CVal) {
4954 return DAG.getConstant(NewVal, V.getValueType());
4955 }
4956 break;
4957 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00004958 case ISD::OR:
4959 case ISD::XOR:
4960 // If the LHS or RHS don't contribute bits to the or, drop them.
4961 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
4962 return V.getOperand(1);
4963 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
4964 return V.getOperand(0);
4965 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00004966 case ISD::SRL:
4967 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00004968 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00004969 break;
4970 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
4971 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004972 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00004973
Dan Gohmancc91d632009-01-03 19:22:06 +00004974 // Watch out for shift count overflow though.
4975 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004976 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00004977 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00004978 if (SimplifyLHS.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004979 return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00004980 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00004981 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00004982 }
Dan Gohman475871a2008-07-27 21:46:04 +00004983 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00004984}
4985
Evan Chengc88138f2007-03-22 01:54:19 +00004986/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
4987/// bits and then truncated to a narrower type and where N is a multiple
4988/// of number of bits of the narrower type, transform it to a narrower load
4989/// from address + N / num of bits of new type. If the result is to be
4990/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00004991SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00004992 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004993
Evan Chengc88138f2007-03-22 01:54:19 +00004994 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00004995 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004996 EVT VT = N->getValueType(0);
4997 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00004998
Dan Gohman7f8613e2008-08-14 20:04:46 +00004999 // This transformation isn't valid for vector loads.
5000 if (VT.isVector())
5001 return SDValue();
5002
Dan Gohmand1996362010-01-09 02:13:55 +00005003 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00005004 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00005005 if (Opc == ISD::SIGN_EXTEND_INREG) {
5006 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00005007 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005008 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00005009 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005010 ExtType = ISD::ZEXTLOAD;
5011 N0 = SDValue(N, 0);
5012 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5013 if (!N01) return SDValue();
5014 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5015 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00005016 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00005017 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5018 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005019
Owen Andersone50ed302009-08-10 22:56:29 +00005020 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00005021
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005022 // Do not generate loads of non-round integer types since these can
5023 // be expensive (and would be wrong if the type is not byte sized).
5024 if (!ExtVT.isRound())
5025 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005026
Evan Chengc88138f2007-03-22 01:54:19 +00005027 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005028 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00005029 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005030 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005031 // Is the shift amount a multiple of size of VT?
5032 if ((ShAmt & (EVTBits-1)) == 0) {
5033 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00005034 // Is the load width a multiple of size of VT?
5035 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005036 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005037 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005038
Chris Lattnercbf68df2010-12-22 08:02:57 +00005039 // At this point, we must have a load or else we can't do the transform.
5040 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005041
Chris Lattner2831a192010-10-01 05:36:09 +00005042 // If the shift amount is larger than the input type then we're not
5043 // accessing any of the loaded bytes. If the load was a zextload/extload
5044 // then the result of the shift+trunc is zero/undef (handled elsewhere).
5045 // If the load was a sextload then the result is a splat of the sign bit
5046 // of the extended byte. This is not worth optimizing for.
Chris Lattnercbf68df2010-12-22 08:02:57 +00005047 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00005048 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005049 }
5050 }
5051
Dan Gohman394d6292010-11-03 01:47:46 +00005052 // If the load is shifted left (and the result isn't shifted back right),
5053 // we can fold the truncate through the shift.
5054 unsigned ShLeftAmt = 0;
5055 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00005056 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00005057 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5058 ShLeftAmt = N01->getZExtValue();
5059 N0 = N0.getOperand(0);
5060 }
5061 }
Owen Anderson95771af2011-02-25 21:41:48 +00005062
Chris Lattner4c32bc22010-12-22 07:36:50 +00005063 // If we haven't found a load, we can't narrow it. Don't transform one with
5064 // multiple uses, this would require adding a new load.
5065 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse() ||
5066 // Don't change the width of a volatile load.
5067 cast<LoadSDNode>(N0)->isVolatile())
5068 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005069
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005070 // Verify that we are actually reducing a load width here.
5071 if (cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00005072 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005073
Chris Lattner4c32bc22010-12-22 07:36:50 +00005074 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5075 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00005076
Evan Cheng16436df2012-06-26 01:19:33 +00005077 if (PtrType == MVT::Untyped || PtrType.isExtended())
5078 // It's not possible to generate a constant of extended or untyped type.
5079 return SDValue();
5080
Chris Lattner4c32bc22010-12-22 07:36:50 +00005081 // For big endian targets, we need to adjust the offset to the pointer to
5082 // load the correct bytes.
5083 if (TLI.isBigEndian()) {
5084 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5085 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5086 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00005087 }
5088
Chris Lattner4c32bc22010-12-22 07:36:50 +00005089 uint64_t PtrOff = ShAmt / 8;
5090 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
5091 SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(),
5092 PtrType, LN0->getBasePtr(),
5093 DAG.getConstant(PtrOff, PtrType));
5094 AddToWorkList(NewPtr.getNode());
5095
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005096 SDValue Load;
5097 if (ExtType == ISD::NON_EXTLOAD)
5098 Load = DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr,
5099 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005100 LN0->isVolatile(), LN0->isNonTemporal(),
5101 LN0->isInvariant(), NewAlign);
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005102 else
Stuart Hastingsa9011292011-02-16 16:23:55 +00005103 Load = DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005104 LN0->getPointerInfo().getWithOffset(PtrOff),
5105 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
5106 NewAlign);
Chris Lattner4c32bc22010-12-22 07:36:50 +00005107
5108 // Replace the old load's chain with the new load's chain.
5109 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005110 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005111
5112 // Shift the result left, if we've swallowed a left shift.
5113 SDValue Result = Load;
5114 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00005115 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005116 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5117 ShImmTy = VT;
5118 Result = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT,
5119 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
5120 }
5121
5122 // Return the new loaded value.
5123 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00005124}
5125
Dan Gohman475871a2008-07-27 21:46:04 +00005126SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5127 SDValue N0 = N->getOperand(0);
5128 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005129 EVT VT = N->getValueType(0);
5130 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00005131 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00005132 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005133
Nate Begeman1d4d4142005-09-01 00:19:25 +00005134 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00005135 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Bill Wendling8509c902009-01-30 22:33:24 +00005136 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005137
Chris Lattner541a24f2006-05-06 22:43:44 +00005138 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00005139 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00005140 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005141
Nate Begeman646d7e22005-09-02 21:18:40 +00005142 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5143 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00005144 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Bill Wendling8509c902009-01-30 22:33:24 +00005145 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
5146 N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00005147 }
Chris Lattner4b37e872006-05-08 21:18:59 +00005148
Dan Gohman75dcf082008-07-31 00:50:31 +00005149 // fold (sext_in_reg (sext x)) -> (sext x)
5150 // fold (sext_in_reg (aext x)) -> (sext x)
5151 // if x is small enough.
5152 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5153 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00005154 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5155 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Bill Wendling8509c902009-01-30 22:33:24 +00005156 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00005157 }
5158
Chris Lattner95a5e052007-04-17 19:03:21 +00005159 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005160 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005161 return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005162
Chris Lattner95a5e052007-04-17 19:03:21 +00005163 // fold operands of sext_in_reg based on knowledge that the top bits are not
5164 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00005165 if (SimplifyDemandedBits(SDValue(N, 0)))
5166 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005167
Evan Chengc88138f2007-03-22 01:54:19 +00005168 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5169 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00005170 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005171 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00005172 return NarrowLoad;
5173
Bill Wendling8509c902009-01-30 22:33:24 +00005174 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005175 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00005176 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5177 if (N0.getOpcode() == ISD::SRL) {
5178 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00005179 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005180 // We can turn this into an SRA iff the input to the SRL is already sign
Chris Lattner4b37e872006-05-08 21:18:59 +00005181 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00005182 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00005183 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Bill Wendling8509c902009-01-30 22:33:24 +00005184 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT,
5185 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00005186 }
5187 }
Evan Chengc88138f2007-03-22 01:54:19 +00005188
Nate Begemanded49632005-10-13 03:11:28 +00005189 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00005190 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005191 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005192 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005193 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005194 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005195 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00005196 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005197 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005198 LN0->getBasePtr(), LN0->getPointerInfo(),
5199 EVT,
David Greene1e559442010-02-15 17:00:31 +00005200 LN0->isVolatile(), LN0->isNonTemporal(),
5201 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005202 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005203 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005204 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005205 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005206 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00005207 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00005208 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005209 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005210 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005211 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005212 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00005213 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005214 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005215 LN0->getBasePtr(), LN0->getPointerInfo(),
5216 EVT,
David Greene1e559442010-02-15 17:00:31 +00005217 LN0->isVolatile(), LN0->isNonTemporal(),
5218 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005219 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005220 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005221 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005222 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00005223
5224 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5225 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5226 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5227 N0.getOperand(1), false);
5228 if (BSwap.getNode() != 0)
5229 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
5230 BSwap, N1);
5231 }
5232
Dan Gohman475871a2008-07-27 21:46:04 +00005233 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005234}
5235
Dan Gohman475871a2008-07-27 21:46:04 +00005236SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5237 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005238 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005239 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005240
5241 // noop truncate
5242 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00005243 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00005244 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005245 if (isa<ConstantSDNode>(N0))
Bill Wendling67a67682009-01-30 22:44:24 +00005246 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005247 // fold (truncate (truncate x)) -> (truncate x)
5248 if (N0.getOpcode() == ISD::TRUNCATE)
Bill Wendling67a67682009-01-30 22:44:24 +00005249 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005250 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00005251 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5252 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00005253 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005254 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005255 // if the source is smaller than the dest, we still need an extend
Bill Wendling67a67682009-01-30 22:44:24 +00005256 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
5257 N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00005258 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005259 // if the source is larger than the dest, than we just need the truncate
Bill Wendling67a67682009-01-30 22:44:24 +00005260 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005261 else
5262 // if the source and dest are the same type, we can drop both the extend
Evan Chengd40d03e2010-01-06 19:38:29 +00005263 // and the truncate.
Nate Begeman83e75ec2005-09-06 04:43:02 +00005264 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005265 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005266
Nadav Rotemcc870a82012-02-05 11:39:23 +00005267 // Fold extract-and-trunc into a narrow extract. For example:
5268 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5269 // i32 y = TRUNCATE(i64 x)
5270 // -- becomes --
5271 // v16i8 b = BITCAST (v2i64 val)
5272 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5273 //
5274 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005275 // creates this pattern) and before operation legalization after which
5276 // we need to be more careful about the vector instructions that we generate.
5277 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5278 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5279
5280 EVT VecTy = N0.getOperand(0).getValueType();
5281 EVT ExTy = N0.getValueType();
5282 EVT TrTy = N->getValueType(0);
5283
5284 unsigned NumElem = VecTy.getVectorNumElements();
5285 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5286
5287 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5288 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5289
5290 SDValue EltNo = N0->getOperand(1);
5291 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5292 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005293 EVT IndexTy = N0->getOperand(1).getValueType();
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005294 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5295
5296 SDValue V = DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
5297 NVT, N0.getOperand(0));
5298
5299 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
5300 N->getDebugLoc(), TrTy, V,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005301 DAG.getConstant(Index, IndexTy));
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005302 }
5303 }
5304
Chris Lattner2b4c2792007-10-13 06:35:54 +00005305 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005306 // only the low bits are being used.
5307 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005308 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005309 // may have different active low bits.
5310 if (!VT.isVector()) {
5311 SDValue Shorter =
5312 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5313 VT.getSizeInBits()));
5314 if (Shorter.getNode())
5315 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter);
5316 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005317 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005318 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005319 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5320 SDValue Reduced = ReduceLoadWidth(N);
5321 if (Reduced.getNode())
5322 return Reduced;
5323 }
5324
5325 // Simplify the operands using demanded-bits information.
5326 if (!VT.isVector() &&
5327 SimplifyDemandedBits(SDValue(N, 0)))
5328 return SDValue(N, 0);
5329
Evan Chenge5b51ac2010-04-17 06:13:15 +00005330 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005331}
5332
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005333static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005334 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005335 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005336 return Elt.getNode();
5337 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005338}
5339
5340/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005341/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005342SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005343 assert(N->getOpcode() == ISD::BUILD_PAIR);
5344
Nate Begemanabc01992009-06-05 21:37:30 +00005345 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5346 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005347 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5348 LD1->getPointerInfo().getAddrSpace() !=
5349 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005350 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005351 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005352
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005353 if (ISD::isNON_EXTLoad(LD2) &&
5354 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005355 // If both are volatile this would reduce the number of volatile loads.
5356 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005357 !LD1->isVolatile() &&
5358 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005359 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005360 unsigned Align = LD1->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00005361 unsigned NewAlign = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005362 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005363
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005364 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005365 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Nate Begemanabc01992009-06-05 21:37:30 +00005366 return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005367 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005368 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005369 }
Bill Wendling67a67682009-01-30 22:44:24 +00005370
Dan Gohman475871a2008-07-27 21:46:04 +00005371 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005372}
5373
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005374SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005375 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005376 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005377
Dan Gohman7f321562007-06-25 16:23:39 +00005378 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5379 // Only do this before legalize, since afterward the target may be depending
5380 // on the bitconvert.
5381 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005382 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005383 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005384 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005385 bool isSimple = true;
5386 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5387 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5388 N0.getOperand(i).getOpcode() != ISD::Constant &&
5389 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005390 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005391 break;
5392 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005393
Owen Andersone50ed302009-08-10 22:56:29 +00005394 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005395 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005396 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005397 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005398 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005399 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005400
Dan Gohman3dd168d2008-09-05 01:58:21 +00005401 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005402 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005403 SDValue Res = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005404 if (Res.getNode() != N) {
5405 if (!LegalOperations ||
5406 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5407 return Res;
5408
5409 // Folding it resulted in an illegal node, and it's too late to
5410 // do that. Clean up the old node and forego the transformation.
5411 // Ideally this won't happen very often, because instcombine
5412 // and the earlier dagcombine runs (where illegal nodes are
5413 // permitted) should have folded most of them already.
5414 DAG.DeleteNode(Res.getNode());
5415 }
Chris Lattner94683772005-12-23 05:30:37 +00005416 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005417
Bill Wendling67a67682009-01-30 22:44:24 +00005418 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005419 if (N0.getOpcode() == ISD::BITCAST)
5420 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005421 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005422
Chris Lattner57104102005-12-23 05:44:41 +00005423 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005424 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005425 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005426 // Do not change the width of a volatile load.
5427 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005428 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005429 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00005430 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005431 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005432 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005433
Evan Cheng59d5b682007-05-07 21:27:48 +00005434 if (Align <= OrigAlign) {
Bill Wendling67a67682009-01-30 22:44:24 +00005435 SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005436 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005437 LN0->isVolatile(), LN0->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005438 LN0->isInvariant(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00005439 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005440 CombineTo(N0.getNode(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005441 DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005442 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005443 Load.getValue(1));
5444 return Load;
5445 }
Chris Lattner57104102005-12-23 05:44:41 +00005446 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005447
Bill Wendling67a67682009-01-30 22:44:24 +00005448 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5449 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005450 // This often reduces constant pool loads.
Owen Anderson29f60f32012-04-02 22:10:29 +00005451 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(VT)) ||
5452 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(VT))) &&
Nadav Rotem91a7e012012-09-13 14:54:28 +00005453 N0.getNode()->hasOneUse() && VT.isInteger() &&
5454 !VT.isVector() && !N0.getValueType().isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005455 SDValue NewConv = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005456 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005457 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005458
Duncan Sands83ec4b62008-06-06 12:08:01 +00005459 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005460 if (N0.getOpcode() == ISD::FNEG)
Bill Wendling67a67682009-01-30 22:44:24 +00005461 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
5462 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005463 assert(N0.getOpcode() == ISD::FABS);
Bill Wendling67a67682009-01-30 22:44:24 +00005464 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
5465 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005466 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005467
Bill Wendling67a67682009-01-30 22:44:24 +00005468 // fold (bitconvert (fcopysign cst, x)) ->
5469 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5470 // Note that we don't handle (copysign x, cst) because this can always be
5471 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005472 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005473 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005474 VT.isInteger() && !VT.isVector()) {
5475 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005476 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005477 if (isTypeLegal(IntXVT)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005478 SDValue X = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005479 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005480 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005481
Duncan Sands25cf2272008-11-24 14:53:14 +00005482 // If X has a different width than the result/lhs, sext it or truncate it.
5483 unsigned VTWidth = VT.getSizeInBits();
5484 if (OrigXWidth < VTWidth) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005485 X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005486 AddToWorkList(X.getNode());
5487 } else if (OrigXWidth > VTWidth) {
5488 // To get the sign bit in the right place, we have to shift it right
5489 // before truncating.
Bill Wendling9729c5a2009-01-31 03:12:48 +00005490 X = DAG.getNode(ISD::SRL, X.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005491 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005492 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5493 AddToWorkList(X.getNode());
Bill Wendling9729c5a2009-01-31 03:12:48 +00005494 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005495 AddToWorkList(X.getNode());
5496 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005497
Duncan Sands25cf2272008-11-24 14:53:14 +00005498 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Bill Wendling9729c5a2009-01-31 03:12:48 +00005499 X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005500 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005501 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005502
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005503 SDValue Cst = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005504 VT, N0.getOperand(0));
Bill Wendling9729c5a2009-01-31 03:12:48 +00005505 Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005506 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005507 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005508
Bill Wendling67a67682009-01-30 22:44:24 +00005509 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005510 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005511 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005512
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005513 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005514 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005515 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5516 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005517 return CombineLD;
5518 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005519
Dan Gohman475871a2008-07-27 21:46:04 +00005520 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005521}
5522
Dan Gohman475871a2008-07-27 21:46:04 +00005523SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005524 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005525 return CombineConsecutiveLoads(N, VT);
5526}
5527
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005528/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005529/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005530/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005531SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005532ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005533 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005534
Chris Lattner6258fb22006-04-02 02:53:43 +00005535 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005536 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005537
Duncan Sands83ec4b62008-06-06 12:08:01 +00005538 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5539 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005540
Chris Lattner6258fb22006-04-02 02:53:43 +00005541 // If this is a conversion of N elements of one type to N elements of another
5542 // type, convert each element. This handles FP<->INT cases.
5543 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005544 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5545 BV->getValueType(0).getVectorNumElements());
5546
5547 // Due to the FP element handling below calling this routine recursively,
5548 // we can end up with a scalar-to-vector node here.
5549 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005550 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
5551 DAG.getNode(ISD::BITCAST, BV->getDebugLoc(),
Nate Begemane0efc212010-07-27 18:02:18 +00005552 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005553
Dan Gohman475871a2008-07-27 21:46:04 +00005554 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005555 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005556 SDValue Op = BV->getOperand(i);
5557 // If the vector element type is not legal, the BUILD_VECTOR operands
5558 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005559 if (Op.getValueType() != SrcEltVT)
5560 Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005561 Ops.push_back(DAG.getNode(ISD::BITCAST, BV->getDebugLoc(),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005562 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005563 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005564 }
Evan Chenga87008d2009-02-25 22:49:59 +00005565 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5566 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005567 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005568
Chris Lattner6258fb22006-04-02 02:53:43 +00005569 // Otherwise, we're growing or shrinking the elements. To avoid having to
5570 // handle annoying details of growing/shrinking FP values, we convert them to
5571 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005572 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005573 // Convert the input float vector to a int vector where the elements are the
5574 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005575 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005576 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005577 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005578 SrcEltVT = IntVT;
5579 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005580
Chris Lattner6258fb22006-04-02 02:53:43 +00005581 // Now we know the input is an integer vector. If the output is a FP type,
5582 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005583 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005584 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005585 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005586 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005587
Chris Lattner6258fb22006-04-02 02:53:43 +00005588 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005589 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005590 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005591
Chris Lattner6258fb22006-04-02 02:53:43 +00005592 // Okay, we know the src/dst types are both integers of differing types.
5593 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005594 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005595 if (SrcBitSize < DstBitSize) {
5596 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005597
Dan Gohman475871a2008-07-27 21:46:04 +00005598 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005599 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005600 i += NumInputsPerOutput) {
5601 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005602 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005603 bool EltIsUndef = true;
5604 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5605 // Shift the previously computed bits over.
5606 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005607 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005608 if (Op.getOpcode() == ISD::UNDEF) continue;
5609 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005610
Jay Foad40f8f622010-12-07 08:25:19 +00005611 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005612 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005613 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005614
Chris Lattner6258fb22006-04-02 02:53:43 +00005615 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005616 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005617 else
5618 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5619 }
5620
Owen Anderson23b9b192009-08-12 00:36:31 +00005621 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Evan Chenga87008d2009-02-25 22:49:59 +00005622 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5623 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005624 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005625
Chris Lattner6258fb22006-04-02 02:53:43 +00005626 // Finally, this must be the case where we are shrinking elements: each input
5627 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005628 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005629 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005630 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5631 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005632 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005633
Dan Gohman7f321562007-06-25 16:23:39 +00005634 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005635 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5636 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005637 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005638 continue;
5639 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005640
Jay Foad40f8f622010-12-07 08:25:19 +00005641 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5642 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005643
Chris Lattner6258fb22006-04-02 02:53:43 +00005644 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00005645 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005646 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00005647 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00005648 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Bill Wendlingb0162f52009-01-30 22:53:48 +00005649 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
5650 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00005651 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005652 }
5653
5654 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00005655 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00005656 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
5657 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005658
Evan Chenga87008d2009-02-25 22:49:59 +00005659 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5660 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005661}
5662
Dan Gohman475871a2008-07-27 21:46:04 +00005663SDValue DAGCombiner::visitFADD(SDNode *N) {
5664 SDValue N0 = N->getOperand(0);
5665 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005666 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5667 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005668 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005669
Dan Gohman7f321562007-06-25 16:23:39 +00005670 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005671 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005672 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005673 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005674 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005675
Lang Hames01806942012-06-14 20:37:15 +00005676 // fold (fadd c1, c2) -> c1 + c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005677 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005678 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005679 // canonicalize constant to RHS
5680 if (N0CFP && !N1CFP)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005681 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0);
5682 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005683 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5684 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005685 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005686 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005687 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005688 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005689 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005690 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00005691 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Andersonafd3d562012-03-06 00:29:31 +00005692 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005693 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005694 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00005695 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005696
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005697 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005698 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5699 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
5700 isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlingb0162f52009-01-30 22:53:48 +00005701 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005702 DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5703 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005704
Owen Anderson43da6c72012-08-30 23:35:16 +00005705 // In unsafe math mode, we can fold chains of FADD's of the same value
5706 // into multiplications. This transform is not safe in general because
5707 // we are reducing the number of rounding steps.
5708 if (DAG.getTarget().Options.UnsafeFPMath &&
5709 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
5710 !N0CFP && !N1CFP) {
5711 if (N0.getOpcode() == ISD::FMUL) {
5712 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
5713 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
5714
5715 // (fadd (fmul c, x), x) -> (fmul c+1, x)
5716 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
5717 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5718 SDValue(CFP00, 0),
5719 DAG.getConstantFP(1.0, VT));
5720 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5721 N1, NewCFP);
5722 }
5723
5724 // (fadd (fmul x, c), x) -> (fmul c+1, x)
5725 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
5726 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5727 SDValue(CFP01, 0),
5728 DAG.getConstantFP(1.0, VT));
5729 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5730 N1, NewCFP);
5731 }
5732
5733 // (fadd (fadd x, x), x) -> (fmul 3.0, x)
5734 if (!CFP00 && !CFP01 && N0.getOperand(0) == N0.getOperand(1) &&
5735 N0.getOperand(0) == N1) {
5736 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5737 N1, DAG.getConstantFP(3.0, VT));
5738 }
5739
5740 // (fadd (fmul c, x), (fadd x, x)) -> (fmul c+2, x)
5741 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
5742 N1.getOperand(0) == N1.getOperand(1) &&
5743 N0.getOperand(1) == N1.getOperand(0)) {
5744 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5745 SDValue(CFP00, 0),
5746 DAG.getConstantFP(2.0, VT));
5747 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5748 N0.getOperand(1), NewCFP);
5749 }
5750
5751 // (fadd (fmul x, c), (fadd x, x)) -> (fmul c+2, x)
5752 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
5753 N1.getOperand(0) == N1.getOperand(1) &&
5754 N0.getOperand(0) == N1.getOperand(0)) {
5755 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5756 SDValue(CFP01, 0),
5757 DAG.getConstantFP(2.0, VT));
5758 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5759 N0.getOperand(0), NewCFP);
5760 }
5761 }
5762
5763 if (N1.getOpcode() == ISD::FMUL) {
5764 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
5765 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
5766
5767 // (fadd x, (fmul c, x)) -> (fmul c+1, x)
5768 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
5769 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5770 SDValue(CFP10, 0),
5771 DAG.getConstantFP(1.0, VT));
5772 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5773 N0, NewCFP);
5774 }
5775
5776 // (fadd x, (fmul x, c)) -> (fmul c+1, x)
5777 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
5778 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5779 SDValue(CFP11, 0),
5780 DAG.getConstantFP(1.0, VT));
5781 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5782 N0, NewCFP);
5783 }
5784
5785 // (fadd x, (fadd x, x)) -> (fmul 3.0, x)
5786 if (!CFP10 && !CFP11 && N1.getOperand(0) == N1.getOperand(1) &&
5787 N1.getOperand(0) == N0) {
5788 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5789 N0, DAG.getConstantFP(3.0, VT));
5790 }
5791
5792 // (fadd (fadd x, x), (fmul c, x)) -> (fmul c+2, x)
5793 if (CFP10 && !CFP11 && N1.getOpcode() == ISD::FADD &&
5794 N1.getOperand(0) == N1.getOperand(1) &&
5795 N0.getOperand(1) == N1.getOperand(0)) {
5796 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5797 SDValue(CFP10, 0),
5798 DAG.getConstantFP(2.0, VT));
5799 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5800 N0.getOperand(1), NewCFP);
5801 }
5802
5803 // (fadd (fadd x, x), (fmul x, c)) -> (fmul c+2, x)
5804 if (CFP11 && !CFP10 && N1.getOpcode() == ISD::FADD &&
5805 N1.getOperand(0) == N1.getOperand(1) &&
5806 N0.getOperand(0) == N1.getOperand(0)) {
5807 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5808 SDValue(CFP11, 0),
5809 DAG.getConstantFP(2.0, VT));
5810 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5811 N0.getOperand(0), NewCFP);
5812 }
5813 }
5814
5815 // (fadd (fadd x, x), (fadd x, x)) -> (fmul 4.0, x)
5816 if (N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
5817 N0.getOperand(0) == N0.getOperand(1) &&
5818 N1.getOperand(0) == N1.getOperand(1) &&
5819 N0.getOperand(0) == N1.getOperand(0)) {
5820 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5821 N0.getOperand(0),
5822 DAG.getConstantFP(4.0, VT));
5823 }
5824 }
5825
Lang Hamesd693caf2012-06-19 22:51:23 +00005826 // FADD -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00005827 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00005828 DAG.getTarget().Options.UnsafeFPMath) &&
5829 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005830 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00005831
5832 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
5833 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) {
5834 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT,
5835 N0.getOperand(0), N0.getOperand(1), N1);
5836 }
Owen Anderson43da6c72012-08-30 23:35:16 +00005837
Michael Liaob79bff52012-09-01 04:09:16 +00005838 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hamesd693caf2012-06-19 22:51:23 +00005839 // Note: Commutes FADD operands.
5840 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) {
5841 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT,
5842 N1.getOperand(0), N1.getOperand(1), N0);
5843 }
5844 }
5845
Dan Gohman475871a2008-07-27 21:46:04 +00005846 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005847}
5848
Dan Gohman475871a2008-07-27 21:46:04 +00005849SDValue DAGCombiner::visitFSUB(SDNode *N) {
5850 SDValue N0 = N->getOperand(0);
5851 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005852 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5853 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005854 EVT VT = N->getValueType(0);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005855 DebugLoc dl = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00005856
Dan Gohman7f321562007-06-25 16:23:39 +00005857 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005858 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005859 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005860 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005861 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005862
Nate Begemana0e221d2005-10-18 00:28:13 +00005863 // fold (fsub c1, c2) -> c1-c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005864 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005865 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005866 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005867 if (DAG.getTarget().Options.UnsafeFPMath &&
5868 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00005869 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005870 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005871 if (DAG.getTarget().Options.UnsafeFPMath &&
5872 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Andersonafd3d562012-03-06 00:29:31 +00005873 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00005874 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00005875 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005876 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00005877 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005878 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005879 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005880 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005881 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005882
Bill Wendling5a894342012-03-15 05:12:00 +00005883 // If 'unsafe math' is enabled, fold
Owen Anderson713e9532012-05-07 20:51:25 +00005884 // (fsub x, x) -> 0.0 &
Bill Wendling5a894342012-03-15 05:12:00 +00005885 // (fsub x, (fadd x, y)) -> (fneg y) &
5886 // (fsub x, (fadd y, x)) -> (fneg y)
5887 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Anderson713e9532012-05-07 20:51:25 +00005888 if (N0 == N1)
5889 return DAG.getConstantFP(0.0f, VT);
5890
Bill Wendling5a894342012-03-15 05:12:00 +00005891 if (N1.getOpcode() == ISD::FADD) {
5892 SDValue N10 = N1->getOperand(0);
5893 SDValue N11 = N1->getOperand(1);
5894
5895 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
5896 &DAG.getTarget().Options))
5897 return GetNegatedExpression(N11, DAG, LegalOperations);
5898 else if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
5899 &DAG.getTarget().Options))
5900 return GetNegatedExpression(N10, DAG, LegalOperations);
5901 }
5902 }
5903
Lang Hamesd693caf2012-06-19 22:51:23 +00005904 // FSUB -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00005905 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00005906 DAG.getTarget().Options.UnsafeFPMath) &&
5907 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005908 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00005909
5910 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
5911 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005912 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00005913 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005914 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hamesd693caf2012-06-19 22:51:23 +00005915 }
5916
5917 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
5918 // Note: Commutes FSUB operands.
5919 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005920 return DAG.getNode(ISD::FMA, dl, VT,
5921 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00005922 N1.getOperand(0)),
5923 N1.getOperand(1), N0);
5924 }
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005925
5926 // fold (fsub (-(fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
5927 if (N0.getOpcode() == ISD::FNEG &&
5928 N0.getOperand(0).getOpcode() == ISD::FMUL &&
5929 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
5930 SDValue N00 = N0.getOperand(0).getOperand(0);
5931 SDValue N01 = N0.getOperand(0).getOperand(1);
5932 return DAG.getNode(ISD::FMA, dl, VT,
5933 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
5934 DAG.getNode(ISD::FNEG, dl, VT, N1));
5935 }
Lang Hamesd693caf2012-06-19 22:51:23 +00005936 }
5937
Dan Gohman475871a2008-07-27 21:46:04 +00005938 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005939}
5940
Dan Gohman475871a2008-07-27 21:46:04 +00005941SDValue DAGCombiner::visitFMUL(SDNode *N) {
5942 SDValue N0 = N->getOperand(0);
5943 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00005944 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5945 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005946 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00005947 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00005948
Dan Gohman7f321562007-06-25 16:23:39 +00005949 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005950 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005951 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005952 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005953 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005954
Nate Begeman11af4ea2005-10-17 20:40:11 +00005955 // fold (fmul c1, c2) -> c1*c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005956 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005957 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00005958 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00005959 if (N0CFP && !N1CFP)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005960 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0);
5961 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005962 if (DAG.getTarget().Options.UnsafeFPMath &&
5963 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005964 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00005965 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005966 if (DAG.getTarget().Options.UnsafeFPMath &&
5967 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00005968 return N1;
Owen Anderson363e4b92012-05-02 21:32:35 +00005969 // fold (fmul A, 1.0) -> A
5970 if (N1CFP && N1CFP->isExactlyValue(1.0))
5971 return N0;
Nate Begeman11af4ea2005-10-17 20:40:11 +00005972 // fold (fmul X, 2.0) -> (fadd X, X)
5973 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005974 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00005975 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00005976 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00005977 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005978 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005979
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005980 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00005981 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005982 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00005983 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005984 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00005985 // Both can be negated for free, check to see if at least one is cheaper
5986 // negated.
5987 if (LHSNeg == 2 || RHSNeg == 2)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005988 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00005989 GetNegatedExpression(N0, DAG, LegalOperations),
5990 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00005991 }
5992 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005993
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005994 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005995 if (DAG.getTarget().Options.UnsafeFPMath &&
5996 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005997 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005998 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00005999 DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Dale Johannesende064702009-02-06 21:50:26 +00006000 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006001
Dan Gohman475871a2008-07-27 21:46:04 +00006002 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006003}
6004
Owen Anderson062c0a52012-05-02 22:17:40 +00006005SDValue DAGCombiner::visitFMA(SDNode *N) {
6006 SDValue N0 = N->getOperand(0);
6007 SDValue N1 = N->getOperand(1);
6008 SDValue N2 = N->getOperand(2);
6009 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6010 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6011 EVT VT = N->getValueType(0);
Owen Anderson58d57292012-09-01 06:04:27 +00006012 DebugLoc dl = N->getDebugLoc();
Owen Anderson062c0a52012-05-02 22:17:40 +00006013
6014 if (N0CFP && N0CFP->isExactlyValue(1.0))
6015 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N2);
6016 if (N1CFP && N1CFP->isExactlyValue(1.0))
6017 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N2);
6018
Owen Anderson85ef6f42012-05-30 18:50:39 +00006019 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Andersonf917d202012-05-30 18:54:50 +00006020 if (N0CFP && !N1CFP)
Owen Anderson85ef6f42012-05-30 18:50:39 +00006021 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT, N1, N0, N2);
6022
Owen Anderson58d57292012-09-01 06:04:27 +00006023 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6024 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6025 N2.getOpcode() == ISD::FMUL &&
6026 N0 == N2.getOperand(0) &&
6027 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6028 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6029 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6030 }
6031
6032
6033 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6034 if (DAG.getTarget().Options.UnsafeFPMath &&
6035 N0.getOpcode() == ISD::FMUL && N1CFP &&
6036 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6037 return DAG.getNode(ISD::FMA, dl, VT,
6038 N0.getOperand(0),
6039 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6040 N2);
6041 }
6042
6043 // (fma x, 1, y) -> (fadd x, y)
6044 // (fma x, -1, y) -> (fadd (fneg x), y)
6045 if (N1CFP) {
6046 if (N1CFP->isExactlyValue(1.0))
6047 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6048
6049 if (N1CFP->isExactlyValue(-1.0) &&
6050 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6051 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6052 AddToWorkList(RHSNeg.getNode());
6053 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6054 }
6055 }
6056
6057 // (fma x, c, x) -> (fmul x, (c+1))
6058 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2) {
6059 return DAG.getNode(ISD::FMUL, dl, VT,
6060 N0,
6061 DAG.getNode(ISD::FADD, dl, VT,
6062 N1, DAG.getConstantFP(1.0, VT)));
6063 }
6064
6065 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6066 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6067 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0) {
6068 return DAG.getNode(ISD::FMUL, dl, VT,
6069 N0,
6070 DAG.getNode(ISD::FADD, dl, VT,
6071 N1, DAG.getConstantFP(-1.0, VT)));
6072 }
6073
6074
Owen Anderson062c0a52012-05-02 22:17:40 +00006075 return SDValue();
6076}
6077
Dan Gohman475871a2008-07-27 21:46:04 +00006078SDValue DAGCombiner::visitFDIV(SDNode *N) {
6079 SDValue N0 = N->getOperand(0);
6080 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006081 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6082 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006083 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006084 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006085
Dan Gohman7f321562007-06-25 16:23:39 +00006086 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006087 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006088 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006089 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006090 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006091
Nate Begemana148d982006-01-18 22:35:16 +00006092 // fold (fdiv c1, c2) -> c1/c2
Owen Anderson825b72b2009-08-11 20:47:22 +00006093 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006094 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006095
Duncan Sands3ef3fcf2012-04-08 18:08:12 +00006096 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
6097 if (N1CFP && VT != MVT::ppcf128 && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands961d6662012-04-07 20:04:00 +00006098 // Compute the reciprocal 1.0 / c2.
6099 APFloat N1APF = N1CFP->getValueAPF();
6100 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6101 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands507bb7a2012-04-10 20:35:27 +00006102 // Only do the transform if the reciprocal is a legal fp immediate that
6103 // isn't too nasty (eg NaN, denormal, ...).
6104 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov999821c2012-04-10 13:22:49 +00006105 (!LegalOperations ||
6106 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6107 // backend)... we should handle this gracefully after Legalize.
6108 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6109 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6110 TLI.isFPImmLegal(Recip, VT)))
Duncan Sands961d6662012-04-07 20:04:00 +00006111 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0,
6112 DAG.getConstantFP(Recip, VT));
6113 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006114
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006115 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006116 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006117 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006118 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006119 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006120 // Both can be negated for free, check to see if at least one is cheaper
6121 // negated.
6122 if (LHSNeg == 2 || RHSNeg == 2)
Scott Michelfdc40a02009-02-17 22:15:04 +00006123 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006124 GetNegatedExpression(N0, DAG, LegalOperations),
6125 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006126 }
6127 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006128
Dan Gohman475871a2008-07-27 21:46:04 +00006129 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006130}
6131
Dan Gohman475871a2008-07-27 21:46:04 +00006132SDValue DAGCombiner::visitFREM(SDNode *N) {
6133 SDValue N0 = N->getOperand(0);
6134 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006135 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6136 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006137 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00006138
Nate Begemana148d982006-01-18 22:35:16 +00006139 // fold (frem c1, c2) -> fmod(c1,c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00006140 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006141 return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00006142
Dan Gohman475871a2008-07-27 21:46:04 +00006143 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006144}
6145
Dan Gohman475871a2008-07-27 21:46:04 +00006146SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6147 SDValue N0 = N->getOperand(0);
6148 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00006149 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6150 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006151 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00006152
Owen Anderson825b72b2009-08-11 20:47:22 +00006153 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006154 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006155
Chris Lattner12d83032006-03-05 05:30:57 +00006156 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00006157 const APFloat& V = N1CFP->getValueAPF();
Sylvestre Ledru94c22712012-09-27 10:14:43 +00006158 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6159 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00006160 if (!V.isNegative()) {
6161 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006162 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00006163 } else {
6164 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006165 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00006166 DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00006167 }
Chris Lattner12d83032006-03-05 05:30:57 +00006168 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006169
Chris Lattner12d83032006-03-05 05:30:57 +00006170 // copysign(fabs(x), y) -> copysign(x, y)
6171 // copysign(fneg(x), y) -> copysign(x, y)
6172 // copysign(copysign(x,z), y) -> copysign(x, y)
6173 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6174 N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006175 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6176 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00006177
6178 // copysign(x, abs(y)) -> abs(x)
6179 if (N1.getOpcode() == ISD::FABS)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006180 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006181
Chris Lattner12d83032006-03-05 05:30:57 +00006182 // copysign(x, copysign(y,z)) -> copysign(x, z)
6183 if (N1.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006184 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6185 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006186
Chris Lattner12d83032006-03-05 05:30:57 +00006187 // copysign(x, fp_extend(y)) -> copysign(x, y)
6188 // copysign(x, fp_round(y)) -> copysign(x, y)
6189 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006190 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6191 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006192
Dan Gohman475871a2008-07-27 21:46:04 +00006193 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00006194}
6195
Dan Gohman475871a2008-07-27 21:46:04 +00006196SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6197 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006198 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006199 EVT VT = N->getValueType(0);
6200 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00006201
Nate Begeman1d4d4142005-09-01 00:19:25 +00006202 // fold (sint_to_fp c1) -> c1fp
Stuart Hastings7e334182011-03-02 19:36:30 +00006203 if (N0C && OpVT != MVT::ppcf128 &&
6204 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006205 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006206 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006207 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006208
Chris Lattnercda88752008-06-26 00:16:49 +00006209 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6210 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006211 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6212 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006213 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006214 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006215 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006216 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006217
Nadav Rotemed1a3352012-07-23 07:59:50 +00006218 // The next optimizations are desireable only if SELECT_CC can be lowered.
6219 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6220 // having to say they don't support SELECT_CC on every type the DAG knows
6221 // about, since there is no way to mark an opcode illegal at all value types
6222 // (See also visitSELECT)
6223 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6224 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6225 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6226 !VT.isVector() &&
6227 (!LegalOperations ||
6228 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6229 SDValue Ops[] =
6230 { N0.getOperand(0), N0.getOperand(1),
6231 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6232 N0.getOperand(2) };
6233 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6234 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006235
Nadav Rotemed1a3352012-07-23 07:59:50 +00006236 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6237 // (select_cc x, y, 1.0, 0.0,, cc)
6238 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6239 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6240 (!LegalOperations ||
6241 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6242 SDValue Ops[] =
6243 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6244 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6245 N0.getOperand(0).getOperand(2) };
6246 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6247 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006248 }
6249
Dan Gohman475871a2008-07-27 21:46:04 +00006250 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006251}
6252
Dan Gohman475871a2008-07-27 21:46:04 +00006253SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6254 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006255 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006256 EVT VT = N->getValueType(0);
6257 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00006258
Nate Begeman1d4d4142005-09-01 00:19:25 +00006259 // fold (uint_to_fp c1) -> c1fp
Stuart Hastings7e334182011-03-02 19:36:30 +00006260 if (N0C && OpVT != MVT::ppcf128 &&
6261 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006262 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006263 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006264 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006265
Chris Lattnercda88752008-06-26 00:16:49 +00006266 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6267 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006268 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6269 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006270 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006271 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006272 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006273 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006274
Nadav Rotemed1a3352012-07-23 07:59:50 +00006275 // The next optimizations are desireable only if SELECT_CC can be lowered.
6276 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6277 // having to say they don't support SELECT_CC on every type the DAG knows
6278 // about, since there is no way to mark an opcode illegal at all value types
6279 // (See also visitSELECT)
6280 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6281 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond9bf71f2012-07-09 20:31:12 +00006282
Nadav Rotemed1a3352012-07-23 07:59:50 +00006283 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6284 (!LegalOperations ||
6285 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6286 SDValue Ops[] =
6287 { N0.getOperand(0), N0.getOperand(1),
6288 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6289 N0.getOperand(2) };
6290 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6291 }
6292 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006293
Dan Gohman475871a2008-07-27 21:46:04 +00006294 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006295}
6296
Dan Gohman475871a2008-07-27 21:46:04 +00006297SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6298 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006299 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006300 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006301
Nate Begeman1d4d4142005-09-01 00:19:25 +00006302 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00006303 if (N0CFP)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006304 return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0);
6305
Dan Gohman475871a2008-07-27 21:46:04 +00006306 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006307}
6308
Dan Gohman475871a2008-07-27 21:46:04 +00006309SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6310 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006311 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006312 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006313
Nate Begeman1d4d4142005-09-01 00:19:25 +00006314 // fold (fp_to_uint c1fp) -> c1
Owen Anderson825b72b2009-08-11 20:47:22 +00006315 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006316 return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0);
6317
Dan Gohman475871a2008-07-27 21:46:04 +00006318 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006319}
6320
Dan Gohman475871a2008-07-27 21:46:04 +00006321SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6322 SDValue N0 = N->getOperand(0);
6323 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006324 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006325 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006326
Nate Begeman1d4d4142005-09-01 00:19:25 +00006327 // fold (fp_round c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00006328 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006329 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006330
Chris Lattner79dbea52006-03-13 06:26:26 +00006331 // fold (fp_round (fp_extend x)) -> x
6332 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6333 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006334
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006335 // fold (fp_round (fp_round x)) -> (fp_round x)
6336 if (N0.getOpcode() == ISD::FP_ROUND) {
6337 // This is a value preserving truncation if both round's are.
6338 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006339 N0.getNode()->getConstantOperandVal(1) == 1;
Bill Wendling0225a1d2009-01-30 23:15:49 +00006340 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006341 DAG.getIntPtrConstant(IsTrunc));
6342 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006343
Chris Lattner79dbea52006-03-13 06:26:26 +00006344 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00006345 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Bill Wendling0225a1d2009-01-30 23:15:49 +00006346 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT,
6347 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00006348 AddToWorkList(Tmp.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00006349 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6350 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00006351 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006352
Dan Gohman475871a2008-07-27 21:46:04 +00006353 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006354}
6355
Dan Gohman475871a2008-07-27 21:46:04 +00006356SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6357 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006358 EVT VT = N->getValueType(0);
6359 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00006360 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006361
Nate Begeman1d4d4142005-09-01 00:19:25 +00006362 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00006363 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00006364 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006365 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006366 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006367
Dan Gohman475871a2008-07-27 21:46:04 +00006368 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006369}
6370
Dan Gohman475871a2008-07-27 21:46:04 +00006371SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6372 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006373 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006374 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006375
Chris Lattner5938bef2007-12-29 06:55:23 +00006376 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00006377 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00006378 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00006379 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00006380
Nate Begeman1d4d4142005-09-01 00:19:25 +00006381 // fold (fp_extend c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00006382 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006383 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006384
6385 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6386 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00006387 if (N0.getOpcode() == ISD::FP_ROUND
6388 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00006389 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006390 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006391 if (VT.bitsLT(In.getValueType()))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006392 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT,
6393 In, N0.getOperand(1));
6394 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00006395 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006396
Chris Lattner0bd48932008-01-17 07:00:52 +00006397 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00006398 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006399 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00006400 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00006401 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00006402 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006403 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00006404 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00006405 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00006406 LN0->isVolatile(), LN0->isNonTemporal(),
6407 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00006408 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006409 CombineTo(N0.getNode(),
6410 DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(),
6411 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00006412 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006413 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00006414 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006415
Dan Gohman475871a2008-07-27 21:46:04 +00006416 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006417}
6418
Dan Gohman475871a2008-07-27 21:46:04 +00006419SDValue DAGCombiner::visitFNEG(SDNode *N) {
6420 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006421 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00006422
Craig Topperdd201ff2012-09-11 01:45:21 +00006423 if (VT.isVector()) {
6424 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6425 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper956342b2012-09-09 22:58:45 +00006426 }
6427
Owen Andersonafd3d562012-03-06 00:29:31 +00006428 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6429 &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006430 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00006431
Chris Lattner3bd39d42008-01-27 17:42:27 +00006432 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6433 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006434 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006435 !VT.isVector() &&
6436 N0.getNode()->hasOneUse() &&
6437 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006438 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006439 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006440 if (IntVT.isInteger() && !IntVT.isVector()) {
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006441 Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int,
6442 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006443 AddToWorkList(Int.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006444 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006445 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006446 }
6447 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006448
Owen Anderson58d57292012-09-01 06:04:27 +00006449 // (fneg (fmul c, x)) -> (fmul -c, x)
6450 if (N0.getOpcode() == ISD::FMUL) {
6451 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
6452 if (CFP1) {
6453 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
6454 N0.getOperand(0),
6455 DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
6456 N0.getOperand(1)));
6457 }
6458 }
6459
Dan Gohman475871a2008-07-27 21:46:04 +00006460 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006461}
6462
Owen Anderson7c626d32012-08-13 23:32:49 +00006463SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6464 SDValue N0 = N->getOperand(0);
6465 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6466 EVT VT = N->getValueType(0);
6467
6468 // fold (fceil c1) -> fceil(c1)
6469 if (N0CFP && VT != MVT::ppcf128)
6470 return DAG.getNode(ISD::FCEIL, N->getDebugLoc(), VT, N0);
6471
6472 return SDValue();
6473}
6474
6475SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6476 SDValue N0 = N->getOperand(0);
6477 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6478 EVT VT = N->getValueType(0);
6479
6480 // fold (ftrunc c1) -> ftrunc(c1)
6481 if (N0CFP && VT != MVT::ppcf128)
6482 return DAG.getNode(ISD::FTRUNC, N->getDebugLoc(), VT, N0);
6483
6484 return SDValue();
6485}
6486
6487SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6488 SDValue N0 = N->getOperand(0);
6489 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6490 EVT VT = N->getValueType(0);
6491
6492 // fold (ffloor c1) -> ffloor(c1)
6493 if (N0CFP && VT != MVT::ppcf128)
6494 return DAG.getNode(ISD::FFLOOR, N->getDebugLoc(), VT, N0);
6495
6496 return SDValue();
6497}
6498
Dan Gohman475871a2008-07-27 21:46:04 +00006499SDValue DAGCombiner::visitFABS(SDNode *N) {
6500 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006501 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006502 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006503
Craig Topperdd201ff2012-09-11 01:45:21 +00006504 if (VT.isVector()) {
6505 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6506 if (FoldedVOp.getNode()) return FoldedVOp;
6507 }
6508
Nate Begeman1d4d4142005-09-01 00:19:25 +00006509 // fold (fabs c1) -> fabs(c1)
Owen Anderson825b72b2009-08-11 20:47:22 +00006510 if (N0CFP && VT != MVT::ppcf128)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006511 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006512 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006513 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00006514 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006515 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006516 // fold (fabs (fcopysign x, y)) -> (fabs x)
6517 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006518 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006519
Chris Lattner3bd39d42008-01-27 17:42:27 +00006520 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6521 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006522 if (!TLI.isFAbsFree(VT) &&
6523 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006524 N0.getOperand(0).getValueType().isInteger() &&
6525 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006526 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006527 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006528 if (IntVT.isInteger() && !IntVT.isVector()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006529 Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006530 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006531 AddToWorkList(Int.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006532 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bill Wendlingc0debad2009-01-30 23:27:35 +00006533 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006534 }
6535 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006536
Dan Gohman475871a2008-07-27 21:46:04 +00006537 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006538}
6539
Dan Gohman475871a2008-07-27 21:46:04 +00006540SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6541 SDValue Chain = N->getOperand(0);
6542 SDValue N1 = N->getOperand(1);
6543 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006544
Dan Gohmane0f06c72009-11-17 00:47:23 +00006545 // If N is a constant we could fold this into a fallthrough or unconditional
6546 // branch. However that doesn't happen very often in normal code, because
6547 // Instcombine/SimplifyCFG should have handled the available opportunities.
6548 // If we did this folding here, it would be necessary to update the
6549 // MachineBasicBlock CFG, which is awkward.
6550
Nate Begeman750ac1b2006-02-01 07:19:44 +00006551 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6552 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006553 if (N1.getOpcode() == ISD::SETCC &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006554 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
6555 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006556 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00006557 N1.getOperand(0), N1.getOperand(1), N2);
6558 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006559
Evan Cheng2a135ae2010-10-04 22:41:01 +00006560 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6561 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6562 (N1.getOperand(0).hasOneUse() &&
6563 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6564 SDNode *Trunc = 0;
6565 if (N1.getOpcode() == ISD::TRUNCATE) {
6566 // Look pass the truncate.
6567 Trunc = N1.getNode();
6568 N1 = N1.getOperand(0);
6569 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006570
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006571 // Match this pattern so that we can generate simpler code:
6572 //
6573 // %a = ...
6574 // %b = and i32 %a, 2
6575 // %c = srl i32 %b, 1
6576 // brcond i32 %c ...
6577 //
6578 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006579 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006580 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00006581 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006582 // %c = setcc eq %b, 0
6583 // brcond %c ...
6584 //
6585 // This applies only when the AND constant value has one bit set and the
6586 // SRL constant is equal to the log2 of the AND constant. The back-end is
6587 // smart enough to convert the result into a TEST/JMP sequence.
6588 SDValue Op0 = N1.getOperand(0);
6589 SDValue Op1 = N1.getOperand(1);
6590
6591 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006592 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006593 SDValue AndOp1 = Op0.getOperand(1);
6594
6595 if (AndOp1.getOpcode() == ISD::Constant) {
6596 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
6597
6598 if (AndConst.isPowerOf2() &&
6599 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
6600 SDValue SetCC =
6601 DAG.getSetCC(N->getDebugLoc(),
6602 TLI.getSetCCResultType(Op0.getValueType()),
6603 Op0, DAG.getConstant(0, Op0.getValueType()),
6604 ISD::SETNE);
6605
Evan Chengd40d03e2010-01-06 19:38:29 +00006606 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6607 MVT::Other, Chain, SetCC, N2);
6608 // Don't add the new BRCond into the worklist or else SimplifySelectCC
6609 // will convert it back to (X & C1) >> C2.
6610 CombineTo(N, NewBRCond, false);
6611 // Truncate is dead.
6612 if (Trunc) {
6613 removeFromWorkList(Trunc);
6614 DAG.DeleteNode(Trunc);
6615 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006616 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00006617 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006618 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006619 removeFromWorkList(N1.getNode());
6620 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00006621 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006622 }
6623 }
6624 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00006625
6626 if (Trunc)
6627 // Restore N1 if the above transformation doesn't match.
6628 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006629 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006630
Evan Cheng2c755ba2010-02-27 07:36:59 +00006631 // Transform br(xor(x, y)) -> br(x != y)
6632 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
6633 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
6634 SDNode *TheXor = N1.getNode();
6635 SDValue Op0 = TheXor->getOperand(0);
6636 SDValue Op1 = TheXor->getOperand(1);
6637 if (Op0.getOpcode() == Op1.getOpcode()) {
6638 // Avoid missing important xor optimizations.
6639 SDValue Tmp = visitXOR(TheXor);
Bill Wendling86c5abb2010-04-20 01:25:01 +00006640 if (Tmp.getNode() && Tmp.getNode() != TheXor) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006641 DEBUG(dbgs() << "\nReplacing.8 ";
6642 TheXor->dump(&DAG);
6643 dbgs() << "\nWith: ";
6644 Tmp.getNode()->dump(&DAG);
6645 dbgs() << '\n');
6646 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006647 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
Evan Cheng2c755ba2010-02-27 07:36:59 +00006648 removeFromWorkList(TheXor);
6649 DAG.DeleteNode(TheXor);
6650 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6651 MVT::Other, Chain, Tmp, N2);
6652 }
6653 }
6654
6655 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
6656 bool Equal = false;
6657 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
6658 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
6659 Op0.getOpcode() == ISD::XOR) {
6660 TheXor = Op0.getNode();
6661 Equal = true;
6662 }
6663
Evan Cheng2a135ae2010-10-04 22:41:01 +00006664 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00006665 if (LegalTypes)
6666 SetCCVT = TLI.getSetCCResultType(SetCCVT);
6667 SDValue SetCC = DAG.getSetCC(TheXor->getDebugLoc(),
6668 SetCCVT,
6669 Op0, Op1,
6670 Equal ? ISD::SETEQ : ISD::SETNE);
6671 // Replace the uses of XOR with SETCC
6672 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006673 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Cheng2a135ae2010-10-04 22:41:01 +00006674 removeFromWorkList(N1.getNode());
6675 DAG.DeleteNode(N1.getNode());
Evan Cheng2c755ba2010-02-27 07:36:59 +00006676 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6677 MVT::Other, Chain, SetCC, N2);
6678 }
6679 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006680
Dan Gohman475871a2008-07-27 21:46:04 +00006681 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006682}
6683
Chris Lattner3ea0b472005-10-05 06:47:48 +00006684// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
6685//
Dan Gohman475871a2008-07-27 21:46:04 +00006686SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00006687 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006688 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00006689
Dan Gohmane0f06c72009-11-17 00:47:23 +00006690 // If N is a constant we could fold this into a fallthrough or unconditional
6691 // branch. However that doesn't happen very often in normal code, because
6692 // Instcombine/SimplifyCFG should have handled the available opportunities.
6693 // If we did this folding here, it would be necessary to update the
6694 // MachineBasicBlock CFG, which is awkward.
6695
Duncan Sands8eab8a22008-06-09 11:32:28 +00006696 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00006697 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006698 CondLHS, CondRHS, CC->get(), N->getDebugLoc(),
6699 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00006700 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00006701
Nate Begemane17daeb2005-10-05 21:43:42 +00006702 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00006703 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Owen Anderson825b72b2009-08-11 20:47:22 +00006704 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006705 N->getOperand(0), Simp.getOperand(2),
6706 Simp.getOperand(0), Simp.getOperand(1),
6707 N->getOperand(4));
6708
Dan Gohman475871a2008-07-27 21:46:04 +00006709 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006710}
6711
Evan Chengc4b527a2012-01-13 01:37:24 +00006712/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
6713/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng03be3622012-03-06 23:33:32 +00006714/// addressing mode.
Evan Chengc4b527a2012-01-13 01:37:24 +00006715static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
6716 SelectionDAG &DAG,
6717 const TargetLowering &TLI) {
6718 EVT VT;
6719 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
6720 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
6721 return false;
6722 VT = Use->getValueType(0);
6723 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
6724 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
6725 return false;
6726 VT = ST->getValue().getValueType();
6727 } else
6728 return false;
6729
6730 TargetLowering::AddrMode AM;
6731 if (N->getOpcode() == ISD::ADD) {
6732 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6733 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00006734 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00006735 AM.BaseOffs = Offset->getSExtValue();
6736 else
Evan Cheng03be3622012-03-06 23:33:32 +00006737 // [reg +/- reg]
6738 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00006739 } else if (N->getOpcode() == ISD::SUB) {
6740 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6741 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00006742 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00006743 AM.BaseOffs = -Offset->getSExtValue();
6744 else
Evan Cheng03be3622012-03-06 23:33:32 +00006745 // [reg +/- reg]
6746 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00006747 } else
6748 return false;
6749
6750 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
6751}
6752
Duncan Sandsec87aa82008-06-15 20:12:31 +00006753/// CombineToPreIndexedLoadStore - Try turning a load / store into a
6754/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00006755/// and it has other uses besides the load / store. After the
6756/// transformation, the new indexed load / store has effectively folded
6757/// the add / subtract in and all of its other uses are redirected to the
6758/// new load / store.
6759bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00006760 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00006761 return false;
6762
6763 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006764 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00006765 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00006766 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006767 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006768 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006769 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00006770 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00006771 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
6772 return false;
6773 Ptr = LD->getBasePtr();
6774 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006775 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006776 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006777 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006778 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
6779 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
6780 return false;
6781 Ptr = ST->getBasePtr();
6782 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006783 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00006784 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006785 }
Chris Lattner448f2192006-11-11 00:39:41 +00006786
Chris Lattner9f1794e2006-11-11 00:56:29 +00006787 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
6788 // out. There is no reason to make this a preinc/predec.
6789 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00006790 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00006791 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00006792
Chris Lattner9f1794e2006-11-11 00:56:29 +00006793 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00006794 SDValue BasePtr;
6795 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006796 ISD::MemIndexedMode AM = ISD::UNINDEXED;
6797 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
6798 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00006799 // Don't create a indexed load / store with zero offset.
6800 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00006801 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00006802 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006803
Chris Lattner41e53fd2006-11-11 01:00:15 +00006804 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00006805 // 1) The new base ptr is a frame index.
6806 // 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 +00006807 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00006808 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00006809 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00006810 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00006811
Chris Lattner41e53fd2006-11-11 01:00:15 +00006812 // Check #1. Preinc'ing a frame index would require copying the stack pointer
6813 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00006814 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00006815 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006816
Chris Lattner41e53fd2006-11-11 01:00:15 +00006817 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00006818 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00006819 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006820 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006821 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00006822 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006823
Evan Chengc843abe2007-05-24 02:35:39 +00006824 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00006825 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00006826
6827 // Caches for hasPredecessorHelper
6828 SmallPtrSet<const SDNode *, 32> Visited;
6829 SmallVector<const SDNode *, 16> Worklist;
6830
Gabor Greifba36cb52008-08-28 21:40:38 +00006831 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
6832 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006833 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006834 if (Use == N)
6835 continue;
Lang Hames944520f2011-07-07 04:31:51 +00006836 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006837 return false;
6838
Evan Chengc4b527a2012-01-13 01:37:24 +00006839 // If Ptr may be folded in addressing mode of other use, then it's
6840 // not profitable to do this transformation.
6841 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006842 RealUse = true;
6843 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006844
Chris Lattner9f1794e2006-11-11 00:56:29 +00006845 if (!RealUse)
6846 return false;
6847
Dan Gohman475871a2008-07-27 21:46:04 +00006848 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006849 if (isLoad)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006850 Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
6851 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006852 else
Bill Wendlingc0debad2009-01-30 23:27:35 +00006853 Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
6854 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006855 ++PreIndexedNodes;
6856 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00006857 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006858 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006859 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006860 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006861 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006862 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006863 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006864 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
6865 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00006866 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006867 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner9f1794e2006-11-11 00:56:29 +00006868 }
6869
Chris Lattner9f1794e2006-11-11 00:56:29 +00006870 // Finally, since the node is now dead, remove it from the graph.
6871 DAG.DeleteNode(N);
6872
6873 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006874 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greifba36cb52008-08-28 21:40:38 +00006875 removeFromWorkList(Ptr.getNode());
6876 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00006877
6878 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00006879}
6880
Duncan Sandsec87aa82008-06-15 20:12:31 +00006881/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00006882/// add / sub of the base pointer node into a post-indexed load / store.
6883/// The transformation folded the add / subtract into the new indexed
6884/// load / store effectively and all of its uses are redirected to the
6885/// new load / store.
6886bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00006887 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00006888 return false;
6889
6890 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006891 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00006892 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00006893 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006894 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006895 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006896 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006897 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
6898 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
6899 return false;
6900 Ptr = LD->getBasePtr();
6901 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006902 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006903 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006904 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006905 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
6906 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
6907 return false;
6908 Ptr = ST->getBasePtr();
6909 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006910 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00006911 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006912 }
Chris Lattner448f2192006-11-11 00:39:41 +00006913
Gabor Greifba36cb52008-08-28 21:40:38 +00006914 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00006915 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006916
Gabor Greifba36cb52008-08-28 21:40:38 +00006917 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
6918 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006919 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006920 if (Op == N ||
6921 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
6922 continue;
6923
Dan Gohman475871a2008-07-27 21:46:04 +00006924 SDValue BasePtr;
6925 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006926 ISD::MemIndexedMode AM = ISD::UNINDEXED;
6927 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00006928 // Don't create a indexed load / store with zero offset.
6929 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00006930 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00006931 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00006932
Chris Lattner9f1794e2006-11-11 00:56:29 +00006933 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00006934 // 1) All uses are load / store ops that use it as base ptr (and
6935 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00006936 // 2) Op must be independent of N, i.e. Op is neither a predecessor
6937 // nor a successor of N. Otherwise, if Op is folded that would
6938 // create a cycle.
6939
Evan Chengcaab1292009-05-06 18:25:01 +00006940 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
6941 continue;
6942
Chris Lattner9f1794e2006-11-11 00:56:29 +00006943 // Check for #1.
6944 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00006945 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
6946 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00006947 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00006948 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00006949 continue;
6950
Chris Lattner9f1794e2006-11-11 00:56:29 +00006951 // If all the uses are load / store addresses, then don't do the
6952 // transformation.
6953 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
6954 bool RealUse = false;
6955 for (SDNode::use_iterator III = Use->use_begin(),
6956 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00006957 SDNode *UseUse = *III;
Evan Chengc4b527a2012-01-13 01:37:24 +00006958 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006959 RealUse = true;
6960 }
Chris Lattner448f2192006-11-11 00:39:41 +00006961
Chris Lattner9f1794e2006-11-11 00:56:29 +00006962 if (!RealUse) {
6963 TryNext = true;
6964 break;
Chris Lattner448f2192006-11-11 00:39:41 +00006965 }
6966 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006967 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006968
Chris Lattner9f1794e2006-11-11 00:56:29 +00006969 if (TryNext)
6970 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00006971
Chris Lattner9f1794e2006-11-11 00:56:29 +00006972 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00006973 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006974 SDValue Result = isLoad
Bill Wendlingc0debad2009-01-30 23:27:35 +00006975 ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
6976 BasePtr, Offset, AM)
6977 : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
6978 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006979 ++PostIndexedNodes;
6980 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00006981 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006982 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006983 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006984 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006985 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006986 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006987 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006988 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
6989 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00006990 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006991 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner448f2192006-11-11 00:39:41 +00006992 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006993
Chris Lattner9f1794e2006-11-11 00:56:29 +00006994 // Finally, since the node is now dead, remove it from the graph.
6995 DAG.DeleteNode(N);
6996
6997 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00006998 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006999 Result.getValue(isLoad ? 1 : 0));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007000 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007001 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007002 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007003 }
7004 }
7005 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007006
Chris Lattner448f2192006-11-11 00:39:41 +00007007 return false;
7008}
7009
Dan Gohman475871a2008-07-27 21:46:04 +00007010SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00007011 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007012 SDValue Chain = LD->getChain();
7013 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007014
Evan Cheng45a7ca92007-05-01 00:38:21 +00007015 // If load is not volatile and there are no uses of the loaded value (and
7016 // the updated indexed value in case of indexed loads), change uses of the
7017 // chain value into uses of the chain input (i.e. delete the dead load).
7018 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007019 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00007020 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00007021 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00007022 // It's not safe to use the two value CombineTo variant here. e.g.
7023 // v1, chain2 = load chain1, loc
7024 // v2, chain3 = load chain2, loc
7025 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00007026 // Now we replace use of chain2 with chain1. This makes the second load
7027 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00007028 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007029 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007030 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007031 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007032 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007033 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007034 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendlingc0debad2009-01-30 23:27:35 +00007035
Chris Lattner125991a2008-01-24 07:57:06 +00007036 if (N->use_empty()) {
7037 removeFromWorkList(N);
7038 DAG.DeleteNode(N);
7039 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007040
Dan Gohman475871a2008-07-27 21:46:04 +00007041 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00007042 }
Evan Cheng498f5592007-05-01 08:53:39 +00007043 } else {
7044 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00007045 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00007046 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007047 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00007048 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007049 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007050 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007051 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007052 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007053 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007054 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman475871a2008-07-27 21:46:04 +00007055 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007056 DAG.getUNDEF(N->getValueType(1)));
7057 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng02c42852008-01-16 23:11:54 +00007058 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00007059 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007060 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00007061 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00007062 }
7063 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007064
Chris Lattner01a22022005-10-10 22:04:48 +00007065 // If this load is directly stored, replace the load value with the stored
7066 // value.
7067 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007068 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00007069 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007070 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007071 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7072 if (PrevST->getBasePtr() == Ptr &&
7073 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007074 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00007075 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007076 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007077
Evan Cheng255f20f2010-04-01 06:04:33 +00007078 // Try to infer better alignment information than the load already has.
7079 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007080 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
7081 if (Align > LD->getAlignment())
7082 return DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(),
7083 LD->getValueType(0),
7084 Chain, Ptr, LD->getPointerInfo(),
7085 LD->getMemoryVT(),
7086 LD->isVolatile(), LD->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00007087 }
7088 }
7089
Jim Laskey7ca56af2006-10-11 13:47:09 +00007090 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007091 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007092 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007093
Jim Laskey6ff23e52006-10-04 16:53:27 +00007094 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007095 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007096 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007097
Jim Laskey279f0532006-09-25 16:29:54 +00007098 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007099 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Bill Wendlingc0debad2009-01-30 23:27:35 +00007100 ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(),
Chris Lattnerfa459012010-09-21 16:08:50 +00007101 BetterChain, Ptr, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007102 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007103 LD->isInvariant(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007104 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +00007105 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(),
7106 LD->getValueType(0),
Chris Lattnerfa459012010-09-21 16:08:50 +00007107 BetterChain, Ptr, LD->getPointerInfo(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007108 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007109 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007110 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00007111 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007112 }
Jim Laskey279f0532006-09-25 16:29:54 +00007113
Jim Laskey6ff23e52006-10-04 16:53:27 +00007114 // Create token factor to keep old chain connected.
Bill Wendlingc0debad2009-01-30 23:27:35 +00007115 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007116 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007117
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007118 // Make sure the new and old chains are cleaned up.
7119 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007120
Jim Laskey274062c2006-10-13 23:32:28 +00007121 // Replace uses with load result and token factor. Don't add users
7122 // to work list.
7123 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007124 }
7125 }
7126
Evan Cheng7fc033a2006-11-03 03:06:21 +00007127 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007128 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007129 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00007130
Dan Gohman475871a2008-07-27 21:46:04 +00007131 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00007132}
7133
Chris Lattner2392ae72010-04-15 04:48:01 +00007134/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
7135/// load is having specific bytes cleared out. If so, return the byte size
7136/// being masked out and the shift amount.
7137static std::pair<unsigned, unsigned>
7138CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
7139 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007140
Chris Lattner2392ae72010-04-15 04:48:01 +00007141 // Check for the structure we're looking for.
7142 if (V->getOpcode() != ISD::AND ||
7143 !isa<ConstantSDNode>(V->getOperand(1)) ||
7144 !ISD::isNormalLoad(V->getOperand(0).getNode()))
7145 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007146
Chris Lattnere6987582010-04-15 06:10:49 +00007147 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00007148 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00007149 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007150
Chris Lattnere6987582010-04-15 06:10:49 +00007151 // The store should be chained directly to the load or be an operand of a
7152 // tokenfactor.
7153 if (LD == Chain.getNode())
7154 ; // ok.
7155 else if (Chain->getOpcode() != ISD::TokenFactor)
7156 return Result; // Fail.
7157 else {
7158 bool isOk = false;
7159 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
7160 if (Chain->getOperand(i).getNode() == LD) {
7161 isOk = true;
7162 break;
7163 }
7164 if (!isOk) return Result;
7165 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007166
Chris Lattner2392ae72010-04-15 04:48:01 +00007167 // This only handles simple types.
7168 if (V.getValueType() != MVT::i16 &&
7169 V.getValueType() != MVT::i32 &&
7170 V.getValueType() != MVT::i64)
7171 return Result;
7172
7173 // Check the constant mask. Invert it so that the bits being masked out are
7174 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
7175 // follow the sign bit for uniformity.
7176 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
7177 unsigned NotMaskLZ = CountLeadingZeros_64(NotMask);
7178 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
7179 unsigned NotMaskTZ = CountTrailingZeros_64(NotMask);
7180 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
7181 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007182
Chris Lattner2392ae72010-04-15 04:48:01 +00007183 // See if we have a continuous run of bits. If so, we have 0*1+0*
7184 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
7185 return Result;
7186
7187 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
7188 if (V.getValueType() != MVT::i64 && NotMaskLZ)
7189 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007190
Chris Lattner2392ae72010-04-15 04:48:01 +00007191 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
7192 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007193 case 1:
7194 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00007195 case 4: break;
7196 default: return Result; // All one mask, or 5-byte mask.
7197 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007198
Chris Lattner2392ae72010-04-15 04:48:01 +00007199 // Verify that the first bit starts at a multiple of mask so that the access
7200 // is aligned the same as the access width.
7201 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007202
Chris Lattner2392ae72010-04-15 04:48:01 +00007203 Result.first = MaskedBytes;
7204 Result.second = NotMaskTZ/8;
7205 return Result;
7206}
7207
7208
7209/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
7210/// provides a value as specified by MaskInfo. If so, replace the specified
7211/// store with a narrower store of truncated IVal.
7212static SDNode *
7213ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
7214 SDValue IVal, StoreSDNode *St,
7215 DAGCombiner *DC) {
7216 unsigned NumBytes = MaskInfo.first;
7217 unsigned ByteShift = MaskInfo.second;
7218 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007219
Chris Lattner2392ae72010-04-15 04:48:01 +00007220 // Check to see if IVal is all zeros in the part being masked in by the 'or'
7221 // that uses this. If not, this is not a replacement.
7222 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
7223 ByteShift*8, (ByteShift+NumBytes)*8);
7224 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007225
Chris Lattner2392ae72010-04-15 04:48:01 +00007226 // Check that it is legal on the target to do this. It is legal if the new
7227 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
7228 // legalization.
7229 MVT VT = MVT::getIntegerVT(NumBytes*8);
7230 if (!DC->isTypeLegal(VT))
7231 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007232
Chris Lattner2392ae72010-04-15 04:48:01 +00007233 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
7234 // shifted by ByteShift and truncated down to NumBytes.
7235 if (ByteShift)
7236 IVal = DAG.getNode(ISD::SRL, IVal->getDebugLoc(), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00007237 DAG.getConstant(ByteShift*8,
7238 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00007239
7240 // Figure out the offset for the store and the alignment of the access.
7241 unsigned StOffset;
7242 unsigned NewAlign = St->getAlignment();
7243
7244 if (DAG.getTargetLoweringInfo().isLittleEndian())
7245 StOffset = ByteShift;
7246 else
7247 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007248
Chris Lattner2392ae72010-04-15 04:48:01 +00007249 SDValue Ptr = St->getBasePtr();
7250 if (StOffset) {
7251 Ptr = DAG.getNode(ISD::ADD, IVal->getDebugLoc(), Ptr.getValueType(),
7252 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
7253 NewAlign = MinAlign(NewAlign, StOffset);
7254 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007255
Chris Lattner2392ae72010-04-15 04:48:01 +00007256 // Truncate down to the new size.
7257 IVal = DAG.getNode(ISD::TRUNCATE, IVal->getDebugLoc(), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007258
Chris Lattner2392ae72010-04-15 04:48:01 +00007259 ++OpsNarrowed;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007260 return DAG.getStore(St->getChain(), St->getDebugLoc(), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007261 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00007262 false, false, NewAlign).getNode();
7263}
7264
Evan Cheng8b944d32009-05-28 00:35:15 +00007265
7266/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
7267/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
7268/// of the loaded bits, try narrowing the load and store if it would end up
7269/// being a win for performance or code size.
7270SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
7271 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00007272 if (ST->isVolatile())
7273 return SDValue();
7274
Evan Cheng8b944d32009-05-28 00:35:15 +00007275 SDValue Chain = ST->getChain();
7276 SDValue Value = ST->getValue();
7277 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00007278 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00007279
7280 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00007281 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007282
7283 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007284
Chris Lattner2392ae72010-04-15 04:48:01 +00007285 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
7286 // is a byte mask indicating a consecutive number of bytes, check to see if
7287 // Y is known to provide just those bytes. If so, we try to replace the
7288 // load + replace + store sequence with a single (narrower) store, which makes
7289 // the load dead.
7290 if (Opc == ISD::OR) {
7291 std::pair<unsigned, unsigned> MaskedLoad;
7292 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
7293 if (MaskedLoad.first)
7294 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7295 Value.getOperand(1), ST,this))
7296 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007297
Chris Lattner2392ae72010-04-15 04:48:01 +00007298 // Or is commutative, so try swapping X and Y.
7299 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
7300 if (MaskedLoad.first)
7301 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7302 Value.getOperand(0), ST,this))
7303 return SDValue(NewST, 0);
7304 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007305
Evan Cheng8b944d32009-05-28 00:35:15 +00007306 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
7307 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00007308 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007309
7310 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00007311 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
7312 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00007313 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00007314 if (LD->getBasePtr() != Ptr ||
7315 LD->getPointerInfo().getAddrSpace() !=
7316 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00007317 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007318
7319 // Find the type to narrow it the load / op / store to.
7320 SDValue N1 = Value.getOperand(1);
7321 unsigned BitWidth = N1.getValueSizeInBits();
7322 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
7323 if (Opc == ISD::AND)
7324 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00007325 if (Imm == 0 || Imm.isAllOnesValue())
7326 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007327 unsigned ShAmt = Imm.countTrailingZeros();
7328 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
7329 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00007330 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007331 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00007332 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00007333 TLI.isNarrowingProfitable(VT, NewVT))) {
7334 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00007335 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007336 }
Evan Chengcdcecc02009-05-28 18:41:02 +00007337 if (NewBW >= BitWidth)
7338 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007339
7340 // If the lsb changed does not start at the type bitwidth boundary,
7341 // start at the previous one.
7342 if (ShAmt % NewBW)
7343 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
7344 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, ShAmt + NewBW);
7345 if ((Imm & Mask) == Imm) {
7346 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
7347 if (Opc == ISD::AND)
7348 NewImm ^= APInt::getAllOnesValue(NewBW);
7349 uint64_t PtrOff = ShAmt / 8;
7350 // For big endian targets, we need to adjust the offset to the pointer to
7351 // load the correct bytes.
7352 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00007353 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00007354
7355 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007356 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Chris Lattner2392ae72010-04-15 04:48:01 +00007357 if (NewAlign < TLI.getTargetData()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00007358 return SDValue();
7359
Evan Cheng8b944d32009-05-28 00:35:15 +00007360 SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(),
7361 Ptr.getValueType(), Ptr,
7362 DAG.getConstant(PtrOff, Ptr.getValueType()));
7363 SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(),
7364 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007365 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007366 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007367 LD->isInvariant(), NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007368 SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD,
7369 DAG.getConstant(NewImm, NewVT));
7370 SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(),
7371 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007372 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007373 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007374
7375 AddToWorkList(NewPtr.getNode());
7376 AddToWorkList(NewLD.getNode());
7377 AddToWorkList(NewVal.getNode());
7378 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007379 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Cheng8b944d32009-05-28 00:35:15 +00007380 ++OpsNarrowed;
7381 return NewST;
7382 }
7383 }
7384
Evan Chengcdcecc02009-05-28 18:41:02 +00007385 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007386}
7387
Evan Cheng31959b12011-02-02 01:06:55 +00007388/// TransformFPLoadStorePair - For a given floating point load / store pair,
7389/// if the load value isn't used by any other operations, then consider
7390/// transforming the pair to integer load / store operations if the target
7391/// deems the transformation profitable.
7392SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
7393 StoreSDNode *ST = cast<StoreSDNode>(N);
7394 SDValue Chain = ST->getChain();
7395 SDValue Value = ST->getValue();
7396 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
7397 Value.hasOneUse() &&
7398 Chain == SDValue(Value.getNode(), 1)) {
7399 LoadSDNode *LD = cast<LoadSDNode>(Value);
7400 EVT VT = LD->getMemoryVT();
7401 if (!VT.isFloatingPoint() ||
7402 VT != ST->getMemoryVT() ||
7403 LD->isNonTemporal() ||
7404 ST->isNonTemporal() ||
7405 LD->getPointerInfo().getAddrSpace() != 0 ||
7406 ST->getPointerInfo().getAddrSpace() != 0)
7407 return SDValue();
7408
7409 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
7410 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
7411 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
7412 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
7413 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
7414 return SDValue();
7415
7416 unsigned LDAlign = LD->getAlignment();
7417 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007418 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Evan Cheng31959b12011-02-02 01:06:55 +00007419 unsigned ABIAlign = TLI.getTargetData()->getABITypeAlignment(IntVTTy);
7420 if (LDAlign < ABIAlign || STAlign < ABIAlign)
7421 return SDValue();
7422
7423 SDValue NewLD = DAG.getLoad(IntVT, Value.getDebugLoc(),
7424 LD->getChain(), LD->getBasePtr(),
7425 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007426 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00007427
7428 SDValue NewST = DAG.getStore(NewLD.getValue(1), N->getDebugLoc(),
7429 NewLD, ST->getBasePtr(),
7430 ST->getPointerInfo(),
7431 false, false, STAlign);
7432
7433 AddToWorkList(NewLD.getNode());
7434 AddToWorkList(NewST.getNode());
7435 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007436 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Cheng31959b12011-02-02 01:06:55 +00007437 ++LdStFP2Int;
7438 return NewST;
7439 }
7440
7441 return SDValue();
7442}
7443
Nadav Rotem72f7b082012-09-29 06:33:25 +00007444/// Returns the base pointer and an integer offset from that object.
7445static std::pair<SDValue, int64_t> GetPointerBaseAndOffset(SDValue Ptr) {
7446 if (Ptr->getOpcode() == ISD::ADD && isa<ConstantSDNode>(Ptr->getOperand(1))) {
7447 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
7448 SDValue Base = Ptr->getOperand(0);
7449 return std::make_pair(Base, Offset);
7450 }
7451
7452 return std::make_pair(Ptr, 0);
7453}
7454
7455struct ConsecutiveMemoryChainSorter {
7456 typedef std::pair<LSBaseSDNode*, int64_t> MemLink;
7457 bool operator()(MemLink LHS, MemLink RHS) {
7458 return LHS.second < RHS.second;
7459 }
7460};
7461
7462bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) {
7463 EVT MemVT = St->getMemoryVT();
7464 int64_t ElementSizeBytes = MemVT.getSizeInBits()/8;
7465
7466 // Don't handle vectors.
7467 if (MemVT.isVector() || !MemVT.isSimple())
7468 return false;
7469
7470 // Perform an early exit check. Do not bother looking at stored values that
7471 // are not constants or loads.
7472 SDValue StoredVal = St->getValue();
7473 if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) &&
7474 !isa<LoadSDNode>(StoredVal))
7475 return false;
7476
7477 // Is this a load-to-store or a const-store.
7478 bool IsLoadSrc = isa<LoadSDNode>(StoredVal);
7479
7480 // Only look at ends of store chains.
7481 SDValue Chain = SDValue(St, 1);
7482 if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE)
7483 return false;
7484
7485 // This holds the base pointer and the offset in bytes from the base pointer.
7486 std::pair<SDValue, int64_t> BasePtr =
7487 GetPointerBaseAndOffset(St->getBasePtr());
7488
7489 // We must have a base and an offset.
7490 if (!BasePtr.first.getNode())
7491 return false;
7492
7493 // Do not handle stores to undef base pointers.
7494 if (BasePtr.first.getOpcode() == ISD::UNDEF)
7495 return false;
7496
7497 SmallVector<std::pair<StoreSDNode*, int64_t>, 8> StoreNodes;
7498 // Walk up the chain and look for nodes with offsets from the same
7499 // base pointer. Stop when reaching an instruction with a different kind
7500 // or instruction which has a different base pointer.
7501 StoreSDNode *Index = St;
7502 while (Index) {
7503 // If the chain has more than one use, then we can't reorder the mem ops.
7504 if (Index != St && !SDValue(Index, 1)->hasOneUse())
7505 break;
7506
7507 // Find the base pointer and offset for this memory node.
7508 std::pair<SDValue, int64_t> Ptr =
7509 GetPointerBaseAndOffset(Index->getBasePtr());
7510
7511 // Check that the base pointer is the same as the original one.
7512 if (Ptr.first.getNode() != BasePtr.first.getNode())
7513 break;
7514
7515 // Check that the alignment is the same.
7516 if (Index->getAlignment() != St->getAlignment())
7517 break;
7518
7519 // The memory operands must not be volatile.
7520 if (Index->isVolatile() || Index->isIndexed())
7521 break;
7522
7523 // No truncation.
7524 if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index))
7525 if (St->isTruncatingStore())
7526 break;
7527
7528 // The stored memory type must be the same.
7529 if (Index->getMemoryVT() != MemVT)
7530 break;
7531
7532 // We found a potential memory operand to merge.
7533 StoreNodes.push_back(std::make_pair(Index,Ptr.second));
7534
7535 // Move up the chain to the next memory operation.
7536 Index = dyn_cast<StoreSDNode>(Index->getChain().getNode());
7537 }
7538
7539 // Check if there is anything to merge.
7540 if (StoreNodes.size() < 2)
7541 return false;
7542
7543 // Remember which node is the earliest node in the chain.
7544 LSBaseSDNode *EarliestOp = StoreNodes.back().first;
7545
7546 // Sort the memory operands according to their distance from the base pointer.
7547 std::sort(StoreNodes.begin(), StoreNodes.end(),
7548 ConsecutiveMemoryChainSorter());
7549
7550 // Scan the memory operations on the chain and find the first non-consecutive
7551 // store memory address.
7552 unsigned LastConsecutiveStore = 0;
7553 int64_t StartAddress = StoreNodes[0].second;
7554 for (unsigned i=1; i<StoreNodes.size(); ++i) {
7555 int64_t CurrAddress = StoreNodes[i].second;
7556 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
7557 break;
7558 LastConsecutiveStore = i;
7559 }
7560
7561 // Store the constants into memory as one consecutive store.
7562 if (!IsLoadSrc) {
7563 unsigned LastConst = 0;
7564 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
7565 SDValue StoredVal = StoreNodes[i].first->getValue();
7566 bool IsConst = (isa<ConstantSDNode>(StoredVal) || isa<ConstantFPSDNode>(StoredVal));
7567 if (!IsConst)
7568 break;
7569 LastConst = i;
7570 }
7571 unsigned NumElem = std::min(LastConsecutiveStore + 1, LastConst + 1);
7572 if (NumElem < 2)
7573 return false;
7574
7575 EVT JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
7576 DebugLoc DL = StoreNodes[0].first->getDebugLoc();
7577 SmallVector<SDValue, 8> Ops;
7578
7579 for (unsigned i = 0; i < NumElem ; ++i) {
7580 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].first);
7581 Ops.push_back(St->getValue());
7582 }
7583
7584 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL,
7585 JointMemOpVT, &Ops[0], Ops.size());
7586
7587 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, BV,
7588 EarliestOp->getBasePtr(),
7589 EarliestOp->getPointerInfo(), false, false,
7590 EarliestOp->getAlignment());
7591
7592 for (unsigned i = 0; i < NumElem ; ++i) {
7593 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].first);
7594 CombineTo(St, NewStore);
7595 }
7596 return true;
7597 }
7598
7599 // Look for load nodes wich are used by the stored values.
7600 SmallVector<std::pair<LoadSDNode*, int64_t>, 8> LoadNodes;
7601
7602 // Find acceptible loads. Loads need to have the same chain (token factor),
7603 // must not be zext, volatile, indexed, and they must be consecutive.
7604 SDValue LdBasePtr;
7605 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
7606 LoadSDNode *Ld = dyn_cast<LoadSDNode>(StoreNodes[i].first->getValue());
7607 if (!Ld) break;
7608
7609 // Loads must only have one use.
7610 if (!Ld->hasNUsesOfValue(1, 0))
7611 break;
7612
7613 // Check that the alignment is the same as the stores.
7614 if (Ld->getAlignment() != St->getAlignment())
7615 break;
7616
7617 // The memory operands must not be volatile.
7618 if (Ld->isVolatile() || Ld->isIndexed())
7619 break;
7620
7621 if (Ld->getExtensionType() != ISD::NON_EXTLOAD)
7622 break;
7623
7624 // The stored memory type must be the same.
7625 if (Ld->getMemoryVT() != MemVT)
7626 break;
7627
7628 std::pair<SDValue, int64_t> LdPtr =
7629 GetPointerBaseAndOffset(Ld->getBasePtr());
7630
7631 // If this is not the first ptr that we check.
7632 if (LdBasePtr.getNode()) {
7633 // The base ptr must be the same,
7634 if (LdPtr.first != LdBasePtr)
7635 break;
7636 } else {
7637 LdBasePtr = LdPtr.first;
7638 }
7639
7640 // We found a potential memory operand to merge.
7641 LoadNodes.push_back(std::make_pair(Ld, LdPtr.second));
7642 }
7643
7644 if (LoadNodes.size() < 2)
7645 return false;
7646
7647 // Scan the memory operations on the chain and find the first non-consecutive
7648 // load memory address.
7649 unsigned LastConsecutiveLoad = 0;
7650 StartAddress = LoadNodes[0].second;
7651 for (unsigned i=1; i<LoadNodes.size(); ++i) {
7652 int64_t CurrAddress = LoadNodes[i].second;
7653 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
7654 break;
7655 LastConsecutiveLoad = i;
7656 }
7657
7658 unsigned NumElem =
7659 std::min(LastConsecutiveStore + 1, LastConsecutiveLoad + 1);
7660
7661 EVT JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
7662 DebugLoc LoadDL = LoadNodes[0].first->getDebugLoc();
7663 DebugLoc StoreDL = StoreNodes[0].first->getDebugLoc();
7664
7665 LoadSDNode *FirstLoad = LoadNodes[0].first;
7666 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL,
7667 FirstLoad->getChain(),
7668 FirstLoad->getBasePtr(),
7669 FirstLoad->getPointerInfo(),
7670 false, false, false,
7671 FirstLoad->getAlignment());
7672
7673 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad,
7674 EarliestOp->getBasePtr(),
7675 EarliestOp->getPointerInfo(), false, false,
7676 EarliestOp->getAlignment());
7677
7678 for (unsigned i = 0; i < NumElem ; ++i) {
7679 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].first);
7680 CombineTo(St, NewStore);
7681 }
7682
7683 return true;
7684}
7685
Dan Gohman475871a2008-07-27 21:46:04 +00007686SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007687 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007688 SDValue Chain = ST->getChain();
7689 SDValue Value = ST->getValue();
7690 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007691
Evan Cheng59d5b682007-05-07 21:27:48 +00007692 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00007693 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007694 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00007695 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00007696 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007697 EVT SVT = Value.getOperand(0).getValueType();
Dan Gohman1ba519b2009-02-20 23:29:13 +00007698 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00007699 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007700 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00007701 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007702 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Bill Wendlingc144a572009-01-30 23:36:47 +00007703 return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00007704 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007705 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00007706 }
Owen Andersona34d9362011-04-14 17:30:49 +00007707
Chris Lattnerb3452ea2011-04-09 02:32:02 +00007708 // Turn 'store undef, Ptr' -> nothing.
7709 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
7710 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007711
Nate Begeman2cbba892006-12-11 02:23:46 +00007712 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00007713 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007714 // NOTE: If the original store is volatile, this transform must not increase
7715 // the number of stores. For example, on x86-32 an f64 can be stored in one
7716 // processor operation but an i64 (which is not legal) requires two. So the
7717 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00007718 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00007719 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007720 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007721 default: llvm_unreachable("Unknown FP type");
Pete Cooper438c0402012-06-21 18:00:39 +00007722 case MVT::f16: // We don't do this for these yet.
7723 case MVT::f80:
Owen Anderson825b72b2009-08-11 20:47:22 +00007724 case MVT::f128:
7725 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00007726 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007727 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00007728 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007729 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00007730 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00007731 bitcastToAPInt().getZExtValue(), MVT::i32);
Bill Wendlingc144a572009-01-30 23:36:47 +00007732 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007733 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007734 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00007735 }
7736 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007737 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00007738 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007739 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007740 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00007741 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00007742 getZExtValue(), MVT::i64);
Bill Wendlingc144a572009-01-30 23:36:47 +00007743 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007744 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007745 ST->isNonTemporal(), ST->getAlignment());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00007746 }
Owen Andersona34d9362011-04-14 17:30:49 +00007747
Chris Lattnerb3452ea2011-04-09 02:32:02 +00007748 if (!ST->isVolatile() &&
7749 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00007750 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00007751 // argument passing. Since this is so common, custom legalize the
7752 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00007753 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007754 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
7755 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00007756 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00007757
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00007758 unsigned Alignment = ST->getAlignment();
7759 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00007760 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00007761
Bill Wendlingc144a572009-01-30 23:36:47 +00007762 SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007763 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007764 isVolatile, isNonTemporal,
7765 ST->getAlignment());
Bill Wendlingc144a572009-01-30 23:36:47 +00007766 Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00007767 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00007768 Alignment = MinAlign(Alignment, 4U);
Bill Wendlingc144a572009-01-30 23:36:47 +00007769 SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007770 Ptr, ST->getPointerInfo().getWithOffset(4),
7771 isVolatile, isNonTemporal,
David Greene1e559442010-02-15 17:00:31 +00007772 Alignment);
Owen Anderson825b72b2009-08-11 20:47:22 +00007773 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00007774 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00007775 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007776
Chris Lattner62be1a72006-12-12 04:16:14 +00007777 break;
Evan Cheng25ece662006-12-11 17:25:19 +00007778 }
Nate Begeman2cbba892006-12-11 02:23:46 +00007779 }
Nate Begeman2cbba892006-12-11 02:23:46 +00007780 }
7781
Evan Cheng255f20f2010-04-01 06:04:33 +00007782 // Try to infer better alignment information than the store already has.
7783 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007784 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
7785 if (Align > ST->getAlignment())
7786 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value,
7787 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
7788 ST->isVolatile(), ST->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00007789 }
7790 }
7791
Evan Cheng31959b12011-02-02 01:06:55 +00007792 // Try transforming a pair floating point load / store ops to integer
7793 // load / store ops.
7794 SDValue NewST = TransformFPLoadStorePair(N);
7795 if (NewST.getNode())
7796 return NewST;
7797
Scott Michelfdc40a02009-02-17 22:15:04 +00007798 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007799 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007800 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007801
Jim Laskey6ff23e52006-10-04 16:53:27 +00007802 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007803 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007804 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007805
7806 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00007807 if (ST->isTruncatingStore()) {
Bill Wendlingc144a572009-01-30 23:36:47 +00007808 ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00007809 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007810 ST->getMemoryVT(), ST->isVolatile(),
7811 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00007812 } else {
Bill Wendlingc144a572009-01-30 23:36:47 +00007813 ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007814 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007815 ST->isVolatile(), ST->isNonTemporal(),
7816 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00007817 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007818
Jim Laskey279f0532006-09-25 16:29:54 +00007819 // Create token to keep both nodes around.
Bill Wendlingc144a572009-01-30 23:36:47 +00007820 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007821 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00007822
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007823 // Make sure the new and old chains are cleaned up.
7824 AddToWorkList(Token.getNode());
7825
Jim Laskey274062c2006-10-13 23:32:28 +00007826 // Don't add users to work list.
7827 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007828 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00007829 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007830
Evan Cheng33dbedc2006-11-05 09:31:14 +00007831 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007832 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007833 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00007834
Chris Lattner3c872852007-12-29 06:26:16 +00007835 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00007836 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00007837 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00007838 // See if we can simplify the input to this truncstore with knowledge that
7839 // only the low bits are being used. For example:
7840 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00007841 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007842 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00007843 APInt::getLowBitsSet(
7844 Value.getValueType().getScalarType().getSizeInBits(),
7845 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00007846 AddToWorkList(Value.getNode());
7847 if (Shorter.getNode())
Bill Wendlingc144a572009-01-30 23:36:47 +00007848 return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00007849 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00007850 ST->isVolatile(), ST->isNonTemporal(),
7851 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00007852
Chris Lattnere33544c2007-10-13 06:58:48 +00007853 // Otherwise, see if we can simplify the operation with
7854 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00007855 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00007856 APInt::getLowBitsSet(
7857 Value.getValueType().getScalarType().getSizeInBits(),
7858 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00007859 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00007860 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007861
Chris Lattner3c872852007-12-29 06:26:16 +00007862 // If this is a load followed by a store to the same location, then the store
7863 // is dead/noop.
7864 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007865 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00007866 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00007867 // There can't be any side effects between the load and store, such as
7868 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00007869 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00007870 // The store is dead, remove it.
7871 return Chain;
7872 }
7873 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007874
Chris Lattnerddf89562008-01-17 19:59:44 +00007875 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
7876 // truncating store. We can do this even if this is already a truncstore.
7877 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00007878 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00007879 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007880 ST->getMemoryVT())) {
Bill Wendlingc144a572009-01-30 23:36:47 +00007881 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0),
Chris Lattnerda2d8e12010-09-21 17:42:31 +00007882 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00007883 ST->isVolatile(), ST->isNonTemporal(),
7884 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00007885 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007886
Nadav Rotem72f7b082012-09-29 06:33:25 +00007887
7888 // Only perform this optimization before the types are legal, because we
7889 // don't want to generate illegal types in this optimization.
7890 if (!LegalTypes && MergeConsecutiveStores(ST))
7891 return SDValue(N, 0);
7892
Evan Cheng8b944d32009-05-28 00:35:15 +00007893 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00007894}
7895
Dan Gohman475871a2008-07-27 21:46:04 +00007896SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
7897 SDValue InVec = N->getOperand(0);
7898 SDValue InVal = N->getOperand(1);
7899 SDValue EltNo = N->getOperand(2);
Eli Friedman9db817f2011-09-09 21:04:06 +00007900 DebugLoc dl = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007901
Bob Wilson492fd452010-05-19 23:42:58 +00007902 // If the inserted element is an UNDEF, just use the input vector.
7903 if (InVal.getOpcode() == ISD::UNDEF)
7904 return InVec;
7905
Nadav Rotem609d54e2011-02-12 14:40:33 +00007906 EVT VT = InVec.getValueType();
7907
Owen Anderson95771af2011-02-25 21:41:48 +00007908 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00007909 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
7910 return SDValue();
7911
Eli Friedman9db817f2011-09-09 21:04:06 +00007912 // Check that we know which element is being inserted
7913 if (!isa<ConstantSDNode>(EltNo))
7914 return SDValue();
7915 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007916
Eli Friedman9db817f2011-09-09 21:04:06 +00007917 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
7918 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
7919 // vector elements.
7920 SmallVector<SDValue, 8> Ops;
7921 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
7922 Ops.append(InVec.getNode()->op_begin(),
7923 InVec.getNode()->op_end());
7924 } else if (InVec.getOpcode() == ISD::UNDEF) {
7925 unsigned NElts = VT.getVectorNumElements();
7926 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
7927 } else {
7928 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00007929 }
Eli Friedman9db817f2011-09-09 21:04:06 +00007930
7931 // Insert the element
7932 if (Elt < Ops.size()) {
7933 // All the operands of BUILD_VECTOR must have the same type;
7934 // we enforce that here.
7935 EVT OpVT = Ops[0].getValueType();
7936 if (InVal.getValueType() != OpVT)
7937 InVal = OpVT.bitsGT(InVal.getValueType()) ?
7938 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
7939 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
7940 Ops[Elt] = InVal;
7941 }
7942
7943 // Return the new vector
7944 return DAG.getNode(ISD::BUILD_VECTOR, dl,
7945 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00007946}
7947
Dan Gohman475871a2008-07-27 21:46:04 +00007948SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00007949 // (vextract (scalar_to_vector val, 0) -> val
7950 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00007951 EVT VT = InVec.getValueType();
7952 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00007953
Duncan Sandsc356f332011-05-09 08:03:33 +00007954 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
7955 // Check if the result type doesn't match the inserted element type. A
7956 // SCALAR_TO_VECTOR may truncate the inserted element and the
7957 // EXTRACT_VECTOR_ELT may widen the extracted vector.
7958 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00007959 if (InOp.getValueType() != NVT) {
7960 assert(InOp.getValueType().isInteger() && NVT.isInteger());
7961 return DAG.getSExtOrTrunc(InOp, InVec.getDebugLoc(), NVT);
7962 }
7963 return InOp;
7964 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007965
Nadav Rotemba05c912012-01-17 21:44:01 +00007966 SDValue EltNo = N->getOperand(1);
7967 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
7968
7969 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
7970 // We only perform this optimization before the op legalization phase because
Nadav Rotem6dfabb62012-09-20 08:53:31 +00007971 // we may introduce new vector instructions which are not backed by TD
7972 // patterns. For example on AVX, extracting elements from a wide vector
7973 // without using extract_subvector.
Nadav Rotemba05c912012-01-17 21:44:01 +00007974 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
7975 && ConstEltNo && !LegalOperations) {
7976 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
7977 int NumElem = VT.getVectorNumElements();
7978 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
7979 // Find the new index to extract from.
7980 int OrigElt = SVOp->getMaskElt(Elt);
7981
7982 // Extracting an undef index is undef.
7983 if (OrigElt == -1)
7984 return DAG.getUNDEF(NVT);
7985
7986 // Select the right vector half to extract from.
7987 if (OrigElt < NumElem) {
7988 InVec = InVec->getOperand(0);
7989 } else {
7990 InVec = InVec->getOperand(1);
7991 OrigElt -= NumElem;
7992 }
7993
Jim Grosbacha249f7d2012-05-08 20:56:07 +00007994 EVT IndexTy = N->getOperand(1).getValueType();
Nadav Rotemba05c912012-01-17 21:44:01 +00007995 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(), NVT,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00007996 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemba05c912012-01-17 21:44:01 +00007997 }
7998
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007999 // Perform only after legalization to ensure build_vector / vector_shuffle
8000 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00008001 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008002
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008003 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
8004 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
8005 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00008006
Nadav Rotemba05c912012-01-17 21:44:01 +00008007 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00008008 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00008009 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00008010 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00008011 EVT ExtVT = VT.getVectorElementType();
8012 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00008013
Evan Cheng84387ea2012-03-13 22:00:52 +00008014 // If the result of load has to be truncated, then it's not necessarily
8015 // profitable.
Evan Chenga03d3662012-03-13 22:16:11 +00008016 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng84387ea2012-03-13 22:00:52 +00008017 return SDValue();
8018
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008019 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008020 // Don't duplicate a load with other uses.
8021 if (!InVec.hasOneUse())
8022 return SDValue();
8023
Owen Andersone50ed302009-08-10 22:56:29 +00008024 EVT BCVT = InVec.getOperand(0).getValueType();
8025 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008026 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00008027 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
8028 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008029 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00008030 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008031 NewLoad = true;
8032 }
Evan Cheng513da432007-10-06 08:19:55 +00008033
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008034 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00008035 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00008036 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008037 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00008038 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00008039 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00008040 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008041 // Don't duplicate a load with other uses.
8042 if (!InVec.hasOneUse())
8043 return SDValue();
8044
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008045 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00008046 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008047 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
8048 // =>
8049 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00008050
Eli Friedmand6e25602011-12-26 22:49:32 +00008051 // Don't duplicate a load with other uses.
8052 if (!InVec.hasOneUse())
8053 return SDValue();
8054
Mon P Wanga60b5232008-12-11 00:26:16 +00008055 // If the bit convert changed the number of elements, it is unsafe
8056 // to examine the mask.
8057 if (BCNumEltsChanged)
8058 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00008059
8060 // Select the input vector, guarding against out of range extract vector.
8061 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00008062 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00008063 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
8064
Eli Friedmand6e25602011-12-26 22:49:32 +00008065 if (InVec.getOpcode() == ISD::BITCAST) {
8066 // Don't duplicate a load with other uses.
8067 if (!InVec.hasOneUse())
8068 return SDValue();
8069
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008070 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00008071 }
Gabor Greifba36cb52008-08-28 21:40:38 +00008072 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008073 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00008074 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00008075 }
8076 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008077
Eli Friedmand6e25602011-12-26 22:49:32 +00008078 // Make sure we found a non-volatile load and the extractelement is
8079 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00008080 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00008081 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008082
Eric Christopherd81f17a2010-11-03 20:44:42 +00008083 // If Idx was -1 above, Elt is going to be -1, so just return undef.
8084 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00008085 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00008086
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008087 unsigned Align = LN0->getAlignment();
8088 if (NewLoad) {
8089 // Check the resultant load doesn't need a higher alignment than the
8090 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00008091 unsigned NewAlign =
Eric Christopher503a64d2010-12-09 04:48:06 +00008092 TLI.getTargetData()
8093 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00008094
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008095 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00008096 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00008097
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008098 Align = NewAlign;
8099 }
8100
Dan Gohman475871a2008-07-27 21:46:04 +00008101 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00008102 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008103
Eric Christopherd81f17a2010-11-03 20:44:42 +00008104 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00008105 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00008106 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008107 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008108 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Bill Wendlingc144a572009-01-30 23:36:47 +00008109 NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008110 DAG.getConstant(PtrOff, PtrType));
8111 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008112
Eli Friedman4db4add2011-11-16 23:50:22 +00008113 // The replacement we need to do here is a little tricky: we need to
8114 // replace an extractelement of a load with a load.
8115 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00008116 // Note that this replacement assumes that the extractvalue is the only
8117 // use of the load; that's okay because we don't want to perform this
8118 // transformation in other cases anyway.
Evan Cheng84387ea2012-03-13 22:00:52 +00008119 SDValue Load;
Evan Chenga03d3662012-03-13 22:16:11 +00008120 SDValue Chain;
Evan Cheng84387ea2012-03-13 22:00:52 +00008121 if (NVT.bitsGT(LVT)) {
8122 // If the result type of vextract is wider than the load, then issue an
8123 // extending load instead.
8124 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
8125 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
8126 Load = DAG.getExtLoad(ExtType, N->getDebugLoc(), NVT, LN0->getChain(),
8127 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
8128 LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008129 Chain = Load.getValue(1);
8130 } else {
Evan Cheng84387ea2012-03-13 22:00:52 +00008131 Load = DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr,
8132 LN0->getPointerInfo().getWithOffset(PtrOff),
8133 LN0->isVolatile(), LN0->isNonTemporal(),
8134 LN0->isInvariant(), Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008135 Chain = Load.getValue(1);
8136 if (NVT.bitsLT(LVT))
8137 Load = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), NVT, Load);
8138 else
8139 Load = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), NVT, Load);
8140 }
Eli Friedman4db4add2011-11-16 23:50:22 +00008141 WorkListRemover DeadNodes(*this);
8142 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chenga03d3662012-03-13 22:16:11 +00008143 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00008144 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedman4db4add2011-11-16 23:50:22 +00008145 // Since we're explcitly calling ReplaceAllUses, add the new node to the
8146 // worklist explicitly as well.
8147 AddToWorkList(Load.getNode());
Craig Topper0c9da212012-03-20 05:28:39 +00008148 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedman4db4add2011-11-16 23:50:22 +00008149 // Make sure to revisit this node to clean it up; it will usually be dead.
8150 AddToWorkList(N);
8151 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00008152 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008153
Dan Gohman475871a2008-07-27 21:46:04 +00008154 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00008155}
Evan Cheng513da432007-10-06 08:19:55 +00008156
Dan Gohman475871a2008-07-27 21:46:04 +00008157SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00008158 unsigned NumInScalars = N->getNumOperands();
Nadav Rotemb00418a2011-10-29 21:23:04 +00008159 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008160 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008161
8162 // A vector built entirely of undefs is undef.
8163 if (ISD::allOperandsUndef(N))
8164 return DAG.getUNDEF(VT);
8165
Nadav Rotemb00418a2011-10-29 21:23:04 +00008166 // Check to see if this is a BUILD_VECTOR of a bunch of values
8167 // which come from any_extend or zero_extend nodes. If so, we can create
8168 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00008169 // optimizations. We do not handle sign-extend because we can't fill the sign
8170 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008171 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00008172 bool AllAnyExt = true;
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008173
Craig Topperd3b58892012-01-17 09:09:48 +00008174 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00008175 SDValue In = N->getOperand(i);
8176 // Ignore undef inputs.
8177 if (In.getOpcode() == ISD::UNDEF) continue;
8178
8179 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
8180 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
8181
Nadav Rotemf47368b2011-10-31 20:08:25 +00008182 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008183 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00008184 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008185 break;
8186 }
8187
8188 // The input is a ZeroExt or AnyExt. Check the original type.
8189 EVT InTy = In.getOperand(0).getValueType();
8190
8191 // Check that all of the widened source types are the same.
8192 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00008193 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008194 SourceType = InTy;
8195 else if (InTy != SourceType) {
8196 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00008197 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008198 break;
8199 }
8200
8201 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00008202 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008203 }
8204
Nadav Rotemf47368b2011-10-31 20:08:25 +00008205 // In order to have valid types, all of the inputs must be extended from the
8206 // same source type and all of the inputs must be any or zero extend.
8207 // Scalar sizes must be a power of two.
8208 EVT OutScalarTy = N->getValueType(0).getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008209 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00008210 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
8211 isPowerOf2_32(SourceType.getSizeInBits());
8212
8213 // We perform this optimization post type-legalization because
8214 // the type-legalizer often scalarizes integer-promoted vectors.
8215 // Performing this optimization before may create bit-casts which
8216 // will be type-legalized to complex code sequences.
8217 // We perform this optimization only before the operation legalizer because we
8218 // may introduce illegal operations.
Nadav Rotem6431ff92012-03-15 08:49:06 +00008219 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
8220 // turn into a single shuffle instruction.
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008221 if ((Level == AfterLegalizeVectorOps || Level == AfterLegalizeTypes) &&
8222 ValidTypes) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00008223 bool isLE = TLI.isLittleEndian();
Nadav Rotemf47368b2011-10-31 20:08:25 +00008224 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
Nadav Rotemb00418a2011-10-29 21:23:04 +00008225 assert(ElemRatio > 1 && "Invalid element size ratio");
Craig Topperd3b58892012-01-17 09:09:48 +00008226 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
Nadav Rotemf47368b2011-10-31 20:08:25 +00008227 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008228
8229 unsigned NewBVElems = ElemRatio * N->getValueType(0).getVectorNumElements();
Benjamin Kramer50bf86e2011-10-30 08:39:55 +00008230 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008231
8232 // Populate the new build_vector
8233 for (unsigned i=0; i < N->getNumOperands(); ++i) {
8234 SDValue Cast = N->getOperand(i);
Benjamin Kramer50bf86e2011-10-30 08:39:55 +00008235 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
8236 Cast.getOpcode() == ISD::ZERO_EXTEND ||
8237 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
Nadav Rotemb00418a2011-10-29 21:23:04 +00008238 SDValue In;
8239 if (Cast.getOpcode() == ISD::UNDEF)
Nadav Rotemf47368b2011-10-31 20:08:25 +00008240 In = DAG.getUNDEF(SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008241 else
8242 In = Cast->getOperand(0);
8243 unsigned Index = isLE ? (i * ElemRatio) :
8244 (i * ElemRatio + (ElemRatio - 1));
8245
8246 assert(Index < Ops.size() && "Invalid index");
8247 Ops[Index] = In;
8248 }
8249
8250 // The type of the new BUILD_VECTOR node.
Nadav Rotemf47368b2011-10-31 20:08:25 +00008251 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008252 assert(VecVT.getSizeInBits() == N->getValueType(0).getSizeInBits() &&
8253 "Invalid vector size");
Nadav Rotemf47368b2011-10-31 20:08:25 +00008254 // Check if the new vector type is legal.
8255 if (!isTypeLegal(VecVT)) return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00008256
8257 // Make the new BUILD_VECTOR.
8258 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
8259 VecVT, &Ops[0], Ops.size());
8260
Nadav Rotem6431ff92012-03-15 08:49:06 +00008261 // The new BUILD_VECTOR node has the potential to be further optimized.
8262 AddToWorkList(BV.getNode());
Nadav Rotemb00418a2011-10-29 21:23:04 +00008263 // Bitcast to the desired type.
8264 return DAG.getNode(ISD::BITCAST, dl, N->getValueType(0), BV);
8265 }
Chris Lattnerca242442006-03-19 01:27:56 +00008266
Dan Gohman7f321562007-06-25 16:23:39 +00008267 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
8268 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
8269 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands00294ca2012-03-19 15:35:44 +00008270
8271 // May only combine to shuffle after legalize if shuffle is legal.
8272 if (LegalOperations &&
8273 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
8274 return SDValue();
8275
Dan Gohman475871a2008-07-27 21:46:04 +00008276 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00008277 for (unsigned i = 0; i != NumInScalars; ++i) {
8278 // Ignore undef inputs.
8279 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00008280
Dan Gohman7f321562007-06-25 16:23:39 +00008281 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00008282 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00008283 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00008284 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00008285 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008286 break;
8287 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008288
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008289 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00008290 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008291 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
8292 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00008293
Gabor Greifba36cb52008-08-28 21:40:38 +00008294 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00008295 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00008296 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00008297 VecIn2 = ExtractedFromVec;
8298 } else {
8299 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008300 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008301 break;
8302 }
8303 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008304
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008305 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00008306 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008307 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00008308 for (unsigned i = 0; i != NumInScalars; ++i) {
8309 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008310 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008311 continue;
8312 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008313
Rafael Espindola15684b22009-04-24 12:40:33 +00008314 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00008315 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00008316 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008317 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00008318 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
8319 if (ExtIndex > VT.getVectorNumElements())
8320 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008321
Nate Begeman5a5ca152009-04-29 05:20:52 +00008322 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008323 continue;
8324 }
8325
8326 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00008327 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00008328 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008329 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008330
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008331 // We can't generate a shuffle node with mismatched input and output types.
8332 // Attempt to transform a single input vector to the correct type.
8333 if ((VT != VecIn1.getValueType())) {
8334 // We don't support shuffeling between TWO values of different types.
8335 if (VecIn2.getNode() != 0)
8336 return SDValue();
8337
8338 // We only support widening of vectors which are half the size of the
8339 // output registers. For example XMM->YMM widening on X86 with AVX.
8340 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
8341 return SDValue();
8342
James Molloy8cd08bf2012-09-10 14:01:21 +00008343 // If the input vector type has a different base type to the output
8344 // vector type, bail out.
8345 if (VecIn1.getValueType().getVectorElementType() !=
8346 VT.getVectorElementType())
8347 return SDValue();
8348
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00008349 // Widen the input vector by adding undef values.
8350 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8351 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008352 }
8353
8354 // If VecIn2 is unused then change it to undef.
8355 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
8356
Nadav Rotem6dfabb62012-09-20 08:53:31 +00008357 // Check that we were able to transform all incoming values to the same
8358 // type.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00008359 if (VecIn2.getValueType() != VecIn1.getValueType() ||
8360 VecIn1.getValueType() != VT)
8361 return SDValue();
8362
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008363 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00008364 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00008365 return SDValue();
8366
Dan Gohman7f321562007-06-25 16:23:39 +00008367 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00008368 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00008369 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008370 Ops[1] = VecIn2;
Nate Begeman9008ca62009-04-27 18:41:29 +00008371 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008372 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008373
Dan Gohman475871a2008-07-27 21:46:04 +00008374 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00008375}
8376
Dan Gohman475871a2008-07-27 21:46:04 +00008377SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00008378 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
8379 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
8380 // inputs come from at most two distinct vectors, turn this into a shuffle
8381 // node.
8382
8383 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00008384 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00008385 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00008386
Nadav Rotemb7e230d2012-07-14 21:30:27 +00008387 // Check if all of the operands are undefs.
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008388 if (ISD::allOperandsUndef(N))
Nadav Rotemb7e230d2012-07-14 21:30:27 +00008389 return DAG.getUNDEF(N->getValueType(0));
8390
Dan Gohman475871a2008-07-27 21:46:04 +00008391 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00008392}
8393
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00008394SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
8395 EVT NVT = N->getValueType(0);
8396 SDValue V = N->getOperand(0);
8397
8398 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
8399 // Handle only simple case where vector being inserted and vector
8400 // being extracted are of same type, and are half size of larger vectors.
8401 EVT BigVT = V->getOperand(0).getValueType();
8402 EVT SmallVT = V->getOperand(1).getValueType();
8403 if (NVT != SmallVT || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
8404 return SDValue();
8405
Eli Friedman26323442011-12-07 00:11:56 +00008406 // Only handle cases where both indexes are constants with the same type.
8407 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
8408 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00008409
Eli Friedman26323442011-12-07 00:11:56 +00008410 if (InsIdx && ExtIdx &&
8411 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
8412 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
8413 // Combine:
8414 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
8415 // Into:
8416 // indices are equal => V1
8417 // otherwise => (extract_subvec V1, ExtIdx)
8418 if (InsIdx->getZExtValue() == ExtIdx->getZExtValue())
8419 return V->getOperand(1);
8420 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, N->getDebugLoc(), NVT,
8421 V->getOperand(0), N->getOperand(1));
8422 }
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00008423 }
8424
8425 return SDValue();
8426}
8427
Dan Gohman475871a2008-07-27 21:46:04 +00008428SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00008429 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008430 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00008431
Mon P Wangaeb06d22008-11-10 04:46:22 +00008432 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +00008433 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +00008434
Craig Topperae1bec52012-04-09 05:16:56 +00008435 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wangaeb06d22008-11-10 04:46:22 +00008436
Craig Topper481b79c2012-01-04 08:07:43 +00008437 // Canonicalize shuffle undef, undef -> undef
8438 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
8439 return DAG.getUNDEF(VT);
8440
8441 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8442
8443 // Canonicalize shuffle v, v -> v, undef
8444 if (N0 == N1) {
8445 SmallVector<int, 8> NewMask;
8446 for (unsigned i = 0; i != NumElts; ++i) {
8447 int Idx = SVN->getMaskElt(i);
8448 if (Idx >= (int)NumElts) Idx -= NumElts;
8449 NewMask.push_back(Idx);
8450 }
8451 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, DAG.getUNDEF(VT),
8452 &NewMask[0]);
8453 }
8454
8455 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
8456 if (N0.getOpcode() == ISD::UNDEF) {
8457 SmallVector<int, 8> NewMask;
8458 for (unsigned i = 0; i != NumElts; ++i) {
8459 int Idx = SVN->getMaskElt(i);
Craig Topper4b206bd2012-04-09 05:55:33 +00008460 if (Idx >= 0) {
8461 if (Idx < (int)NumElts)
8462 Idx += NumElts;
8463 else
8464 Idx -= NumElts;
8465 }
8466 NewMask.push_back(Idx);
Craig Topper481b79c2012-01-04 08:07:43 +00008467 }
8468 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N1, DAG.getUNDEF(VT),
8469 &NewMask[0]);
8470 }
8471
8472 // Remove references to rhs if it is undef
8473 if (N1.getOpcode() == ISD::UNDEF) {
8474 bool Changed = false;
8475 SmallVector<int, 8> NewMask;
8476 for (unsigned i = 0; i != NumElts; ++i) {
8477 int Idx = SVN->getMaskElt(i);
8478 if (Idx >= (int)NumElts) {
8479 Idx = -1;
8480 Changed = true;
8481 }
8482 NewMask.push_back(Idx);
8483 }
8484 if (Changed)
8485 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, N1, &NewMask[0]);
8486 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00008487
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008488 // If it is a splat, check if the argument vector is another splat or a
8489 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008490 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008491 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00008492
Dan Gohman7f321562007-06-25 16:23:39 +00008493 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00008494 // not the number of vector elements, look through it. Be careful not to
8495 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008496 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +00008497 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00008498 if (ConvInput.getValueType().isVector() &&
8499 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00008500 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00008501 }
8502
Dan Gohman7f321562007-06-25 16:23:39 +00008503 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008504 assert(V->getNumOperands() == NumElts &&
8505 "BUILD_VECTOR has wrong number of operands");
8506 SDValue Base;
8507 bool AllSame = true;
8508 for (unsigned i = 0; i != NumElts; ++i) {
8509 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
8510 Base = V->getOperand(i);
8511 break;
Evan Cheng917ec982006-07-21 08:25:53 +00008512 }
Evan Cheng917ec982006-07-21 08:25:53 +00008513 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008514 // Splat of <u, u, u, u>, return <u, u, u, u>
8515 if (!Base.getNode())
8516 return N0;
8517 for (unsigned i = 0; i != NumElts; ++i) {
8518 if (V->getOperand(i) != Base) {
8519 AllSame = false;
8520 break;
8521 }
8522 }
8523 // Splat of <x, x, x, x>, return <x, x, x, x>
8524 if (AllSame)
8525 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +00008526 }
8527 }
Nadav Rotem4ac90812012-04-01 19:31:22 +00008528
8529 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008530 // and it reverses the swizzle of the previous shuffle then we can
8531 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotem4ac90812012-04-01 19:31:22 +00008532 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
8533 N1.getOpcode() == ISD::UNDEF) {
8534
Nadav Rotem4ac90812012-04-01 19:31:22 +00008535 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
8536
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008537 // Shuffle nodes can only reverse shuffles with a single non-undef value.
8538 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
8539 return SDValue();
8540
Craig Topperae1bec52012-04-09 05:16:56 +00008541 // The incoming shuffle must be of the same type as the result of the
8542 // current shuffle.
8543 assert(OtherSV->getOperand(0).getValueType() == VT &&
8544 "Shuffle types don't match");
Nadav Rotem4ac90812012-04-01 19:31:22 +00008545
8546 for (unsigned i = 0; i != NumElts; ++i) {
8547 int Idx = SVN->getMaskElt(i);
Craig Topperae1bec52012-04-09 05:16:56 +00008548 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotem4ac90812012-04-01 19:31:22 +00008549 // Next, this index comes from the first value, which is the incoming
8550 // shuffle. Adopt the incoming index.
8551 if (Idx >= 0)
8552 Idx = OtherSV->getMaskElt(Idx);
8553
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008554 // The combined shuffle must map each index to itself.
Craig Topperae1bec52012-04-09 05:16:56 +00008555 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008556 return SDValue();
Nadav Rotem4ac90812012-04-01 19:31:22 +00008557 }
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008558
8559 return OtherSV->getOperand(0);
Nadav Rotem4ac90812012-04-01 19:31:22 +00008560 }
8561
Dan Gohman475871a2008-07-27 21:46:04 +00008562 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00008563}
8564
Jim Grosbach9a526492010-06-23 16:07:42 +00008565SDValue DAGCombiner::visitMEMBARRIER(SDNode* N) {
8566 if (!TLI.getShouldFoldAtomicFences())
8567 return SDValue();
8568
8569 SDValue atomic = N->getOperand(0);
8570 switch (atomic.getOpcode()) {
8571 case ISD::ATOMIC_CMP_SWAP:
8572 case ISD::ATOMIC_SWAP:
8573 case ISD::ATOMIC_LOAD_ADD:
8574 case ISD::ATOMIC_LOAD_SUB:
8575 case ISD::ATOMIC_LOAD_AND:
8576 case ISD::ATOMIC_LOAD_OR:
8577 case ISD::ATOMIC_LOAD_XOR:
8578 case ISD::ATOMIC_LOAD_NAND:
8579 case ISD::ATOMIC_LOAD_MIN:
8580 case ISD::ATOMIC_LOAD_MAX:
8581 case ISD::ATOMIC_LOAD_UMIN:
8582 case ISD::ATOMIC_LOAD_UMAX:
8583 break;
8584 default:
8585 return SDValue();
8586 }
8587
8588 SDValue fence = atomic.getOperand(0);
8589 if (fence.getOpcode() != ISD::MEMBARRIER)
8590 return SDValue();
8591
8592 switch (atomic.getOpcode()) {
8593 case ISD::ATOMIC_CMP_SWAP:
8594 return SDValue(DAG.UpdateNodeOperands(atomic.getNode(),
8595 fence.getOperand(0),
8596 atomic.getOperand(1), atomic.getOperand(2),
8597 atomic.getOperand(3)), atomic.getResNo());
8598 case ISD::ATOMIC_SWAP:
8599 case ISD::ATOMIC_LOAD_ADD:
8600 case ISD::ATOMIC_LOAD_SUB:
8601 case ISD::ATOMIC_LOAD_AND:
8602 case ISD::ATOMIC_LOAD_OR:
8603 case ISD::ATOMIC_LOAD_XOR:
8604 case ISD::ATOMIC_LOAD_NAND:
8605 case ISD::ATOMIC_LOAD_MIN:
8606 case ISD::ATOMIC_LOAD_MAX:
8607 case ISD::ATOMIC_LOAD_UMIN:
8608 case ISD::ATOMIC_LOAD_UMAX:
8609 return SDValue(DAG.UpdateNodeOperands(atomic.getNode(),
8610 fence.getOperand(0),
8611 atomic.getOperand(1), atomic.getOperand(2)),
8612 atomic.getResNo());
8613 default:
8614 return SDValue();
8615 }
8616}
8617
Evan Cheng44f1f092006-04-20 08:56:16 +00008618/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00008619/// an AND to a vector_shuffle with the destination vector and a zero vector.
8620/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00008621/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00008622SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00008623 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008624 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008625 SDValue LHS = N->getOperand(0);
8626 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00008627 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008628 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +00008629 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00008630 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008631 SmallVector<int, 8> Indices;
8632 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00008633 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00008634 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00008635 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00008636 return SDValue();
Craig Topperb7135e52012-04-09 05:59:53 +00008637
8638 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00008639 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00008640 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00008641 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00008642 else
Dan Gohman475871a2008-07-27 21:46:04 +00008643 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00008644 }
8645
8646 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00008647 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008648 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00008649 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00008650
Dan Gohman7f321562007-06-25 16:23:39 +00008651 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00008652 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008653 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00008654 DAG.getConstant(0, EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00008655 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
8656 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008657 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +00008658 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008659 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00008660 }
8661 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008662
Dan Gohman475871a2008-07-27 21:46:04 +00008663 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00008664}
8665
Dan Gohman7f321562007-06-25 16:23:39 +00008666/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00008667SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00008668 // After legalize, the target may be depending on adds and other
8669 // binary ops to provide legal ways to construct constants or other
8670 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00008671 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00008672
Bob Wilsond7273432010-12-17 23:06:49 +00008673 assert(N->getValueType(0).isVector() &&
8674 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00008675
Dan Gohman475871a2008-07-27 21:46:04 +00008676 SDValue LHS = N->getOperand(0);
8677 SDValue RHS = N->getOperand(1);
8678 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00008679 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00008680
Dan Gohman7f321562007-06-25 16:23:39 +00008681 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00008682 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00008683 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00008684 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00008685 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00008686 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00008687 SDValue LHSOp = LHS.getOperand(i);
8688 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00008689 // If these two elements can't be folded, bail out.
8690 if ((LHSOp.getOpcode() != ISD::UNDEF &&
8691 LHSOp.getOpcode() != ISD::Constant &&
8692 LHSOp.getOpcode() != ISD::ConstantFP) ||
8693 (RHSOp.getOpcode() != ISD::UNDEF &&
8694 RHSOp.getOpcode() != ISD::Constant &&
8695 RHSOp.getOpcode() != ISD::ConstantFP))
8696 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00008697
Evan Cheng7b336a82006-05-31 06:08:35 +00008698 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00008699 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
8700 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00008701 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00008702 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00008703 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00008704 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00008705 break;
8706 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008707
Bob Wilsond7273432010-12-17 23:06:49 +00008708 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +00008709 EVT RVT = RHSOp.getValueType();
8710 if (RVT != VT) {
8711 // Integer BUILD_VECTOR operands may have types larger than the element
8712 // size (e.g., when the element type is not legal). Prior to type
8713 // legalization, the types may not match between the two BUILD_VECTORS.
8714 // Truncate one of the operands to make them match.
8715 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
8716 RHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, RHSOp);
8717 } else {
8718 LHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), RVT, LHSOp);
8719 VT = RVT;
8720 }
8721 }
Bob Wilsond7273432010-12-17 23:06:49 +00008722 SDValue FoldOp = DAG.getNode(N->getOpcode(), LHS.getDebugLoc(), VT,
Evan Chenga0839882010-05-18 00:03:40 +00008723 LHSOp, RHSOp);
8724 if (FoldOp.getOpcode() != ISD::UNDEF &&
8725 FoldOp.getOpcode() != ISD::Constant &&
8726 FoldOp.getOpcode() != ISD::ConstantFP)
8727 break;
8728 Ops.push_back(FoldOp);
8729 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00008730 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008731
Bob Wilsond7273432010-12-17 23:06:49 +00008732 if (Ops.size() == LHS.getNumOperands())
8733 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
8734 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +00008735 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008736
Dan Gohman475871a2008-07-27 21:46:04 +00008737 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00008738}
8739
Craig Topperdd201ff2012-09-11 01:45:21 +00008740/// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG.
8741SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) {
8742 // After legalize, the target may be depending on adds and other
8743 // binary ops to provide legal ways to construct constants or other
8744 // things. Simplifying them may result in a loss of legality.
8745 if (LegalOperations) return SDValue();
8746
8747 assert(N->getValueType(0).isVector() &&
8748 "SimplifyVUnaryOp only works on vectors!");
8749
8750 SDValue N0 = N->getOperand(0);
8751
8752 if (N0.getOpcode() != ISD::BUILD_VECTOR)
8753 return SDValue();
8754
8755 // Operand is a BUILD_VECTOR node, see if we can constant fold it.
8756 SmallVector<SDValue, 8> Ops;
8757 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
8758 SDValue Op = N0.getOperand(i);
8759 if (Op.getOpcode() != ISD::UNDEF &&
8760 Op.getOpcode() != ISD::ConstantFP)
8761 break;
8762 EVT EltVT = Op.getValueType();
8763 SDValue FoldOp = DAG.getNode(N->getOpcode(), N0.getDebugLoc(), EltVT, Op);
8764 if (FoldOp.getOpcode() != ISD::UNDEF &&
8765 FoldOp.getOpcode() != ISD::ConstantFP)
8766 break;
8767 Ops.push_back(FoldOp);
8768 AddToWorkList(FoldOp.getNode());
8769 }
8770
8771 if (Ops.size() != N0.getNumOperands())
8772 return SDValue();
8773
8774 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
8775 N0.getValueType(), &Ops[0], Ops.size());
8776}
8777
Bill Wendling836ca7d2009-01-30 23:59:18 +00008778SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0,
8779 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00008780 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00008781
Bill Wendling836ca7d2009-01-30 23:59:18 +00008782 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00008783 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008784
Nate Begemanf845b452005-10-08 00:29:44 +00008785 // If we got a simplified select_cc node back from SimplifySelectCC, then
8786 // break it down into a new SETCC node, and a new SELECT node, and then return
8787 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00008788 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00008789 // Check to see if we got a select_cc back (to turn into setcc/select).
8790 // Otherwise, just return whatever node we got back, like fabs.
8791 if (SCC.getOpcode() == ISD::SELECT_CC) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00008792 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(),
8793 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008794 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008795 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00008796 AddToWorkList(SETCC.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008797 return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(),
8798 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00008799 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008800
Nate Begemanf845b452005-10-08 00:29:44 +00008801 return SCC;
8802 }
Dan Gohman475871a2008-07-27 21:46:04 +00008803 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00008804}
8805
Chris Lattner40c62d52005-10-18 06:04:22 +00008806/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
8807/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00008808/// select. Callers of this should assume that TheSelect is deleted if this
8809/// returns true. As such, they should return the appropriate thing (e.g. the
8810/// node) back to the top-level of the DAG combiner loop to avoid it being
8811/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00008812bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00008813 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00008814
Nadav Rotemf94fdb62011-02-11 19:57:47 +00008815 // Cannot simplify select with vector condition
8816 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
8817
Chris Lattner40c62d52005-10-18 06:04:22 +00008818 // If this is a select from two identical things, try to pull the operation
8819 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +00008820 if (LHS.getOpcode() != RHS.getOpcode() ||
8821 !LHS.hasOneUse() || !RHS.hasOneUse())
8822 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008823
Chris Lattner18061612010-09-21 15:46:59 +00008824 // If this is a load and the token chain is identical, replace the select
8825 // of two loads with a load through a select of the address to load from.
8826 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
8827 // constants have been dropped into the constant pool.
8828 if (LHS.getOpcode() == ISD::LOAD) {
8829 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
8830 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008831
Chris Lattner18061612010-09-21 15:46:59 +00008832 // Token chains must be identical.
8833 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008834 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +00008835 LLD->isVolatile() || RLD->isVolatile() ||
8836 // If this is an EXTLOAD, the VT's must match.
8837 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +00008838 // If this is an EXTLOAD, the kind of extension must match.
8839 (LLD->getExtensionType() != RLD->getExtensionType() &&
8840 // The only exception is if one of the extensions is anyext.
8841 LLD->getExtensionType() != ISD::EXTLOAD &&
8842 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +00008843 // FIXME: this discards src value information. This is
8844 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00008845 // both potential memory locations. Since we are discarding
8846 // src value info, don't do the transformation if the memory
8847 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +00008848 LLD->getPointerInfo().getAddrSpace() != 0 ||
8849 RLD->getPointerInfo().getAddrSpace() != 0)
8850 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008851
Chris Lattnerf1658062010-09-21 15:58:55 +00008852 // Check that the select condition doesn't reach either load. If so,
8853 // folding this will induce a cycle into the DAG. If not, this is safe to
8854 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +00008855 SDValue Addr;
8856 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +00008857 SDNode *CondNode = TheSelect->getOperand(0).getNode();
8858 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
8859 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
8860 return false;
8861 Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(),
8862 LLD->getBasePtr().getValueType(),
8863 TheSelect->getOperand(0), LLD->getBasePtr(),
8864 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +00008865 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +00008866 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
8867 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
8868
8869 if ((LLD->hasAnyUseOfValue(1) &&
8870 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner77d95212012-03-27 16:27:21 +00008871 (RLD->hasAnyUseOfValue(1) &&
8872 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnerf1658062010-09-21 15:58:55 +00008873 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008874
Chris Lattnerf1658062010-09-21 15:58:55 +00008875 Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(),
8876 LLD->getBasePtr().getValueType(),
8877 TheSelect->getOperand(0),
8878 TheSelect->getOperand(1),
8879 LLD->getBasePtr(), RLD->getBasePtr(),
8880 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +00008881 }
8882
Chris Lattnerf1658062010-09-21 15:58:55 +00008883 SDValue Load;
8884 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
8885 Load = DAG.getLoad(TheSelect->getValueType(0),
8886 TheSelect->getDebugLoc(),
8887 // FIXME: Discards pointer info.
8888 LLD->getChain(), Addr, MachinePointerInfo(),
8889 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008890 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +00008891 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +00008892 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
8893 RLD->getExtensionType() : LLD->getExtensionType(),
Chris Lattnerf1658062010-09-21 15:58:55 +00008894 TheSelect->getDebugLoc(),
Stuart Hastingsa9011292011-02-16 16:23:55 +00008895 TheSelect->getValueType(0),
Chris Lattnerf1658062010-09-21 15:58:55 +00008896 // FIXME: Discards pointer info.
8897 LLD->getChain(), Addr, MachinePointerInfo(),
8898 LLD->getMemoryVT(), LLD->isVolatile(),
8899 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +00008900 }
Chris Lattnerf1658062010-09-21 15:58:55 +00008901
8902 // Users of the select now use the result of the load.
8903 CombineTo(TheSelect, Load);
8904
8905 // Users of the old loads now use the new load's chain. We know the
8906 // old-load value is dead now.
8907 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
8908 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
8909 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +00008910 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008911
Chris Lattner40c62d52005-10-18 06:04:22 +00008912 return false;
8913}
8914
Chris Lattner600fec32009-03-11 05:08:08 +00008915/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
8916/// where 'cond' is the comparison specified by CC.
Scott Michelfdc40a02009-02-17 22:15:04 +00008917SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00008918 SDValue N2, SDValue N3,
8919 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00008920 // (x ? y : y) -> y.
8921 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008922
Owen Andersone50ed302009-08-10 22:56:29 +00008923 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00008924 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
8925 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
8926 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008927
8928 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00008929 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00008930 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00008931 if (SCC.getNode()) AddToWorkList(SCC.getNode());
8932 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008933
8934 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00008935 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00008936 return N2;
8937 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00008938 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00008939 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00008940
Nate Begemanf845b452005-10-08 00:29:44 +00008941 // Check to see if we can simplify the select into an fabs node
8942 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
8943 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00008944 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00008945 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
8946 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
8947 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
8948 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00008949 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008950
Nate Begemanf845b452005-10-08 00:29:44 +00008951 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
8952 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
8953 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
8954 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00008955 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00008956 }
8957 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008958
Chris Lattner600fec32009-03-11 05:08:08 +00008959 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
8960 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
8961 // in it. This is a win when the constant is not otherwise available because
8962 // it replaces two constant pool loads with one. We only do this if the FP
8963 // type is known to be legal, because if it isn't, then we are before legalize
8964 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00008965 // messing with soft float) and if the ConstantFP is not legal, because if
8966 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00008967 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
8968 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
8969 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00008970 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
8971 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00008972 // If both constants have multiple uses, then we won't need to do an
8973 // extra load, they are likely around in registers for other users.
8974 (TV->hasOneUse() || FV->hasOneUse())) {
8975 Constant *Elts[] = {
8976 const_cast<ConstantFP*>(FV->getConstantFPValue()),
8977 const_cast<ConstantFP*>(TV->getConstantFPValue())
8978 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008979 Type *FPTy = Elts[0]->getType();
Chris Lattner600fec32009-03-11 05:08:08 +00008980 const TargetData &TD = *TLI.getTargetData();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008981
Chris Lattner600fec32009-03-11 05:08:08 +00008982 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +00008983 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +00008984 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
8985 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00008986 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00008987
8988 // Get the offsets to the 0 and 1 element of the array so that we can
8989 // select between them.
8990 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00008991 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00008992 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008993
Chris Lattner600fec32009-03-11 05:08:08 +00008994 SDValue Cond = DAG.getSetCC(DL,
8995 TLI.getSetCCResultType(N0.getValueType()),
8996 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +00008997 AddToWorkList(Cond.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00008998 SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(),
8999 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +00009000 AddToWorkList(CstOffset.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00009001 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
9002 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +00009003 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00009004 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00009005 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009006 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00009007
9008 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009009 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009010
Nate Begemanf845b452005-10-08 00:29:44 +00009011 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00009012 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00009013 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00009014 (N1C->isNullValue() || // (a < 0) ? b : 0
9015 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00009016 EVT XType = N0.getValueType();
9017 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00009018 if (XType.bitsGE(AType)) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00009019 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00009020 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00009021 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
9022 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00009023 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +00009024 SDValue ShCt = DAG.getConstant(ShCtV,
9025 getShiftAmountTy(N0.getValueType()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00009026 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009027 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00009028 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009029
Duncan Sands8e4eb092008-06-08 20:54:56 +00009030 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009031 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009032 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009033 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009034
9035 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009036 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009037
Bill Wendling9729c5a2009-01-31 03:12:48 +00009038 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009039 XType, N0,
9040 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009041 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00009042 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009043
Duncan Sands8e4eb092008-06-08 20:54:56 +00009044 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009045 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009046 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009047 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009048
9049 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009050 }
9051 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009052
Owen Andersoned1088a2010-09-22 22:58:22 +00009053 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
9054 // where y is has a single bit set.
9055 // A plaintext description would be, we can turn the SELECT_CC into an AND
9056 // when the condition can be materialized as an all-ones register. Any
9057 // single bit-test can be materialized as an all-ones register with
9058 // shift-left and shift-right-arith.
9059 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
9060 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009061 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +00009062 N2C && N2C->isNullValue()) {
9063 SDValue AndLHS = N0->getOperand(0);
9064 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
9065 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
9066 // Shift the tested bit over the sign bit.
9067 APInt AndMask = ConstAndRHS->getAPIntValue();
9068 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00009069 DAG.getConstant(AndMask.countLeadingZeros(),
9070 getShiftAmountTy(AndLHS.getValueType()));
Owen Andersoned1088a2010-09-22 22:58:22 +00009071 SDValue Shl = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009072
Owen Andersoned1088a2010-09-22 22:58:22 +00009073 // Now arithmetic right shift it all the way over, so the result is either
9074 // all-ones, or zero.
9075 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00009076 DAG.getConstant(AndMask.getBitWidth()-1,
9077 getShiftAmountTy(Shl.getValueType()));
Owen Andersoned1088a2010-09-22 22:58:22 +00009078 SDValue Shr = DAG.getNode(ISD::SRA, N0.getDebugLoc(), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009079
Owen Andersoned1088a2010-09-22 22:58:22 +00009080 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
9081 }
9082 }
9083
Nate Begeman07ed4172005-10-10 21:26:48 +00009084 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00009085 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00009086 TLI.getBooleanContents(N0.getValueType().isVector()) ==
9087 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009088
Chris Lattner1eba01e2007-04-11 06:50:51 +00009089 // If the caller doesn't want us to simplify this into a zext of a compare,
9090 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00009091 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00009092 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009093
Nate Begeman07ed4172005-10-10 21:26:48 +00009094 // Get a SetCC of the condition
9095 // FIXME: Should probably make sure that setcc is legal if we ever have a
9096 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00009097 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00009098 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00009099 if (LegalTypes) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009100 SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00009101 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00009102 if (N2.getValueType().bitsLT(SCC.getValueType()))
Bill Wendling9729c5a2009-01-31 03:12:48 +00009103 Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), N2.getValueType());
Chris Lattner555d8d62006-12-07 22:36:47 +00009104 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00009105 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009106 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00009107 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00009108 SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC);
Bill Wendling9729c5a2009-01-31 03:12:48 +00009109 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009110 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00009111 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009112
Gabor Greifba36cb52008-08-28 21:40:38 +00009113 AddToWorkList(SCC.getNode());
9114 AddToWorkList(Temp.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00009115
Dan Gohman002e5d02008-03-13 22:13:53 +00009116 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00009117 return Temp;
Bill Wendling836ca7d2009-01-30 23:59:18 +00009118
Nate Begeman07ed4172005-10-10 21:26:48 +00009119 // shl setcc result by log2 n2c
Bill Wendling836ca7d2009-01-30 23:59:18 +00009120 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00009121 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00009122 getShiftAmountTy(Temp.getValueType())));
Nate Begeman07ed4172005-10-10 21:26:48 +00009123 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009124
Nate Begemanf845b452005-10-08 00:29:44 +00009125 // Check to see if this is the equivalent of setcc
9126 // FIXME: Turn all of these into setcc if setcc if setcc is legal
9127 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00009128 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +00009129 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00009130 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00009131 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00009132 SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00009133 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +00009134 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +00009135 return Res;
9136 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009137
Bill Wendling836ca7d2009-01-30 23:59:18 +00009138 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +00009139 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00009140 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00009141 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009142 SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009143 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00009144 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +00009145 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +00009146 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009147 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +00009148 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00009149 SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(),
9150 XType, DAG.getConstant(0, XType), N0);
Bill Wendling7581bfa2009-01-30 23:03:19 +00009151 SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +00009152 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00009153 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00009154 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009155 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +00009156 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009157 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +00009158 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009159 SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00009160 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009161 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +00009162 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +00009163 }
9164 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009165
Benjamin Kramercde51102010-07-08 12:09:56 +00009166 // Check to see if this is an integer abs.
9167 // select_cc setg[te] X, 0, X, -X ->
9168 // select_cc setgt X, -1, X, -X ->
9169 // select_cc setl[te] X, 0, -X, X ->
9170 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +00009171 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +00009172 if (N1C) {
9173 ConstantSDNode *SubC = NULL;
9174 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
9175 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
9176 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
9177 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
9178 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
9179 (N1C->isOne() && CC == ISD::SETLT)) &&
9180 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
9181 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
9182
Owen Andersone50ed302009-08-10 22:56:29 +00009183 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +00009184 if (SubC && SubC->isNullValue() && XType.isInteger()) {
9185 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType,
9186 N0,
9187 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009188 getShiftAmountTy(N0.getValueType())));
Benjamin Kramercde51102010-07-08 12:09:56 +00009189 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(),
9190 XType, N0, Shift);
9191 AddToWorkList(Shift.getNode());
9192 AddToWorkList(Add.getNode());
9193 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +00009194 }
9195 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009196
Dan Gohman475871a2008-07-27 21:46:04 +00009197 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00009198}
9199
Evan Chengfa1eb272007-02-08 22:13:59 +00009200/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +00009201SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +00009202 SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00009203 DebugLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009204 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00009205 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00009206 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7beb2005-09-16 00:54:12 +00009207}
9208
Nate Begeman69575232005-10-20 02:15:44 +00009209/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
9210/// return a DAG expression to select that will generate the same value by
9211/// multiplying by a magic number. See:
9212/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00009213SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00009214 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +00009215 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00009216
Andrew Lenharth232c9102006-06-12 16:07:18 +00009217 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00009218 ii != ee; ++ii)
9219 AddToWorkList(*ii);
9220 return S;
Nate Begeman69575232005-10-20 02:15:44 +00009221}
9222
9223/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
9224/// return a DAG expression to select that will generate the same value by
9225/// multiplying by a magic number. See:
9226/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00009227SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00009228 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +00009229 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00009230
Andrew Lenharth232c9102006-06-12 16:07:18 +00009231 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00009232 ii != ee; ++ii)
9233 AddToWorkList(*ii);
9234 return S;
Nate Begeman69575232005-10-20 02:15:44 +00009235}
9236
Nate Begemancc66cdd2009-09-25 06:05:26 +00009237/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +00009238// to alias with anything but itself. Provides base object and offset as
9239// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +00009240static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky2943e372012-09-05 22:15:49 +00009241 const GlobalValue *&GV, const void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +00009242 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +00009243 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00009244
Jim Laskey71382342006-10-07 23:37:56 +00009245 // If it's an adding a simple constant then integrate the offset.
9246 if (Base.getOpcode() == ISD::ADD) {
9247 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
9248 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009249 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00009250 }
9251 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009252
Nate Begemancc66cdd2009-09-25 06:05:26 +00009253 // Return the underlying GlobalValue, and update the Offset. Return false
9254 // for GlobalAddressSDNode since the same GlobalAddress may be represented
9255 // by multiple nodes with different offsets.
9256 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
9257 GV = G->getGlobal();
9258 Offset += G->getOffset();
9259 return false;
9260 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009261
Nate Begemancc66cdd2009-09-25 06:05:26 +00009262 // Return the underlying Constant value, and update the Offset. Return false
9263 // for ConstantSDNodes since the same constant pool entry may be represented
9264 // by multiple nodes with different offsets.
9265 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky2943e372012-09-05 22:15:49 +00009266 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
9267 : (const void *)C->getConstVal();
Nate Begemancc66cdd2009-09-25 06:05:26 +00009268 Offset += C->getOffset();
9269 return false;
9270 }
Jim Laskey71382342006-10-07 23:37:56 +00009271 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +00009272 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +00009273}
9274
9275/// isAlias - Return true if there is any possibility that the two addresses
9276/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00009277bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00009278 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009279 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009280 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +00009281 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009282 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009283 unsigned SrcValueAlign2,
9284 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +00009285 // If they are the same then they must be aliases.
9286 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00009287
Jim Laskey71382342006-10-07 23:37:56 +00009288 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00009289 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00009290 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +00009291 const GlobalValue *GV1, *GV2;
Roman Divacky2943e372012-09-05 22:15:49 +00009292 const void *CV1, *CV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +00009293 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
9294 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009295
Nate Begemancc66cdd2009-09-25 06:05:26 +00009296 // If they have a same base address then check to see if they overlap.
9297 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +00009298 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009299
Owen Anderson4a9f1502010-09-20 20:39:59 +00009300 // It is possible for different frame indices to alias each other, mostly
9301 // when tail call optimization reuses return address slots for arguments.
9302 // To catch this case, look up the actual index of frame indices to compute
9303 // the real alias relationship.
9304 if (isFrameIndex1 && isFrameIndex2) {
9305 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9306 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
9307 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
9308 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
9309 }
9310
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009311 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +00009312 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +00009313 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
9314 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +00009315
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009316 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
9317 // compared to the size and offset of the access, we may be able to prove they
9318 // do not alias. This check is conservative for now to catch cases created by
9319 // splitting vector types.
9320 if ((SrcValueAlign1 == SrcValueAlign2) &&
9321 (SrcValueOffset1 != SrcValueOffset2) &&
9322 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
9323 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
9324 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009325
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009326 // There is no overlap between these relatively aligned accesses of similar
9327 // size, return no alias.
9328 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
9329 return false;
9330 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009331
Jim Laskey07a27092006-10-18 19:08:31 +00009332 if (CombinerGlobalAA) {
9333 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00009334 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
9335 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
9336 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00009337 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009338 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
9339 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +00009340 if (AAResult == AliasAnalysis::NoAlias)
9341 return false;
9342 }
Jim Laskey096c22e2006-10-18 12:29:57 +00009343
9344 // Otherwise we have to assume they alias.
9345 return true;
Jim Laskey71382342006-10-07 23:37:56 +00009346}
9347
9348/// FindAliasInfo - Extracts the relevant alias information from the memory
9349/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00009350bool DAGCombiner::FindAliasInfo(SDNode *N,
Benjamin Kramerae4746b2012-01-15 11:50:43 +00009351 SDValue &Ptr, int64_t &Size,
9352 const Value *&SrcValue,
9353 int &SrcValueOffset,
9354 unsigned &SrcValueAlign,
9355 const MDNode *&TBAAInfo) const {
9356 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
9357
9358 Ptr = LS->getBasePtr();
9359 Size = LS->getMemoryVT().getSizeInBits() >> 3;
9360 SrcValue = LS->getSrcValue();
9361 SrcValueOffset = LS->getSrcValueOffset();
9362 SrcValueAlign = LS->getOriginalAlignment();
9363 TBAAInfo = LS->getTBAAInfo();
9364 return isa<LoadSDNode>(LS);
Jim Laskey71382342006-10-07 23:37:56 +00009365}
9366
Jim Laskey6ff23e52006-10-04 16:53:27 +00009367/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
9368/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00009369void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
9370 SmallVector<SDValue, 8> &Aliases) {
9371 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009372 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +00009373
Jim Laskey279f0532006-09-25 16:29:54 +00009374 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00009375 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009376 int64_t Size;
9377 const Value *SrcValue;
9378 int SrcValueOffset;
9379 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009380 const MDNode *SrcTBAAInfo;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009381 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009382 SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +00009383
Jim Laskey6ff23e52006-10-04 16:53:27 +00009384 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00009385 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +00009386 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009387
Jim Laskeybc588b82006-10-05 15:07:25 +00009388 // Look at each chain and determine if it is an alias. If so, add it to the
9389 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +00009390 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +00009391 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009392 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00009393 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009394
9395 // For TokenFactor nodes, look at each operand and only continue up the
9396 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +00009397 // find more and revert to original chain since the xform is unlikely to be
9398 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009399 //
9400 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +00009401 // chain we found before we hit a tokenfactor rather than the original
9402 // chain.
9403 if (Depth > 6 || Aliases.size() == 2) {
9404 Aliases.clear();
9405 Aliases.push_back(OriginalChain);
9406 break;
9407 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009408
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009409 // Don't bother if we've been before.
9410 if (!Visited.insert(Chain.getNode()))
9411 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009412
Jim Laskeybc588b82006-10-05 15:07:25 +00009413 switch (Chain.getOpcode()) {
9414 case ISD::EntryToken:
9415 // Entry token is ideal chain operand, but handled in FindBetterChain.
9416 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00009417
Jim Laskeybc588b82006-10-05 15:07:25 +00009418 case ISD::LOAD:
9419 case ISD::STORE: {
9420 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00009421 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009422 int64_t OpSize;
9423 const Value *OpSrcValue;
9424 int OpSrcValueOffset;
9425 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009426 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +00009427 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009428 OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009429 OpSrcValueAlign,
9430 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +00009431
Jim Laskeybc588b82006-10-05 15:07:25 +00009432 // If chain is alias then stop here.
9433 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009434 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009435 SrcTBAAInfo,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009436 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009437 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00009438 Aliases.push_back(Chain);
9439 } else {
9440 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +00009441 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +00009442 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +00009443 }
Jim Laskeybc588b82006-10-05 15:07:25 +00009444 break;
9445 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009446
Jim Laskeybc588b82006-10-05 15:07:25 +00009447 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009448 // We have to check each of the operands of the token factor for "small"
9449 // token factors, so we queue them up. Adding the operands to the queue
9450 // (stack) in reverse order maintains the original order and increases the
9451 // likelihood that getNode will find a matching token factor (CSE.)
9452 if (Chain.getNumOperands() > 16) {
9453 Aliases.push_back(Chain);
9454 break;
9455 }
Jim Laskeybc588b82006-10-05 15:07:25 +00009456 for (unsigned n = Chain.getNumOperands(); n;)
9457 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +00009458 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +00009459 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00009460
Jim Laskeybc588b82006-10-05 15:07:25 +00009461 default:
9462 // For all other instructions we will just have to take what we can get.
9463 Aliases.push_back(Chain);
9464 break;
Jim Laskey279f0532006-09-25 16:29:54 +00009465 }
9466 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00009467}
9468
9469/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
9470/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00009471SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
9472 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00009473
Jim Laskey6ff23e52006-10-04 16:53:27 +00009474 // Accumulate all the aliases to this node.
9475 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +00009476
Dan Gohman71dc7c92011-05-17 22:20:36 +00009477 // If no operands then chain to entry token.
9478 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +00009479 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +00009480
9481 // If a single operand then chain to it. We don't need to revisit it.
9482 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +00009483 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009484
Jim Laskey6ff23e52006-10-04 16:53:27 +00009485 // Construct a custom tailored token factor.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009486 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009487 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +00009488}
9489
Nate Begeman1d4d4142005-09-01 00:19:25 +00009490// SelectionDAG::Combine - This is the entry point for the file.
9491//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00009492void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +00009493 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00009494 /// run - This is the main entry point to this class.
9495 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00009496 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00009497}