blob: d7fa00962a6f6c8c69a9de111d423c91c869b036 [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);
197 SDValue visitSHL(SDNode *N);
198 SDValue visitSRA(SDNode *N);
199 SDValue visitSRL(SDNode *N);
200 SDValue visitCTLZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000201 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000202 SDValue visitCTTZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000203 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000204 SDValue visitCTPOP(SDNode *N);
205 SDValue visitSELECT(SDNode *N);
206 SDValue visitSELECT_CC(SDNode *N);
207 SDValue visitSETCC(SDNode *N);
208 SDValue visitSIGN_EXTEND(SDNode *N);
209 SDValue visitZERO_EXTEND(SDNode *N);
210 SDValue visitANY_EXTEND(SDNode *N);
211 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
212 SDValue visitTRUNCATE(SDNode *N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000213 SDValue visitBITCAST(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000214 SDValue visitBUILD_PAIR(SDNode *N);
215 SDValue visitFADD(SDNode *N);
216 SDValue visitFSUB(SDNode *N);
217 SDValue visitFMUL(SDNode *N);
Owen Anderson062c0a52012-05-02 22:17:40 +0000218 SDValue visitFMA(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000219 SDValue visitFDIV(SDNode *N);
220 SDValue visitFREM(SDNode *N);
221 SDValue visitFCOPYSIGN(SDNode *N);
222 SDValue visitSINT_TO_FP(SDNode *N);
223 SDValue visitUINT_TO_FP(SDNode *N);
224 SDValue visitFP_TO_SINT(SDNode *N);
225 SDValue visitFP_TO_UINT(SDNode *N);
226 SDValue visitFP_ROUND(SDNode *N);
227 SDValue visitFP_ROUND_INREG(SDNode *N);
228 SDValue visitFP_EXTEND(SDNode *N);
229 SDValue visitFNEG(SDNode *N);
230 SDValue visitFABS(SDNode *N);
Owen Anderson7c626d32012-08-13 23:32:49 +0000231 SDValue visitFCEIL(SDNode *N);
232 SDValue visitFTRUNC(SDNode *N);
233 SDValue visitFFLOOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000234 SDValue visitBRCOND(SDNode *N);
235 SDValue visitBR_CC(SDNode *N);
236 SDValue visitLOAD(SDNode *N);
237 SDValue visitSTORE(SDNode *N);
238 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
239 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
240 SDValue visitBUILD_VECTOR(SDNode *N);
241 SDValue visitCONCAT_VECTORS(SDNode *N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +0000242 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000243 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Jim Grosbach9a526492010-06-23 16:07:42 +0000244 SDValue visitMEMBARRIER(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000245
Dan Gohman475871a2008-07-27 21:46:04 +0000246 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendling35247c32009-01-30 00:45:56 +0000247 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000248
Dan Gohman475871a2008-07-27 21:46:04 +0000249 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000250
Dan Gohman475871a2008-07-27 21:46:04 +0000251 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
252 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Bill Wendling836ca7d2009-01-30 23:59:18 +0000253 SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000254 SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2,
255 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000256 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000257 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000258 DebugLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000259 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000260 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000261 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000262 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000263 SDValue BuildSDIV(SDNode *N);
264 SDValue BuildUDIV(SDNode *N);
Evan Cheng9568e5c2011-06-21 06:01:08 +0000265 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
266 bool DemandHighBits = true);
267 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
Bill Wendling317bd702009-01-30 21:14:50 +0000268 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000269 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000270 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Cheng31959b12011-02-02 01:06:55 +0000271 SDValue TransformFPLoadStorePair(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000272
Dan Gohman475871a2008-07-27 21:46:04 +0000273 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000274
Jim Laskey6ff23e52006-10-04 16:53:27 +0000275 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
276 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000277 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
278 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000279
Jim Laskey096c22e2006-10-18 12:29:57 +0000280 /// isAlias - Return true if there is any possibility that the two addresses
281 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000282 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000283 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000284 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000285 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +0000286 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000287 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000288 unsigned SrcValueAlign2,
289 const MDNode *TBAAInfo2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000290
Jim Laskey7ca56af2006-10-11 13:47:09 +0000291 /// FindAliasInfo - Extracts the relevant alias information from the memory
292 /// node. Returns true if the operand was a load.
293 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000294 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000295 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000296 unsigned &SrcValueAlignment,
297 const MDNode *&TBAAInfo) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000298
Jim Laskey279f0532006-09-25 16:29:54 +0000299 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000300 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000301 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000302
Chris Lattner2392ae72010-04-15 04:48:01 +0000303 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000304 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Eli Friedman50185242011-11-12 00:35:34 +0000305 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
Chris Lattner2392ae72010-04-15 04:48:01 +0000306 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000307
Nate Begeman1d4d4142005-09-01 00:19:25 +0000308 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000309 void Run(CombineLevel AtLevel);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000310
Chris Lattner2392ae72010-04-15 04:48:01 +0000311 SelectionDAG &getDAG() const { return DAG; }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000312
Chris Lattner2392ae72010-04-15 04:48:01 +0000313 /// getShiftAmountTy - Returns a type large enough to hold any valid
314 /// shift amount - before type legalization these can be huge.
Owen Anderson95771af2011-02-25 21:41:48 +0000315 EVT getShiftAmountTy(EVT LHSTy) {
316 return LegalTypes ? TLI.getShiftAmountTy(LHSTy) : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000317 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000318
Chris Lattner2392ae72010-04-15 04:48:01 +0000319 /// isTypeLegal - This method returns true if we are running before type
320 /// legalization or if the specified VT is legal.
321 bool isTypeLegal(const EVT &VT) {
322 if (!LegalTypes) return true;
323 return TLI.isTypeLegal(VT);
324 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000325 };
326}
327
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000328
329namespace {
330/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
331/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000332class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000333 DAGCombiner &DC;
334public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000335 explicit WorkListRemover(DAGCombiner &dc)
336 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000337
Duncan Sandsedfcf592008-06-11 11:42:12 +0000338 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000339 DC.removeFromWorkList(N);
340 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000341};
342}
343
Chris Lattner24664722006-03-01 04:53:38 +0000344//===----------------------------------------------------------------------===//
345// TargetLowering::DAGCombinerInfo implementation
346//===----------------------------------------------------------------------===//
347
348void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
349 ((DAGCombiner*)DC)->AddToWorkList(N);
350}
351
Cameron Zwariched3caf92011-04-02 02:40:26 +0000352void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
353 ((DAGCombiner*)DC)->removeFromWorkList(N);
354}
355
Dan Gohman475871a2008-07-27 21:46:04 +0000356SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000357CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
358 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000359}
360
Dan Gohman475871a2008-07-27 21:46:04 +0000361SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000362CombineTo(SDNode *N, SDValue Res, bool AddTo) {
363 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000364}
365
366
Dan Gohman475871a2008-07-27 21:46:04 +0000367SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000368CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
369 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000370}
371
Dan Gohmane5af2d32009-01-29 01:59:02 +0000372void TargetLowering::DAGCombinerInfo::
373CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
374 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
375}
Chris Lattner24664722006-03-01 04:53:38 +0000376
Chris Lattner24664722006-03-01 04:53:38 +0000377//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000378// Helper Functions
379//===----------------------------------------------------------------------===//
380
381/// isNegatibleForFree - Return 1 if we can compute the negated form of the
382/// specified expression for the same cost as the expression itself, or 2 if we
383/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000384static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000385 const TargetLowering &TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000386 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000387 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000388 // No compile time optimizations on this type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 if (Op.getValueType() == MVT::ppcf128)
Dale Johannesendb44bf82007-10-16 23:38:29 +0000390 return 0;
391
Chris Lattner29446522007-05-14 22:04:50 +0000392 // fneg is removable even if it has multiple uses.
393 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000394
Chris Lattner29446522007-05-14 22:04:50 +0000395 // Don't allow anything with multiple uses.
396 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000397
Chris Lattner3adf9512007-05-25 02:19:06 +0000398 // Don't recurse exponentially.
399 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000400
Chris Lattner29446522007-05-14 22:04:50 +0000401 switch (Op.getOpcode()) {
402 default: return false;
403 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000404 // Don't invert constant FP values after legalize. The negated constant
405 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000406 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000407 case ISD::FADD:
408 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000409 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000410
Owen Andersonafd3d562012-03-06 00:29:31 +0000411 // After operation legalization, it might not be legal to create new FSUBs.
412 if (LegalOperations &&
413 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
414 return 0;
415
Craig Topper956342b2012-09-09 22:58:45 +0000416 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Andersonafd3d562012-03-06 00:29:31 +0000417 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
418 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000419 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000420 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Andersonafd3d562012-03-06 00:29:31 +0000421 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000422 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000423 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000424 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000425 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000426
Bill Wendlingd34470c2009-01-30 23:10:18 +0000427 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000428 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000429
Chris Lattner29446522007-05-14 22:04:50 +0000430 case ISD::FMUL:
431 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000432 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000433
Bill Wendlingd34470c2009-01-30 23:10:18 +0000434 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Andersonafd3d562012-03-06 00:29:31 +0000435 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
436 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000437 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000438
Owen Andersonafd3d562012-03-06 00:29:31 +0000439 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000440 Depth + 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000441
Chris Lattner29446522007-05-14 22:04:50 +0000442 case ISD::FP_EXTEND:
443 case ISD::FP_ROUND:
444 case ISD::FSIN:
Owen Andersonafd3d562012-03-06 00:29:31 +0000445 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000446 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000447 }
448}
449
450/// GetNegatedExpression - If isNegatibleForFree returns true, this function
451/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000452static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000453 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000454 // fneg is removable even if it has multiple uses.
455 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000456
Chris Lattner29446522007-05-14 22:04:50 +0000457 // Don't allow anything with multiple uses.
458 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000459
Chris Lattner3adf9512007-05-25 02:19:06 +0000460 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000461 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000462 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000463 case ISD::ConstantFP: {
464 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
465 V.changeSign();
466 return DAG.getConstantFP(V, Op.getValueType());
467 }
Chris Lattner29446522007-05-14 22:04:50 +0000468 case ISD::FADD:
469 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000470 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000471
Bill Wendlingd34470c2009-01-30 23:10:18 +0000472 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000473 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000474 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000475 &DAG.getTarget().Options, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000476 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000477 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000478 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000479 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000480 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
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(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000483 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000484 Op.getOperand(0));
485 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000486 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000487 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000488
Bill Wendlingd34470c2009-01-30 23:10:18 +0000489 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000490 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000491 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000492 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000493
Bill Wendlingd34470c2009-01-30 23:10:18 +0000494 // fold (fneg (fsub A, B)) -> (fsub B, A)
Bill Wendling35247c32009-01-30 00:45:56 +0000495 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
496 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000497
Chris Lattner29446522007-05-14 22:04:50 +0000498 case ISD::FMUL:
499 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000500 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000501
Bill Wendlingd34470c2009-01-30 23:10:18 +0000502 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000503 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000504 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000505 &DAG.getTarget().Options, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000506 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000507 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000508 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000509 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000510
Bill Wendlingd34470c2009-01-30 23:10:18 +0000511 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Bill Wendling35247c32009-01-30 00:45:56 +0000512 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000513 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000514 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000515 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000516
Chris Lattner29446522007-05-14 22:04:50 +0000517 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000518 case ISD::FSIN:
Bill Wendling35247c32009-01-30 00:45:56 +0000519 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000520 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000521 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000522 case ISD::FP_ROUND:
Bill Wendling35247c32009-01-30 00:45:56 +0000523 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000524 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000525 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000526 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000527 }
528}
Chris Lattner24664722006-03-01 04:53:38 +0000529
530
Nate Begeman4ebd8052005-09-01 23:24:04 +0000531// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
532// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000533// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000534// nodes based on the type of node we are checking. This simplifies life a
535// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000536static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
537 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000538 if (N.getOpcode() == ISD::SETCC) {
539 LHS = N.getOperand(0);
540 RHS = N.getOperand(1);
541 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000542 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000543 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000544 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000545 N.getOperand(2).getOpcode() == ISD::Constant &&
546 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000547 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000548 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
549 LHS = N.getOperand(0);
550 RHS = N.getOperand(1);
551 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000552 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000553 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000554 return false;
555}
556
Nate Begeman99801192005-09-07 23:25:52 +0000557// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
558// one use. If this is true, it allows the users to invert the operation for
559// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000560static bool isOneUseSetCC(SDValue N) {
561 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000562 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000563 return true;
564 return false;
565}
566
Bill Wendling35247c32009-01-30 00:45:56 +0000567SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
568 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000569 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000570 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
571 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000572 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000573 SDValue OpNode =
574 DAG.FoldConstantArithmetic(Opc, VT,
575 cast<ConstantSDNode>(N0.getOperand(1)),
576 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000577 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000578 }
579 if (N0.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000580 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
581 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
582 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000583 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000584 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000585 }
586 }
Bill Wendling35247c32009-01-30 00:45:56 +0000587
Nate Begemancd4d58c2006-02-03 06:46:56 +0000588 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
589 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000590 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000591 SDValue OpNode =
592 DAG.FoldConstantArithmetic(Opc, VT,
593 cast<ConstantSDNode>(N1.getOperand(1)),
594 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000595 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000596 }
597 if (N1.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000598 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlingd69c3142009-01-30 02:23:43 +0000599 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000600 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000601 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000602 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000603 }
604 }
Bill Wendling35247c32009-01-30 00:45:56 +0000605
Dan Gohman475871a2008-07-27 21:46:04 +0000606 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000607}
608
Dan Gohman475871a2008-07-27 21:46:04 +0000609SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
610 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000611 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
612 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000613 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000614 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000615 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000616 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000617 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000618 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000619 assert((!To[i].getNode() ||
620 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000621 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000622 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000623 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000624 if (AddTo) {
625 // Push the new nodes and any users onto the worklist
626 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000627 if (To[i].getNode()) {
628 AddToWorkList(To[i].getNode());
629 AddUsersToWorkList(To[i].getNode());
630 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000631 }
632 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000633
Dan Gohmandbe664a2009-01-19 21:44:21 +0000634 // Finally, if the node is now dead, remove it from the graph. The node
635 // may not be dead if the replacement process recursively simplified to
636 // something else needing this node.
637 if (N->use_empty()) {
638 // Nodes can be reintroduced into the worklist. Make sure we do not
639 // process a node that has been replaced.
640 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000641
Dan Gohmandbe664a2009-01-19 21:44:21 +0000642 // Finally, since the node is now dead, remove it from the graph.
643 DAG.DeleteNode(N);
644 }
Dan Gohman475871a2008-07-27 21:46:04 +0000645 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000646}
647
Evan Chenge5b51ac2010-04-17 06:13:15 +0000648void DAGCombiner::
649CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000650 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000651 // are deleted, make sure to remove them from our worklist.
652 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000653 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000654
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000655 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000656 AddToWorkList(TLO.New.getNode());
657 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000658
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000659 // Finally, if the node is now dead, remove it from the graph. The node
660 // may not be dead if the replacement process recursively simplified to
661 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000662 if (TLO.Old.getNode()->use_empty()) {
663 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000664
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000665 // If the operands of this node are only used by the node, they will now
666 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000667 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
668 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
669 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000670
Gabor Greifba36cb52008-08-28 21:40:38 +0000671 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000672 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000673}
674
675/// SimplifyDemandedBits - Check the specified integer node value to see if
676/// it can be simplified or if things it uses can be simplified by bit
677/// propagation. If so, return true.
678bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000679 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000680 APInt KnownZero, KnownOne;
681 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
682 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000683
Dan Gohmane5af2d32009-01-29 01:59:02 +0000684 // Revisit the node.
685 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000686
Dan Gohmane5af2d32009-01-29 01:59:02 +0000687 // Replace the old value with the new one.
688 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000689 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000690 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000691 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000692 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000693 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000694
Dan Gohmane5af2d32009-01-29 01:59:02 +0000695 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000696 return true;
697}
698
Evan Cheng95c57ea2010-04-24 04:43:44 +0000699void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
700 DebugLoc dl = Load->getDebugLoc();
701 EVT VT = Load->getValueType(0);
702 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000703
Evan Cheng95c57ea2010-04-24 04:43:44 +0000704 DEBUG(dbgs() << "\nReplacing.9 ";
705 Load->dump(&DAG);
706 dbgs() << "\nWith: ";
707 Trunc.getNode()->dump(&DAG);
708 dbgs() << '\n');
709 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000710 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
711 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng95c57ea2010-04-24 04:43:44 +0000712 removeFromWorkList(Load);
713 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000714 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000715}
716
717SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
718 Replace = false;
Evan Cheng4c26e932010-04-19 19:29:22 +0000719 DebugLoc dl = Op.getDebugLoc();
Evan Chenge5b51ac2010-04-17 06:13:15 +0000720 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000721 EVT MemVT = LD->getMemoryVT();
722 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000723 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000724 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000725 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000726 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000727 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000728 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000729 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000730 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000731 LD->isNonTemporal(), LD->getAlignment());
732 }
733
Evan Cheng4c26e932010-04-19 19:29:22 +0000734 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000735 switch (Opc) {
736 default: break;
737 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000738 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000739 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000740 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000741 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000742 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000743 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000744 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000745 case ISD::Constant: {
746 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000747 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000748 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000749 }
Evan Chengcaf77402010-04-23 19:10:30 +0000750 }
751
752 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000753 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000754 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000755}
756
Evan Cheng95c57ea2010-04-24 04:43:44 +0000757SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000758 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
759 return SDValue();
760 EVT OldVT = Op.getValueType();
761 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000762 bool Replace = false;
763 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
764 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000765 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000766 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000767
768 if (Replace)
769 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
770 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000771 DAG.getValueType(OldVT));
772}
773
Evan Cheng95c57ea2010-04-24 04:43:44 +0000774SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000775 EVT OldVT = Op.getValueType();
776 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000777 bool Replace = false;
778 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
779 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000780 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000781 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000782
783 if (Replace)
784 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
785 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000786}
787
Evan Cheng64b7bf72010-04-16 06:14:10 +0000788/// PromoteIntBinOp - Promote the specified integer binary operation if the
789/// target indicates it is beneficial. e.g. On x86, it's usually better to
790/// promote i16 operations to i32 since i16 instructions are longer.
791SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
792 if (!LegalOperations)
793 return SDValue();
794
795 EVT VT = Op.getValueType();
796 if (VT.isVector() || !VT.isInteger())
797 return SDValue();
798
Evan Chenge5b51ac2010-04-17 06:13:15 +0000799 // If operation type is 'undesirable', e.g. i16 on x86, consider
800 // promoting it.
801 unsigned Opc = Op.getOpcode();
802 if (TLI.isTypeDesirableForOp(Opc, VT))
803 return SDValue();
804
Evan Cheng64b7bf72010-04-16 06:14:10 +0000805 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000806 // Consult target whether it is a good idea to promote this operation and
807 // what's the right type to promote it to.
808 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000809 assert(PVT != VT && "Don't know what type to promote to!");
810
Evan Cheng95c57ea2010-04-24 04:43:44 +0000811 bool Replace0 = false;
812 SDValue N0 = Op.getOperand(0);
813 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
814 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000815 return SDValue();
816
Evan Cheng95c57ea2010-04-24 04:43:44 +0000817 bool Replace1 = false;
818 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000819 SDValue NN1;
820 if (N0 == N1)
821 NN1 = NN0;
822 else {
823 NN1 = PromoteOperand(N1, PVT, Replace1);
824 if (NN1.getNode() == 0)
825 return SDValue();
826 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000827
Evan Cheng95c57ea2010-04-24 04:43:44 +0000828 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000829 if (NN1.getNode())
830 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000831
832 if (Replace0)
833 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
834 if (Replace1)
835 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000836
Evan Chengac7eae52010-04-27 19:48:13 +0000837 DEBUG(dbgs() << "\nPromoting ";
838 Op.getNode()->dump(&DAG));
Evan Cheng07c4e102010-04-22 20:19:46 +0000839 DebugLoc dl = Op.getDebugLoc();
840 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000841 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000842 }
843 return SDValue();
844}
845
846/// PromoteIntShiftOp - Promote the specified integer shift operation if the
847/// target indicates it is beneficial. e.g. On x86, it's usually better to
848/// promote i16 operations to i32 since i16 instructions are longer.
849SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
850 if (!LegalOperations)
851 return SDValue();
852
853 EVT VT = Op.getValueType();
854 if (VT.isVector() || !VT.isInteger())
855 return SDValue();
856
857 // If operation type is 'undesirable', e.g. i16 on x86, consider
858 // promoting it.
859 unsigned Opc = Op.getOpcode();
860 if (TLI.isTypeDesirableForOp(Opc, VT))
861 return SDValue();
862
863 EVT PVT = VT;
864 // Consult target whether it is a good idea to promote this operation and
865 // what's the right type to promote it to.
866 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
867 assert(PVT != VT && "Don't know what type to promote to!");
868
Evan Cheng95c57ea2010-04-24 04:43:44 +0000869 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000870 SDValue N0 = Op.getOperand(0);
871 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000872 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000873 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000874 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000875 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000876 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000877 if (N0.getNode() == 0)
878 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000879
Evan Chenge5b51ac2010-04-17 06:13:15 +0000880 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000881 if (Replace)
882 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000883
Evan Chengac7eae52010-04-27 19:48:13 +0000884 DEBUG(dbgs() << "\nPromoting ";
885 Op.getNode()->dump(&DAG));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000886 DebugLoc dl = Op.getDebugLoc();
887 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000888 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000889 }
890 return SDValue();
891}
892
Evan Cheng4c26e932010-04-19 19:29:22 +0000893SDValue DAGCombiner::PromoteExtend(SDValue Op) {
894 if (!LegalOperations)
895 return SDValue();
896
897 EVT VT = Op.getValueType();
898 if (VT.isVector() || !VT.isInteger())
899 return SDValue();
900
901 // If operation type is 'undesirable', e.g. i16 on x86, consider
902 // promoting it.
903 unsigned Opc = Op.getOpcode();
904 if (TLI.isTypeDesirableForOp(Opc, VT))
905 return SDValue();
906
907 EVT PVT = VT;
908 // Consult target whether it is a good idea to promote this operation and
909 // what's the right type to promote it to.
910 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
911 assert(PVT != VT && "Don't know what type to promote to!");
912 // fold (aext (aext x)) -> (aext x)
913 // fold (aext (zext x)) -> (zext x)
914 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000915 DEBUG(dbgs() << "\nPromoting ";
916 Op.getNode()->dump(&DAG));
Evan Cheng4c26e932010-04-19 19:29:22 +0000917 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), VT, Op.getOperand(0));
918 }
919 return SDValue();
920}
921
922bool DAGCombiner::PromoteLoad(SDValue Op) {
923 if (!LegalOperations)
924 return false;
925
926 EVT VT = Op.getValueType();
927 if (VT.isVector() || !VT.isInteger())
928 return false;
929
930 // If operation type is 'undesirable', e.g. i16 on x86, consider
931 // promoting it.
932 unsigned Opc = Op.getOpcode();
933 if (TLI.isTypeDesirableForOp(Opc, VT))
934 return false;
935
936 EVT PVT = VT;
937 // Consult target whether it is a good idea to promote this operation and
938 // what's the right type to promote it to.
939 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
940 assert(PVT != VT && "Don't know what type to promote to!");
941
942 DebugLoc dl = Op.getDebugLoc();
943 SDNode *N = Op.getNode();
944 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000945 EVT MemVT = LD->getMemoryVT();
946 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000947 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000948 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000949 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000950 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000951 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000952 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000953 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000954 LD->isNonTemporal(), LD->getAlignment());
955 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
956
Evan Cheng95c57ea2010-04-24 04:43:44 +0000957 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000958 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000959 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000960 Result.getNode()->dump(&DAG);
961 dbgs() << '\n');
962 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000963 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
964 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Cheng4c26e932010-04-19 19:29:22 +0000965 removeFromWorkList(N);
966 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000967 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +0000968 return true;
969 }
970 return false;
971}
972
Evan Chenge5b51ac2010-04-17 06:13:15 +0000973
Chris Lattner29446522007-05-14 22:04:50 +0000974//===----------------------------------------------------------------------===//
975// Main DAG Combiner implementation
976//===----------------------------------------------------------------------===//
977
Duncan Sands25cf2272008-11-24 14:53:14 +0000978void DAGCombiner::Run(CombineLevel AtLevel) {
979 // set the instance variables, so that the various visit routines may use it.
980 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +0000981 LegalOperations = Level >= AfterLegalizeVectorOps;
982 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000983
Evan Cheng17a568b2008-08-29 22:21:44 +0000984 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +0000985 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
986 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +0000987 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000988
Evan Cheng17a568b2008-08-29 22:21:44 +0000989 // Create a dummy node (which is not added to allnodes), that adds a reference
990 // to the root node, preventing it from being deleted, and tracking any
991 // changes of the root.
992 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +0000993
Jim Laskey26f7fa72006-10-17 19:33:52 +0000994 // The root of the dag may dangle to deleted nodes until the dag combiner is
995 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000996 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +0000997
James Molloy6660c052012-02-16 09:17:04 +0000998 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +0000999 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +00001000 while (!WorkListContents.empty()) {
1001 SDNode *N;
1002 // The WorkListOrder holds the SDNodes in order, but it may contain duplicates.
1003 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1004 // worklist *should* contain, and check the node we want to visit is should
1005 // actually be visited.
1006 do {
Benjamin Kramerd5f76902012-03-10 00:23:58 +00001007 N = WorkListOrder.pop_back_val();
James Molloy6660c052012-02-16 09:17:04 +00001008 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001009
Evan Cheng17a568b2008-08-29 22:21:44 +00001010 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1011 // N is deleted from the DAG, since they too may now be dead or may have a
1012 // reduced number of uses, allowing other xforms.
1013 if (N->use_empty() && N != &Dummy) {
1014 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1015 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001016
Evan Cheng17a568b2008-08-29 22:21:44 +00001017 DAG.DeleteNode(N);
1018 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001019 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001020
Evan Cheng17a568b2008-08-29 22:21:44 +00001021 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001022
Evan Cheng17a568b2008-08-29 22:21:44 +00001023 if (RV.getNode() == 0)
1024 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001025
Evan Cheng17a568b2008-08-29 22:21:44 +00001026 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001027
Evan Cheng17a568b2008-08-29 22:21:44 +00001028 // If we get back the same node we passed in, rather than a new node or
1029 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001030 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001031 // mechanics for us, we have no work to do in this case.
1032 if (RV.getNode() == N)
1033 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001034
Evan Cheng17a568b2008-08-29 22:21:44 +00001035 assert(N->getOpcode() != ISD::DELETED_NODE &&
1036 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1037 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001038
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001039 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001040 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001041 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001042 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001043 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001044
Devang Patel9728ea22011-05-23 22:04:42 +00001045 // Transfer debug value.
1046 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001047 WorkListRemover DeadNodes(*this);
1048 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001049 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng17a568b2008-08-29 22:21:44 +00001050 else {
1051 assert(N->getValueType(0) == RV.getValueType() &&
1052 N->getNumValues() == 1 && "Type mismatch");
1053 SDValue OpV = RV;
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001054 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001055 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001056
Evan Cheng17a568b2008-08-29 22:21:44 +00001057 // Push the new node and any users onto the worklist
1058 AddToWorkList(RV.getNode());
1059 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001060
Evan Cheng17a568b2008-08-29 22:21:44 +00001061 // Add any uses of the old node to the worklist in case this node is the
1062 // last one that uses them. They may become dead after this node is
1063 // deleted.
1064 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1065 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001066
Dan Gohmandbe664a2009-01-19 21:44:21 +00001067 // Finally, if the node is now dead, remove it from the graph. The node
1068 // may not be dead if the replacement process recursively simplified to
1069 // something else needing this node.
1070 if (N->use_empty()) {
1071 // Nodes can be reintroduced into the worklist. Make sure we do not
1072 // process a node that has been replaced.
1073 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001074
Dan Gohmandbe664a2009-01-19 21:44:21 +00001075 // Finally, since the node is now dead, remove it from the graph.
1076 DAG.DeleteNode(N);
1077 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001078 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001079
Chris Lattner95038592005-10-05 06:35:28 +00001080 // If the root changed (e.g. it was a dead load, update the root).
1081 DAG.setRoot(Dummy.getValue());
Hal Finkel31490ba2012-04-16 03:33:22 +00001082 DAG.RemoveDeadNodes();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001083}
1084
Dan Gohman475871a2008-07-27 21:46:04 +00001085SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001086 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001087 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001088 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001089 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001090 case ISD::ADD: return visitADD(N);
1091 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001092 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001093 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001094 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001095 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001096 case ISD::MUL: return visitMUL(N);
1097 case ISD::SDIV: return visitSDIV(N);
1098 case ISD::UDIV: return visitUDIV(N);
1099 case ISD::SREM: return visitSREM(N);
1100 case ISD::UREM: return visitUREM(N);
1101 case ISD::MULHU: return visitMULHU(N);
1102 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001103 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1104 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001105 case ISD::SMULO: return visitSMULO(N);
1106 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001107 case ISD::SDIVREM: return visitSDIVREM(N);
1108 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001109 case ISD::AND: return visitAND(N);
1110 case ISD::OR: return visitOR(N);
1111 case ISD::XOR: return visitXOR(N);
1112 case ISD::SHL: return visitSHL(N);
1113 case ISD::SRA: return visitSRA(N);
1114 case ISD::SRL: return visitSRL(N);
1115 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001116 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001117 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001118 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001119 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7beb2005-09-16 00:54:12 +00001120 case ISD::SELECT: return visitSELECT(N);
1121 case ISD::SELECT_CC: return visitSELECT_CC(N);
1122 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001123 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1124 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001125 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001126 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1127 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001128 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001129 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001130 case ISD::FADD: return visitFADD(N);
1131 case ISD::FSUB: return visitFSUB(N);
1132 case ISD::FMUL: return visitFMUL(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00001133 case ISD::FMA: return visitFMA(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001134 case ISD::FDIV: return visitFDIV(N);
1135 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001136 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001137 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1138 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1139 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1140 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1141 case ISD::FP_ROUND: return visitFP_ROUND(N);
1142 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1143 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1144 case ISD::FNEG: return visitFNEG(N);
1145 case ISD::FABS: return visitFABS(N);
Owen Anderson7c626d32012-08-13 23:32:49 +00001146 case ISD::FFLOOR: return visitFFLOOR(N);
1147 case ISD::FCEIL: return visitFCEIL(N);
1148 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001149 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001150 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001151 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001152 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001153 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001154 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001155 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1156 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001157 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001158 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Jim Grosbach9a526492010-06-23 16:07:42 +00001159 case ISD::MEMBARRIER: return visitMEMBARRIER(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001160 }
Dan Gohman475871a2008-07-27 21:46:04 +00001161 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001162}
1163
Dan Gohman475871a2008-07-27 21:46:04 +00001164SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001165 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001166
1167 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001168 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001169 assert(N->getOpcode() != ISD::DELETED_NODE &&
1170 "Node was deleted but visit returned NULL!");
1171
1172 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1173 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1174
1175 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001176 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00001177 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001178
1179 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1180 }
1181 }
1182
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001183 // If nothing happened still, try promoting the operation.
1184 if (RV.getNode() == 0) {
1185 switch (N->getOpcode()) {
1186 default: break;
1187 case ISD::ADD:
1188 case ISD::SUB:
1189 case ISD::MUL:
1190 case ISD::AND:
1191 case ISD::OR:
1192 case ISD::XOR:
1193 RV = PromoteIntBinOp(SDValue(N, 0));
1194 break;
1195 case ISD::SHL:
1196 case ISD::SRA:
1197 case ISD::SRL:
1198 RV = PromoteIntShiftOp(SDValue(N, 0));
1199 break;
1200 case ISD::SIGN_EXTEND:
1201 case ISD::ZERO_EXTEND:
1202 case ISD::ANY_EXTEND:
1203 RV = PromoteExtend(SDValue(N, 0));
1204 break;
1205 case ISD::LOAD:
1206 if (PromoteLoad(SDValue(N, 0)))
1207 RV = SDValue(N, 0);
1208 break;
1209 }
1210 }
1211
Scott Michelfdc40a02009-02-17 22:15:04 +00001212 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001213 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001214 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001215 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1216 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001217 SDValue N0 = N->getOperand(0);
1218 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001219
Evan Cheng08b11732008-03-22 01:55:50 +00001220 // Constant operands are canonicalized to RHS.
1221 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001222 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001223 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1224 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001225 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001226 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001227 }
1228 }
1229
Dan Gohman389079b2007-10-08 17:57:15 +00001230 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001231}
Dan Gohman389079b2007-10-08 17:57:15 +00001232
Chris Lattner6270f682006-10-08 22:57:01 +00001233/// getInputChainForNode - Given a node, return its input chain if it has one,
1234/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001235static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001236 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001237 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001238 return N->getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001239 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001240 return N->getOperand(NumOps-1);
1241 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001242 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001243 return N->getOperand(i);
1244 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001245 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001246}
1247
Dan Gohman475871a2008-07-27 21:46:04 +00001248SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001249 // If N has two operands, where one has an input chain equal to the other,
1250 // the 'other' chain is redundant.
1251 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001252 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001253 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001254 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001255 return N->getOperand(1);
1256 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001257
Chris Lattnerc76d4412007-05-16 06:37:59 +00001258 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001259 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001260 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001261 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001262
Jim Laskey6ff23e52006-10-04 16:53:27 +00001263 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001264 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001265
Jim Laskey71382342006-10-07 23:37:56 +00001266 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001267 // encountered.
1268 for (unsigned i = 0; i < TFs.size(); ++i) {
1269 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001270
Jim Laskey6ff23e52006-10-04 16:53:27 +00001271 // Check each of the operands.
1272 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001273 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001274
Jim Laskey6ff23e52006-10-04 16:53:27 +00001275 switch (Op.getOpcode()) {
1276 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001277 // Entry tokens don't need to be added to the list. They are
1278 // rededundant.
1279 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001280 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001281
Jim Laskey6ff23e52006-10-04 16:53:27 +00001282 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001283 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001284 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001285 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001286 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001287 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001288 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001289 Changed = true;
1290 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001291 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001292 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001293
Jim Laskey6ff23e52006-10-04 16:53:27 +00001294 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001295 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001296 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001297 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001298 else
1299 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001300 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001301 }
1302 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001303 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001304
Dan Gohman475871a2008-07-27 21:46:04 +00001305 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001306
1307 // If we've change things around then replace token factor.
1308 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001309 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001310 // The entry token is the only possible outcome.
1311 Result = DAG.getEntryNode();
1312 } else {
1313 // New and improved token factor.
Bill Wendling5c71acf2009-01-30 01:13:16 +00001314 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001315 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001316 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001317
Jim Laskey274062c2006-10-13 23:32:28 +00001318 // Don't add users to work list.
1319 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001320 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001321
Jim Laskey6ff23e52006-10-04 16:53:27 +00001322 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001323}
1324
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001325/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001326SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001327 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001328 // Replacing results may cause a different MERGE_VALUES to suddenly
1329 // be CSE'd with N, and carry its uses with it. Iterate until no
1330 // uses remain, to ensure that the node can be safely deleted.
Pete Cooper3affd9e2012-06-20 19:35:43 +00001331 // First add the users of this node to the work list so that they
1332 // can be tried again once they have new operands.
1333 AddUsersToWorkList(N);
Dan Gohman00edf392009-08-10 23:43:19 +00001334 do {
1335 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001336 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman00edf392009-08-10 23:43:19 +00001337 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001338 removeFromWorkList(N);
1339 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001340 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001341}
1342
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001343static
Bill Wendlingd69c3142009-01-30 02:23:43 +00001344SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
1345 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001346 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001347 SDValue N00 = N0.getOperand(0);
1348 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001349 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001350
Gabor Greifba36cb52008-08-28 21:40:38 +00001351 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001352 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001353 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1354 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
1355 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
1356 N00.getOperand(0), N01),
1357 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
1358 N00.getOperand(1), N01));
1359 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001360 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001361
Dan Gohman475871a2008-07-27 21:46:04 +00001362 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001363}
1364
Dan Gohman475871a2008-07-27 21:46:04 +00001365SDValue DAGCombiner::visitADD(SDNode *N) {
1366 SDValue N0 = N->getOperand(0);
1367 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001368 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1369 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001370 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001371
1372 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001373 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001374 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001375 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001376 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001377
Dan Gohman613e0d82007-07-03 14:03:57 +00001378 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001379 if (N0.getOpcode() == ISD::UNDEF)
1380 return N0;
1381 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001382 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001384 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001385 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001386 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001387 if (N0C && !N1C)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001388 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001390 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001391 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001392 // fold (add Sym, c) -> Sym+c
1393 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001394 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001395 GA->getOpcode() == ISD::GlobalAddress)
Devang Patel0d881da2010-07-06 22:08:15 +00001396 return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001397 GA->getOffset() +
1398 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001399 // fold ((c1-A)+c2) -> (c1+c2)-A
1400 if (N1C && N0.getOpcode() == ISD::SUB)
1401 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001402 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001403 DAG.getConstant(N1C->getAPIntValue()+
1404 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001405 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001406 // reassociate add
Bill Wendling35247c32009-01-30 00:45:56 +00001407 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001408 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001409 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001410 // fold ((0-A) + B) -> B-A
1411 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1412 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001413 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001414 // fold (A + (0-B)) -> A-B
1415 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1416 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001417 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001418 // fold (A+(B-A)) -> B
1419 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001420 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001421 // fold ((B-A)+A) -> B
1422 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1423 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001424 // fold (A+(B-(A+C))) to (B-C)
1425 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001426 N0 == N1.getOperand(1).getOperand(0))
1427 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001428 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001429 // fold (A+(B-(C+A))) 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(1))
1432 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001433 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001434 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001435 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1436 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001437 N0 == N1.getOperand(0).getOperand(1))
1438 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1439 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001440
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001441 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1442 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1443 SDValue N00 = N0.getOperand(0);
1444 SDValue N01 = N0.getOperand(1);
1445 SDValue N10 = N1.getOperand(0);
1446 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001447
1448 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1449 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1450 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1451 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001452 }
Chris Lattner947c2892006-03-13 06:51:27 +00001453
Dan Gohman475871a2008-07-27 21:46:04 +00001454 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1455 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001456
Chris Lattner947c2892006-03-13 06:51:27 +00001457 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001458 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001459 APInt LHSZero, LHSOne;
1460 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001461 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001462
Dan Gohman948d8ea2008-02-20 16:33:30 +00001463 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001464 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001465
Chris Lattner947c2892006-03-13 06:51:27 +00001466 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1467 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001468 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001469 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001470 }
1471 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001472
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001473 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001474 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001475 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001476 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001477 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001478 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001479 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001480 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001481 }
1482
Dan Gohmancd9e1552010-01-19 23:30:49 +00001483 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1484 if (N1.getOpcode() == ISD::SHL &&
1485 N1.getOperand(0).getOpcode() == ISD::SUB)
1486 if (ConstantSDNode *C =
1487 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1488 if (C->getAPIntValue() == 0)
1489 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0,
1490 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1491 N1.getOperand(0).getOperand(1),
1492 N1.getOperand(1)));
1493 if (N0.getOpcode() == ISD::SHL &&
1494 N0.getOperand(0).getOpcode() == ISD::SUB)
1495 if (ConstantSDNode *C =
1496 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1497 if (C->getAPIntValue() == 0)
1498 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1,
1499 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1500 N0.getOperand(0).getOperand(1),
1501 N0.getOperand(1)));
1502
Owen Andersonbc146b02010-09-21 20:42:50 +00001503 if (N1.getOpcode() == ISD::AND) {
1504 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001505 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001506 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1507 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001508
Owen Andersonbc146b02010-09-21 20:42:50 +00001509 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1510 // and similar xforms where the inner op is either ~0 or 0.
1511 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
1512 DebugLoc DL = N->getDebugLoc();
1513 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1514 }
1515 }
1516
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001517 // add (sext i1), X -> sub X, (zext i1)
1518 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1519 N0.getOperand(0).getValueType() == MVT::i1 &&
1520 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
1521 DebugLoc DL = N->getDebugLoc();
1522 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1523 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1524 }
1525
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001526 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001527}
1528
Dan Gohman475871a2008-07-27 21:46:04 +00001529SDValue DAGCombiner::visitADDC(SDNode *N) {
1530 SDValue N0 = N->getOperand(0);
1531 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001532 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1533 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001534 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001535
Chris Lattner91153682007-03-04 20:03:15 +00001536 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001537 if (!N->hasAnyUseOfValue(1))
Craig Toppercc274522012-01-07 09:06:39 +00001538 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001539 DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001540 N->getDebugLoc(), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001541
Chris Lattner91153682007-03-04 20:03:15 +00001542 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001543 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001544 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001545
Chris Lattnerb6541762007-03-04 20:40:38 +00001546 // fold (addc x, 0) -> x + no carry out
1547 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001548 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001549 N->getDebugLoc(), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001550
Dale Johannesen874ae252009-06-02 03:12:52 +00001551 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001552 APInt LHSZero, LHSOne;
1553 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001554 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001555
Dan Gohman948d8ea2008-02-20 16:33:30 +00001556 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001557 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Chris Lattnerb6541762007-03-04 20:40:38 +00001559 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1560 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001561 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Bill Wendling14036c02009-01-30 02:38:00 +00001562 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001563 DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001564 N->getDebugLoc(), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001565 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001566
Dan Gohman475871a2008-07-27 21:46:04 +00001567 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001568}
1569
Dan Gohman475871a2008-07-27 21:46:04 +00001570SDValue DAGCombiner::visitADDE(SDNode *N) {
1571 SDValue N0 = N->getOperand(0);
1572 SDValue N1 = N->getOperand(1);
1573 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001574 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1575 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001576
Chris Lattner91153682007-03-04 20:03:15 +00001577 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001578 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001579 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1580 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001581
Chris Lattnerb6541762007-03-04 20:40:38 +00001582 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001583 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Craig Toppercc274522012-01-07 09:06:39 +00001584 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001585
Dan Gohman475871a2008-07-27 21:46:04 +00001586 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001587}
1588
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001589// Since it may not be valid to emit a fold to zero for vector initializers
1590// check if we can before folding.
1591static SDValue tryFoldToZero(DebugLoc DL, const TargetLowering &TLI, EVT VT,
Owen Anderson95771af2011-02-25 21:41:48 +00001592 SelectionDAG &DAG, bool LegalOperations) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001593 if (!VT.isVector()) {
1594 return DAG.getConstant(0, VT);
Dan Gohman71dc7c92011-05-17 22:20:36 +00001595 }
1596 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001597 // Produce a vector of zeros.
1598 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
1599 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
1600 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
1601 &Ops[0], Ops.size());
1602 }
1603 return SDValue();
1604}
1605
Dan Gohman475871a2008-07-27 21:46:04 +00001606SDValue DAGCombiner::visitSUB(SDNode *N) {
1607 SDValue N0 = N->getOperand(0);
1608 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001609 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1610 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001611 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1612 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001613 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001614
Dan Gohman7f321562007-06-25 16:23:39 +00001615 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001616 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001617 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001618 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001619 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001620
Chris Lattner854077d2005-10-17 01:07:11 +00001621 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001622 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001623 if (N0 == N1)
1624 return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001625 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001626 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001627 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001628 // fold (sub x, c) -> (add x, -c)
1629 if (N1C)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001630 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001631 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001632 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1633 if (N0C && N0C->isAllOnesValue())
1634 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001635 // fold A-(A-B) -> B
1636 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1637 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001638 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001639 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001640 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001641 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001642 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001643 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001644 // fold C2-(A+C1) -> (C2-C1)-A
1645 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
1646 SDValue NewC = DAG.getConstant((N0C->getAPIntValue() - N1C1->getAPIntValue()), VT);
1647 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, NewC,
Bill Wendling96cb1122012-07-19 00:04:14 +00001648 N1.getOperand(0));
Eric Christopher7332e6e2011-07-14 01:12:15 +00001649 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001650 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001651 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001652 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1653 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001654 N0.getOperand(1).getOperand(0) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001655 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1656 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001657 // fold ((A+(C+B))-B) -> A+C
1658 if (N0.getOpcode() == ISD::ADD &&
1659 N0.getOperand(1).getOpcode() == ISD::ADD &&
1660 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001661 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1662 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001663 // fold ((A-(B-C))-C) -> A-B
1664 if (N0.getOpcode() == ISD::SUB &&
1665 N0.getOperand(1).getOpcode() == ISD::SUB &&
1666 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001667 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1668 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001669
Dan Gohman613e0d82007-07-03 14:03:57 +00001670 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001671 if (N0.getOpcode() == ISD::UNDEF)
1672 return N0;
1673 if (N1.getOpcode() == ISD::UNDEF)
1674 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001675
Dan Gohman6520e202008-10-18 02:06:02 +00001676 // If the relocation model supports it, consider symbol offsets.
1677 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001678 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001679 // fold (sub Sym, c) -> Sym-c
1680 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Devang Patel0d881da2010-07-06 22:08:15 +00001681 return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001682 GA->getOffset() -
1683 (uint64_t)N1C->getSExtValue());
1684 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1685 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1686 if (GA->getGlobal() == GB->getGlobal())
1687 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1688 VT);
1689 }
1690
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001691 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001692}
1693
Craig Toppercc274522012-01-07 09:06:39 +00001694SDValue DAGCombiner::visitSUBC(SDNode *N) {
1695 SDValue N0 = N->getOperand(0);
1696 SDValue N1 = N->getOperand(1);
1697 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1698 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1699 EVT VT = N0.getValueType();
1700
1701 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001702 if (!N->hasAnyUseOfValue(1))
Craig Toppercc274522012-01-07 09:06:39 +00001703 return CombineTo(N, DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1),
1704 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1705 MVT::Glue));
1706
1707 // fold (subc x, x) -> 0 + no borrow
1708 if (N0 == N1)
1709 return CombineTo(N, DAG.getConstant(0, VT),
1710 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1711 MVT::Glue));
1712
1713 // fold (subc x, 0) -> x + no borrow
1714 if (N1C && N1C->isNullValue())
1715 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1716 MVT::Glue));
1717
1718 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1719 if (N0C && N0C->isAllOnesValue())
1720 return CombineTo(N, DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0),
1721 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1722 MVT::Glue));
1723
1724 return SDValue();
1725}
1726
1727SDValue DAGCombiner::visitSUBE(SDNode *N) {
1728 SDValue N0 = N->getOperand(0);
1729 SDValue N1 = N->getOperand(1);
1730 SDValue CarryIn = N->getOperand(2);
1731
1732 // fold (sube x, y, false) -> (subc x, y)
1733 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
1734 return DAG.getNode(ISD::SUBC, N->getDebugLoc(), N->getVTList(), N0, N1);
1735
1736 return SDValue();
1737}
1738
Dan Gohman475871a2008-07-27 21:46:04 +00001739SDValue DAGCombiner::visitMUL(SDNode *N) {
1740 SDValue N0 = N->getOperand(0);
1741 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001742 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1743 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001744 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001745
Dan Gohman7f321562007-06-25 16:23:39 +00001746 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001747 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001748 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001749 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001750 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001751
Dan Gohman613e0d82007-07-03 14:03:57 +00001752 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001753 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001754 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001755 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001756 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001757 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001758 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001759 if (N0C && !N1C)
Bill Wendling9c8148a2009-01-30 02:45:56 +00001760 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001761 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001762 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001763 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001764 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001765 if (N1C && N1C->isAllOnesValue())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001766 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1767 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001768 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001769 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001770 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001771 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001772 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001773 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Chris Lattner66b8bc32009-03-09 20:22:18 +00001774 if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) {
1775 unsigned Log2Val = (-N1C->getAPIntValue()).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001776 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001777 // single-use add), we should put the negate there.
Bill Wendling9c8148a2009-01-30 02:45:56 +00001778 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1779 DAG.getConstant(0, VT),
Bill Wendling73e16b22009-01-30 02:49:26 +00001780 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001781 DAG.getConstant(Log2Val,
1782 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001783 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001784 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendling73e16b22009-01-30 02:49:26 +00001785 if (N1C && N0.getOpcode() == ISD::SHL &&
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001786 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001787 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1788 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001789 AddToWorkList(C3.getNode());
Bill Wendling9c8148a2009-01-30 02:45:56 +00001790 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1791 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001792 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001793
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001794 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1795 // use.
1796 {
Dan Gohman475871a2008-07-27 21:46:04 +00001797 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001798 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1799 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001800 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001801 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001802 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001803 isa<ConstantSDNode>(N1.getOperand(1)) &&
1804 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001805 Sh = N1; Y = N0;
1806 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001807
Gabor Greifba36cb52008-08-28 21:40:38 +00001808 if (Sh.getNode()) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001809 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1810 Sh.getOperand(0), Y);
1811 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1812 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001813 }
1814 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001815
Chris Lattnera1deca32006-03-04 23:33:26 +00001816 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Scott Michelfdc40a02009-02-17 22:15:04 +00001817 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendling9c8148a2009-01-30 02:45:56 +00001818 isa<ConstantSDNode>(N0.getOperand(1)))
1819 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1820 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1821 N0.getOperand(0), N1),
1822 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1823 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001824
Nate Begemancd4d58c2006-02-03 06:46:56 +00001825 // reassociate mul
Bill Wendling35247c32009-01-30 00:45:56 +00001826 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001827 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001828 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001829
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001830 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001831}
1832
Dan Gohman475871a2008-07-27 21:46:04 +00001833SDValue DAGCombiner::visitSDIV(SDNode *N) {
1834 SDValue N0 = N->getOperand(0);
1835 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001836 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1837 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001838 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001839
Dan Gohman7f321562007-06-25 16:23:39 +00001840 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001841 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001842 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001843 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001844 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001845
Nate Begeman1d4d4142005-09-01 00:19:25 +00001846 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001847 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001848 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001849 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001850 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001851 return N0;
1852 // fold (sdiv X, -1) -> 0-X
1853 if (N1C && N1C->isAllOnesValue())
Bill Wendling944d34b2009-01-30 02:52:17 +00001854 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1855 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001856 // If we know the sign bits of both operands are zero, strength reduce to a
1857 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001858 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001859 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling944d34b2009-01-30 02:52:17 +00001860 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1861 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001862 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001863 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001864 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001865 (N1C->getAPIntValue().isPowerOf2() ||
1866 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001867 // If dividing by powers of two is cheap, then don't perform the following
1868 // fold.
1869 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001870 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001871
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001872 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001873
Chris Lattner8f4880b2006-02-16 08:02:36 +00001874 // Splat the sign bit into the register
Bill Wendling944d34b2009-01-30 02:52:17 +00001875 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1876 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001877 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001878 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001879
Chris Lattner8f4880b2006-02-16 08:02:36 +00001880 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling944d34b2009-01-30 02:52:17 +00001881 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1882 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001883 getShiftAmountTy(SGN.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001884 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001885 AddToWorkList(SRL.getNode());
1886 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling944d34b2009-01-30 02:52:17 +00001887 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001888 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001889
Nate Begeman405e3ec2005-10-21 00:02:42 +00001890 // If we're dividing by a positive value, we're done. Otherwise, we must
1891 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001892 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001893 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001894
Gabor Greifba36cb52008-08-28 21:40:38 +00001895 AddToWorkList(SRA.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001896 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1897 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001898 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001899
Nate Begeman69575232005-10-20 02:15:44 +00001900 // if integer divide is expensive and we satisfy the requirements, emit an
1901 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001902 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001903 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001904 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001905 }
Dan Gohman7f321562007-06-25 16:23:39 +00001906
Dan Gohman613e0d82007-07-03 14:03:57 +00001907 // undef / X -> 0
1908 if (N0.getOpcode() == ISD::UNDEF)
1909 return DAG.getConstant(0, VT);
1910 // X / undef -> undef
1911 if (N1.getOpcode() == ISD::UNDEF)
1912 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001913
Dan Gohman475871a2008-07-27 21:46:04 +00001914 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001915}
1916
Dan Gohman475871a2008-07-27 21:46:04 +00001917SDValue DAGCombiner::visitUDIV(SDNode *N) {
1918 SDValue N0 = N->getOperand(0);
1919 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001920 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1921 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001922 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001923
Dan Gohman7f321562007-06-25 16:23:39 +00001924 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001925 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001926 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001927 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001928 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001929
Nate Begeman1d4d4142005-09-01 00:19:25 +00001930 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001931 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001932 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001933 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001934 if (N1C && N1C->getAPIntValue().isPowerOf2())
Scott Michelfdc40a02009-02-17 22:15:04 +00001935 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001936 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001937 getShiftAmountTy(N0.getValueType())));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001938 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1939 if (N1.getOpcode() == ISD::SHL) {
1940 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001941 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001942 EVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendling07d85142009-01-30 02:55:25 +00001943 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1944 N1.getOperand(1),
1945 DAG.getConstant(SHC->getAPIntValue()
1946 .logBase2(),
1947 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001948 AddToWorkList(Add.getNode());
Bill Wendling07d85142009-01-30 02:55:25 +00001949 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001950 }
1951 }
1952 }
Nate Begeman69575232005-10-20 02:15:44 +00001953 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001954 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001955 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001956 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001957 }
Dan Gohman7f321562007-06-25 16:23:39 +00001958
Dan Gohman613e0d82007-07-03 14:03:57 +00001959 // undef / X -> 0
1960 if (N0.getOpcode() == ISD::UNDEF)
1961 return DAG.getConstant(0, VT);
1962 // X / undef -> undef
1963 if (N1.getOpcode() == ISD::UNDEF)
1964 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001965
Dan Gohman475871a2008-07-27 21:46:04 +00001966 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001967}
1968
Dan Gohman475871a2008-07-27 21:46:04 +00001969SDValue DAGCombiner::visitSREM(SDNode *N) {
1970 SDValue N0 = N->getOperand(0);
1971 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001972 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1973 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001974 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001975
Nate Begeman1d4d4142005-09-01 00:19:25 +00001976 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001977 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001978 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001979 // If we know the sign bits of both operands are zero, strength reduce to a
1980 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001981 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001982 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001983 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00001984 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001985
Dan Gohman77003042007-11-26 23:46:11 +00001986 // If X/C can be simplified by the division-by-constant logic, lower
1987 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001988 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001989 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001990 AddToWorkList(Div.getNode());
1991 SDValue OptimizedDiv = combine(Div.getNode());
1992 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001993 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1994 OptimizedDiv, N1);
1995 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001996 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001997 return Sub;
1998 }
Chris Lattner26d29902006-10-12 20:58:32 +00001999 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002000
Dan Gohman613e0d82007-07-03 14:03:57 +00002001 // undef % X -> 0
2002 if (N0.getOpcode() == ISD::UNDEF)
2003 return DAG.getConstant(0, VT);
2004 // X % undef -> undef
2005 if (N1.getOpcode() == ISD::UNDEF)
2006 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002007
Dan Gohman475871a2008-07-27 21:46:04 +00002008 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002009}
2010
Dan Gohman475871a2008-07-27 21:46:04 +00002011SDValue DAGCombiner::visitUREM(SDNode *N) {
2012 SDValue N0 = N->getOperand(0);
2013 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002014 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2015 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002016 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002017
Nate Begeman1d4d4142005-09-01 00:19:25 +00002018 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002019 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002020 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002021 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002022 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002023 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002024 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002025 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2026 if (N1.getOpcode() == ISD::SHL) {
2027 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002028 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002029 SDValue Add =
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002030 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002031 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002032 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002033 AddToWorkList(Add.getNode());
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002034 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002035 }
2036 }
2037 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002038
Dan Gohman77003042007-11-26 23:46:11 +00002039 // If X/C can be simplified by the division-by-constant logic, lower
2040 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002041 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002042 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002043 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002044 SDValue OptimizedDiv = combine(Div.getNode());
2045 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002046 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
2047 OptimizedDiv, N1);
2048 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002049 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002050 return Sub;
2051 }
Chris Lattner26d29902006-10-12 20:58:32 +00002052 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002053
Dan Gohman613e0d82007-07-03 14:03:57 +00002054 // undef % X -> 0
2055 if (N0.getOpcode() == ISD::UNDEF)
2056 return DAG.getConstant(0, VT);
2057 // X % undef -> undef
2058 if (N1.getOpcode() == ISD::UNDEF)
2059 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002060
Dan Gohman475871a2008-07-27 21:46:04 +00002061 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002062}
2063
Dan Gohman475871a2008-07-27 21:46:04 +00002064SDValue DAGCombiner::visitMULHS(SDNode *N) {
2065 SDValue N0 = N->getOperand(0);
2066 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002067 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002068 EVT VT = N->getValueType(0);
Chris Lattnerde1c3602010-12-13 08:39:01 +00002069 DebugLoc DL = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00002070
Nate Begeman1d4d4142005-09-01 00:19:25 +00002071 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002072 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002073 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002074 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002075 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendling326411d2009-01-30 03:00:18 +00002076 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
2077 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002078 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002079 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002080 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002081 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002082
Chris Lattnerde1c3602010-12-13 08:39:01 +00002083 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2084 // plus a shift.
2085 if (VT.isSimple() && !VT.isVector()) {
2086 MVT Simple = VT.getSimpleVT();
2087 unsigned SimpleSize = Simple.getSizeInBits();
2088 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2089 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2090 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2091 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2092 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002093 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002094 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002095 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2096 }
2097 }
Owen Anderson95771af2011-02-25 21:41:48 +00002098
Dan Gohman475871a2008-07-27 21:46:04 +00002099 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002100}
2101
Dan Gohman475871a2008-07-27 21:46:04 +00002102SDValue DAGCombiner::visitMULHU(SDNode *N) {
2103 SDValue N0 = N->getOperand(0);
2104 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002105 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002106 EVT VT = N->getValueType(0);
Chris Lattnerde1c3602010-12-13 08:39:01 +00002107 DebugLoc DL = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00002108
Nate Begeman1d4d4142005-09-01 00:19:25 +00002109 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002110 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002111 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002112 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002113 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002114 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002115 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002116 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002117 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002118
Chris Lattnerde1c3602010-12-13 08:39:01 +00002119 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2120 // plus a shift.
2121 if (VT.isSimple() && !VT.isVector()) {
2122 MVT Simple = VT.getSimpleVT();
2123 unsigned SimpleSize = Simple.getSizeInBits();
2124 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2125 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2126 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2127 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2128 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2129 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002130 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002131 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2132 }
2133 }
Owen Anderson95771af2011-02-25 21:41:48 +00002134
Dan Gohman475871a2008-07-27 21:46:04 +00002135 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002136}
2137
Dan Gohman389079b2007-10-08 17:57:15 +00002138/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2139/// compute two values. LoOp and HiOp give the opcodes for the two computations
2140/// that are being performed. Return true if a simplification was made.
2141///
Scott Michelfdc40a02009-02-17 22:15:04 +00002142SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002143 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002144 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002145 bool HiExists = N->hasAnyUseOfValue(1);
2146 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002147 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002148 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00002149 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
2150 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002151 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002152 }
2153
2154 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002155 bool LoExists = N->hasAnyUseOfValue(0);
2156 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002157 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002158 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00002159 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
2160 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002161 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002162 }
2163
Evan Cheng44711942007-11-08 09:25:29 +00002164 // If both halves are used, return as it is.
2165 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002166 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002167
2168 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002169 if (LoExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00002170 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
2171 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002172 AddToWorkList(Lo.getNode());
2173 SDValue LoOpt = combine(Lo.getNode());
2174 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002175 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002176 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002177 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002178 }
2179
Evan Cheng44711942007-11-08 09:25:29 +00002180 if (HiExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00002181 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002182 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002183 AddToWorkList(Hi.getNode());
2184 SDValue HiOpt = combine(Hi.getNode());
2185 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002186 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002187 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002188 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002189 }
Bill Wendling826d1142009-01-30 03:08:40 +00002190
Dan Gohman475871a2008-07-27 21:46:04 +00002191 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002192}
2193
Dan Gohman475871a2008-07-27 21:46:04 +00002194SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2195 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002196 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002197
Chris Lattner33e77d32010-12-15 06:04:19 +00002198 EVT VT = N->getValueType(0);
2199 DebugLoc DL = N->getDebugLoc();
2200
2201 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2202 // plus a shift.
2203 if (VT.isSimple() && !VT.isVector()) {
2204 MVT Simple = VT.getSimpleVT();
2205 unsigned SimpleSize = Simple.getSizeInBits();
2206 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2207 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2208 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2209 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2210 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2211 // Compute the high part as N1.
2212 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002213 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002214 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2215 // Compute the low part as N0.
2216 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2217 return CombineTo(N, Lo, Hi);
2218 }
2219 }
Owen Anderson95771af2011-02-25 21:41:48 +00002220
Dan Gohman475871a2008-07-27 21:46:04 +00002221 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002222}
2223
Dan Gohman475871a2008-07-27 21:46:04 +00002224SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2225 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002226 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002227
Chris Lattner33e77d32010-12-15 06:04:19 +00002228 EVT VT = N->getValueType(0);
2229 DebugLoc DL = N->getDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002230
Chris Lattner33e77d32010-12-15 06:04:19 +00002231 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2232 // plus a shift.
2233 if (VT.isSimple() && !VT.isVector()) {
2234 MVT Simple = VT.getSimpleVT();
2235 unsigned SimpleSize = Simple.getSizeInBits();
2236 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2237 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2238 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2239 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2240 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2241 // Compute the high part as N1.
2242 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002243 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002244 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2245 // Compute the low part as N0.
2246 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2247 return CombineTo(N, Lo, Hi);
2248 }
2249 }
Owen Anderson95771af2011-02-25 21:41:48 +00002250
Dan Gohman475871a2008-07-27 21:46:04 +00002251 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002252}
2253
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002254SDValue DAGCombiner::visitSMULO(SDNode *N) {
2255 // (smulo x, 2) -> (saddo x, x)
2256 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2257 if (C2->getAPIntValue() == 2)
2258 return DAG.getNode(ISD::SADDO, N->getDebugLoc(), N->getVTList(),
2259 N->getOperand(0), N->getOperand(0));
2260
2261 return SDValue();
2262}
2263
2264SDValue DAGCombiner::visitUMULO(SDNode *N) {
2265 // (umulo x, 2) -> (uaddo x, x)
2266 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2267 if (C2->getAPIntValue() == 2)
2268 return DAG.getNode(ISD::UADDO, N->getDebugLoc(), N->getVTList(),
2269 N->getOperand(0), N->getOperand(0));
2270
2271 return SDValue();
2272}
2273
Dan Gohman475871a2008-07-27 21:46:04 +00002274SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2275 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002276 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002277
Dan Gohman475871a2008-07-27 21:46:04 +00002278 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002279}
2280
Dan Gohman475871a2008-07-27 21:46:04 +00002281SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2282 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002283 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002284
Dan Gohman475871a2008-07-27 21:46:04 +00002285 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002286}
2287
Chris Lattner35e5c142006-05-05 05:51:50 +00002288/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2289/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002290SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2291 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002292 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002293 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002294
Dan Gohmanff00a552010-01-14 03:08:49 +00002295 // Bail early if none of these transforms apply.
2296 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2297
Chris Lattner540121f2006-05-05 06:31:05 +00002298 // For each of OP in AND/OR/XOR:
2299 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2300 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2301 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002302 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002303 //
2304 // do not sink logical op inside of a vector extend, since it may combine
2305 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002306 EVT Op0VT = N0.getOperand(0).getValueType();
2307 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002308 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002309 // Avoid infinite looping with PromoteIntBinOp.
2310 (N0.getOpcode() == ISD::ANY_EXTEND &&
2311 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002312 (N0.getOpcode() == ISD::TRUNCATE &&
2313 (!TLI.isZExtFree(VT, Op0VT) ||
2314 !TLI.isTruncateFree(Op0VT, VT)) &&
2315 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002316 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002317 Op0VT == N1.getOperand(0).getValueType() &&
2318 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002319 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2320 N0.getOperand(0).getValueType(),
2321 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002322 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002323 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002324 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002325
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002326 // For each of OP in SHL/SRL/SRA/AND...
2327 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2328 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2329 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002330 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002331 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002332 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002333 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2334 N0.getOperand(0).getValueType(),
2335 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002336 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002337 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
2338 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002339 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002340
Nadav Rotem4ac90812012-04-01 19:31:22 +00002341 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2342 // Only perform this optimization after type legalization and before
2343 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2344 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2345 // we don't want to undo this promotion.
2346 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2347 // on scalars.
2348 if ((N0.getOpcode() == ISD::BITCAST || N0.getOpcode() == ISD::SCALAR_TO_VECTOR)
Nadav Rotem2dd83eb2012-07-10 13:25:08 +00002349 && Level == AfterLegalizeTypes) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002350 SDValue In0 = N0.getOperand(0);
2351 SDValue In1 = N1.getOperand(0);
2352 EVT In0Ty = In0.getValueType();
2353 EVT In1Ty = In1.getValueType();
2354 // If both incoming values are integers, and the original types are the same.
2355 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
2356 SDValue Op = DAG.getNode(N->getOpcode(), N->getDebugLoc(), In0Ty, In0, In1);
2357 SDValue BC = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, Op);
2358 AddToWorkList(Op.getNode());
2359 return BC;
2360 }
2361 }
2362
2363 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2364 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2365 // If both shuffles use the same mask, and both shuffle within a single
2366 // vector, then it is worthwhile to move the swizzle after the operation.
2367 // The type-legalizer generates this pattern when loading illegal
2368 // vector types from memory. In many cases this allows additional shuffle
2369 // optimizations.
Craig Topperf9204232012-04-09 07:19:09 +00002370 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2371 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2372 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002373 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2374 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topperf9204232012-04-09 07:19:09 +00002375
2376 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2377 "Inputs to shuffles are not the same type");
Nadav Rotem4ac90812012-04-01 19:31:22 +00002378
2379 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotem4ac90812012-04-01 19:31:22 +00002380
2381 // Check that both shuffles use the same mask. The masks are known to be of
2382 // the same length because the result vector type is the same.
2383 bool SameMask = true;
2384 for (unsigned i = 0; i != NumElts; ++i) {
2385 int Idx0 = SVN0->getMaskElt(i);
2386 int Idx1 = SVN1->getMaskElt(i);
2387 if (Idx0 != Idx1) {
2388 SameMask = false;
2389 break;
2390 }
2391 }
2392
Craig Topperf9204232012-04-09 07:19:09 +00002393 if (SameMask) {
2394 SDValue Op = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
2395 N0.getOperand(0), N1.getOperand(0));
Nadav Rotem4ac90812012-04-01 19:31:22 +00002396 AddToWorkList(Op.getNode());
Craig Topperf9204232012-04-09 07:19:09 +00002397 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Op,
2398 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002399 }
2400 }
Craig Topperf9204232012-04-09 07:19:09 +00002401
Dan Gohman475871a2008-07-27 21:46:04 +00002402 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002403}
2404
Dan Gohman475871a2008-07-27 21:46:04 +00002405SDValue DAGCombiner::visitAND(SDNode *N) {
2406 SDValue N0 = N->getOperand(0);
2407 SDValue N1 = N->getOperand(1);
2408 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002409 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2410 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002411 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002412 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002413
Dan Gohman7f321562007-06-25 16:23:39 +00002414 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002415 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002416 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002417 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002418 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002419
Dan Gohman613e0d82007-07-03 14:03:57 +00002420 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002421 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002422 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002423 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002424 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002425 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002426 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002427 if (N0C && !N1C)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00002428 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002429 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002430 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002431 return N0;
2432 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002433 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002434 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002435 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002436 // reassociate and
Bill Wendling35247c32009-01-30 00:45:56 +00002437 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002438 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002439 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002440 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002441 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002442 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002443 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002444 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002445 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2446 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002447 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002448 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002449 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002450 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendling2627a882009-01-30 20:43:18 +00002451 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
2452 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002453
Chris Lattner1ec05d12006-03-01 21:47:21 +00002454 // Replace uses of the AND with uses of the Zero extend node.
2455 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002456
Chris Lattner3603cd62006-02-02 07:17:31 +00002457 // We actually want to replace all uses of the any_extend with the
2458 // zero_extend, to avoid duplicating things. This will later cause this
2459 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002460 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002461 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002462 }
2463 }
James Molloy6259dcd2012-02-20 12:02:38 +00002464 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
2465 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2466 // already be zero by virtue of the width of the base type of the load.
2467 //
2468 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2469 // more cases.
2470 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2471 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2472 N0.getOpcode() == ISD::LOAD) {
2473 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2474 N0 : N0.getOperand(0) );
2475
2476 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2477 // This can be a pure constant or a vector splat, in which case we treat the
2478 // vector as a scalar and use the splat value.
2479 APInt Constant = APInt::getNullValue(1);
2480 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2481 Constant = C->getAPIntValue();
2482 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2483 APInt SplatValue, SplatUndef;
2484 unsigned SplatBitSize;
2485 bool HasAnyUndefs;
2486 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2487 SplatBitSize, HasAnyUndefs);
2488 if (IsSplat) {
2489 // Undef bits can contribute to a possible optimisation if set, so
2490 // set them.
2491 SplatValue |= SplatUndef;
2492
2493 // The splat value may be something like "0x00FFFFFF", which means 0 for
2494 // the first vector value and FF for the rest, repeating. We need a mask
2495 // that will apply equally to all members of the vector, so AND all the
2496 // lanes of the constant together.
2497 EVT VT = Vector->getValueType(0);
2498 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002499
2500 // If the splat value has been compressed to a bitlength lower
2501 // than the size of the vector lane, we need to re-expand it to
2502 // the lane size.
2503 if (BitWidth > SplatBitSize)
2504 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2505 SplatBitSize < BitWidth;
2506 SplatBitSize = SplatBitSize * 2)
2507 SplatValue |= SplatValue.shl(SplatBitSize);
2508
James Molloy6259dcd2012-02-20 12:02:38 +00002509 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002510 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy6259dcd2012-02-20 12:02:38 +00002511 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2512 }
2513 }
2514
2515 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2516 // actually legal and isn't going to get expanded, else this is a false
2517 // optimisation.
2518 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2519 Load->getMemoryVT());
2520
2521 // Resize the constant to the same size as the original memory access before
2522 // extension. If it is still the AllOnesValue then this AND is completely
2523 // unneeded.
2524 Constant =
2525 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2526
2527 bool B;
2528 switch (Load->getExtensionType()) {
2529 default: B = false; break;
2530 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2531 case ISD::ZEXTLOAD:
2532 case ISD::NON_EXTLOAD: B = true; break;
2533 }
2534
2535 if (B && Constant.isAllOnesValue()) {
2536 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2537 // preserve semantics once we get rid of the AND.
2538 SDValue NewLoad(Load, 0);
2539 if (Load->getExtensionType() == ISD::EXTLOAD) {
2540 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
2541 Load->getValueType(0), Load->getDebugLoc(),
2542 Load->getChain(), Load->getBasePtr(),
2543 Load->getOffset(), Load->getMemoryVT(),
2544 Load->getMemOperand());
2545 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkeld65e4632012-06-20 15:42:48 +00002546 if (Load->getNumValues() == 3) {
2547 // PRE/POST_INC loads have 3 values.
2548 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2549 NewLoad.getValue(2) };
2550 CombineTo(Load, To, 3, true);
2551 } else {
2552 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2553 }
James Molloy6259dcd2012-02-20 12:02:38 +00002554 }
2555
2556 // Fold the AND away, taking care not to fold to the old load node if we
2557 // replaced it.
2558 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2559
2560 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2561 }
2562 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002563 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2564 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2565 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2566 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002567
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002568 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002569 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002570 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002571 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002572 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2573 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002574 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002575 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002576 }
Bill Wendling2627a882009-01-30 20:43:18 +00002577 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002578 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002579 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
2580 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002581 AddToWorkList(ANDNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002582 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002583 }
Bill Wendling2627a882009-01-30 20:43:18 +00002584 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002585 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendling2627a882009-01-30 20:43:18 +00002586 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2587 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002588 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002589 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002590 }
2591 }
2592 // canonicalize equivalent to ll == rl
2593 if (LL == RR && LR == RL) {
2594 Op1 = ISD::getSetCCSwappedOperands(Op1);
2595 std::swap(RL, RR);
2596 }
2597 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002598 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002599 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002600 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002601 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling2627a882009-01-30 20:43:18 +00002602 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2603 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002604 }
2605 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002606
Bill Wendling2627a882009-01-30 20:43:18 +00002607 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002608 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002609 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002610 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002611 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002612
Nate Begemande996292006-02-03 22:24:05 +00002613 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2614 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002615 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002616 SimplifyDemandedBits(SDValue(N, 0)))
2617 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002618
Nate Begemanded49632005-10-13 03:11:28 +00002619 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002620 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002621 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002622 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002623 // If we zero all the possible extended bits, then we can turn this into
2624 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002625 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002626 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002627 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002628 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002629 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00002630 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002631 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002632 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002633 LN0->isVolatile(), LN0->isNonTemporal(),
2634 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002635 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002636 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002637 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002638 }
2639 }
2640 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002641 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002642 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002643 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002644 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002645 // If we zero all the possible extended bits, then we can turn this into
2646 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002647 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002648 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002649 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002650 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002651 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00002652 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002653 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002654 LN0->getBasePtr(), LN0->getPointerInfo(),
2655 MemVT,
David Greene1e559442010-02-15 17:00:31 +00002656 LN0->isVolatile(), LN0->isNonTemporal(),
2657 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002658 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002659 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002660 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002661 }
2662 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002663
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002664 // fold (and (load x), 255) -> (zextload x, i8)
2665 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002666 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2667 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2668 (N0.getOpcode() == ISD::ANY_EXTEND &&
2669 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2670 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2671 LoadSDNode *LN0 = HasAnyExt
2672 ? cast<LoadSDNode>(N0.getOperand(0))
2673 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002674 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerbd1fccf2010-01-07 21:59:23 +00002675 LN0->isUnindexed() && N0.hasOneUse() && LN0->hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002676 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002677 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2678 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2679 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002680
Evan Chengd40d03e2010-01-06 19:38:29 +00002681 if (ExtVT == LoadedVT &&
2682 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002683 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002684
2685 SDValue NewLoad =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002686 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002687 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002688 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002689 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2690 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002691 AddToWorkList(N);
2692 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2693 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2694 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002695
Chris Lattneref7634c2010-01-07 21:53:27 +00002696 // Do not change the width of a volatile load.
2697 // Do not generate loads of non-round integer types since these can
2698 // be expensive (and would be wrong if the type is not byte sized).
2699 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2700 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2701 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002702
Chris Lattneref7634c2010-01-07 21:53:27 +00002703 unsigned Alignment = LN0->getAlignment();
2704 SDValue NewPtr = LN0->getBasePtr();
2705
2706 // For big endian targets, we need to add an offset to the pointer
2707 // to load the correct bytes. For little endian systems, we merely
2708 // need to read fewer bytes from the same pointer.
2709 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002710 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2711 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2712 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Chris Lattneref7634c2010-01-07 21:53:27 +00002713 NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType,
2714 NewPtr, DAG.getConstant(PtrOff, PtrType));
2715 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002716 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002717
2718 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002719
Chris Lattneref7634c2010-01-07 21:53:27 +00002720 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2721 SDValue Load =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002722 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002723 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002724 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002725 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2726 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002727 AddToWorkList(N);
2728 CombineTo(LN0, Load, Load.getValue(1));
2729 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002730 }
Evan Cheng466685d2006-10-09 20:57:25 +00002731 }
Chris Lattner15045b62006-02-28 06:35:35 +00002732 }
2733 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002734
Evan Chenga9e13ba2012-07-17 18:54:11 +00002735 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2736 VT.getSizeInBits() <= 64) {
2737 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2738 APInt ADDC = ADDI->getAPIntValue();
2739 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2740 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2741 // immediate for an add, but it is legal if its top c2 bits are set,
2742 // transform the ADD so the immediate doesn't need to be materialized
2743 // in a register.
2744 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2745 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2746 SRLI->getZExtValue());
2747 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2748 ADDC |= Mask;
2749 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2750 SDValue NewAdd =
2751 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
2752 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2753 CombineTo(N0.getNode(), NewAdd);
2754 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2755 }
2756 }
2757 }
2758 }
2759 }
2760 }
2761
2762
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002763 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002764}
2765
Evan Cheng9568e5c2011-06-21 06:01:08 +00002766/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2767///
2768SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2769 bool DemandHighBits) {
2770 if (!LegalOperations)
2771 return SDValue();
2772
2773 EVT VT = N->getValueType(0);
2774 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2775 return SDValue();
2776 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2777 return SDValue();
2778
2779 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2780 bool LookPassAnd0 = false;
2781 bool LookPassAnd1 = false;
2782 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2783 std::swap(N0, N1);
2784 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2785 std::swap(N0, N1);
2786 if (N0.getOpcode() == ISD::AND) {
2787 if (!N0.getNode()->hasOneUse())
2788 return SDValue();
2789 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2790 if (!N01C || N01C->getZExtValue() != 0xFF00)
2791 return SDValue();
2792 N0 = N0.getOperand(0);
2793 LookPassAnd0 = true;
2794 }
2795
2796 if (N1.getOpcode() == ISD::AND) {
2797 if (!N1.getNode()->hasOneUse())
2798 return SDValue();
2799 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2800 if (!N11C || N11C->getZExtValue() != 0xFF)
2801 return SDValue();
2802 N1 = N1.getOperand(0);
2803 LookPassAnd1 = true;
2804 }
2805
2806 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2807 std::swap(N0, N1);
2808 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2809 return SDValue();
2810 if (!N0.getNode()->hasOneUse() ||
2811 !N1.getNode()->hasOneUse())
2812 return SDValue();
2813
2814 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2815 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2816 if (!N01C || !N11C)
2817 return SDValue();
2818 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2819 return SDValue();
2820
2821 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2822 SDValue N00 = N0->getOperand(0);
2823 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2824 if (!N00.getNode()->hasOneUse())
2825 return SDValue();
2826 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2827 if (!N001C || N001C->getZExtValue() != 0xFF)
2828 return SDValue();
2829 N00 = N00.getOperand(0);
2830 LookPassAnd0 = true;
2831 }
2832
2833 SDValue N10 = N1->getOperand(0);
2834 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2835 if (!N10.getNode()->hasOneUse())
2836 return SDValue();
2837 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2838 if (!N101C || N101C->getZExtValue() != 0xFF00)
2839 return SDValue();
2840 N10 = N10.getOperand(0);
2841 LookPassAnd1 = true;
2842 }
2843
2844 if (N00 != N10)
2845 return SDValue();
2846
2847 // Make sure everything beyond the low halfword is zero since the SRL 16
2848 // will clear the top bits.
2849 unsigned OpSizeInBits = VT.getSizeInBits();
2850 if (DemandHighBits && OpSizeInBits > 16 &&
2851 (!LookPassAnd0 || !LookPassAnd1) &&
2852 !DAG.MaskedValueIsZero(N10, APInt::getHighBitsSet(OpSizeInBits, 16)))
2853 return SDValue();
Eric Christopher7332e6e2011-07-14 01:12:15 +00002854
Evan Cheng9568e5c2011-06-21 06:01:08 +00002855 SDValue Res = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT, N00);
2856 if (OpSizeInBits > 16)
2857 Res = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Res,
2858 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2859 return Res;
2860}
2861
2862/// isBSwapHWordElement - Return true if the specified node is an element
2863/// that makes up a 32-bit packed halfword byteswap. i.e.
2864/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2865static bool isBSwapHWordElement(SDValue N, SmallVector<SDNode*,4> &Parts) {
2866 if (!N.getNode()->hasOneUse())
2867 return false;
2868
2869 unsigned Opc = N.getOpcode();
2870 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
2871 return false;
2872
2873 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2874 if (!N1C)
2875 return false;
2876
2877 unsigned Num;
2878 switch (N1C->getZExtValue()) {
2879 default:
2880 return false;
2881 case 0xFF: Num = 0; break;
2882 case 0xFF00: Num = 1; break;
2883 case 0xFF0000: Num = 2; break;
2884 case 0xFF000000: Num = 3; break;
2885 }
2886
2887 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
2888 SDValue N0 = N.getOperand(0);
2889 if (Opc == ISD::AND) {
2890 if (Num == 0 || Num == 2) {
2891 // (x >> 8) & 0xff
2892 // (x >> 8) & 0xff0000
2893 if (N0.getOpcode() != ISD::SRL)
2894 return false;
2895 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2896 if (!C || C->getZExtValue() != 8)
2897 return false;
2898 } else {
2899 // (x << 8) & 0xff00
2900 // (x << 8) & 0xff000000
2901 if (N0.getOpcode() != ISD::SHL)
2902 return false;
2903 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2904 if (!C || C->getZExtValue() != 8)
2905 return false;
2906 }
2907 } else if (Opc == ISD::SHL) {
2908 // (x & 0xff) << 8
2909 // (x & 0xff0000) << 8
2910 if (Num != 0 && Num != 2)
2911 return false;
2912 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2913 if (!C || C->getZExtValue() != 8)
2914 return false;
2915 } else { // Opc == ISD::SRL
2916 // (x & 0xff00) >> 8
2917 // (x & 0xff000000) >> 8
2918 if (Num != 1 && Num != 3)
2919 return false;
2920 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2921 if (!C || C->getZExtValue() != 8)
2922 return false;
2923 }
2924
2925 if (Parts[Num])
2926 return false;
2927
2928 Parts[Num] = N0.getOperand(0).getNode();
2929 return true;
2930}
2931
2932/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
2933/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2934/// => (rotl (bswap x), 16)
2935SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
2936 if (!LegalOperations)
2937 return SDValue();
2938
2939 EVT VT = N->getValueType(0);
2940 if (VT != MVT::i32)
2941 return SDValue();
2942 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2943 return SDValue();
2944
2945 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
2946 // Look for either
2947 // (or (or (and), (and)), (or (and), (and)))
2948 // (or (or (or (and), (and)), (and)), (and))
2949 if (N0.getOpcode() != ISD::OR)
2950 return SDValue();
2951 SDValue N00 = N0.getOperand(0);
2952 SDValue N01 = N0.getOperand(1);
2953
2954 if (N1.getOpcode() == ISD::OR) {
2955 // (or (or (and), (and)), (or (and), (and)))
2956 SDValue N000 = N00.getOperand(0);
2957 if (!isBSwapHWordElement(N000, Parts))
2958 return SDValue();
2959
2960 SDValue N001 = N00.getOperand(1);
2961 if (!isBSwapHWordElement(N001, Parts))
2962 return SDValue();
2963 SDValue N010 = N01.getOperand(0);
2964 if (!isBSwapHWordElement(N010, Parts))
2965 return SDValue();
2966 SDValue N011 = N01.getOperand(1);
2967 if (!isBSwapHWordElement(N011, Parts))
2968 return SDValue();
2969 } else {
2970 // (or (or (or (and), (and)), (and)), (and))
2971 if (!isBSwapHWordElement(N1, Parts))
2972 return SDValue();
2973 if (!isBSwapHWordElement(N01, Parts))
2974 return SDValue();
2975 if (N00.getOpcode() != ISD::OR)
2976 return SDValue();
2977 SDValue N000 = N00.getOperand(0);
2978 if (!isBSwapHWordElement(N000, Parts))
2979 return SDValue();
2980 SDValue N001 = N00.getOperand(1);
2981 if (!isBSwapHWordElement(N001, Parts))
2982 return SDValue();
2983 }
2984
2985 // Make sure the parts are all coming from the same node.
2986 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
2987 return SDValue();
2988
2989 SDValue BSwap = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT,
2990 SDValue(Parts[0],0));
2991
2992 // Result of the bswap should be rotated by 16. If it's not legal, than
2993 // do (x << 16) | (x >> 16).
2994 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
2995 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
2996 return DAG.getNode(ISD::ROTL, N->getDebugLoc(), VT, BSwap, ShAmt);
2997 else if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
2998 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, BSwap, ShAmt);
2999 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT,
3000 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, BSwap, ShAmt),
3001 DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, BSwap, ShAmt));
3002}
3003
Dan Gohman475871a2008-07-27 21:46:04 +00003004SDValue DAGCombiner::visitOR(SDNode *N) {
3005 SDValue N0 = N->getOperand(0);
3006 SDValue N1 = N->getOperand(1);
3007 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00003008 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3009 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003010 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003011
Dan Gohman7f321562007-06-25 16:23:39 +00003012 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003013 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003014 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003015 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003016 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003017
Dan Gohman613e0d82007-07-03 14:03:57 +00003018 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00003019 if (!LegalOperations &&
3020 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00003021 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3022 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3023 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003024 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003025 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003026 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003027 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003028 if (N0C && !N1C)
Bill Wendling09025642009-01-30 20:59:34 +00003029 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003030 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003031 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003032 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003033 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003034 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003035 return N1;
3036 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003037 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003038 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00003039
3040 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3041 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3042 if (BSwap.getNode() != 0)
3043 return BSwap;
3044 BSwap = MatchBSwapHWordLow(N, N0, N1);
3045 if (BSwap.getNode() != 0)
3046 return BSwap;
3047
Nate Begemancd4d58c2006-02-03 06:46:56 +00003048 // reassociate or
Bill Wendling35247c32009-01-30 00:45:56 +00003049 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003050 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003051 return ROR;
3052 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003053 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00003054 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00003055 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00003056 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00003057 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003058 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3059 DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
3060 N0.getOperand(0), N1),
3061 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00003062 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003063 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3064 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3065 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3066 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003067
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003068 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003069 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00003070 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3071 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00003072 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003073 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling09025642009-01-30 20:59:34 +00003074 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
3075 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003076 AddToWorkList(ORNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00003077 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003078 }
Bill Wendling09025642009-01-30 20:59:34 +00003079 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3080 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00003081 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003082 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling09025642009-01-30 20:59:34 +00003083 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
3084 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003085 AddToWorkList(ANDNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00003086 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003087 }
3088 }
3089 // canonicalize equivalent to ll == rl
3090 if (LL == RR && LR == RL) {
3091 Op1 = ISD::getSetCCSwappedOperands(Op1);
3092 std::swap(RL, RR);
3093 }
3094 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003095 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003096 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00003097 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003098 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling09025642009-01-30 20:59:34 +00003099 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
3100 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003101 }
3102 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003103
Bill Wendling09025642009-01-30 20:59:34 +00003104 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00003105 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003106 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003107 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003108 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003109
Bill Wendling09025642009-01-30 20:59:34 +00003110 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00003111 if (N0.getOpcode() == ISD::AND &&
3112 N1.getOpcode() == ISD::AND &&
3113 N0.getOperand(1).getOpcode() == ISD::Constant &&
3114 N1.getOperand(1).getOpcode() == ISD::Constant &&
3115 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003116 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00003117 // We can only do this xform if we know that bits from X that are set in C2
3118 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003119 const APInt &LHSMask =
3120 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3121 const APInt &RHSMask =
3122 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003123
Dan Gohmanea859be2007-06-22 14:59:07 +00003124 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3125 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling09025642009-01-30 20:59:34 +00003126 SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
3127 N0.getOperand(0), N1.getOperand(0));
3128 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X,
3129 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00003130 }
3131 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003132
Chris Lattner516b9622006-09-14 20:50:57 +00003133 // See if this is some rotate idiom.
Bill Wendling317bd702009-01-30 21:14:50 +00003134 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman475871a2008-07-27 21:46:04 +00003135 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00003136
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003137 // Simplify the operands using demanded-bits information.
3138 if (!VT.isVector() &&
3139 SimplifyDemandedBits(SDValue(N, 0)))
3140 return SDValue(N, 0);
3141
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003142 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003143}
3144
Chris Lattner516b9622006-09-14 20:50:57 +00003145/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003146static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00003147 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00003148 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00003149 Mask = Op.getOperand(1);
3150 Op = Op.getOperand(0);
3151 } else {
3152 return false;
3153 }
3154 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003155
Chris Lattner516b9622006-09-14 20:50:57 +00003156 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3157 Shift = Op;
3158 return true;
3159 }
Bill Wendling09025642009-01-30 20:59:34 +00003160
Scott Michelfdc40a02009-02-17 22:15:04 +00003161 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00003162}
3163
Chris Lattner516b9622006-09-14 20:50:57 +00003164// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3165// idioms for rotate, and if the target supports rotation instructions, generate
3166// a rot[lr].
Bill Wendling317bd702009-01-30 21:14:50 +00003167SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003168 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00003169 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00003170 if (!TLI.isTypeLegal(VT)) return 0;
3171
3172 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003173 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3174 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00003175 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003176
Chris Lattner516b9622006-09-14 20:50:57 +00003177 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003178 SDValue LHSShift; // The shift.
3179 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003180 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3181 return 0; // Not part of a rotate.
3182
Dan Gohman475871a2008-07-27 21:46:04 +00003183 SDValue RHSShift; // The shift.
3184 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003185 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3186 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00003187
Chris Lattner516b9622006-09-14 20:50:57 +00003188 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3189 return 0; // Not shifting the same value.
3190
3191 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3192 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00003193
Chris Lattner516b9622006-09-14 20:50:57 +00003194 // Canonicalize shl to left side in a shl/srl pair.
3195 if (RHSShift.getOpcode() == ISD::SHL) {
3196 std::swap(LHS, RHS);
3197 std::swap(LHSShift, RHSShift);
3198 std::swap(LHSMask , RHSMask );
3199 }
3200
Duncan Sands83ec4b62008-06-06 12:08:01 +00003201 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003202 SDValue LHSShiftArg = LHSShift.getOperand(0);
3203 SDValue LHSShiftAmt = LHSShift.getOperand(1);
3204 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003205
3206 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3207 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003208 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3209 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003210 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3211 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003212 if ((LShVal + RShVal) != OpSizeInBits)
3213 return 0;
3214
Dan Gohman475871a2008-07-27 21:46:04 +00003215 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00003216 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00003217 Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00003218 else
Bill Wendling317bd702009-01-30 21:14:50 +00003219 Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003220
Chris Lattner516b9622006-09-14 20:50:57 +00003221 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003222 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003223 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003224
Gabor Greifba36cb52008-08-28 21:40:38 +00003225 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003226 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3227 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003228 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003229 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003230 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3231 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003232 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003233
Bill Wendling317bd702009-01-30 21:14:50 +00003234 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003235 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003236
Gabor Greifba36cb52008-08-28 21:40:38 +00003237 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003238 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003239
Chris Lattner516b9622006-09-14 20:50:57 +00003240 // If there is a mask here, and we have a variable shift, we can't be sure
3241 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003242 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003243 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003244
Chris Lattner516b9622006-09-14 20:50:57 +00003245 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
3246 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003247 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
3248 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003249 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003250 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003251 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00003252 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00003253 return DAG.getNode(ISD::ROTL, DL, VT,
3254 LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003255 else
Bill Wendling317bd702009-01-30 21:14:50 +00003256 return DAG.getNode(ISD::ROTR, DL, VT,
3257 LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003258 }
Chris Lattner516b9622006-09-14 20:50:57 +00003259 }
3260 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003261
Chris Lattner516b9622006-09-14 20:50:57 +00003262 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
3263 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003264 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
3265 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003266 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003267 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003268 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00003269 if (HasROTR)
Bill Wendling317bd702009-01-30 21:14:50 +00003270 return DAG.getNode(ISD::ROTR, DL, VT,
3271 LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00003272 else
Bill Wendling317bd702009-01-30 21:14:50 +00003273 return DAG.getNode(ISD::ROTL, DL, VT,
3274 LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003275 }
Scott Michelc9dc1142007-04-02 21:36:32 +00003276 }
3277 }
3278
Dan Gohman74feef22008-10-17 01:23:35 +00003279 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00003280 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
3281 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00003282 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
3283 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00003284 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
3285 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00003286 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
3287 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003288 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
3289 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00003290 if (RExtOp0.getOpcode() == ISD::SUB &&
3291 RExtOp0.getOperand(1) == LExtOp0) {
3292 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003293 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00003294 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003295 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003296 if (ConstantSDNode *SUBC =
3297 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003298 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003299 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3300 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00003301 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003302 }
3303 }
3304 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3305 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003306 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003307 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00003308 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003309 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003310 if (ConstantSDNode *SUBC =
3311 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003312 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003313 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
3314 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00003315 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003316 }
3317 }
Chris Lattner516b9622006-09-14 20:50:57 +00003318 }
3319 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003320
Chris Lattner516b9622006-09-14 20:50:57 +00003321 return 0;
3322}
3323
Dan Gohman475871a2008-07-27 21:46:04 +00003324SDValue DAGCombiner::visitXOR(SDNode *N) {
3325 SDValue N0 = N->getOperand(0);
3326 SDValue N1 = N->getOperand(1);
3327 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003328 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3329 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003330 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003331
Dan Gohman7f321562007-06-25 16:23:39 +00003332 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003333 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003334 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003335 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003336 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003337
Evan Cheng26471c42008-03-25 20:08:07 +00003338 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3339 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3340 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003341 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003342 if (N0.getOpcode() == ISD::UNDEF)
3343 return N0;
3344 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003345 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003346 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003347 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003348 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003349 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003350 if (N0C && !N1C)
Bill Wendling317bd702009-01-30 21:14:50 +00003351 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003352 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003353 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003354 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003355 // reassociate xor
Bill Wendling35247c32009-01-30 00:45:56 +00003356 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003357 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003358 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003359
Nate Begeman1d4d4142005-09-01 00:19:25 +00003360 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003361 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003362 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003363 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3364 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003365
Duncan Sands25cf2272008-11-24 14:53:14 +00003366 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003367 switch (N0.getOpcode()) {
3368 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003369 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003370 case ISD::SETCC:
Bill Wendling317bd702009-01-30 21:14:50 +00003371 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003372 case ISD::SELECT_CC:
Bill Wendling317bd702009-01-30 21:14:50 +00003373 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003374 N0.getOperand(3), NotCC);
3375 }
3376 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003377 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003378
Chris Lattner61c5ff42007-09-10 21:39:07 +00003379 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003380 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003381 N0.getNode()->hasOneUse() &&
3382 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003383 SDValue V = N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003384 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003385 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003386 AddToWorkList(V.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003387 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003388 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003389
Bill Wendling317bd702009-01-30 21:14:50 +00003390 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003391 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003392 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003393 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003394 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3395 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00003396 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
3397 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003398 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003399 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003400 }
3401 }
Bill Wendling317bd702009-01-30 21:14:50 +00003402 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003403 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003404 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003405 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003406 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3407 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00003408 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
3409 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003410 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003411 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003412 }
3413 }
Bill Wendling317bd702009-01-30 21:14:50 +00003414 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003415 if (N1C && N0.getOpcode() == ISD::XOR) {
3416 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3417 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3418 if (N00C)
Bill Wendling317bd702009-01-30 21:14:50 +00003419 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
3420 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003421 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003422 if (N01C)
Bill Wendling317bd702009-01-30 21:14:50 +00003423 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
3424 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003425 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003426 }
3427 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003428 if (N0 == N1)
3429 return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations);
Scott Michelfdc40a02009-02-17 22:15:04 +00003430
Chris Lattner35e5c142006-05-05 05:51:50 +00003431 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3432 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003433 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003434 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003435 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003436
Chris Lattner3e104b12006-04-08 04:15:24 +00003437 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003438 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003439 SimplifyDemandedBits(SDValue(N, 0)))
3440 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003441
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003442 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003443}
3444
Chris Lattnere70da202007-12-06 07:33:36 +00003445/// visitShiftByConstant - Handle transforms common to the three shifts, when
3446/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003447SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003448 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003449 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003450
Chris Lattnere70da202007-12-06 07:33:36 +00003451 // We want to pull some binops through shifts, so that we have (and (shift))
3452 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3453 // thing happens with address calculations, so it's important to canonicalize
3454 // it.
3455 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003456
Chris Lattnere70da202007-12-06 07:33:36 +00003457 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003458 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003459 case ISD::OR:
3460 case ISD::XOR:
3461 HighBitSet = false; // We can only transform sra if the high bit is clear.
3462 break;
3463 case ISD::AND:
3464 HighBitSet = true; // We can only transform sra if the high bit is set.
3465 break;
3466 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003467 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003468 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003469 HighBitSet = false; // We can only transform sra if the high bit is clear.
3470 break;
3471 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003472
Chris Lattnere70da202007-12-06 07:33:36 +00003473 // We require the RHS of the binop to be a constant as well.
3474 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003475 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003476
3477 // FIXME: disable this unless the input to the binop is a shift by a constant.
3478 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003479 //
Bill Wendling88103372009-01-30 21:37:17 +00003480 // void foo(int *X, int i) { X[i & 1235] = 1; }
3481 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003482 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003483 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003484 BinOpLHSVal->getOpcode() != ISD::SRA &&
3485 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3486 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003487 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003488
Owen Andersone50ed302009-08-10 22:56:29 +00003489 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003490
Bill Wendling88103372009-01-30 21:37:17 +00003491 // If this is a signed shift right, and the high bit is modified by the
3492 // logical operation, do not perform the transformation. The highBitSet
3493 // boolean indicates the value of the high bit of the constant which would
3494 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003495 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003496 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3497 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003498 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003499 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003500
Chris Lattnere70da202007-12-06 07:33:36 +00003501 // Fold the constants, shifting the binop RHS by the shift amount.
Bill Wendling88103372009-01-30 21:37:17 +00003502 SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(),
3503 N->getValueType(0),
3504 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003505
3506 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003507 SDValue NewShift = DAG.getNode(N->getOpcode(),
3508 LHS->getOperand(0).getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003509 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003510
3511 // Create the new binop.
Bill Wendling88103372009-01-30 21:37:17 +00003512 return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003513}
3514
Dan Gohman475871a2008-07-27 21:46:04 +00003515SDValue DAGCombiner::visitSHL(SDNode *N) {
3516 SDValue N0 = N->getOperand(0);
3517 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003518 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3519 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003520 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003521 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003522
Nate Begeman1d4d4142005-09-01 00:19:25 +00003523 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003524 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003525 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003526 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003527 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003528 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003529 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003530 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003531 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003532 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003533 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003534 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003535 // fold (shl undef, x) -> 0
3536 if (N0.getOpcode() == ISD::UNDEF)
3537 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003538 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003539 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003540 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003541 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003542 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003543 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003544 N1.getOperand(0).getOpcode() == ISD::AND &&
3545 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003546 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003547 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003548 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003549 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003550 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003551 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003552 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003553 DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT,
3554 DAG.getNode(ISD::TRUNCATE,
3555 N->getDebugLoc(),
3556 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003557 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003558 }
3559 }
3560
Dan Gohman475871a2008-07-27 21:46:04 +00003561 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3562 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003563
3564 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003565 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003566 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003567 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3568 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003569 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003570 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00003571 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003572 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003573 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003574
3575 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3576 // For this to be valid, the second form must not preserve any of the bits
3577 // that are shifted out by the inner shift in the first form. This means
3578 // the outer shift size must be >= the number of bits added by the ext.
3579 // As a corollary, we don't care what kind of ext it is.
3580 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3581 N0.getOpcode() == ISD::ANY_EXTEND ||
3582 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3583 N0.getOperand(0).getOpcode() == ISD::SHL &&
3584 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003585 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003586 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3587 uint64_t c2 = N1C->getZExtValue();
3588 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3589 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3590 if (c2 >= OpSizeInBits - InnerShiftSize) {
3591 if (c1 + c2 >= OpSizeInBits)
3592 return DAG.getConstant(0, VT);
3593 return DAG.getNode(ISD::SHL, N0->getDebugLoc(), VT,
3594 DAG.getNode(N0.getOpcode(), N0->getDebugLoc(), VT,
3595 N0.getOperand(0)->getOperand(0)),
3596 DAG.getConstant(c1 + c2, N1.getValueType()));
3597 }
3598 }
3599
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003600 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3601 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003602 // Only fold this if the inner shift has no other uses -- if it does, folding
3603 // this will increase the total number of instructions.
3604 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003605 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003606 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003607 if (c1 < VT.getSizeInBits()) {
3608 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003609 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3610 VT.getSizeInBits() - c1);
3611 SDValue Shift;
3612 if (c2 > c1) {
3613 Mask = Mask.shl(c2-c1);
3614 Shift = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
3615 DAG.getConstant(c2-c1, N1.getValueType()));
3616 } else {
3617 Mask = Mask.lshr(c1-c2);
3618 Shift = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
3619 DAG.getConstant(c1-c2, N1.getValueType()));
3620 }
3621 return DAG.getNode(ISD::AND, N0.getDebugLoc(), VT, Shift,
3622 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003623 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003624 }
Bill Wendling88103372009-01-30 21:37:17 +00003625 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003626 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3627 SDValue HiBitsMask =
3628 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3629 VT.getSizeInBits() -
3630 N1C->getZExtValue()),
3631 VT);
Bill Wendling88103372009-01-30 21:37:17 +00003632 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003633 HiBitsMask);
3634 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003635
Evan Chenge5b51ac2010-04-17 06:13:15 +00003636 if (N1C) {
3637 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3638 if (NewSHL.getNode())
3639 return NewSHL;
3640 }
3641
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003642 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003643}
3644
Dan Gohman475871a2008-07-27 21:46:04 +00003645SDValue DAGCombiner::visitSRA(SDNode *N) {
3646 SDValue N0 = N->getOperand(0);
3647 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003648 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3649 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003650 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003651 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003652
Bill Wendling88103372009-01-30 21:37:17 +00003653 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003654 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003655 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003656 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003657 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003658 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003659 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003660 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003661 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003662 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003663 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003664 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003665 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003666 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003667 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003668 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3669 // sext_inreg.
3670 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003671 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003672 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3673 if (VT.isVector())
3674 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3675 ExtVT, VT.getVectorNumElements());
3676 if ((!LegalOperations ||
3677 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Bill Wendling88103372009-01-30 21:37:17 +00003678 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003679 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003680 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003681
Bill Wendling88103372009-01-30 21:37:17 +00003682 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003683 if (N1C && N0.getOpcode() == ISD::SRA) {
3684 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003685 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003686 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Bill Wendling88103372009-01-30 21:37:17 +00003687 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003688 DAG.getConstant(Sum, N1C->getValueType(0)));
3689 }
3690 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003691
Bill Wendling88103372009-01-30 21:37:17 +00003692 // fold (sra (shl X, m), (sub result_size, n))
3693 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003694 // result_size - n != m.
3695 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003696 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003697 if (N0.getOpcode() == ISD::SHL) {
3698 // Get the two constanst of the shifts, CN0 = m, CN = n.
3699 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3700 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003701 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003702 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003703 EVT::getIntegerVT(*DAG.getContext(),
3704 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003705 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003706 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003707
Scott Michelfdc40a02009-02-17 22:15:04 +00003708 // If the shift is not a no-op (in which case this should be just a sign
3709 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003710 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003711 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003712 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003713 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3714 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003715 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003716
Owen Anderson95771af2011-02-25 21:41:48 +00003717 SDValue Amt = DAG.getConstant(ShiftAmt,
3718 getShiftAmountTy(N0.getOperand(0).getValueType()));
Bill Wendling88103372009-01-30 21:37:17 +00003719 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT,
3720 N0.getOperand(0), Amt);
3721 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT,
3722 Shift);
3723 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(),
3724 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003725 }
3726 }
3727 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003728
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003729 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003730 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003731 N1.getOperand(0).getOpcode() == ISD::AND &&
3732 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003733 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003734 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003735 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003736 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003737 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003738 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003739 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003740 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003741 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003742 DAG.getNode(ISD::TRUNCATE,
3743 N->getDebugLoc(),
3744 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003745 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003746 }
3747 }
3748
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003749 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3750 // if c1 is equal to the number of bits the trunc removes
3751 if (N0.getOpcode() == ISD::TRUNCATE &&
3752 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3753 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3754 N0.getOperand(0).hasOneUse() &&
3755 N0.getOperand(0).getOperand(1).hasOneUse() &&
3756 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3757 EVT LargeVT = N0.getOperand(0).getValueType();
3758 ConstantSDNode *LargeShiftAmt =
3759 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3760
3761 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3762 LargeShiftAmt->getZExtValue()) {
3763 SDValue Amt =
3764 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003765 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003766 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), LargeVT,
3767 N0.getOperand(0).getOperand(0), Amt);
3768 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, SRA);
3769 }
3770 }
3771
Scott Michelfdc40a02009-02-17 22:15:04 +00003772 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003773 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3774 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003775
3776
Nate Begeman1d4d4142005-09-01 00:19:25 +00003777 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003778 if (DAG.SignBitIsZero(N0))
Bill Wendling88103372009-01-30 21:37:17 +00003779 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003780
Evan Chenge5b51ac2010-04-17 06:13:15 +00003781 if (N1C) {
3782 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3783 if (NewSRA.getNode())
3784 return NewSRA;
3785 }
3786
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003787 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003788}
3789
Dan Gohman475871a2008-07-27 21:46:04 +00003790SDValue DAGCombiner::visitSRL(SDNode *N) {
3791 SDValue N0 = N->getOperand(0);
3792 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003793 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3794 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003795 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003796 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003797
Nate Begeman1d4d4142005-09-01 00:19:25 +00003798 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003799 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003800 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003801 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003802 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003803 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003804 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003805 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003806 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003807 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003808 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003809 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003810 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003811 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003812 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003813 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003814
Bill Wendling88103372009-01-30 21:37:17 +00003815 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003816 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003817 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003818 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3819 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003820 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003821 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00003822 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003823 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003824 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003825
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003826 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003827 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
3828 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003829 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003830 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003831 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3832 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003833 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3834 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003835 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003836 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003837 if (c1 + OpSizeInBits == InnerShiftSize) {
3838 if (c1 + c2 >= InnerShiftSize)
3839 return DAG.getConstant(0, VT);
3840 return DAG.getNode(ISD::TRUNCATE, N0->getDebugLoc(), VT,
Owen Anderson95771af2011-02-25 21:41:48 +00003841 DAG.getNode(ISD::SRL, N0->getDebugLoc(), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003842 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003843 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003844 }
3845 }
3846
Chris Lattnerefcddc32010-04-15 05:28:43 +00003847 // fold (srl (shl x, c), c) -> (and x, cst2)
3848 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
3849 N0.getValueSizeInBits() <= 64) {
3850 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
3851 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
3852 DAG.getConstant(~0ULL >> ShAmt, VT));
3853 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003854
Scott Michelfdc40a02009-02-17 22:15:04 +00003855
Chris Lattner06afe072006-05-05 22:53:17 +00003856 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
3857 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
3858 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00003859 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003860 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00003861 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00003862
Evan Chenge5b51ac2010-04-17 06:13:15 +00003863 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00003864 uint64_t ShiftAmt = N1C->getZExtValue();
Evan Chenge5b51ac2010-04-17 06:13:15 +00003865 SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00003866 N0.getOperand(0),
3867 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00003868 AddToWorkList(SmallShift.getNode());
3869 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift);
3870 }
Chris Lattner06afe072006-05-05 22:53:17 +00003871 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003872
Chris Lattner3657ffe2006-10-12 20:23:19 +00003873 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
3874 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00003875 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00003876 if (N0.getOpcode() == ISD::SRA)
Bill Wendling88103372009-01-30 21:37:17 +00003877 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00003878 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003879
Chris Lattner350bec02006-04-02 06:11:11 +00003880 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00003881 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003882 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00003883 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003884 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00003885
Chris Lattner350bec02006-04-02 06:11:11 +00003886 // If any of the input bits are KnownOne, then the input couldn't be all
3887 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003888 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003889
Chris Lattner350bec02006-04-02 06:11:11 +00003890 // If all of the bits input the to ctlz node are known to be zero, then
3891 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003892 APInt UnknownBits = ~KnownZero;
Chris Lattner350bec02006-04-02 06:11:11 +00003893 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003894
Chris Lattner350bec02006-04-02 06:11:11 +00003895 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00003896 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00003897 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00003898 // could be set on input to the CTLZ node. If this bit is set, the SRL
3899 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
3900 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003901 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00003902 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00003903
Chris Lattner350bec02006-04-02 06:11:11 +00003904 if (ShAmt) {
Bill Wendling88103372009-01-30 21:37:17 +00003905 Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00003906 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00003907 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00003908 }
Bill Wendling88103372009-01-30 21:37:17 +00003909
3910 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
3911 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00003912 }
3913 }
Evan Chengeb9f8922008-08-30 02:03:58 +00003914
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003915 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003916 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003917 N1.getOperand(0).getOpcode() == ISD::AND &&
3918 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003919 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003920 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003921 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003922 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003923 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003924 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003925 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003926 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003927 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003928 DAG.getNode(ISD::TRUNCATE,
3929 N->getDebugLoc(),
3930 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003931 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003932 }
3933 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003934
Chris Lattner61a4c072007-04-18 03:06:49 +00003935 // fold operands of srl based on knowledge that the low bits are not
3936 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003937 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3938 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003939
Evan Cheng9ab2b982009-12-18 21:31:31 +00003940 if (N1C) {
3941 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
3942 if (NewSRL.getNode())
3943 return NewSRL;
3944 }
3945
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003946 // Attempt to convert a srl of a load into a narrower zero-extending load.
3947 SDValue NarrowLoad = ReduceLoadWidth(N);
3948 if (NarrowLoad.getNode())
3949 return NarrowLoad;
3950
Evan Cheng9ab2b982009-12-18 21:31:31 +00003951 // Here is a common situation. We want to optimize:
3952 //
3953 // %a = ...
3954 // %b = and i32 %a, 2
3955 // %c = srl i32 %b, 1
3956 // brcond i32 %c ...
3957 //
3958 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003959 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00003960 // %a = ...
3961 // %b = and %a, 2
3962 // %c = setcc eq %b, 0
3963 // brcond %c ...
3964 //
3965 // However when after the source operand of SRL is optimized into AND, the SRL
3966 // itself may not be optimized further. Look for it and add the BRCOND into
3967 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00003968 if (N->hasOneUse()) {
3969 SDNode *Use = *N->use_begin();
3970 if (Use->getOpcode() == ISD::BRCOND)
3971 AddToWorkList(Use);
3972 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
3973 // Also look pass the truncate.
3974 Use = *Use->use_begin();
3975 if (Use->getOpcode() == ISD::BRCOND)
3976 AddToWorkList(Use);
3977 }
3978 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00003979
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003980 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00003981}
3982
Dan Gohman475871a2008-07-27 21:46:04 +00003983SDValue DAGCombiner::visitCTLZ(SDNode *N) {
3984 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003985 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003986
3987 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003988 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003989 return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003990 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003991}
3992
Chandler Carruth63974b22011-12-13 01:56:10 +00003993SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
3994 SDValue N0 = N->getOperand(0);
3995 EVT VT = N->getValueType(0);
3996
3997 // fold (ctlz_zero_undef c1) -> c2
3998 if (isa<ConstantSDNode>(N0))
3999 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0);
4000 return SDValue();
4001}
4002
Dan Gohman475871a2008-07-27 21:46:04 +00004003SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4004 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004005 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004006
Nate Begeman1d4d4142005-09-01 00:19:25 +00004007 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004008 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00004009 return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004010 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004011}
4012
Chandler Carruth63974b22011-12-13 01:56:10 +00004013SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4014 SDValue N0 = N->getOperand(0);
4015 EVT VT = N->getValueType(0);
4016
4017 // fold (cttz_zero_undef c1) -> c2
4018 if (isa<ConstantSDNode>(N0))
4019 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0);
4020 return SDValue();
4021}
4022
Dan Gohman475871a2008-07-27 21:46:04 +00004023SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4024 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004025 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004026
Nate Begeman1d4d4142005-09-01 00:19:25 +00004027 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004028 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00004029 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004030 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004031}
4032
Dan Gohman475871a2008-07-27 21:46:04 +00004033SDValue DAGCombiner::visitSELECT(SDNode *N) {
4034 SDValue N0 = N->getOperand(0);
4035 SDValue N1 = N->getOperand(1);
4036 SDValue N2 = N->getOperand(2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004037 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4038 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4039 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004040 EVT VT = N->getValueType(0);
4041 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00004042
Bill Wendling34584e62009-01-30 22:02:18 +00004043 // fold (select C, X, X) -> X
Nate Begeman452d7beb2005-09-16 00:54:12 +00004044 if (N1 == N2)
4045 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004046 // fold (select true, X, Y) -> X
Nate Begeman452d7beb2005-09-16 00:54:12 +00004047 if (N0C && !N0C->isNullValue())
4048 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004049 // fold (select false, X, Y) -> Y
Nate Begeman452d7beb2005-09-16 00:54:12 +00004050 if (N0C && N0C->isNullValue())
4051 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00004052 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004053 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Bill Wendling34584e62009-01-30 22:02:18 +00004054 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
4055 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00004056 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004057 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00004058 (VT0.isInteger() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00004059 TLI.getBooleanContents(false) == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004060 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004061 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00004062 if (VT == VT0)
Bill Wendling34584e62009-01-30 22:02:18 +00004063 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0,
4064 N0, DAG.getConstant(1, VT0));
4065 XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0,
4066 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004067 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004068 if (VT.bitsGT(VT0))
Bill Wendling34584e62009-01-30 22:02:18 +00004069 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode);
4070 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00004071 }
Bill Wendling34584e62009-01-30 22:02:18 +00004072 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004073 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bill Wendling7581bfa2009-01-30 23:03:19 +00004074 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004075 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00004076 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004077 }
Bill Wendling34584e62009-01-30 22:02:18 +00004078 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004079 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004080 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004081 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00004082 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004083 }
Bill Wendling34584e62009-01-30 22:02:18 +00004084 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004085 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Bill Wendling34584e62009-01-30 22:02:18 +00004086 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
4087 // fold (select X, X, Y) -> (or X, Y)
4088 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004089 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Bill Wendling34584e62009-01-30 22:02:18 +00004090 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
4091 // fold (select X, Y, X) -> (and X, Y)
4092 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004093 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Bill Wendling34584e62009-01-30 22:02:18 +00004094 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004095
Chris Lattner40c62d52005-10-18 06:04:22 +00004096 // If we can fold this based on the true/false value, do so.
4097 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00004098 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004099
Nate Begeman44728a72005-09-19 22:34:01 +00004100 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004101 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004102 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00004103 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00004104 // having to say they don't support SELECT_CC on every type the DAG knows
4105 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00004106 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00004107 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Bill Wendling34584e62009-01-30 22:02:18 +00004108 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT,
4109 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004110 N1, N2, N0.getOperand(2));
Chris Lattner600fec32009-03-11 05:08:08 +00004111 return SimplifySelect(N->getDebugLoc(), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004112 }
Bill Wendling34584e62009-01-30 22:02:18 +00004113
Dan Gohman475871a2008-07-27 21:46:04 +00004114 return SDValue();
Nate Begeman452d7beb2005-09-16 00:54:12 +00004115}
4116
Dan Gohman475871a2008-07-27 21:46:04 +00004117SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4118 SDValue N0 = N->getOperand(0);
4119 SDValue N1 = N->getOperand(1);
4120 SDValue N2 = N->getOperand(2);
4121 SDValue N3 = N->getOperand(3);
4122 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00004123 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004124
Nate Begeman44728a72005-09-19 22:34:01 +00004125 // fold select_cc lhs, rhs, x, x, cc -> x
4126 if (N2 == N3)
4127 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004128
Chris Lattner5f42a242006-09-20 06:19:26 +00004129 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00004130 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004131 N0, N1, CC, N->getDebugLoc(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004132 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00004133
Gabor Greifba36cb52008-08-28 21:40:38 +00004134 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00004135 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00004136 return N2; // cond always true -> true val
4137 else
4138 return N3; // cond always false -> false val
4139 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004140
Chris Lattner5f42a242006-09-20 06:19:26 +00004141 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00004142 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Scott Michelfdc40a02009-02-17 22:15:04 +00004143 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(),
4144 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
Chris Lattner5f42a242006-09-20 06:19:26 +00004145 SCC.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004146
Chris Lattner40c62d52005-10-18 06:04:22 +00004147 // If we can fold this based on the true/false value, do so.
4148 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00004149 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00004150
Nate Begeman44728a72005-09-19 22:34:01 +00004151 // fold select_cc into other things, such as min/max/abs
Bill Wendling836ca7d2009-01-30 23:59:18 +00004152 return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004153}
4154
Dan Gohman475871a2008-07-27 21:46:04 +00004155SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7beb2005-09-16 00:54:12 +00004156 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004157 cast<CondCodeSDNode>(N->getOperand(2))->get(),
4158 N->getDebugLoc());
Nate Begeman452d7beb2005-09-16 00:54:12 +00004159}
4160
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004161// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00004162// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004163// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00004164// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00004165static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004166 unsigned ExtOpc,
4167 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00004168 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004169 bool HasCopyToRegUses = false;
4170 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00004171 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4172 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004173 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004174 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004175 if (User == N)
4176 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00004177 if (UI.getUse().getResNo() != N0.getResNo())
4178 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004179 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00004180 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004181 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4182 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4183 // Sign bits will be lost after a zext.
4184 return false;
4185 bool Add = false;
4186 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004187 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004188 if (UseOp == N0)
4189 continue;
4190 if (!isa<ConstantSDNode>(UseOp))
4191 return false;
4192 Add = true;
4193 }
4194 if (Add)
4195 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004196 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004197 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004198 // If truncates aren't free and there are users we can't
4199 // extend, it isn't worthwhile.
4200 if (!isTruncFree)
4201 return false;
4202 // Remember if this value is live-out.
4203 if (User->getOpcode() == ISD::CopyToReg)
4204 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004205 }
4206
4207 if (HasCopyToRegUses) {
4208 bool BothLiveOut = false;
4209 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4210 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004211 SDUse &Use = UI.getUse();
4212 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4213 BothLiveOut = true;
4214 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004215 }
4216 }
4217 if (BothLiveOut)
4218 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004219 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004220 return ExtendNodes.size();
4221 }
4222 return true;
4223}
4224
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004225void DAGCombiner::ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
4226 SDValue Trunc, SDValue ExtLoad, DebugLoc DL,
4227 ISD::NodeType ExtType) {
4228 // Extend SetCC uses if necessary.
4229 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4230 SDNode *SetCC = SetCCs[i];
4231 SmallVector<SDValue, 4> Ops;
4232
4233 for (unsigned j = 0; j != 2; ++j) {
4234 SDValue SOp = SetCC->getOperand(j);
4235 if (SOp == Trunc)
4236 Ops.push_back(ExtLoad);
4237 else
4238 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4239 }
4240
4241 Ops.push_back(SetCC->getOperand(2));
4242 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4243 &Ops[0], Ops.size()));
4244 }
4245}
4246
Dan Gohman475871a2008-07-27 21:46:04 +00004247SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4248 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004249 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004250
Nate Begeman1d4d4142005-09-01 00:19:25 +00004251 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004252 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004253 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004254
Nate Begeman1d4d4142005-09-01 00:19:25 +00004255 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004256 // fold (sext (aext x)) -> (sext x)
4257 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004258 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT,
4259 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004260
Chris Lattner22558872007-02-26 03:13:59 +00004261 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004262 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4263 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004264 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4265 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004266 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4267 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004268 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004269 // CombineTo deleted the truncate, if needed, but not what's under it.
4270 AddToWorkList(oye);
4271 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004272 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004273 }
Evan Chengc88138f2007-03-22 01:54:19 +00004274
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004275 // See if the value being truncated is already sign extended. If so, just
4276 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004277 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004278 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4279 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4280 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004281 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004282
Chris Lattner22558872007-02-26 03:13:59 +00004283 if (OpBits == DestBits) {
4284 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4285 // bits, it is already ready.
4286 if (NumSignBits > DestBits-MidBits)
4287 return Op;
4288 } else if (OpBits < DestBits) {
4289 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4290 // bits, just sext from i32.
4291 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004292 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004293 } else {
4294 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4295 // bits, just truncate to i32.
4296 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004297 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004298 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004299
Chris Lattner22558872007-02-26 03:13:59 +00004300 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004301 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4302 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004303 if (OpBits < DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004304 Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004305 else if (OpBits > DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004306 Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op);
4307 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004308 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004309 }
Chris Lattner6007b842006-09-21 06:00:20 +00004310 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004311
Evan Cheng110dec22005-12-14 02:19:23 +00004312 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004313 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004314 // on vectors in one instruction. We only perform this transformation on
4315 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004316 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004317 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004318 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004319 bool DoXform = true;
4320 SmallVector<SDNode*, 4> SetCCs;
4321 if (!N0.hasOneUse())
4322 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4323 if (DoXform) {
4324 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004325 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004326 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004327 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004328 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004329 LN0->isVolatile(), LN0->isNonTemporal(),
4330 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004331 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00004332 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4333 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004334 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004335 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4336 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004337 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004338 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004339 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004340
4341 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4342 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004343 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4344 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004345 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004346 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004347 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004348 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004349 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004350 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004351 LN0->getBasePtr(), LN0->getPointerInfo(),
4352 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004353 LN0->isVolatile(), LN0->isNonTemporal(),
4354 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004355 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004356 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004357 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4358 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004359 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004360 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004361 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004362 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004363
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004364 // fold (sext (and/or/xor (load x), cst)) ->
4365 // (and/or/xor (sextload x), (sext cst))
4366 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4367 N0.getOpcode() == ISD::XOR) &&
4368 isa<LoadSDNode>(N0.getOperand(0)) &&
4369 N0.getOperand(1).getOpcode() == ISD::Constant &&
4370 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4371 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4372 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4373 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4374 bool DoXform = true;
4375 SmallVector<SDNode*, 4> SetCCs;
4376 if (!N0.hasOneUse())
4377 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4378 SetCCs, TLI);
4379 if (DoXform) {
4380 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, LN0->getDebugLoc(), VT,
4381 LN0->getChain(), LN0->getBasePtr(),
4382 LN0->getPointerInfo(),
4383 LN0->getMemoryVT(),
4384 LN0->isVolatile(),
4385 LN0->isNonTemporal(),
4386 LN0->getAlignment());
4387 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4388 Mask = Mask.sext(VT.getSizeInBits());
4389 SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4390 ExtLoad, DAG.getConstant(Mask, VT));
4391 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
4392 N0.getOperand(0).getDebugLoc(),
4393 N0.getOperand(0).getValueType(), ExtLoad);
4394 CombineTo(N, And);
4395 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
4396 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4397 ISD::SIGN_EXTEND);
4398 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4399 }
4400 }
4401 }
4402
Chris Lattner20a35c32007-04-11 05:32:27 +00004403 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004404 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004405 // Only do this before legalize for now.
4406 if (VT.isVector() && !LegalOperations) {
4407 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem2e506192012-04-11 08:26:11 +00004408 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4409 // of the same size as the compared operands. Only optimize sext(setcc())
4410 // if this is the case.
4411 EVT SVT = TLI.getSetCCResultType(N0VT);
4412
4413 // We know that the # elements of the results is the same as the
4414 // # elements of the compare (and the # elements of the compare result
4415 // for that matter). Check to see that they are the same size. If so,
4416 // we know that the element size of the sext'd result matches the
4417 // element size of the compare operands.
4418 if (VT.getSizeInBits() == SVT.getSizeInBits())
Duncan Sands28b77e92011-09-06 19:07:46 +00004419 return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004420 N0.getOperand(1),
4421 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Dan Gohman3ce89f42010-04-30 17:19:19 +00004422 // If the desired elements are smaller or larger than the source
4423 // elements we can use a matching integer vector type and then
4424 // truncate/sign extend
4425 else {
Duncan Sands34727662010-07-12 08:16:59 +00004426 EVT MatchingElementType =
4427 EVT::getIntegerVT(*DAG.getContext(),
4428 N0VT.getScalarType().getSizeInBits());
4429 EVT MatchingVectorType =
4430 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4431 N0VT.getVectorNumElements());
Nadav Rotem2e506192012-04-11 08:26:11 +00004432
4433 if (SVT == MatchingVectorType) {
4434 SDValue VsetCC = DAG.getSetCC(N->getDebugLoc(), MatchingVectorType,
4435 N0.getOperand(0), N0.getOperand(1),
4436 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4437 return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT);
4438 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004439 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004440 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004441
Chris Lattner2b7a2712009-07-08 00:31:33 +00004442 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004443 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004444 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004445 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004446 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004447 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004448 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004449 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004450 if (SCC.getNode()) return SCC;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00004451 if (!LegalOperations ||
4452 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(VT)))
4453 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4454 DAG.getSetCC(N->getDebugLoc(),
4455 TLI.getSetCCResultType(VT),
4456 N0.getOperand(0), N0.getOperand(1),
4457 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4458 NegOne, DAG.getConstant(0, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004459 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004460
Dan Gohman8f0ad582008-04-28 16:58:24 +00004461 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004462 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004463 DAG.SignBitIsZero(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004464 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004465
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004466 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004467}
4468
Rafael Espindoladecbc432012-04-09 16:06:03 +00004469// isTruncateOf - If N is a truncate of some other value, return true, record
4470// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4471// This function computes KnownZero to avoid a duplicated call to
4472// ComputeMaskedBits in the caller.
4473static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4474 APInt &KnownZero) {
4475 APInt KnownOne;
4476 if (N->getOpcode() == ISD::TRUNCATE) {
4477 Op = N->getOperand(0);
4478 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4479 return true;
4480 }
4481
4482 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4483 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4484 return false;
4485
4486 SDValue Op0 = N->getOperand(0);
4487 SDValue Op1 = N->getOperand(1);
4488 assert(Op0.getValueType() == Op1.getValueType());
4489
4490 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4491 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004492 if (COp0 && COp0->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004493 Op = Op1;
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004494 else if (COp1 && COp1->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004495 Op = Op0;
4496 else
4497 return false;
4498
4499 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4500
4501 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4502 return false;
4503
4504 return true;
4505}
4506
Dan Gohman475871a2008-07-27 21:46:04 +00004507SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4508 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004509 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004510
Nate Begeman1d4d4142005-09-01 00:19:25 +00004511 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004512 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004513 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004514 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004515 // fold (zext (aext x)) -> (zext x)
4516 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004517 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT,
4518 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004519
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004520 // fold (zext (truncate x)) -> (zext x) or
4521 // (zext (truncate x)) -> (truncate x)
4522 // This is valid when the truncated bits of x are already zero.
4523 // FIXME: We should extend this to work for vectors too.
Rafael Espindoladecbc432012-04-09 16:06:03 +00004524 SDValue Op;
4525 APInt KnownZero;
4526 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4527 APInt TruncatedBits =
4528 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4529 APInt(Op.getValueSizeInBits(), 0) :
4530 APInt::getBitsSet(Op.getValueSizeInBits(),
4531 N0.getValueSizeInBits(),
4532 std::min(Op.getValueSizeInBits(),
4533 VT.getSizeInBits()));
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004534 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004535 if (VT.bitsGT(Op.getValueType()))
4536 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, Op);
4537 if (VT.bitsLT(Op.getValueType()))
4538 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
4539
4540 return Op;
4541 }
4542 }
4543
Evan Chengc88138f2007-03-22 01:54:19 +00004544 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4545 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004546 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004547 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4548 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004549 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4550 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004551 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004552 // CombineTo deleted the truncate, if needed, but not what's under it.
4553 AddToWorkList(oye);
4554 }
Eli Friedmane545d382011-04-16 23:25:34 +00004555 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004556 }
Evan Chengc88138f2007-03-22 01:54:19 +00004557 }
4558
Chris Lattner6007b842006-09-21 06:00:20 +00004559 // fold (zext (truncate x)) -> (and x, mask)
4560 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004561 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004562
4563 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4564 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4565 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4566 if (NarrowLoad.getNode()) {
4567 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4568 if (NarrowLoad.getNode() != N0.getNode()) {
4569 CombineTo(N0.getNode(), NarrowLoad);
4570 // CombineTo deleted the truncate, if needed, but not what's under it.
4571 AddToWorkList(oye);
4572 }
4573 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4574 }
4575
Dan Gohman475871a2008-07-27 21:46:04 +00004576 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004577 if (Op.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004578 Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004579 AddToWorkList(Op.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004580 } else if (Op.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004581 Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004582 AddToWorkList(Op.getNode());
Chris Lattner6007b842006-09-21 06:00:20 +00004583 }
Dan Gohman87862e72009-12-11 21:31:27 +00004584 return DAG.getZeroExtendInReg(Op, N->getDebugLoc(),
4585 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004586 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004587
Dan Gohman97121ba2009-04-08 00:15:30 +00004588 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4589 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004590 if (N0.getOpcode() == ISD::AND &&
4591 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004592 N0.getOperand(1).getOpcode() == ISD::Constant &&
4593 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4594 N0.getValueType()) ||
4595 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004596 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004597 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004598 X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004599 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004600 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004601 }
Dan Gohman220a8232008-03-03 23:51:38 +00004602 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004603 Mask = Mask.zext(VT.getSizeInBits());
Bill Wendling6ce610f2009-01-30 22:23:15 +00004604 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4605 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004606 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004607
Evan Cheng110dec22005-12-14 02:19:23 +00004608 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004609 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004610 // on vectors in one instruction. We only perform this transformation on
4611 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004612 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004613 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004614 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004615 bool DoXform = true;
4616 SmallVector<SDNode*, 4> SetCCs;
4617 if (!N0.hasOneUse())
4618 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4619 if (DoXform) {
4620 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004621 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004622 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004623 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004624 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004625 LN0->isVolatile(), LN0->isNonTemporal(),
4626 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004627 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00004628 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4629 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004630 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004631
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004632 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4633 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004634 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004635 }
Evan Cheng110dec22005-12-14 02:19:23 +00004636 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004637
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004638 // fold (zext (and/or/xor (load x), cst)) ->
4639 // (and/or/xor (zextload x), (zext cst))
4640 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4641 N0.getOpcode() == ISD::XOR) &&
4642 isa<LoadSDNode>(N0.getOperand(0)) &&
4643 N0.getOperand(1).getOpcode() == ISD::Constant &&
4644 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4645 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4646 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4647 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4648 bool DoXform = true;
4649 SmallVector<SDNode*, 4> SetCCs;
4650 if (!N0.hasOneUse())
4651 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4652 SetCCs, TLI);
4653 if (DoXform) {
4654 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), VT,
4655 LN0->getChain(), LN0->getBasePtr(),
4656 LN0->getPointerInfo(),
4657 LN0->getMemoryVT(),
4658 LN0->isVolatile(),
4659 LN0->isNonTemporal(),
4660 LN0->getAlignment());
4661 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4662 Mask = Mask.zext(VT.getSizeInBits());
4663 SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4664 ExtLoad, DAG.getConstant(Mask, VT));
4665 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
4666 N0.getOperand(0).getDebugLoc(),
4667 N0.getOperand(0).getValueType(), ExtLoad);
4668 CombineTo(N, And);
4669 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
4670 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4671 ISD::ZERO_EXTEND);
4672 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4673 }
4674 }
4675 }
4676
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004677 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4678 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004679 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4680 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004681 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004682 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004683 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004684 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004685 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004686 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004687 LN0->getBasePtr(), LN0->getPointerInfo(),
4688 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004689 LN0->isVolatile(), LN0->isNonTemporal(),
4690 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004691 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004692 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004693 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(),
4694 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004695 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004696 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004697 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004698 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004699
Chris Lattner20a35c32007-04-11 05:32:27 +00004700 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004701 if (!LegalOperations && VT.isVector()) {
4702 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4703 // Only do this before legalize for now.
4704 EVT N0VT = N0.getOperand(0).getValueType();
4705 EVT EltVT = VT.getVectorElementType();
4706 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4707 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004708 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004709 // We know that the # elements of the results is the same as the
4710 // # elements of the compare (and the # elements of the compare result
4711 // for that matter). Check to see that they are the same size. If so,
4712 // we know that the element size of the sext'd result matches the
4713 // element size of the compare operands.
4714 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
Duncan Sands28b77e92011-09-06 19:07:46 +00004715 DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004716 N0.getOperand(1),
4717 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4718 DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
4719 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004720
4721 // If the desired elements are smaller or larger than the source
4722 // elements we can use a matching integer vector type and then
4723 // truncate/sign extend
4724 EVT MatchingElementType =
4725 EVT::getIntegerVT(*DAG.getContext(),
4726 N0VT.getScalarType().getSizeInBits());
4727 EVT MatchingVectorType =
4728 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4729 N0VT.getVectorNumElements());
4730 SDValue VsetCC =
Duncan Sands28b77e92011-09-06 19:07:46 +00004731 DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00004732 N0.getOperand(1),
4733 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4734 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4735 DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT),
4736 DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
4737 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00004738 }
4739
4740 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004741 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004742 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00004743 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004744 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004745 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004746 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004747
Evan Cheng9818c042009-12-15 03:00:32 +00004748 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00004749 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00004750 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00004751 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4752 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00004753 SDValue ShAmt = N0.getOperand(1);
4754 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00004755 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00004756 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00004757 // If the original shl may be shifting out bits, do not perform this
4758 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00004759 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
4760 InnerZExt.getOperand(0).getValueType().getSizeInBits();
4761 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00004762 return SDValue();
4763 }
Chris Lattnere0751182011-02-13 19:09:16 +00004764
4765 DebugLoc DL = N->getDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00004766
4767 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00004768 if (VT.getSizeInBits() >= 256)
4769 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00004770
Chris Lattnere0751182011-02-13 19:09:16 +00004771 return DAG.getNode(N0.getOpcode(), DL, VT,
4772 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
4773 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00004774 }
4775
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004776 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004777}
4778
Dan Gohman475871a2008-07-27 21:46:04 +00004779SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
4780 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004781 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004782
Chris Lattner5ffc0662006-05-05 05:58:59 +00004783 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004784 if (isa<ConstantSDNode>(N0))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004785 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00004786 // fold (aext (aext x)) -> (aext x)
4787 // fold (aext (zext x)) -> (zext x)
4788 // fold (aext (sext x)) -> (sext x)
4789 if (N0.getOpcode() == ISD::ANY_EXTEND ||
4790 N0.getOpcode() == ISD::ZERO_EXTEND ||
4791 N0.getOpcode() == ISD::SIGN_EXTEND)
Bill Wendling683c9572009-01-30 22:27:33 +00004792 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004793
Evan Chengc88138f2007-03-22 01:54:19 +00004794 // fold (aext (truncate (load x))) -> (aext (smaller load x))
4795 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
4796 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004797 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4798 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00004799 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4800 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004801 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00004802 // CombineTo deleted the truncate, if needed, but not what's under it.
4803 AddToWorkList(oye);
4804 }
Eli Friedmane545d382011-04-16 23:25:34 +00004805 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004806 }
Evan Chengc88138f2007-03-22 01:54:19 +00004807 }
4808
Chris Lattner84750582006-09-20 06:29:17 +00004809 // fold (aext (truncate x))
4810 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00004811 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00004812 if (TruncOp.getValueType() == VT)
4813 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004814 if (TruncOp.getValueType().bitsGT(VT))
Bill Wendling683c9572009-01-30 22:27:33 +00004815 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp);
4816 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00004817 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004818
Dan Gohman97121ba2009-04-08 00:15:30 +00004819 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
4820 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00004821 if (N0.getOpcode() == ISD::AND &&
4822 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004823 N0.getOperand(1).getOpcode() == ISD::Constant &&
4824 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4825 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004826 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004827 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004828 X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004829 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004830 X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00004831 }
Dan Gohman220a8232008-03-03 23:51:38 +00004832 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004833 Mask = Mask.zext(VT.getSizeInBits());
Bill Wendling683c9572009-01-30 22:27:33 +00004834 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4835 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00004836 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004837
Chris Lattner5ffc0662006-05-05 05:58:59 +00004838 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004839 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004840 // on vectors in one instruction. We only perform this transformation on
4841 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004842 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004843 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004844 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004845 bool DoXform = true;
4846 SmallVector<SDNode*, 4> SetCCs;
4847 if (!N0.hasOneUse())
4848 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
4849 if (DoXform) {
4850 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004851 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004852 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004853 LN0->getBasePtr(), LN0->getPointerInfo(),
Dan Gohman57fc82d2009-04-09 03:51:29 +00004854 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004855 LN0->isVolatile(), LN0->isNonTemporal(),
4856 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00004857 CombineTo(N, ExtLoad);
4858 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4859 N0.getValueType(), ExtLoad);
4860 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004861 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4862 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004863 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4864 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00004865 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004866
Chris Lattner5ffc0662006-05-05 05:58:59 +00004867 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
4868 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
4869 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00004870 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004871 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00004872 N0.hasOneUse()) {
4873 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004874 EVT MemVT = LN0->getMemoryVT();
Stuart Hastingsa9011292011-02-16 16:23:55 +00004875 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(),
4876 VT, LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004877 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00004878 LN0->isVolatile(), LN0->isNonTemporal(),
4879 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00004880 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00004881 CombineTo(N0.getNode(),
Bill Wendling683c9572009-01-30 22:27:33 +00004882 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4883 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00004884 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004885 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00004886 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004887
Chris Lattner20a35c32007-04-11 05:32:27 +00004888 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004889 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
4890 // Only do this before legalize for now.
4891 if (VT.isVector() && !LegalOperations) {
4892 EVT N0VT = N0.getOperand(0).getValueType();
4893 // We know that the # elements of the results is the same as the
4894 // # elements of the compare (and the # elements of the compare result
4895 // for that matter). Check to see that they are the same size. If so,
4896 // we know that the element size of the sext'd result matches the
4897 // element size of the compare operands.
4898 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Duncan Sands28b77e92011-09-06 19:07:46 +00004899 return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004900 N0.getOperand(1),
4901 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00004902 // If the desired elements are smaller or larger than the source
4903 // elements we can use a matching integer vector type and then
4904 // truncate/sign extend
4905 else {
Duncan Sands34727662010-07-12 08:16:59 +00004906 EVT MatchingElementType =
4907 EVT::getIntegerVT(*DAG.getContext(),
4908 N0VT.getScalarType().getSizeInBits());
4909 EVT MatchingVectorType =
4910 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4911 N0VT.getVectorNumElements());
4912 SDValue VsetCC =
Duncan Sands28b77e92011-09-06 19:07:46 +00004913 DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004914 N0.getOperand(1),
4915 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4916 return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00004917 }
4918 }
4919
4920 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004921 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004922 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004923 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00004924 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004925 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004926 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004927 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004928
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004929 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00004930}
4931
Chris Lattner2b4c2792007-10-13 06:35:54 +00004932/// GetDemandedBits - See if the specified operand can be simplified with the
4933/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00004934/// simpler operand, otherwise return a null SDValue.
4935SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004936 switch (V.getOpcode()) {
4937 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00004938 case ISD::Constant: {
4939 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
4940 assert(CV != 0 && "Const value should be ConstSDNode.");
4941 const APInt &CVal = CV->getAPIntValue();
4942 APInt NewVal = CVal & Mask;
4943 if (NewVal != CVal) {
4944 return DAG.getConstant(NewVal, V.getValueType());
4945 }
4946 break;
4947 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00004948 case ISD::OR:
4949 case ISD::XOR:
4950 // If the LHS or RHS don't contribute bits to the or, drop them.
4951 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
4952 return V.getOperand(1);
4953 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
4954 return V.getOperand(0);
4955 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00004956 case ISD::SRL:
4957 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00004958 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00004959 break;
4960 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
4961 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004962 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00004963
Dan Gohmancc91d632009-01-03 19:22:06 +00004964 // Watch out for shift count overflow though.
4965 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004966 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00004967 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00004968 if (SimplifyLHS.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004969 return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00004970 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00004971 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00004972 }
Dan Gohman475871a2008-07-27 21:46:04 +00004973 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00004974}
4975
Evan Chengc88138f2007-03-22 01:54:19 +00004976/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
4977/// bits and then truncated to a narrower type and where N is a multiple
4978/// of number of bits of the narrower type, transform it to a narrower load
4979/// from address + N / num of bits of new type. If the result is to be
4980/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00004981SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00004982 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004983
Evan Chengc88138f2007-03-22 01:54:19 +00004984 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00004985 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004986 EVT VT = N->getValueType(0);
4987 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00004988
Dan Gohman7f8613e2008-08-14 20:04:46 +00004989 // This transformation isn't valid for vector loads.
4990 if (VT.isVector())
4991 return SDValue();
4992
Dan Gohmand1996362010-01-09 02:13:55 +00004993 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00004994 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00004995 if (Opc == ISD::SIGN_EXTEND_INREG) {
4996 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00004997 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004998 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00004999 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005000 ExtType = ISD::ZEXTLOAD;
5001 N0 = SDValue(N, 0);
5002 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5003 if (!N01) return SDValue();
5004 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5005 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00005006 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00005007 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5008 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005009
Owen Andersone50ed302009-08-10 22:56:29 +00005010 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00005011
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005012 // Do not generate loads of non-round integer types since these can
5013 // be expensive (and would be wrong if the type is not byte sized).
5014 if (!ExtVT.isRound())
5015 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005016
Evan Chengc88138f2007-03-22 01:54:19 +00005017 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005018 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00005019 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005020 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005021 // Is the shift amount a multiple of size of VT?
5022 if ((ShAmt & (EVTBits-1)) == 0) {
5023 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00005024 // Is the load width a multiple of size of VT?
5025 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005026 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005027 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005028
Chris Lattnercbf68df2010-12-22 08:02:57 +00005029 // At this point, we must have a load or else we can't do the transform.
5030 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005031
Chris Lattner2831a192010-10-01 05:36:09 +00005032 // If the shift amount is larger than the input type then we're not
5033 // accessing any of the loaded bytes. If the load was a zextload/extload
5034 // then the result of the shift+trunc is zero/undef (handled elsewhere).
5035 // If the load was a sextload then the result is a splat of the sign bit
5036 // of the extended byte. This is not worth optimizing for.
Chris Lattnercbf68df2010-12-22 08:02:57 +00005037 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00005038 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005039 }
5040 }
5041
Dan Gohman394d6292010-11-03 01:47:46 +00005042 // If the load is shifted left (and the result isn't shifted back right),
5043 // we can fold the truncate through the shift.
5044 unsigned ShLeftAmt = 0;
5045 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00005046 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00005047 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5048 ShLeftAmt = N01->getZExtValue();
5049 N0 = N0.getOperand(0);
5050 }
5051 }
Owen Anderson95771af2011-02-25 21:41:48 +00005052
Chris Lattner4c32bc22010-12-22 07:36:50 +00005053 // If we haven't found a load, we can't narrow it. Don't transform one with
5054 // multiple uses, this would require adding a new load.
5055 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse() ||
5056 // Don't change the width of a volatile load.
5057 cast<LoadSDNode>(N0)->isVolatile())
5058 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005059
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005060 // Verify that we are actually reducing a load width here.
5061 if (cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00005062 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005063
Chris Lattner4c32bc22010-12-22 07:36:50 +00005064 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5065 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00005066
Evan Cheng16436df2012-06-26 01:19:33 +00005067 if (PtrType == MVT::Untyped || PtrType.isExtended())
5068 // It's not possible to generate a constant of extended or untyped type.
5069 return SDValue();
5070
Chris Lattner4c32bc22010-12-22 07:36:50 +00005071 // For big endian targets, we need to adjust the offset to the pointer to
5072 // load the correct bytes.
5073 if (TLI.isBigEndian()) {
5074 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5075 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5076 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00005077 }
5078
Chris Lattner4c32bc22010-12-22 07:36:50 +00005079 uint64_t PtrOff = ShAmt / 8;
5080 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
5081 SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(),
5082 PtrType, LN0->getBasePtr(),
5083 DAG.getConstant(PtrOff, PtrType));
5084 AddToWorkList(NewPtr.getNode());
5085
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005086 SDValue Load;
5087 if (ExtType == ISD::NON_EXTLOAD)
5088 Load = DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr,
5089 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005090 LN0->isVolatile(), LN0->isNonTemporal(),
5091 LN0->isInvariant(), NewAlign);
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005092 else
Stuart Hastingsa9011292011-02-16 16:23:55 +00005093 Load = DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005094 LN0->getPointerInfo().getWithOffset(PtrOff),
5095 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
5096 NewAlign);
Chris Lattner4c32bc22010-12-22 07:36:50 +00005097
5098 // Replace the old load's chain with the new load's chain.
5099 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005100 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005101
5102 // Shift the result left, if we've swallowed a left shift.
5103 SDValue Result = Load;
5104 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00005105 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005106 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5107 ShImmTy = VT;
5108 Result = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT,
5109 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
5110 }
5111
5112 // Return the new loaded value.
5113 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00005114}
5115
Dan Gohman475871a2008-07-27 21:46:04 +00005116SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5117 SDValue N0 = N->getOperand(0);
5118 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005119 EVT VT = N->getValueType(0);
5120 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00005121 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00005122 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005123
Nate Begeman1d4d4142005-09-01 00:19:25 +00005124 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00005125 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Bill Wendling8509c902009-01-30 22:33:24 +00005126 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005127
Chris Lattner541a24f2006-05-06 22:43:44 +00005128 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00005129 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00005130 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005131
Nate Begeman646d7e22005-09-02 21:18:40 +00005132 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5133 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00005134 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Bill Wendling8509c902009-01-30 22:33:24 +00005135 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
5136 N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00005137 }
Chris Lattner4b37e872006-05-08 21:18:59 +00005138
Dan Gohman75dcf082008-07-31 00:50:31 +00005139 // fold (sext_in_reg (sext x)) -> (sext x)
5140 // fold (sext_in_reg (aext x)) -> (sext x)
5141 // if x is small enough.
5142 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5143 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00005144 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5145 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Bill Wendling8509c902009-01-30 22:33:24 +00005146 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00005147 }
5148
Chris Lattner95a5e052007-04-17 19:03:21 +00005149 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005150 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005151 return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005152
Chris Lattner95a5e052007-04-17 19:03:21 +00005153 // fold operands of sext_in_reg based on knowledge that the top bits are not
5154 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00005155 if (SimplifyDemandedBits(SDValue(N, 0)))
5156 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005157
Evan Chengc88138f2007-03-22 01:54:19 +00005158 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5159 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00005160 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005161 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00005162 return NarrowLoad;
5163
Bill Wendling8509c902009-01-30 22:33:24 +00005164 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
5165 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00005166 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5167 if (N0.getOpcode() == ISD::SRL) {
5168 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00005169 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Chris Lattner4b37e872006-05-08 21:18:59 +00005170 // We can turn this into an SRA iff the input to the SRL is already sign
5171 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00005172 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00005173 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Bill Wendling8509c902009-01-30 22:33:24 +00005174 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT,
5175 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00005176 }
5177 }
Evan Chengc88138f2007-03-22 01:54:19 +00005178
Nate Begemanded49632005-10-13 03:11:28 +00005179 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00005180 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005181 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005182 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005183 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005184 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005185 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00005186 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005187 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005188 LN0->getBasePtr(), LN0->getPointerInfo(),
5189 EVT,
David Greene1e559442010-02-15 17:00:31 +00005190 LN0->isVolatile(), LN0->isNonTemporal(),
5191 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005192 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005193 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005194 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005195 }
5196 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00005197 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00005198 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005199 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005200 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005201 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005202 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00005203 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005204 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005205 LN0->getBasePtr(), LN0->getPointerInfo(),
5206 EVT,
David Greene1e559442010-02-15 17:00:31 +00005207 LN0->isVolatile(), LN0->isNonTemporal(),
5208 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005209 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005210 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005211 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005212 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00005213
5214 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5215 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5216 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5217 N0.getOperand(1), false);
5218 if (BSwap.getNode() != 0)
5219 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
5220 BSwap, N1);
5221 }
5222
Dan Gohman475871a2008-07-27 21:46:04 +00005223 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005224}
5225
Dan Gohman475871a2008-07-27 21:46:04 +00005226SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5227 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005228 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005229 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005230
5231 // noop truncate
5232 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00005233 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00005234 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005235 if (isa<ConstantSDNode>(N0))
Bill Wendling67a67682009-01-30 22:44:24 +00005236 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005237 // fold (truncate (truncate x)) -> (truncate x)
5238 if (N0.getOpcode() == ISD::TRUNCATE)
Bill Wendling67a67682009-01-30 22:44:24 +00005239 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005240 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00005241 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5242 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00005243 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005244 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005245 // if the source is smaller than the dest, we still need an extend
Bill Wendling67a67682009-01-30 22:44:24 +00005246 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
5247 N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00005248 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005249 // if the source is larger than the dest, than we just need the truncate
Bill Wendling67a67682009-01-30 22:44:24 +00005250 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005251 else
5252 // if the source and dest are the same type, we can drop both the extend
Evan Chengd40d03e2010-01-06 19:38:29 +00005253 // and the truncate.
Nate Begeman83e75ec2005-09-06 04:43:02 +00005254 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005255 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005256
Nadav Rotemcc870a82012-02-05 11:39:23 +00005257 // Fold extract-and-trunc into a narrow extract. For example:
5258 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5259 // i32 y = TRUNCATE(i64 x)
5260 // -- becomes --
5261 // v16i8 b = BITCAST (v2i64 val)
5262 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5263 //
5264 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005265 // creates this pattern) and before operation legalization after which
5266 // we need to be more careful about the vector instructions that we generate.
5267 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5268 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5269
5270 EVT VecTy = N0.getOperand(0).getValueType();
5271 EVT ExTy = N0.getValueType();
5272 EVT TrTy = N->getValueType(0);
5273
5274 unsigned NumElem = VecTy.getVectorNumElements();
5275 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5276
5277 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5278 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5279
5280 SDValue EltNo = N0->getOperand(1);
5281 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5282 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005283 EVT IndexTy = N0->getOperand(1).getValueType();
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005284 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5285
5286 SDValue V = DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
5287 NVT, N0.getOperand(0));
5288
5289 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
5290 N->getDebugLoc(), TrTy, V,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005291 DAG.getConstant(Index, IndexTy));
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005292 }
5293 }
5294
Chris Lattner2b4c2792007-10-13 06:35:54 +00005295 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005296 // only the low bits are being used.
5297 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005298 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005299 // may have different active low bits.
5300 if (!VT.isVector()) {
5301 SDValue Shorter =
5302 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5303 VT.getSizeInBits()));
5304 if (Shorter.getNode())
5305 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter);
5306 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005307 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005308 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005309 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5310 SDValue Reduced = ReduceLoadWidth(N);
5311 if (Reduced.getNode())
5312 return Reduced;
5313 }
5314
5315 // Simplify the operands using demanded-bits information.
5316 if (!VT.isVector() &&
5317 SimplifyDemandedBits(SDValue(N, 0)))
5318 return SDValue(N, 0);
5319
Evan Chenge5b51ac2010-04-17 06:13:15 +00005320 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005321}
5322
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005323static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005324 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005325 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005326 return Elt.getNode();
5327 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005328}
5329
5330/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005331/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005332SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005333 assert(N->getOpcode() == ISD::BUILD_PAIR);
5334
Nate Begemanabc01992009-06-05 21:37:30 +00005335 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5336 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005337 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5338 LD1->getPointerInfo().getAddrSpace() !=
5339 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005340 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005341 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005342
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005343 if (ISD::isNON_EXTLoad(LD2) &&
5344 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005345 // If both are volatile this would reduce the number of volatile loads.
5346 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005347 !LD1->isVolatile() &&
5348 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005349 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005350 unsigned Align = LD1->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00005351 unsigned NewAlign = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005352 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005353
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005354 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005355 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Nate Begemanabc01992009-06-05 21:37:30 +00005356 return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005357 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005358 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005359 }
Bill Wendling67a67682009-01-30 22:44:24 +00005360
Dan Gohman475871a2008-07-27 21:46:04 +00005361 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005362}
5363
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005364SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005365 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005366 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005367
Dan Gohman7f321562007-06-25 16:23:39 +00005368 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5369 // Only do this before legalize, since afterward the target may be depending
5370 // on the bitconvert.
5371 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005372 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005373 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005374 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005375 bool isSimple = true;
5376 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5377 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5378 N0.getOperand(i).getOpcode() != ISD::Constant &&
5379 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005380 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005381 break;
5382 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005383
Owen Andersone50ed302009-08-10 22:56:29 +00005384 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005385 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005386 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005387 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005388 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005389 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005390
Dan Gohman3dd168d2008-09-05 01:58:21 +00005391 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005392 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005393 SDValue Res = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005394 if (Res.getNode() != N) {
5395 if (!LegalOperations ||
5396 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5397 return Res;
5398
5399 // Folding it resulted in an illegal node, and it's too late to
5400 // do that. Clean up the old node and forego the transformation.
5401 // Ideally this won't happen very often, because instcombine
5402 // and the earlier dagcombine runs (where illegal nodes are
5403 // permitted) should have folded most of them already.
5404 DAG.DeleteNode(Res.getNode());
5405 }
Chris Lattner94683772005-12-23 05:30:37 +00005406 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005407
Bill Wendling67a67682009-01-30 22:44:24 +00005408 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005409 if (N0.getOpcode() == ISD::BITCAST)
5410 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005411 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005412
Chris Lattner57104102005-12-23 05:44:41 +00005413 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005414 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005415 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005416 // Do not change the width of a volatile load.
5417 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005418 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005419 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00005420 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005421 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005422 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005423
Evan Cheng59d5b682007-05-07 21:27:48 +00005424 if (Align <= OrigAlign) {
Bill Wendling67a67682009-01-30 22:44:24 +00005425 SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005426 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005427 LN0->isVolatile(), LN0->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005428 LN0->isInvariant(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00005429 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005430 CombineTo(N0.getNode(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005431 DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005432 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005433 Load.getValue(1));
5434 return Load;
5435 }
Chris Lattner57104102005-12-23 05:44:41 +00005436 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005437
Bill Wendling67a67682009-01-30 22:44:24 +00005438 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5439 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005440 // This often reduces constant pool loads.
Owen Anderson29f60f32012-04-02 22:10:29 +00005441 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(VT)) ||
5442 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(VT))) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005443 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005444 SDValue NewConv = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005445 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005446 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005447
Duncan Sands83ec4b62008-06-06 12:08:01 +00005448 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005449 if (N0.getOpcode() == ISD::FNEG)
Bill Wendling67a67682009-01-30 22:44:24 +00005450 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
5451 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005452 assert(N0.getOpcode() == ISD::FABS);
Bill Wendling67a67682009-01-30 22:44:24 +00005453 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
5454 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005455 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005456
Bill Wendling67a67682009-01-30 22:44:24 +00005457 // fold (bitconvert (fcopysign cst, x)) ->
5458 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5459 // Note that we don't handle (copysign x, cst) because this can always be
5460 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005461 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005462 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005463 VT.isInteger() && !VT.isVector()) {
5464 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005465 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005466 if (isTypeLegal(IntXVT)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005467 SDValue X = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005468 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005469 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005470
Duncan Sands25cf2272008-11-24 14:53:14 +00005471 // If X has a different width than the result/lhs, sext it or truncate it.
5472 unsigned VTWidth = VT.getSizeInBits();
5473 if (OrigXWidth < VTWidth) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005474 X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005475 AddToWorkList(X.getNode());
5476 } else if (OrigXWidth > VTWidth) {
5477 // To get the sign bit in the right place, we have to shift it right
5478 // before truncating.
Bill Wendling9729c5a2009-01-31 03:12:48 +00005479 X = DAG.getNode(ISD::SRL, X.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005480 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005481 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5482 AddToWorkList(X.getNode());
Bill Wendling9729c5a2009-01-31 03:12:48 +00005483 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005484 AddToWorkList(X.getNode());
5485 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005486
Duncan Sands25cf2272008-11-24 14:53:14 +00005487 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Bill Wendling9729c5a2009-01-31 03:12:48 +00005488 X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005489 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005490 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005491
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005492 SDValue Cst = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005493 VT, N0.getOperand(0));
Bill Wendling9729c5a2009-01-31 03:12:48 +00005494 Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005495 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005496 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005497
Bill Wendling67a67682009-01-30 22:44:24 +00005498 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005499 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005500 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005501
Scott Michelfdc40a02009-02-17 22:15:04 +00005502 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005503 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005504 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5505 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005506 return CombineLD;
5507 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005508
Dan Gohman475871a2008-07-27 21:46:04 +00005509 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005510}
5511
Dan Gohman475871a2008-07-27 21:46:04 +00005512SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005513 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005514 return CombineConsecutiveLoads(N, VT);
5515}
5516
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005517/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005518/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005519/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005520SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005521ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005522 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005523
Chris Lattner6258fb22006-04-02 02:53:43 +00005524 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005525 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005526
Duncan Sands83ec4b62008-06-06 12:08:01 +00005527 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5528 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005529
Chris Lattner6258fb22006-04-02 02:53:43 +00005530 // If this is a conversion of N elements of one type to N elements of another
5531 // type, convert each element. This handles FP<->INT cases.
5532 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005533 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5534 BV->getValueType(0).getVectorNumElements());
5535
5536 // Due to the FP element handling below calling this routine recursively,
5537 // we can end up with a scalar-to-vector node here.
5538 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005539 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
5540 DAG.getNode(ISD::BITCAST, BV->getDebugLoc(),
Nate Begemane0efc212010-07-27 18:02:18 +00005541 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005542
Dan Gohman475871a2008-07-27 21:46:04 +00005543 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005544 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005545 SDValue Op = BV->getOperand(i);
5546 // If the vector element type is not legal, the BUILD_VECTOR operands
5547 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005548 if (Op.getValueType() != SrcEltVT)
5549 Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005550 Ops.push_back(DAG.getNode(ISD::BITCAST, BV->getDebugLoc(),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005551 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005552 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005553 }
Evan Chenga87008d2009-02-25 22:49:59 +00005554 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5555 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005556 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005557
Chris Lattner6258fb22006-04-02 02:53:43 +00005558 // Otherwise, we're growing or shrinking the elements. To avoid having to
5559 // handle annoying details of growing/shrinking FP values, we convert them to
5560 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005561 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005562 // Convert the input float vector to a int vector where the elements are the
5563 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005564 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005565 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005566 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005567 SrcEltVT = IntVT;
5568 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005569
Chris Lattner6258fb22006-04-02 02:53:43 +00005570 // Now we know the input is an integer vector. If the output is a FP type,
5571 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005572 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005573 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005574 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005575 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005576
Chris Lattner6258fb22006-04-02 02:53:43 +00005577 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005578 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005579 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005580
Chris Lattner6258fb22006-04-02 02:53:43 +00005581 // Okay, we know the src/dst types are both integers of differing types.
5582 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005583 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005584 if (SrcBitSize < DstBitSize) {
5585 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005586
Dan Gohman475871a2008-07-27 21:46:04 +00005587 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005588 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005589 i += NumInputsPerOutput) {
5590 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005591 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005592 bool EltIsUndef = true;
5593 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5594 // Shift the previously computed bits over.
5595 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005596 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005597 if (Op.getOpcode() == ISD::UNDEF) continue;
5598 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005599
Jay Foad40f8f622010-12-07 08:25:19 +00005600 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005601 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005602 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005603
Chris Lattner6258fb22006-04-02 02:53:43 +00005604 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005605 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005606 else
5607 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5608 }
5609
Owen Anderson23b9b192009-08-12 00:36:31 +00005610 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Evan Chenga87008d2009-02-25 22:49:59 +00005611 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5612 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005613 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005614
Chris Lattner6258fb22006-04-02 02:53:43 +00005615 // Finally, this must be the case where we are shrinking elements: each input
5616 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005617 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005618 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005619 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5620 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005621 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005622
Dan Gohman7f321562007-06-25 16:23:39 +00005623 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005624 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5625 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005626 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005627 continue;
5628 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005629
Jay Foad40f8f622010-12-07 08:25:19 +00005630 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5631 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005632
Chris Lattner6258fb22006-04-02 02:53:43 +00005633 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00005634 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005635 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00005636 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00005637 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Bill Wendlingb0162f52009-01-30 22:53:48 +00005638 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
5639 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00005640 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005641 }
5642
5643 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00005644 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00005645 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
5646 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005647
Evan Chenga87008d2009-02-25 22:49:59 +00005648 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5649 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005650}
5651
Dan Gohman475871a2008-07-27 21:46:04 +00005652SDValue DAGCombiner::visitFADD(SDNode *N) {
5653 SDValue N0 = N->getOperand(0);
5654 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005655 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5656 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005657 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005658
Dan Gohman7f321562007-06-25 16:23:39 +00005659 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005660 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005661 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005662 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005663 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005664
Lang Hames01806942012-06-14 20:37:15 +00005665 // fold (fadd c1, c2) -> c1 + c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005666 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005667 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005668 // canonicalize constant to RHS
5669 if (N0CFP && !N1CFP)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005670 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0);
5671 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005672 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5673 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005674 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005675 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005676 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
5677 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005678 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005679 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00005680 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Andersonafd3d562012-03-06 00:29:31 +00005681 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
5682 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005683 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00005684 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005685
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005686 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005687 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5688 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
5689 isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlingb0162f52009-01-30 22:53:48 +00005690 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005691 DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5692 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005693
Owen Anderson43da6c72012-08-30 23:35:16 +00005694 // In unsafe math mode, we can fold chains of FADD's of the same value
5695 // into multiplications. This transform is not safe in general because
5696 // we are reducing the number of rounding steps.
5697 if (DAG.getTarget().Options.UnsafeFPMath &&
5698 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
5699 !N0CFP && !N1CFP) {
5700 if (N0.getOpcode() == ISD::FMUL) {
5701 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
5702 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
5703
5704 // (fadd (fmul c, x), x) -> (fmul c+1, x)
5705 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
5706 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5707 SDValue(CFP00, 0),
5708 DAG.getConstantFP(1.0, VT));
5709 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5710 N1, NewCFP);
5711 }
5712
5713 // (fadd (fmul x, c), x) -> (fmul c+1, x)
5714 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
5715 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5716 SDValue(CFP01, 0),
5717 DAG.getConstantFP(1.0, VT));
5718 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5719 N1, NewCFP);
5720 }
5721
5722 // (fadd (fadd x, x), x) -> (fmul 3.0, x)
5723 if (!CFP00 && !CFP01 && N0.getOperand(0) == N0.getOperand(1) &&
5724 N0.getOperand(0) == N1) {
5725 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5726 N1, DAG.getConstantFP(3.0, VT));
5727 }
5728
5729 // (fadd (fmul c, x), (fadd x, x)) -> (fmul c+2, x)
5730 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
5731 N1.getOperand(0) == N1.getOperand(1) &&
5732 N0.getOperand(1) == N1.getOperand(0)) {
5733 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5734 SDValue(CFP00, 0),
5735 DAG.getConstantFP(2.0, VT));
5736 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5737 N0.getOperand(1), NewCFP);
5738 }
5739
5740 // (fadd (fmul x, c), (fadd x, x)) -> (fmul c+2, x)
5741 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
5742 N1.getOperand(0) == N1.getOperand(1) &&
5743 N0.getOperand(0) == N1.getOperand(0)) {
5744 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5745 SDValue(CFP01, 0),
5746 DAG.getConstantFP(2.0, VT));
5747 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5748 N0.getOperand(0), NewCFP);
5749 }
5750 }
5751
5752 if (N1.getOpcode() == ISD::FMUL) {
5753 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
5754 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
5755
5756 // (fadd x, (fmul c, x)) -> (fmul c+1, x)
5757 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
5758 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5759 SDValue(CFP10, 0),
5760 DAG.getConstantFP(1.0, VT));
5761 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5762 N0, NewCFP);
5763 }
5764
5765 // (fadd x, (fmul x, c)) -> (fmul c+1, x)
5766 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
5767 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5768 SDValue(CFP11, 0),
5769 DAG.getConstantFP(1.0, VT));
5770 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5771 N0, NewCFP);
5772 }
5773
5774 // (fadd x, (fadd x, x)) -> (fmul 3.0, x)
5775 if (!CFP10 && !CFP11 && N1.getOperand(0) == N1.getOperand(1) &&
5776 N1.getOperand(0) == N0) {
5777 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5778 N0, DAG.getConstantFP(3.0, VT));
5779 }
5780
5781 // (fadd (fadd x, x), (fmul c, x)) -> (fmul c+2, x)
5782 if (CFP10 && !CFP11 && N1.getOpcode() == ISD::FADD &&
5783 N1.getOperand(0) == N1.getOperand(1) &&
5784 N0.getOperand(1) == N1.getOperand(0)) {
5785 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5786 SDValue(CFP10, 0),
5787 DAG.getConstantFP(2.0, VT));
5788 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5789 N0.getOperand(1), NewCFP);
5790 }
5791
5792 // (fadd (fadd x, x), (fmul x, c)) -> (fmul c+2, x)
5793 if (CFP11 && !CFP10 && N1.getOpcode() == ISD::FADD &&
5794 N1.getOperand(0) == N1.getOperand(1) &&
5795 N0.getOperand(0) == N1.getOperand(0)) {
5796 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5797 SDValue(CFP11, 0),
5798 DAG.getConstantFP(2.0, VT));
5799 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5800 N0.getOperand(0), NewCFP);
5801 }
5802 }
5803
5804 // (fadd (fadd x, x), (fadd x, x)) -> (fmul 4.0, x)
5805 if (N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
5806 N0.getOperand(0) == N0.getOperand(1) &&
5807 N1.getOperand(0) == N1.getOperand(1) &&
5808 N0.getOperand(0) == N1.getOperand(0)) {
5809 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5810 N0.getOperand(0),
5811 DAG.getConstantFP(4.0, VT));
5812 }
5813 }
5814
Lang Hamesd693caf2012-06-19 22:51:23 +00005815 // FADD -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00005816 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00005817 DAG.getTarget().Options.UnsafeFPMath) &&
5818 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005819 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00005820
5821 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
5822 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) {
5823 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT,
5824 N0.getOperand(0), N0.getOperand(1), N1);
5825 }
Owen Anderson43da6c72012-08-30 23:35:16 +00005826
Michael Liaob79bff52012-09-01 04:09:16 +00005827 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hamesd693caf2012-06-19 22:51:23 +00005828 // Note: Commutes FADD operands.
5829 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) {
5830 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT,
5831 N1.getOperand(0), N1.getOperand(1), N0);
5832 }
5833 }
5834
Dan Gohman475871a2008-07-27 21:46:04 +00005835 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005836}
5837
Dan Gohman475871a2008-07-27 21:46:04 +00005838SDValue DAGCombiner::visitFSUB(SDNode *N) {
5839 SDValue N0 = N->getOperand(0);
5840 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005841 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5842 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005843 EVT VT = N->getValueType(0);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005844 DebugLoc dl = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00005845
Dan Gohman7f321562007-06-25 16:23:39 +00005846 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005847 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005848 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005849 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005850 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005851
Nate Begemana0e221d2005-10-18 00:28:13 +00005852 // fold (fsub c1, c2) -> c1-c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005853 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005854 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005855 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005856 if (DAG.getTarget().Options.UnsafeFPMath &&
5857 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00005858 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005859 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005860 if (DAG.getTarget().Options.UnsafeFPMath &&
5861 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Andersonafd3d562012-03-06 00:29:31 +00005862 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00005863 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00005864 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005865 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00005866 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005867 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005868 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005869 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005870 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005871
Bill Wendling5a894342012-03-15 05:12:00 +00005872 // If 'unsafe math' is enabled, fold
Owen Anderson713e9532012-05-07 20:51:25 +00005873 // (fsub x, x) -> 0.0 &
Bill Wendling5a894342012-03-15 05:12:00 +00005874 // (fsub x, (fadd x, y)) -> (fneg y) &
5875 // (fsub x, (fadd y, x)) -> (fneg y)
5876 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Anderson713e9532012-05-07 20:51:25 +00005877 if (N0 == N1)
5878 return DAG.getConstantFP(0.0f, VT);
5879
Bill Wendling5a894342012-03-15 05:12:00 +00005880 if (N1.getOpcode() == ISD::FADD) {
5881 SDValue N10 = N1->getOperand(0);
5882 SDValue N11 = N1->getOperand(1);
5883
5884 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
5885 &DAG.getTarget().Options))
5886 return GetNegatedExpression(N11, DAG, LegalOperations);
5887 else if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
5888 &DAG.getTarget().Options))
5889 return GetNegatedExpression(N10, DAG, LegalOperations);
5890 }
5891 }
5892
Lang Hamesd693caf2012-06-19 22:51:23 +00005893 // FSUB -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00005894 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00005895 DAG.getTarget().Options.UnsafeFPMath) &&
5896 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005897 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00005898
5899 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
5900 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005901 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00005902 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005903 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hamesd693caf2012-06-19 22:51:23 +00005904 }
5905
5906 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
5907 // Note: Commutes FSUB operands.
5908 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005909 return DAG.getNode(ISD::FMA, dl, VT,
5910 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00005911 N1.getOperand(0)),
5912 N1.getOperand(1), N0);
5913 }
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005914
5915 // fold (fsub (-(fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
5916 if (N0.getOpcode() == ISD::FNEG &&
5917 N0.getOperand(0).getOpcode() == ISD::FMUL &&
5918 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
5919 SDValue N00 = N0.getOperand(0).getOperand(0);
5920 SDValue N01 = N0.getOperand(0).getOperand(1);
5921 return DAG.getNode(ISD::FMA, dl, VT,
5922 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
5923 DAG.getNode(ISD::FNEG, dl, VT, N1));
5924 }
Lang Hamesd693caf2012-06-19 22:51:23 +00005925 }
5926
Dan Gohman475871a2008-07-27 21:46:04 +00005927 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005928}
5929
Dan Gohman475871a2008-07-27 21:46:04 +00005930SDValue DAGCombiner::visitFMUL(SDNode *N) {
5931 SDValue N0 = N->getOperand(0);
5932 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00005933 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5934 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005935 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00005936 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00005937
Dan Gohman7f321562007-06-25 16:23:39 +00005938 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005939 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005940 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005941 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005942 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005943
Nate Begeman11af4ea2005-10-17 20:40:11 +00005944 // fold (fmul c1, c2) -> c1*c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005945 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005946 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00005947 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00005948 if (N0CFP && !N1CFP)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005949 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0);
5950 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005951 if (DAG.getTarget().Options.UnsafeFPMath &&
5952 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005953 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00005954 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005955 if (DAG.getTarget().Options.UnsafeFPMath &&
5956 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00005957 return N1;
Owen Anderson363e4b92012-05-02 21:32:35 +00005958 // fold (fmul A, 1.0) -> A
5959 if (N1CFP && N1CFP->isExactlyValue(1.0))
5960 return N0;
Nate Begeman11af4ea2005-10-17 20:40:11 +00005961 // fold (fmul X, 2.0) -> (fadd X, X)
5962 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005963 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00005964 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00005965 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00005966 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005967 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005968
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005969 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00005970 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005971 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00005972 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005973 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00005974 // Both can be negated for free, check to see if at least one is cheaper
5975 // negated.
5976 if (LHSNeg == 2 || RHSNeg == 2)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005977 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00005978 GetNegatedExpression(N0, DAG, LegalOperations),
5979 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00005980 }
5981 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005982
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005983 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005984 if (DAG.getTarget().Options.UnsafeFPMath &&
5985 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005986 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005987 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00005988 DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Dale Johannesende064702009-02-06 21:50:26 +00005989 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005990
Dan Gohman475871a2008-07-27 21:46:04 +00005991 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005992}
5993
Owen Anderson062c0a52012-05-02 22:17:40 +00005994SDValue DAGCombiner::visitFMA(SDNode *N) {
5995 SDValue N0 = N->getOperand(0);
5996 SDValue N1 = N->getOperand(1);
5997 SDValue N2 = N->getOperand(2);
5998 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5999 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6000 EVT VT = N->getValueType(0);
Owen Anderson58d57292012-09-01 06:04:27 +00006001 DebugLoc dl = N->getDebugLoc();
Owen Anderson062c0a52012-05-02 22:17:40 +00006002
6003 if (N0CFP && N0CFP->isExactlyValue(1.0))
6004 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N2);
6005 if (N1CFP && N1CFP->isExactlyValue(1.0))
6006 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N2);
6007
Owen Anderson85ef6f42012-05-30 18:50:39 +00006008 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Andersonf917d202012-05-30 18:54:50 +00006009 if (N0CFP && !N1CFP)
Owen Anderson85ef6f42012-05-30 18:50:39 +00006010 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT, N1, N0, N2);
6011
Owen Anderson58d57292012-09-01 06:04:27 +00006012 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6013 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6014 N2.getOpcode() == ISD::FMUL &&
6015 N0 == N2.getOperand(0) &&
6016 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6017 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6018 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6019 }
6020
6021
6022 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6023 if (DAG.getTarget().Options.UnsafeFPMath &&
6024 N0.getOpcode() == ISD::FMUL && N1CFP &&
6025 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6026 return DAG.getNode(ISD::FMA, dl, VT,
6027 N0.getOperand(0),
6028 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6029 N2);
6030 }
6031
6032 // (fma x, 1, y) -> (fadd x, y)
6033 // (fma x, -1, y) -> (fadd (fneg x), y)
6034 if (N1CFP) {
6035 if (N1CFP->isExactlyValue(1.0))
6036 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6037
6038 if (N1CFP->isExactlyValue(-1.0) &&
6039 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6040 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6041 AddToWorkList(RHSNeg.getNode());
6042 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6043 }
6044 }
6045
6046 // (fma x, c, x) -> (fmul x, (c+1))
6047 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2) {
6048 return DAG.getNode(ISD::FMUL, dl, VT,
6049 N0,
6050 DAG.getNode(ISD::FADD, dl, VT,
6051 N1, DAG.getConstantFP(1.0, VT)));
6052 }
6053
6054 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6055 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6056 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0) {
6057 return DAG.getNode(ISD::FMUL, dl, VT,
6058 N0,
6059 DAG.getNode(ISD::FADD, dl, VT,
6060 N1, DAG.getConstantFP(-1.0, VT)));
6061 }
6062
6063
Owen Anderson062c0a52012-05-02 22:17:40 +00006064 return SDValue();
6065}
6066
Dan Gohman475871a2008-07-27 21:46:04 +00006067SDValue DAGCombiner::visitFDIV(SDNode *N) {
6068 SDValue N0 = N->getOperand(0);
6069 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006070 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6071 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006072 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006073 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006074
Dan Gohman7f321562007-06-25 16:23:39 +00006075 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006076 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006077 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006078 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006079 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006080
Nate Begemana148d982006-01-18 22:35:16 +00006081 // fold (fdiv c1, c2) -> c1/c2
Owen Anderson825b72b2009-08-11 20:47:22 +00006082 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006083 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006084
Duncan Sands3ef3fcf2012-04-08 18:08:12 +00006085 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
6086 if (N1CFP && VT != MVT::ppcf128 && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands961d6662012-04-07 20:04:00 +00006087 // Compute the reciprocal 1.0 / c2.
6088 APFloat N1APF = N1CFP->getValueAPF();
6089 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6090 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands507bb7a2012-04-10 20:35:27 +00006091 // Only do the transform if the reciprocal is a legal fp immediate that
6092 // isn't too nasty (eg NaN, denormal, ...).
6093 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov999821c2012-04-10 13:22:49 +00006094 (!LegalOperations ||
6095 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6096 // backend)... we should handle this gracefully after Legalize.
6097 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6098 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6099 TLI.isFPImmLegal(Recip, VT)))
Duncan Sands961d6662012-04-07 20:04:00 +00006100 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0,
6101 DAG.getConstantFP(Recip, VT));
6102 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006103
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006104 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006105 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006106 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006107 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006108 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006109 // Both can be negated for free, check to see if at least one is cheaper
6110 // negated.
6111 if (LHSNeg == 2 || RHSNeg == 2)
Scott Michelfdc40a02009-02-17 22:15:04 +00006112 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006113 GetNegatedExpression(N0, DAG, LegalOperations),
6114 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006115 }
6116 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006117
Dan Gohman475871a2008-07-27 21:46:04 +00006118 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006119}
6120
Dan Gohman475871a2008-07-27 21:46:04 +00006121SDValue DAGCombiner::visitFREM(SDNode *N) {
6122 SDValue N0 = N->getOperand(0);
6123 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006124 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6125 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006126 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00006127
Nate Begemana148d982006-01-18 22:35:16 +00006128 // fold (frem c1, c2) -> fmod(c1,c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00006129 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006130 return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00006131
Dan Gohman475871a2008-07-27 21:46:04 +00006132 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006133}
6134
Dan Gohman475871a2008-07-27 21:46:04 +00006135SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6136 SDValue N0 = N->getOperand(0);
6137 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00006138 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6139 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006140 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00006141
Owen Anderson825b72b2009-08-11 20:47:22 +00006142 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006143 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006144
Chris Lattner12d83032006-03-05 05:30:57 +00006145 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00006146 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00006147 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6148 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00006149 if (!V.isNegative()) {
6150 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006151 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00006152 } else {
6153 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006154 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00006155 DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00006156 }
Chris Lattner12d83032006-03-05 05:30:57 +00006157 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006158
Chris Lattner12d83032006-03-05 05:30:57 +00006159 // copysign(fabs(x), y) -> copysign(x, y)
6160 // copysign(fneg(x), y) -> copysign(x, y)
6161 // copysign(copysign(x,z), y) -> copysign(x, y)
6162 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6163 N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006164 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6165 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00006166
6167 // copysign(x, abs(y)) -> abs(x)
6168 if (N1.getOpcode() == ISD::FABS)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006169 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006170
Chris Lattner12d83032006-03-05 05:30:57 +00006171 // copysign(x, copysign(y,z)) -> copysign(x, z)
6172 if (N1.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006173 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6174 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006175
Chris Lattner12d83032006-03-05 05:30:57 +00006176 // copysign(x, fp_extend(y)) -> copysign(x, y)
6177 // copysign(x, fp_round(y)) -> copysign(x, y)
6178 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006179 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6180 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006181
Dan Gohman475871a2008-07-27 21:46:04 +00006182 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00006183}
6184
Dan Gohman475871a2008-07-27 21:46:04 +00006185SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6186 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006187 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006188 EVT VT = N->getValueType(0);
6189 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00006190
Nate Begeman1d4d4142005-09-01 00:19:25 +00006191 // fold (sint_to_fp c1) -> c1fp
Stuart Hastings7e334182011-03-02 19:36:30 +00006192 if (N0C && OpVT != MVT::ppcf128 &&
6193 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006194 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006195 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006196 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006197
Chris Lattnercda88752008-06-26 00:16:49 +00006198 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6199 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006200 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6201 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006202 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006203 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006204 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006205 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006206
Nadav Rotemed1a3352012-07-23 07:59:50 +00006207 // The next optimizations are desireable only if SELECT_CC can be lowered.
6208 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6209 // having to say they don't support SELECT_CC on every type the DAG knows
6210 // about, since there is no way to mark an opcode illegal at all value types
6211 // (See also visitSELECT)
6212 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6213 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6214 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6215 !VT.isVector() &&
6216 (!LegalOperations ||
6217 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6218 SDValue Ops[] =
6219 { N0.getOperand(0), N0.getOperand(1),
6220 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6221 N0.getOperand(2) };
6222 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6223 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006224
Nadav Rotemed1a3352012-07-23 07:59:50 +00006225 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6226 // (select_cc x, y, 1.0, 0.0,, cc)
6227 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6228 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6229 (!LegalOperations ||
6230 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6231 SDValue Ops[] =
6232 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6233 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6234 N0.getOperand(0).getOperand(2) };
6235 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6236 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006237 }
6238
Dan Gohman475871a2008-07-27 21:46:04 +00006239 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006240}
6241
Dan Gohman475871a2008-07-27 21:46:04 +00006242SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6243 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006244 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006245 EVT VT = N->getValueType(0);
6246 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00006247
Nate Begeman1d4d4142005-09-01 00:19:25 +00006248 // fold (uint_to_fp c1) -> c1fp
Stuart Hastings7e334182011-03-02 19:36:30 +00006249 if (N0C && OpVT != MVT::ppcf128 &&
6250 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006251 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006252 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006253 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006254
Chris Lattnercda88752008-06-26 00:16:49 +00006255 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6256 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006257 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6258 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006259 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006260 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006261 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006262 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006263
Nadav Rotemed1a3352012-07-23 07:59:50 +00006264 // The next optimizations are desireable only if SELECT_CC can be lowered.
6265 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6266 // having to say they don't support SELECT_CC on every type the DAG knows
6267 // about, since there is no way to mark an opcode illegal at all value types
6268 // (See also visitSELECT)
6269 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6270 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond9bf71f2012-07-09 20:31:12 +00006271
Nadav Rotemed1a3352012-07-23 07:59:50 +00006272 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6273 (!LegalOperations ||
6274 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6275 SDValue Ops[] =
6276 { N0.getOperand(0), N0.getOperand(1),
6277 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6278 N0.getOperand(2) };
6279 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6280 }
6281 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006282
Dan Gohman475871a2008-07-27 21:46:04 +00006283 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006284}
6285
Dan Gohman475871a2008-07-27 21:46:04 +00006286SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6287 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006288 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006289 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006290
Nate Begeman1d4d4142005-09-01 00:19:25 +00006291 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00006292 if (N0CFP)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006293 return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0);
6294
Dan Gohman475871a2008-07-27 21:46:04 +00006295 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006296}
6297
Dan Gohman475871a2008-07-27 21:46:04 +00006298SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6299 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006300 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006301 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006302
Nate Begeman1d4d4142005-09-01 00:19:25 +00006303 // fold (fp_to_uint c1fp) -> c1
Owen Anderson825b72b2009-08-11 20:47:22 +00006304 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006305 return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0);
6306
Dan Gohman475871a2008-07-27 21:46:04 +00006307 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006308}
6309
Dan Gohman475871a2008-07-27 21:46:04 +00006310SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6311 SDValue N0 = N->getOperand(0);
6312 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006313 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006314 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006315
Nate Begeman1d4d4142005-09-01 00:19:25 +00006316 // fold (fp_round c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00006317 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006318 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006319
Chris Lattner79dbea52006-03-13 06:26:26 +00006320 // fold (fp_round (fp_extend x)) -> x
6321 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6322 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006323
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006324 // fold (fp_round (fp_round x)) -> (fp_round x)
6325 if (N0.getOpcode() == ISD::FP_ROUND) {
6326 // This is a value preserving truncation if both round's are.
6327 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006328 N0.getNode()->getConstantOperandVal(1) == 1;
Bill Wendling0225a1d2009-01-30 23:15:49 +00006329 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006330 DAG.getIntPtrConstant(IsTrunc));
6331 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006332
Chris Lattner79dbea52006-03-13 06:26:26 +00006333 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00006334 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Bill Wendling0225a1d2009-01-30 23:15:49 +00006335 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT,
6336 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00006337 AddToWorkList(Tmp.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00006338 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6339 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00006340 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006341
Dan Gohman475871a2008-07-27 21:46:04 +00006342 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006343}
6344
Dan Gohman475871a2008-07-27 21:46:04 +00006345SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6346 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006347 EVT VT = N->getValueType(0);
6348 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00006349 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006350
Nate Begeman1d4d4142005-09-01 00:19:25 +00006351 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00006352 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00006353 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006354 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006355 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006356
Dan Gohman475871a2008-07-27 21:46:04 +00006357 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006358}
6359
Dan Gohman475871a2008-07-27 21:46:04 +00006360SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6361 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006362 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006363 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006364
Chris Lattner5938bef2007-12-29 06:55:23 +00006365 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00006366 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00006367 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00006368 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00006369
Nate Begeman1d4d4142005-09-01 00:19:25 +00006370 // fold (fp_extend c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00006371 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006372 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006373
6374 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6375 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00006376 if (N0.getOpcode() == ISD::FP_ROUND
6377 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00006378 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006379 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006380 if (VT.bitsLT(In.getValueType()))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006381 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT,
6382 In, N0.getOperand(1));
6383 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00006384 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006385
Chris Lattner0bd48932008-01-17 07:00:52 +00006386 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00006387 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006388 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00006389 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00006390 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00006391 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006392 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00006393 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00006394 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00006395 LN0->isVolatile(), LN0->isNonTemporal(),
6396 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00006397 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006398 CombineTo(N0.getNode(),
6399 DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(),
6400 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00006401 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006402 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00006403 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006404
Dan Gohman475871a2008-07-27 21:46:04 +00006405 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006406}
6407
Dan Gohman475871a2008-07-27 21:46:04 +00006408SDValue DAGCombiner::visitFNEG(SDNode *N) {
6409 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006410 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00006411
Craig Topper956342b2012-09-09 22:58:45 +00006412 if (VT.isVector() && !LegalOperations) {
6413 // If operand is a BUILD_VECTOR node, see if we can constant fold it.
6414 if (N0.getOpcode() == ISD::BUILD_VECTOR) {
6415 SmallVector<SDValue, 8> Ops;
6416 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
6417 SDValue Op = N0.getOperand(i);
6418 if (Op.getOpcode() != ISD::UNDEF &&
6419 Op.getOpcode() != ISD::ConstantFP)
6420 break;
6421 EVT EltVT = Op.getValueType();
6422 SDValue FoldOp = DAG.getNode(ISD::FNEG, N0.getDebugLoc(), EltVT, Op);
6423 if (FoldOp.getOpcode() != ISD::UNDEF &&
6424 FoldOp.getOpcode() != ISD::ConstantFP)
6425 break;
6426 Ops.push_back(FoldOp);
6427 AddToWorkList(FoldOp.getNode());
6428 }
6429
6430 if (Ops.size() == N0.getNumOperands())
6431 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
6432 VT, &Ops[0], Ops.size());
6433 }
6434 }
6435
Owen Andersonafd3d562012-03-06 00:29:31 +00006436 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6437 &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006438 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00006439
Chris Lattner3bd39d42008-01-27 17:42:27 +00006440 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6441 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006442 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006443 !VT.isVector() &&
6444 N0.getNode()->hasOneUse() &&
6445 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006446 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006447 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006448 if (IntVT.isInteger() && !IntVT.isVector()) {
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006449 Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int,
6450 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006451 AddToWorkList(Int.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006452 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006453 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006454 }
6455 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006456
Owen Anderson58d57292012-09-01 06:04:27 +00006457 // (fneg (fmul c, x)) -> (fmul -c, x)
6458 if (N0.getOpcode() == ISD::FMUL) {
6459 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
6460 if (CFP1) {
6461 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
6462 N0.getOperand(0),
6463 DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
6464 N0.getOperand(1)));
6465 }
6466 }
6467
Dan Gohman475871a2008-07-27 21:46:04 +00006468 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006469}
6470
Owen Anderson7c626d32012-08-13 23:32:49 +00006471SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6472 SDValue N0 = N->getOperand(0);
6473 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6474 EVT VT = N->getValueType(0);
6475
6476 // fold (fceil c1) -> fceil(c1)
6477 if (N0CFP && VT != MVT::ppcf128)
6478 return DAG.getNode(ISD::FCEIL, N->getDebugLoc(), VT, N0);
6479
6480 return SDValue();
6481}
6482
6483SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6484 SDValue N0 = N->getOperand(0);
6485 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6486 EVT VT = N->getValueType(0);
6487
6488 // fold (ftrunc c1) -> ftrunc(c1)
6489 if (N0CFP && VT != MVT::ppcf128)
6490 return DAG.getNode(ISD::FTRUNC, N->getDebugLoc(), VT, N0);
6491
6492 return SDValue();
6493}
6494
6495SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6496 SDValue N0 = N->getOperand(0);
6497 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6498 EVT VT = N->getValueType(0);
6499
6500 // fold (ffloor c1) -> ffloor(c1)
6501 if (N0CFP && VT != MVT::ppcf128)
6502 return DAG.getNode(ISD::FFLOOR, N->getDebugLoc(), VT, N0);
6503
6504 return SDValue();
6505}
6506
Dan Gohman475871a2008-07-27 21:46:04 +00006507SDValue DAGCombiner::visitFABS(SDNode *N) {
6508 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006509 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006510 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006511
Nate Begeman1d4d4142005-09-01 00:19:25 +00006512 // fold (fabs c1) -> fabs(c1)
Owen Anderson825b72b2009-08-11 20:47:22 +00006513 if (N0CFP && VT != MVT::ppcf128)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006514 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006515 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006516 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00006517 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006518 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006519 // fold (fabs (fcopysign x, y)) -> (fabs x)
6520 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006521 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006522
Chris Lattner3bd39d42008-01-27 17:42:27 +00006523 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6524 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006525 if (!TLI.isFAbsFree(VT) &&
6526 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006527 N0.getOperand(0).getValueType().isInteger() &&
6528 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006529 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006530 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006531 if (IntVT.isInteger() && !IntVT.isVector()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006532 Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006533 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006534 AddToWorkList(Int.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006535 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bill Wendlingc0debad2009-01-30 23:27:35 +00006536 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006537 }
6538 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006539
Dan Gohman475871a2008-07-27 21:46:04 +00006540 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006541}
6542
Dan Gohman475871a2008-07-27 21:46:04 +00006543SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6544 SDValue Chain = N->getOperand(0);
6545 SDValue N1 = N->getOperand(1);
6546 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006547
Dan Gohmane0f06c72009-11-17 00:47:23 +00006548 // If N is a constant we could fold this into a fallthrough or unconditional
6549 // branch. However that doesn't happen very often in normal code, because
6550 // Instcombine/SimplifyCFG should have handled the available opportunities.
6551 // If we did this folding here, it would be necessary to update the
6552 // MachineBasicBlock CFG, which is awkward.
6553
Nate Begeman750ac1b2006-02-01 07:19:44 +00006554 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6555 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006556 if (N1.getOpcode() == ISD::SETCC &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006557 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
6558 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006559 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00006560 N1.getOperand(0), N1.getOperand(1), N2);
6561 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006562
Evan Cheng2a135ae2010-10-04 22:41:01 +00006563 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6564 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6565 (N1.getOperand(0).hasOneUse() &&
6566 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6567 SDNode *Trunc = 0;
6568 if (N1.getOpcode() == ISD::TRUNCATE) {
6569 // Look pass the truncate.
6570 Trunc = N1.getNode();
6571 N1 = N1.getOperand(0);
6572 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006573
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006574 // Match this pattern so that we can generate simpler code:
6575 //
6576 // %a = ...
6577 // %b = and i32 %a, 2
6578 // %c = srl i32 %b, 1
6579 // brcond i32 %c ...
6580 //
6581 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006582 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006583 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00006584 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006585 // %c = setcc eq %b, 0
6586 // brcond %c ...
6587 //
6588 // This applies only when the AND constant value has one bit set and the
6589 // SRL constant is equal to the log2 of the AND constant. The back-end is
6590 // smart enough to convert the result into a TEST/JMP sequence.
6591 SDValue Op0 = N1.getOperand(0);
6592 SDValue Op1 = N1.getOperand(1);
6593
6594 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006595 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006596 SDValue AndOp1 = Op0.getOperand(1);
6597
6598 if (AndOp1.getOpcode() == ISD::Constant) {
6599 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
6600
6601 if (AndConst.isPowerOf2() &&
6602 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
6603 SDValue SetCC =
6604 DAG.getSetCC(N->getDebugLoc(),
6605 TLI.getSetCCResultType(Op0.getValueType()),
6606 Op0, DAG.getConstant(0, Op0.getValueType()),
6607 ISD::SETNE);
6608
Evan Chengd40d03e2010-01-06 19:38:29 +00006609 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6610 MVT::Other, Chain, SetCC, N2);
6611 // Don't add the new BRCond into the worklist or else SimplifySelectCC
6612 // will convert it back to (X & C1) >> C2.
6613 CombineTo(N, NewBRCond, false);
6614 // Truncate is dead.
6615 if (Trunc) {
6616 removeFromWorkList(Trunc);
6617 DAG.DeleteNode(Trunc);
6618 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006619 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00006620 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006621 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006622 removeFromWorkList(N1.getNode());
6623 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00006624 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006625 }
6626 }
6627 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00006628
6629 if (Trunc)
6630 // Restore N1 if the above transformation doesn't match.
6631 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006632 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006633
Evan Cheng2c755ba2010-02-27 07:36:59 +00006634 // Transform br(xor(x, y)) -> br(x != y)
6635 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
6636 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
6637 SDNode *TheXor = N1.getNode();
6638 SDValue Op0 = TheXor->getOperand(0);
6639 SDValue Op1 = TheXor->getOperand(1);
6640 if (Op0.getOpcode() == Op1.getOpcode()) {
6641 // Avoid missing important xor optimizations.
6642 SDValue Tmp = visitXOR(TheXor);
Bill Wendling86c5abb2010-04-20 01:25:01 +00006643 if (Tmp.getNode() && Tmp.getNode() != TheXor) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006644 DEBUG(dbgs() << "\nReplacing.8 ";
6645 TheXor->dump(&DAG);
6646 dbgs() << "\nWith: ";
6647 Tmp.getNode()->dump(&DAG);
6648 dbgs() << '\n');
6649 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006650 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
Evan Cheng2c755ba2010-02-27 07:36:59 +00006651 removeFromWorkList(TheXor);
6652 DAG.DeleteNode(TheXor);
6653 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6654 MVT::Other, Chain, Tmp, N2);
6655 }
6656 }
6657
6658 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
6659 bool Equal = false;
6660 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
6661 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
6662 Op0.getOpcode() == ISD::XOR) {
6663 TheXor = Op0.getNode();
6664 Equal = true;
6665 }
6666
Evan Cheng2a135ae2010-10-04 22:41:01 +00006667 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00006668 if (LegalTypes)
6669 SetCCVT = TLI.getSetCCResultType(SetCCVT);
6670 SDValue SetCC = DAG.getSetCC(TheXor->getDebugLoc(),
6671 SetCCVT,
6672 Op0, Op1,
6673 Equal ? ISD::SETEQ : ISD::SETNE);
6674 // Replace the uses of XOR with SETCC
6675 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006676 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Cheng2a135ae2010-10-04 22:41:01 +00006677 removeFromWorkList(N1.getNode());
6678 DAG.DeleteNode(N1.getNode());
Evan Cheng2c755ba2010-02-27 07:36:59 +00006679 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6680 MVT::Other, Chain, SetCC, N2);
6681 }
6682 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006683
Dan Gohman475871a2008-07-27 21:46:04 +00006684 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006685}
6686
Chris Lattner3ea0b472005-10-05 06:47:48 +00006687// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
6688//
Dan Gohman475871a2008-07-27 21:46:04 +00006689SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00006690 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006691 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00006692
Dan Gohmane0f06c72009-11-17 00:47:23 +00006693 // If N is a constant we could fold this into a fallthrough or unconditional
6694 // branch. However that doesn't happen very often in normal code, because
6695 // Instcombine/SimplifyCFG should have handled the available opportunities.
6696 // If we did this folding here, it would be necessary to update the
6697 // MachineBasicBlock CFG, which is awkward.
6698
Duncan Sands8eab8a22008-06-09 11:32:28 +00006699 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00006700 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006701 CondLHS, CondRHS, CC->get(), N->getDebugLoc(),
6702 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00006703 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00006704
Nate Begemane17daeb2005-10-05 21:43:42 +00006705 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00006706 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Owen Anderson825b72b2009-08-11 20:47:22 +00006707 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006708 N->getOperand(0), Simp.getOperand(2),
6709 Simp.getOperand(0), Simp.getOperand(1),
6710 N->getOperand(4));
6711
Dan Gohman475871a2008-07-27 21:46:04 +00006712 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006713}
6714
Evan Chengc4b527a2012-01-13 01:37:24 +00006715/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
6716/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng03be3622012-03-06 23:33:32 +00006717/// addressing mode.
Evan Chengc4b527a2012-01-13 01:37:24 +00006718static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
6719 SelectionDAG &DAG,
6720 const TargetLowering &TLI) {
6721 EVT VT;
6722 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
6723 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
6724 return false;
6725 VT = Use->getValueType(0);
6726 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
6727 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
6728 return false;
6729 VT = ST->getValue().getValueType();
6730 } else
6731 return false;
6732
6733 TargetLowering::AddrMode AM;
6734 if (N->getOpcode() == ISD::ADD) {
6735 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6736 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00006737 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00006738 AM.BaseOffs = Offset->getSExtValue();
6739 else
Evan Cheng03be3622012-03-06 23:33:32 +00006740 // [reg +/- reg]
6741 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00006742 } else if (N->getOpcode() == ISD::SUB) {
6743 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6744 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00006745 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00006746 AM.BaseOffs = -Offset->getSExtValue();
6747 else
Evan Cheng03be3622012-03-06 23:33:32 +00006748 // [reg +/- reg]
6749 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00006750 } else
6751 return false;
6752
6753 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
6754}
6755
Duncan Sandsec87aa82008-06-15 20:12:31 +00006756/// CombineToPreIndexedLoadStore - Try turning a load / store into a
6757/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00006758/// and it has other uses besides the load / store. After the
6759/// transformation, the new indexed load / store has effectively folded
6760/// the add / subtract in and all of its other uses are redirected to the
6761/// new load / store.
6762bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00006763 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00006764 return false;
6765
6766 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006767 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00006768 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00006769 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006770 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006771 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006772 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00006773 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00006774 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
6775 return false;
6776 Ptr = LD->getBasePtr();
6777 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006778 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006779 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006780 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006781 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
6782 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
6783 return false;
6784 Ptr = ST->getBasePtr();
6785 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006786 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00006787 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006788 }
Chris Lattner448f2192006-11-11 00:39:41 +00006789
Chris Lattner9f1794e2006-11-11 00:56:29 +00006790 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
6791 // out. There is no reason to make this a preinc/predec.
6792 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00006793 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00006794 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00006795
Chris Lattner9f1794e2006-11-11 00:56:29 +00006796 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00006797 SDValue BasePtr;
6798 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006799 ISD::MemIndexedMode AM = ISD::UNINDEXED;
6800 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
6801 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00006802 // Don't create a indexed load / store with zero offset.
6803 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00006804 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00006805 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006806
Chris Lattner41e53fd2006-11-11 01:00:15 +00006807 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00006808 // 1) The new base ptr is a frame index.
6809 // 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 +00006810 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00006811 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00006812 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00006813 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00006814
Chris Lattner41e53fd2006-11-11 01:00:15 +00006815 // Check #1. Preinc'ing a frame index would require copying the stack pointer
6816 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00006817 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00006818 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006819
Chris Lattner41e53fd2006-11-11 01:00:15 +00006820 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00006821 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00006822 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006823 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006824 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00006825 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006826
Evan Chengc843abe2007-05-24 02:35:39 +00006827 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00006828 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00006829
6830 // Caches for hasPredecessorHelper
6831 SmallPtrSet<const SDNode *, 32> Visited;
6832 SmallVector<const SDNode *, 16> Worklist;
6833
Gabor Greifba36cb52008-08-28 21:40:38 +00006834 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
6835 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006836 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006837 if (Use == N)
6838 continue;
Lang Hames944520f2011-07-07 04:31:51 +00006839 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006840 return false;
6841
Evan Chengc4b527a2012-01-13 01:37:24 +00006842 // If Ptr may be folded in addressing mode of other use, then it's
6843 // not profitable to do this transformation.
6844 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006845 RealUse = true;
6846 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006847
Chris Lattner9f1794e2006-11-11 00:56:29 +00006848 if (!RealUse)
6849 return false;
6850
Dan Gohman475871a2008-07-27 21:46:04 +00006851 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006852 if (isLoad)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006853 Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
6854 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006855 else
Bill Wendlingc0debad2009-01-30 23:27:35 +00006856 Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
6857 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006858 ++PreIndexedNodes;
6859 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00006860 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006861 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006862 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006863 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006864 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006865 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006866 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006867 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
6868 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00006869 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006870 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner9f1794e2006-11-11 00:56:29 +00006871 }
6872
Chris Lattner9f1794e2006-11-11 00:56:29 +00006873 // Finally, since the node is now dead, remove it from the graph.
6874 DAG.DeleteNode(N);
6875
6876 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006877 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greifba36cb52008-08-28 21:40:38 +00006878 removeFromWorkList(Ptr.getNode());
6879 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00006880
6881 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00006882}
6883
Duncan Sandsec87aa82008-06-15 20:12:31 +00006884/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00006885/// add / sub of the base pointer node into a post-indexed load / store.
6886/// The transformation folded the add / subtract into the new indexed
6887/// load / store effectively and all of its uses are redirected to the
6888/// new load / store.
6889bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00006890 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00006891 return false;
6892
6893 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006894 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00006895 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00006896 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006897 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006898 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006899 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006900 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
6901 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
6902 return false;
6903 Ptr = LD->getBasePtr();
6904 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006905 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006906 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006907 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006908 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
6909 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
6910 return false;
6911 Ptr = ST->getBasePtr();
6912 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006913 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00006914 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006915 }
Chris Lattner448f2192006-11-11 00:39:41 +00006916
Gabor Greifba36cb52008-08-28 21:40:38 +00006917 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00006918 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006919
Gabor Greifba36cb52008-08-28 21:40:38 +00006920 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
6921 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006922 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006923 if (Op == N ||
6924 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
6925 continue;
6926
Dan Gohman475871a2008-07-27 21:46:04 +00006927 SDValue BasePtr;
6928 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006929 ISD::MemIndexedMode AM = ISD::UNINDEXED;
6930 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00006931 // Don't create a indexed load / store with zero offset.
6932 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00006933 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00006934 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00006935
Chris Lattner9f1794e2006-11-11 00:56:29 +00006936 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00006937 // 1) All uses are load / store ops that use it as base ptr (and
6938 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00006939 // 2) Op must be independent of N, i.e. Op is neither a predecessor
6940 // nor a successor of N. Otherwise, if Op is folded that would
6941 // create a cycle.
6942
Evan Chengcaab1292009-05-06 18:25:01 +00006943 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
6944 continue;
6945
Chris Lattner9f1794e2006-11-11 00:56:29 +00006946 // Check for #1.
6947 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00006948 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
6949 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00006950 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00006951 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00006952 continue;
6953
Chris Lattner9f1794e2006-11-11 00:56:29 +00006954 // If all the uses are load / store addresses, then don't do the
6955 // transformation.
6956 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
6957 bool RealUse = false;
6958 for (SDNode::use_iterator III = Use->use_begin(),
6959 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00006960 SDNode *UseUse = *III;
Evan Chengc4b527a2012-01-13 01:37:24 +00006961 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006962 RealUse = true;
6963 }
Chris Lattner448f2192006-11-11 00:39:41 +00006964
Chris Lattner9f1794e2006-11-11 00:56:29 +00006965 if (!RealUse) {
6966 TryNext = true;
6967 break;
Chris Lattner448f2192006-11-11 00:39:41 +00006968 }
6969 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006970 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006971
Chris Lattner9f1794e2006-11-11 00:56:29 +00006972 if (TryNext)
6973 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00006974
Chris Lattner9f1794e2006-11-11 00:56:29 +00006975 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00006976 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006977 SDValue Result = isLoad
Bill Wendlingc0debad2009-01-30 23:27:35 +00006978 ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
6979 BasePtr, Offset, AM)
6980 : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
6981 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006982 ++PostIndexedNodes;
6983 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00006984 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006985 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006986 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006987 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006988 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006989 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006990 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006991 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
6992 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00006993 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006994 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner448f2192006-11-11 00:39:41 +00006995 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006996
Chris Lattner9f1794e2006-11-11 00:56:29 +00006997 // Finally, since the node is now dead, remove it from the graph.
6998 DAG.DeleteNode(N);
6999
7000 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00007001 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007002 Result.getValue(isLoad ? 1 : 0));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007003 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007004 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007005 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007006 }
7007 }
7008 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007009
Chris Lattner448f2192006-11-11 00:39:41 +00007010 return false;
7011}
7012
Dan Gohman475871a2008-07-27 21:46:04 +00007013SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00007014 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007015 SDValue Chain = LD->getChain();
7016 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007017
Evan Cheng45a7ca92007-05-01 00:38:21 +00007018 // If load is not volatile and there are no uses of the loaded value (and
7019 // the updated indexed value in case of indexed loads), change uses of the
7020 // chain value into uses of the chain input (i.e. delete the dead load).
7021 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007022 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00007023 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00007024 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00007025 // It's not safe to use the two value CombineTo variant here. e.g.
7026 // v1, chain2 = load chain1, loc
7027 // v2, chain3 = load chain2, loc
7028 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00007029 // Now we replace use of chain2 with chain1. This makes the second load
7030 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00007031 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007032 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007033 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007034 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007035 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007036 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007037 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendlingc0debad2009-01-30 23:27:35 +00007038
Chris Lattner125991a2008-01-24 07:57:06 +00007039 if (N->use_empty()) {
7040 removeFromWorkList(N);
7041 DAG.DeleteNode(N);
7042 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007043
Dan Gohman475871a2008-07-27 21:46:04 +00007044 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00007045 }
Evan Cheng498f5592007-05-01 08:53:39 +00007046 } else {
7047 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00007048 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00007049 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007050 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00007051 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007052 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007053 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007054 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007055 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007056 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007057 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman475871a2008-07-27 21:46:04 +00007058 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007059 DAG.getUNDEF(N->getValueType(1)));
7060 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng02c42852008-01-16 23:11:54 +00007061 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00007062 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007063 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00007064 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00007065 }
7066 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007067
Chris Lattner01a22022005-10-10 22:04:48 +00007068 // If this load is directly stored, replace the load value with the stored
7069 // value.
7070 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007071 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00007072 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007073 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007074 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7075 if (PrevST->getBasePtr() == Ptr &&
7076 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007077 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00007078 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007079 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007080
Evan Cheng255f20f2010-04-01 06:04:33 +00007081 // Try to infer better alignment information than the load already has.
7082 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007083 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
7084 if (Align > LD->getAlignment())
7085 return DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(),
7086 LD->getValueType(0),
7087 Chain, Ptr, LD->getPointerInfo(),
7088 LD->getMemoryVT(),
7089 LD->isVolatile(), LD->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00007090 }
7091 }
7092
Jim Laskey7ca56af2006-10-11 13:47:09 +00007093 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007094 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007095 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007096
Jim Laskey6ff23e52006-10-04 16:53:27 +00007097 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007098 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007099 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007100
Jim Laskey279f0532006-09-25 16:29:54 +00007101 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007102 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Bill Wendlingc0debad2009-01-30 23:27:35 +00007103 ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(),
Chris Lattnerfa459012010-09-21 16:08:50 +00007104 BetterChain, Ptr, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007105 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007106 LD->isInvariant(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007107 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +00007108 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(),
7109 LD->getValueType(0),
Chris Lattnerfa459012010-09-21 16:08:50 +00007110 BetterChain, Ptr, LD->getPointerInfo(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007111 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007112 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007113 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00007114 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007115 }
Jim Laskey279f0532006-09-25 16:29:54 +00007116
Jim Laskey6ff23e52006-10-04 16:53:27 +00007117 // Create token factor to keep old chain connected.
Bill Wendlingc0debad2009-01-30 23:27:35 +00007118 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007119 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007120
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007121 // Make sure the new and old chains are cleaned up.
7122 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007123
Jim Laskey274062c2006-10-13 23:32:28 +00007124 // Replace uses with load result and token factor. Don't add users
7125 // to work list.
7126 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007127 }
7128 }
7129
Evan Cheng7fc033a2006-11-03 03:06:21 +00007130 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007131 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007132 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00007133
Dan Gohman475871a2008-07-27 21:46:04 +00007134 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00007135}
7136
Chris Lattner2392ae72010-04-15 04:48:01 +00007137/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
7138/// load is having specific bytes cleared out. If so, return the byte size
7139/// being masked out and the shift amount.
7140static std::pair<unsigned, unsigned>
7141CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
7142 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007143
Chris Lattner2392ae72010-04-15 04:48:01 +00007144 // Check for the structure we're looking for.
7145 if (V->getOpcode() != ISD::AND ||
7146 !isa<ConstantSDNode>(V->getOperand(1)) ||
7147 !ISD::isNormalLoad(V->getOperand(0).getNode()))
7148 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007149
Chris Lattnere6987582010-04-15 06:10:49 +00007150 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00007151 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00007152 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007153
Chris Lattnere6987582010-04-15 06:10:49 +00007154 // The store should be chained directly to the load or be an operand of a
7155 // tokenfactor.
7156 if (LD == Chain.getNode())
7157 ; // ok.
7158 else if (Chain->getOpcode() != ISD::TokenFactor)
7159 return Result; // Fail.
7160 else {
7161 bool isOk = false;
7162 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
7163 if (Chain->getOperand(i).getNode() == LD) {
7164 isOk = true;
7165 break;
7166 }
7167 if (!isOk) return Result;
7168 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007169
Chris Lattner2392ae72010-04-15 04:48:01 +00007170 // This only handles simple types.
7171 if (V.getValueType() != MVT::i16 &&
7172 V.getValueType() != MVT::i32 &&
7173 V.getValueType() != MVT::i64)
7174 return Result;
7175
7176 // Check the constant mask. Invert it so that the bits being masked out are
7177 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
7178 // follow the sign bit for uniformity.
7179 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
7180 unsigned NotMaskLZ = CountLeadingZeros_64(NotMask);
7181 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
7182 unsigned NotMaskTZ = CountTrailingZeros_64(NotMask);
7183 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
7184 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007185
Chris Lattner2392ae72010-04-15 04:48:01 +00007186 // See if we have a continuous run of bits. If so, we have 0*1+0*
7187 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
7188 return Result;
7189
7190 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
7191 if (V.getValueType() != MVT::i64 && NotMaskLZ)
7192 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007193
Chris Lattner2392ae72010-04-15 04:48:01 +00007194 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
7195 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007196 case 1:
7197 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00007198 case 4: break;
7199 default: return Result; // All one mask, or 5-byte mask.
7200 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007201
Chris Lattner2392ae72010-04-15 04:48:01 +00007202 // Verify that the first bit starts at a multiple of mask so that the access
7203 // is aligned the same as the access width.
7204 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007205
Chris Lattner2392ae72010-04-15 04:48:01 +00007206 Result.first = MaskedBytes;
7207 Result.second = NotMaskTZ/8;
7208 return Result;
7209}
7210
7211
7212/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
7213/// provides a value as specified by MaskInfo. If so, replace the specified
7214/// store with a narrower store of truncated IVal.
7215static SDNode *
7216ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
7217 SDValue IVal, StoreSDNode *St,
7218 DAGCombiner *DC) {
7219 unsigned NumBytes = MaskInfo.first;
7220 unsigned ByteShift = MaskInfo.second;
7221 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007222
Chris Lattner2392ae72010-04-15 04:48:01 +00007223 // Check to see if IVal is all zeros in the part being masked in by the 'or'
7224 // that uses this. If not, this is not a replacement.
7225 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
7226 ByteShift*8, (ByteShift+NumBytes)*8);
7227 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007228
Chris Lattner2392ae72010-04-15 04:48:01 +00007229 // Check that it is legal on the target to do this. It is legal if the new
7230 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
7231 // legalization.
7232 MVT VT = MVT::getIntegerVT(NumBytes*8);
7233 if (!DC->isTypeLegal(VT))
7234 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007235
Chris Lattner2392ae72010-04-15 04:48:01 +00007236 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
7237 // shifted by ByteShift and truncated down to NumBytes.
7238 if (ByteShift)
7239 IVal = DAG.getNode(ISD::SRL, IVal->getDebugLoc(), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00007240 DAG.getConstant(ByteShift*8,
7241 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00007242
7243 // Figure out the offset for the store and the alignment of the access.
7244 unsigned StOffset;
7245 unsigned NewAlign = St->getAlignment();
7246
7247 if (DAG.getTargetLoweringInfo().isLittleEndian())
7248 StOffset = ByteShift;
7249 else
7250 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007251
Chris Lattner2392ae72010-04-15 04:48:01 +00007252 SDValue Ptr = St->getBasePtr();
7253 if (StOffset) {
7254 Ptr = DAG.getNode(ISD::ADD, IVal->getDebugLoc(), Ptr.getValueType(),
7255 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
7256 NewAlign = MinAlign(NewAlign, StOffset);
7257 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007258
Chris Lattner2392ae72010-04-15 04:48:01 +00007259 // Truncate down to the new size.
7260 IVal = DAG.getNode(ISD::TRUNCATE, IVal->getDebugLoc(), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007261
Chris Lattner2392ae72010-04-15 04:48:01 +00007262 ++OpsNarrowed;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007263 return DAG.getStore(St->getChain(), St->getDebugLoc(), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007264 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00007265 false, false, NewAlign).getNode();
7266}
7267
Evan Cheng8b944d32009-05-28 00:35:15 +00007268
7269/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
7270/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
7271/// of the loaded bits, try narrowing the load and store if it would end up
7272/// being a win for performance or code size.
7273SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
7274 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00007275 if (ST->isVolatile())
7276 return SDValue();
7277
Evan Cheng8b944d32009-05-28 00:35:15 +00007278 SDValue Chain = ST->getChain();
7279 SDValue Value = ST->getValue();
7280 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00007281 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00007282
7283 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00007284 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007285
7286 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007287
Chris Lattner2392ae72010-04-15 04:48:01 +00007288 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
7289 // is a byte mask indicating a consecutive number of bytes, check to see if
7290 // Y is known to provide just those bytes. If so, we try to replace the
7291 // load + replace + store sequence with a single (narrower) store, which makes
7292 // the load dead.
7293 if (Opc == ISD::OR) {
7294 std::pair<unsigned, unsigned> MaskedLoad;
7295 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
7296 if (MaskedLoad.first)
7297 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7298 Value.getOperand(1), ST,this))
7299 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007300
Chris Lattner2392ae72010-04-15 04:48:01 +00007301 // Or is commutative, so try swapping X and Y.
7302 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
7303 if (MaskedLoad.first)
7304 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7305 Value.getOperand(0), ST,this))
7306 return SDValue(NewST, 0);
7307 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007308
Evan Cheng8b944d32009-05-28 00:35:15 +00007309 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
7310 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00007311 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007312
7313 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00007314 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
7315 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00007316 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00007317 if (LD->getBasePtr() != Ptr ||
7318 LD->getPointerInfo().getAddrSpace() !=
7319 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00007320 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007321
7322 // Find the type to narrow it the load / op / store to.
7323 SDValue N1 = Value.getOperand(1);
7324 unsigned BitWidth = N1.getValueSizeInBits();
7325 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
7326 if (Opc == ISD::AND)
7327 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00007328 if (Imm == 0 || Imm.isAllOnesValue())
7329 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007330 unsigned ShAmt = Imm.countTrailingZeros();
7331 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
7332 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00007333 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007334 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00007335 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00007336 TLI.isNarrowingProfitable(VT, NewVT))) {
7337 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00007338 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007339 }
Evan Chengcdcecc02009-05-28 18:41:02 +00007340 if (NewBW >= BitWidth)
7341 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007342
7343 // If the lsb changed does not start at the type bitwidth boundary,
7344 // start at the previous one.
7345 if (ShAmt % NewBW)
7346 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
7347 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, ShAmt + NewBW);
7348 if ((Imm & Mask) == Imm) {
7349 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
7350 if (Opc == ISD::AND)
7351 NewImm ^= APInt::getAllOnesValue(NewBW);
7352 uint64_t PtrOff = ShAmt / 8;
7353 // For big endian targets, we need to adjust the offset to the pointer to
7354 // load the correct bytes.
7355 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00007356 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00007357
7358 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007359 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Chris Lattner2392ae72010-04-15 04:48:01 +00007360 if (NewAlign < TLI.getTargetData()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00007361 return SDValue();
7362
Evan Cheng8b944d32009-05-28 00:35:15 +00007363 SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(),
7364 Ptr.getValueType(), Ptr,
7365 DAG.getConstant(PtrOff, Ptr.getValueType()));
7366 SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(),
7367 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007368 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007369 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007370 LD->isInvariant(), NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007371 SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD,
7372 DAG.getConstant(NewImm, NewVT));
7373 SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(),
7374 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007375 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007376 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007377
7378 AddToWorkList(NewPtr.getNode());
7379 AddToWorkList(NewLD.getNode());
7380 AddToWorkList(NewVal.getNode());
7381 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007382 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Cheng8b944d32009-05-28 00:35:15 +00007383 ++OpsNarrowed;
7384 return NewST;
7385 }
7386 }
7387
Evan Chengcdcecc02009-05-28 18:41:02 +00007388 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007389}
7390
Evan Cheng31959b12011-02-02 01:06:55 +00007391/// TransformFPLoadStorePair - For a given floating point load / store pair,
7392/// if the load value isn't used by any other operations, then consider
7393/// transforming the pair to integer load / store operations if the target
7394/// deems the transformation profitable.
7395SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
7396 StoreSDNode *ST = cast<StoreSDNode>(N);
7397 SDValue Chain = ST->getChain();
7398 SDValue Value = ST->getValue();
7399 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
7400 Value.hasOneUse() &&
7401 Chain == SDValue(Value.getNode(), 1)) {
7402 LoadSDNode *LD = cast<LoadSDNode>(Value);
7403 EVT VT = LD->getMemoryVT();
7404 if (!VT.isFloatingPoint() ||
7405 VT != ST->getMemoryVT() ||
7406 LD->isNonTemporal() ||
7407 ST->isNonTemporal() ||
7408 LD->getPointerInfo().getAddrSpace() != 0 ||
7409 ST->getPointerInfo().getAddrSpace() != 0)
7410 return SDValue();
7411
7412 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
7413 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
7414 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
7415 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
7416 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
7417 return SDValue();
7418
7419 unsigned LDAlign = LD->getAlignment();
7420 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007421 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Evan Cheng31959b12011-02-02 01:06:55 +00007422 unsigned ABIAlign = TLI.getTargetData()->getABITypeAlignment(IntVTTy);
7423 if (LDAlign < ABIAlign || STAlign < ABIAlign)
7424 return SDValue();
7425
7426 SDValue NewLD = DAG.getLoad(IntVT, Value.getDebugLoc(),
7427 LD->getChain(), LD->getBasePtr(),
7428 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007429 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00007430
7431 SDValue NewST = DAG.getStore(NewLD.getValue(1), N->getDebugLoc(),
7432 NewLD, ST->getBasePtr(),
7433 ST->getPointerInfo(),
7434 false, false, STAlign);
7435
7436 AddToWorkList(NewLD.getNode());
7437 AddToWorkList(NewST.getNode());
7438 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007439 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Cheng31959b12011-02-02 01:06:55 +00007440 ++LdStFP2Int;
7441 return NewST;
7442 }
7443
7444 return SDValue();
7445}
7446
Dan Gohman475871a2008-07-27 21:46:04 +00007447SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007448 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007449 SDValue Chain = ST->getChain();
7450 SDValue Value = ST->getValue();
7451 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007452
Evan Cheng59d5b682007-05-07 21:27:48 +00007453 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00007454 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007455 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00007456 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00007457 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007458 EVT SVT = Value.getOperand(0).getValueType();
Dan Gohman1ba519b2009-02-20 23:29:13 +00007459 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00007460 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007461 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00007462 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007463 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Bill Wendlingc144a572009-01-30 23:36:47 +00007464 return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00007465 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007466 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00007467 }
Owen Andersona34d9362011-04-14 17:30:49 +00007468
Chris Lattnerb3452ea2011-04-09 02:32:02 +00007469 // Turn 'store undef, Ptr' -> nothing.
7470 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
7471 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007472
Nate Begeman2cbba892006-12-11 02:23:46 +00007473 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00007474 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007475 // NOTE: If the original store is volatile, this transform must not increase
7476 // the number of stores. For example, on x86-32 an f64 can be stored in one
7477 // processor operation but an i64 (which is not legal) requires two. So the
7478 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00007479 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00007480 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007481 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007482 default: llvm_unreachable("Unknown FP type");
Pete Cooper438c0402012-06-21 18:00:39 +00007483 case MVT::f16: // We don't do this for these yet.
7484 case MVT::f80:
Owen Anderson825b72b2009-08-11 20:47:22 +00007485 case MVT::f128:
7486 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00007487 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007488 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00007489 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007490 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00007491 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00007492 bitcastToAPInt().getZExtValue(), MVT::i32);
Bill Wendlingc144a572009-01-30 23:36:47 +00007493 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007494 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007495 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00007496 }
7497 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007498 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00007499 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007500 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007501 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00007502 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00007503 getZExtValue(), MVT::i64);
Bill Wendlingc144a572009-01-30 23:36:47 +00007504 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007505 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007506 ST->isNonTemporal(), ST->getAlignment());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00007507 }
Owen Andersona34d9362011-04-14 17:30:49 +00007508
Chris Lattnerb3452ea2011-04-09 02:32:02 +00007509 if (!ST->isVolatile() &&
7510 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00007511 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00007512 // argument passing. Since this is so common, custom legalize the
7513 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00007514 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007515 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
7516 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00007517 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00007518
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00007519 unsigned Alignment = ST->getAlignment();
7520 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00007521 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00007522
Bill Wendlingc144a572009-01-30 23:36:47 +00007523 SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007524 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007525 isVolatile, isNonTemporal,
7526 ST->getAlignment());
Bill Wendlingc144a572009-01-30 23:36:47 +00007527 Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00007528 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00007529 Alignment = MinAlign(Alignment, 4U);
Bill Wendlingc144a572009-01-30 23:36:47 +00007530 SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007531 Ptr, ST->getPointerInfo().getWithOffset(4),
7532 isVolatile, isNonTemporal,
David Greene1e559442010-02-15 17:00:31 +00007533 Alignment);
Owen Anderson825b72b2009-08-11 20:47:22 +00007534 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00007535 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00007536 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007537
Chris Lattner62be1a72006-12-12 04:16:14 +00007538 break;
Evan Cheng25ece662006-12-11 17:25:19 +00007539 }
Nate Begeman2cbba892006-12-11 02:23:46 +00007540 }
Nate Begeman2cbba892006-12-11 02:23:46 +00007541 }
7542
Evan Cheng255f20f2010-04-01 06:04:33 +00007543 // Try to infer better alignment information than the store already has.
7544 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007545 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
7546 if (Align > ST->getAlignment())
7547 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value,
7548 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
7549 ST->isVolatile(), ST->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00007550 }
7551 }
7552
Evan Cheng31959b12011-02-02 01:06:55 +00007553 // Try transforming a pair floating point load / store ops to integer
7554 // load / store ops.
7555 SDValue NewST = TransformFPLoadStorePair(N);
7556 if (NewST.getNode())
7557 return NewST;
7558
Scott Michelfdc40a02009-02-17 22:15:04 +00007559 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007560 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007561 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007562
Jim Laskey6ff23e52006-10-04 16:53:27 +00007563 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007564 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007565 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007566
7567 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00007568 if (ST->isTruncatingStore()) {
Bill Wendlingc144a572009-01-30 23:36:47 +00007569 ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00007570 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007571 ST->getMemoryVT(), ST->isVolatile(),
7572 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00007573 } else {
Bill Wendlingc144a572009-01-30 23:36:47 +00007574 ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007575 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007576 ST->isVolatile(), ST->isNonTemporal(),
7577 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00007578 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007579
Jim Laskey279f0532006-09-25 16:29:54 +00007580 // Create token to keep both nodes around.
Bill Wendlingc144a572009-01-30 23:36:47 +00007581 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007582 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00007583
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007584 // Make sure the new and old chains are cleaned up.
7585 AddToWorkList(Token.getNode());
7586
Jim Laskey274062c2006-10-13 23:32:28 +00007587 // Don't add users to work list.
7588 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007589 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00007590 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007591
Evan Cheng33dbedc2006-11-05 09:31:14 +00007592 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007593 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007594 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00007595
Chris Lattner3c872852007-12-29 06:26:16 +00007596 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00007597 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00007598 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00007599 // See if we can simplify the input to this truncstore with knowledge that
7600 // only the low bits are being used. For example:
7601 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00007602 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007603 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00007604 APInt::getLowBitsSet(
7605 Value.getValueType().getScalarType().getSizeInBits(),
7606 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00007607 AddToWorkList(Value.getNode());
7608 if (Shorter.getNode())
Bill Wendlingc144a572009-01-30 23:36:47 +00007609 return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00007610 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00007611 ST->isVolatile(), ST->isNonTemporal(),
7612 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00007613
Chris Lattnere33544c2007-10-13 06:58:48 +00007614 // Otherwise, see if we can simplify the operation with
7615 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00007616 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00007617 APInt::getLowBitsSet(
7618 Value.getValueType().getScalarType().getSizeInBits(),
7619 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00007620 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00007621 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007622
Chris Lattner3c872852007-12-29 06:26:16 +00007623 // If this is a load followed by a store to the same location, then the store
7624 // is dead/noop.
7625 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007626 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00007627 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00007628 // There can't be any side effects between the load and store, such as
7629 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00007630 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00007631 // The store is dead, remove it.
7632 return Chain;
7633 }
7634 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007635
Chris Lattnerddf89562008-01-17 19:59:44 +00007636 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
7637 // truncating store. We can do this even if this is already a truncstore.
7638 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00007639 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00007640 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007641 ST->getMemoryVT())) {
Bill Wendlingc144a572009-01-30 23:36:47 +00007642 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0),
Chris Lattnerda2d8e12010-09-21 17:42:31 +00007643 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00007644 ST->isVolatile(), ST->isNonTemporal(),
7645 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00007646 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007647
Evan Cheng8b944d32009-05-28 00:35:15 +00007648 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00007649}
7650
Dan Gohman475871a2008-07-27 21:46:04 +00007651SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
7652 SDValue InVec = N->getOperand(0);
7653 SDValue InVal = N->getOperand(1);
7654 SDValue EltNo = N->getOperand(2);
Eli Friedman9db817f2011-09-09 21:04:06 +00007655 DebugLoc dl = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007656
Bob Wilson492fd452010-05-19 23:42:58 +00007657 // If the inserted element is an UNDEF, just use the input vector.
7658 if (InVal.getOpcode() == ISD::UNDEF)
7659 return InVec;
7660
Nadav Rotem609d54e2011-02-12 14:40:33 +00007661 EVT VT = InVec.getValueType();
7662
Owen Anderson95771af2011-02-25 21:41:48 +00007663 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00007664 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
7665 return SDValue();
7666
Eli Friedman9db817f2011-09-09 21:04:06 +00007667 // Check that we know which element is being inserted
7668 if (!isa<ConstantSDNode>(EltNo))
7669 return SDValue();
7670 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007671
Eli Friedman9db817f2011-09-09 21:04:06 +00007672 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
7673 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
7674 // vector elements.
7675 SmallVector<SDValue, 8> Ops;
7676 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
7677 Ops.append(InVec.getNode()->op_begin(),
7678 InVec.getNode()->op_end());
7679 } else if (InVec.getOpcode() == ISD::UNDEF) {
7680 unsigned NElts = VT.getVectorNumElements();
7681 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
7682 } else {
7683 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00007684 }
Eli Friedman9db817f2011-09-09 21:04:06 +00007685
7686 // Insert the element
7687 if (Elt < Ops.size()) {
7688 // All the operands of BUILD_VECTOR must have the same type;
7689 // we enforce that here.
7690 EVT OpVT = Ops[0].getValueType();
7691 if (InVal.getValueType() != OpVT)
7692 InVal = OpVT.bitsGT(InVal.getValueType()) ?
7693 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
7694 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
7695 Ops[Elt] = InVal;
7696 }
7697
7698 // Return the new vector
7699 return DAG.getNode(ISD::BUILD_VECTOR, dl,
7700 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00007701}
7702
Dan Gohman475871a2008-07-27 21:46:04 +00007703SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00007704 // (vextract (scalar_to_vector val, 0) -> val
7705 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00007706 EVT VT = InVec.getValueType();
7707 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00007708
Duncan Sandsc356f332011-05-09 08:03:33 +00007709 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
7710 // Check if the result type doesn't match the inserted element type. A
7711 // SCALAR_TO_VECTOR may truncate the inserted element and the
7712 // EXTRACT_VECTOR_ELT may widen the extracted vector.
7713 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00007714 if (InOp.getValueType() != NVT) {
7715 assert(InOp.getValueType().isInteger() && NVT.isInteger());
7716 return DAG.getSExtOrTrunc(InOp, InVec.getDebugLoc(), NVT);
7717 }
7718 return InOp;
7719 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007720
Nadav Rotemba05c912012-01-17 21:44:01 +00007721 SDValue EltNo = N->getOperand(1);
7722 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
7723
7724 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
7725 // We only perform this optimization before the op legalization phase because
7726 // we may introduce new vector instructions which are not backed by TD patterns.
7727 // For example on AVX, extracting elements from a wide vector without using
7728 // extract_subvector.
7729 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
7730 && ConstEltNo && !LegalOperations) {
7731 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
7732 int NumElem = VT.getVectorNumElements();
7733 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
7734 // Find the new index to extract from.
7735 int OrigElt = SVOp->getMaskElt(Elt);
7736
7737 // Extracting an undef index is undef.
7738 if (OrigElt == -1)
7739 return DAG.getUNDEF(NVT);
7740
7741 // Select the right vector half to extract from.
7742 if (OrigElt < NumElem) {
7743 InVec = InVec->getOperand(0);
7744 } else {
7745 InVec = InVec->getOperand(1);
7746 OrigElt -= NumElem;
7747 }
7748
Jim Grosbacha249f7d2012-05-08 20:56:07 +00007749 EVT IndexTy = N->getOperand(1).getValueType();
Nadav Rotemba05c912012-01-17 21:44:01 +00007750 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(), NVT,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00007751 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemba05c912012-01-17 21:44:01 +00007752 }
7753
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007754 // Perform only after legalization to ensure build_vector / vector_shuffle
7755 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00007756 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007757
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00007758 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
7759 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
7760 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00007761
Nadav Rotemba05c912012-01-17 21:44:01 +00007762 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00007763 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00007764 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00007765 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00007766 EVT ExtVT = VT.getVectorElementType();
7767 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00007768
Evan Cheng84387ea2012-03-13 22:00:52 +00007769 // If the result of load has to be truncated, then it's not necessarily
7770 // profitable.
Evan Chenga03d3662012-03-13 22:16:11 +00007771 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng84387ea2012-03-13 22:00:52 +00007772 return SDValue();
7773
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007774 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00007775 // Don't duplicate a load with other uses.
7776 if (!InVec.hasOneUse())
7777 return SDValue();
7778
Owen Andersone50ed302009-08-10 22:56:29 +00007779 EVT BCVT = InVec.getOperand(0).getValueType();
7780 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007781 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00007782 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
7783 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007784 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00007785 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007786 NewLoad = true;
7787 }
Evan Cheng513da432007-10-06 08:19:55 +00007788
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007789 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00007790 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00007791 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007792 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00007793 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00007794 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00007795 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00007796 // Don't duplicate a load with other uses.
7797 if (!InVec.hasOneUse())
7798 return SDValue();
7799
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007800 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00007801 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007802 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
7803 // =>
7804 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00007805
Eli Friedmand6e25602011-12-26 22:49:32 +00007806 // Don't duplicate a load with other uses.
7807 if (!InVec.hasOneUse())
7808 return SDValue();
7809
Mon P Wanga60b5232008-12-11 00:26:16 +00007810 // If the bit convert changed the number of elements, it is unsafe
7811 // to examine the mask.
7812 if (BCNumEltsChanged)
7813 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00007814
7815 // Select the input vector, guarding against out of range extract vector.
7816 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00007817 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00007818 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
7819
Eli Friedmand6e25602011-12-26 22:49:32 +00007820 if (InVec.getOpcode() == ISD::BITCAST) {
7821 // Don't duplicate a load with other uses.
7822 if (!InVec.hasOneUse())
7823 return SDValue();
7824
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007825 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00007826 }
Gabor Greifba36cb52008-08-28 21:40:38 +00007827 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007828 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00007829 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00007830 }
7831 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007832
Eli Friedmand6e25602011-12-26 22:49:32 +00007833 // Make sure we found a non-volatile load and the extractelement is
7834 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00007835 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00007836 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007837
Eric Christopherd81f17a2010-11-03 20:44:42 +00007838 // If Idx was -1 above, Elt is going to be -1, so just return undef.
7839 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00007840 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00007841
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007842 unsigned Align = LN0->getAlignment();
7843 if (NewLoad) {
7844 // Check the resultant load doesn't need a higher alignment than the
7845 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00007846 unsigned NewAlign =
Eric Christopher503a64d2010-12-09 04:48:06 +00007847 TLI.getTargetData()
7848 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00007849
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007850 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00007851 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00007852
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007853 Align = NewAlign;
7854 }
7855
Dan Gohman475871a2008-07-27 21:46:04 +00007856 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00007857 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007858
Eric Christopherd81f17a2010-11-03 20:44:42 +00007859 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00007860 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007861 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007862 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007863 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Bill Wendlingc144a572009-01-30 23:36:47 +00007864 NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007865 DAG.getConstant(PtrOff, PtrType));
7866 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007867
Eli Friedman4db4add2011-11-16 23:50:22 +00007868 // The replacement we need to do here is a little tricky: we need to
7869 // replace an extractelement of a load with a load.
7870 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00007871 // Note that this replacement assumes that the extractvalue is the only
7872 // use of the load; that's okay because we don't want to perform this
7873 // transformation in other cases anyway.
Evan Cheng84387ea2012-03-13 22:00:52 +00007874 SDValue Load;
Evan Chenga03d3662012-03-13 22:16:11 +00007875 SDValue Chain;
Evan Cheng84387ea2012-03-13 22:00:52 +00007876 if (NVT.bitsGT(LVT)) {
7877 // If the result type of vextract is wider than the load, then issue an
7878 // extending load instead.
7879 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
7880 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
7881 Load = DAG.getExtLoad(ExtType, N->getDebugLoc(), NVT, LN0->getChain(),
7882 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
7883 LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align);
Evan Chenga03d3662012-03-13 22:16:11 +00007884 Chain = Load.getValue(1);
7885 } else {
Evan Cheng84387ea2012-03-13 22:00:52 +00007886 Load = DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr,
7887 LN0->getPointerInfo().getWithOffset(PtrOff),
7888 LN0->isVolatile(), LN0->isNonTemporal(),
7889 LN0->isInvariant(), Align);
Evan Chenga03d3662012-03-13 22:16:11 +00007890 Chain = Load.getValue(1);
7891 if (NVT.bitsLT(LVT))
7892 Load = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), NVT, Load);
7893 else
7894 Load = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), NVT, Load);
7895 }
Eli Friedman4db4add2011-11-16 23:50:22 +00007896 WorkListRemover DeadNodes(*this);
7897 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chenga03d3662012-03-13 22:16:11 +00007898 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007899 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedman4db4add2011-11-16 23:50:22 +00007900 // Since we're explcitly calling ReplaceAllUses, add the new node to the
7901 // worklist explicitly as well.
7902 AddToWorkList(Load.getNode());
Craig Topper0c9da212012-03-20 05:28:39 +00007903 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedman4db4add2011-11-16 23:50:22 +00007904 // Make sure to revisit this node to clean it up; it will usually be dead.
7905 AddToWorkList(N);
7906 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00007907 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007908
Dan Gohman475871a2008-07-27 21:46:04 +00007909 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00007910}
Evan Cheng513da432007-10-06 08:19:55 +00007911
Dan Gohman475871a2008-07-27 21:46:04 +00007912SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00007913 unsigned NumInScalars = N->getNumOperands();
Nadav Rotemb00418a2011-10-29 21:23:04 +00007914 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007915 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00007916
7917 // A vector built entirely of undefs is undef.
7918 if (ISD::allOperandsUndef(N))
7919 return DAG.getUNDEF(VT);
7920
Nadav Rotemb00418a2011-10-29 21:23:04 +00007921 // Check to see if this is a BUILD_VECTOR of a bunch of values
7922 // which come from any_extend or zero_extend nodes. If so, we can create
7923 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00007924 // optimizations. We do not handle sign-extend because we can't fill the sign
7925 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00007926 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00007927 bool AllAnyExt = true;
Nadav Rotemb87bdac2012-07-15 08:38:23 +00007928
Craig Topperd3b58892012-01-17 09:09:48 +00007929 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00007930 SDValue In = N->getOperand(i);
7931 // Ignore undef inputs.
7932 if (In.getOpcode() == ISD::UNDEF) continue;
7933
7934 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
7935 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
7936
Nadav Rotemf47368b2011-10-31 20:08:25 +00007937 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00007938 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00007939 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00007940 break;
7941 }
7942
7943 // The input is a ZeroExt or AnyExt. Check the original type.
7944 EVT InTy = In.getOperand(0).getValueType();
7945
7946 // Check that all of the widened source types are the same.
7947 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00007948 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00007949 SourceType = InTy;
7950 else if (InTy != SourceType) {
7951 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00007952 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00007953 break;
7954 }
7955
7956 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00007957 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00007958 }
7959
Nadav Rotemf47368b2011-10-31 20:08:25 +00007960 // In order to have valid types, all of the inputs must be extended from the
7961 // same source type and all of the inputs must be any or zero extend.
7962 // Scalar sizes must be a power of two.
7963 EVT OutScalarTy = N->getValueType(0).getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00007964 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00007965 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
7966 isPowerOf2_32(SourceType.getSizeInBits());
7967
7968 // We perform this optimization post type-legalization because
7969 // the type-legalizer often scalarizes integer-promoted vectors.
7970 // Performing this optimization before may create bit-casts which
7971 // will be type-legalized to complex code sequences.
7972 // We perform this optimization only before the operation legalizer because we
7973 // may introduce illegal operations.
Nadav Rotem6431ff92012-03-15 08:49:06 +00007974 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
7975 // turn into a single shuffle instruction.
Nadav Rotem2ee746b2012-02-12 15:05:31 +00007976 if ((Level == AfterLegalizeVectorOps || Level == AfterLegalizeTypes) &&
7977 ValidTypes) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00007978 bool isLE = TLI.isLittleEndian();
Nadav Rotemf47368b2011-10-31 20:08:25 +00007979 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
Nadav Rotemb00418a2011-10-29 21:23:04 +00007980 assert(ElemRatio > 1 && "Invalid element size ratio");
Craig Topperd3b58892012-01-17 09:09:48 +00007981 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
Nadav Rotemf47368b2011-10-31 20:08:25 +00007982 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007983
7984 unsigned NewBVElems = ElemRatio * N->getValueType(0).getVectorNumElements();
Benjamin Kramer50bf86e2011-10-30 08:39:55 +00007985 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007986
7987 // Populate the new build_vector
7988 for (unsigned i=0; i < N->getNumOperands(); ++i) {
7989 SDValue Cast = N->getOperand(i);
Benjamin Kramer50bf86e2011-10-30 08:39:55 +00007990 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
7991 Cast.getOpcode() == ISD::ZERO_EXTEND ||
7992 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
Nadav Rotemb00418a2011-10-29 21:23:04 +00007993 SDValue In;
7994 if (Cast.getOpcode() == ISD::UNDEF)
Nadav Rotemf47368b2011-10-31 20:08:25 +00007995 In = DAG.getUNDEF(SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007996 else
7997 In = Cast->getOperand(0);
7998 unsigned Index = isLE ? (i * ElemRatio) :
7999 (i * ElemRatio + (ElemRatio - 1));
8000
8001 assert(Index < Ops.size() && "Invalid index");
8002 Ops[Index] = In;
8003 }
8004
8005 // The type of the new BUILD_VECTOR node.
Nadav Rotemf47368b2011-10-31 20:08:25 +00008006 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008007 assert(VecVT.getSizeInBits() == N->getValueType(0).getSizeInBits() &&
8008 "Invalid vector size");
Nadav Rotemf47368b2011-10-31 20:08:25 +00008009 // Check if the new vector type is legal.
8010 if (!isTypeLegal(VecVT)) return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00008011
8012 // Make the new BUILD_VECTOR.
8013 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
8014 VecVT, &Ops[0], Ops.size());
8015
Nadav Rotem6431ff92012-03-15 08:49:06 +00008016 // The new BUILD_VECTOR node has the potential to be further optimized.
8017 AddToWorkList(BV.getNode());
Nadav Rotemb00418a2011-10-29 21:23:04 +00008018 // Bitcast to the desired type.
8019 return DAG.getNode(ISD::BITCAST, dl, N->getValueType(0), BV);
8020 }
Chris Lattnerca242442006-03-19 01:27:56 +00008021
Dan Gohman7f321562007-06-25 16:23:39 +00008022 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
8023 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
8024 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands00294ca2012-03-19 15:35:44 +00008025
8026 // May only combine to shuffle after legalize if shuffle is legal.
8027 if (LegalOperations &&
8028 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
8029 return SDValue();
8030
Dan Gohman475871a2008-07-27 21:46:04 +00008031 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00008032 for (unsigned i = 0; i != NumInScalars; ++i) {
8033 // Ignore undef inputs.
8034 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00008035
Dan Gohman7f321562007-06-25 16:23:39 +00008036 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00008037 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00008038 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00008039 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00008040 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008041 break;
8042 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008043
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008044 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00008045 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008046 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
8047 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00008048
Gabor Greifba36cb52008-08-28 21:40:38 +00008049 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00008050 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00008051 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00008052 VecIn2 = ExtractedFromVec;
8053 } else {
8054 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008055 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008056 break;
8057 }
8058 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008059
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008060 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00008061 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008062 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00008063 for (unsigned i = 0; i != NumInScalars; ++i) {
8064 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008065 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008066 continue;
8067 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008068
Rafael Espindola15684b22009-04-24 12:40:33 +00008069 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00008070 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00008071 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008072 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00008073 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
8074 if (ExtIndex > VT.getVectorNumElements())
8075 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008076
Nate Begeman5a5ca152009-04-29 05:20:52 +00008077 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008078 continue;
8079 }
8080
8081 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00008082 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00008083 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008084 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008085
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008086 // We can't generate a shuffle node with mismatched input and output types.
8087 // Attempt to transform a single input vector to the correct type.
8088 if ((VT != VecIn1.getValueType())) {
8089 // We don't support shuffeling between TWO values of different types.
8090 if (VecIn2.getNode() != 0)
8091 return SDValue();
8092
8093 // We only support widening of vectors which are half the size of the
8094 // output registers. For example XMM->YMM widening on X86 with AVX.
8095 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
8096 return SDValue();
8097
James Molloy8cd08bf2012-09-10 14:01:21 +00008098 // If the input vector type has a different base type to the output
8099 // vector type, bail out.
8100 if (VecIn1.getValueType().getVectorElementType() !=
8101 VT.getVectorElementType())
8102 return SDValue();
8103
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00008104 // Widen the input vector by adding undef values.
8105 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8106 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008107 }
8108
8109 // If VecIn2 is unused then change it to undef.
8110 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
8111
Nadav Rotem0877fdf2012-02-13 12:42:26 +00008112 // Check that we were able to transform all incoming values to the same type.
8113 if (VecIn2.getValueType() != VecIn1.getValueType() ||
8114 VecIn1.getValueType() != VT)
8115 return SDValue();
8116
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008117 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00008118 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00008119 return SDValue();
8120
Dan Gohman7f321562007-06-25 16:23:39 +00008121 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00008122 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00008123 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008124 Ops[1] = VecIn2;
Nate Begeman9008ca62009-04-27 18:41:29 +00008125 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008126 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008127
Dan Gohman475871a2008-07-27 21:46:04 +00008128 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00008129}
8130
Dan Gohman475871a2008-07-27 21:46:04 +00008131SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00008132 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
8133 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
8134 // inputs come from at most two distinct vectors, turn this into a shuffle
8135 // node.
8136
8137 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00008138 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00008139 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00008140
Nadav Rotemb7e230d2012-07-14 21:30:27 +00008141 // Check if all of the operands are undefs.
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008142 if (ISD::allOperandsUndef(N))
Nadav Rotemb7e230d2012-07-14 21:30:27 +00008143 return DAG.getUNDEF(N->getValueType(0));
8144
Dan Gohman475871a2008-07-27 21:46:04 +00008145 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00008146}
8147
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00008148SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
8149 EVT NVT = N->getValueType(0);
8150 SDValue V = N->getOperand(0);
8151
8152 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
8153 // Handle only simple case where vector being inserted and vector
8154 // being extracted are of same type, and are half size of larger vectors.
8155 EVT BigVT = V->getOperand(0).getValueType();
8156 EVT SmallVT = V->getOperand(1).getValueType();
8157 if (NVT != SmallVT || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
8158 return SDValue();
8159
Eli Friedman26323442011-12-07 00:11:56 +00008160 // Only handle cases where both indexes are constants with the same type.
8161 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
8162 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00008163
Eli Friedman26323442011-12-07 00:11:56 +00008164 if (InsIdx && ExtIdx &&
8165 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
8166 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
8167 // Combine:
8168 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
8169 // Into:
8170 // indices are equal => V1
8171 // otherwise => (extract_subvec V1, ExtIdx)
8172 if (InsIdx->getZExtValue() == ExtIdx->getZExtValue())
8173 return V->getOperand(1);
8174 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, N->getDebugLoc(), NVT,
8175 V->getOperand(0), N->getOperand(1));
8176 }
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00008177 }
8178
8179 return SDValue();
8180}
8181
Dan Gohman475871a2008-07-27 21:46:04 +00008182SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00008183 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008184 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00008185
Mon P Wangaeb06d22008-11-10 04:46:22 +00008186 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +00008187 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +00008188
Craig Topperae1bec52012-04-09 05:16:56 +00008189 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wangaeb06d22008-11-10 04:46:22 +00008190
Craig Topper481b79c2012-01-04 08:07:43 +00008191 // Canonicalize shuffle undef, undef -> undef
8192 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
8193 return DAG.getUNDEF(VT);
8194
8195 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8196
8197 // Canonicalize shuffle v, v -> v, undef
8198 if (N0 == N1) {
8199 SmallVector<int, 8> NewMask;
8200 for (unsigned i = 0; i != NumElts; ++i) {
8201 int Idx = SVN->getMaskElt(i);
8202 if (Idx >= (int)NumElts) Idx -= NumElts;
8203 NewMask.push_back(Idx);
8204 }
8205 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, DAG.getUNDEF(VT),
8206 &NewMask[0]);
8207 }
8208
8209 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
8210 if (N0.getOpcode() == ISD::UNDEF) {
8211 SmallVector<int, 8> NewMask;
8212 for (unsigned i = 0; i != NumElts; ++i) {
8213 int Idx = SVN->getMaskElt(i);
Craig Topper4b206bd2012-04-09 05:55:33 +00008214 if (Idx >= 0) {
8215 if (Idx < (int)NumElts)
8216 Idx += NumElts;
8217 else
8218 Idx -= NumElts;
8219 }
8220 NewMask.push_back(Idx);
Craig Topper481b79c2012-01-04 08:07:43 +00008221 }
8222 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N1, DAG.getUNDEF(VT),
8223 &NewMask[0]);
8224 }
8225
8226 // Remove references to rhs if it is undef
8227 if (N1.getOpcode() == ISD::UNDEF) {
8228 bool Changed = false;
8229 SmallVector<int, 8> NewMask;
8230 for (unsigned i = 0; i != NumElts; ++i) {
8231 int Idx = SVN->getMaskElt(i);
8232 if (Idx >= (int)NumElts) {
8233 Idx = -1;
8234 Changed = true;
8235 }
8236 NewMask.push_back(Idx);
8237 }
8238 if (Changed)
8239 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, N1, &NewMask[0]);
8240 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00008241
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008242 // If it is a splat, check if the argument vector is another splat or a
8243 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008244 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008245 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00008246
Dan Gohman7f321562007-06-25 16:23:39 +00008247 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00008248 // not the number of vector elements, look through it. Be careful not to
8249 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008250 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +00008251 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00008252 if (ConvInput.getValueType().isVector() &&
8253 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00008254 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00008255 }
8256
Dan Gohman7f321562007-06-25 16:23:39 +00008257 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008258 assert(V->getNumOperands() == NumElts &&
8259 "BUILD_VECTOR has wrong number of operands");
8260 SDValue Base;
8261 bool AllSame = true;
8262 for (unsigned i = 0; i != NumElts; ++i) {
8263 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
8264 Base = V->getOperand(i);
8265 break;
Evan Cheng917ec982006-07-21 08:25:53 +00008266 }
Evan Cheng917ec982006-07-21 08:25:53 +00008267 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008268 // Splat of <u, u, u, u>, return <u, u, u, u>
8269 if (!Base.getNode())
8270 return N0;
8271 for (unsigned i = 0; i != NumElts; ++i) {
8272 if (V->getOperand(i) != Base) {
8273 AllSame = false;
8274 break;
8275 }
8276 }
8277 // Splat of <x, x, x, x>, return <x, x, x, x>
8278 if (AllSame)
8279 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +00008280 }
8281 }
Nadav Rotem4ac90812012-04-01 19:31:22 +00008282
8283 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008284 // and it reverses the swizzle of the previous shuffle then we can
8285 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotem4ac90812012-04-01 19:31:22 +00008286 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
8287 N1.getOpcode() == ISD::UNDEF) {
8288
Nadav Rotem4ac90812012-04-01 19:31:22 +00008289 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
8290
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008291 // Shuffle nodes can only reverse shuffles with a single non-undef value.
8292 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
8293 return SDValue();
8294
Craig Topperae1bec52012-04-09 05:16:56 +00008295 // The incoming shuffle must be of the same type as the result of the
8296 // current shuffle.
8297 assert(OtherSV->getOperand(0).getValueType() == VT &&
8298 "Shuffle types don't match");
Nadav Rotem4ac90812012-04-01 19:31:22 +00008299
8300 for (unsigned i = 0; i != NumElts; ++i) {
8301 int Idx = SVN->getMaskElt(i);
Craig Topperae1bec52012-04-09 05:16:56 +00008302 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotem4ac90812012-04-01 19:31:22 +00008303 // Next, this index comes from the first value, which is the incoming
8304 // shuffle. Adopt the incoming index.
8305 if (Idx >= 0)
8306 Idx = OtherSV->getMaskElt(Idx);
8307
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008308 // The combined shuffle must map each index to itself.
Craig Topperae1bec52012-04-09 05:16:56 +00008309 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008310 return SDValue();
Nadav Rotem4ac90812012-04-01 19:31:22 +00008311 }
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008312
8313 return OtherSV->getOperand(0);
Nadav Rotem4ac90812012-04-01 19:31:22 +00008314 }
8315
Dan Gohman475871a2008-07-27 21:46:04 +00008316 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00008317}
8318
Jim Grosbach9a526492010-06-23 16:07:42 +00008319SDValue DAGCombiner::visitMEMBARRIER(SDNode* N) {
8320 if (!TLI.getShouldFoldAtomicFences())
8321 return SDValue();
8322
8323 SDValue atomic = N->getOperand(0);
8324 switch (atomic.getOpcode()) {
8325 case ISD::ATOMIC_CMP_SWAP:
8326 case ISD::ATOMIC_SWAP:
8327 case ISD::ATOMIC_LOAD_ADD:
8328 case ISD::ATOMIC_LOAD_SUB:
8329 case ISD::ATOMIC_LOAD_AND:
8330 case ISD::ATOMIC_LOAD_OR:
8331 case ISD::ATOMIC_LOAD_XOR:
8332 case ISD::ATOMIC_LOAD_NAND:
8333 case ISD::ATOMIC_LOAD_MIN:
8334 case ISD::ATOMIC_LOAD_MAX:
8335 case ISD::ATOMIC_LOAD_UMIN:
8336 case ISD::ATOMIC_LOAD_UMAX:
8337 break;
8338 default:
8339 return SDValue();
8340 }
8341
8342 SDValue fence = atomic.getOperand(0);
8343 if (fence.getOpcode() != ISD::MEMBARRIER)
8344 return SDValue();
8345
8346 switch (atomic.getOpcode()) {
8347 case ISD::ATOMIC_CMP_SWAP:
8348 return SDValue(DAG.UpdateNodeOperands(atomic.getNode(),
8349 fence.getOperand(0),
8350 atomic.getOperand(1), atomic.getOperand(2),
8351 atomic.getOperand(3)), atomic.getResNo());
8352 case ISD::ATOMIC_SWAP:
8353 case ISD::ATOMIC_LOAD_ADD:
8354 case ISD::ATOMIC_LOAD_SUB:
8355 case ISD::ATOMIC_LOAD_AND:
8356 case ISD::ATOMIC_LOAD_OR:
8357 case ISD::ATOMIC_LOAD_XOR:
8358 case ISD::ATOMIC_LOAD_NAND:
8359 case ISD::ATOMIC_LOAD_MIN:
8360 case ISD::ATOMIC_LOAD_MAX:
8361 case ISD::ATOMIC_LOAD_UMIN:
8362 case ISD::ATOMIC_LOAD_UMAX:
8363 return SDValue(DAG.UpdateNodeOperands(atomic.getNode(),
8364 fence.getOperand(0),
8365 atomic.getOperand(1), atomic.getOperand(2)),
8366 atomic.getResNo());
8367 default:
8368 return SDValue();
8369 }
8370}
8371
Evan Cheng44f1f092006-04-20 08:56:16 +00008372/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00008373/// an AND to a vector_shuffle with the destination vector and a zero vector.
8374/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00008375/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00008376SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00008377 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008378 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008379 SDValue LHS = N->getOperand(0);
8380 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00008381 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008382 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +00008383 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00008384 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008385 SmallVector<int, 8> Indices;
8386 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00008387 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00008388 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00008389 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00008390 return SDValue();
Craig Topperb7135e52012-04-09 05:59:53 +00008391
8392 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00008393 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00008394 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00008395 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00008396 else
Dan Gohman475871a2008-07-27 21:46:04 +00008397 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00008398 }
8399
8400 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00008401 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008402 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00008403 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00008404
Dan Gohman7f321562007-06-25 16:23:39 +00008405 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00008406 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008407 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00008408 DAG.getConstant(0, EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00008409 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
8410 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008411 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +00008412 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008413 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00008414 }
8415 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008416
Dan Gohman475871a2008-07-27 21:46:04 +00008417 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00008418}
8419
Dan Gohman7f321562007-06-25 16:23:39 +00008420/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00008421SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00008422 // After legalize, the target may be depending on adds and other
8423 // binary ops to provide legal ways to construct constants or other
8424 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00008425 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00008426
Bob Wilsond7273432010-12-17 23:06:49 +00008427 assert(N->getValueType(0).isVector() &&
8428 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00008429
Dan Gohman475871a2008-07-27 21:46:04 +00008430 SDValue LHS = N->getOperand(0);
8431 SDValue RHS = N->getOperand(1);
8432 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00008433 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00008434
Dan Gohman7f321562007-06-25 16:23:39 +00008435 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00008436 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00008437 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00008438 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00008439 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00008440 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00008441 SDValue LHSOp = LHS.getOperand(i);
8442 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00008443 // If these two elements can't be folded, bail out.
8444 if ((LHSOp.getOpcode() != ISD::UNDEF &&
8445 LHSOp.getOpcode() != ISD::Constant &&
8446 LHSOp.getOpcode() != ISD::ConstantFP) ||
8447 (RHSOp.getOpcode() != ISD::UNDEF &&
8448 RHSOp.getOpcode() != ISD::Constant &&
8449 RHSOp.getOpcode() != ISD::ConstantFP))
8450 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00008451
Evan Cheng7b336a82006-05-31 06:08:35 +00008452 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00008453 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
8454 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00008455 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00008456 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00008457 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00008458 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00008459 break;
8460 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008461
Bob Wilsond7273432010-12-17 23:06:49 +00008462 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +00008463 EVT RVT = RHSOp.getValueType();
8464 if (RVT != VT) {
8465 // Integer BUILD_VECTOR operands may have types larger than the element
8466 // size (e.g., when the element type is not legal). Prior to type
8467 // legalization, the types may not match between the two BUILD_VECTORS.
8468 // Truncate one of the operands to make them match.
8469 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
8470 RHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, RHSOp);
8471 } else {
8472 LHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), RVT, LHSOp);
8473 VT = RVT;
8474 }
8475 }
Bob Wilsond7273432010-12-17 23:06:49 +00008476 SDValue FoldOp = DAG.getNode(N->getOpcode(), LHS.getDebugLoc(), VT,
Evan Chenga0839882010-05-18 00:03:40 +00008477 LHSOp, RHSOp);
8478 if (FoldOp.getOpcode() != ISD::UNDEF &&
8479 FoldOp.getOpcode() != ISD::Constant &&
8480 FoldOp.getOpcode() != ISD::ConstantFP)
8481 break;
8482 Ops.push_back(FoldOp);
8483 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00008484 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008485
Bob Wilsond7273432010-12-17 23:06:49 +00008486 if (Ops.size() == LHS.getNumOperands())
8487 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
8488 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +00008489 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008490
Dan Gohman475871a2008-07-27 21:46:04 +00008491 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00008492}
8493
Bill Wendling836ca7d2009-01-30 23:59:18 +00008494SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0,
8495 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00008496 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00008497
Bill Wendling836ca7d2009-01-30 23:59:18 +00008498 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00008499 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008500
Nate Begemanf845b452005-10-08 00:29:44 +00008501 // If we got a simplified select_cc node back from SimplifySelectCC, then
8502 // break it down into a new SETCC node, and a new SELECT node, and then return
8503 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00008504 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00008505 // Check to see if we got a select_cc back (to turn into setcc/select).
8506 // Otherwise, just return whatever node we got back, like fabs.
8507 if (SCC.getOpcode() == ISD::SELECT_CC) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00008508 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(),
8509 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008510 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008511 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00008512 AddToWorkList(SETCC.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008513 return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(),
8514 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00008515 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008516
Nate Begemanf845b452005-10-08 00:29:44 +00008517 return SCC;
8518 }
Dan Gohman475871a2008-07-27 21:46:04 +00008519 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00008520}
8521
Chris Lattner40c62d52005-10-18 06:04:22 +00008522/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
8523/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00008524/// select. Callers of this should assume that TheSelect is deleted if this
8525/// returns true. As such, they should return the appropriate thing (e.g. the
8526/// node) back to the top-level of the DAG combiner loop to avoid it being
8527/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00008528bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00008529 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00008530
Nadav Rotemf94fdb62011-02-11 19:57:47 +00008531 // Cannot simplify select with vector condition
8532 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
8533
Chris Lattner40c62d52005-10-18 06:04:22 +00008534 // If this is a select from two identical things, try to pull the operation
8535 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +00008536 if (LHS.getOpcode() != RHS.getOpcode() ||
8537 !LHS.hasOneUse() || !RHS.hasOneUse())
8538 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008539
Chris Lattner18061612010-09-21 15:46:59 +00008540 // If this is a load and the token chain is identical, replace the select
8541 // of two loads with a load through a select of the address to load from.
8542 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
8543 // constants have been dropped into the constant pool.
8544 if (LHS.getOpcode() == ISD::LOAD) {
8545 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
8546 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008547
Chris Lattner18061612010-09-21 15:46:59 +00008548 // Token chains must be identical.
8549 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008550 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +00008551 LLD->isVolatile() || RLD->isVolatile() ||
8552 // If this is an EXTLOAD, the VT's must match.
8553 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +00008554 // If this is an EXTLOAD, the kind of extension must match.
8555 (LLD->getExtensionType() != RLD->getExtensionType() &&
8556 // The only exception is if one of the extensions is anyext.
8557 LLD->getExtensionType() != ISD::EXTLOAD &&
8558 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +00008559 // FIXME: this discards src value information. This is
8560 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00008561 // both potential memory locations. Since we are discarding
8562 // src value info, don't do the transformation if the memory
8563 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +00008564 LLD->getPointerInfo().getAddrSpace() != 0 ||
8565 RLD->getPointerInfo().getAddrSpace() != 0)
8566 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008567
Chris Lattnerf1658062010-09-21 15:58:55 +00008568 // Check that the select condition doesn't reach either load. If so,
8569 // folding this will induce a cycle into the DAG. If not, this is safe to
8570 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +00008571 SDValue Addr;
8572 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +00008573 SDNode *CondNode = TheSelect->getOperand(0).getNode();
8574 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
8575 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
8576 return false;
8577 Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(),
8578 LLD->getBasePtr().getValueType(),
8579 TheSelect->getOperand(0), LLD->getBasePtr(),
8580 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +00008581 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +00008582 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
8583 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
8584
8585 if ((LLD->hasAnyUseOfValue(1) &&
8586 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner77d95212012-03-27 16:27:21 +00008587 (RLD->hasAnyUseOfValue(1) &&
8588 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnerf1658062010-09-21 15:58:55 +00008589 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008590
Chris Lattnerf1658062010-09-21 15:58:55 +00008591 Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(),
8592 LLD->getBasePtr().getValueType(),
8593 TheSelect->getOperand(0),
8594 TheSelect->getOperand(1),
8595 LLD->getBasePtr(), RLD->getBasePtr(),
8596 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +00008597 }
8598
Chris Lattnerf1658062010-09-21 15:58:55 +00008599 SDValue Load;
8600 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
8601 Load = DAG.getLoad(TheSelect->getValueType(0),
8602 TheSelect->getDebugLoc(),
8603 // FIXME: Discards pointer info.
8604 LLD->getChain(), Addr, MachinePointerInfo(),
8605 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008606 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +00008607 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +00008608 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
8609 RLD->getExtensionType() : LLD->getExtensionType(),
Chris Lattnerf1658062010-09-21 15:58:55 +00008610 TheSelect->getDebugLoc(),
Stuart Hastingsa9011292011-02-16 16:23:55 +00008611 TheSelect->getValueType(0),
Chris Lattnerf1658062010-09-21 15:58:55 +00008612 // FIXME: Discards pointer info.
8613 LLD->getChain(), Addr, MachinePointerInfo(),
8614 LLD->getMemoryVT(), LLD->isVolatile(),
8615 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +00008616 }
Chris Lattnerf1658062010-09-21 15:58:55 +00008617
8618 // Users of the select now use the result of the load.
8619 CombineTo(TheSelect, Load);
8620
8621 // Users of the old loads now use the new load's chain. We know the
8622 // old-load value is dead now.
8623 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
8624 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
8625 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +00008626 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008627
Chris Lattner40c62d52005-10-18 06:04:22 +00008628 return false;
8629}
8630
Chris Lattner600fec32009-03-11 05:08:08 +00008631/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
8632/// where 'cond' is the comparison specified by CC.
Scott Michelfdc40a02009-02-17 22:15:04 +00008633SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00008634 SDValue N2, SDValue N3,
8635 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00008636 // (x ? y : y) -> y.
8637 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008638
Owen Andersone50ed302009-08-10 22:56:29 +00008639 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00008640 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
8641 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
8642 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008643
8644 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00008645 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00008646 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00008647 if (SCC.getNode()) AddToWorkList(SCC.getNode());
8648 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008649
8650 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00008651 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00008652 return N2;
8653 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00008654 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00008655 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00008656
Nate Begemanf845b452005-10-08 00:29:44 +00008657 // Check to see if we can simplify the select into an fabs node
8658 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
8659 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00008660 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00008661 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
8662 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
8663 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
8664 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00008665 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008666
Nate Begemanf845b452005-10-08 00:29:44 +00008667 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
8668 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
8669 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
8670 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00008671 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00008672 }
8673 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008674
Chris Lattner600fec32009-03-11 05:08:08 +00008675 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
8676 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
8677 // in it. This is a win when the constant is not otherwise available because
8678 // it replaces two constant pool loads with one. We only do this if the FP
8679 // type is known to be legal, because if it isn't, then we are before legalize
8680 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00008681 // messing with soft float) and if the ConstantFP is not legal, because if
8682 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00008683 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
8684 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
8685 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00008686 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
8687 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00008688 // If both constants have multiple uses, then we won't need to do an
8689 // extra load, they are likely around in registers for other users.
8690 (TV->hasOneUse() || FV->hasOneUse())) {
8691 Constant *Elts[] = {
8692 const_cast<ConstantFP*>(FV->getConstantFPValue()),
8693 const_cast<ConstantFP*>(TV->getConstantFPValue())
8694 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008695 Type *FPTy = Elts[0]->getType();
Chris Lattner600fec32009-03-11 05:08:08 +00008696 const TargetData &TD = *TLI.getTargetData();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008697
Chris Lattner600fec32009-03-11 05:08:08 +00008698 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +00008699 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +00008700 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
8701 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00008702 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00008703
8704 // Get the offsets to the 0 and 1 element of the array so that we can
8705 // select between them.
8706 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00008707 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00008708 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008709
Chris Lattner600fec32009-03-11 05:08:08 +00008710 SDValue Cond = DAG.getSetCC(DL,
8711 TLI.getSetCCResultType(N0.getValueType()),
8712 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +00008713 AddToWorkList(Cond.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00008714 SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(),
8715 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +00008716 AddToWorkList(CstOffset.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00008717 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
8718 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +00008719 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00008720 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00008721 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +00008722 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00008723
8724 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008725 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008726
Nate Begemanf845b452005-10-08 00:29:44 +00008727 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00008728 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00008729 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00008730 (N1C->isNullValue() || // (a < 0) ? b : 0
8731 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00008732 EVT XType = N0.getValueType();
8733 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00008734 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00008735 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00008736 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00008737 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
8738 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00008739 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +00008740 SDValue ShCt = DAG.getConstant(ShCtV,
8741 getShiftAmountTy(N0.getValueType()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00008742 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008743 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00008744 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008745
Duncan Sands8e4eb092008-06-08 20:54:56 +00008746 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008747 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00008748 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008749 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008750
8751 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00008752 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008753
Bill Wendling9729c5a2009-01-31 03:12:48 +00008754 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008755 XType, N0,
8756 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008757 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00008758 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008759
Duncan Sands8e4eb092008-06-08 20:54:56 +00008760 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008761 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00008762 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008763 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008764
8765 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00008766 }
8767 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008768
Owen Andersoned1088a2010-09-22 22:58:22 +00008769 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
8770 // where y is has a single bit set.
8771 // A plaintext description would be, we can turn the SELECT_CC into an AND
8772 // when the condition can be materialized as an all-ones register. Any
8773 // single bit-test can be materialized as an all-ones register with
8774 // shift-left and shift-right-arith.
8775 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
8776 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008777 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +00008778 N2C && N2C->isNullValue()) {
8779 SDValue AndLHS = N0->getOperand(0);
8780 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
8781 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
8782 // Shift the tested bit over the sign bit.
8783 APInt AndMask = ConstAndRHS->getAPIntValue();
8784 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00008785 DAG.getConstant(AndMask.countLeadingZeros(),
8786 getShiftAmountTy(AndLHS.getValueType()));
Owen Andersoned1088a2010-09-22 22:58:22 +00008787 SDValue Shl = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008788
Owen Andersoned1088a2010-09-22 22:58:22 +00008789 // Now arithmetic right shift it all the way over, so the result is either
8790 // all-ones, or zero.
8791 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00008792 DAG.getConstant(AndMask.getBitWidth()-1,
8793 getShiftAmountTy(Shl.getValueType()));
Owen Andersoned1088a2010-09-22 22:58:22 +00008794 SDValue Shr = DAG.getNode(ISD::SRA, N0.getDebugLoc(), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008795
Owen Andersoned1088a2010-09-22 22:58:22 +00008796 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
8797 }
8798 }
8799
Nate Begeman07ed4172005-10-10 21:26:48 +00008800 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00008801 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00008802 TLI.getBooleanContents(N0.getValueType().isVector()) ==
8803 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00008804
Chris Lattner1eba01e2007-04-11 06:50:51 +00008805 // If the caller doesn't want us to simplify this into a zext of a compare,
8806 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00008807 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00008808 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008809
Nate Begeman07ed4172005-10-10 21:26:48 +00008810 // Get a SetCC of the condition
8811 // FIXME: Should probably make sure that setcc is legal if we ever have a
8812 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00008813 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00008814 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00008815 if (LegalTypes) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008816 SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00008817 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00008818 if (N2.getValueType().bitsLT(SCC.getValueType()))
Bill Wendling9729c5a2009-01-31 03:12:48 +00008819 Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), N2.getValueType());
Chris Lattner555d8d62006-12-07 22:36:47 +00008820 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00008821 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008822 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00008823 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00008824 SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC);
Bill Wendling9729c5a2009-01-31 03:12:48 +00008825 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008826 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00008827 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008828
Gabor Greifba36cb52008-08-28 21:40:38 +00008829 AddToWorkList(SCC.getNode());
8830 AddToWorkList(Temp.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00008831
Dan Gohman002e5d02008-03-13 22:13:53 +00008832 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00008833 return Temp;
Bill Wendling836ca7d2009-01-30 23:59:18 +00008834
Nate Begeman07ed4172005-10-10 21:26:48 +00008835 // shl setcc result by log2 n2c
Bill Wendling836ca7d2009-01-30 23:59:18 +00008836 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00008837 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00008838 getShiftAmountTy(Temp.getValueType())));
Nate Begeman07ed4172005-10-10 21:26:48 +00008839 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008840
Nate Begemanf845b452005-10-08 00:29:44 +00008841 // Check to see if this is the equivalent of setcc
8842 // FIXME: Turn all of these into setcc if setcc if setcc is legal
8843 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00008844 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +00008845 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00008846 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00008847 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00008848 SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00008849 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +00008850 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +00008851 return Res;
8852 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008853
Bill Wendling836ca7d2009-01-30 23:59:18 +00008854 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +00008855 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00008856 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00008857 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008858 SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008859 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008860 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +00008861 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +00008862 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008863 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +00008864 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00008865 SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(),
8866 XType, DAG.getConstant(0, XType), N0);
Bill Wendling7581bfa2009-01-30 23:03:19 +00008867 SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +00008868 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00008869 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00008870 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008871 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +00008872 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008873 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +00008874 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008875 SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00008876 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008877 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +00008878 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +00008879 }
8880 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008881
Benjamin Kramercde51102010-07-08 12:09:56 +00008882 // Check to see if this is an integer abs.
8883 // select_cc setg[te] X, 0, X, -X ->
8884 // select_cc setgt X, -1, X, -X ->
8885 // select_cc setl[te] X, 0, -X, X ->
8886 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +00008887 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +00008888 if (N1C) {
8889 ConstantSDNode *SubC = NULL;
8890 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
8891 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
8892 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
8893 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
8894 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
8895 (N1C->isOne() && CC == ISD::SETLT)) &&
8896 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
8897 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
8898
Owen Andersone50ed302009-08-10 22:56:29 +00008899 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +00008900 if (SubC && SubC->isNullValue() && XType.isInteger()) {
8901 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType,
8902 N0,
8903 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008904 getShiftAmountTy(N0.getValueType())));
Benjamin Kramercde51102010-07-08 12:09:56 +00008905 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(),
8906 XType, N0, Shift);
8907 AddToWorkList(Shift.getNode());
8908 AddToWorkList(Add.getNode());
8909 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +00008910 }
8911 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008912
Dan Gohman475871a2008-07-27 21:46:04 +00008913 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00008914}
8915
Evan Chengfa1eb272007-02-08 22:13:59 +00008916/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +00008917SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +00008918 SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00008919 DebugLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +00008920 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00008921 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00008922 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7beb2005-09-16 00:54:12 +00008923}
8924
Nate Begeman69575232005-10-20 02:15:44 +00008925/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
8926/// return a DAG expression to select that will generate the same value by
8927/// multiplying by a magic number. See:
8928/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00008929SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00008930 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +00008931 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00008932
Andrew Lenharth232c9102006-06-12 16:07:18 +00008933 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00008934 ii != ee; ++ii)
8935 AddToWorkList(*ii);
8936 return S;
Nate Begeman69575232005-10-20 02:15:44 +00008937}
8938
8939/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
8940/// return a DAG expression to select that will generate the same value by
8941/// multiplying by a magic number. See:
8942/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00008943SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00008944 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +00008945 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00008946
Andrew Lenharth232c9102006-06-12 16:07:18 +00008947 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00008948 ii != ee; ++ii)
8949 AddToWorkList(*ii);
8950 return S;
Nate Begeman69575232005-10-20 02:15:44 +00008951}
8952
Nate Begemancc66cdd2009-09-25 06:05:26 +00008953/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +00008954// to alias with anything but itself. Provides base object and offset as
8955// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008956static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky2943e372012-09-05 22:15:49 +00008957 const GlobalValue *&GV, const void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +00008958 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008959 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008960
Jim Laskey71382342006-10-07 23:37:56 +00008961 // If it's an adding a simple constant then integrate the offset.
8962 if (Base.getOpcode() == ISD::ADD) {
8963 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
8964 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008965 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00008966 }
8967 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008968
Nate Begemancc66cdd2009-09-25 06:05:26 +00008969 // Return the underlying GlobalValue, and update the Offset. Return false
8970 // for GlobalAddressSDNode since the same GlobalAddress may be represented
8971 // by multiple nodes with different offsets.
8972 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
8973 GV = G->getGlobal();
8974 Offset += G->getOffset();
8975 return false;
8976 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008977
Nate Begemancc66cdd2009-09-25 06:05:26 +00008978 // Return the underlying Constant value, and update the Offset. Return false
8979 // for ConstantSDNodes since the same constant pool entry may be represented
8980 // by multiple nodes with different offsets.
8981 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky2943e372012-09-05 22:15:49 +00008982 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
8983 : (const void *)C->getConstVal();
Nate Begemancc66cdd2009-09-25 06:05:26 +00008984 Offset += C->getOffset();
8985 return false;
8986 }
Jim Laskey71382342006-10-07 23:37:56 +00008987 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008988 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +00008989}
8990
8991/// isAlias - Return true if there is any possibility that the two addresses
8992/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00008993bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00008994 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008995 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008996 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +00008997 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008998 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008999 unsigned SrcValueAlign2,
9000 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +00009001 // If they are the same then they must be aliases.
9002 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00009003
Jim Laskey71382342006-10-07 23:37:56 +00009004 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00009005 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00009006 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +00009007 const GlobalValue *GV1, *GV2;
Roman Divacky2943e372012-09-05 22:15:49 +00009008 const void *CV1, *CV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +00009009 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
9010 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009011
Nate Begemancc66cdd2009-09-25 06:05:26 +00009012 // If they have a same base address then check to see if they overlap.
9013 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +00009014 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009015
Owen Anderson4a9f1502010-09-20 20:39:59 +00009016 // It is possible for different frame indices to alias each other, mostly
9017 // when tail call optimization reuses return address slots for arguments.
9018 // To catch this case, look up the actual index of frame indices to compute
9019 // the real alias relationship.
9020 if (isFrameIndex1 && isFrameIndex2) {
9021 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
9022 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
9023 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
9024 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
9025 }
9026
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009027 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +00009028 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +00009029 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
9030 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +00009031
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009032 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
9033 // compared to the size and offset of the access, we may be able to prove they
9034 // do not alias. This check is conservative for now to catch cases created by
9035 // splitting vector types.
9036 if ((SrcValueAlign1 == SrcValueAlign2) &&
9037 (SrcValueOffset1 != SrcValueOffset2) &&
9038 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
9039 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
9040 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009041
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009042 // There is no overlap between these relatively aligned accesses of similar
9043 // size, return no alias.
9044 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
9045 return false;
9046 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009047
Jim Laskey07a27092006-10-18 19:08:31 +00009048 if (CombinerGlobalAA) {
9049 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00009050 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
9051 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
9052 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00009053 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009054 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
9055 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +00009056 if (AAResult == AliasAnalysis::NoAlias)
9057 return false;
9058 }
Jim Laskey096c22e2006-10-18 12:29:57 +00009059
9060 // Otherwise we have to assume they alias.
9061 return true;
Jim Laskey71382342006-10-07 23:37:56 +00009062}
9063
9064/// FindAliasInfo - Extracts the relevant alias information from the memory
9065/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00009066bool DAGCombiner::FindAliasInfo(SDNode *N,
Benjamin Kramerae4746b2012-01-15 11:50:43 +00009067 SDValue &Ptr, int64_t &Size,
9068 const Value *&SrcValue,
9069 int &SrcValueOffset,
9070 unsigned &SrcValueAlign,
9071 const MDNode *&TBAAInfo) const {
9072 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
9073
9074 Ptr = LS->getBasePtr();
9075 Size = LS->getMemoryVT().getSizeInBits() >> 3;
9076 SrcValue = LS->getSrcValue();
9077 SrcValueOffset = LS->getSrcValueOffset();
9078 SrcValueAlign = LS->getOriginalAlignment();
9079 TBAAInfo = LS->getTBAAInfo();
9080 return isa<LoadSDNode>(LS);
Jim Laskey71382342006-10-07 23:37:56 +00009081}
9082
Jim Laskey6ff23e52006-10-04 16:53:27 +00009083/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
9084/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00009085void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
9086 SmallVector<SDValue, 8> &Aliases) {
9087 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009088 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +00009089
Jim Laskey279f0532006-09-25 16:29:54 +00009090 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00009091 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009092 int64_t Size;
9093 const Value *SrcValue;
9094 int SrcValueOffset;
9095 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009096 const MDNode *SrcTBAAInfo;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009097 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009098 SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +00009099
Jim Laskey6ff23e52006-10-04 16:53:27 +00009100 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00009101 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +00009102 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009103
Jim Laskeybc588b82006-10-05 15:07:25 +00009104 // Look at each chain and determine if it is an alias. If so, add it to the
9105 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +00009106 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +00009107 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009108 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00009109 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009110
9111 // For TokenFactor nodes, look at each operand and only continue up the
9112 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +00009113 // find more and revert to original chain since the xform is unlikely to be
9114 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009115 //
9116 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +00009117 // chain we found before we hit a tokenfactor rather than the original
9118 // chain.
9119 if (Depth > 6 || Aliases.size() == 2) {
9120 Aliases.clear();
9121 Aliases.push_back(OriginalChain);
9122 break;
9123 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009124
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009125 // Don't bother if we've been before.
9126 if (!Visited.insert(Chain.getNode()))
9127 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009128
Jim Laskeybc588b82006-10-05 15:07:25 +00009129 switch (Chain.getOpcode()) {
9130 case ISD::EntryToken:
9131 // Entry token is ideal chain operand, but handled in FindBetterChain.
9132 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00009133
Jim Laskeybc588b82006-10-05 15:07:25 +00009134 case ISD::LOAD:
9135 case ISD::STORE: {
9136 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00009137 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009138 int64_t OpSize;
9139 const Value *OpSrcValue;
9140 int OpSrcValueOffset;
9141 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009142 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +00009143 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009144 OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009145 OpSrcValueAlign,
9146 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +00009147
Jim Laskeybc588b82006-10-05 15:07:25 +00009148 // If chain is alias then stop here.
9149 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009150 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009151 SrcTBAAInfo,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009152 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009153 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00009154 Aliases.push_back(Chain);
9155 } else {
9156 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +00009157 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +00009158 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +00009159 }
Jim Laskeybc588b82006-10-05 15:07:25 +00009160 break;
9161 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009162
Jim Laskeybc588b82006-10-05 15:07:25 +00009163 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009164 // We have to check each of the operands of the token factor for "small"
9165 // token factors, so we queue them up. Adding the operands to the queue
9166 // (stack) in reverse order maintains the original order and increases the
9167 // likelihood that getNode will find a matching token factor (CSE.)
9168 if (Chain.getNumOperands() > 16) {
9169 Aliases.push_back(Chain);
9170 break;
9171 }
Jim Laskeybc588b82006-10-05 15:07:25 +00009172 for (unsigned n = Chain.getNumOperands(); n;)
9173 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +00009174 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +00009175 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00009176
Jim Laskeybc588b82006-10-05 15:07:25 +00009177 default:
9178 // For all other instructions we will just have to take what we can get.
9179 Aliases.push_back(Chain);
9180 break;
Jim Laskey279f0532006-09-25 16:29:54 +00009181 }
9182 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00009183}
9184
9185/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
9186/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00009187SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
9188 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00009189
Jim Laskey6ff23e52006-10-04 16:53:27 +00009190 // Accumulate all the aliases to this node.
9191 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +00009192
Dan Gohman71dc7c92011-05-17 22:20:36 +00009193 // If no operands then chain to entry token.
9194 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +00009195 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +00009196
9197 // If a single operand then chain to it. We don't need to revisit it.
9198 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +00009199 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009200
Jim Laskey6ff23e52006-10-04 16:53:27 +00009201 // Construct a custom tailored token factor.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009202 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009203 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +00009204}
9205
Nate Begeman1d4d4142005-09-01 00:19:25 +00009206// SelectionDAG::Combine - This is the entry point for the file.
9207//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00009208void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +00009209 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00009210 /// run - This is the main entry point to this class.
9211 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00009212 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00009213}