blob: 0a523fa8dc2ce9cad60abca13646ece5223b1cac [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +000013#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000014#include "llvm/Constants.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000015#include "llvm/Analysis/ValueTracking.h"
Dan Gohmanffef8ac2009-08-11 16:02:12 +000016#include "llvm/Function.h"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000017#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000018#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000019#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000020#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000021#include "llvm/Assembly/Writer.h"
Dan Gohman707e0182008-04-12 04:36:06 +000022#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000024#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000027#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000029#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000030#include "llvm/Target/TargetLowering.h"
Dan Gohman760f86f2009-01-22 21:58:43 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000032#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetMachine.h"
Bill Wendling6f287b22008-09-30 21:22:07 +000034#include "llvm/Support/CommandLine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Owen Andersonb4459082009-06-25 17:09:00 +000036#include "llvm/Support/ManagedStatic.h"
Chris Lattner944fac72008-08-23 22:23:09 +000037#include "llvm/Support/MathExtras.h"
38#include "llvm/Support/raw_ostream.h"
Owen Andersonb4459082009-06-25 17:09:00 +000039#include "llvm/System/Mutex.h"
Chris Lattner012f2412006-02-17 21:58:01 +000040#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000041#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000042#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000043#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000044#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000045#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000046#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000047using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000048
Chris Lattner0b3e5252006-08-15 19:11:05 +000049/// makeVTList - Return an instance of the SDVTList struct initialized with the
50/// specified members.
Owen Andersone50ed302009-08-10 22:56:29 +000051static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000052 SDVTList Res = {VTs, NumVTs};
53 return Res;
54}
55
Owen Andersone50ed302009-08-10 22:56:29 +000056static const fltSemantics *EVTToAPFloatSemantics(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +000057 switch (VT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +000058 default: llvm_unreachable("Unknown FP format");
Owen Anderson825b72b2009-08-11 20:47:22 +000059 case MVT::f32: return &APFloat::IEEEsingle;
60 case MVT::f64: return &APFloat::IEEEdouble;
61 case MVT::f80: return &APFloat::x87DoubleExtended;
62 case MVT::f128: return &APFloat::IEEEquad;
63 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
Chris Lattnerec4a5672008-03-05 06:48:13 +000064 }
65}
66
Chris Lattnerf8dc0612008-02-03 06:49:24 +000067SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
68
Jim Laskey58b968b2005-08-17 20:08:02 +000069//===----------------------------------------------------------------------===//
70// ConstantFPSDNode Class
71//===----------------------------------------------------------------------===//
72
73/// isExactlyValue - We don't rely on operator== working on double values, as
74/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
75/// As such, this method can be used to do an exact bit-for-bit comparison of
76/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000077bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000078 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000079}
80
Owen Andersone50ed302009-08-10 22:56:29 +000081bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000082 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000083 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelfdc40a02009-02-17 22:15:04 +000084
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000085 // PPC long double cannot be converted to any other type.
Owen Anderson825b72b2009-08-11 20:47:22 +000086 if (VT == MVT::ppcf128 ||
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000087 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000088 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000089
Dale Johannesenf04afdb2007-08-30 00:23:21 +000090 // convert modifies in place, so make a copy.
91 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +000092 bool losesInfo;
Owen Andersone50ed302009-08-10 22:56:29 +000093 (void) Val2.convert(*EVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
Dale Johannesen23a98552008-10-09 23:00:39 +000094 &losesInfo);
95 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000096}
97
Jim Laskey58b968b2005-08-17 20:08:02 +000098//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000099// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +0000100//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +0000101
Evan Chenga8df1662006-03-27 06:58:47 +0000102/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +0000103/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +0000104bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000105 // Look through a bit convert.
106 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000107 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000108
Evan Chenga8df1662006-03-27 06:58:47 +0000109 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000110
Chris Lattner61d43992006-03-25 22:57:01 +0000111 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000112
Chris Lattner61d43992006-03-25 22:57:01 +0000113 // Skip over all of the undef values.
114 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
115 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000116
Chris Lattner61d43992006-03-25 22:57:01 +0000117 // Do not accept an all-undef vector.
118 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000119
Chris Lattner61d43992006-03-25 22:57:01 +0000120 // Do not accept build_vectors that aren't all constants or which have non-~0
121 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000122 SDValue NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000123 if (isa<ConstantSDNode>(NotZero)) {
124 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
125 return false;
126 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000127 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
Dale Johannesen23a98552008-10-09 23:00:39 +0000128 bitcastToAPInt().isAllOnesValue())
Dan Gohman6c231502008-02-29 01:47:35 +0000129 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000130 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000131 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000132
Chris Lattner61d43992006-03-25 22:57:01 +0000133 // Okay, we have at least one ~0 value, check to see if the rest match or are
134 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000135 for (++i; i != e; ++i)
136 if (N->getOperand(i) != NotZero &&
137 N->getOperand(i).getOpcode() != ISD::UNDEF)
138 return false;
139 return true;
140}
141
142
Evan Cheng4a147842006-03-26 09:50:58 +0000143/// isBuildVectorAllZeros - Return true if the specified node is a
144/// BUILD_VECTOR where all of the elements are 0 or undef.
145bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000146 // Look through a bit convert.
147 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000148 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000149
Evan Cheng4a147842006-03-26 09:50:58 +0000150 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000151
Evan Chenga8df1662006-03-27 06:58:47 +0000152 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000153
Evan Chenga8df1662006-03-27 06:58:47 +0000154 // Skip over all of the undef values.
155 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
156 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000157
Evan Chenga8df1662006-03-27 06:58:47 +0000158 // Do not accept an all-undef vector.
159 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000160
Dan Gohman68f32cb2009-06-04 16:49:15 +0000161 // Do not accept build_vectors that aren't all constants or which have non-0
Evan Chenga8df1662006-03-27 06:58:47 +0000162 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000163 SDValue Zero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000164 if (isa<ConstantSDNode>(Zero)) {
165 if (!cast<ConstantSDNode>(Zero)->isNullValue())
166 return false;
167 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000168 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000169 return false;
170 } else
171 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000172
Dan Gohman68f32cb2009-06-04 16:49:15 +0000173 // Okay, we have at least one 0 value, check to see if the rest match or are
Evan Chenga8df1662006-03-27 06:58:47 +0000174 // undefs.
175 for (++i; i != e; ++i)
176 if (N->getOperand(i) != Zero &&
177 N->getOperand(i).getOpcode() != ISD::UNDEF)
178 return false;
179 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000180}
181
Evan Chengefec7512008-02-18 23:04:32 +0000182/// isScalarToVector - Return true if the specified node is a
183/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
184/// element is not an undef.
185bool ISD::isScalarToVector(const SDNode *N) {
186 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
187 return true;
188
189 if (N->getOpcode() != ISD::BUILD_VECTOR)
190 return false;
191 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
192 return false;
193 unsigned NumElems = N->getNumOperands();
194 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000195 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000196 if (V.getOpcode() != ISD::UNDEF)
197 return false;
198 }
199 return true;
200}
201
202
Evan Chengbb81d972008-01-31 09:59:15 +0000203/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000204/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000205bool ISD::isDebugLabel(const SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000206 SDValue Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000207 if (N->getOpcode() == ISD::DBG_LABEL)
208 return true;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000209 if (N->isMachineOpcode() &&
210 N->getMachineOpcode() == TargetInstrInfo::DBG_LABEL)
Dan Gohman44066042008-07-01 00:05:16 +0000211 return true;
212 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000213}
214
Chris Lattnerc3aae252005-01-07 07:46:32 +0000215/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
216/// when given the operation for (X op Y).
217ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
218 // To perform this operation, we just need to swap the L and G bits of the
219 // operation.
220 unsigned OldL = (Operation >> 2) & 1;
221 unsigned OldG = (Operation >> 1) & 1;
222 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
223 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000224 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000225}
226
227/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
228/// 'op' is a valid SetCC operation.
229ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
230 unsigned Operation = Op;
231 if (isInteger)
232 Operation ^= 7; // Flip L, G, E bits, but not U.
233 else
234 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000235
Chris Lattnerc3aae252005-01-07 07:46:32 +0000236 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000237 Operation &= ~8; // Don't let N and U bits get set.
238
Chris Lattnerc3aae252005-01-07 07:46:32 +0000239 return ISD::CondCode(Operation);
240}
241
242
243/// isSignedOp - For an integer comparison, return 1 if the comparison is a
244/// signed operation and 2 if the result is an unsigned comparison. Return zero
245/// if the operation does not depend on the sign of the input (setne and seteq).
246static int isSignedOp(ISD::CondCode Opcode) {
247 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000248 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattnerc3aae252005-01-07 07:46:32 +0000249 case ISD::SETEQ:
250 case ISD::SETNE: return 0;
251 case ISD::SETLT:
252 case ISD::SETLE:
253 case ISD::SETGT:
254 case ISD::SETGE: return 1;
255 case ISD::SETULT:
256 case ISD::SETULE:
257 case ISD::SETUGT:
258 case ISD::SETUGE: return 2;
259 }
260}
261
262/// getSetCCOrOperation - Return the result of a logical OR between different
263/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
264/// returns SETCC_INVALID if it is not possible to represent the resultant
265/// comparison.
266ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
267 bool isInteger) {
268 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
269 // Cannot fold a signed integer setcc with an unsigned integer setcc.
270 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000271
Chris Lattnerc3aae252005-01-07 07:46:32 +0000272 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000273
Chris Lattnerc3aae252005-01-07 07:46:32 +0000274 // If the N and U bits get set then the resultant comparison DOES suddenly
275 // care about orderedness, and is true when ordered.
276 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000277 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelfdc40a02009-02-17 22:15:04 +0000278
Chris Lattnere41102b2006-05-12 17:03:46 +0000279 // Canonicalize illegal integer setcc's.
280 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
281 Op = ISD::SETNE;
Scott Michelfdc40a02009-02-17 22:15:04 +0000282
Chris Lattnerc3aae252005-01-07 07:46:32 +0000283 return ISD::CondCode(Op);
284}
285
286/// getSetCCAndOperation - Return the result of a logical AND between different
287/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
288/// function returns zero if it is not possible to represent the resultant
289/// comparison.
290ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
291 bool isInteger) {
292 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
293 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000294 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000295
296 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000297 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000298
Chris Lattnera83385f2006-04-27 05:01:07 +0000299 // Canonicalize illegal integer setcc's.
300 if (isInteger) {
301 switch (Result) {
302 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000303 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000304 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000305 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
306 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
307 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000308 }
309 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000310
Chris Lattnera83385f2006-04-27 05:01:07 +0000311 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000312}
313
Chris Lattnerb48da392005-01-23 04:39:44 +0000314const TargetMachine &SelectionDAG::getTarget() const {
Dan Gohman6448d912008-09-04 15:39:15 +0000315 return MF->getTarget();
Chris Lattnerb48da392005-01-23 04:39:44 +0000316}
317
Jim Laskey58b968b2005-08-17 20:08:02 +0000318//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000319// SDNode Profile Support
320//===----------------------------------------------------------------------===//
321
Jim Laskeydef69b92006-10-27 23:52:51 +0000322/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
323///
Jim Laskey583bd472006-10-27 23:46:08 +0000324static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
325 ID.AddInteger(OpC);
326}
327
328/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
329/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000330static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000331 ID.AddPointer(VTList.VTs);
Jim Laskey583bd472006-10-27 23:46:08 +0000332}
333
Jim Laskeydef69b92006-10-27 23:52:51 +0000334/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
335///
Jim Laskey583bd472006-10-27 23:46:08 +0000336static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000337 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000338 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000339 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000340 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000341 }
Jim Laskey583bd472006-10-27 23:46:08 +0000342}
343
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000344/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
345///
346static void AddNodeIDOperands(FoldingSetNodeID &ID,
347 const SDUse *Ops, unsigned NumOps) {
348 for (; NumOps; --NumOps, ++Ops) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000349 ID.AddPointer(Ops->getNode());
350 ID.AddInteger(Ops->getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000351 }
352}
353
Jim Laskey583bd472006-10-27 23:46:08 +0000354static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michelfdc40a02009-02-17 22:15:04 +0000355 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000356 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000357 AddNodeIDOpcode(ID, OpC);
358 AddNodeIDValueTypes(ID, VTList);
359 AddNodeIDOperands(ID, OpList, N);
360}
361
Duncan Sands0dc40452008-10-27 15:30:53 +0000362/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
363/// the NodeID data.
364static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000365 switch (N->getOpcode()) {
Chris Lattner2a4ed822009-06-25 21:21:14 +0000366 case ISD::TargetExternalSymbol:
367 case ISD::ExternalSymbol:
Torok Edwinc23197a2009-07-14 16:55:14 +0000368 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000369 default: break; // Normal nodes don't need extra info.
370 case ISD::TargetConstant:
371 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000372 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000373 break;
374 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000375 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000376 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000377 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000378 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000379 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000380 case ISD::GlobalAddress:
381 case ISD::TargetGlobalTLSAddress:
382 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000383 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000384 ID.AddPointer(GA->getGlobal());
385 ID.AddInteger(GA->getOffset());
Chris Lattner2a4ed822009-06-25 21:21:14 +0000386 ID.AddInteger(GA->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000387 break;
388 }
389 case ISD::BasicBlock:
390 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
391 break;
392 case ISD::Register:
393 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
394 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000395 case ISD::DBG_STOPPOINT: {
396 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
397 ID.AddInteger(DSP->getLine());
398 ID.AddInteger(DSP->getColumn());
399 ID.AddPointer(DSP->getCompileUnit());
400 break;
401 }
Dan Gohman69de1932008-02-06 22:27:42 +0000402 case ISD::SRCVALUE:
403 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
404 break;
405 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000406 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000407 MO.Profile(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000408 break;
409 }
410 case ISD::FrameIndex:
411 case ISD::TargetFrameIndex:
412 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
413 break;
414 case ISD::JumpTable:
415 case ISD::TargetJumpTable:
416 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000417 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000418 break;
419 case ISD::ConstantPool:
420 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000421 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000422 ID.AddInteger(CP->getAlignment());
423 ID.AddInteger(CP->getOffset());
424 if (CP->isMachineConstantPoolEntry())
425 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
426 else
427 ID.AddPointer(CP->getConstVal());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000428 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000429 break;
430 }
431 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000432 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000433 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000434 ID.AddInteger(LD->getRawSubclassData());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000435 break;
436 }
437 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000438 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000439 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000440 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000441 break;
442 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000443 case ISD::ATOMIC_CMP_SWAP:
444 case ISD::ATOMIC_SWAP:
445 case ISD::ATOMIC_LOAD_ADD:
446 case ISD::ATOMIC_LOAD_SUB:
447 case ISD::ATOMIC_LOAD_AND:
448 case ISD::ATOMIC_LOAD_OR:
449 case ISD::ATOMIC_LOAD_XOR:
450 case ISD::ATOMIC_LOAD_NAND:
451 case ISD::ATOMIC_LOAD_MIN:
452 case ISD::ATOMIC_LOAD_MAX:
453 case ISD::ATOMIC_LOAD_UMIN:
454 case ISD::ATOMIC_LOAD_UMAX: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000455 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000456 ID.AddInteger(AT->getMemoryVT().getRawBits());
457 ID.AddInteger(AT->getRawSubclassData());
Mon P Wang28873102008-06-25 08:15:39 +0000458 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000459 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000460 case ISD::VECTOR_SHUFFLE: {
461 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000462 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000463 i != e; ++i)
464 ID.AddInteger(SVN->getMaskElt(i));
465 break;
466 }
Mon P Wang28873102008-06-25 08:15:39 +0000467 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000468}
469
Duncan Sands0dc40452008-10-27 15:30:53 +0000470/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
471/// data.
472static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
473 AddNodeIDOpcode(ID, N->getOpcode());
474 // Add the return value info.
475 AddNodeIDValueTypes(ID, N->getVTList());
476 // Add the operand info.
477 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
478
479 // Handle SDNode leafs with special info.
480 AddNodeIDCustom(ID, N);
481}
482
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000483/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
Dan Gohmana7ce7412009-02-03 00:08:45 +0000484/// the CSE map that carries alignment, volatility, indexing mode, and
485/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000486///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000487static inline unsigned
Dan Gohmana7ce7412009-02-03 00:08:45 +0000488encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM,
489 bool isVolatile, unsigned Alignment) {
490 assert((ConvType & 3) == ConvType &&
491 "ConvType may not require more than 2 bits!");
492 assert((AM & 7) == AM &&
493 "AM may not require more than 3 bits!");
494 return ConvType |
495 (AM << 2) |
496 (isVolatile << 5) |
497 ((Log2_32(Alignment) + 1) << 6);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000498}
499
Jim Laskey583bd472006-10-27 23:46:08 +0000500//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000501// SelectionDAG Class
502//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000503
Duncan Sands0dc40452008-10-27 15:30:53 +0000504/// doNotCSE - Return true if CSE should not be performed for this node.
505static bool doNotCSE(SDNode *N) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 if (N->getValueType(0) == MVT::Flag)
Duncan Sands0dc40452008-10-27 15:30:53 +0000507 return true; // Never CSE anything that produces a flag.
508
509 switch (N->getOpcode()) {
510 default: break;
511 case ISD::HANDLENODE:
512 case ISD::DBG_LABEL:
513 case ISD::DBG_STOPPOINT:
514 case ISD::EH_LABEL:
Duncan Sands0dc40452008-10-27 15:30:53 +0000515 return true; // Never CSE these nodes.
516 }
517
518 // Check that remaining values produced are not flags.
519 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 if (N->getValueType(i) == MVT::Flag)
Duncan Sands0dc40452008-10-27 15:30:53 +0000521 return true; // Never CSE anything that produces a flag.
522
523 return false;
524}
525
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000526/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000527/// SelectionDAG.
528void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000529 // Create a dummy node (which is not added to allnodes), that adds a reference
530 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000531 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000532
Chris Lattner190a4182006-08-04 17:45:20 +0000533 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000534
Chris Lattner190a4182006-08-04 17:45:20 +0000535 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000536 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000537 if (I->use_empty())
538 DeadNodes.push_back(I);
539
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000540 RemoveDeadNodes(DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000541
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000542 // If the root changed (e.g. it was a dead load, update the root).
543 setRoot(Dummy.getValue());
544}
545
546/// RemoveDeadNodes - This method deletes the unreachable nodes in the
547/// given list, and any nodes that become unreachable as a result.
548void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
549 DAGUpdateListener *UpdateListener) {
550
Chris Lattner190a4182006-08-04 17:45:20 +0000551 // Process the worklist, deleting the nodes and adding their uses to the
552 // worklist.
553 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000554 SDNode *N = DeadNodes.pop_back_val();
Scott Michelfdc40a02009-02-17 22:15:04 +0000555
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000556 if (UpdateListener)
557 UpdateListener->NodeDeleted(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000558
Chris Lattner190a4182006-08-04 17:45:20 +0000559 // Take the node out of the appropriate CSE map.
560 RemoveNodeFromCSEMaps(N);
561
562 // Next, brutally remove the operand list. This is safe to do, as there are
563 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000564 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
565 SDUse &Use = *I++;
566 SDNode *Operand = Use.getNode();
567 Use.set(SDValue());
568
Chris Lattner190a4182006-08-04 17:45:20 +0000569 // Now that we removed this operand, see if there are no uses of it left.
570 if (Operand->use_empty())
571 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000572 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000573
Dan Gohmanc5336122009-01-19 22:39:36 +0000574 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000575 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000576}
577
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000578void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000579 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000580 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000581}
582
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000583void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000584 // First take this out of the appropriate CSE map.
585 RemoveNodeFromCSEMaps(N);
586
Scott Michelfdc40a02009-02-17 22:15:04 +0000587 // Finally, remove uses due to operands of this node, remove from the
Chris Lattner1e111c72005-09-07 05:37:01 +0000588 // AllNodes list, and delete the node.
589 DeleteNodeNotInCSEMaps(N);
590}
591
592void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000593 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
594 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000595
Dan Gohmanf06c8352008-09-30 18:30:35 +0000596 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000597 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000598
Dan Gohmanc5336122009-01-19 22:39:36 +0000599 DeallocateNode(N);
600}
601
602void SelectionDAG::DeallocateNode(SDNode *N) {
603 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000604 delete[] N->OperandList;
Scott Michelfdc40a02009-02-17 22:15:04 +0000605
Dan Gohmanc5336122009-01-19 22:39:36 +0000606 // Set the opcode to DELETED_NODE to help catch bugs when node
607 // memory is reallocated.
608 N->NodeType = ISD::DELETED_NODE;
609
Dan Gohman11467282008-08-26 01:44:34 +0000610 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000611}
612
Chris Lattner149c58c2005-08-16 18:17:10 +0000613/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
614/// correspond to it. This is useful when we're about to delete or repurpose
615/// the node. We don't want future request for structurally identical nodes
616/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000617bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000618 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000619 switch (N->getOpcode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000620 case ISD::EntryToken:
Torok Edwinc23197a2009-07-14 16:55:14 +0000621 llvm_unreachable("EntryToken should not be in CSEMaps!");
Dan Gohman095cc292008-09-13 01:54:27 +0000622 return false;
623 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000624 case ISD::CONDCODE:
625 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
626 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000627 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000628 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
629 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000630 case ISD::ExternalSymbol:
631 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000632 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000633 case ISD::TargetExternalSymbol: {
634 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
635 Erased = TargetExternalSymbols.erase(
636 std::pair<std::string,unsigned char>(ESN->getSymbol(),
637 ESN->getTargetFlags()));
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000638 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000639 }
Duncan Sandsf411b832007-10-17 13:49:58 +0000640 case ISD::VALUETYPE: {
Owen Andersone50ed302009-08-10 22:56:29 +0000641 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000642 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000643 Erased = ExtendedValueTypeNodes.erase(VT);
644 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000645 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
646 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000647 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000648 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000649 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000650 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000651 // Remove it from the CSE Map.
652 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000653 break;
654 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000655#ifndef NDEBUG
Scott Michelfdc40a02009-02-17 22:15:04 +0000656 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner6621e3b2005-09-02 19:15:44 +0000657 // flag result (which cannot be CSE'd) or is one of the special cases that are
658 // not subject to CSE.
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000660 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000661 N->dump(this);
Chris Lattner45cfe542009-08-23 06:03:38 +0000662 errs() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000663 llvm_unreachable("Node is not in map!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000664 }
665#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000666 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000667}
668
Dan Gohman39946102009-01-25 16:29:12 +0000669/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
670/// maps and modified in place. Add it back to the CSE maps, unless an identical
671/// node already exists, in which case transfer all its users to the existing
672/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000673///
Dan Gohman39946102009-01-25 16:29:12 +0000674void
675SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N,
676 DAGUpdateListener *UpdateListener) {
677 // For node types that aren't CSE'd, just act as if no identical node
678 // already exists.
679 if (!doNotCSE(N)) {
680 SDNode *Existing = CSEMap.GetOrInsertNode(N);
681 if (Existing != N) {
682 // If there was already an existing matching node, use ReplaceAllUsesWith
683 // to replace the dead one with the existing one. This can cause
684 // recursive merging of other unrelated nodes down the line.
685 ReplaceAllUsesWith(N, Existing, UpdateListener);
Evan Cheng71e86852008-07-08 20:06:39 +0000686
Dan Gohman39946102009-01-25 16:29:12 +0000687 // N is now dead. Inform the listener if it exists and delete it.
Scott Michelfdc40a02009-02-17 22:15:04 +0000688 if (UpdateListener)
Dan Gohman39946102009-01-25 16:29:12 +0000689 UpdateListener->NodeDeleted(N, Existing);
690 DeleteNodeNotInCSEMaps(N);
691 return;
692 }
693 }
Evan Cheng71e86852008-07-08 20:06:39 +0000694
Dan Gohman39946102009-01-25 16:29:12 +0000695 // If the node doesn't already exist, we updated it. Inform a listener if
696 // it exists.
Scott Michelfdc40a02009-02-17 22:15:04 +0000697 if (UpdateListener)
Dan Gohman39946102009-01-25 16:29:12 +0000698 UpdateListener->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000699}
700
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000701/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000702/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000703/// return null, otherwise return a pointer to the slot it would take. If a
704/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000705SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000706 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000707 if (doNotCSE(N))
708 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000709
Dan Gohman475871a2008-07-27 21:46:04 +0000710 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000711 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000712 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000713 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000714 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000715}
716
717/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000718/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000719/// return null, otherwise return a pointer to the slot it would take. If a
720/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000721SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000722 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000723 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000724 if (doNotCSE(N))
725 return 0;
726
Dan Gohman475871a2008-07-27 21:46:04 +0000727 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000728 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000729 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000730 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000731 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
732}
733
734
735/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000736/// were replaced with those specified. If this node is never memoized,
Chris Lattnera5682852006-08-07 23:03:03 +0000737/// return null, otherwise return a pointer to the slot it would take. If a
738/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000739SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000740 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000741 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000742 if (doNotCSE(N))
743 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000744
Jim Laskey583bd472006-10-27 23:46:08 +0000745 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000746 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands0dc40452008-10-27 15:30:53 +0000747 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000748 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000749}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000750
Duncan Sandsd038e042008-07-21 10:20:31 +0000751/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
752void SelectionDAG::VerifyNode(SDNode *N) {
753 switch (N->getOpcode()) {
754 default:
755 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000756 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +0000757 EVT VT = N->getValueType(0);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000758 assert(N->getNumValues() == 1 && "Too many results!");
759 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
760 "Wrong return type!");
761 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
762 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
763 "Mismatched operand types!");
764 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
765 "Wrong operand type!");
766 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
767 "Wrong return type size");
768 break;
769 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000770 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000771 assert(N->getNumValues() == 1 && "Too many results!");
772 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000773 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000774 "Wrong number of operands!");
Owen Andersone50ed302009-08-10 22:56:29 +0000775 EVT EltVT = N->getValueType(0).getVectorElementType();
Rafael Espindola15684b22009-04-24 12:40:33 +0000776 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
777 assert((I->getValueType() == EltVT ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000778 (EltVT.isInteger() && I->getValueType().isInteger() &&
779 EltVT.bitsLE(I->getValueType()))) &&
780 "Wrong operand type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000781 break;
782 }
783 }
784}
785
Owen Andersone50ed302009-08-10 22:56:29 +0000786/// getEVTAlignment - Compute the default alignment value for the
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000787/// given type.
788///
Owen Andersone50ed302009-08-10 22:56:29 +0000789unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 const Type *Ty = VT == MVT::iPTR ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000791 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson23b9b192009-08-12 00:36:31 +0000792 VT.getTypeForEVT(*getContext());
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000793
794 return TLI.getTargetData()->getABITypeAlignment(Ty);
795}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000796
Dale Johannesen92570c42009-02-07 02:15:05 +0000797// EntryNode could meaningfully have debug info if we can find it...
Dan Gohman7c3234c2008-08-27 23:52:12 +0000798SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
Devang Patelb51d40c2009-02-03 18:46:32 +0000799 : TLI(tli), FLI(fli), DW(0),
Dale Johannesen92570c42009-02-07 02:15:05 +0000800 EntryNode(ISD::EntryToken, DebugLoc::getUnknownLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 getVTList(MVT::Other)), Root(getEntryNode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000802 AllNodes.push_back(&EntryNode);
Dan Gohman6f179662008-08-23 00:50:30 +0000803}
804
Devang Patel6e7a1612009-01-09 19:11:50 +0000805void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi,
Owen Anderson5dcaceb2009-07-09 18:44:09 +0000806 DwarfWriter *dw) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000807 MF = &mf;
808 MMI = mmi;
Devang Patel6e7a1612009-01-09 19:11:50 +0000809 DW = dw;
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000810 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000811}
812
Chris Lattner78ec3112003-08-11 14:57:33 +0000813SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000814 allnodes_clear();
815}
816
817void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000818 assert(&*AllNodes.begin() == &EntryNode);
819 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000820 while (!AllNodes.empty())
821 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000822}
823
Dan Gohman7c3234c2008-08-27 23:52:12 +0000824void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000825 allnodes_clear();
826 OperandAllocator.Reset();
827 CSEMap.clear();
828
829 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000830 ExternalSymbols.clear();
831 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000832 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
833 static_cast<CondCodeSDNode*>(0));
834 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
835 static_cast<SDNode*>(0));
836
Dan Gohmane7852d02009-01-26 04:35:06 +0000837 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000838 AllNodes.push_back(&EntryNode);
839 Root = getEntryNode();
840}
841
Owen Andersone50ed302009-08-10 22:56:29 +0000842SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT VT) {
Bill Wendling6ce610f2009-01-30 22:23:15 +0000843 if (Op.getValueType() == VT) return Op;
844 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
845 VT.getSizeInBits());
846 return getNode(ISD::AND, DL, Op.getValueType(), Op,
847 getConstant(Imm, Op.getValueType()));
848}
849
Bob Wilson4c245462009-01-22 17:39:32 +0000850/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
851///
Owen Andersone50ed302009-08-10 22:56:29 +0000852SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, EVT VT) {
853 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohmanbd209ab2009-04-20 22:51:43 +0000854 SDValue NegOne =
855 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000856 return getNode(ISD::XOR, DL, VT, Val, NegOne);
857}
858
Owen Andersone50ed302009-08-10 22:56:29 +0000859SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
860 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohmance9bc122009-01-27 20:39:34 +0000861 assert((EltVT.getSizeInBits() >= 64 ||
862 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
863 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000864 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000865}
866
Owen Andersone50ed302009-08-10 22:56:29 +0000867SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000868 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000869}
870
Owen Andersone50ed302009-08-10 22:56:29 +0000871SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000872 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000873
Owen Andersone50ed302009-08-10 22:56:29 +0000874 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000875 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000876 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000877
878 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000879 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000880 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000881 ID.AddPointer(&Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000882 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000883 SDNode *N = NULL;
884 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000885 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000886 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000887 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000888 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000889 new (N) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000890 CSEMap.InsertNode(N, IP);
891 AllNodes.push_back(N);
892 }
893
Dan Gohman475871a2008-07-27 21:46:04 +0000894 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000895 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000896 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000897 Ops.assign(VT.getVectorNumElements(), Result);
Evan Chenga87008d2009-02-25 22:49:59 +0000898 Result = getNode(ISD::BUILD_VECTOR, DebugLoc::getUnknownLoc(),
899 VT, &Ops[0], Ops.size());
Dan Gohman89081322007-12-12 22:21:26 +0000900 }
901 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000902}
903
Dan Gohman475871a2008-07-27 21:46:04 +0000904SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000905 return getConstant(Val, TLI.getPointerTy(), isTarget);
906}
907
908
Owen Andersone50ed302009-08-10 22:56:29 +0000909SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000910 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000911}
912
Owen Andersone50ed302009-08-10 22:56:29 +0000913SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000914 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000915
Owen Andersone50ed302009-08-10 22:56:29 +0000916 EVT EltVT =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000917 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000918
Chris Lattnerd8658612005-02-17 20:17:32 +0000919 // Do the map lookup using the actual bit pattern for the floating point
920 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
921 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000922 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000923 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000924 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000925 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000926 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000927 SDNode *N = NULL;
928 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000929 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000930 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000931 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000932 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000933 new (N) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000934 CSEMap.InsertNode(N, IP);
935 AllNodes.push_back(N);
936 }
937
Dan Gohman475871a2008-07-27 21:46:04 +0000938 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000939 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000940 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000941 Ops.assign(VT.getVectorNumElements(), Result);
Evan Chenga87008d2009-02-25 22:49:59 +0000942 // FIXME DebugLoc info might be appropriate here
943 Result = getNode(ISD::BUILD_VECTOR, DebugLoc::getUnknownLoc(),
944 VT, &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +0000945 }
946 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000947}
948
Owen Andersone50ed302009-08-10 22:56:29 +0000949SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
950 EVT EltVT =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000951 VT.isVector() ? VT.getVectorElementType() : VT;
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 if (EltVT==MVT::f32)
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000953 return getConstantFP(APFloat((float)Val), VT, isTarget);
954 else
955 return getConstantFP(APFloat(Val), VT, isTarget);
956}
957
Dan Gohman475871a2008-07-27 21:46:04 +0000958SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Owen Andersone50ed302009-08-10 22:56:29 +0000959 EVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +0000960 bool isTargetGA,
961 unsigned char TargetFlags) {
962 assert((TargetFlags == 0 || isTargetGA) &&
963 "Cannot set target flags on target-independent globals");
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000964
Dan Gohman6520e202008-10-18 02:06:02 +0000965 // Truncate (with sign-extension) the offset value to the pointer size.
Owen Andersone50ed302009-08-10 22:56:29 +0000966 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +0000967 unsigned BitWidth = PTy.getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +0000968 if (BitWidth < 64)
969 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
970
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000971 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
972 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000973 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000974 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000975 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000976 }
977
Chris Lattner2a4ed822009-06-25 21:21:14 +0000978 unsigned Opc;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000979 if (GVar && GVar->isThreadLocal())
980 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
981 else
982 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000983
Jim Laskey583bd472006-10-27 23:46:08 +0000984 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000985 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000986 ID.AddPointer(GV);
987 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +0000988 ID.AddInteger(TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +0000989 void *IP = 0;
990 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +0000991 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000992 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Chris Lattner505e9822009-06-26 21:14:05 +0000993 new (N) GlobalAddressSDNode(Opc, GV, VT, Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +0000994 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000995 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000996 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000997}
998
Owen Andersone50ed302009-08-10 22:56:29 +0000999SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001000 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +00001001 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001002 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001003 ID.AddInteger(FI);
1004 void *IP = 0;
1005 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001006 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001007 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001008 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001009 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001010 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001011 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001012}
1013
Owen Andersone50ed302009-08-10 22:56:29 +00001014SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001015 unsigned char TargetFlags) {
1016 assert((TargetFlags == 0 || isTarget) &&
1017 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001018 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001019 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001020 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001021 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001022 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001023 void *IP = 0;
1024 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001025 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001026 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Chris Lattnerf5a55462009-06-25 21:35:31 +00001027 new (N) JumpTableSDNode(JTI, VT, isTarget, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001028 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001029 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001030 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001031}
1032
Owen Andersone50ed302009-08-10 22:56:29 +00001033SDValue SelectionDAG::getConstantPool(Constant *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001034 unsigned Alignment, int Offset,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001035 bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001036 unsigned char TargetFlags) {
1037 assert((TargetFlags == 0 || isTarget) &&
1038 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001039 if (Alignment == 0)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001040 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001041 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001042 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001043 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001044 ID.AddInteger(Alignment);
1045 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001046 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001047 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001048 void *IP = 0;
1049 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001050 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001051 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Chris Lattnerf5a55462009-06-25 21:35:31 +00001052 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001053 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001054 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001055 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001056}
1057
Chris Lattnerc3aae252005-01-07 07:46:32 +00001058
Owen Andersone50ed302009-08-10 22:56:29 +00001059SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001060 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001061 bool isTarget,
1062 unsigned char TargetFlags) {
1063 assert((TargetFlags == 0 || isTarget) &&
1064 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001065 if (Alignment == 0)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001066 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001067 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001068 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001069 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001070 ID.AddInteger(Alignment);
1071 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +00001072 C->AddSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001073 ID.AddInteger(TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001074 void *IP = 0;
1075 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001076 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001077 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Chris Lattnerf5a55462009-06-25 21:35:31 +00001078 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001079 CSEMap.InsertNode(N, IP);
1080 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001081 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001082}
1083
Dan Gohman475871a2008-07-27 21:46:04 +00001084SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001085 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001086 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001087 ID.AddPointer(MBB);
1088 void *IP = 0;
1089 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001090 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001091 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001092 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001093 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001094 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001095 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001096}
1097
Owen Andersone50ed302009-08-10 22:56:29 +00001098SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001099 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1100 ValueTypeNodes.size())
1101 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001102
Duncan Sands83ec4b62008-06-06 12:08:01 +00001103 SDNode *&N = VT.isExtended() ?
Owen Anderson825b72b2009-08-11 20:47:22 +00001104 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsf411b832007-10-17 13:49:58 +00001105
Dan Gohman475871a2008-07-27 21:46:04 +00001106 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001107 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001108 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001109 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001110 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001111}
1112
Owen Andersone50ed302009-08-10 22:56:29 +00001113SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling056292f2008-09-16 21:48:12 +00001114 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001115 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001116 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Chris Lattner1af22312009-06-25 18:45:50 +00001117 new (N) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001118 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001119 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001120}
1121
Owen Andersone50ed302009-08-10 22:56:29 +00001122SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner1af22312009-06-25 18:45:50 +00001123 unsigned char TargetFlags) {
1124 SDNode *&N =
1125 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1126 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001127 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001128 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Chris Lattner1af22312009-06-25 18:45:50 +00001129 new (N) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001130 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001131 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001132}
1133
Dan Gohman475871a2008-07-27 21:46:04 +00001134SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001135 if ((unsigned)Cond >= CondCodeNodes.size())
1136 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001137
Chris Lattner079a27a2005-08-09 20:40:02 +00001138 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001139 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001140 new (N) CondCodeSDNode(Cond);
1141 CondCodeNodes[Cond] = N;
1142 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001143 }
Dan Gohman475871a2008-07-27 21:46:04 +00001144 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001145}
1146
Nate Begeman5a5ca152009-04-29 05:20:52 +00001147// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1148// the shuffle mask M that point at N1 to point at N2, and indices that point
1149// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001150static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1151 std::swap(N1, N2);
1152 int NElts = M.size();
1153 for (int i = 0; i != NElts; ++i) {
1154 if (M[i] >= NElts)
1155 M[i] -= NElts;
1156 else if (M[i] >= 0)
1157 M[i] += NElts;
1158 }
1159}
1160
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001161SDValue SelectionDAG::getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1,
Nate Begeman9008ca62009-04-27 18:41:29 +00001162 SDValue N2, const int *Mask) {
1163 assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001164 assert(VT.isVector() && N1.getValueType().isVector() &&
Nate Begeman9008ca62009-04-27 18:41:29 +00001165 "Vector Shuffle VTs must be a vectors");
1166 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType()
1167 && "Vector Shuffle VTs must have same element type");
1168
1169 // Canonicalize shuffle undef, undef -> undef
1170 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001171 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001172
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001173 // Validate that all indices in Mask are within the range of the elements
Nate Begeman9008ca62009-04-27 18:41:29 +00001174 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001175 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001176 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001177 for (unsigned i = 0; i != NElts; ++i) {
1178 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001179 MaskVec.push_back(Mask[i]);
1180 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001181
Nate Begeman9008ca62009-04-27 18:41:29 +00001182 // Canonicalize shuffle v, v -> v, undef
1183 if (N1 == N2) {
1184 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001185 for (unsigned i = 0; i != NElts; ++i)
1186 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001187 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001188
Nate Begeman9008ca62009-04-27 18:41:29 +00001189 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1190 if (N1.getOpcode() == ISD::UNDEF)
1191 commuteShuffle(N1, N2, MaskVec);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001192
Nate Begeman9008ca62009-04-27 18:41:29 +00001193 // Canonicalize all index into lhs, -> shuffle lhs, undef
1194 // Canonicalize all index into rhs, -> shuffle rhs, undef
1195 bool AllLHS = true, AllRHS = true;
1196 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001197 for (unsigned i = 0; i != NElts; ++i) {
1198 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001199 if (N2Undef)
1200 MaskVec[i] = -1;
1201 else
1202 AllLHS = false;
1203 } else if (MaskVec[i] >= 0) {
1204 AllRHS = false;
1205 }
1206 }
1207 if (AllLHS && AllRHS)
1208 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001209 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001210 N2 = getUNDEF(VT);
1211 if (AllRHS) {
1212 N1 = getUNDEF(VT);
1213 commuteShuffle(N1, N2, MaskVec);
1214 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001215
Nate Begeman9008ca62009-04-27 18:41:29 +00001216 // If Identity shuffle, or all shuffle in to undef, return that node.
1217 bool AllUndef = true;
1218 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001219 for (unsigned i = 0; i != NElts; ++i) {
1220 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001221 if (MaskVec[i] >= 0) AllUndef = false;
1222 }
Dan Gohman2e4284d2009-07-09 00:46:33 +00001223 if (Identity && NElts == N1.getValueType().getVectorNumElements())
Nate Begeman9008ca62009-04-27 18:41:29 +00001224 return N1;
1225 if (AllUndef)
1226 return getUNDEF(VT);
1227
1228 FoldingSetNodeID ID;
1229 SDValue Ops[2] = { N1, N2 };
1230 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001231 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001232 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001233
Nate Begeman9008ca62009-04-27 18:41:29 +00001234 void* IP = 0;
1235 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1236 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001237
Nate Begeman9008ca62009-04-27 18:41:29 +00001238 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1239 // SDNode doesn't have access to it. This memory will be "leaked" when
1240 // the node is deallocated, but recovered when the NodeAllocator is released.
1241 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1242 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001243
Nate Begeman9008ca62009-04-27 18:41:29 +00001244 ShuffleVectorSDNode *N = NodeAllocator.Allocate<ShuffleVectorSDNode>();
1245 new (N) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc);
1246 CSEMap.InsertNode(N, IP);
1247 AllNodes.push_back(N);
1248 return SDValue(N, 0);
1249}
1250
Owen Andersone50ed302009-08-10 22:56:29 +00001251SDValue SelectionDAG::getConvertRndSat(EVT VT, DebugLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001252 SDValue Val, SDValue DTy,
1253 SDValue STy, SDValue Rnd, SDValue Sat,
1254 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001255 // If the src and dest types are the same and the conversion is between
1256 // integer types of the same sign or two floats, no conversion is necessary.
1257 if (DTy == STy &&
1258 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001259 return Val;
1260
1261 FoldingSetNodeID ID;
1262 void* IP = 0;
1263 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1264 return SDValue(E, 0);
1265 CvtRndSatSDNode *N = NodeAllocator.Allocate<CvtRndSatSDNode>();
1266 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1267 new (N) CvtRndSatSDNode(VT, dl, Ops, 5, Code);
1268 CSEMap.InsertNode(N, IP);
1269 AllNodes.push_back(N);
1270 return SDValue(N, 0);
1271}
1272
Owen Andersone50ed302009-08-10 22:56:29 +00001273SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001274 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001275 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001276 ID.AddInteger(RegNo);
1277 void *IP = 0;
1278 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001279 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001280 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001281 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001282 CSEMap.InsertNode(N, IP);
1283 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001284 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001285}
1286
Chris Lattneraf29a522009-05-04 22:10:05 +00001287SDValue SelectionDAG::getDbgStopPoint(DebugLoc DL, SDValue Root,
Devang Patel83489bb2009-01-13 00:35:13 +00001288 unsigned Line, unsigned Col,
Devang Patele4b27562009-08-28 23:24:31 +00001289 MDNode *CU) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001290 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001291 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Chris Lattneraf29a522009-05-04 22:10:05 +00001292 N->setDebugLoc(DL);
Dan Gohman7f460202008-06-30 20:59:49 +00001293 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001294 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001295}
1296
Dale Johannesene8c17332009-01-29 00:47:48 +00001297SDValue SelectionDAG::getLabel(unsigned Opcode, DebugLoc dl,
1298 SDValue Root,
1299 unsigned LabelID) {
1300 FoldingSetNodeID ID;
1301 SDValue Ops[] = { Root };
Owen Anderson825b72b2009-08-11 20:47:22 +00001302 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
Dale Johannesene8c17332009-01-29 00:47:48 +00001303 ID.AddInteger(LabelID);
1304 void *IP = 0;
1305 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1306 return SDValue(E, 0);
1307 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
1308 new (N) LabelSDNode(Opcode, dl, Root, LabelID);
1309 CSEMap.InsertNode(N, IP);
1310 AllNodes.push_back(N);
1311 return SDValue(N, 0);
1312}
1313
Dan Gohman475871a2008-07-27 21:46:04 +00001314SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001315 assert((!V || isa<PointerType>(V->getType())) &&
1316 "SrcValue is not a pointer?");
1317
Jim Laskey583bd472006-10-27 23:46:08 +00001318 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001319 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001320 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001321
Chris Lattner61b09412006-08-11 21:01:22 +00001322 void *IP = 0;
1323 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001324 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001325
Dan Gohmanfed90b62008-07-28 21:51:04 +00001326 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001327 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001328 CSEMap.InsertNode(N, IP);
1329 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001330 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001331}
1332
Dan Gohman475871a2008-07-27 21:46:04 +00001333SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Evan Cheng24ac4082008-11-24 07:09:49 +00001334#ifndef NDEBUG
Dan Gohman69de1932008-02-06 22:27:42 +00001335 const Value *v = MO.getValue();
1336 assert((!v || isa<PointerType>(v->getType())) &&
1337 "SrcValue is not a pointer?");
Evan Cheng24ac4082008-11-24 07:09:49 +00001338#endif
Dan Gohman69de1932008-02-06 22:27:42 +00001339
1340 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001341 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001342 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001343
1344 void *IP = 0;
1345 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001346 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001347
Dan Gohmanfed90b62008-07-28 21:51:04 +00001348 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001349 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001350 CSEMap.InsertNode(N, IP);
1351 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001352 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001353}
1354
Duncan Sands92abc622009-01-31 15:50:11 +00001355/// getShiftAmountOperand - Return the specified value casted to
1356/// the target's desired shift amount type.
1357SDValue SelectionDAG::getShiftAmountOperand(SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001358 EVT OpTy = Op.getValueType();
Owen Anderson766b5ef2009-08-11 21:59:30 +00001359 MVT ShTy = TLI.getShiftAmountTy();
Duncan Sands92abc622009-01-31 15:50:11 +00001360 if (OpTy == ShTy || OpTy.isVector()) return Op;
1361
1362 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001363 return getNode(Opcode, Op.getDebugLoc(), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001364}
1365
Chris Lattner37ce9df2007-10-15 17:47:20 +00001366/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1367/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001368SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001369 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands9304f2c2008-12-04 18:08:40 +00001370 unsigned ByteSize = VT.getStoreSizeInBits()/8;
Owen Anderson23b9b192009-08-12 00:36:31 +00001371 const Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang364d73d2008-07-05 20:40:31 +00001372 unsigned StackAlign =
1373 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001374
Chris Lattner37ce9df2007-10-15 17:47:20 +00001375 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1376 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1377}
1378
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001379/// CreateStackTemporary - Create a stack temporary suitable for holding
1380/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001381SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001382 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1383 VT2.getStoreSizeInBits())/8;
Owen Anderson23b9b192009-08-12 00:36:31 +00001384 const Type *Ty1 = VT1.getTypeForEVT(*getContext());
1385 const Type *Ty2 = VT2.getTypeForEVT(*getContext());
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001386 const TargetData *TD = TLI.getTargetData();
1387 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1388 TD->getPrefTypeAlignment(Ty2));
1389
1390 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1391 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align);
1392 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1393}
1394
Owen Andersone50ed302009-08-10 22:56:29 +00001395SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001396 SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001397 // These setcc operations always fold.
1398 switch (Cond) {
1399 default: break;
1400 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001401 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001402 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001403 case ISD::SETTRUE2: return getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001404
Chris Lattnera83385f2006-04-27 05:01:07 +00001405 case ISD::SETOEQ:
1406 case ISD::SETOGT:
1407 case ISD::SETOGE:
1408 case ISD::SETOLT:
1409 case ISD::SETOLE:
1410 case ISD::SETONE:
1411 case ISD::SETO:
1412 case ISD::SETUO:
1413 case ISD::SETUEQ:
1414 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001415 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001416 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001417 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001418
Gabor Greifba36cb52008-08-28 21:40:38 +00001419 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001420 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001421 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001422 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001423
Chris Lattnerc3aae252005-01-07 07:46:32 +00001424 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001425 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001426 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1427 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001428 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1429 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1430 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1431 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1432 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1433 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1434 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1435 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001436 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001437 }
Chris Lattner67255a12005-04-07 18:14:58 +00001438 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001439 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1440 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001441 // No compile time operations on this type yet.
Owen Anderson825b72b2009-08-11 20:47:22 +00001442 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001443 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001444
1445 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001446 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001447 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001448 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001449 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001450 // fall through
1451 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001452 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001453 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001454 // fall through
1455 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001456 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001457 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001458 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001459 // fall through
1460 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001461 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001462 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001463 // fall through
1464 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001465 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001466 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001467 // fall through
1468 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001469 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001470 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001471 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001472 // fall through
1473 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001474 R==APFloat::cmpEqual, VT);
1475 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1476 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1477 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1478 R==APFloat::cmpEqual, VT);
1479 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1480 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1481 R==APFloat::cmpLessThan, VT);
1482 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1483 R==APFloat::cmpUnordered, VT);
1484 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1485 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001486 }
1487 } else {
1488 // Ensure that the constant occurs on the RHS.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001489 return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001490 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001491 }
1492
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001493 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001494 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001495}
1496
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001497/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1498/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001499bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001500 // This predicate is not safe for vector operations.
1501 if (Op.getValueType().isVector())
1502 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001503
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001504 unsigned BitWidth = Op.getValueSizeInBits();
1505 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1506}
1507
Dan Gohmanea859be2007-06-22 14:59:07 +00001508/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1509/// this predicate to simplify operations downstream. Mask is known to be zero
1510/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001511bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001512 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001513 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001514 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001515 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001516 return (KnownZero & Mask) == Mask;
1517}
1518
1519/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1520/// known to be either zero or one and return them in the KnownZero/KnownOne
1521/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1522/// processing.
Scott Michelfdc40a02009-02-17 22:15:04 +00001523void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001524 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001525 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001526 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001527 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001528 "Mask size mismatches value type size!");
1529
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001530 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001531 if (Depth == 6 || Mask == 0)
1532 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001533
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001534 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001535
1536 switch (Op.getOpcode()) {
1537 case ISD::Constant:
1538 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001539 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001540 KnownZero = ~KnownOne & Mask;
1541 return;
1542 case ISD::AND:
1543 // If either the LHS or the RHS are Zero, the result is zero.
1544 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001545 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1546 KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001547 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1548 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001549
1550 // Output known-1 bits are only known if set in both the LHS & RHS.
1551 KnownOne &= KnownOne2;
1552 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1553 KnownZero |= KnownZero2;
1554 return;
1555 case ISD::OR:
1556 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001557 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1558 KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001559 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1560 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1561
Dan Gohmanea859be2007-06-22 14:59:07 +00001562 // Output known-0 bits are only known if clear in both the LHS & RHS.
1563 KnownZero &= KnownZero2;
1564 // Output known-1 are known to be set if set in either the LHS | RHS.
1565 KnownOne |= KnownOne2;
1566 return;
1567 case ISD::XOR: {
1568 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1569 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001570 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1571 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1572
Dan Gohmanea859be2007-06-22 14:59:07 +00001573 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001574 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001575 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1576 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1577 KnownZero = KnownZeroOut;
1578 return;
1579 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001580 case ISD::MUL: {
1581 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1582 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1583 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1584 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1585 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1586
1587 // If low bits are zero in either operand, output low known-0 bits.
1588 // Also compute a conserative estimate for high known-0 bits.
1589 // More trickiness is possible, but this is sufficient for the
1590 // interesting case of alignment computation.
1591 KnownOne.clear();
1592 unsigned TrailZ = KnownZero.countTrailingOnes() +
1593 KnownZero2.countTrailingOnes();
1594 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001595 KnownZero2.countLeadingOnes(),
1596 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001597
1598 TrailZ = std::min(TrailZ, BitWidth);
1599 LeadZ = std::min(LeadZ, BitWidth);
1600 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1601 APInt::getHighBitsSet(BitWidth, LeadZ);
1602 KnownZero &= Mask;
1603 return;
1604 }
1605 case ISD::UDIV: {
1606 // For the purposes of computing leading zeros we can conservatively
1607 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001608 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001609 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1610 ComputeMaskedBits(Op.getOperand(0),
1611 AllOnes, KnownZero2, KnownOne2, Depth+1);
1612 unsigned LeadZ = KnownZero2.countLeadingOnes();
1613
1614 KnownOne2.clear();
1615 KnownZero2.clear();
1616 ComputeMaskedBits(Op.getOperand(1),
1617 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001618 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1619 if (RHSUnknownLeadingOnes != BitWidth)
1620 LeadZ = std::min(BitWidth,
1621 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001622
1623 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1624 return;
1625 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001626 case ISD::SELECT:
1627 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1628 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001629 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1630 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1631
Dan Gohmanea859be2007-06-22 14:59:07 +00001632 // Only known if known in both the LHS and RHS.
1633 KnownOne &= KnownOne2;
1634 KnownZero &= KnownZero2;
1635 return;
1636 case ISD::SELECT_CC:
1637 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1638 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001639 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1640 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1641
Dan Gohmanea859be2007-06-22 14:59:07 +00001642 // Only known if known in both the LHS and RHS.
1643 KnownOne &= KnownOne2;
1644 KnownZero &= KnownZero2;
1645 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001646 case ISD::SADDO:
1647 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001648 case ISD::SSUBO:
1649 case ISD::USUBO:
1650 case ISD::SMULO:
1651 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001652 if (Op.getResNo() != 1)
1653 return;
Duncan Sands03228082008-11-23 15:47:28 +00001654 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001655 case ISD::SETCC:
1656 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands03228082008-11-23 15:47:28 +00001657 if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent &&
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001658 BitWidth > 1)
1659 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001660 return;
1661 case ISD::SHL:
1662 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1663 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001664 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001665
1666 // If the shift count is an invalid immediate, don't do anything.
1667 if (ShAmt >= BitWidth)
1668 return;
1669
1670 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001671 KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001672 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001673 KnownZero <<= ShAmt;
1674 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001675 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001676 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001677 }
1678 return;
1679 case ISD::SRL:
1680 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1681 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001682 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001683
Dan Gohmand4cf9922008-02-26 18:50:50 +00001684 // If the shift count is an invalid immediate, don't do anything.
1685 if (ShAmt >= BitWidth)
1686 return;
1687
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001688 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001689 KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001690 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001691 KnownZero = KnownZero.lshr(ShAmt);
1692 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001693
Dan Gohman72d2fd52008-02-13 22:43:25 +00001694 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001695 KnownZero |= HighBits; // High bits known zero.
1696 }
1697 return;
1698 case ISD::SRA:
1699 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001700 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001701
Dan Gohmand4cf9922008-02-26 18:50:50 +00001702 // If the shift count is an invalid immediate, don't do anything.
1703 if (ShAmt >= BitWidth)
1704 return;
1705
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001706 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001707 // If any of the demanded bits are produced by the sign extension, we also
1708 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001709 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1710 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001711 InDemandedMask |= APInt::getSignBit(BitWidth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001712
Dan Gohmanea859be2007-06-22 14:59:07 +00001713 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1714 Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001715 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001716 KnownZero = KnownZero.lshr(ShAmt);
1717 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001718
Dan Gohmanea859be2007-06-22 14:59:07 +00001719 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001720 APInt SignBit = APInt::getSignBit(BitWidth);
1721 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00001722
Dan Gohmanca93a432008-02-20 16:30:17 +00001723 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001724 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001725 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001726 KnownOne |= HighBits; // New bits are known one.
1727 }
1728 }
1729 return;
1730 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001731 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001732 unsigned EBits = EVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001733
1734 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00001735 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001736 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001737
Dan Gohman977a76f2008-02-13 22:28:48 +00001738 APInt InSignBit = APInt::getSignBit(EBits);
1739 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00001740
Dan Gohmanea859be2007-06-22 14:59:07 +00001741 // If the sign extended bits are demanded, we know that the sign
1742 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001743 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001744 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001745 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00001746
Dan Gohmanea859be2007-06-22 14:59:07 +00001747 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1748 KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001749 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1750
Dan Gohmanea859be2007-06-22 14:59:07 +00001751 // If the sign bit of the input is known set or clear, then we know the
1752 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001753 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001754 KnownZero |= NewBits;
1755 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001756 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001757 KnownOne |= NewBits;
1758 KnownZero &= ~NewBits;
1759 } else { // Input sign bit unknown
1760 KnownZero &= ~NewBits;
1761 KnownOne &= ~NewBits;
1762 }
1763 return;
1764 }
1765 case ISD::CTTZ:
1766 case ISD::CTLZ:
1767 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001768 unsigned LowBits = Log2_32(BitWidth)+1;
1769 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001770 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001771 return;
1772 }
1773 case ISD::LOAD: {
Gabor Greifba36cb52008-08-28 21:40:38 +00001774 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001775 LoadSDNode *LD = cast<LoadSDNode>(Op);
Owen Andersone50ed302009-08-10 22:56:29 +00001776 EVT VT = LD->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001777 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001778 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001779 }
1780 return;
1781 }
1782 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001783 EVT InVT = Op.getOperand(0).getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001784 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001785 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1786 APInt InMask = Mask;
1787 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001788 KnownZero.trunc(InBits);
1789 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001790 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001791 KnownZero.zext(BitWidth);
1792 KnownOne.zext(BitWidth);
1793 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001794 return;
1795 }
1796 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001797 EVT InVT = Op.getOperand(0).getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001798 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001799 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001800 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1801 APInt InMask = Mask;
1802 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001803
1804 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001805 // bit is demanded. Temporarily set this bit in the mask for our callee.
1806 if (NewBits.getBoolValue())
1807 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001808
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001809 KnownZero.trunc(InBits);
1810 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001811 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1812
1813 // Note if the sign bit is known to be zero or one.
1814 bool SignBitKnownZero = KnownZero.isNegative();
1815 bool SignBitKnownOne = KnownOne.isNegative();
1816 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1817 "Sign bit can't be known to be both zero and one!");
1818
1819 // If the sign bit wasn't actually demanded by our caller, we don't
1820 // want it set in the KnownZero and KnownOne result values. Reset the
1821 // mask and reapply it to the result values.
1822 InMask = Mask;
1823 InMask.trunc(InBits);
1824 KnownZero &= InMask;
1825 KnownOne &= InMask;
1826
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001827 KnownZero.zext(BitWidth);
1828 KnownOne.zext(BitWidth);
1829
Dan Gohman977a76f2008-02-13 22:28:48 +00001830 // If the sign bit is known zero or one, the top bits match.
1831 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001832 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001833 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001834 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001835 return;
1836 }
1837 case ISD::ANY_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001838 EVT InVT = Op.getOperand(0).getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001839 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001840 APInt InMask = Mask;
1841 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001842 KnownZero.trunc(InBits);
1843 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001844 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001845 KnownZero.zext(BitWidth);
1846 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001847 return;
1848 }
1849 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00001850 EVT InVT = Op.getOperand(0).getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001851 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001852 APInt InMask = Mask;
1853 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001854 KnownZero.zext(InBits);
1855 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001856 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001857 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001858 KnownZero.trunc(BitWidth);
1859 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001860 break;
1861 }
1862 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00001863 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001864 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Scott Michelfdc40a02009-02-17 22:15:04 +00001865 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
Dan Gohmanea859be2007-06-22 14:59:07 +00001866 KnownOne, Depth+1);
1867 KnownZero |= (~InMask) & Mask;
1868 return;
1869 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001870 case ISD::FGETSIGN:
1871 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001872 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001873 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00001874
Dan Gohman23e8b712008-04-28 17:02:21 +00001875 case ISD::SUB: {
1876 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1877 // We know that the top bits of C-X are clear if X contains less bits
1878 // than C (i.e. no wrap-around can happen). For example, 20-X is
1879 // positive if we can prove that X is >= 0 and < 16.
1880 if (CLHS->getAPIntValue().isNonNegative()) {
1881 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1882 // NLZ can't be BitWidth with no sign bit
1883 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1884 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1885 Depth+1);
1886
1887 // If all of the MaskV bits are known to be zero, then we know the
1888 // output top bits are zero, because we now know that the output is
1889 // from [0-C].
1890 if ((KnownZero2 & MaskV) == MaskV) {
1891 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1892 // Top bits known zero.
1893 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1894 }
1895 }
1896 }
1897 }
1898 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001899 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001900 // Output known-0 bits are known if clear or set in both the low clear bits
1901 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1902 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001903 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1904 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001905 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00001906 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1907
1908 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001909 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00001910 KnownZeroOut = std::min(KnownZeroOut,
1911 KnownZero2.countTrailingOnes());
1912
1913 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001914 return;
1915 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001916 case ISD::SREM:
1917 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001918 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001919 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001920 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001921 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1922 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001923
Dan Gohmana60832b2008-08-13 23:12:35 +00001924 // If the sign bit of the first operand is zero, the sign bit of
1925 // the result is zero. If the first operand has no one bits below
1926 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001927 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1928 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001929
Dan Gohman23e8b712008-04-28 17:02:21 +00001930 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001931
1932 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001933 }
1934 }
1935 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001936 case ISD::UREM: {
1937 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001938 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001939 if (RA.isPowerOf2()) {
1940 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001941 APInt Mask2 = LowBits & Mask;
1942 KnownZero |= ~LowBits & Mask;
1943 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1944 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1945 break;
1946 }
1947 }
1948
1949 // Since the result is less than or equal to either operand, any leading
1950 // zero bits in either operand must also exist in the result.
1951 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1952 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1953 Depth+1);
1954 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1955 Depth+1);
1956
1957 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1958 KnownZero2.countLeadingOnes());
1959 KnownOne.clear();
1960 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1961 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001962 }
1963 default:
1964 // Allow the target to implement this method for its nodes.
1965 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1966 case ISD::INTRINSIC_WO_CHAIN:
1967 case ISD::INTRINSIC_W_CHAIN:
1968 case ISD::INTRINSIC_VOID:
Dan Gohman206ad102009-08-04 00:24:42 +00001969 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this,
1970 Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001971 }
1972 return;
1973 }
1974}
1975
1976/// ComputeNumSignBits - Return the number of times the sign bit of the
1977/// register is replicated into the other bits. We know that at least 1 bit
1978/// is always equal to the sign bit (itself), but other cases can give us
1979/// information. For example, immediately after an "SRA X, 2", we know that
1980/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001981unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Andersone50ed302009-08-10 22:56:29 +00001982 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001983 assert(VT.isInteger() && "Invalid VT!");
1984 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001985 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001986 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001987
Dan Gohmanea859be2007-06-22 14:59:07 +00001988 if (Depth == 6)
1989 return 1; // Limit search depth.
1990
1991 switch (Op.getOpcode()) {
1992 default: break;
1993 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001994 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001995 return VTBits-Tmp+1;
1996 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001997 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001998 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00001999
Dan Gohmanea859be2007-06-22 14:59:07 +00002000 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002001 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
2002 // If negative, return # leading ones.
2003 if (Val.isNegative())
2004 return Val.countLeadingOnes();
Scott Michelfdc40a02009-02-17 22:15:04 +00002005
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002006 // Return # leading zeros.
2007 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00002008 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002009
Dan Gohmanea859be2007-06-22 14:59:07 +00002010 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002011 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002012 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002013
Dan Gohmanea859be2007-06-22 14:59:07 +00002014 case ISD::SIGN_EXTEND_INREG:
2015 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002016 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002017 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002018
Dan Gohmanea859be2007-06-22 14:59:07 +00002019 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2020 return std::max(Tmp, Tmp2);
2021
2022 case ISD::SRA:
2023 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2024 // SRA X, C -> adds C sign bits.
2025 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002026 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002027 if (Tmp > VTBits) Tmp = VTBits;
2028 }
2029 return Tmp;
2030 case ISD::SHL:
2031 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2032 // shl destroys sign bits.
2033 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002034 if (C->getZExtValue() >= VTBits || // Bad shift.
2035 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2036 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002037 }
2038 break;
2039 case ISD::AND:
2040 case ISD::OR:
2041 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002042 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002043 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002044 if (Tmp != 1) {
2045 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2046 FirstAnswer = std::min(Tmp, Tmp2);
2047 // We computed what we know about the sign bits as our first
2048 // answer. Now proceed to the generic code that uses
2049 // ComputeMaskedBits, and pick whichever answer is better.
2050 }
2051 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002052
2053 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002054 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002055 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002056 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002057 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002058
2059 case ISD::SADDO:
2060 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002061 case ISD::SSUBO:
2062 case ISD::USUBO:
2063 case ISD::SMULO:
2064 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002065 if (Op.getResNo() != 1)
2066 break;
Duncan Sands03228082008-11-23 15:47:28 +00002067 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002068 case ISD::SETCC:
2069 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands03228082008-11-23 15:47:28 +00002070 if (TLI.getBooleanContents() ==
2071 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002072 return VTBits;
2073 break;
2074 case ISD::ROTL:
2075 case ISD::ROTR:
2076 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002077 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002078
Dan Gohmanea859be2007-06-22 14:59:07 +00002079 // Handle rotate right by N like a rotate left by 32-N.
2080 if (Op.getOpcode() == ISD::ROTR)
2081 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2082
2083 // If we aren't rotating out all of the known-in sign bits, return the
2084 // number that are left. This handles rotl(sext(x), 1) for example.
2085 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2086 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2087 }
2088 break;
2089 case ISD::ADD:
2090 // Add can have at most one carry bit. Thus we know that the output
2091 // is, at worst, one more bit than the inputs.
2092 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2093 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002094
Dan Gohmanea859be2007-06-22 14:59:07 +00002095 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002096 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002097 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002098 APInt KnownZero, KnownOne;
2099 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002100 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002101
Dan Gohmanea859be2007-06-22 14:59:07 +00002102 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2103 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002104 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002105 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002106
Dan Gohmanea859be2007-06-22 14:59:07 +00002107 // If we are subtracting one from a positive number, there is no carry
2108 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002109 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002110 return Tmp;
2111 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002112
Dan Gohmanea859be2007-06-22 14:59:07 +00002113 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2114 if (Tmp2 == 1) return 1;
2115 return std::min(Tmp, Tmp2)-1;
2116 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002117
Dan Gohmanea859be2007-06-22 14:59:07 +00002118 case ISD::SUB:
2119 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2120 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002121
Dan Gohmanea859be2007-06-22 14:59:07 +00002122 // Handle NEG.
2123 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002124 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002125 APInt KnownZero, KnownOne;
2126 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002127 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
2128 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2129 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002130 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002131 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002132
Dan Gohmanea859be2007-06-22 14:59:07 +00002133 // If the input is known to be positive (the sign bit is known clear),
2134 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002135 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002136 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002137
Dan Gohmanea859be2007-06-22 14:59:07 +00002138 // Otherwise, we treat this like a SUB.
2139 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002140
Dan Gohmanea859be2007-06-22 14:59:07 +00002141 // Sub can have at most one carry bit. Thus we know that the output
2142 // is, at worst, one more bit than the inputs.
2143 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2144 if (Tmp == 1) return 1; // Early out.
2145 return std::min(Tmp, Tmp2)-1;
2146 break;
2147 case ISD::TRUNCATE:
2148 // FIXME: it's tricky to do anything useful for this, but it is an important
2149 // case for targets like X86.
2150 break;
2151 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002152
Dan Gohmanea859be2007-06-22 14:59:07 +00002153 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2154 if (Op.getOpcode() == ISD::LOAD) {
2155 LoadSDNode *LD = cast<LoadSDNode>(Op);
2156 unsigned ExtType = LD->getExtensionType();
2157 switch (ExtType) {
2158 default: break;
2159 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00002160 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002161 return VTBits-Tmp+1;
2162 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00002163 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002164 return VTBits-Tmp;
2165 }
2166 }
2167
2168 // Allow the target to implement this method for its nodes.
2169 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002170 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002171 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2172 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2173 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002174 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002175 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002176
Dan Gohmanea859be2007-06-22 14:59:07 +00002177 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2178 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002179 APInt KnownZero, KnownOne;
2180 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002181 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002182
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002183 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002184 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002185 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002186 Mask = KnownOne;
2187 } else {
2188 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002189 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002190 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002191
Dan Gohmanea859be2007-06-22 14:59:07 +00002192 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2193 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002194 Mask = ~Mask;
2195 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002196 // Return # leading zeros. We use 'min' here in case Val was zero before
2197 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002198 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002199}
2200
Dan Gohman8d44b282009-09-03 20:34:31 +00002201bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2202 // If we're told that NaNs won't happen, assume they won't.
2203 if (FiniteOnlyFPMath())
2204 return true;
2205
2206 // If the value is a constant, we can obviously see if it is a NaN or not.
2207 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2208 return !C->getValueAPF().isNaN();
2209
2210 // TODO: Recognize more cases here.
2211
2212 return false;
2213}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002214
Dan Gohman475871a2008-07-27 21:46:04 +00002215bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00002216 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2217 if (!GA) return false;
Dan Gohman6520e202008-10-18 02:06:02 +00002218 if (GA->getOffset() != 0) return false;
Evan Chenga844bde2008-02-02 04:07:54 +00002219 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2220 if (!GV) return false;
Devang Patel35fe7342009-01-13 22:54:57 +00002221 MachineModuleInfo *MMI = getMachineModuleInfo();
Devang Patelbbdc8202009-01-13 23:54:55 +00002222 return MMI && MMI->hasDebugInfo();
Evan Chenga844bde2008-02-02 04:07:54 +00002223}
2224
2225
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002226/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2227/// element of the result of the vector shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002228SDValue SelectionDAG::getShuffleScalarElt(const ShuffleVectorSDNode *N,
2229 unsigned i) {
Owen Andersone50ed302009-08-10 22:56:29 +00002230 EVT VT = N->getValueType(0);
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002231 DebugLoc dl = N->getDebugLoc();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002232 if (N->getMaskElt(i) < 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002233 return getUNDEF(VT.getVectorElementType());
Nate Begeman5a5ca152009-04-29 05:20:52 +00002234 unsigned Index = N->getMaskElt(i);
2235 unsigned NumElems = VT.getVectorNumElements();
Dan Gohman475871a2008-07-27 21:46:04 +00002236 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00002237 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002238
2239 if (V.getOpcode() == ISD::BIT_CONVERT) {
2240 V = V.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002241 EVT VVT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00002242 if (!VVT.isVector() || VVT.getVectorNumElements() != (unsigned)NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00002243 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002244 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002245 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002246 return (Index == 0) ? V.getOperand(0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002247 : getUNDEF(VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00002248 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002249 return V.getOperand(Index);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002250 if (const ShuffleVectorSDNode *SVN = dyn_cast<ShuffleVectorSDNode>(V))
2251 return getShuffleScalarElt(SVN, Index);
Dan Gohman475871a2008-07-27 21:46:04 +00002252 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002253}
2254
2255
Chris Lattnerc3aae252005-01-07 07:46:32 +00002256/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002257///
Owen Andersone50ed302009-08-10 22:56:29 +00002258SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002259 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002260 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002261 void *IP = 0;
2262 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002263 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002264 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohmanfc166572009-04-09 23:54:40 +00002265 new (N) SDNode(Opcode, DL, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002266 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002267
Chris Lattner4a283e92006-08-11 18:38:11 +00002268 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002269#ifndef NDEBUG
2270 VerifyNode(N);
2271#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002272 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002273}
2274
Bill Wendling7ade28c2009-01-28 22:17:52 +00002275SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002276 EVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002277 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002278 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002279 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002280 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002281 switch (Opcode) {
2282 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002283 case ISD::SIGN_EXTEND:
2284 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002285 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002286 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002287 case ISD::TRUNCATE:
2288 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002289 case ISD::UINT_TO_FP:
2290 case ISD::SINT_TO_FP: {
2291 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002292 // No compile time operations on this type.
Owen Anderson825b72b2009-08-11 20:47:22 +00002293 if (VT==MVT::ppcf128)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002294 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002295 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
Scott Michelfdc40a02009-02-17 22:15:04 +00002296 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002297 Opcode==ISD::SINT_TO_FP,
2298 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002299 return getConstantFP(apf, VT);
2300 }
Chris Lattner94683772005-12-23 05:30:37 +00002301 case ISD::BIT_CONVERT:
Owen Anderson825b72b2009-08-11 20:47:22 +00002302 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002303 return getConstantFP(Val.bitsToFloat(), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002304 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002305 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002306 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002307 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002308 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002309 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002310 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002311 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002312 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002313 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002314 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002315 }
2316 }
2317
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002318 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002319 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002320 APFloat V = C->getValueAPF(); // make copy
Owen Anderson825b72b2009-08-11 20:47:22 +00002321 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002322 switch (Opcode) {
2323 case ISD::FNEG:
2324 V.changeSign();
2325 return getConstantFP(V, VT);
2326 case ISD::FABS:
2327 V.clearSign();
2328 return getConstantFP(V, VT);
2329 case ISD::FP_ROUND:
Dale Johannesen23a98552008-10-09 23:00:39 +00002330 case ISD::FP_EXTEND: {
2331 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002332 // This can return overflow, underflow, or inexact; we don't care.
2333 // FIXME need to be more flexible about rounding mode.
Owen Andersone50ed302009-08-10 22:56:29 +00002334 (void)V.convert(*EVTToAPFloatSemantics(VT),
Dale Johannesen23a98552008-10-09 23:00:39 +00002335 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002336 return getConstantFP(V, VT);
Dale Johannesen23a98552008-10-09 23:00:39 +00002337 }
Dale Johannesendb44bf82007-10-16 23:38:29 +00002338 case ISD::FP_TO_SINT:
2339 case ISD::FP_TO_UINT: {
Dale Johannesen2f91f302009-04-24 21:34:13 +00002340 integerPart x[2];
Dale Johannesen23a98552008-10-09 23:00:39 +00002341 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002342 assert(integerPartWidth >= 64);
2343 // FIXME need to be more flexible about rounding mode.
Dale Johannesen2f91f302009-04-24 21:34:13 +00002344 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
Dale Johannesendb44bf82007-10-16 23:38:29 +00002345 Opcode==ISD::FP_TO_SINT,
Dale Johannesen23a98552008-10-09 23:00:39 +00002346 APFloat::rmTowardZero, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002347 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2348 break;
Dale Johannesen2f91f302009-04-24 21:34:13 +00002349 APInt api(VT.getSizeInBits(), 2, x);
2350 return getConstant(api, VT);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002351 }
2352 case ISD::BIT_CONVERT:
Owen Anderson825b72b2009-08-11 20:47:22 +00002353 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen23a98552008-10-09 23:00:39 +00002354 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002355 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen23a98552008-10-09 23:00:39 +00002356 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002357 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002358 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002359 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002360 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002361
Gabor Greifba36cb52008-08-28 21:40:38 +00002362 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002363 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002364 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002365 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002366 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002367 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002368 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002369 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002370 assert(VT.isFloatingPoint() &&
2371 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002372 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002373 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002374 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002375 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002376 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002377 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002378 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002379 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002380 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002381 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002382 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002383 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002384 break;
2385 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002386 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002387 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002388 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002389 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002390 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002391 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002392 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002393 Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002394 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002395 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002396 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002397 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002398 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002399 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002400 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002401 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2402 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002403 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002404 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002405 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002406 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002407 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002408 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002409 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002410 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002411 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002412 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002413 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2414 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002415 // If the source is smaller than the dest, we still need an extend.
Gabor Greifba36cb52008-08-28 21:40:38 +00002416 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002417 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002418 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002419 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002420 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002421 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002422 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002423 break;
Chris Lattner35481892005-12-23 00:16:34 +00002424 case ISD::BIT_CONVERT:
2425 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002426 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002427 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002428 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002429 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002430 return getNode(ISD::BIT_CONVERT, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002431 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002432 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002433 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002434 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002435 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002436 (VT.getVectorElementType() == Operand.getValueType() ||
2437 (VT.getVectorElementType().isInteger() &&
2438 Operand.getValueType().isInteger() &&
2439 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002440 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002441 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002442 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002443 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2444 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2445 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2446 Operand.getConstantOperandVal(1) == 0 &&
2447 Operand.getOperand(0).getValueType() == VT)
2448 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002449 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002450 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002451 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
2452 if (UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002453 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002454 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002455 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002456 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002457 break;
2458 case ISD::FABS:
2459 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002460 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002461 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002462 }
2463
Chris Lattner43247a12005-08-25 19:12:10 +00002464 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002465 SDVTList VTs = getVTList(VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002466 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002467 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002468 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002469 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002470 void *IP = 0;
2471 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002472 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002473 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002474 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002475 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002476 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002477 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002478 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002479 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002480
Chris Lattnerc3aae252005-01-07 07:46:32 +00002481 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002482#ifndef NDEBUG
2483 VerifyNode(N);
2484#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002485 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002486}
2487
Bill Wendling688d1c42008-09-24 10:16:24 +00002488SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
Owen Andersone50ed302009-08-10 22:56:29 +00002489 EVT VT,
Bill Wendling688d1c42008-09-24 10:16:24 +00002490 ConstantSDNode *Cst1,
2491 ConstantSDNode *Cst2) {
2492 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2493
2494 switch (Opcode) {
2495 case ISD::ADD: return getConstant(C1 + C2, VT);
2496 case ISD::SUB: return getConstant(C1 - C2, VT);
2497 case ISD::MUL: return getConstant(C1 * C2, VT);
2498 case ISD::UDIV:
2499 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2500 break;
2501 case ISD::UREM:
2502 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2503 break;
2504 case ISD::SDIV:
2505 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2506 break;
2507 case ISD::SREM:
2508 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2509 break;
2510 case ISD::AND: return getConstant(C1 & C2, VT);
2511 case ISD::OR: return getConstant(C1 | C2, VT);
2512 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2513 case ISD::SHL: return getConstant(C1 << C2, VT);
2514 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2515 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2516 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2517 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2518 default: break;
2519 }
2520
2521 return SDValue();
2522}
2523
Owen Andersone50ed302009-08-10 22:56:29 +00002524SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002525 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002526 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2527 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002528 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002529 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002530 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002531 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2532 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002533 // Fold trivial token factors.
2534 if (N1.getOpcode() == ISD::EntryToken) return N2;
2535 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002536 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002537 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002538 case ISD::CONCAT_VECTORS:
2539 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2540 // one big BUILD_VECTOR.
2541 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2542 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002543 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2544 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002545 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002546 }
2547 break;
Chris Lattner76365122005-01-16 02:23:22 +00002548 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002549 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002550 N1.getValueType() == VT && "Binary operator types must match!");
2551 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2552 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002553 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002554 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002555 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2556 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002557 break;
Chris Lattner76365122005-01-16 02:23:22 +00002558 case ISD::OR:
2559 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002560 case ISD::ADD:
2561 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002562 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002563 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002564 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2565 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002566 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002567 return N1;
2568 break;
Chris Lattner76365122005-01-16 02:23:22 +00002569 case ISD::UDIV:
2570 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002571 case ISD::MULHU:
2572 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002573 case ISD::MUL:
2574 case ISD::SDIV:
2575 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002576 assert(VT.isInteger() && "This operator does not apply to FP types!");
2577 // fall through
Chris Lattner01b3d732005-09-28 22:28:18 +00002578 case ISD::FADD:
2579 case ISD::FSUB:
2580 case ISD::FMUL:
2581 case ISD::FDIV:
2582 case ISD::FREM:
Dan Gohmana90c8e62009-01-23 19:10:37 +00002583 if (UnsafeFPMath) {
2584 if (Opcode == ISD::FADD) {
2585 // 0+x --> x
2586 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2587 if (CFP->getValueAPF().isZero())
2588 return N2;
2589 // x+0 --> x
2590 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2591 if (CFP->getValueAPF().isZero())
2592 return N1;
2593 } else if (Opcode == ISD::FSUB) {
2594 // x-0 --> x
2595 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2596 if (CFP->getValueAPF().isZero())
2597 return N1;
2598 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002599 }
Chris Lattner76365122005-01-16 02:23:22 +00002600 assert(N1.getValueType() == N2.getValueType() &&
2601 N1.getValueType() == VT && "Binary operator types must match!");
2602 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002603 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2604 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002605 N1.getValueType().isFloatingPoint() &&
2606 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002607 "Invalid FCOPYSIGN!");
2608 break;
Chris Lattner76365122005-01-16 02:23:22 +00002609 case ISD::SHL:
2610 case ISD::SRA:
2611 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002612 case ISD::ROTL:
2613 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002614 assert(VT == N1.getValueType() &&
2615 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002616 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002617 "Shifts only work on integers");
2618
2619 // Always fold shifts of i1 values so the code generator doesn't need to
2620 // handle them. Since we know the size of the shift has to be less than the
2621 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00002622 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00002623 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002624 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002625 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002626 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002627 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002628 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002629 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002630 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002631 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002632 break;
2633 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002634 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002635 assert(VT.isFloatingPoint() &&
2636 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002637 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002638 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002639 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002640 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002641 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002642 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002643 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002644 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002645 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002646 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002647 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002648 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002649 break;
2650 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002651 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002652 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002653 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002654 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002655 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002656 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002657 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002658
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002659 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002660 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002661 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002662 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002663 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002664 return getConstant(Val, VT);
2665 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002666 break;
2667 }
2668 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002669 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2670 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002671 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002672
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002673 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2674 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002675 if (N2C &&
2676 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002677 N1.getNumOperands() > 0) {
2678 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002679 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002680 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002681 N1.getOperand(N2C->getZExtValue() / Factor),
2682 getConstant(N2C->getZExtValue() % Factor,
2683 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002684 }
2685
2686 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2687 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00002688 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
2689 SDValue Elt = N1.getOperand(N2C->getZExtValue());
Owen Andersone50ed302009-08-10 22:56:29 +00002690 EVT VEltTy = N1.getValueType().getVectorElementType();
Eli Friedmanc680ac92009-07-09 22:01:03 +00002691 if (Elt.getValueType() != VEltTy) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00002692 // If the vector element type is not legal, the BUILD_VECTOR operands
2693 // are promoted and implicitly truncated. Make that explicit here.
Eli Friedmanc680ac92009-07-09 22:01:03 +00002694 Elt = getNode(ISD::TRUNCATE, DL, VEltTy, Elt);
2695 }
2696 if (VT != VEltTy) {
2697 // If the vector element type is not legal, the EXTRACT_VECTOR_ELT
2698 // result is implicitly extended.
2699 Elt = getNode(ISD::ANY_EXTEND, DL, VT, Elt);
Bob Wilsonb1303d02009-04-13 22:05:19 +00002700 }
2701 return Elt;
2702 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002703
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002704 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2705 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002706 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Dan Gohman197e88f2009-01-29 16:10:46 +00002707 // If the indices are the same, return the inserted element.
Dan Gohman6ab64222008-08-13 21:51:37 +00002708 if (N1.getOperand(2) == N2)
2709 return N1.getOperand(1);
Dan Gohman197e88f2009-01-29 16:10:46 +00002710 // If the indices are known different, extract the element from
2711 // the original vector.
2712 else if (isa<ConstantSDNode>(N1.getOperand(2)) &&
2713 isa<ConstantSDNode>(N2))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002714 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Dan Gohman6ab64222008-08-13 21:51:37 +00002715 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002716 break;
2717 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002718 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002719 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2720 (N1.getValueType().isInteger() == VT.isInteger()) &&
2721 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002722
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002723 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2724 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00002725 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002726 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002727 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002728
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002729 // EXTRACT_ELEMENT of a constant int is also very common.
2730 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002731 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002732 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002733 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2734 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002735 }
2736 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002737 case ISD::EXTRACT_SUBVECTOR:
2738 if (N1.getValueType() == VT) // Trivial extraction.
2739 return N1;
2740 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002741 }
2742
2743 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002744 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00002745 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2746 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002747 } else { // Cannonicalize constant to RHS if commutative
2748 if (isCommutativeBinOp(Opcode)) {
2749 std::swap(N1C, N2C);
2750 std::swap(N1, N2);
2751 }
2752 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002753 }
2754
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002755 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002756 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2757 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002758 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002759 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2760 // Cannonicalize constant to RHS if commutative
2761 std::swap(N1CFP, N2CFP);
2762 std::swap(N1, N2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002763 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002764 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2765 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002766 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002767 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002768 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002769 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002770 return getConstantFP(V1, VT);
2771 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002772 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002773 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2774 if (s!=APFloat::opInvalidOp)
2775 return getConstantFP(V1, VT);
2776 break;
2777 case ISD::FMUL:
2778 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2779 if (s!=APFloat::opInvalidOp)
2780 return getConstantFP(V1, VT);
2781 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002782 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002783 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2784 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2785 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002786 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002787 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002788 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2789 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2790 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002791 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002792 case ISD::FCOPYSIGN:
2793 V1.copySign(V2);
2794 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002795 default: break;
2796 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002797 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002798 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002799
Chris Lattner62b57722006-04-20 05:39:12 +00002800 // Canonicalize an UNDEF to the RHS, even over a constant.
2801 if (N1.getOpcode() == ISD::UNDEF) {
2802 if (isCommutativeBinOp(Opcode)) {
2803 std::swap(N1, N2);
2804 } else {
2805 switch (Opcode) {
2806 case ISD::FP_ROUND_INREG:
2807 case ISD::SIGN_EXTEND_INREG:
2808 case ISD::SUB:
2809 case ISD::FSUB:
2810 case ISD::FDIV:
2811 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002812 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002813 return N1; // fold op(undef, arg2) -> undef
2814 case ISD::UDIV:
2815 case ISD::SDIV:
2816 case ISD::UREM:
2817 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002818 case ISD::SRL:
2819 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002820 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002821 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2822 // For vectors, we can't easily build an all zero vector, just return
2823 // the LHS.
2824 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002825 }
2826 }
2827 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002828
2829 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002830 if (N2.getOpcode() == ISD::UNDEF) {
2831 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002832 case ISD::XOR:
2833 if (N1.getOpcode() == ISD::UNDEF)
2834 // Handle undef ^ undef -> 0 special case. This is a common
2835 // idiom (misuse).
2836 return getConstant(0, VT);
2837 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002838 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002839 case ISD::ADDC:
2840 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002841 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00002842 case ISD::UDIV:
2843 case ISD::SDIV:
2844 case ISD::UREM:
2845 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002846 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00002847 case ISD::FADD:
2848 case ISD::FSUB:
2849 case ISD::FMUL:
2850 case ISD::FDIV:
2851 case ISD::FREM:
2852 if (UnsafeFPMath)
2853 return N2;
2854 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002855 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00002856 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002857 case ISD::SRL:
2858 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002859 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002860 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2861 // For vectors, we can't easily build an all zero vector, just return
2862 // the LHS.
2863 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002864 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002865 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002866 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002867 // For vectors, we can't easily build an all one vector, just return
2868 // the LHS.
2869 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002870 case ISD::SRA:
2871 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002872 }
2873 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002874
Chris Lattner27e9b412005-05-11 18:57:39 +00002875 // Memoize this node if possible.
2876 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002877 SDVTList VTs = getVTList(VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002878 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002879 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002880 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002881 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002882 void *IP = 0;
2883 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002884 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002885 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002886 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002887 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002888 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002889 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002890 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002891 }
2892
Chris Lattnerc3aae252005-01-07 07:46:32 +00002893 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002894#ifndef NDEBUG
2895 VerifyNode(N);
2896#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002897 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002898}
2899
Owen Andersone50ed302009-08-10 22:56:29 +00002900SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002901 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002902 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00002903 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2904 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002905 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002906 case ISD::CONCAT_VECTORS:
2907 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2908 // one big BUILD_VECTOR.
2909 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2910 N2.getOpcode() == ISD::BUILD_VECTOR &&
2911 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002912 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2913 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2914 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002915 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002916 }
2917 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002918 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002919 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002920 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002921 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002922 break;
2923 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002924 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002925 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002926 if (N1C->getZExtValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002927 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002928 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002929 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002930 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002931
2932 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002933 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002934 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002935 if (N2C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002936 if (N2C->getZExtValue()) // Unconditional branch
Owen Anderson825b72b2009-08-11 20:47:22 +00002937 return getNode(ISD::BR, DL, MVT::Other, N1, N3);
Chris Lattner5351e9b2005-01-07 22:49:57 +00002938 else
2939 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002940 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002941 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002942 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00002943 llvm_unreachable("should use getVectorShuffle constructor!");
Chris Lattnerfb194b92006-03-19 23:56:04 +00002944 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002945 case ISD::BIT_CONVERT:
2946 // Fold bit_convert nodes from a type to themselves.
2947 if (N1.getValueType() == VT)
2948 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002949 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002950 }
2951
Chris Lattner43247a12005-08-25 19:12:10 +00002952 // Memoize node if it doesn't produce a flag.
2953 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002954 SDVTList VTs = getVTList(VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002955 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002956 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002957 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002958 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002959 void *IP = 0;
2960 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002961 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002962 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002963 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002964 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002965 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002966 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002967 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002968 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002969 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002970#ifndef NDEBUG
2971 VerifyNode(N);
2972#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002973 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002974}
2975
Owen Andersone50ed302009-08-10 22:56:29 +00002976SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002977 SDValue N1, SDValue N2, SDValue N3,
2978 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00002979 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00002980 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002981}
2982
Owen Andersone50ed302009-08-10 22:56:29 +00002983SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002984 SDValue N1, SDValue N2, SDValue N3,
2985 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00002986 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00002987 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002988}
2989
Dan Gohman98ca4f22009-08-05 01:29:28 +00002990/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
2991/// the incoming stack arguments to be loaded from the stack.
2992SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
2993 SmallVector<SDValue, 8> ArgChains;
2994
2995 // Include the original chain at the beginning of the list. When this is
2996 // used by target LowerCall hooks, this helps legalize find the
2997 // CALLSEQ_BEGIN node.
2998 ArgChains.push_back(Chain);
2999
3000 // Add a chain value for each stack argument.
3001 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3002 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3003 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3004 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3005 if (FI->getIndex() < 0)
3006 ArgChains.push_back(SDValue(L, 1));
3007
3008 // Build a tokenfactor for all the chains.
Owen Anderson825b72b2009-08-11 20:47:22 +00003009 return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003010 &ArgChains[0], ArgChains.size());
3011}
3012
Dan Gohman707e0182008-04-12 04:36:06 +00003013/// getMemsetValue - Vectorized representation of the memset value
3014/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003015static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Dale Johannesen0f502f62009-02-03 22:26:09 +00003016 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003017 unsigned NumBits = VT.isVector() ?
3018 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003019 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003020 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00003021 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003022 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00003023 Val = (Val << Shift) | Val;
3024 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00003025 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003026 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003027 return DAG.getConstant(Val, VT);
3028 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003029 }
Evan Chengf0df0312008-05-15 08:39:06 +00003030
Duncan Sands3d0f5af2008-10-30 20:26:50 +00003031 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dale Johannesen0f502f62009-02-03 22:26:09 +00003032 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Evan Chengf0df0312008-05-15 08:39:06 +00003033 unsigned Shift = 8;
3034 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00003035 Value = DAG.getNode(ISD::OR, dl, VT,
3036 DAG.getNode(ISD::SHL, dl, VT, Value,
Duncan Sands3d0f5af2008-10-30 20:26:50 +00003037 DAG.getConstant(Shift,
3038 TLI.getShiftAmountTy())),
3039 Value);
Evan Chengf0df0312008-05-15 08:39:06 +00003040 Shift <<= 1;
3041 }
3042
3043 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003044}
3045
Dan Gohman707e0182008-04-12 04:36:06 +00003046/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3047/// used when a memcpy is turned into a memset when the source is a constant
3048/// string ptr.
Owen Andersone50ed302009-08-10 22:56:29 +00003049static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00003050 const TargetLowering &TLI,
3051 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003052 // Handle vector with all elements zero.
3053 if (Str.empty()) {
3054 if (VT.isInteger())
3055 return DAG.getConstant(0, VT);
3056 unsigned NumElts = VT.getVectorNumElements();
Owen Anderson766b5ef2009-08-11 21:59:30 +00003057 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Dale Johannesen0f502f62009-02-03 22:26:09 +00003058 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson23b9b192009-08-12 00:36:31 +00003059 DAG.getConstant(0,
3060 EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts)));
Evan Cheng0ff39b32008-06-30 07:31:25 +00003061 }
3062
Duncan Sands83ec4b62008-06-06 12:08:01 +00003063 assert(!VT.isVector() && "Can't handle vector type here!");
3064 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00003065 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003066 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003067 if (TLI.isLittleEndian())
3068 Offset = Offset + MSB - 1;
3069 for (unsigned i = 0; i != MSB; ++i) {
3070 Val = (Val << 8) | (unsigned char)Str[Offset];
3071 Offset += TLI.isLittleEndian() ? -1 : 1;
3072 }
3073 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003074}
3075
Scott Michelfdc40a02009-02-17 22:15:04 +00003076/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003077///
Dan Gohman475871a2008-07-27 21:46:04 +00003078static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00003079 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003080 EVT VT = Base.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003081 return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003082 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003083}
3084
Evan Chengf0df0312008-05-15 08:39:06 +00003085/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3086///
Dan Gohman475871a2008-07-27 21:46:04 +00003087static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003088 unsigned SrcDelta = 0;
3089 GlobalAddressSDNode *G = NULL;
3090 if (Src.getOpcode() == ISD::GlobalAddress)
3091 G = cast<GlobalAddressSDNode>(Src);
3092 else if (Src.getOpcode() == ISD::ADD &&
3093 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3094 Src.getOperand(1).getOpcode() == ISD::Constant) {
3095 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003096 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003097 }
3098 if (!G)
3099 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003100
Evan Chengf0df0312008-05-15 08:39:06 +00003101 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Bill Wendling0582ae92009-03-13 04:39:26 +00003102 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
3103 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00003104
Evan Chengf0df0312008-05-15 08:39:06 +00003105 return false;
3106}
Dan Gohman707e0182008-04-12 04:36:06 +00003107
Evan Chengf0df0312008-05-15 08:39:06 +00003108/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
3109/// to replace the memset / memcpy is below the threshold. It also returns the
3110/// types of the sequence of memory ops to perform memset / memcpy.
3111static
Owen Andersone50ed302009-08-10 22:56:29 +00003112bool MeetsMaxMemopRequirement(std::vector<EVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00003113 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00003114 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003115 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00003116 SelectionDAG &DAG,
3117 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003118 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00003119 bool isSrcConst = isa<ConstantSDNode>(Src);
Owen Andersone50ed302009-08-10 22:56:29 +00003120 EVT VT = TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr, DAG);
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00003121 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses(VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00003122 if (VT != MVT::iAny) {
Evan Cheng8a9d5f92009-08-15 23:41:42 +00003123 const Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3124 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
Evan Chengf0df0312008-05-15 08:39:06 +00003125 // If source is a string constant, this will require an unaligned load.
3126 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
3127 if (Dst.getOpcode() != ISD::FrameIndex) {
3128 // Can't change destination alignment. It requires a unaligned store.
3129 if (AllowUnalign)
Owen Anderson825b72b2009-08-11 20:47:22 +00003130 VT = MVT::iAny;
Evan Chengf0df0312008-05-15 08:39:06 +00003131 } else {
3132 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
3133 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3134 if (MFI->isFixedObjectIndex(FI)) {
3135 // Can't change destination alignment. It requires a unaligned store.
3136 if (AllowUnalign)
Owen Anderson825b72b2009-08-11 20:47:22 +00003137 VT = MVT::iAny;
Evan Chengf0df0312008-05-15 08:39:06 +00003138 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00003139 // Give the stack frame object a larger alignment if needed.
3140 if (MFI->getObjectAlignment(FI) < NewAlign)
3141 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00003142 Align = NewAlign;
3143 }
3144 }
3145 }
3146 }
3147
Owen Anderson825b72b2009-08-11 20:47:22 +00003148 if (VT == MVT::iAny) {
Evan Cheng8a9d5f92009-08-15 23:41:42 +00003149 if (TLI.allowsUnalignedMemoryAccesses(MVT::i64)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 VT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003151 } else {
3152 switch (Align & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003153 case 0: VT = MVT::i64; break;
3154 case 4: VT = MVT::i32; break;
3155 case 2: VT = MVT::i16; break;
3156 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003157 }
3158 }
3159
Owen Anderson766b5ef2009-08-11 21:59:30 +00003160 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003161 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003162 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003163 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003164
Duncan Sands8e4eb092008-06-08 20:54:56 +00003165 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003166 VT = LVT;
3167 }
Dan Gohman707e0182008-04-12 04:36:06 +00003168
3169 unsigned NumMemOps = 0;
3170 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003171 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003172 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003173 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003174 if (VT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003175 VT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003176 while (!TLI.isTypeLegal(VT))
Owen Anderson825b72b2009-08-11 20:47:22 +00003177 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003178 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003179 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003180 // This can result in a type that is not legal on the target, e.g.
3181 // 1 or 2 bytes on PPC.
Owen Anderson825b72b2009-08-11 20:47:22 +00003182 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00003183 VTSize >>= 1;
3184 }
Dan Gohman707e0182008-04-12 04:36:06 +00003185 }
Dan Gohman707e0182008-04-12 04:36:06 +00003186
3187 if (++NumMemOps > Limit)
3188 return false;
3189 MemOps.push_back(VT);
3190 Size -= VTSize;
3191 }
3192
3193 return true;
3194}
3195
Dale Johannesen0f502f62009-02-03 22:26:09 +00003196static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00003197 SDValue Chain, SDValue Dst,
3198 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00003199 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003200 const Value *DstSV, uint64_t DstSVOff,
3201 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00003202 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3203
Dan Gohman21323f32008-05-29 19:42:22 +00003204 // Expand memcpy to a series of load and store ops if the size operand falls
3205 // below a certain threshold.
Owen Andersone50ed302009-08-10 22:56:29 +00003206 std::vector<EVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00003207 uint64_t Limit = -1ULL;
Dan Gohman707e0182008-04-12 04:36:06 +00003208 if (!AlwaysInline)
3209 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00003210 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003211 std::string Str;
3212 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003213 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003214 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003215 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003216
Dan Gohman707e0182008-04-12 04:36:06 +00003217
Evan Cheng0ff39b32008-06-30 07:31:25 +00003218 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00003219 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003220 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003221 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003222 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003223 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003224 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003225 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003226
Evan Cheng0ff39b32008-06-30 07:31:25 +00003227 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00003228 // It's unlikely a store of a vector immediate can be done in a single
3229 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003230 // We also handle store a vector with all zero's.
3231 // FIXME: Handle other cases where store of vector immediate is done in
3232 // a single instruction.
Dale Johannesen0f502f62009-02-03 22:26:09 +00003233 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str, SrcOff);
3234 Store = DAG.getStore(Chain, dl, Value,
Evan Chengf0df0312008-05-15 08:39:06 +00003235 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00003236 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00003237 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003238 // The type might not be legal for the target. This should only happen
3239 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003240 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3241 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003242 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003243 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003244 assert(NVT.bitsGE(VT));
3245 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
3246 getMemBasePlusOffset(Src, SrcOff, DAG),
3247 SrcSV, SrcSVOff + SrcOff, VT, false, Align);
3248 Store = DAG.getTruncStore(Chain, dl, Value,
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003249 getMemBasePlusOffset(Dst, DstOff, DAG),
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003250 DstSV, DstSVOff + DstOff, VT, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00003251 }
3252 OutChains.push_back(Store);
3253 SrcOff += VTSize;
3254 DstOff += VTSize;
3255 }
3256
Owen Anderson825b72b2009-08-11 20:47:22 +00003257 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003258 &OutChains[0], OutChains.size());
3259}
3260
Dale Johannesen0f502f62009-02-03 22:26:09 +00003261static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00003262 SDValue Chain, SDValue Dst,
3263 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00003264 unsigned Align, bool AlwaysInline,
3265 const Value *DstSV, uint64_t DstSVOff,
3266 const Value *SrcSV, uint64_t SrcSVOff){
3267 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3268
3269 // Expand memmove to a series of load and store ops if the size operand falls
3270 // below a certain threshold.
Owen Andersone50ed302009-08-10 22:56:29 +00003271 std::vector<EVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00003272 uint64_t Limit = -1ULL;
Dan Gohman21323f32008-05-29 19:42:22 +00003273 if (!AlwaysInline)
3274 Limit = TLI.getMaxStoresPerMemmove();
3275 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003276 std::string Str;
3277 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00003278 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003279 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003280 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003281
Dan Gohman21323f32008-05-29 19:42:22 +00003282 uint64_t SrcOff = 0, DstOff = 0;
3283
Dan Gohman475871a2008-07-27 21:46:04 +00003284 SmallVector<SDValue, 8> LoadValues;
3285 SmallVector<SDValue, 8> LoadChains;
3286 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003287 unsigned NumMemOps = MemOps.size();
3288 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003289 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003290 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003291 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003292
Dale Johannesen0f502f62009-02-03 22:26:09 +00003293 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman21323f32008-05-29 19:42:22 +00003294 getMemBasePlusOffset(Src, SrcOff, DAG),
3295 SrcSV, SrcSVOff + SrcOff, false, Align);
3296 LoadValues.push_back(Value);
3297 LoadChains.push_back(Value.getValue(1));
3298 SrcOff += VTSize;
3299 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003300 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003301 &LoadChains[0], LoadChains.size());
3302 OutChains.clear();
3303 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003304 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003305 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003306 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003307
Dale Johannesen0f502f62009-02-03 22:26:09 +00003308 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman21323f32008-05-29 19:42:22 +00003309 getMemBasePlusOffset(Dst, DstOff, DAG),
3310 DstSV, DstSVOff + DstOff, false, DstAlign);
3311 OutChains.push_back(Store);
3312 DstOff += VTSize;
3313 }
3314
Owen Anderson825b72b2009-08-11 20:47:22 +00003315 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003316 &OutChains[0], OutChains.size());
3317}
3318
Dale Johannesen0f502f62009-02-03 22:26:09 +00003319static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00003320 SDValue Chain, SDValue Dst,
3321 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00003322 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003323 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003324 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3325
3326 // Expand memset to a series of load/store ops if the size operand
3327 // falls below a certain threshold.
Owen Andersone50ed302009-08-10 22:56:29 +00003328 std::vector<EVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00003329 std::string Str;
3330 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003331 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00003332 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003333 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003334
Dan Gohman475871a2008-07-27 21:46:04 +00003335 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003336 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003337
3338 unsigned NumMemOps = MemOps.size();
3339 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003340 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003341 unsigned VTSize = VT.getSizeInBits() / 8;
Dale Johannesen0f502f62009-02-03 22:26:09 +00003342 SDValue Value = getMemsetValue(Src, VT, DAG, dl);
3343 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003344 getMemBasePlusOffset(Dst, DstOff, DAG),
3345 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003346 OutChains.push_back(Store);
3347 DstOff += VTSize;
3348 }
3349
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003351 &OutChains[0], OutChains.size());
3352}
3353
Dale Johannesen0f502f62009-02-03 22:26:09 +00003354SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003355 SDValue Src, SDValue Size,
3356 unsigned Align, bool AlwaysInline,
3357 const Value *DstSV, uint64_t DstSVOff,
3358 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003359
3360 // Check to see if we should lower the memcpy to loads and stores first.
3361 // For cases within the target-specified limits, this is the best choice.
3362 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3363 if (ConstantSize) {
3364 // Memcpy with size zero? Just return the original chain.
3365 if (ConstantSize->isNullValue())
3366 return Chain;
3367
Dan Gohman475871a2008-07-27 21:46:04 +00003368 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003369 getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003370 ConstantSize->getZExtValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003371 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003372 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003373 return Result;
3374 }
3375
3376 // Then check to see if we should lower the memcpy with target-specific
3377 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003378 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003379 TLI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Dan Gohman707e0182008-04-12 04:36:06 +00003380 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003381 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003382 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003383 return Result;
3384
3385 // If we really need inline code and the target declined to provide it,
3386 // use a (potentially long) sequence of loads and stores.
3387 if (AlwaysInline) {
3388 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003389 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003390 ConstantSize->getZExtValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003391 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003392 }
3393
3394 // Emit a library call.
3395 TargetLowering::ArgListTy Args;
3396 TargetLowering::ArgListEntry Entry;
Owen Anderson1d0be152009-08-13 21:58:54 +00003397 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003398 Entry.Node = Dst; Args.push_back(Entry);
3399 Entry.Node = Src; Args.push_back(Entry);
3400 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003401 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003402 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003403 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003404 false, false, false, false, 0,
3405 TLI.getLibcallCallingConv(RTLIB::MEMCPY), false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003406 /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003407 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003408 TLI.getPointerTy()),
Dale Johannesen0f502f62009-02-03 22:26:09 +00003409 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003410 return CallResult.second;
3411}
3412
Dale Johannesen0f502f62009-02-03 22:26:09 +00003413SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003414 SDValue Src, SDValue Size,
3415 unsigned Align,
3416 const Value *DstSV, uint64_t DstSVOff,
3417 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003418
Dan Gohman21323f32008-05-29 19:42:22 +00003419 // Check to see if we should lower the memmove to loads and stores first.
3420 // For cases within the target-specified limits, this is the best choice.
3421 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3422 if (ConstantSize) {
3423 // Memmove with size zero? Just return the original chain.
3424 if (ConstantSize->isNullValue())
3425 return Chain;
3426
Dan Gohman475871a2008-07-27 21:46:04 +00003427 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003428 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003429 ConstantSize->getZExtValue(),
Dan Gohman21323f32008-05-29 19:42:22 +00003430 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003431 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003432 return Result;
3433 }
Dan Gohman707e0182008-04-12 04:36:06 +00003434
3435 // Then check to see if we should lower the memmove with target-specific
3436 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003437 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003438 TLI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003439 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003440 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003441 return Result;
3442
3443 // Emit a library call.
3444 TargetLowering::ArgListTy Args;
3445 TargetLowering::ArgListEntry Entry;
Owen Anderson1d0be152009-08-13 21:58:54 +00003446 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003447 Entry.Node = Dst; Args.push_back(Entry);
3448 Entry.Node = Src; Args.push_back(Entry);
3449 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003450 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003451 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003452 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003453 false, false, false, false, 0,
3454 TLI.getLibcallCallingConv(RTLIB::MEMMOVE), false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003455 /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003456 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003457 TLI.getPointerTy()),
Dale Johannesen0f502f62009-02-03 22:26:09 +00003458 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003459 return CallResult.second;
3460}
3461
Dale Johannesen0f502f62009-02-03 22:26:09 +00003462SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003463 SDValue Src, SDValue Size,
3464 unsigned Align,
3465 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003466
3467 // Check to see if we should lower the memset to stores first.
3468 // For cases within the target-specified limits, this is the best choice.
3469 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3470 if (ConstantSize) {
3471 // Memset with size zero? Just return the original chain.
3472 if (ConstantSize->isNullValue())
3473 return Chain;
3474
Dan Gohman475871a2008-07-27 21:46:04 +00003475 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003476 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003477 Align, DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003478 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003479 return Result;
3480 }
3481
3482 // Then check to see if we should lower the memset with target-specific
3483 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003484 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003485 TLI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align,
Bill Wendling6158d842008-10-01 00:59:58 +00003486 DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003487 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003488 return Result;
3489
3490 // Emit a library call.
Owen Anderson1d0be152009-08-13 21:58:54 +00003491 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003492 TargetLowering::ArgListTy Args;
3493 TargetLowering::ArgListEntry Entry;
3494 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3495 Args.push_back(Entry);
3496 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00003497 if (Src.getValueType().bitsGT(MVT::i32))
3498 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00003499 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003500 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00003501 Entry.Node = Src;
3502 Entry.Ty = Type::getInt32Ty(*getContext());
3503 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00003504 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00003505 Entry.Node = Size;
3506 Entry.Ty = IntPtrTy;
3507 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00003508 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003509 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003510 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003511 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003512 false, false, false, false, 0,
3513 TLI.getLibcallCallingConv(RTLIB::MEMSET), false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003514 /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003515 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003516 TLI.getPointerTy()),
Dale Johannesen0f502f62009-02-03 22:26:09 +00003517 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003518 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003519}
3520
Owen Andersone50ed302009-08-10 22:56:29 +00003521SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003522 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00003523 SDValue Ptr, SDValue Cmp,
Dale Johannesene8c17332009-01-29 00:47:48 +00003524 SDValue Swp, const Value* PtrVal,
3525 unsigned Alignment) {
3526 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
3527 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3528
Owen Andersone50ed302009-08-10 22:56:29 +00003529 EVT VT = Cmp.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003530
3531 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Owen Andersone50ed302009-08-10 22:56:29 +00003532 Alignment = getEVTAlignment(MemVT);
Dale Johannesene8c17332009-01-29 00:47:48 +00003533
Owen Anderson825b72b2009-08-11 20:47:22 +00003534 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003535 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003536 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003537 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
3538 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
3539 void* IP = 0;
3540 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3541 return SDValue(E, 0);
3542 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
3543 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT,
3544 Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
3545 CSEMap.InsertNode(N, IP);
3546 AllNodes.push_back(N);
3547 return SDValue(N, 0);
3548}
3549
Owen Andersone50ed302009-08-10 22:56:29 +00003550SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003551 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00003552 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00003553 const Value* PtrVal,
3554 unsigned Alignment) {
3555 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3556 Opcode == ISD::ATOMIC_LOAD_SUB ||
3557 Opcode == ISD::ATOMIC_LOAD_AND ||
3558 Opcode == ISD::ATOMIC_LOAD_OR ||
3559 Opcode == ISD::ATOMIC_LOAD_XOR ||
3560 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00003561 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00003562 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00003563 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00003564 Opcode == ISD::ATOMIC_LOAD_UMAX ||
3565 Opcode == ISD::ATOMIC_SWAP) &&
3566 "Invalid Atomic Op");
3567
Owen Andersone50ed302009-08-10 22:56:29 +00003568 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003569
3570 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Owen Andersone50ed302009-08-10 22:56:29 +00003571 Alignment = getEVTAlignment(MemVT);
Dale Johannesene8c17332009-01-29 00:47:48 +00003572
Owen Anderson825b72b2009-08-11 20:47:22 +00003573 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003574 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003575 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003576 SDValue Ops[] = {Chain, Ptr, Val};
3577 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
3578 void* IP = 0;
3579 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3580 return SDValue(E, 0);
3581 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
3582 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT,
3583 Chain, Ptr, Val, PtrVal, Alignment);
3584 CSEMap.InsertNode(N, IP);
3585 AllNodes.push_back(N);
3586 return SDValue(N, 0);
3587}
3588
Duncan Sands4bdcb612008-07-02 17:40:58 +00003589/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3590/// Allowed to return something different (and simpler) if Simplify is true.
Dale Johannesen54c94522009-02-02 20:47:48 +00003591SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3592 DebugLoc dl) {
3593 if (NumOps == 1)
3594 return Ops[0];
3595
Owen Andersone50ed302009-08-10 22:56:29 +00003596 SmallVector<EVT, 4> VTs;
Dale Johannesen54c94522009-02-02 20:47:48 +00003597 VTs.reserve(NumOps);
3598 for (unsigned i = 0; i < NumOps; ++i)
3599 VTs.push_back(Ops[i].getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00003600 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesen54c94522009-02-02 20:47:48 +00003601 Ops, NumOps);
3602}
3603
Dan Gohman475871a2008-07-27 21:46:04 +00003604SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003605SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00003606 const EVT *VTs, unsigned NumVTs,
Dale Johannesene8c17332009-01-29 00:47:48 +00003607 const SDValue *Ops, unsigned NumOps,
Owen Andersone50ed302009-08-10 22:56:29 +00003608 EVT MemVT, const Value *srcValue, int SVOff,
Dale Johannesene8c17332009-01-29 00:47:48 +00003609 unsigned Align, bool Vol,
3610 bool ReadMem, bool WriteMem) {
3611 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
3612 MemVT, srcValue, SVOff, Align, Vol,
3613 ReadMem, WriteMem);
3614}
3615
3616SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003617SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
3618 const SDValue *Ops, unsigned NumOps,
Owen Andersone50ed302009-08-10 22:56:29 +00003619 EVT MemVT, const Value *srcValue, int SVOff,
Dale Johannesene8c17332009-01-29 00:47:48 +00003620 unsigned Align, bool Vol,
3621 bool ReadMem, bool WriteMem) {
3622 // Memoize the node unless it returns a flag.
3623 MemIntrinsicSDNode *N;
Owen Anderson825b72b2009-08-11 20:47:22 +00003624 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003625 FoldingSetNodeID ID;
3626 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3627 void *IP = 0;
3628 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3629 return SDValue(E, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003630
Dale Johannesene8c17332009-01-29 00:47:48 +00003631 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3632 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT,
3633 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3634 CSEMap.InsertNode(N, IP);
3635 } else {
3636 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3637 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT,
3638 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3639 }
3640 AllNodes.push_back(N);
3641 return SDValue(N, 0);
3642}
3643
3644SDValue
Scott Michelfdc40a02009-02-17 22:15:04 +00003645SelectionDAG::getLoad(ISD::MemIndexedMode AM, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00003646 ISD::LoadExtType ExtType, EVT VT, SDValue Chain,
Dale Johannesene8c17332009-01-29 00:47:48 +00003647 SDValue Ptr, SDValue Offset,
Owen Andersone50ed302009-08-10 22:56:29 +00003648 const Value *SV, int SVOffset, EVT EVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003649 bool isVolatile, unsigned Alignment) {
3650 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Owen Andersone50ed302009-08-10 22:56:29 +00003651 Alignment = getEVTAlignment(VT);
Dale Johannesene8c17332009-01-29 00:47:48 +00003652
3653 if (VT == EVT) {
3654 ExtType = ISD::NON_EXTLOAD;
3655 } else if (ExtType == ISD::NON_EXTLOAD) {
3656 assert(VT == EVT && "Non-extending load from different memory type!");
3657 } else {
3658 // Extending load.
3659 if (VT.isVector())
3660 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3661 "Invalid vector extload!");
3662 else
3663 assert(EVT.bitsLT(VT) &&
3664 "Should only be an extending load, not truncating!");
3665 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
3666 "Cannot sign/zero extend a FP/Vector load!");
3667 assert(VT.isInteger() == EVT.isInteger() &&
3668 "Cannot convert from FP to Int or Int -> FP!");
3669 }
3670
3671 bool Indexed = AM != ISD::UNINDEXED;
3672 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
3673 "Unindexed load with an offset!");
3674
3675 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00003676 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003677 SDValue Ops[] = { Chain, Ptr, Offset };
3678 FoldingSetNodeID ID;
3679 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dale Johannesene8c17332009-01-29 00:47:48 +00003680 ID.AddInteger(EVT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003681 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, isVolatile, Alignment));
Dale Johannesene8c17332009-01-29 00:47:48 +00003682 void *IP = 0;
3683 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3684 return SDValue(E, 0);
3685 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
3686 new (N) LoadSDNode(Ops, dl, VTs, AM, ExtType, EVT, SV, SVOffset,
3687 Alignment, isVolatile);
3688 CSEMap.InsertNode(N, IP);
3689 AllNodes.push_back(N);
3690 return SDValue(N, 0);
3691}
3692
Owen Andersone50ed302009-08-10 22:56:29 +00003693SDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00003694 SDValue Chain, SDValue Ptr,
3695 const Value *SV, int SVOffset,
3696 bool isVolatile, unsigned Alignment) {
Dale Johannesene8d72302009-02-06 23:05:02 +00003697 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00003698 return getLoad(ISD::UNINDEXED, dl, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3699 SV, SVOffset, VT, isVolatile, Alignment);
3700}
3701
Owen Andersone50ed302009-08-10 22:56:29 +00003702SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003703 SDValue Chain, SDValue Ptr,
3704 const Value *SV,
Owen Andersone50ed302009-08-10 22:56:29 +00003705 int SVOffset, EVT EVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003706 bool isVolatile, unsigned Alignment) {
Dale Johannesene8d72302009-02-06 23:05:02 +00003707 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00003708 return getLoad(ISD::UNINDEXED, dl, ExtType, VT, Chain, Ptr, Undef,
3709 SV, SVOffset, EVT, isVolatile, Alignment);
3710}
3711
Dan Gohman475871a2008-07-27 21:46:04 +00003712SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003713SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
3714 SDValue Offset, ISD::MemIndexedMode AM) {
3715 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
3716 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3717 "Load is already a indexed load!");
3718 return getLoad(AM, dl, LD->getExtensionType(), OrigLoad.getValueType(),
3719 LD->getChain(), Base, Offset, LD->getSrcValue(),
3720 LD->getSrcValueOffset(), LD->getMemoryVT(),
3721 LD->isVolatile(), LD->getAlignment());
3722}
3723
Dale Johannesene8c17332009-01-29 00:47:48 +00003724SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
3725 SDValue Ptr, const Value *SV, int SVOffset,
3726 bool isVolatile, unsigned Alignment) {
Owen Andersone50ed302009-08-10 22:56:29 +00003727 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003728
3729 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Owen Andersone50ed302009-08-10 22:56:29 +00003730 Alignment = getEVTAlignment(VT);
Dale Johannesene8c17332009-01-29 00:47:48 +00003731
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00003733 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00003734 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3735 FoldingSetNodeID ID;
3736 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00003737 ID.AddInteger(VT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003738 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED,
3739 isVolatile, Alignment));
Dale Johannesene8c17332009-01-29 00:47:48 +00003740 void *IP = 0;
3741 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3742 return SDValue(E, 0);
3743 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3744 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, false,
3745 VT, SV, SVOffset, Alignment, isVolatile);
3746 CSEMap.InsertNode(N, IP);
3747 AllNodes.push_back(N);
3748 return SDValue(N, 0);
3749}
3750
Dale Johannesene8c17332009-01-29 00:47:48 +00003751SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
3752 SDValue Ptr, const Value *SV,
Owen Andersone50ed302009-08-10 22:56:29 +00003753 int SVOffset, EVT SVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003754 bool isVolatile, unsigned Alignment) {
Owen Andersone50ed302009-08-10 22:56:29 +00003755 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003756
3757 if (VT == SVT)
3758 return getStore(Chain, dl, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
3759
3760 assert(VT.bitsGT(SVT) && "Not a truncation?");
3761 assert(VT.isInteger() == SVT.isInteger() &&
3762 "Can't do FP-INT conversion!");
3763
3764 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Owen Andersone50ed302009-08-10 22:56:29 +00003765 Alignment = getEVTAlignment(VT);
Dale Johannesene8c17332009-01-29 00:47:48 +00003766
Owen Anderson825b72b2009-08-11 20:47:22 +00003767 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00003768 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00003769 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3770 FoldingSetNodeID ID;
3771 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00003772 ID.AddInteger(SVT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003773 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED,
3774 isVolatile, Alignment));
Dale Johannesene8c17332009-01-29 00:47:48 +00003775 void *IP = 0;
3776 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3777 return SDValue(E, 0);
3778 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3779 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, true,
3780 SVT, SV, SVOffset, Alignment, isVolatile);
3781 CSEMap.InsertNode(N, IP);
3782 AllNodes.push_back(N);
3783 return SDValue(N, 0);
3784}
3785
Dan Gohman475871a2008-07-27 21:46:04 +00003786SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003787SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
3788 SDValue Offset, ISD::MemIndexedMode AM) {
3789 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3790 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3791 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003792 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003793 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3794 FoldingSetNodeID ID;
3795 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00003796 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003797 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesene8c17332009-01-29 00:47:48 +00003798 void *IP = 0;
3799 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3800 return SDValue(E, 0);
3801 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3802 new (N) StoreSDNode(Ops, dl, VTs, AM,
3803 ST->isTruncatingStore(), ST->getMemoryVT(),
3804 ST->getSrcValue(), ST->getSrcValueOffset(),
3805 ST->getAlignment(), ST->isVolatile());
3806 CSEMap.InsertNode(N, IP);
3807 AllNodes.push_back(N);
3808 return SDValue(N, 0);
3809}
3810
Owen Andersone50ed302009-08-10 22:56:29 +00003811SDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00003812 SDValue Chain, SDValue Ptr,
3813 SDValue SV) {
3814 SDValue Ops[] = { Chain, Ptr, SV };
Owen Anderson825b72b2009-08-11 20:47:22 +00003815 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 3);
Dale Johannesen0f502f62009-02-03 22:26:09 +00003816}
3817
Owen Andersone50ed302009-08-10 22:56:29 +00003818SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003819 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003820 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003821 case 0: return getNode(Opcode, DL, VT);
3822 case 1: return getNode(Opcode, DL, VT, Ops[0]);
3823 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
3824 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003825 default: break;
3826 }
3827
Dan Gohman475871a2008-07-27 21:46:04 +00003828 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003829 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003830 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00003831 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003832}
3833
Owen Andersone50ed302009-08-10 22:56:29 +00003834SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003835 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003836 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003837 case 0: return getNode(Opcode, DL, VT);
3838 case 1: return getNode(Opcode, DL, VT, Ops[0]);
3839 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
3840 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003841 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003842 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003843
Chris Lattneref847df2005-04-09 03:27:28 +00003844 switch (Opcode) {
3845 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003846 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003847 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003848 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3849 "LHS and RHS of condition must have same type!");
3850 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3851 "True and False arms of SelectCC must have same type!");
3852 assert(Ops[2].getValueType() == VT &&
3853 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003854 break;
3855 }
3856 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003857 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003858 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3859 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003860 break;
3861 }
Chris Lattneref847df2005-04-09 03:27:28 +00003862 }
3863
Chris Lattner385328c2005-05-14 07:42:29 +00003864 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003865 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003866 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00003867
Owen Anderson825b72b2009-08-11 20:47:22 +00003868 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003869 FoldingSetNodeID ID;
3870 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003871 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00003872
Chris Lattnera5682852006-08-07 23:03:03 +00003873 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003874 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00003875
Dan Gohmanfed90b62008-07-28 21:51:04 +00003876 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003877 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003878 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003879 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003880 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003881 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003882 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00003883
Chris Lattneref847df2005-04-09 03:27:28 +00003884 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003885#ifndef NDEBUG
3886 VerifyNode(N);
3887#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003888 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003889}
3890
Bill Wendling7ade28c2009-01-28 22:17:52 +00003891SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00003892 const std::vector<EVT> &ResultTys,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003893 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00003894 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003895 Ops, NumOps);
3896}
3897
Bill Wendling7ade28c2009-01-28 22:17:52 +00003898SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00003899 const EVT *VTs, unsigned NumVTs,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003900 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003901 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00003902 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
3903 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelfdc40a02009-02-17 22:15:04 +00003904}
3905
Bill Wendling7ade28c2009-01-28 22:17:52 +00003906SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3907 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003908 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00003909 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003910
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00003911#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00003912 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003913 // FIXME: figure out how to safely handle things like
3914 // int foo(int x) { return 1 << (x & 255); }
3915 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00003916 case ISD::SRA_PARTS:
3917 case ISD::SRL_PARTS:
3918 case ISD::SHL_PARTS:
3919 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00003920 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00003921 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00003922 else if (N3.getOpcode() == ISD::AND)
3923 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3924 // If the and is only masking out bits that cannot effect the shift,
3925 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003926 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003927 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00003928 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00003929 }
3930 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00003931 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00003932#endif
Chris Lattner89c34632005-05-14 06:20:26 +00003933
Chris Lattner43247a12005-08-25 19:12:10 +00003934 // Memoize the node unless it returns a flag.
3935 SDNode *N;
Owen Anderson825b72b2009-08-11 20:47:22 +00003936 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003937 FoldingSetNodeID ID;
3938 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003939 void *IP = 0;
3940 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003941 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003942 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003943 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003944 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003945 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003946 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003947 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003948 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003949 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003950 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003951 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003952 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003953 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003954 }
Chris Lattnera5682852006-08-07 23:03:03 +00003955 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003956 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003957 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003958 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003959 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003960 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003961 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003962 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003963 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003964 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003965 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003966 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003967 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003968 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003969 }
Chris Lattner43247a12005-08-25 19:12:10 +00003970 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003971 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003972#ifndef NDEBUG
3973 VerifyNode(N);
3974#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003975 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003976}
3977
Bill Wendling7ade28c2009-01-28 22:17:52 +00003978SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
3979 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003980}
3981
Bill Wendling7ade28c2009-01-28 22:17:52 +00003982SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3983 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00003984 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003985 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00003986}
3987
Bill Wendling7ade28c2009-01-28 22:17:52 +00003988SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3989 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00003990 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003991 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00003992}
3993
Bill Wendling7ade28c2009-01-28 22:17:52 +00003994SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3995 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00003996 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003997 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00003998}
3999
Bill Wendling7ade28c2009-01-28 22:17:52 +00004000SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4001 SDValue N1, SDValue N2, SDValue N3,
4002 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004003 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004004 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004005}
4006
Bill Wendling7ade28c2009-01-28 22:17:52 +00004007SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4008 SDValue N1, SDValue N2, SDValue N3,
4009 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004010 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004011 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004012}
4013
Owen Andersone50ed302009-08-10 22:56:29 +00004014SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004015 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004016}
4017
Owen Andersone50ed302009-08-10 22:56:29 +00004018SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004019 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4020 E = VTList.rend(); I != E; ++I)
4021 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4022 return *I;
4023
Owen Andersone50ed302009-08-10 22:56:29 +00004024 EVT *Array = Allocator.Allocate<EVT>(2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004025 Array[0] = VT1;
4026 Array[1] = VT2;
4027 SDVTList Result = makeVTList(Array, 2);
4028 VTList.push_back(Result);
4029 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00004030}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004031
Owen Andersone50ed302009-08-10 22:56:29 +00004032SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004033 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4034 E = VTList.rend(); I != E; ++I)
4035 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4036 I->VTs[2] == VT3)
4037 return *I;
4038
Owen Andersone50ed302009-08-10 22:56:29 +00004039 EVT *Array = Allocator.Allocate<EVT>(3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004040 Array[0] = VT1;
4041 Array[1] = VT2;
4042 Array[2] = VT3;
4043 SDVTList Result = makeVTList(Array, 3);
4044 VTList.push_back(Result);
4045 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00004046}
4047
Owen Andersone50ed302009-08-10 22:56:29 +00004048SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Bill Wendling13d6d442008-12-01 23:28:22 +00004049 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4050 E = VTList.rend(); I != E; ++I)
4051 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4052 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4053 return *I;
4054
Owen Andersone50ed302009-08-10 22:56:29 +00004055 EVT *Array = Allocator.Allocate<EVT>(3);
Bill Wendling13d6d442008-12-01 23:28:22 +00004056 Array[0] = VT1;
4057 Array[1] = VT2;
4058 Array[2] = VT3;
4059 Array[3] = VT4;
4060 SDVTList Result = makeVTList(Array, 4);
4061 VTList.push_back(Result);
4062 return Result;
4063}
4064
Owen Andersone50ed302009-08-10 22:56:29 +00004065SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00004066 switch (NumVTs) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004067 case 0: llvm_unreachable("Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00004068 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00004069 case 2: return getVTList(VTs[0], VTs[1]);
4070 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
4071 default: break;
4072 }
4073
Dan Gohmane8be6c62008-07-17 19:10:17 +00004074 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4075 E = VTList.rend(); I != E; ++I) {
4076 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4077 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00004078
Chris Lattner70046e92006-08-15 17:46:01 +00004079 bool NoMatch = false;
4080 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004081 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00004082 NoMatch = true;
4083 break;
4084 }
4085 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004086 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00004087 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004088
Owen Andersone50ed302009-08-10 22:56:29 +00004089 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004090 std::copy(VTs, VTs+NumVTs, Array);
4091 SDVTList Result = makeVTList(Array, NumVTs);
4092 VTList.push_back(Result);
4093 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004094}
4095
4096
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004097/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4098/// specified operands. If the resultant node already exists in the DAG,
4099/// this does not modify the specified node, instead it returns the node that
4100/// already exists. If the resultant node does not exist in the DAG, the
4101/// input node is returned. As a degenerate case, if you specify the same
4102/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00004103SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004104 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004105 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004106
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004107 // Check to see if there is no change.
4108 if (Op == N->getOperand(0)) return InN;
Scott Michelfdc40a02009-02-17 22:15:04 +00004109
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004110 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004111 void *InsertPos = 0;
4112 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004113 return SDValue(Existing, InN.getResNo());
Scott Michelfdc40a02009-02-17 22:15:04 +00004114
Dan Gohman79acd2b2008-07-21 22:38:59 +00004115 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004116 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004117 if (!RemoveNodeFromCSEMaps(N))
4118 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004119
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004120 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004121 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004122
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004123 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004124 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004125 return InN;
4126}
4127
Dan Gohman475871a2008-07-27 21:46:04 +00004128SDValue SelectionDAG::
4129UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004130 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004131 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004132
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004133 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004134 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
4135 return InN; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00004136
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004137 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004138 void *InsertPos = 0;
4139 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004140 return SDValue(Existing, InN.getResNo());
Scott Michelfdc40a02009-02-17 22:15:04 +00004141
Dan Gohman79acd2b2008-07-21 22:38:59 +00004142 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004143 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004144 if (!RemoveNodeFromCSEMaps(N))
4145 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004146
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004147 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004148 if (N->OperandList[0] != Op1)
4149 N->OperandList[0].set(Op1);
4150 if (N->OperandList[1] != Op2)
4151 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004152
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004153 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004154 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004155 return InN;
4156}
4157
Dan Gohman475871a2008-07-27 21:46:04 +00004158SDValue SelectionDAG::
4159UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
4160 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004161 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004162}
4163
Dan Gohman475871a2008-07-27 21:46:04 +00004164SDValue SelectionDAG::
Scott Michelfdc40a02009-02-17 22:15:04 +00004165UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004166 SDValue Op3, SDValue Op4) {
4167 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004168 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004169}
4170
Dan Gohman475871a2008-07-27 21:46:04 +00004171SDValue SelectionDAG::
4172UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
4173 SDValue Op3, SDValue Op4, SDValue Op5) {
4174 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004175 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00004176}
4177
Dan Gohman475871a2008-07-27 21:46:04 +00004178SDValue SelectionDAG::
4179UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004180 SDNode *N = InN.getNode();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004181 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004182 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004183
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004184 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004185 bool AnyChange = false;
4186 for (unsigned i = 0; i != NumOps; ++i) {
4187 if (Ops[i] != N->getOperand(i)) {
4188 AnyChange = true;
4189 break;
4190 }
4191 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004192
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004193 // No operands changed, just return the input node.
4194 if (!AnyChange) return InN;
Scott Michelfdc40a02009-02-17 22:15:04 +00004195
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004196 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004197 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004198 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004199 return SDValue(Existing, InN.getResNo());
Scott Michelfdc40a02009-02-17 22:15:04 +00004200
Dan Gohman7ceda162008-05-02 00:05:03 +00004201 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004202 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004203 if (!RemoveNodeFromCSEMaps(N))
4204 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004205
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004206 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004207 for (unsigned i = 0; i != NumOps; ++i)
4208 if (N->OperandList[i] != Ops[i])
4209 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004210
4211 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004212 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004213 return InN;
4214}
4215
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004216/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00004217/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004218void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004219 // Unlike the code in MorphNodeTo that does this, we don't need to
4220 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00004221 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4222 SDUse &Use = *I++;
4223 Use.set(SDValue());
4224 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00004225}
Chris Lattner149c58c2005-08-16 18:17:10 +00004226
Dan Gohmane8be6c62008-07-17 19:10:17 +00004227/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4228/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004229///
Dan Gohmane8be6c62008-07-17 19:10:17 +00004230SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004231 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004232 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004233 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00004234}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004235
Dan Gohmane8be6c62008-07-17 19:10:17 +00004236SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004237 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004238 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004239 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004240 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004241}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004242
Dan Gohmane8be6c62008-07-17 19:10:17 +00004243SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004244 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004245 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004246 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004247 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004248 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004249}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004250
Dan Gohmane8be6c62008-07-17 19:10:17 +00004251SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004252 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004253 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004254 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004255 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004256 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004257}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004258
Dan Gohmane8be6c62008-07-17 19:10:17 +00004259SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004260 EVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004261 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004262 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004263 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004264}
4265
Dan Gohmane8be6c62008-07-17 19:10:17 +00004266SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004267 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004268 unsigned NumOps) {
4269 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004270 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004271}
4272
Dan Gohmane8be6c62008-07-17 19:10:17 +00004273SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004274 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004275 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004276 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00004277}
4278
Dan Gohmane8be6c62008-07-17 19:10:17 +00004279SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004280 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004281 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004282 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004283 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004284}
4285
Bill Wendling13d6d442008-12-01 23:28:22 +00004286SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004287 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling13d6d442008-12-01 23:28:22 +00004288 const SDValue *Ops, unsigned NumOps) {
4289 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4290 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4291}
4292
Scott Michelfdc40a02009-02-17 22:15:04 +00004293SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004294 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004295 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004296 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004297 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004298 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00004299}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00004300
Scott Michelfdc40a02009-02-17 22:15:04 +00004301SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004302 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004303 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004304 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004305 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004306 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00004307}
4308
Dan Gohmane8be6c62008-07-17 19:10:17 +00004309SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004310 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00004311 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004312 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004313 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004314 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004315 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00004316}
4317
Dan Gohmane8be6c62008-07-17 19:10:17 +00004318SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004319 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00004320 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00004321 SDValue Op3) {
4322 SDVTList VTs = getVTList(VT1, VT2, VT3);
4323 SDValue Ops[] = { Op1, Op2, Op3 };
4324 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4325}
4326
4327SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004328 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004329 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004330 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4331}
4332
4333SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004334 EVT VT) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004335 SDVTList VTs = getVTList(VT);
4336 return MorphNodeTo(N, Opc, VTs, 0, 0);
4337}
4338
4339SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004340 EVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004341 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004342 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004343 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4344}
4345
4346SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004347 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004348 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004349 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004350 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004351 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4352}
4353
4354SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004355 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004356 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004357 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004358 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004359 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4360}
4361
4362SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004363 EVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004364 unsigned NumOps) {
4365 SDVTList VTs = getVTList(VT);
4366 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4367}
4368
4369SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004370 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004371 unsigned NumOps) {
4372 SDVTList VTs = getVTList(VT1, VT2);
4373 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4374}
4375
4376SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004377 EVT VT1, EVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004378 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004379 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004380}
4381
4382SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004383 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004384 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004385 SDVTList VTs = getVTList(VT1, VT2, VT3);
4386 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4387}
4388
Scott Michelfdc40a02009-02-17 22:15:04 +00004389SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004390 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004391 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004392 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004393 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004394 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4395}
4396
Scott Michelfdc40a02009-02-17 22:15:04 +00004397SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004398 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004399 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004400 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004401 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004402 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4403}
4404
4405SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004406 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00004407 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004408 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004409 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004410 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004411 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4412}
4413
4414/// MorphNodeTo - These *mutate* the specified node to have the specified
4415/// return type, opcode, and operands.
4416///
4417/// Note that MorphNodeTo returns the resultant node. If there is already a
4418/// node of the specified opcode and operands, it returns that node instead of
Dale Johannesena05dca42009-02-04 23:02:30 +00004419/// the current one. Note that the DebugLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00004420///
4421/// Using MorphNodeTo is faster than creating a new node and swapping it in
4422/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004423/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004424/// the node's users.
4425///
4426SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004427 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004428 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004429 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004430 void *IP = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00004431 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004432 FoldingSetNodeID ID;
4433 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4434 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4435 return ON;
4436 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004437
Dan Gohman095cc292008-09-13 01:54:27 +00004438 if (!RemoveNodeFromCSEMaps(N))
4439 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00004440
Dan Gohmane8be6c62008-07-17 19:10:17 +00004441 // Start the morphing.
4442 N->NodeType = Opc;
4443 N->ValueList = VTs.VTs;
4444 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00004445
Dan Gohmane8be6c62008-07-17 19:10:17 +00004446 // Clear the operands list, updating used nodes to remove this from their
4447 // use list. Keep track of any operands that become dead as a result.
4448 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00004449 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
4450 SDUse &Use = *I++;
4451 SDNode *Used = Use.getNode();
4452 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004453 if (Used->use_empty())
4454 DeadNodeSet.insert(Used);
4455 }
4456
4457 // If NumOps is larger than the # of operands we currently have, reallocate
4458 // the operand list.
4459 if (NumOps > N->NumOperands) {
4460 if (N->OperandsNeedDelete)
4461 delete[] N->OperandList;
Bill Wendlinga1dc6022008-10-19 20:51:12 +00004462
Dan Gohmane8be6c62008-07-17 19:10:17 +00004463 if (N->isMachineOpcode()) {
4464 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004465 // remainder of the current SelectionDAG iteration, so we can allocate
4466 // the operands directly out of a pool with no recycling metadata.
4467 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004468 N->OperandsNeedDelete = false;
4469 } else {
4470 N->OperandList = new SDUse[NumOps];
4471 N->OperandsNeedDelete = true;
4472 }
4473 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004474
Dan Gohmane8be6c62008-07-17 19:10:17 +00004475 // Assign the new operands.
4476 N->NumOperands = NumOps;
4477 for (unsigned i = 0, e = NumOps; i != e; ++i) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004478 N->OperandList[i].setUser(N);
Dan Gohmane7852d02009-01-26 04:35:06 +00004479 N->OperandList[i].setInitial(Ops[i]);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004480 }
4481
4482 // Delete any nodes that are still dead after adding the uses for the
4483 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004484 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004485 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4486 E = DeadNodeSet.end(); I != E; ++I)
4487 if ((*I)->use_empty())
4488 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004489 RemoveDeadNodes(DeadNodes);
4490
Dan Gohmane8be6c62008-07-17 19:10:17 +00004491 if (IP)
4492 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004493 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004494}
4495
Chris Lattner0fb094f2005-11-19 01:44:53 +00004496
Evan Cheng6ae46c42006-02-09 07:15:23 +00004497/// getTargetNode - These are used for target selectors to create a new node
4498/// with specified return type(s), target opcode, and operands.
4499///
4500/// Note that getTargetNode returns the resultant node. If there is already a
4501/// node of the specified opcode and operands, it returns that node instead of
4502/// the current one.
Owen Andersone50ed302009-08-10 22:56:29 +00004503SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, EVT VT) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004504 return getNode(~Opcode, dl, VT).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004505}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004506
Owen Andersone50ed302009-08-10 22:56:29 +00004507SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, EVT VT,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004508 SDValue Op1) {
4509 return getNode(~Opcode, dl, VT, Op1).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004510}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004511
Owen Andersone50ed302009-08-10 22:56:29 +00004512SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, EVT VT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004513 SDValue Op1, SDValue Op2) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004514 return getNode(~Opcode, dl, VT, Op1, Op2).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004515}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004516
Owen Andersone50ed302009-08-10 22:56:29 +00004517SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, EVT VT,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004518 SDValue Op1, SDValue Op2,
4519 SDValue Op3) {
4520 return getNode(~Opcode, dl, VT, Op1, Op2, Op3).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004521}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004522
Owen Andersone50ed302009-08-10 22:56:29 +00004523SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, EVT VT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004524 const SDValue *Ops, unsigned NumOps) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004525 return getNode(~Opcode, dl, VT, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004526}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004527
Scott Michelfdc40a02009-02-17 22:15:04 +00004528SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004529 EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004530 SDVTList VTs = getVTList(VT1, VT2);
Dale Johannesene8c17332009-01-29 00:47:48 +00004531 SDValue Op;
Dan Gohmanfc166572009-04-09 23:54:40 +00004532 return getNode(~Opcode, dl, VTs, &Op, 0).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004533}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004534
Owen Andersone50ed302009-08-10 22:56:29 +00004535SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, EVT VT1,
4536 EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004537 SDVTList VTs = getVTList(VT1, VT2);
4538 return getNode(~Opcode, dl, VTs, &Op1, 1).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004539}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004540
Owen Andersone50ed302009-08-10 22:56:29 +00004541SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, EVT VT1,
4542 EVT VT2, SDValue Op1,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004543 SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004544 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004545 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanfc166572009-04-09 23:54:40 +00004546 return getNode(~Opcode, dl, VTs, Ops, 2).getNode();
Bill Wendling56ab1a22009-01-29 09:01:55 +00004547}
4548
Owen Andersone50ed302009-08-10 22:56:29 +00004549SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, EVT VT1,
4550 EVT VT2, SDValue Op1,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004551 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004552 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004553 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanfc166572009-04-09 23:54:40 +00004554 return getNode(~Opcode, dl, VTs, Ops, 3).getNode();
Bill Wendling56ab1a22009-01-29 09:01:55 +00004555}
4556
Scott Michelfdc40a02009-02-17 22:15:04 +00004557SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004558 EVT VT1, EVT VT2,
Dale Johannesene8c17332009-01-29 00:47:48 +00004559 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004560 SDVTList VTs = getVTList(VT1, VT2);
4561 return getNode(~Opcode, dl, VTs, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004562}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004563
Bill Wendling56ab1a22009-01-29 09:01:55 +00004564SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004565 EVT VT1, EVT VT2, EVT VT3,
Dale Johannesene8c17332009-01-29 00:47:48 +00004566 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004567 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00004568 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanfc166572009-04-09 23:54:40 +00004569 return getNode(~Opcode, dl, VTs, Ops, 2).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004570}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004571
Bill Wendling56ab1a22009-01-29 09:01:55 +00004572SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004573 EVT VT1, EVT VT2, EVT VT3,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004574 SDValue Op1, SDValue Op2,
4575 SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004576 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004577 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanfc166572009-04-09 23:54:40 +00004578 return getNode(~Opcode, dl, VTs, Ops, 3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004579}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004580
Bill Wendling56ab1a22009-01-29 09:01:55 +00004581SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004582 EVT VT1, EVT VT2, EVT VT3,
Dale Johannesene8c17332009-01-29 00:47:48 +00004583 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004584 SDVTList VTs = getVTList(VT1, VT2, VT3);
4585 return getNode(~Opcode, dl, VTs, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004586}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004587
Owen Andersone50ed302009-08-10 22:56:29 +00004588SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, EVT VT1,
4589 EVT VT2, EVT VT3, EVT VT4,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004590 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004591 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4592 return getNode(~Opcode, dl, VTs, Ops, NumOps).getNode();
Bill Wendling56ab1a22009-01-29 09:01:55 +00004593}
4594
Bill Wendling56ab1a22009-01-29 09:01:55 +00004595SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004596 const std::vector<EVT> &ResultTys,
Dale Johannesene8c17332009-01-29 00:47:48 +00004597 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004598 return getNode(~Opcode, dl, ResultTys, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004599}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004600
Dan Gohman6a402dc2009-08-19 18:16:17 +00004601/// getTargetExtractSubreg - A convenience function for creating
4602/// TargetInstrInfo::EXTRACT_SUBREG nodes.
4603SDValue
4604SelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
4605 SDValue Operand) {
4606 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
4607 SDNode *Subreg = getTargetNode(TargetInstrInfo::EXTRACT_SUBREG, DL,
4608 VT, Operand, SRIdxVal);
4609 return SDValue(Subreg, 0);
4610}
4611
Evan Cheng08b11732008-03-22 01:55:50 +00004612/// getNodeIfExists - Get the specified node if it's already available, or
4613/// else return NULL.
4614SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004615 const SDValue *Ops, unsigned NumOps) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004616 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Evan Cheng08b11732008-03-22 01:55:50 +00004617 FoldingSetNodeID ID;
4618 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4619 void *IP = 0;
4620 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4621 return E;
4622 }
4623 return NULL;
4624}
4625
Evan Cheng99157a02006-08-07 22:13:29 +00004626/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004627/// This can cause recursive merging of nodes in the DAG.
4628///
Chris Lattner11d049c2008-02-03 03:35:22 +00004629/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004630///
Dan Gohman475871a2008-07-27 21:46:04 +00004631void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004632 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004633 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00004634 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004635 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00004636 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004637
Dan Gohman39946102009-01-25 16:29:12 +00004638 // Iterate over all the existing uses of From. New uses will be added
4639 // to the beginning of the use list, which we avoid visiting.
4640 // This specifically avoids visiting uses of From that arise while the
4641 // replacement is happening, because any such uses would be the result
4642 // of CSE: If an existing node looks like From after one of its operands
4643 // is replaced by To, we don't want to replace of all its users with To
4644 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00004645 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4646 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00004647 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004648
Chris Lattner8b8749f2005-08-17 19:00:20 +00004649 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00004650 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00004651
Dan Gohman39946102009-01-25 16:29:12 +00004652 // A user can appear in a use list multiple times, and when this
4653 // happens the uses are usually next to each other in the list.
4654 // To help reduce the number of CSE recomputations, process all
4655 // the uses of this user that we can find this way.
4656 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004657 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00004658 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004659 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00004660 } while (UI != UE && *UI == User);
4661
4662 // Now that we have modified User, add it back to the CSE maps. If it
4663 // already exists there, recursively merge the results together.
4664 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00004665 }
4666}
4667
4668/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4669/// This can cause recursive merging of nodes in the DAG.
4670///
Dan Gohmanc23e4962009-04-15 20:06:30 +00004671/// This version assumes that for each value of From, there is a
4672/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00004673///
Chris Lattner1e111c72005-09-07 05:37:01 +00004674void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004675 DAGUpdateListener *UpdateListener) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00004676#ifndef NDEBUG
4677 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
4678 assert((!From->hasAnyUseOfValue(i) ||
4679 From->getValueType(i) == To->getValueType(i)) &&
4680 "Cannot use this version of ReplaceAllUsesWith!");
4681#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00004682
4683 // Handle the trivial case.
4684 if (From == To)
4685 return;
4686
Dan Gohmandbe664a2009-01-19 21:44:21 +00004687 // Iterate over just the existing users of From. See the comments in
4688 // the ReplaceAllUsesWith above.
4689 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4690 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00004691 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004692
Chris Lattner8b52f212005-08-26 18:36:28 +00004693 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00004694 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004695
Dan Gohman39946102009-01-25 16:29:12 +00004696 // A user can appear in a use list multiple times, and when this
4697 // happens the uses are usually next to each other in the list.
4698 // To help reduce the number of CSE recomputations, process all
4699 // the uses of this user that we can find this way.
4700 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004701 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00004702 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004703 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00004704 } while (UI != UE && *UI == User);
4705
4706 // Now that we have modified User, add it back to the CSE maps. If it
4707 // already exists there, recursively merge the results together.
4708 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004709 }
4710}
4711
Chris Lattner8b52f212005-08-26 18:36:28 +00004712/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4713/// This can cause recursive merging of nodes in the DAG.
4714///
4715/// This version can replace From with any result values. To must match the
4716/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004717void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004718 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004719 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004720 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004721 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004722
Dan Gohmandbe664a2009-01-19 21:44:21 +00004723 // Iterate over just the existing users of From. See the comments in
4724 // the ReplaceAllUsesWith above.
4725 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4726 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00004727 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004728
Chris Lattner7b2880c2005-08-24 22:44:39 +00004729 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00004730 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004731
Dan Gohman39946102009-01-25 16:29:12 +00004732 // A user can appear in a use list multiple times, and when this
4733 // happens the uses are usually next to each other in the list.
4734 // To help reduce the number of CSE recomputations, process all
4735 // the uses of this user that we can find this way.
4736 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004737 SDUse &Use = UI.getUse();
4738 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00004739 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004740 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00004741 } while (UI != UE && *UI == User);
4742
4743 // Now that we have modified User, add it back to the CSE maps. If it
4744 // already exists there, recursively merge the results together.
4745 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004746 }
4747}
4748
Chris Lattner012f2412006-02-17 21:58:01 +00004749/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00004750/// uses of other values produced by From.getNode() alone. The Deleted
4751/// vector is handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004752void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004753 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004754 // Handle the really simple, really trivial case efficiently.
4755 if (From == To) return;
4756
Chris Lattner012f2412006-02-17 21:58:01 +00004757 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00004758 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004759 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004760 return;
4761 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004762
Dan Gohman39946102009-01-25 16:29:12 +00004763 // Iterate over just the existing users of From. See the comments in
4764 // the ReplaceAllUsesWith above.
4765 SDNode::use_iterator UI = From.getNode()->use_begin(),
4766 UE = From.getNode()->use_end();
4767 while (UI != UE) {
4768 SDNode *User = *UI;
4769 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00004770
Dan Gohman39946102009-01-25 16:29:12 +00004771 // A user can appear in a use list multiple times, and when this
4772 // happens the uses are usually next to each other in the list.
4773 // To help reduce the number of CSE recomputations, process all
4774 // the uses of this user that we can find this way.
4775 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004776 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00004777
4778 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00004779 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00004780 ++UI;
4781 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00004782 }
Dan Gohman39946102009-01-25 16:29:12 +00004783
4784 // If this node hasn't been modified yet, it's still in the CSE maps,
4785 // so remove its old self from the CSE maps.
4786 if (!UserRemovedFromCSEMaps) {
4787 RemoveNodeFromCSEMaps(User);
4788 UserRemovedFromCSEMaps = true;
4789 }
4790
4791 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004792 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00004793 } while (UI != UE && *UI == User);
4794
4795 // We are iterating over all uses of the From node, so if a use
4796 // doesn't use the specific value, no changes are made.
4797 if (!UserRemovedFromCSEMaps)
4798 continue;
4799
Chris Lattner01d029b2007-10-15 06:10:22 +00004800 // Now that we have modified User, add it back to the CSE maps. If it
4801 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00004802 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004803 }
4804}
4805
Dan Gohman39946102009-01-25 16:29:12 +00004806namespace {
4807 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
4808 /// to record information about a use.
4809 struct UseMemo {
4810 SDNode *User;
4811 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00004812 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00004813 };
Dan Gohmane7852d02009-01-26 04:35:06 +00004814
4815 /// operator< - Sort Memos by User.
4816 bool operator<(const UseMemo &L, const UseMemo &R) {
4817 return (intptr_t)L.User < (intptr_t)R.User;
4818 }
Dan Gohman39946102009-01-25 16:29:12 +00004819}
4820
Dan Gohmane8be6c62008-07-17 19:10:17 +00004821/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00004822/// uses of other values produced by From.getNode() alone. The same value
4823/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00004824/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004825void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4826 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004827 unsigned Num,
4828 DAGUpdateListener *UpdateListener){
4829 // Handle the simple, trivial case efficiently.
4830 if (Num == 1)
4831 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4832
Dan Gohman39946102009-01-25 16:29:12 +00004833 // Read up all the uses and make records of them. This helps
4834 // processing new uses that are introduced during the
4835 // replacement process.
4836 SmallVector<UseMemo, 4> Uses;
4837 for (unsigned i = 0; i != Num; ++i) {
4838 unsigned FromResNo = From[i].getResNo();
4839 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00004840 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00004841 E = FromNode->use_end(); UI != E; ++UI) {
4842 SDUse &Use = UI.getUse();
4843 if (Use.getResNo() == FromResNo) {
4844 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00004845 Uses.push_back(Memo);
4846 }
Dan Gohmane7852d02009-01-26 04:35:06 +00004847 }
Dan Gohman39946102009-01-25 16:29:12 +00004848 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004849
Dan Gohmane7852d02009-01-26 04:35:06 +00004850 // Sort the uses, so that all the uses from a given User are together.
4851 std::sort(Uses.begin(), Uses.end());
4852
Dan Gohman39946102009-01-25 16:29:12 +00004853 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
4854 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004855 // We know that this user uses some value of From. If it is the right
4856 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00004857 SDNode *User = Uses[UseIndex].User;
4858
4859 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00004860 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00004861
Dan Gohmane7852d02009-01-26 04:35:06 +00004862 // The Uses array is sorted, so all the uses for a given User
4863 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00004864 // To help reduce the number of CSE recomputations, process all
4865 // the uses of this user that we can find this way.
4866 do {
4867 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00004868 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00004869 ++UseIndex;
4870
Dan Gohmane7852d02009-01-26 04:35:06 +00004871 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00004872 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
4873
Dan Gohmane8be6c62008-07-17 19:10:17 +00004874 // Now that we have modified User, add it back to the CSE maps. If it
4875 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00004876 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004877 }
4878}
4879
Evan Chenge6f35d82006-08-01 08:20:41 +00004880/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004881/// based on their topological order. It returns the maximum id and a vector
4882/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00004883unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00004884
Dan Gohmanf06c8352008-09-30 18:30:35 +00004885 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004886
Dan Gohmanf06c8352008-09-30 18:30:35 +00004887 // SortedPos tracks the progress of the algorithm. Nodes before it are
4888 // sorted, nodes after it are unsorted. When the algorithm completes
4889 // it is at the end of the list.
4890 allnodes_iterator SortedPos = allnodes_begin();
4891
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00004892 // Visit all the nodes. Move nodes with no operands to the front of
4893 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00004894 // operand count. Before we do this, the Node Id fields of the nodes
4895 // may contain arbitrary values. After, the Node Id fields for nodes
4896 // before SortedPos will contain the topological sort index, and the
4897 // Node Id fields for nodes At SortedPos and after will contain the
4898 // count of outstanding operands.
4899 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
4900 SDNode *N = I++;
4901 unsigned Degree = N->getNumOperands();
4902 if (Degree == 0) {
4903 // A node with no uses, add it to the result array immediately.
4904 N->setNodeId(DAGSize++);
4905 allnodes_iterator Q = N;
4906 if (Q != SortedPos)
4907 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
4908 ++SortedPos;
4909 } else {
4910 // Temporarily use the Node Id as scratch space for the degree count.
4911 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004912 }
4913 }
4914
Dan Gohmanf06c8352008-09-30 18:30:35 +00004915 // Visit all the nodes. As we iterate, moves nodes into sorted order,
4916 // such that by the time the end is reached all nodes will be sorted.
4917 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
4918 SDNode *N = I;
4919 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4920 UI != UE; ++UI) {
4921 SDNode *P = *UI;
4922 unsigned Degree = P->getNodeId();
4923 --Degree;
4924 if (Degree == 0) {
4925 // All of P's operands are sorted, so P may sorted now.
4926 P->setNodeId(DAGSize++);
4927 if (P != SortedPos)
4928 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
4929 ++SortedPos;
4930 } else {
4931 // Update P's outstanding operand count.
4932 P->setNodeId(Degree);
4933 }
4934 }
4935 }
4936
4937 assert(SortedPos == AllNodes.end() &&
4938 "Topological sort incomplete!");
4939 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
4940 "First node in topological sort is not the entry token!");
4941 assert(AllNodes.front().getNodeId() == 0 &&
4942 "First node in topological sort has non-zero id!");
4943 assert(AllNodes.front().getNumOperands() == 0 &&
4944 "First node in topological sort has operands!");
4945 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
4946 "Last node in topologic sort has unexpected id!");
4947 assert(AllNodes.back().use_empty() &&
4948 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00004949 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00004950 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00004951}
4952
4953
Evan Cheng091cba12006-07-27 06:39:06 +00004954
Jim Laskey58b968b2005-08-17 20:08:02 +00004955//===----------------------------------------------------------------------===//
4956// SDNode Class
4957//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004958
Chris Lattner48b85922007-02-04 02:41:42 +00004959HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004960 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004961}
4962
Chris Lattner505e9822009-06-26 21:14:05 +00004963GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00004964 EVT VT, int64_t o, unsigned char TF)
Chris Lattner505e9822009-06-26 21:14:05 +00004965 : SDNode(Opc, DebugLoc::getUnknownLoc(), getSDVTList(VT)),
Chris Lattner2a4ed822009-06-25 21:21:14 +00004966 Offset(o), TargetFlags(TF) {
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004967 TheGlobal = const_cast<GlobalValue*>(GA);
4968}
Chris Lattner48b85922007-02-04 02:41:42 +00004969
Owen Andersone50ed302009-08-10 22:56:29 +00004970MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt,
Dale Johannesen3edb43e2009-01-28 21:18:29 +00004971 const Value *srcValue, int SVO,
4972 unsigned alignment, bool vol)
Dan Gohmana7ce7412009-02-03 00:08:45 +00004973 : SDNode(Opc, dl, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
4974 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Dale Johannesen3edb43e2009-01-28 21:18:29 +00004975 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4976 assert(getAlignment() == alignment && "Alignment representation error!");
4977 assert(isVolatile() == vol && "Volatile representation error!");
4978}
4979
Scott Michelfdc40a02009-02-17 22:15:04 +00004980MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
Dale Johannesen3edb43e2009-01-28 21:18:29 +00004981 const SDValue *Ops,
Owen Andersone50ed302009-08-10 22:56:29 +00004982 unsigned NumOps, EVT memvt, const Value *srcValue,
Dale Johannesen3edb43e2009-01-28 21:18:29 +00004983 int SVO, unsigned alignment, bool vol)
4984 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohmana7ce7412009-02-03 00:08:45 +00004985 MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
4986 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Dale Johannesen3edb43e2009-01-28 21:18:29 +00004987 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4988 assert(getAlignment() == alignment && "Alignment representation error!");
4989 assert(isVolatile() == vol && "Volatile representation error!");
4990}
4991
Dan Gohman36b5c132008-04-07 19:35:22 +00004992/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004993/// reference performed by this memory reference.
4994MachineMemOperand MemSDNode::getMemOperand() const {
Dale Johannesenfea90882008-10-24 01:06:58 +00004995 int Flags = 0;
Dan Gohman1ea58a52008-07-09 22:08:04 +00004996 if (isa<LoadSDNode>(this))
4997 Flags = MachineMemOperand::MOLoad;
4998 else if (isa<StoreSDNode>(this))
4999 Flags = MachineMemOperand::MOStore;
Mon P Wangc4d10212008-10-17 18:22:58 +00005000 else if (isa<AtomicSDNode>(this)) {
Dan Gohman1ea58a52008-07-09 22:08:04 +00005001 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
5002 }
Mon P Wangc4d10212008-10-17 18:22:58 +00005003 else {
5004 const MemIntrinsicSDNode* MemIntrinNode = dyn_cast<MemIntrinsicSDNode>(this);
5005 assert(MemIntrinNode && "Unknown MemSDNode opcode!");
5006 if (MemIntrinNode->readMem()) Flags |= MachineMemOperand::MOLoad;
5007 if (MemIntrinNode->writeMem()) Flags |= MachineMemOperand::MOStore;
5008 }
Dan Gohman1ea58a52008-07-09 22:08:04 +00005009
5010 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00005011 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
Scott Michelfdc40a02009-02-17 22:15:04 +00005012
Dan Gohman1ea58a52008-07-09 22:08:04 +00005013 // Check if the memory reference references a frame index
Scott Michelfdc40a02009-02-17 22:15:04 +00005014 const FrameIndexSDNode *FI =
Gabor Greifba36cb52008-08-28 21:40:38 +00005015 dyn_cast<const FrameIndexSDNode>(getBasePtr().getNode());
Mon P Wang28873102008-06-25 08:15:39 +00005016 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00005017 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
5018 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00005019 else
5020 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
5021 Size, getAlignment());
5022}
5023
Jim Laskey583bd472006-10-27 23:46:08 +00005024/// Profile - Gather unique data for the node.
5025///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005026void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00005027 AddNodeIDNode(ID, this);
5028}
5029
Owen Andersond8110fb2009-08-25 22:27:22 +00005030namespace {
5031 struct EVTArray {
5032 std::vector<EVT> VTs;
5033
5034 EVTArray() {
5035 VTs.reserve(MVT::LAST_VALUETYPE);
5036 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
5037 VTs.push_back(MVT((MVT::SimpleValueType)i));
5038 }
5039 };
5040}
5041
Owen Andersone50ed302009-08-10 22:56:29 +00005042static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00005043static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00005044static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00005045
Chris Lattnera3255112005-11-08 23:30:28 +00005046/// getValueTypeList - Return a pointer to the specified value type.
5047///
Owen Andersone50ed302009-08-10 22:56:29 +00005048const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005049 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00005050 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00005051 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00005052 } else {
Owen Andersond8110fb2009-08-25 22:27:22 +00005053 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00005054 }
Chris Lattnera3255112005-11-08 23:30:28 +00005055}
Duncan Sandsaf47b112007-10-16 09:56:48 +00005056
Chris Lattner5c884562005-01-12 18:37:47 +00005057/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5058/// indicated value. This method ignores uses of other values defined by this
5059/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00005060bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00005061 assert(Value < getNumValues() && "Bad value!");
5062
Roman Levensteindc1adac2008-04-07 10:06:32 +00005063 // TODO: Only iterate over uses of a given value of the node
5064 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00005065 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00005066 if (NUses == 0)
5067 return false;
5068 --NUses;
5069 }
Chris Lattner5c884562005-01-12 18:37:47 +00005070 }
5071
5072 // Found exactly the right number of uses?
5073 return NUses == 0;
5074}
5075
5076
Evan Cheng33d55952007-08-02 05:29:38 +00005077/// hasAnyUseOfValue - Return true if there are any use of the indicated
5078/// value. This method ignores uses of other values defined by this operation.
5079bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5080 assert(Value < getNumValues() && "Bad value!");
5081
Dan Gohman1373c1c2008-07-09 22:39:01 +00005082 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00005083 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00005084 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00005085
5086 return false;
5087}
5088
5089
Dan Gohman2a629952008-07-27 18:06:42 +00005090/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00005091///
Dan Gohman2a629952008-07-27 18:06:42 +00005092bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00005093 bool Seen = false;
5094 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005095 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00005096 if (User == this)
5097 Seen = true;
5098 else
5099 return false;
5100 }
5101
5102 return Seen;
5103}
5104
Evan Chenge6e97e62006-11-03 07:31:32 +00005105/// isOperand - Return true if this node is an operand of N.
5106///
Dan Gohman475871a2008-07-27 21:46:04 +00005107bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00005108 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5109 if (*this == N->getOperand(i))
5110 return true;
5111 return false;
5112}
5113
Evan Cheng917be682008-03-04 00:41:45 +00005114bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005115 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00005116 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005117 return true;
5118 return false;
5119}
Evan Cheng4ee62112006-02-05 06:29:23 +00005120
Chris Lattner572dee72008-01-16 05:49:24 +00005121/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00005122/// be a chain) reaches the specified operand without crossing any
Chris Lattner572dee72008-01-16 05:49:24 +00005123/// side-effecting instructions. In practice, this looks through token
5124/// factors and non-volatile loads. In order to remain efficient, this only
5125/// looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00005126bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00005127 unsigned Depth) const {
5128 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00005129
Chris Lattner572dee72008-01-16 05:49:24 +00005130 // Don't search too deeply, we just want to be able to see through
5131 // TokenFactor's etc.
5132 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005133
Chris Lattner572dee72008-01-16 05:49:24 +00005134 // If this is a token factor, all inputs to the TF happen in parallel. If any
5135 // of the operands of the TF reach dest, then we can do the xform.
5136 if (getOpcode() == ISD::TokenFactor) {
5137 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
5138 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5139 return true;
5140 return false;
5141 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005142
Chris Lattner572dee72008-01-16 05:49:24 +00005143 // Loads don't have side effects, look through them.
5144 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5145 if (!Ld->isVolatile())
5146 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5147 }
5148 return false;
5149}
5150
5151
Evan Chengc5fc57d2006-11-03 03:05:24 +00005152static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00005153 SmallPtrSet<SDNode *, 32> &Visited) {
5154 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00005155 return;
5156
5157 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005158 SDNode *Op = N->getOperand(i).getNode();
Evan Chengc5fc57d2006-11-03 03:05:24 +00005159 if (Op == P) {
5160 found = true;
5161 return;
5162 }
5163 findPredecessor(Op, P, found, Visited);
5164 }
5165}
5166
Evan Cheng917be682008-03-04 00:41:45 +00005167/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00005168/// is either an operand of N or it can be reached by recursively traversing
5169/// up the operands.
5170/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00005171bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00005172 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00005173 bool found = false;
5174 findPredecessor(N, this, found, Visited);
5175 return found;
5176}
5177
Evan Chengc5484282006-10-04 00:56:09 +00005178uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
5179 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005180 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00005181}
5182
Reid Spencer577cc322007-04-01 07:32:19 +00005183std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005184 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005185 default:
5186 if (getOpcode() < ISD::BUILTIN_OP_END)
5187 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00005188 if (isMachineOpcode()) {
5189 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005190 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00005191 if (getMachineOpcode() < TII->getNumOpcodes())
5192 return TII->get(getMachineOpcode()).getName();
5193 return "<<Unknown Machine Node>>";
5194 }
5195 if (G) {
Dan Gohmane9530ec2009-01-15 16:58:17 +00005196 const TargetLowering &TLI = G->getTargetLoweringInfo();
Dan Gohmane8be6c62008-07-17 19:10:17 +00005197 const char *Name = TLI.getTargetNodeName(getOpcode());
5198 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00005199 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005200 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005201 return "<<Unknown Node>>";
Scott Michelfdc40a02009-02-17 22:15:04 +00005202
Dan Gohmane8be6c62008-07-17 19:10:17 +00005203#ifndef NDEBUG
5204 case ISD::DELETED_NODE:
5205 return "<<Deleted Node!>>";
5206#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00005207 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00005208 case ISD::MEMBARRIER: return "MemBarrier";
Dan Gohman0b1d4a72008-12-23 21:37:04 +00005209 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
5210 case ISD::ATOMIC_SWAP: return "AtomicSwap";
5211 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
5212 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
5213 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
5214 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
5215 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
5216 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
5217 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
5218 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
5219 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
5220 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
Andrew Lenharth95762122005-03-31 21:24:06 +00005221 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00005222 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005223 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00005224 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005225 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00005226 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00005227 case ISD::AssertSext: return "AssertSext";
5228 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005229
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005230 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005231 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00005232 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005233
5234 case ISD::Constant: return "Constant";
5235 case ISD::ConstantFP: return "ConstantFP";
5236 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005237 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005238 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005239 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00005240 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Bill Wendling056292f2008-09-16 21:48:12 +00005241 case ISD::RETURNADDR: return "RETURNADDR";
5242 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005243 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00005244 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
Jim Grosbach1b747ad2009-08-11 00:09:57 +00005245 case ISD::LSDAADDR: return "LSDAADDR";
Bill Wendling056292f2008-09-16 21:48:12 +00005246 case ISD::EHSELECTION: return "EHSELECTION";
5247 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00005248 case ISD::ConstantPool: return "ConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005249 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00005250 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005251 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getZExtValue();
Chris Lattner48b61a72006-03-28 00:40:33 +00005252 return Intrinsic::getName((Intrinsic::ID)IID);
5253 }
5254 case ISD::INTRINSIC_VOID:
5255 case ISD::INTRINSIC_W_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005256 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getZExtValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00005257 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00005258 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00005259
Chris Lattnerb2827b02006-03-19 00:52:58 +00005260 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005261 case ISD::TargetConstant: return "TargetConstant";
5262 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005263 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005264 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005265 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005266 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00005267 case ISD::TargetConstantPool: return "TargetConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005268 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005269
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005270 case ISD::CopyToReg: return "CopyToReg";
5271 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005272 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00005273 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00005274 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00005275 case ISD::DBG_LABEL: return "dbg_label";
5276 case ISD::EH_LABEL: return "eh_label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00005277 case ISD::HANDLENODE: return "handlenode";
Scott Michelfdc40a02009-02-17 22:15:04 +00005278
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005279 // Unary operators
5280 case ISD::FABS: return "fabs";
5281 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00005282 case ISD::FSQRT: return "fsqrt";
5283 case ISD::FSIN: return "fsin";
5284 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00005285 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00005286 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00005287 case ISD::FTRUNC: return "ftrunc";
5288 case ISD::FFLOOR: return "ffloor";
5289 case ISD::FCEIL: return "fceil";
5290 case ISD::FRINT: return "frint";
5291 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005292
5293 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005294 case ISD::ADD: return "add";
5295 case ISD::SUB: return "sub";
5296 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00005297 case ISD::MULHU: return "mulhu";
5298 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005299 case ISD::SDIV: return "sdiv";
5300 case ISD::UDIV: return "udiv";
5301 case ISD::SREM: return "srem";
5302 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00005303 case ISD::SMUL_LOHI: return "smul_lohi";
5304 case ISD::UMUL_LOHI: return "umul_lohi";
5305 case ISD::SDIVREM: return "sdivrem";
Dan Gohmana47916d2008-09-08 16:30:29 +00005306 case ISD::UDIVREM: return "udivrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005307 case ISD::AND: return "and";
5308 case ISD::OR: return "or";
5309 case ISD::XOR: return "xor";
5310 case ISD::SHL: return "shl";
5311 case ISD::SRA: return "sra";
5312 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00005313 case ISD::ROTL: return "rotl";
5314 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00005315 case ISD::FADD: return "fadd";
5316 case ISD::FSUB: return "fsub";
5317 case ISD::FMUL: return "fmul";
5318 case ISD::FDIV: return "fdiv";
5319 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00005320 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00005321 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00005322
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005323 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00005324 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005325 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00005326 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005327 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005328 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00005329 case ISD::CONCAT_VECTORS: return "concat_vectors";
5330 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005331 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005332 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Dale Johannesen874ae252009-06-02 03:12:52 +00005333 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005334 case ISD::ADDC: return "addc";
5335 case ISD::ADDE: return "adde";
Bill Wendling1c55a9c2008-11-21 02:12:42 +00005336 case ISD::SADDO: return "saddo";
5337 case ISD::UADDO: return "uaddo";
Bill Wendling74c37652008-12-09 22:08:41 +00005338 case ISD::SSUBO: return "ssubo";
5339 case ISD::USUBO: return "usubo";
5340 case ISD::SMULO: return "smulo";
5341 case ISD::UMULO: return "umulo";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005342 case ISD::SUBC: return "subc";
5343 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00005344 case ISD::SHL_PARTS: return "shl_parts";
5345 case ISD::SRA_PARTS: return "sra_parts";
5346 case ISD::SRL_PARTS: return "srl_parts";
Scott Michelfdc40a02009-02-17 22:15:04 +00005347
Chris Lattner7f644642005-04-28 21:44:03 +00005348 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005349 case ISD::SIGN_EXTEND: return "sign_extend";
5350 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00005351 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00005352 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005353 case ISD::TRUNCATE: return "truncate";
5354 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00005355 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00005356 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005357 case ISD::FP_EXTEND: return "fp_extend";
5358
5359 case ISD::SINT_TO_FP: return "sint_to_fp";
5360 case ISD::UINT_TO_FP: return "uint_to_fp";
5361 case ISD::FP_TO_SINT: return "fp_to_sint";
5362 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00005363 case ISD::BIT_CONVERT: return "bit_convert";
Scott Michelfdc40a02009-02-17 22:15:04 +00005364
Mon P Wang77cdf302008-11-10 20:54:11 +00005365 case ISD::CONVERT_RNDSAT: {
5366 switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005367 default: llvm_unreachable("Unknown cvt code!");
Mon P Wang77cdf302008-11-10 20:54:11 +00005368 case ISD::CVT_FF: return "cvt_ff";
5369 case ISD::CVT_FS: return "cvt_fs";
5370 case ISD::CVT_FU: return "cvt_fu";
5371 case ISD::CVT_SF: return "cvt_sf";
5372 case ISD::CVT_UF: return "cvt_uf";
5373 case ISD::CVT_SS: return "cvt_ss";
5374 case ISD::CVT_SU: return "cvt_su";
5375 case ISD::CVT_US: return "cvt_us";
5376 case ISD::CVT_UU: return "cvt_uu";
5377 }
5378 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005379
5380 // Control flow instructions
5381 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00005382 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00005383 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005384 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00005385 case ISD::BR_CC: return "br_cc";
Chris Lattnera364fa12005-05-12 23:51:40 +00005386 case ISD::CALLSEQ_START: return "callseq_start";
5387 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005388
5389 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00005390 case ISD::LOAD: return "load";
5391 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00005392 case ISD::VAARG: return "vaarg";
5393 case ISD::VACOPY: return "vacopy";
5394 case ISD::VAEND: return "vaend";
5395 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005396 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00005397 case ISD::EXTRACT_ELEMENT: return "extract_element";
5398 case ISD::BUILD_PAIR: return "build_pair";
5399 case ISD::STACKSAVE: return "stacksave";
5400 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00005401 case ISD::TRAP: return "trap";
5402
Nate Begeman1b5db7a2006-01-16 08:07:10 +00005403 // Bit manipulation
5404 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00005405 case ISD::CTPOP: return "ctpop";
5406 case ISD::CTTZ: return "cttz";
5407 case ISD::CTLZ: return "ctlz";
5408
Chris Lattner36ce6912005-11-29 06:21:05 +00005409 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00005410 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00005411 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00005412
Duncan Sands36397f52007-07-27 12:58:54 +00005413 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00005414 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00005415
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005416 case ISD::CONDCODE:
5417 switch (cast<CondCodeSDNode>(this)->get()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005418 default: llvm_unreachable("Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005419 case ISD::SETOEQ: return "setoeq";
5420 case ISD::SETOGT: return "setogt";
5421 case ISD::SETOGE: return "setoge";
5422 case ISD::SETOLT: return "setolt";
5423 case ISD::SETOLE: return "setole";
5424 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005425
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005426 case ISD::SETO: return "seto";
5427 case ISD::SETUO: return "setuo";
5428 case ISD::SETUEQ: return "setue";
5429 case ISD::SETUGT: return "setugt";
5430 case ISD::SETUGE: return "setuge";
5431 case ISD::SETULT: return "setult";
5432 case ISD::SETULE: return "setule";
5433 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005434
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005435 case ISD::SETEQ: return "seteq";
5436 case ISD::SETGT: return "setgt";
5437 case ISD::SETGE: return "setge";
5438 case ISD::SETLT: return "setlt";
5439 case ISD::SETLE: return "setle";
5440 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005441 }
5442 }
5443}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005444
Evan Cheng144d8f02006-11-09 17:55:04 +00005445const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00005446 switch (AM) {
5447 default:
5448 return "";
5449 case ISD::PRE_INC:
5450 return "<pre-inc>";
5451 case ISD::PRE_DEC:
5452 return "<pre-dec>";
5453 case ISD::POST_INC:
5454 return "<post-inc>";
5455 case ISD::POST_DEC:
5456 return "<post-dec>";
5457 }
5458}
5459
Duncan Sands276dcbd2008-03-21 09:14:45 +00005460std::string ISD::ArgFlagsTy::getArgFlagsString() {
5461 std::string S = "< ";
5462
5463 if (isZExt())
5464 S += "zext ";
5465 if (isSExt())
5466 S += "sext ";
5467 if (isInReg())
5468 S += "inreg ";
5469 if (isSRet())
5470 S += "sret ";
5471 if (isByVal())
5472 S += "byval ";
5473 if (isNest())
5474 S += "nest ";
5475 if (getByValAlign())
5476 S += "byval-align:" + utostr(getByValAlign()) + " ";
5477 if (getOrigAlign())
5478 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5479 if (getByValSize())
5480 S += "byval-size:" + utostr(getByValSize()) + " ";
5481 return S + ">";
5482}
5483
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005484void SDNode::dump() const { dump(0); }
5485void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005486 print(errs(), G);
5487}
5488
Stuart Hastings80d69772009-02-04 16:46:19 +00005489void SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005490 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005491
5492 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005493 if (i) OS << ",";
Owen Anderson825b72b2009-08-11 20:47:22 +00005494 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00005495 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00005496 else
Owen Andersone50ed302009-08-10 22:56:29 +00005497 OS << getValueType(i).getEVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005498 }
Chris Lattner944fac72008-08-23 22:23:09 +00005499 OS << " = " << getOperationName(G);
Stuart Hastings80d69772009-02-04 16:46:19 +00005500}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005501
Stuart Hastings80d69772009-02-04 16:46:19 +00005502void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
Evan Chengce254432007-12-11 02:08:35 +00005503 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005504 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(this);
Chris Lattner944fac72008-08-23 22:23:09 +00005505 OS << "<";
Nate Begeman9008ca62009-04-27 18:41:29 +00005506 for (unsigned i = 0, e = ValueList[0].getVectorNumElements(); i != e; ++i) {
5507 int Idx = SVN->getMaskElt(i);
Chris Lattner944fac72008-08-23 22:23:09 +00005508 if (i) OS << ",";
Nate Begeman9008ca62009-04-27 18:41:29 +00005509 if (Idx < 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005510 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005511 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005512 OS << Idx;
Evan Chengce254432007-12-11 02:08:35 +00005513 }
Chris Lattner944fac72008-08-23 22:23:09 +00005514 OS << ">";
Evan Chengce254432007-12-11 02:08:35 +00005515 }
5516
Chris Lattnerc3aae252005-01-07 07:46:32 +00005517 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005518 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005519 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005520 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005521 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005522 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005523 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005524 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005525 OS << "<APFloat(";
Dale Johannesen23a98552008-10-09 23:00:39 +00005526 CSDN->getValueAPF().bitcastToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005527 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005528 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005529 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005530 dyn_cast<GlobalAddressSDNode>(this)) {
Dan Gohman668aff62008-10-18 18:22:42 +00005531 int64_t offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005532 OS << '<';
5533 WriteAsOperand(OS, GADN->getGlobal());
5534 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005535 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005536 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005537 else
Chris Lattner944fac72008-08-23 22:23:09 +00005538 OS << " " << offset;
Dan Gohman1944da12009-08-01 19:13:38 +00005539 if (unsigned int TF = GADN->getTargetFlags())
Chris Lattner2a4ed822009-06-25 21:21:14 +00005540 OS << " [TF=" << TF << ']';
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005541 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005542 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005543 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005544 OS << "<" << JTDN->getIndex() << ">";
Dan Gohman1944da12009-08-01 19:13:38 +00005545 if (unsigned int TF = JTDN->getTargetFlags())
Chris Lattnerf5a55462009-06-25 21:35:31 +00005546 OS << " [TF=" << TF << ']';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005547 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005548 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005549 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005550 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005551 else
Chris Lattner944fac72008-08-23 22:23:09 +00005552 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005553 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005554 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005555 else
Chris Lattner944fac72008-08-23 22:23:09 +00005556 OS << " " << offset;
Dan Gohman1944da12009-08-01 19:13:38 +00005557 if (unsigned int TF = CP->getTargetFlags())
Chris Lattnerf5a55462009-06-25 21:35:31 +00005558 OS << " [TF=" << TF << ']';
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005559 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005560 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005561 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5562 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005563 OS << LBB->getName() << " ";
5564 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005565 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005566 if (G && R->getReg() &&
5567 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner944fac72008-08-23 22:23:09 +00005568 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005569 } else {
Chris Lattner944fac72008-08-23 22:23:09 +00005570 OS << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005571 }
Bill Wendling056292f2008-09-16 21:48:12 +00005572 } else if (const ExternalSymbolSDNode *ES =
5573 dyn_cast<ExternalSymbolSDNode>(this)) {
5574 OS << "'" << ES->getSymbol() << "'";
Dan Gohman1944da12009-08-01 19:13:38 +00005575 if (unsigned int TF = ES->getTargetFlags())
Chris Lattner2a4ed822009-06-25 21:21:14 +00005576 OS << " [TF=" << TF << ']';
Chris Lattner2bf3c262005-05-09 04:08:27 +00005577 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5578 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005579 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005580 else
Chris Lattner944fac72008-08-23 22:23:09 +00005581 OS << "<null>";
Dan Gohman69de1932008-02-06 22:27:42 +00005582 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5583 if (M->MO.getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005584 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman69de1932008-02-06 22:27:42 +00005585 else
Chris Lattner944fac72008-08-23 22:23:09 +00005586 OS << "<null:" << M->MO.getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00005587 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005588 OS << ":" << N->getVT().getEVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005589 }
5590 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005591 const Value *SrcValue = LD->getSrcValue();
5592 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005593 OS << " <";
Evan Cheng81310132007-12-18 19:06:30 +00005594 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005595 OS << SrcValue;
Evan Cheng81310132007-12-18 19:06:30 +00005596 else
Chris Lattner944fac72008-08-23 22:23:09 +00005597 OS << "null";
5598 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005599
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005600 bool doExt = true;
5601 switch (LD->getExtensionType()) {
5602 default: doExt = false; break;
Chris Lattner944fac72008-08-23 22:23:09 +00005603 case ISD::EXTLOAD: OS << " <anyext "; break;
5604 case ISD::SEXTLOAD: OS << " <sext "; break;
5605 case ISD::ZEXTLOAD: OS << " <zext "; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005606 }
5607 if (doExt)
Owen Andersone50ed302009-08-10 22:56:29 +00005608 OS << LD->getMemoryVT().getEVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005609
Evan Cheng144d8f02006-11-09 17:55:04 +00005610 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005611 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005612 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005613 if (LD->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005614 OS << " <volatile>";
5615 OS << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005616 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005617 const Value *SrcValue = ST->getSrcValue();
5618 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005619 OS << " <";
Evan Cheng88ce93e2007-12-18 07:02:08 +00005620 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005621 OS << SrcValue;
Evan Cheng88ce93e2007-12-18 07:02:08 +00005622 else
Chris Lattner944fac72008-08-23 22:23:09 +00005623 OS << "null";
5624 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005625
5626 if (ST->isTruncatingStore())
Owen Andersone50ed302009-08-10 22:56:29 +00005627 OS << " <trunc " << ST->getMemoryVT().getEVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005628
5629 const char *AM = getIndexedModeName(ST->getAddressingMode());
5630 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005631 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005632 if (ST->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005633 OS << " <volatile>";
5634 OS << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005635 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5636 const Value *SrcValue = AT->getSrcValue();
5637 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005638 OS << " <";
Mon P Wang28873102008-06-25 08:15:39 +00005639 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005640 OS << SrcValue;
Mon P Wang28873102008-06-25 08:15:39 +00005641 else
Chris Lattner944fac72008-08-23 22:23:09 +00005642 OS << "null";
5643 OS << ":" << SrcOffset << ">";
Mon P Wang28873102008-06-25 08:15:39 +00005644 if (AT->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005645 OS << " <volatile>";
5646 OS << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005647 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005648}
5649
Stuart Hastings80d69772009-02-04 16:46:19 +00005650void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5651 print_types(OS, G);
5652 OS << " ";
5653 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
5654 if (i) OS << ", ";
5655 OS << (void*)getOperand(i).getNode();
5656 if (unsigned RN = getOperand(i).getResNo())
5657 OS << ":" << RN;
5658 }
5659 print_details(OS, G);
5660}
5661
Chris Lattnerde202b32005-11-09 23:47:37 +00005662static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005663 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00005664 if (N->getOperand(i).getNode()->hasOneUse())
5665 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005666 else
Chris Lattner45cfe542009-08-23 06:03:38 +00005667 errs() << "\n" << std::string(indent+2, ' ')
5668 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005669
Chris Lattnerea946cd2005-01-09 20:38:33 +00005670
Chris Lattner45cfe542009-08-23 06:03:38 +00005671 errs() << "\n";
5672 errs().indent(indent);
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005673 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005674}
5675
Chris Lattnerc3aae252005-01-07 07:46:32 +00005676void SelectionDAG::dump() const {
Chris Lattner45cfe542009-08-23 06:03:38 +00005677 errs() << "SelectionDAG has " << AllNodes.size() << " nodes:";
Scott Michelfdc40a02009-02-17 22:15:04 +00005678
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005679 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5680 I != E; ++I) {
5681 const SDNode *N = I;
Gabor Greifba36cb52008-08-28 21:40:38 +00005682 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005683 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005684 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005685
Gabor Greifba36cb52008-08-28 21:40:38 +00005686 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005687
Chris Lattner45cfe542009-08-23 06:03:38 +00005688 errs() << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005689}
5690
Stuart Hastings80d69772009-02-04 16:46:19 +00005691void SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const {
5692 print_types(OS, G);
5693 print_details(OS, G);
5694}
5695
5696typedef SmallPtrSet<const SDNode *, 128> VisitedSDNodeSet;
Stuart Hastings4caa0422009-02-04 20:30:10 +00005697static void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent,
Bill Wendling51b16f42009-05-30 01:09:53 +00005698 const SelectionDAG *G, VisitedSDNodeSet &once) {
5699 if (!once.insert(N)) // If we've been here before, return now.
Stuart Hastings80d69772009-02-04 16:46:19 +00005700 return;
5701 // Dump the current SDNode, but don't end the line yet.
5702 OS << std::string(indent, ' ');
5703 N->printr(OS, G);
5704 // Having printed this SDNode, walk the children:
5705 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5706 const SDNode *child = N->getOperand(i).getNode();
5707 if (i) OS << ",";
5708 OS << " ";
5709 if (child->getNumOperands() == 0) {
5710 // This child has no grandchildren; print it inline right here.
5711 child->printr(OS, G);
5712 once.insert(child);
Bill Wendling51b16f42009-05-30 01:09:53 +00005713 } else { // Just the address. FIXME: also print the child's opcode
Stuart Hastings80d69772009-02-04 16:46:19 +00005714 OS << (void*)child;
5715 if (unsigned RN = N->getOperand(i).getResNo())
Bill Wendling51b16f42009-05-30 01:09:53 +00005716 OS << ":" << RN;
Stuart Hastings80d69772009-02-04 16:46:19 +00005717 }
5718 }
5719 OS << "\n";
5720 // Dump children that have grandchildren on their own line(s).
5721 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5722 const SDNode *child = N->getOperand(i).getNode();
5723 DumpNodesr(OS, child, indent+2, G, once);
5724 }
5725}
5726
5727void SDNode::dumpr() const {
5728 VisitedSDNodeSet once;
5729 DumpNodesr(errs(), this, 0, 0, once);
Stuart Hastings80d69772009-02-04 16:46:19 +00005730}
5731
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00005732
5733// getAddressSpace - Return the address space this GlobalAddress belongs to.
5734unsigned GlobalAddressSDNode::getAddressSpace() const {
5735 return getGlobal()->getType()->getAddressSpace();
5736}
5737
5738
Evan Chengd6594ae2006-09-12 21:00:35 +00005739const Type *ConstantPoolSDNode::getType() const {
5740 if (isMachineConstantPoolEntry())
5741 return Val.MachineCPVal->getType();
5742 return Val.ConstVal->getType();
5743}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005744
Bob Wilson24e338e2009-03-02 23:24:16 +00005745bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
5746 APInt &SplatUndef,
5747 unsigned &SplatBitSize,
5748 bool &HasAnyUndefs,
5749 unsigned MinSplatBits) {
Owen Andersone50ed302009-08-10 22:56:29 +00005750 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00005751 assert(VT.isVector() && "Expected a vector type");
5752 unsigned sz = VT.getSizeInBits();
5753 if (MinSplatBits > sz)
5754 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005755
Bob Wilson24e338e2009-03-02 23:24:16 +00005756 SplatValue = APInt(sz, 0);
5757 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005758
Bob Wilson24e338e2009-03-02 23:24:16 +00005759 // Get the bits. Bits with undefined values (when the corresponding element
5760 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
5761 // in SplatValue. If any of the values are not constant, give up and return
5762 // false.
5763 unsigned int nOps = getNumOperands();
5764 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
5765 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
5766 for (unsigned i = 0; i < nOps; ++i) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005767 SDValue OpVal = getOperand(i);
Bob Wilson24e338e2009-03-02 23:24:16 +00005768 unsigned BitPos = i * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005769
Bob Wilson24e338e2009-03-02 23:24:16 +00005770 if (OpVal.getOpcode() == ISD::UNDEF)
5771 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos +EltBitSize);
5772 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Bob Wilsonb1303d02009-04-13 22:05:19 +00005773 SplatValue |= (APInt(CN->getAPIntValue()).zextOrTrunc(EltBitSize).
5774 zextOrTrunc(sz) << BitPos);
Bob Wilson24e338e2009-03-02 23:24:16 +00005775 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00005776 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00005777 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005778 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005779 }
5780
Bob Wilson24e338e2009-03-02 23:24:16 +00005781 // The build_vector is all constants or undefs. Find the smallest element
5782 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005783
Bob Wilson24e338e2009-03-02 23:24:16 +00005784 HasAnyUndefs = (SplatUndef != 0);
5785 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005786
Bob Wilson24e338e2009-03-02 23:24:16 +00005787 unsigned HalfSize = sz / 2;
5788 APInt HighValue = APInt(SplatValue).lshr(HalfSize).trunc(HalfSize);
5789 APInt LowValue = APInt(SplatValue).trunc(HalfSize);
5790 APInt HighUndef = APInt(SplatUndef).lshr(HalfSize).trunc(HalfSize);
5791 APInt LowUndef = APInt(SplatUndef).trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005792
Bob Wilson24e338e2009-03-02 23:24:16 +00005793 // If the two halves do not match (ignoring undef bits), stop here.
5794 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
5795 MinSplatBits > HalfSize)
5796 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005797
Bob Wilson24e338e2009-03-02 23:24:16 +00005798 SplatValue = HighValue | LowValue;
5799 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00005800
Bob Wilson24e338e2009-03-02 23:24:16 +00005801 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005802 }
5803
Bob Wilson24e338e2009-03-02 23:24:16 +00005804 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005805 return true;
5806}
Nate Begeman9008ca62009-04-27 18:41:29 +00005807
Owen Andersone50ed302009-08-10 22:56:29 +00005808bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00005809 // Find the first non-undef value in the shuffle mask.
5810 unsigned i, e;
5811 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
5812 /* search */;
5813
Nate Begemana6415752009-04-29 18:13:31 +00005814 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00005815
Nate Begeman5a5ca152009-04-29 05:20:52 +00005816 // Make sure all remaining elements are either undef or the same as the first
5817 // non-undef value.
5818 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005819 if (Mask[i] >= 0 && Mask[i] != Idx)
5820 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00005821 return true;
5822}