Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1 | //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 10 | // This implements the SelectionDAG class. |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Bill Wendling | e36025e | 2009-12-21 19:34:59 +0000 | [diff] [blame] | 13 | |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/SelectionDAG.h" |
Evan Cheng | a8efe28 | 2010-03-14 19:56:39 +0000 | [diff] [blame] | 15 | #include "SDNodeDbgValue.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SetVector.h" |
| 17 | #include "llvm/ADT/SmallPtrSet.h" |
| 18 | #include "llvm/ADT/SmallSet.h" |
| 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/ADT/StringExtras.h" |
| 21 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 25 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/CallingConv.h" |
| 27 | #include "llvm/IR/Constants.h" |
| 28 | #include "llvm/IR/DataLayout.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 29 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DerivedTypes.h" |
| 31 | #include "llvm/IR/Function.h" |
| 32 | #include "llvm/IR/GlobalAlias.h" |
| 33 | #include "llvm/IR/GlobalVariable.h" |
| 34 | #include "llvm/IR/Intrinsics.h" |
Bill Wendling | 6f287b2 | 2008-09-30 21:22:07 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" |
David Greene | 55d146e | 2010-01-05 01:24:36 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 37 | #include "llvm/Support/ErrorHandling.h" |
Owen Anderson | b445908 | 2009-06-25 17:09:00 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 39 | #include "llvm/Support/MathExtras.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Mutex.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
| 42 | #include "llvm/Target/TargetInstrInfo.h" |
| 43 | #include "llvm/Target/TargetIntrinsicInfo.h" |
| 44 | #include "llvm/Target/TargetLowering.h" |
| 45 | #include "llvm/Target/TargetMachine.h" |
| 46 | #include "llvm/Target/TargetOptions.h" |
| 47 | #include "llvm/Target/TargetRegisterInfo.h" |
| 48 | #include "llvm/Target/TargetSelectionDAGInfo.h" |
Jeff Cohen | fd161e9 | 2005-01-09 20:41:56 +0000 | [diff] [blame] | 49 | #include <algorithm> |
Jeff Cohen | 97af751 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 50 | #include <cmath> |
Chris Lattner | e25738c | 2004-06-02 04:28:06 +0000 | [diff] [blame] | 51 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 0b3e525 | 2006-08-15 19:11:05 +0000 | [diff] [blame] | 53 | /// makeVTList - Return an instance of the SDVTList struct initialized with the |
| 54 | /// specified members. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 55 | static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) { |
Chris Lattner | 0b3e525 | 2006-08-15 19:11:05 +0000 | [diff] [blame] | 56 | SDVTList Res = {VTs, NumVTs}; |
| 57 | return Res; |
| 58 | } |
| 59 | |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 60 | // Default null implementations of the callbacks. |
| 61 | void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {} |
| 62 | void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {} |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 63 | |
Jim Laskey | 58b968b | 2005-08-17 20:08:02 +0000 | [diff] [blame] | 64 | //===----------------------------------------------------------------------===// |
| 65 | // ConstantFPSDNode Class |
| 66 | //===----------------------------------------------------------------------===// |
| 67 | |
| 68 | /// isExactlyValue - We don't rely on operator== working on double values, as |
| 69 | /// it returns true for things that are clearly not equal, like -0.0 and 0.0. |
| 70 | /// As such, this method can be used to do an exact bit-for-bit comparison of |
| 71 | /// two floating point values. |
Dale Johannesen | e6c1742 | 2007-08-26 01:18:27 +0000 | [diff] [blame] | 72 | bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const { |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 73 | return getValueAPF().bitwiseIsEqual(V); |
Jim Laskey | 58b968b | 2005-08-17 20:08:02 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 76 | bool ConstantFPSDNode::isValueValidForType(EVT VT, |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 77 | const APFloat& Val) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 78 | assert(VT.isFloatingPoint() && "Can only convert between FP types"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 79 | |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 80 | // convert modifies in place, so make a copy. |
| 81 | APFloat Val2 = APFloat(Val); |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 82 | bool losesInfo; |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 83 | (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT), |
| 84 | APFloat::rmNearestTiesToEven, |
Dale Johannesen | 23a9855 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 85 | &losesInfo); |
| 86 | return !losesInfo; |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Jim Laskey | 58b968b | 2005-08-17 20:08:02 +0000 | [diff] [blame] | 89 | //===----------------------------------------------------------------------===// |
Chris Lattner | 61d4399 | 2006-03-25 22:57:01 +0000 | [diff] [blame] | 90 | // ISD Namespace |
Jim Laskey | 58b968b | 2005-08-17 20:08:02 +0000 | [diff] [blame] | 91 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 92 | |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 93 | /// isBuildVectorAllOnes - Return true if the specified node is a |
Chris Lattner | 61d4399 | 2006-03-25 22:57:01 +0000 | [diff] [blame] | 94 | /// BUILD_VECTOR where all of the elements are ~0 or undef. |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 95 | bool ISD::isBuildVectorAllOnes(const SDNode *N) { |
Chris Lattner | 547a16f | 2006-04-15 23:38:00 +0000 | [diff] [blame] | 96 | // Look through a bit convert. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 97 | if (N->getOpcode() == ISD::BITCAST) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 98 | N = N->getOperand(0).getNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 99 | |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 100 | if (N->getOpcode() != ISD::BUILD_VECTOR) return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 61d4399 | 2006-03-25 22:57:01 +0000 | [diff] [blame] | 102 | unsigned i = 0, e = N->getNumOperands(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 61d4399 | 2006-03-25 22:57:01 +0000 | [diff] [blame] | 104 | // Skip over all of the undef values. |
| 105 | while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) |
| 106 | ++i; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 61d4399 | 2006-03-25 22:57:01 +0000 | [diff] [blame] | 108 | // Do not accept an all-undef vector. |
| 109 | if (i == e) return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 61d4399 | 2006-03-25 22:57:01 +0000 | [diff] [blame] | 111 | // Do not accept build_vectors that aren't all constants or which have non-~0 |
Jim Grosbach | 331ff3b | 2012-03-21 17:48:04 +0000 | [diff] [blame] | 112 | // elements. We have to be a bit careful here, as the type of the constant |
| 113 | // may not be the same as the type of the vector elements due to type |
| 114 | // legalization (the elements are promoted to a legal type for the target and |
| 115 | // a vector of a type may be legal when the base element type is not). |
| 116 | // We only want to check enough bits to cover the vector elements, because |
| 117 | // we care if the resultant vector is all ones, not whether the individual |
| 118 | // constants are. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 119 | SDValue NotZero = N->getOperand(i); |
Jim Grosbach | 331ff3b | 2012-03-21 17:48:04 +0000 | [diff] [blame] | 120 | unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits(); |
Jakub Staszak | 554c676 | 2012-09-30 21:24:57 +0000 | [diff] [blame] | 121 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) { |
| 122 | if (CN->getAPIntValue().countTrailingOnes() < EltSize) |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 123 | return false; |
Jakub Staszak | 554c676 | 2012-09-30 21:24:57 +0000 | [diff] [blame] | 124 | } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) { |
| 125 | if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize) |
Dan Gohman | 6c23150 | 2008-02-29 01:47:35 +0000 | [diff] [blame] | 126 | return false; |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 127 | } else |
Chris Lattner | 61d4399 | 2006-03-25 22:57:01 +0000 | [diff] [blame] | 128 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 61d4399 | 2006-03-25 22:57:01 +0000 | [diff] [blame] | 130 | // Okay, we have at least one ~0 value, check to see if the rest match or are |
Jim Grosbach | 331ff3b | 2012-03-21 17:48:04 +0000 | [diff] [blame] | 131 | // undefs. Even with the above element type twiddling, this should be OK, as |
| 132 | // the same type legalization should have applied to all the elements. |
Chris Lattner | 61d4399 | 2006-03-25 22:57:01 +0000 | [diff] [blame] | 133 | for (++i; i != e; ++i) |
| 134 | if (N->getOperand(i) != NotZero && |
| 135 | N->getOperand(i).getOpcode() != ISD::UNDEF) |
| 136 | return false; |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | |
Evan Cheng | 4a14784 | 2006-03-26 09:50:58 +0000 | [diff] [blame] | 141 | /// isBuildVectorAllZeros - Return true if the specified node is a |
| 142 | /// BUILD_VECTOR where all of the elements are 0 or undef. |
| 143 | bool ISD::isBuildVectorAllZeros(const SDNode *N) { |
Chris Lattner | 547a16f | 2006-04-15 23:38:00 +0000 | [diff] [blame] | 144 | // Look through a bit convert. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 145 | if (N->getOpcode() == ISD::BITCAST) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 146 | N = N->getOperand(0).getNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 147 | |
Evan Cheng | 4a14784 | 2006-03-26 09:50:58 +0000 | [diff] [blame] | 148 | if (N->getOpcode() != ISD::BUILD_VECTOR) return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 149 | |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 150 | unsigned i = 0, e = N->getNumOperands(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 151 | |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 152 | // Skip over all of the undef values. |
| 153 | while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) |
| 154 | ++i; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 155 | |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 156 | // Do not accept an all-undef vector. |
| 157 | if (i == e) return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 158 | |
Dan Gohman | 68f32cb | 2009-06-04 16:49:15 +0000 | [diff] [blame] | 159 | // Do not accept build_vectors that aren't all constants or which have non-0 |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 160 | // elements. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 161 | SDValue Zero = N->getOperand(i); |
Jakub Staszak | 554c676 | 2012-09-30 21:24:57 +0000 | [diff] [blame] | 162 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) { |
| 163 | if (!CN->isNullValue()) |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 164 | return false; |
Jakub Staszak | 554c676 | 2012-09-30 21:24:57 +0000 | [diff] [blame] | 165 | } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) { |
| 166 | if (!CFPN->getValueAPF().isPosZero()) |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 167 | return false; |
| 168 | } else |
| 169 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 170 | |
Dan Gohman | 68f32cb | 2009-06-04 16:49:15 +0000 | [diff] [blame] | 171 | // Okay, we have at least one 0 value, check to see if the rest match or are |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 172 | // undefs. |
| 173 | for (++i; i != e; ++i) |
| 174 | if (N->getOperand(i) != Zero && |
| 175 | N->getOperand(i).getOpcode() != ISD::UNDEF) |
| 176 | return false; |
| 177 | return true; |
Evan Cheng | 4a14784 | 2006-03-26 09:50:58 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 180 | /// \brief Return true if the specified node is a BUILD_VECTOR node of |
| 181 | /// all ConstantSDNode or undef. |
| 182 | bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) { |
| 183 | if (N->getOpcode() != ISD::BUILD_VECTOR) |
| 184 | return false; |
| 185 | |
| 186 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 187 | SDValue Op = N->getOperand(i); |
| 188 | if (Op.getOpcode() == ISD::UNDEF) |
| 189 | continue; |
| 190 | if (!isa<ConstantSDNode>(Op)) |
| 191 | return false; |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
Evan Cheng | efec751 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 196 | /// isScalarToVector - Return true if the specified node is a |
| 197 | /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low |
| 198 | /// element is not an undef. |
| 199 | bool ISD::isScalarToVector(const SDNode *N) { |
| 200 | if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) |
| 201 | return true; |
| 202 | |
| 203 | if (N->getOpcode() != ISD::BUILD_VECTOR) |
| 204 | return false; |
| 205 | if (N->getOperand(0).getOpcode() == ISD::UNDEF) |
| 206 | return false; |
| 207 | unsigned NumElems = N->getNumOperands(); |
Mon P Wang | cab98e3 | 2010-11-19 19:08:12 +0000 | [diff] [blame] | 208 | if (NumElems == 1) |
| 209 | return false; |
Evan Cheng | efec751 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 210 | for (unsigned i = 1; i < NumElems; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 211 | SDValue V = N->getOperand(i); |
Evan Cheng | efec751 | 2008-02-18 23:04:32 +0000 | [diff] [blame] | 212 | if (V.getOpcode() != ISD::UNDEF) |
| 213 | return false; |
| 214 | } |
| 215 | return true; |
| 216 | } |
| 217 | |
Nadav Rotem | b87bdac | 2012-07-15 08:38:23 +0000 | [diff] [blame] | 218 | /// allOperandsUndef - Return true if the node has at least one operand |
| 219 | /// and all operands of the specified node are ISD::UNDEF. |
| 220 | bool ISD::allOperandsUndef(const SDNode *N) { |
| 221 | // Return false if the node has no operands. |
| 222 | // This is "logically inconsistent" with the definition of "all" but |
| 223 | // is probably the desired behavior. |
| 224 | if (N->getNumOperands() == 0) |
| 225 | return false; |
| 226 | |
| 227 | for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i) |
| 228 | if (N->getOperand(i).getOpcode() != ISD::UNDEF) |
| 229 | return false; |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 234 | ISD::NodeType ISD::getExtForLoadExtType(ISD::LoadExtType ExtType) { |
| 235 | switch (ExtType) { |
| 236 | case ISD::EXTLOAD: |
| 237 | return ISD::ANY_EXTEND; |
| 238 | case ISD::SEXTLOAD: |
| 239 | return ISD::SIGN_EXTEND; |
| 240 | case ISD::ZEXTLOAD: |
| 241 | return ISD::ZERO_EXTEND; |
| 242 | default: |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | llvm_unreachable("Invalid LoadExtType"); |
| 247 | } |
| 248 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 249 | /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) |
| 250 | /// when given the operation for (X op Y). |
| 251 | ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) { |
| 252 | // To perform this operation, we just need to swap the L and G bits of the |
| 253 | // operation. |
| 254 | unsigned OldL = (Operation >> 2) & 1; |
| 255 | unsigned OldG = (Operation >> 1) & 1; |
| 256 | return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits |
| 257 | (OldL << 1) | // New G bit |
Bill Wendling | a1dc602 | 2008-10-19 20:51:12 +0000 | [diff] [blame] | 258 | (OldG << 2)); // New L bit. |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /// getSetCCInverse - Return the operation corresponding to !(X op Y), where |
| 262 | /// 'op' is a valid SetCC operation. |
| 263 | ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) { |
| 264 | unsigned Operation = Op; |
| 265 | if (isInteger) |
| 266 | Operation ^= 7; // Flip L, G, E bits, but not U. |
| 267 | else |
| 268 | Operation ^= 15; // Flip all of the condition bits. |
Bill Wendling | a1dc602 | 2008-10-19 20:51:12 +0000 | [diff] [blame] | 269 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 270 | if (Operation > ISD::SETTRUE2) |
Bill Wendling | a1dc602 | 2008-10-19 20:51:12 +0000 | [diff] [blame] | 271 | Operation &= ~8; // Don't let N and U bits get set. |
| 272 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 273 | return ISD::CondCode(Operation); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | /// isSignedOp - For an integer comparison, return 1 if the comparison is a |
| 278 | /// signed operation and 2 if the result is an unsigned comparison. Return zero |
| 279 | /// if the operation does not depend on the sign of the input (setne and seteq). |
| 280 | static int isSignedOp(ISD::CondCode Opcode) { |
| 281 | switch (Opcode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 282 | default: llvm_unreachable("Illegal integer setcc operation!"); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 283 | case ISD::SETEQ: |
| 284 | case ISD::SETNE: return 0; |
| 285 | case ISD::SETLT: |
| 286 | case ISD::SETLE: |
| 287 | case ISD::SETGT: |
| 288 | case ISD::SETGE: return 1; |
| 289 | case ISD::SETULT: |
| 290 | case ISD::SETULE: |
| 291 | case ISD::SETUGT: |
| 292 | case ISD::SETUGE: return 2; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /// getSetCCOrOperation - Return the result of a logical OR between different |
| 297 | /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function |
| 298 | /// returns SETCC_INVALID if it is not possible to represent the resultant |
| 299 | /// comparison. |
| 300 | ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2, |
| 301 | bool isInteger) { |
| 302 | if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) |
| 303 | // Cannot fold a signed integer setcc with an unsigned integer setcc. |
| 304 | return ISD::SETCC_INVALID; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 305 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 306 | unsigned Op = Op1 | Op2; // Combine all of the condition bits. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 307 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 308 | // If the N and U bits get set then the resultant comparison DOES suddenly |
| 309 | // care about orderedness, and is true when ordered. |
| 310 | if (Op > ISD::SETTRUE2) |
Chris Lattner | e41102b | 2006-05-12 17:03:46 +0000 | [diff] [blame] | 311 | Op &= ~16; // Clear the U bit if the N bit is set. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 312 | |
Chris Lattner | e41102b | 2006-05-12 17:03:46 +0000 | [diff] [blame] | 313 | // Canonicalize illegal integer setcc's. |
| 314 | if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT |
| 315 | Op = ISD::SETNE; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 316 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 317 | return ISD::CondCode(Op); |
| 318 | } |
| 319 | |
| 320 | /// getSetCCAndOperation - Return the result of a logical AND between different |
| 321 | /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This |
| 322 | /// function returns zero if it is not possible to represent the resultant |
| 323 | /// comparison. |
| 324 | ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2, |
| 325 | bool isInteger) { |
| 326 | if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) |
| 327 | // Cannot fold a signed setcc with an unsigned setcc. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 328 | return ISD::SETCC_INVALID; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 329 | |
| 330 | // Combine all of the condition bits. |
Chris Lattner | a83385f | 2006-04-27 05:01:07 +0000 | [diff] [blame] | 331 | ISD::CondCode Result = ISD::CondCode(Op1 & Op2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 332 | |
Chris Lattner | a83385f | 2006-04-27 05:01:07 +0000 | [diff] [blame] | 333 | // Canonicalize illegal integer setcc's. |
| 334 | if (isInteger) { |
| 335 | switch (Result) { |
| 336 | default: break; |
Chris Lattner | 883a52d | 2006-06-28 18:29:47 +0000 | [diff] [blame] | 337 | case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT |
Dan Gohman | d64a78c | 2008-05-14 18:17:09 +0000 | [diff] [blame] | 338 | case ISD::SETOEQ: // SETEQ & SETU[LG]E |
Chris Lattner | 883a52d | 2006-06-28 18:29:47 +0000 | [diff] [blame] | 339 | case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE |
| 340 | case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE |
| 341 | case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE |
Chris Lattner | a83385f | 2006-04-27 05:01:07 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 344 | |
Chris Lattner | a83385f | 2006-04-27 05:01:07 +0000 | [diff] [blame] | 345 | return Result; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Jim Laskey | 58b968b | 2005-08-17 20:08:02 +0000 | [diff] [blame] | 348 | //===----------------------------------------------------------------------===// |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 349 | // SDNode Profile Support |
| 350 | //===----------------------------------------------------------------------===// |
| 351 | |
Jim Laskey | def69b9 | 2006-10-27 23:52:51 +0000 | [diff] [blame] | 352 | /// AddNodeIDOpcode - Add the node opcode to the NodeID data. |
| 353 | /// |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 354 | static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) { |
| 355 | ID.AddInteger(OpC); |
| 356 | } |
| 357 | |
| 358 | /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them |
| 359 | /// solely with their pointer. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 360 | static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 361 | ID.AddPointer(VTList.VTs); |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Jim Laskey | def69b9 | 2006-10-27 23:52:51 +0000 | [diff] [blame] | 364 | /// AddNodeIDOperands - Various routines for adding operands to the NodeID data. |
| 365 | /// |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 366 | static void AddNodeIDOperands(FoldingSetNodeID &ID, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 367 | const SDValue *Ops, unsigned NumOps) { |
Chris Lattner | 63e3f14 | 2007-02-04 07:28:00 +0000 | [diff] [blame] | 368 | for (; NumOps; --NumOps, ++Ops) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 369 | ID.AddPointer(Ops->getNode()); |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 370 | ID.AddInteger(Ops->getResNo()); |
Chris Lattner | 63e3f14 | 2007-02-04 07:28:00 +0000 | [diff] [blame] | 371 | } |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 374 | /// AddNodeIDOperands - Various routines for adding operands to the NodeID data. |
| 375 | /// |
| 376 | static void AddNodeIDOperands(FoldingSetNodeID &ID, |
| 377 | const SDUse *Ops, unsigned NumOps) { |
| 378 | for (; NumOps; --NumOps, ++Ops) { |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 379 | ID.AddPointer(Ops->getNode()); |
| 380 | ID.AddInteger(Ops->getResNo()); |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 384 | static void AddNodeIDNode(FoldingSetNodeID &ID, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 385 | unsigned short OpC, SDVTList VTList, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 386 | const SDValue *OpList, unsigned N) { |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 387 | AddNodeIDOpcode(ID, OpC); |
| 388 | AddNodeIDValueTypes(ID, VTList); |
| 389 | AddNodeIDOperands(ID, OpList, N); |
| 390 | } |
| 391 | |
Duncan Sands | 0dc4045 | 2008-10-27 15:30:53 +0000 | [diff] [blame] | 392 | /// AddNodeIDCustom - If this is an SDNode with special info, add this info to |
| 393 | /// the NodeID data. |
| 394 | static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) { |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 395 | switch (N->getOpcode()) { |
Chris Lattner | 2a4ed82 | 2009-06-25 21:21:14 +0000 | [diff] [blame] | 396 | case ISD::TargetExternalSymbol: |
| 397 | case ISD::ExternalSymbol: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 398 | llvm_unreachable("Should only be used on nodes with operands"); |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 399 | default: break; // Normal nodes don't need extra info. |
| 400 | case ISD::TargetConstant: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 401 | case ISD::Constant: { |
| 402 | const ConstantSDNode *C = cast<ConstantSDNode>(N); |
| 403 | ID.AddPointer(C->getConstantIntValue()); |
| 404 | ID.AddBoolean(C->isOpaque()); |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 405 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 406 | } |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 407 | case ISD::TargetConstantFP: |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 408 | case ISD::ConstantFP: { |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 409 | ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue()); |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 410 | break; |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 411 | } |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 412 | case ISD::TargetGlobalAddress: |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 413 | case ISD::GlobalAddress: |
| 414 | case ISD::TargetGlobalTLSAddress: |
| 415 | case ISD::GlobalTLSAddress: { |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 416 | const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N); |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 417 | ID.AddPointer(GA->getGlobal()); |
| 418 | ID.AddInteger(GA->getOffset()); |
Chris Lattner | 2a4ed82 | 2009-06-25 21:21:14 +0000 | [diff] [blame] | 419 | ID.AddInteger(GA->getTargetFlags()); |
Pete Cooper | 32ecfb4 | 2012-07-30 20:23:19 +0000 | [diff] [blame] | 420 | ID.AddInteger(GA->getAddressSpace()); |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 421 | break; |
| 422 | } |
| 423 | case ISD::BasicBlock: |
| 424 | ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock()); |
| 425 | break; |
| 426 | case ISD::Register: |
| 427 | ID.AddInteger(cast<RegisterSDNode>(N)->getReg()); |
| 428 | break; |
Jakob Stoklund Olesen | 9cf37e8 | 2012-01-18 23:52:12 +0000 | [diff] [blame] | 429 | case ISD::RegisterMask: |
| 430 | ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask()); |
| 431 | break; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 432 | case ISD::SRCVALUE: |
| 433 | ID.AddPointer(cast<SrcValueSDNode>(N)->getValue()); |
| 434 | break; |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 435 | case ISD::FrameIndex: |
| 436 | case ISD::TargetFrameIndex: |
| 437 | ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex()); |
| 438 | break; |
| 439 | case ISD::JumpTable: |
| 440 | case ISD::TargetJumpTable: |
| 441 | ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex()); |
Chris Lattner | f5a5546 | 2009-06-25 21:35:31 +0000 | [diff] [blame] | 442 | ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags()); |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 443 | break; |
| 444 | case ISD::ConstantPool: |
| 445 | case ISD::TargetConstantPool: { |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 446 | const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N); |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 447 | ID.AddInteger(CP->getAlignment()); |
| 448 | ID.AddInteger(CP->getOffset()); |
| 449 | if (CP->isMachineConstantPoolEntry()) |
Jim Grosbach | 5405d58 | 2011-09-27 20:59:33 +0000 | [diff] [blame] | 450 | CP->getMachineCPVal()->addSelectionDAGCSEId(ID); |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 451 | else |
| 452 | ID.AddPointer(CP->getConstVal()); |
Chris Lattner | f5a5546 | 2009-06-25 21:35:31 +0000 | [diff] [blame] | 453 | ID.AddInteger(CP->getTargetFlags()); |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 454 | break; |
| 455 | } |
Jakob Stoklund Olesen | 74500bd | 2012-08-07 22:37:05 +0000 | [diff] [blame] | 456 | case ISD::TargetIndex: { |
| 457 | const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N); |
| 458 | ID.AddInteger(TI->getIndex()); |
| 459 | ID.AddInteger(TI->getOffset()); |
| 460 | ID.AddInteger(TI->getTargetFlags()); |
| 461 | break; |
| 462 | } |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 463 | case ISD::LOAD: { |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 464 | const LoadSDNode *LD = cast<LoadSDNode>(N); |
Duncan Sands | 3b3adbb | 2008-06-06 12:49:32 +0000 | [diff] [blame] | 465 | ID.AddInteger(LD->getMemoryVT().getRawBits()); |
Dan Gohman | a7ce741 | 2009-02-03 00:08:45 +0000 | [diff] [blame] | 466 | ID.AddInteger(LD->getRawSubclassData()); |
Pete Cooper | 32ecfb4 | 2012-07-30 20:23:19 +0000 | [diff] [blame] | 467 | ID.AddInteger(LD->getPointerInfo().getAddrSpace()); |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 468 | break; |
| 469 | } |
| 470 | case ISD::STORE: { |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 471 | const StoreSDNode *ST = cast<StoreSDNode>(N); |
Duncan Sands | 3b3adbb | 2008-06-06 12:49:32 +0000 | [diff] [blame] | 472 | ID.AddInteger(ST->getMemoryVT().getRawBits()); |
Dan Gohman | a7ce741 | 2009-02-03 00:08:45 +0000 | [diff] [blame] | 473 | ID.AddInteger(ST->getRawSubclassData()); |
Pete Cooper | 32ecfb4 | 2012-07-30 20:23:19 +0000 | [diff] [blame] | 474 | ID.AddInteger(ST->getPointerInfo().getAddrSpace()); |
Dale Johannesen | 2041a0e | 2007-03-30 21:38:07 +0000 | [diff] [blame] | 475 | break; |
| 476 | } |
Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 477 | case ISD::ATOMIC_CMP_SWAP: |
| 478 | case ISD::ATOMIC_SWAP: |
| 479 | case ISD::ATOMIC_LOAD_ADD: |
| 480 | case ISD::ATOMIC_LOAD_SUB: |
| 481 | case ISD::ATOMIC_LOAD_AND: |
| 482 | case ISD::ATOMIC_LOAD_OR: |
| 483 | case ISD::ATOMIC_LOAD_XOR: |
| 484 | case ISD::ATOMIC_LOAD_NAND: |
| 485 | case ISD::ATOMIC_LOAD_MIN: |
| 486 | case ISD::ATOMIC_LOAD_MAX: |
| 487 | case ISD::ATOMIC_LOAD_UMIN: |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 488 | case ISD::ATOMIC_LOAD_UMAX: |
| 489 | case ISD::ATOMIC_LOAD: |
| 490 | case ISD::ATOMIC_STORE: { |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 491 | const AtomicSDNode *AT = cast<AtomicSDNode>(N); |
Dan Gohman | a7ce741 | 2009-02-03 00:08:45 +0000 | [diff] [blame] | 492 | ID.AddInteger(AT->getMemoryVT().getRawBits()); |
| 493 | ID.AddInteger(AT->getRawSubclassData()); |
Pete Cooper | 32ecfb4 | 2012-07-30 20:23:19 +0000 | [diff] [blame] | 494 | ID.AddInteger(AT->getPointerInfo().getAddrSpace()); |
| 495 | break; |
| 496 | } |
| 497 | case ISD::PREFETCH: { |
| 498 | const MemSDNode *PF = cast<MemSDNode>(N); |
| 499 | ID.AddInteger(PF->getPointerInfo().getAddrSpace()); |
Mon P Wang | 2887310 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 500 | break; |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 501 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 502 | case ISD::VECTOR_SHUFFLE: { |
| 503 | const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N); |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 504 | for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 505 | i != e; ++i) |
| 506 | ID.AddInteger(SVN->getMaskElt(i)); |
| 507 | break; |
| 508 | } |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 509 | case ISD::TargetBlockAddress: |
| 510 | case ISD::BlockAddress: { |
Michael Liao | 6c7ccaa | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 511 | const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N); |
| 512 | ID.AddPointer(BA->getBlockAddress()); |
| 513 | ID.AddInteger(BA->getOffset()); |
| 514 | ID.AddInteger(BA->getTargetFlags()); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 515 | break; |
| 516 | } |
Mon P Wang | 2887310 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 517 | } // end switch (N->getOpcode()) |
Pete Cooper | 32ecfb4 | 2012-07-30 20:23:19 +0000 | [diff] [blame] | 518 | |
| 519 | // Target specific memory nodes could also have address spaces to check. |
| 520 | if (N->isTargetMemoryOpcode()) |
| 521 | ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace()); |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Duncan Sands | 0dc4045 | 2008-10-27 15:30:53 +0000 | [diff] [blame] | 524 | /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID |
| 525 | /// data. |
| 526 | static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) { |
| 527 | AddNodeIDOpcode(ID, N->getOpcode()); |
| 528 | // Add the return value info. |
| 529 | AddNodeIDValueTypes(ID, N->getVTList()); |
| 530 | // Add the operand info. |
| 531 | AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands()); |
| 532 | |
| 533 | // Handle SDNode leafs with special info. |
| 534 | AddNodeIDCustom(ID, N); |
| 535 | } |
| 536 | |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 537 | /// encodeMemSDNodeFlags - Generic routine for computing a value for use in |
David Greene | 1157f79 | 2010-02-17 20:21:42 +0000 | [diff] [blame] | 538 | /// the CSE map that carries volatility, temporalness, indexing mode, and |
Dan Gohman | a7ce741 | 2009-02-03 00:08:45 +0000 | [diff] [blame] | 539 | /// extension/truncation information. |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 540 | /// |
Bill Wendling | a1dc602 | 2008-10-19 20:51:12 +0000 | [diff] [blame] | 541 | static inline unsigned |
David Greene | 1157f79 | 2010-02-17 20:21:42 +0000 | [diff] [blame] | 542 | encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 543 | bool isNonTemporal, bool isInvariant) { |
Dan Gohman | a7ce741 | 2009-02-03 00:08:45 +0000 | [diff] [blame] | 544 | assert((ConvType & 3) == ConvType && |
| 545 | "ConvType may not require more than 2 bits!"); |
| 546 | assert((AM & 7) == AM && |
| 547 | "AM may not require more than 3 bits!"); |
| 548 | return ConvType | |
| 549 | (AM << 2) | |
David Greene | 1157f79 | 2010-02-17 20:21:42 +0000 | [diff] [blame] | 550 | (isVolatile << 5) | |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 551 | (isNonTemporal << 6) | |
| 552 | (isInvariant << 7); |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 555 | //===----------------------------------------------------------------------===// |
Jim Laskey | 58b968b | 2005-08-17 20:08:02 +0000 | [diff] [blame] | 556 | // SelectionDAG Class |
| 557 | //===----------------------------------------------------------------------===// |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 558 | |
Duncan Sands | 0dc4045 | 2008-10-27 15:30:53 +0000 | [diff] [blame] | 559 | /// doNotCSE - Return true if CSE should not be performed for this node. |
| 560 | static bool doNotCSE(SDNode *N) { |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 561 | if (N->getValueType(0) == MVT::Glue) |
Duncan Sands | 0dc4045 | 2008-10-27 15:30:53 +0000 | [diff] [blame] | 562 | return true; // Never CSE anything that produces a flag. |
| 563 | |
| 564 | switch (N->getOpcode()) { |
| 565 | default: break; |
| 566 | case ISD::HANDLENODE: |
Duncan Sands | 0dc4045 | 2008-10-27 15:30:53 +0000 | [diff] [blame] | 567 | case ISD::EH_LABEL: |
Duncan Sands | 0dc4045 | 2008-10-27 15:30:53 +0000 | [diff] [blame] | 568 | return true; // Never CSE these nodes. |
| 569 | } |
| 570 | |
| 571 | // Check that remaining values produced are not flags. |
| 572 | for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 573 | if (N->getValueType(i) == MVT::Glue) |
Duncan Sands | 0dc4045 | 2008-10-27 15:30:53 +0000 | [diff] [blame] | 574 | return true; // Never CSE anything that produces a flag. |
| 575 | |
| 576 | return false; |
| 577 | } |
| 578 | |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 579 | /// RemoveDeadNodes - This method deletes all unreachable nodes in the |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 580 | /// SelectionDAG. |
| 581 | void SelectionDAG::RemoveDeadNodes() { |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 582 | // Create a dummy node (which is not added to allnodes), that adds a reference |
| 583 | // to the root node, preventing it from being deleted. |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 584 | HandleSDNode Dummy(getRoot()); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 585 | |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 586 | SmallVector<SDNode*, 128> DeadNodes; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 587 | |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 588 | // Add all obviously-dead nodes to the DeadNodes worklist. |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 589 | for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I) |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 590 | if (I->use_empty()) |
| 591 | DeadNodes.push_back(I); |
| 592 | |
Dan Gohman | 0fe9c6e | 2008-07-07 20:57:48 +0000 | [diff] [blame] | 593 | RemoveDeadNodes(DeadNodes); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 594 | |
Dan Gohman | 0fe9c6e | 2008-07-07 20:57:48 +0000 | [diff] [blame] | 595 | // If the root changed (e.g. it was a dead load, update the root). |
| 596 | setRoot(Dummy.getValue()); |
| 597 | } |
| 598 | |
| 599 | /// RemoveDeadNodes - This method deletes the unreachable nodes in the |
| 600 | /// given list, and any nodes that become unreachable as a result. |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 601 | void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) { |
Dan Gohman | 0fe9c6e | 2008-07-07 20:57:48 +0000 | [diff] [blame] | 602 | |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 603 | // Process the worklist, deleting the nodes and adding their uses to the |
| 604 | // worklist. |
| 605 | while (!DeadNodes.empty()) { |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 606 | SDNode *N = DeadNodes.pop_back_val(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 607 | |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 608 | for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next) |
| 609 | DUL->NodeDeleted(N, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 610 | |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 611 | // Take the node out of the appropriate CSE map. |
| 612 | RemoveNodeFromCSEMaps(N); |
| 613 | |
| 614 | // Next, brutally remove the operand list. This is safe to do, as there are |
| 615 | // no cycles in the graph. |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 616 | for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) { |
| 617 | SDUse &Use = *I++; |
| 618 | SDNode *Operand = Use.getNode(); |
| 619 | Use.set(SDValue()); |
| 620 | |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 621 | // Now that we removed this operand, see if there are no uses of it left. |
| 622 | if (Operand->use_empty()) |
| 623 | DeadNodes.push_back(Operand); |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 624 | } |
Bill Wendling | a1dc602 | 2008-10-19 20:51:12 +0000 | [diff] [blame] | 625 | |
Dan Gohman | c533612 | 2009-01-19 22:39:36 +0000 | [diff] [blame] | 626 | DeallocateNode(N); |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 627 | } |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 630 | void SelectionDAG::RemoveDeadNode(SDNode *N){ |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 631 | SmallVector<SDNode*, 16> DeadNodes(1, N); |
Eli Friedman | 2efa35f | 2011-11-08 01:25:24 +0000 | [diff] [blame] | 632 | |
| 633 | // Create a dummy node that adds a reference to the root node, preventing |
| 634 | // it from being deleted. (This matters if the root is an operand of the |
| 635 | // dead node.) |
| 636 | HandleSDNode Dummy(getRoot()); |
| 637 | |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 638 | RemoveDeadNodes(DeadNodes); |
Evan Cheng | 130a647 | 2006-10-12 20:34:05 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Chris Lattner | c26aefa | 2005-08-29 21:59:31 +0000 | [diff] [blame] | 641 | void SelectionDAG::DeleteNode(SDNode *N) { |
Chris Lattner | c26aefa | 2005-08-29 21:59:31 +0000 | [diff] [blame] | 642 | // First take this out of the appropriate CSE map. |
| 643 | RemoveNodeFromCSEMaps(N); |
| 644 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 645 | // Finally, remove uses due to operands of this node, remove from the |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 646 | // AllNodes list, and delete the node. |
| 647 | DeleteNodeNotInCSEMaps(N); |
| 648 | } |
| 649 | |
| 650 | void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) { |
Dan Gohman | e77f89d | 2009-01-25 16:20:37 +0000 | [diff] [blame] | 651 | assert(N != AllNodes.begin() && "Cannot delete the entry node!"); |
| 652 | assert(N->use_empty() && "Cannot delete a node that is not dead!"); |
Dan Gohman | c533612 | 2009-01-19 22:39:36 +0000 | [diff] [blame] | 653 | |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 654 | // Drop all of the operands and decrement used node's use counts. |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 655 | N->DropOperands(); |
Bill Wendling | a1dc602 | 2008-10-19 20:51:12 +0000 | [diff] [blame] | 656 | |
Dan Gohman | c533612 | 2009-01-19 22:39:36 +0000 | [diff] [blame] | 657 | DeallocateNode(N); |
| 658 | } |
| 659 | |
| 660 | void SelectionDAG::DeallocateNode(SDNode *N) { |
| 661 | if (N->OperandsNeedDelete) |
Chris Lattner | 63e3f14 | 2007-02-04 07:28:00 +0000 | [diff] [blame] | 662 | delete[] N->OperandList; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 663 | |
Dan Gohman | c533612 | 2009-01-19 22:39:36 +0000 | [diff] [blame] | 664 | // Set the opcode to DELETED_NODE to help catch bugs when node |
| 665 | // memory is reallocated. |
| 666 | N->NodeType = ISD::DELETED_NODE; |
| 667 | |
Dan Gohman | 1146728 | 2008-08-26 01:44:34 +0000 | [diff] [blame] | 668 | NodeAllocator.Deallocate(AllNodes.remove(N)); |
Daniel Dunbar | 819309e | 2009-12-16 20:10:05 +0000 | [diff] [blame] | 669 | |
Evan Cheng | bfcb305 | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 670 | // If any of the SDDbgValue nodes refer to this SDNode, invalidate them. |
Benjamin Kramer | 22a54c1 | 2011-06-18 13:13:44 +0000 | [diff] [blame] | 671 | ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N); |
Evan Cheng | bfcb305 | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 672 | for (unsigned i = 0, e = DbgVals.size(); i != e; ++i) |
| 673 | DbgVals[i]->setIsInvalidated(); |
Chris Lattner | c26aefa | 2005-08-29 21:59:31 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 676 | /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that |
| 677 | /// correspond to it. This is useful when we're about to delete or repurpose |
| 678 | /// the node. We don't want future request for structurally identical nodes |
| 679 | /// to return N anymore. |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 680 | bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) { |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 681 | bool Erased = false; |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 682 | switch (N->getOpcode()) { |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 683 | case ISD::HANDLENODE: return false; // noop. |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 684 | case ISD::CONDCODE: |
| 685 | assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] && |
| 686 | "Cond code doesn't exist!"); |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 687 | Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0; |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 688 | CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0; |
| 689 | break; |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 690 | case ISD::ExternalSymbol: |
| 691 | Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 692 | break; |
Chris Lattner | 1af2231 | 2009-06-25 18:45:50 +0000 | [diff] [blame] | 693 | case ISD::TargetExternalSymbol: { |
| 694 | ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N); |
| 695 | Erased = TargetExternalSymbols.erase( |
| 696 | std::pair<std::string,unsigned char>(ESN->getSymbol(), |
| 697 | ESN->getTargetFlags())); |
Andrew Lenharth | 2a2de66 | 2005-10-23 03:40:17 +0000 | [diff] [blame] | 698 | break; |
Chris Lattner | 1af2231 | 2009-06-25 18:45:50 +0000 | [diff] [blame] | 699 | } |
Duncan Sands | f411b83 | 2007-10-17 13:49:58 +0000 | [diff] [blame] | 700 | case ISD::VALUETYPE: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 701 | EVT VT = cast<VTSDNode>(N)->getVT(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 702 | if (VT.isExtended()) { |
Duncan Sands | f411b83 | 2007-10-17 13:49:58 +0000 | [diff] [blame] | 703 | Erased = ExtendedValueTypeNodes.erase(VT); |
| 704 | } else { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 705 | Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0; |
| 706 | ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0; |
Duncan Sands | f411b83 | 2007-10-17 13:49:58 +0000 | [diff] [blame] | 707 | } |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 708 | break; |
Duncan Sands | f411b83 | 2007-10-17 13:49:58 +0000 | [diff] [blame] | 709 | } |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 710 | default: |
Chris Lattner | 4a283e9 | 2006-08-11 18:38:11 +0000 | [diff] [blame] | 711 | // Remove it from the CSE Map. |
Duncan Sands | a30b7d2 | 2010-12-12 13:22:50 +0000 | [diff] [blame] | 712 | assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!"); |
| 713 | assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!"); |
Chris Lattner | 4a283e9 | 2006-08-11 18:38:11 +0000 | [diff] [blame] | 714 | Erased = CSEMap.RemoveNode(N); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 715 | break; |
| 716 | } |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 717 | #ifndef NDEBUG |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 718 | // Verify that the node was actually in one of the CSE maps, unless it has a |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 719 | // flag result (which cannot be CSE'd) or is one of the special cases that are |
| 720 | // not subject to CSE. |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 721 | if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue && |
Duncan Sands | 0dc4045 | 2008-10-27 15:30:53 +0000 | [diff] [blame] | 722 | !N->isMachineOpcode() && !doNotCSE(N)) { |
Dan Gohman | b5bec2b | 2007-06-19 14:13:56 +0000 | [diff] [blame] | 723 | N->dump(this); |
David Greene | 55d146e | 2010-01-05 01:24:36 +0000 | [diff] [blame] | 724 | dbgs() << "\n"; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 725 | llvm_unreachable("Node is not in map!"); |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 726 | } |
| 727 | #endif |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 728 | return Erased; |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 731 | /// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE |
| 732 | /// maps and modified in place. Add it back to the CSE maps, unless an identical |
| 733 | /// node already exists, in which case transfer all its users to the existing |
| 734 | /// node. This transfer can potentially trigger recursive merging. |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 735 | /// |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 736 | void |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 737 | SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) { |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 738 | // For node types that aren't CSE'd, just act as if no identical node |
| 739 | // already exists. |
| 740 | if (!doNotCSE(N)) { |
| 741 | SDNode *Existing = CSEMap.GetOrInsertNode(N); |
| 742 | if (Existing != N) { |
| 743 | // If there was already an existing matching node, use ReplaceAllUsesWith |
| 744 | // to replace the dead one with the existing one. This can cause |
| 745 | // recursive merging of other unrelated nodes down the line. |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 746 | ReplaceAllUsesWith(N, Existing); |
Evan Cheng | 71e8685 | 2008-07-08 20:06:39 +0000 | [diff] [blame] | 747 | |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 748 | // N is now dead. Inform the listeners and delete it. |
| 749 | for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next) |
| 750 | DUL->NodeDeleted(N, Existing); |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 751 | DeleteNodeNotInCSEMaps(N); |
| 752 | return; |
| 753 | } |
| 754 | } |
Evan Cheng | 71e8685 | 2008-07-08 20:06:39 +0000 | [diff] [blame] | 755 | |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 756 | // If the node doesn't already exist, we updated it. Inform listeners. |
| 757 | for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next) |
| 758 | DUL->NodeUpdated(N); |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 761 | /// FindModifiedNodeSlot - Find a slot for the specified node if its operands |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 762 | /// were replaced with those specified. If this node is never memoized, |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 763 | /// return null, otherwise return a pointer to the slot it would take. If a |
| 764 | /// node already exists with these operands, the slot will be non-null. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 765 | SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op, |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 766 | void *&InsertPos) { |
Duncan Sands | 0dc4045 | 2008-10-27 15:30:53 +0000 | [diff] [blame] | 767 | if (doNotCSE(N)) |
| 768 | return 0; |
Evan Cheng | 71e8685 | 2008-07-08 20:06:39 +0000 | [diff] [blame] | 769 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 770 | SDValue Ops[] = { Op }; |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 771 | FoldingSetNodeID ID; |
Chris Lattner | 63e3f14 | 2007-02-04 07:28:00 +0000 | [diff] [blame] | 772 | AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1); |
Duncan Sands | 0dc4045 | 2008-10-27 15:30:53 +0000 | [diff] [blame] | 773 | AddNodeIDCustom(ID, N); |
Daniel Dunbar | 819309e | 2009-12-16 20:10:05 +0000 | [diff] [blame] | 774 | SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos); |
Daniel Dunbar | 819309e | 2009-12-16 20:10:05 +0000 | [diff] [blame] | 775 | return Node; |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /// FindModifiedNodeSlot - Find a slot for the specified node if its operands |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 779 | /// were replaced with those specified. If this node is never memoized, |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 780 | /// return null, otherwise return a pointer to the slot it would take. If a |
| 781 | /// node already exists with these operands, the slot will be non-null. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 782 | SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 783 | SDValue Op1, SDValue Op2, |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 784 | void *&InsertPos) { |
Duncan Sands | 0dc4045 | 2008-10-27 15:30:53 +0000 | [diff] [blame] | 785 | if (doNotCSE(N)) |
| 786 | return 0; |
| 787 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 788 | SDValue Ops[] = { Op1, Op2 }; |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 789 | FoldingSetNodeID ID; |
Chris Lattner | 63e3f14 | 2007-02-04 07:28:00 +0000 | [diff] [blame] | 790 | AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2); |
Duncan Sands | 0dc4045 | 2008-10-27 15:30:53 +0000 | [diff] [blame] | 791 | AddNodeIDCustom(ID, N); |
Daniel Dunbar | 819309e | 2009-12-16 20:10:05 +0000 | [diff] [blame] | 792 | SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos); |
Daniel Dunbar | 819309e | 2009-12-16 20:10:05 +0000 | [diff] [blame] | 793 | return Node; |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | |
| 797 | /// FindModifiedNodeSlot - Find a slot for the specified node if its operands |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 798 | /// were replaced with those specified. If this node is never memoized, |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 799 | /// return null, otherwise return a pointer to the slot it would take. If a |
| 800 | /// node already exists with these operands, the slot will be non-null. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 801 | SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 802 | const SDValue *Ops,unsigned NumOps, |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 803 | void *&InsertPos) { |
Duncan Sands | 0dc4045 | 2008-10-27 15:30:53 +0000 | [diff] [blame] | 804 | if (doNotCSE(N)) |
| 805 | return 0; |
Evan Cheng | 71e8685 | 2008-07-08 20:06:39 +0000 | [diff] [blame] | 806 | |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 807 | FoldingSetNodeID ID; |
Dan Gohman | 575e2f4 | 2007-06-04 15:49:41 +0000 | [diff] [blame] | 808 | AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps); |
Duncan Sands | 0dc4045 | 2008-10-27 15:30:53 +0000 | [diff] [blame] | 809 | AddNodeIDCustom(ID, N); |
Daniel Dunbar | 819309e | 2009-12-16 20:10:05 +0000 | [diff] [blame] | 810 | SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos); |
Daniel Dunbar | 819309e | 2009-12-16 20:10:05 +0000 | [diff] [blame] | 811 | return Node; |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 812 | } |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 813 | |
Benjamin Kramer | 3ca1363 | 2010-11-20 15:53:24 +0000 | [diff] [blame] | 814 | #ifndef NDEBUG |
Duncan Sands | 59d2dad | 2010-11-20 11:25:00 +0000 | [diff] [blame] | 815 | /// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid. |
| 816 | static void VerifyNodeCommon(SDNode *N) { |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 817 | switch (N->getOpcode()) { |
| 818 | default: |
| 819 | break; |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 820 | case ISD::BUILD_PAIR: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 821 | EVT VT = N->getValueType(0); |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 822 | assert(N->getNumValues() == 1 && "Too many results!"); |
| 823 | assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) && |
| 824 | "Wrong return type!"); |
| 825 | assert(N->getNumOperands() == 2 && "Wrong number of operands!"); |
| 826 | assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() && |
| 827 | "Mismatched operand types!"); |
| 828 | assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() && |
| 829 | "Wrong operand type!"); |
| 830 | assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() && |
| 831 | "Wrong return type size"); |
| 832 | break; |
| 833 | } |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 834 | case ISD::BUILD_VECTOR: { |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 835 | assert(N->getNumValues() == 1 && "Too many results!"); |
| 836 | assert(N->getValueType(0).isVector() && "Wrong return type!"); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 837 | assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() && |
Duncan Sands | d22ec5f | 2008-10-29 14:22:20 +0000 | [diff] [blame] | 838 | "Wrong number of operands!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 839 | EVT EltVT = N->getValueType(0).getVectorElementType(); |
Eli Friedman | 9db817f | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 840 | for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) { |
Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 841 | assert((I->getValueType() == EltVT || |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 842 | (EltVT.isInteger() && I->getValueType().isInteger() && |
| 843 | EltVT.bitsLE(I->getValueType()))) && |
| 844 | "Wrong operand type!"); |
Eli Friedman | 9db817f | 2011-09-09 21:04:06 +0000 | [diff] [blame] | 845 | assert(I->getValueType() == N->getOperand(0).getValueType() && |
| 846 | "Operands must all have the same type"); |
| 847 | } |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 848 | break; |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
Duncan Sands | 59d2dad | 2010-11-20 11:25:00 +0000 | [diff] [blame] | 853 | /// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid. |
| 854 | static void VerifySDNode(SDNode *N) { |
| 855 | // The SDNode allocators cannot be used to allocate nodes with fields that are |
| 856 | // not present in an SDNode! |
| 857 | assert(!isa<MemSDNode>(N) && "Bad MemSDNode!"); |
| 858 | assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!"); |
| 859 | assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!"); |
| 860 | assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!"); |
| 861 | assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!"); |
| 862 | assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!"); |
| 863 | assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!"); |
| 864 | assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!"); |
| 865 | assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!"); |
| 866 | assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!"); |
| 867 | assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!"); |
| 868 | assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!"); |
| 869 | assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!"); |
| 870 | assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!"); |
| 871 | assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!"); |
| 872 | assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!"); |
| 873 | assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!"); |
| 874 | assert(!isa<VTSDNode>(N) && "Bad VTSDNode!"); |
| 875 | assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!"); |
| 876 | |
| 877 | VerifyNodeCommon(N); |
| 878 | } |
| 879 | |
| 880 | /// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is |
| 881 | /// invalid. |
| 882 | static void VerifyMachineNode(SDNode *N) { |
| 883 | // The MachineNode allocators cannot be used to allocate nodes with fields |
| 884 | // that are not present in a MachineNode! |
| 885 | // Currently there are no such nodes. |
| 886 | |
| 887 | VerifyNodeCommon(N); |
| 888 | } |
Benjamin Kramer | 3ca1363 | 2010-11-20 15:53:24 +0000 | [diff] [blame] | 889 | #endif // NDEBUG |
Duncan Sands | 59d2dad | 2010-11-20 11:25:00 +0000 | [diff] [blame] | 890 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 891 | /// getEVTAlignment - Compute the default alignment value for the |
Dan Gohman | 9c6e70e | 2008-07-08 23:46:32 +0000 | [diff] [blame] | 892 | /// given type. |
| 893 | /// |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 894 | unsigned SelectionDAG::getEVTAlignment(EVT VT) const { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 895 | Type *Ty = VT == MVT::iPTR ? |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 896 | PointerType::get(Type::getInt8Ty(*getContext()), 0) : |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 897 | VT.getTypeForEVT(*getContext()); |
Dan Gohman | 9c6e70e | 2008-07-08 23:46:32 +0000 | [diff] [blame] | 898 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 899 | return TM.getTargetLowering()->getDataLayout()->getABITypeAlignment(Ty); |
Dan Gohman | 9c6e70e | 2008-07-08 23:46:32 +0000 | [diff] [blame] | 900 | } |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 901 | |
Dale Johannesen | 92570c4 | 2009-02-07 02:15:05 +0000 | [diff] [blame] | 902 | // EntryNode could meaningfully have debug info if we can find it... |
Devang Patel | 0508d04 | 2011-12-15 18:21:18 +0000 | [diff] [blame] | 903 | SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 904 | : TM(tm), TSI(*tm.getSelectionDAGInfo()), TLI(0), OptLevel(OL), |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 905 | EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)), |
Daniel Sanders | ea28aaf | 2013-11-15 12:56:49 +0000 | [diff] [blame] | 906 | Root(getEntryNode()), NewNodesMustHaveLegalTypes(false), |
| 907 | UpdateListeners(0) { |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 908 | AllNodes.push_back(&EntryNode); |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 909 | DbgInfo = new SDDbgInfo(); |
Dan Gohman | 6f17966 | 2008-08-23 00:50:30 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 912 | void SelectionDAG::init(MachineFunction &mf, const TargetLowering *tli) { |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 913 | MF = &mf; |
Bill Wendling | ee287ca | 2013-11-22 05:17:44 +0000 | [diff] [blame] | 914 | TLI = tli; |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 915 | Context = &mf.getFunction()->getContext(); |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 918 | SelectionDAG::~SelectionDAG() { |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 919 | assert(!UpdateListeners && "Dangling registered DAGUpdateListeners"); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 920 | allnodes_clear(); |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 921 | delete DbgInfo; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | void SelectionDAG::allnodes_clear() { |
Dan Gohman | 1146728 | 2008-08-26 01:44:34 +0000 | [diff] [blame] | 925 | assert(&*AllNodes.begin() == &EntryNode); |
| 926 | AllNodes.remove(AllNodes.begin()); |
Dan Gohman | c533612 | 2009-01-19 22:39:36 +0000 | [diff] [blame] | 927 | while (!AllNodes.empty()) |
| 928 | DeallocateNode(AllNodes.begin()); |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 931 | void SelectionDAG::clear() { |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 932 | allnodes_clear(); |
| 933 | OperandAllocator.Reset(); |
| 934 | CSEMap.clear(); |
| 935 | |
| 936 | ExtendedValueTypeNodes.clear(); |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 937 | ExternalSymbols.clear(); |
| 938 | TargetExternalSymbols.clear(); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 939 | std::fill(CondCodeNodes.begin(), CondCodeNodes.end(), |
| 940 | static_cast<CondCodeSDNode*>(0)); |
| 941 | std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(), |
| 942 | static_cast<SDNode*>(0)); |
| 943 | |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 944 | EntryNode.UseList = 0; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 945 | AllNodes.push_back(&EntryNode); |
| 946 | Root = getEntryNode(); |
Evan Cheng | 31441b7 | 2010-03-29 20:48:30 +0000 | [diff] [blame] | 947 | DbgInfo->clear(); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 950 | SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) { |
Nadav Rotem | a3c42f3 | 2011-09-27 11:16:47 +0000 | [diff] [blame] | 951 | return VT.bitsGT(Op.getValueType()) ? |
| 952 | getNode(ISD::ANY_EXTEND, DL, VT, Op) : |
| 953 | getNode(ISD::TRUNCATE, DL, VT, Op); |
| 954 | } |
| 955 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 956 | SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) { |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 957 | return VT.bitsGT(Op.getValueType()) ? |
| 958 | getNode(ISD::SIGN_EXTEND, DL, VT, Op) : |
| 959 | getNode(ISD::TRUNCATE, DL, VT, Op); |
| 960 | } |
| 961 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 962 | SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) { |
Duncan Sands | 3a66a68 | 2009-10-13 21:04:12 +0000 | [diff] [blame] | 963 | return VT.bitsGT(Op.getValueType()) ? |
| 964 | getNode(ISD::ZERO_EXTEND, DL, VT, Op) : |
| 965 | getNode(ISD::TRUNCATE, DL, VT, Op); |
| 966 | } |
| 967 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 968 | SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) { |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 969 | assert(!VT.isVector() && |
| 970 | "getZeroExtendInReg should use the vector element type instead of " |
| 971 | "the vector type!"); |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 972 | if (Op.getValueType() == VT) return Op; |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 973 | unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); |
| 974 | APInt Imm = APInt::getLowBitsSet(BitWidth, |
Bill Wendling | 6ce610f | 2009-01-30 22:23:15 +0000 | [diff] [blame] | 975 | VT.getSizeInBits()); |
| 976 | return getNode(ISD::AND, DL, Op.getValueType(), Op, |
| 977 | getConstant(Imm, Op.getValueType())); |
| 978 | } |
| 979 | |
Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 980 | /// getNOT - Create a bitwise NOT operation as (XOR Val, -1). |
| 981 | /// |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 982 | SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) { |
Chris Lattner | 5cf0b6e | 2010-02-24 22:44:06 +0000 | [diff] [blame] | 983 | EVT EltVT = VT.getScalarType(); |
Dan Gohman | bd209ab | 2009-04-20 22:51:43 +0000 | [diff] [blame] | 984 | SDValue NegOne = |
| 985 | getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT); |
Bill Wendling | 41b9d27 | 2009-01-30 22:11:22 +0000 | [diff] [blame] | 986 | return getNode(ISD::XOR, DL, VT, Val, NegOne); |
| 987 | } |
| 988 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 989 | SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT, bool isO) { |
Chris Lattner | 5cf0b6e | 2010-02-24 22:44:06 +0000 | [diff] [blame] | 990 | EVT EltVT = VT.getScalarType(); |
Dan Gohman | ce9bc12 | 2009-01-27 20:39:34 +0000 | [diff] [blame] | 991 | assert((EltVT.getSizeInBits() >= 64 || |
| 992 | (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) && |
| 993 | "getConstant with a uint64_t value that doesn't fit in the type!"); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 994 | return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT, isO); |
Dan Gohman | 6394b09 | 2008-02-08 22:59:30 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 997 | SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT, bool isO) |
| 998 | { |
| 999 | return getConstant(*ConstantInt::get(*Context, Val), VT, isT, isO); |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 1002 | SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT, |
| 1003 | bool isO) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1004 | assert(VT.isInteger() && "Cannot create FP integer constant!"); |
Dan Gohman | 8908132 | 2007-12-12 22:21:26 +0000 | [diff] [blame] | 1005 | |
Chris Lattner | 5cf0b6e | 2010-02-24 22:44:06 +0000 | [diff] [blame] | 1006 | EVT EltVT = VT.getScalarType(); |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 1007 | const ConstantInt *Elt = &Val; |
Chris Lattner | 61b0941 | 2006-08-11 21:01:22 +0000 | [diff] [blame] | 1008 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1009 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 1010 | |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 1011 | // In some cases the vector type is legal but the element type is illegal and |
| 1012 | // needs to be promoted, for example v8i8 on ARM. In this case, promote the |
| 1013 | // inserted value (the type does not need to match the vector element type). |
| 1014 | // Any extra bits introduced will be truncated away. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1015 | if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) == |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 1016 | TargetLowering::TypePromoteInteger) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1017 | EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT); |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 1018 | APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits()); |
| 1019 | Elt = ConstantInt::get(*getContext(), NewVal); |
| 1020 | } |
Daniel Sanders | ea28aaf | 2013-11-15 12:56:49 +0000 | [diff] [blame] | 1021 | // In other cases the element type is illegal and needs to be expanded, for |
| 1022 | // example v2i64 on MIPS32. In this case, find the nearest legal type, split |
| 1023 | // the value into n parts and use a vector type with n-times the elements. |
| 1024 | // Then bitcast to the type requested. |
| 1025 | // Legalizing constants too early makes the DAGCombiner's job harder so we |
| 1026 | // only legalize if the DAG tells us we must produce legal types. |
| 1027 | else if (NewNodesMustHaveLegalTypes && VT.isVector() && |
| 1028 | TLI->getTypeAction(*getContext(), EltVT) == |
| 1029 | TargetLowering::TypeExpandInteger) { |
| 1030 | APInt NewVal = Elt->getValue(); |
| 1031 | EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT); |
| 1032 | unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits(); |
| 1033 | unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits; |
| 1034 | EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts); |
| 1035 | |
| 1036 | // Check the temporary vector is the correct size. If this fails then |
| 1037 | // getTypeToTransformTo() probably returned a type whose size (in bits) |
| 1038 | // isn't a power-of-2 factor of the requested type size. |
| 1039 | assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits()); |
| 1040 | |
| 1041 | SmallVector<SDValue, 2> EltParts; |
| 1042 | for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) { |
| 1043 | EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits) |
| 1044 | .trunc(ViaEltSizeInBits), |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 1045 | ViaEltVT, isT, isO)); |
Daniel Sanders | ea28aaf | 2013-11-15 12:56:49 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | // EltParts is currently in little endian order. If we actually want |
| 1049 | // big-endian order then reverse it now. |
| 1050 | if (TLI->isBigEndian()) |
| 1051 | std::reverse(EltParts.begin(), EltParts.end()); |
| 1052 | |
| 1053 | // The elements must be reversed when the element order is different |
| 1054 | // to the endianness of the elements (because the BITCAST is itself a |
| 1055 | // vector shuffle in this situation). However, we do not need any code to |
| 1056 | // perform this reversal because getConstant() is producing a vector |
| 1057 | // splat. |
| 1058 | // This situation occurs in MIPS MSA. |
| 1059 | |
| 1060 | SmallVector<SDValue, 8> Ops; |
| 1061 | for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) |
| 1062 | Ops.insert(Ops.end(), EltParts.begin(), EltParts.end()); |
| 1063 | |
| 1064 | SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT, |
| 1065 | getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT, |
| 1066 | &Ops[0], Ops.size())); |
| 1067 | return Result; |
| 1068 | } |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 1069 | |
| 1070 | assert(Elt->getBitWidth() == EltVT.getSizeInBits() && |
| 1071 | "APInt size does not match type size!"); |
Chris Lattner | 61b0941 | 2006-08-11 21:01:22 +0000 | [diff] [blame] | 1072 | unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant; |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 1073 | FoldingSetNodeID ID; |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 1074 | AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0); |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 1075 | ID.AddPointer(Elt); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 1076 | ID.AddBoolean(isO); |
Chris Lattner | 61b0941 | 2006-08-11 21:01:22 +0000 | [diff] [blame] | 1077 | void *IP = 0; |
Dan Gohman | 8908132 | 2007-12-12 22:21:26 +0000 | [diff] [blame] | 1078 | SDNode *N = NULL; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1079 | if ((N = CSEMap.FindNodeOrInsertPos(ID, IP))) |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1080 | if (!VT.isVector()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1081 | return SDValue(N, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1082 | |
Dan Gohman | 8908132 | 2007-12-12 22:21:26 +0000 | [diff] [blame] | 1083 | if (!N) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 1084 | N = new (NodeAllocator) ConstantSDNode(isT, isO, Elt, EltVT); |
Dan Gohman | 8908132 | 2007-12-12 22:21:26 +0000 | [diff] [blame] | 1085 | CSEMap.InsertNode(N, IP); |
| 1086 | AllNodes.push_back(N); |
| 1087 | } |
| 1088 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1089 | SDValue Result(N, 0); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1090 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1091 | SmallVector<SDValue, 8> Ops; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1092 | Ops.assign(VT.getVectorNumElements(), Result); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1093 | Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size()); |
Dan Gohman | 8908132 | 2007-12-12 22:21:26 +0000 | [diff] [blame] | 1094 | } |
| 1095 | return Result; |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1098 | SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1099 | return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1103 | SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1104 | return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget); |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1107 | SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){ |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1108 | assert(VT.isFloatingPoint() && "Cannot create integer FP constant!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1109 | |
Chris Lattner | 5cf0b6e | 2010-02-24 22:44:06 +0000 | [diff] [blame] | 1110 | EVT EltVT = VT.getScalarType(); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | d865861 | 2005-02-17 20:17:32 +0000 | [diff] [blame] | 1112 | // Do the map lookup using the actual bit pattern for the floating point |
| 1113 | // value, so that we don't have problems with 0.0 comparing equal to -0.0, and |
| 1114 | // we don't have issues with SNANs. |
Chris Lattner | c9f8f41 | 2006-08-11 21:55:30 +0000 | [diff] [blame] | 1115 | unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP; |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 1116 | FoldingSetNodeID ID; |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 1117 | AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0); |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 1118 | ID.AddPointer(&V); |
Chris Lattner | c9f8f41 | 2006-08-11 21:55:30 +0000 | [diff] [blame] | 1119 | void *IP = 0; |
Evan Cheng | c908dcd | 2007-06-29 21:36:04 +0000 | [diff] [blame] | 1120 | SDNode *N = NULL; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1121 | if ((N = CSEMap.FindNodeOrInsertPos(ID, IP))) |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1122 | if (!VT.isVector()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1123 | return SDValue(N, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1124 | |
Evan Cheng | c908dcd | 2007-06-29 21:36:04 +0000 | [diff] [blame] | 1125 | if (!N) { |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1126 | N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT); |
Evan Cheng | c908dcd | 2007-06-29 21:36:04 +0000 | [diff] [blame] | 1127 | CSEMap.InsertNode(N, IP); |
| 1128 | AllNodes.push_back(N); |
| 1129 | } |
| 1130 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1131 | SDValue Result(N, 0); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1132 | if (VT.isVector()) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1133 | SmallVector<SDValue, 8> Ops; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1134 | Ops.assign(VT.getVectorNumElements(), Result); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1135 | // FIXME SDLoc info might be appropriate here |
| 1136 | Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size()); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1137 | } |
| 1138 | return Result; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1141 | SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) { |
Chris Lattner | 5cf0b6e | 2010-02-24 22:44:06 +0000 | [diff] [blame] | 1142 | EVT EltVT = VT.getScalarType(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1143 | if (EltVT==MVT::f32) |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 1144 | return getConstantFP(APFloat((float)Val), VT, isTarget); |
Dale Johannesen | 0a406ae | 2010-05-07 21:35:53 +0000 | [diff] [blame] | 1145 | else if (EltVT==MVT::f64) |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 1146 | return getConstantFP(APFloat(Val), VT, isTarget); |
Hal Finkel | 6eb7a42 | 2012-12-30 19:03:32 +0000 | [diff] [blame] | 1147 | else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 || |
| 1148 | EltVT==MVT::f16) { |
Dale Johannesen | 0a406ae | 2010-05-07 21:35:53 +0000 | [diff] [blame] | 1149 | bool ignored; |
| 1150 | APFloat apf = APFloat(Val); |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 1151 | apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven, |
Dale Johannesen | 0a406ae | 2010-05-07 21:35:53 +0000 | [diff] [blame] | 1152 | &ignored); |
| 1153 | return getConstantFP(apf, VT, isTarget); |
Craig Topper | 5e25ee8 | 2012-02-05 08:31:47 +0000 | [diff] [blame] | 1154 | } else |
| 1155 | llvm_unreachable("Unsupported type in getConstantFP"); |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1158 | SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1159 | EVT VT, int64_t Offset, |
Chris Lattner | 2a4ed82 | 2009-06-25 21:21:14 +0000 | [diff] [blame] | 1160 | bool isTargetGA, |
| 1161 | unsigned char TargetFlags) { |
| 1162 | assert((TargetFlags == 0 || isTargetGA) && |
| 1163 | "Cannot set target flags on target-independent globals"); |
Matt Arsenault | 94437c9 | 2013-11-17 00:06:39 +0000 | [diff] [blame] | 1164 | const TargetLowering *TLI = TM.getTargetLowering(); |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 1165 | |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1166 | // Truncate (with sign-extension) the offset value to the pointer size. |
Matt Arsenault | 94437c9 | 2013-11-17 00:06:39 +0000 | [diff] [blame] | 1167 | unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType()); |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1168 | if (BitWidth < 64) |
Richard Smith | 1144af3 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 1169 | Offset = SignExtend64(Offset, BitWidth); |
Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 1170 | |
Anton Korobeynikov | 4d86e2a | 2008-03-11 22:38:53 +0000 | [diff] [blame] | 1171 | const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); |
| 1172 | if (!GVar) { |
Anton Korobeynikov | c73ede0 | 2008-03-22 07:53:40 +0000 | [diff] [blame] | 1173 | // If GV is an alias then use the aliasee for determining thread-localness. |
Anton Korobeynikov | 4d86e2a | 2008-03-11 22:38:53 +0000 | [diff] [blame] | 1174 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 1175 | GVar = dyn_cast_or_null<GlobalVariable>(GA->getAliasedGlobal()); |
Anton Korobeynikov | 4d86e2a | 2008-03-11 22:38:53 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Chris Lattner | 2a4ed82 | 2009-06-25 21:21:14 +0000 | [diff] [blame] | 1178 | unsigned Opc; |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 1179 | if (GVar && GVar->isThreadLocal()) |
| 1180 | Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress; |
| 1181 | else |
| 1182 | Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress; |
Anton Korobeynikov | 4d86e2a | 2008-03-11 22:38:53 +0000 | [diff] [blame] | 1183 | |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 1184 | FoldingSetNodeID ID; |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 1185 | AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); |
Chris Lattner | 61b0941 | 2006-08-11 21:01:22 +0000 | [diff] [blame] | 1186 | ID.AddPointer(GV); |
| 1187 | ID.AddInteger(Offset); |
Chris Lattner | 2a4ed82 | 2009-06-25 21:21:14 +0000 | [diff] [blame] | 1188 | ID.AddInteger(TargetFlags); |
Pete Cooper | 32ecfb4 | 2012-07-30 20:23:19 +0000 | [diff] [blame] | 1189 | ID.AddInteger(GV->getType()->getAddressSpace()); |
Chris Lattner | 61b0941 | 2006-08-11 21:01:22 +0000 | [diff] [blame] | 1190 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1191 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 74692c0 | 2009-01-25 16:21:38 +0000 | [diff] [blame] | 1192 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1193 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1194 | SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(), |
| 1195 | DL.getDebugLoc(), GV, VT, |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1196 | Offset, TargetFlags); |
Chris Lattner | 61b0941 | 2006-08-11 21:01:22 +0000 | [diff] [blame] | 1197 | CSEMap.InsertNode(N, IP); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1198 | AllNodes.push_back(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1199 | return SDValue(N, 0); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1202 | SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) { |
Chris Lattner | c9f8f41 | 2006-08-11 21:55:30 +0000 | [diff] [blame] | 1203 | unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex; |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 1204 | FoldingSetNodeID ID; |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 1205 | AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); |
Chris Lattner | c9f8f41 | 2006-08-11 21:55:30 +0000 | [diff] [blame] | 1206 | ID.AddInteger(FI); |
| 1207 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1208 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1209 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1210 | |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1211 | SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget); |
Chris Lattner | c9f8f41 | 2006-08-11 21:55:30 +0000 | [diff] [blame] | 1212 | CSEMap.InsertNode(N, IP); |
Chris Lattner | afb2dd4 | 2005-08-25 00:43:01 +0000 | [diff] [blame] | 1213 | AllNodes.push_back(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1214 | return SDValue(N, 0); |
Chris Lattner | afb2dd4 | 2005-08-25 00:43:01 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1217 | SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget, |
Chris Lattner | f5a5546 | 2009-06-25 21:35:31 +0000 | [diff] [blame] | 1218 | unsigned char TargetFlags) { |
| 1219 | assert((TargetFlags == 0 || isTarget) && |
| 1220 | "Cannot set target flags on target-independent jump tables"); |
Chris Lattner | c9f8f41 | 2006-08-11 21:55:30 +0000 | [diff] [blame] | 1221 | unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable; |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 1222 | FoldingSetNodeID ID; |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 1223 | AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); |
Chris Lattner | c9f8f41 | 2006-08-11 21:55:30 +0000 | [diff] [blame] | 1224 | ID.AddInteger(JTI); |
Chris Lattner | f5a5546 | 2009-06-25 21:35:31 +0000 | [diff] [blame] | 1225 | ID.AddInteger(TargetFlags); |
Chris Lattner | c9f8f41 | 2006-08-11 21:55:30 +0000 | [diff] [blame] | 1226 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1227 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1228 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1229 | |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1230 | SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget, |
| 1231 | TargetFlags); |
Chris Lattner | c9f8f41 | 2006-08-11 21:55:30 +0000 | [diff] [blame] | 1232 | CSEMap.InsertNode(N, IP); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1233 | AllNodes.push_back(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1234 | return SDValue(N, 0); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1237 | SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1238 | unsigned Alignment, int Offset, |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 1239 | bool isTarget, |
Chris Lattner | f5a5546 | 2009-06-25 21:35:31 +0000 | [diff] [blame] | 1240 | unsigned char TargetFlags) { |
| 1241 | assert((TargetFlags == 0 || isTarget) && |
| 1242 | "Cannot set target flags on target-independent globals"); |
Dan Gohman | 50284d8 | 2008-09-16 22:05:41 +0000 | [diff] [blame] | 1243 | if (Alignment == 0) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1244 | Alignment = |
| 1245 | TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType()); |
Chris Lattner | c9f8f41 | 2006-08-11 21:55:30 +0000 | [diff] [blame] | 1246 | unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 1247 | FoldingSetNodeID ID; |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 1248 | AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); |
Chris Lattner | c9f8f41 | 2006-08-11 21:55:30 +0000 | [diff] [blame] | 1249 | ID.AddInteger(Alignment); |
| 1250 | ID.AddInteger(Offset); |
Chris Lattner | 130fc13 | 2006-08-14 20:12:44 +0000 | [diff] [blame] | 1251 | ID.AddPointer(C); |
Chris Lattner | f5a5546 | 2009-06-25 21:35:31 +0000 | [diff] [blame] | 1252 | ID.AddInteger(TargetFlags); |
Chris Lattner | c9f8f41 | 2006-08-11 21:55:30 +0000 | [diff] [blame] | 1253 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1254 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1255 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1256 | |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1257 | SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset, |
| 1258 | Alignment, TargetFlags); |
Chris Lattner | c9f8f41 | 2006-08-11 21:55:30 +0000 | [diff] [blame] | 1259 | CSEMap.InsertNode(N, IP); |
Chris Lattner | 4025a9c | 2005-08-25 05:03:06 +0000 | [diff] [blame] | 1260 | AllNodes.push_back(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1261 | return SDValue(N, 0); |
Chris Lattner | 4025a9c | 2005-08-25 05:03:06 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1264 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1265 | SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1266 | unsigned Alignment, int Offset, |
Chris Lattner | f5a5546 | 2009-06-25 21:35:31 +0000 | [diff] [blame] | 1267 | bool isTarget, |
| 1268 | unsigned char TargetFlags) { |
| 1269 | assert((TargetFlags == 0 || isTarget) && |
| 1270 | "Cannot set target flags on target-independent globals"); |
Dan Gohman | 50284d8 | 2008-09-16 22:05:41 +0000 | [diff] [blame] | 1271 | if (Alignment == 0) |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1272 | Alignment = |
| 1273 | TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType()); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1274 | unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool; |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 1275 | FoldingSetNodeID ID; |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 1276 | AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1277 | ID.AddInteger(Alignment); |
| 1278 | ID.AddInteger(Offset); |
Jim Grosbach | 5405d58 | 2011-09-27 20:59:33 +0000 | [diff] [blame] | 1279 | C->addSelectionDAGCSEId(ID); |
Chris Lattner | f5a5546 | 2009-06-25 21:35:31 +0000 | [diff] [blame] | 1280 | ID.AddInteger(TargetFlags); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1281 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1282 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1283 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1284 | |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1285 | SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset, |
| 1286 | Alignment, TargetFlags); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1287 | CSEMap.InsertNode(N, IP); |
| 1288 | AllNodes.push_back(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1289 | return SDValue(N, 0); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Jakob Stoklund Olesen | 74500bd | 2012-08-07 22:37:05 +0000 | [diff] [blame] | 1292 | SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset, |
| 1293 | unsigned char TargetFlags) { |
| 1294 | FoldingSetNodeID ID; |
| 1295 | AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), 0, 0); |
| 1296 | ID.AddInteger(Index); |
| 1297 | ID.AddInteger(Offset); |
| 1298 | ID.AddInteger(TargetFlags); |
| 1299 | void *IP = 0; |
| 1300 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
| 1301 | return SDValue(E, 0); |
| 1302 | |
| 1303 | SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset, |
| 1304 | TargetFlags); |
| 1305 | CSEMap.InsertNode(N, IP); |
| 1306 | AllNodes.push_back(N); |
| 1307 | return SDValue(N, 0); |
| 1308 | } |
| 1309 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1310 | SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) { |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 1311 | FoldingSetNodeID ID; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1312 | AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0); |
Chris Lattner | 61b0941 | 2006-08-11 21:01:22 +0000 | [diff] [blame] | 1313 | ID.AddPointer(MBB); |
| 1314 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1315 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1316 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1317 | |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1318 | SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB); |
Chris Lattner | 61b0941 | 2006-08-11 21:01:22 +0000 | [diff] [blame] | 1319 | CSEMap.InsertNode(N, IP); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1320 | AllNodes.push_back(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1321 | return SDValue(N, 0); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1324 | SDValue SelectionDAG::getValueType(EVT VT) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1325 | if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >= |
| 1326 | ValueTypeNodes.size()) |
| 1327 | ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1328 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1329 | SDNode *&N = VT.isExtended() ? |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1330 | ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy]; |
Duncan Sands | f411b83 | 2007-10-17 13:49:58 +0000 | [diff] [blame] | 1331 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1332 | if (N) return SDValue(N, 0); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1333 | N = new (NodeAllocator) VTSDNode(VT); |
Duncan Sands | f411b83 | 2007-10-17 13:49:58 +0000 | [diff] [blame] | 1334 | AllNodes.push_back(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1335 | return SDValue(N, 0); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1338 | SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) { |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 1339 | SDNode *&N = ExternalSymbols[Sym]; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1340 | if (N) return SDValue(N, 0); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1341 | N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT); |
Andrew Lenharth | 2a2de66 | 2005-10-23 03:40:17 +0000 | [diff] [blame] | 1342 | AllNodes.push_back(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1343 | return SDValue(N, 0); |
Andrew Lenharth | 2a2de66 | 2005-10-23 03:40:17 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1346 | SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT, |
Chris Lattner | 1af2231 | 2009-06-25 18:45:50 +0000 | [diff] [blame] | 1347 | unsigned char TargetFlags) { |
| 1348 | SDNode *&N = |
| 1349 | TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym, |
| 1350 | TargetFlags)]; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1351 | if (N) return SDValue(N, 0); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1352 | N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1353 | AllNodes.push_back(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1354 | return SDValue(N, 0); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1357 | SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) { |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 1358 | if ((unsigned)Cond >= CondCodeNodes.size()) |
| 1359 | CondCodeNodes.resize(Cond+1); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1360 | |
Chris Lattner | 079a27a | 2005-08-09 20:40:02 +0000 | [diff] [blame] | 1361 | if (CondCodeNodes[Cond] == 0) { |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1362 | CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond); |
Dan Gohman | 0e5f130 | 2008-07-07 23:02:41 +0000 | [diff] [blame] | 1363 | CondCodeNodes[Cond] = N; |
| 1364 | AllNodes.push_back(N); |
Chris Lattner | 079a27a | 2005-08-09 20:40:02 +0000 | [diff] [blame] | 1365 | } |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1366 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1367 | return SDValue(CondCodeNodes[Cond], 0); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 1370 | // commuteShuffle - swaps the values of N1 and N2, and swaps all indices in |
| 1371 | // the shuffle mask M that point at N1 to point at N2, and indices that point |
| 1372 | // N2 to point at N1. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1373 | static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) { |
| 1374 | std::swap(N1, N2); |
| 1375 | int NElts = M.size(); |
| 1376 | for (int i = 0; i != NElts; ++i) { |
| 1377 | if (M[i] >= NElts) |
| 1378 | M[i] -= NElts; |
| 1379 | else if (M[i] >= 0) |
| 1380 | M[i] += NElts; |
| 1381 | } |
| 1382 | } |
| 1383 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1384 | SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1385 | SDValue N2, const int *Mask) { |
Craig Topper | ad445a6 | 2013-08-09 04:37:24 +0000 | [diff] [blame] | 1386 | assert(VT == N1.getValueType() && VT == N2.getValueType() && |
| 1387 | "Invalid VECTOR_SHUFFLE"); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1388 | |
| 1389 | // Canonicalize shuffle undef, undef -> undef |
| 1390 | if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF) |
Dan Gohman | 2e4284d | 2009-07-09 00:46:33 +0000 | [diff] [blame] | 1391 | return getUNDEF(VT); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1392 | |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 1393 | // Validate that all indices in Mask are within the range of the elements |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1394 | // input to the shuffle. |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 1395 | unsigned NElts = VT.getVectorNumElements(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1396 | SmallVector<int, 8> MaskVec; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 1397 | for (unsigned i = 0; i != NElts; ++i) { |
| 1398 | assert(Mask[i] < (int)(NElts * 2) && "Index out of range"); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1399 | MaskVec.push_back(Mask[i]); |
| 1400 | } |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 1401 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1402 | // Canonicalize shuffle v, v -> v, undef |
| 1403 | if (N1 == N2) { |
| 1404 | N2 = getUNDEF(VT); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 1405 | for (unsigned i = 0; i != NElts; ++i) |
| 1406 | if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1407 | } |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 1408 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1409 | // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask. |
| 1410 | if (N1.getOpcode() == ISD::UNDEF) |
| 1411 | commuteShuffle(N1, N2, MaskVec); |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 1412 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1413 | // Canonicalize all index into lhs, -> shuffle lhs, undef |
| 1414 | // Canonicalize all index into rhs, -> shuffle rhs, undef |
| 1415 | bool AllLHS = true, AllRHS = true; |
| 1416 | bool N2Undef = N2.getOpcode() == ISD::UNDEF; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 1417 | for (unsigned i = 0; i != NElts; ++i) { |
| 1418 | if (MaskVec[i] >= (int)NElts) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1419 | if (N2Undef) |
| 1420 | MaskVec[i] = -1; |
| 1421 | else |
| 1422 | AllLHS = false; |
| 1423 | } else if (MaskVec[i] >= 0) { |
| 1424 | AllRHS = false; |
| 1425 | } |
| 1426 | } |
| 1427 | if (AllLHS && AllRHS) |
| 1428 | return getUNDEF(VT); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 1429 | if (AllLHS && !N2Undef) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1430 | N2 = getUNDEF(VT); |
| 1431 | if (AllRHS) { |
| 1432 | N1 = getUNDEF(VT); |
| 1433 | commuteShuffle(N1, N2, MaskVec); |
| 1434 | } |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 1435 | |
Craig Topper | eee2a11 | 2013-08-08 08:03:12 +0000 | [diff] [blame] | 1436 | // If Identity shuffle return that node. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1437 | bool Identity = true; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 1438 | for (unsigned i = 0; i != NElts; ++i) { |
| 1439 | if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1440 | } |
Craig Topper | ad445a6 | 2013-08-09 04:37:24 +0000 | [diff] [blame] | 1441 | if (Identity && NElts) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1442 | return N1; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1443 | |
| 1444 | FoldingSetNodeID ID; |
| 1445 | SDValue Ops[2] = { N1, N2 }; |
| 1446 | AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2); |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 1447 | for (unsigned i = 0; i != NElts; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1448 | ID.AddInteger(MaskVec[i]); |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 1449 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1450 | void* IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1451 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1452 | return SDValue(E, 0); |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 1453 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1454 | // Allocate the mask array for the node out of the BumpPtrAllocator, since |
| 1455 | // SDNode doesn't have access to it. This memory will be "leaked" when |
| 1456 | // the node is deallocated, but recovered when the NodeAllocator is released. |
| 1457 | int *MaskAlloc = OperandAllocator.Allocate<int>(NElts); |
| 1458 | memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int)); |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 1459 | |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1460 | ShuffleVectorSDNode *N = |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 1461 | new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(), |
| 1462 | dl.getDebugLoc(), N1, N2, |
| 1463 | MaskAlloc); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 1464 | CSEMap.InsertNode(N, IP); |
| 1465 | AllNodes.push_back(N); |
| 1466 | return SDValue(N, 0); |
| 1467 | } |
| 1468 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1469 | SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl, |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1470 | SDValue Val, SDValue DTy, |
| 1471 | SDValue STy, SDValue Rnd, SDValue Sat, |
| 1472 | ISD::CvtCode Code) { |
Mon P Wang | b0e341b | 2009-02-05 04:47:42 +0000 | [diff] [blame] | 1473 | // If the src and dest types are the same and the conversion is between |
| 1474 | // integer types of the same sign or two floats, no conversion is necessary. |
| 1475 | if (DTy == STy && |
| 1476 | (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF)) |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1477 | return Val; |
| 1478 | |
| 1479 | FoldingSetNodeID ID; |
Mon P Wang | bdea348 | 2009-11-07 04:46:25 +0000 | [diff] [blame] | 1480 | SDValue Ops[] = { Val, DTy, STy, Rnd, Sat }; |
| 1481 | AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1482 | void* IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1483 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1484 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1485 | |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 1486 | CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(), |
| 1487 | dl.getDebugLoc(), |
| 1488 | Ops, 5, Code); |
Dale Johannesen | a04b757 | 2009-02-03 23:04:43 +0000 | [diff] [blame] | 1489 | CSEMap.InsertNode(N, IP); |
| 1490 | AllNodes.push_back(N); |
| 1491 | return SDValue(N, 0); |
| 1492 | } |
| 1493 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1494 | SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) { |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 1495 | FoldingSetNodeID ID; |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 1496 | AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0); |
Chris Lattner | 61b0941 | 2006-08-11 21:01:22 +0000 | [diff] [blame] | 1497 | ID.AddInteger(RegNo); |
| 1498 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1499 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1500 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1501 | |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1502 | SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT); |
Chris Lattner | 61b0941 | 2006-08-11 21:01:22 +0000 | [diff] [blame] | 1503 | CSEMap.InsertNode(N, IP); |
| 1504 | AllNodes.push_back(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1505 | return SDValue(N, 0); |
Chris Lattner | 61b0941 | 2006-08-11 21:01:22 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Jakob Stoklund Olesen | 9cf37e8 | 2012-01-18 23:52:12 +0000 | [diff] [blame] | 1508 | SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) { |
| 1509 | FoldingSetNodeID ID; |
| 1510 | AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), 0, 0); |
| 1511 | ID.AddPointer(RegMask); |
| 1512 | void *IP = 0; |
| 1513 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
| 1514 | return SDValue(E, 0); |
| 1515 | |
| 1516 | SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask); |
| 1517 | CSEMap.InsertNode(N, IP); |
| 1518 | AllNodes.push_back(N); |
| 1519 | return SDValue(N, 0); |
| 1520 | } |
| 1521 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1522 | SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) { |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 1523 | FoldingSetNodeID ID; |
| 1524 | SDValue Ops[] = { Root }; |
Chris Lattner | 7561d48 | 2010-03-14 02:33:54 +0000 | [diff] [blame] | 1525 | AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1); |
| 1526 | ID.AddPointer(Label); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 1527 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1528 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 1529 | return SDValue(E, 0); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1530 | |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 1531 | SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(), |
| 1532 | dl.getDebugLoc(), Root, Label); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 1533 | CSEMap.InsertNode(N, IP); |
| 1534 | AllNodes.push_back(N); |
| 1535 | return SDValue(N, 0); |
| 1536 | } |
| 1537 | |
Chris Lattner | 7561d48 | 2010-03-14 02:33:54 +0000 | [diff] [blame] | 1538 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1539 | SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT, |
Michael Liao | 6c7ccaa | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 1540 | int64_t Offset, |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 1541 | bool isTarget, |
| 1542 | unsigned char TargetFlags) { |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1543 | unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress; |
| 1544 | |
| 1545 | FoldingSetNodeID ID; |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 1546 | AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1547 | ID.AddPointer(BA); |
Michael Liao | 6c7ccaa | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 1548 | ID.AddInteger(Offset); |
Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 1549 | ID.AddInteger(TargetFlags); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1550 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1551 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1552 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1553 | |
Michael Liao | 6c7ccaa | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 1554 | SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset, |
| 1555 | TargetFlags); |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 1556 | CSEMap.InsertNode(N, IP); |
| 1557 | AllNodes.push_back(N); |
| 1558 | return SDValue(N, 0); |
| 1559 | } |
| 1560 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1561 | SDValue SelectionDAG::getSrcValue(const Value *V) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1562 | assert((!V || V->getType()->isPointerTy()) && |
Chris Lattner | 61b0941 | 2006-08-11 21:01:22 +0000 | [diff] [blame] | 1563 | "SrcValue is not a pointer?"); |
| 1564 | |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 1565 | FoldingSetNodeID ID; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1566 | AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0); |
Chris Lattner | 61b0941 | 2006-08-11 21:01:22 +0000 | [diff] [blame] | 1567 | ID.AddPointer(V); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1568 | |
Chris Lattner | 61b0941 | 2006-08-11 21:01:22 +0000 | [diff] [blame] | 1569 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 1570 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1571 | return SDValue(E, 0); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1572 | |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1573 | SDNode *N = new (NodeAllocator) SrcValueSDNode(V); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1574 | CSEMap.InsertNode(N, IP); |
| 1575 | AllNodes.push_back(N); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1576 | return SDValue(N, 0); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 1579 | /// getMDNode - Return an MDNodeSDNode which holds an MDNode. |
| 1580 | SDValue SelectionDAG::getMDNode(const MDNode *MD) { |
| 1581 | FoldingSetNodeID ID; |
| 1582 | AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0); |
| 1583 | ID.AddPointer(MD); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1584 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 1585 | void *IP = 0; |
| 1586 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
| 1587 | return SDValue(E, 0); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1588 | |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 1589 | SDNode *N = new (NodeAllocator) MDNodeSDNode(MD); |
| 1590 | CSEMap.InsertNode(N, IP); |
| 1591 | AllNodes.push_back(N); |
| 1592 | return SDValue(N, 0); |
| 1593 | } |
| 1594 | |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 1595 | /// getAddrSpaceCast - Return an AddrSpaceCastSDNode. |
| 1596 | SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr, |
| 1597 | unsigned SrcAS, unsigned DestAS) { |
| 1598 | SDValue Ops[] = {Ptr}; |
| 1599 | FoldingSetNodeID ID; |
| 1600 | AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), &Ops[0], 1); |
| 1601 | ID.AddInteger(SrcAS); |
| 1602 | ID.AddInteger(DestAS); |
| 1603 | |
| 1604 | void *IP = 0; |
| 1605 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
| 1606 | return SDValue(E, 0); |
| 1607 | |
| 1608 | SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(), |
| 1609 | dl.getDebugLoc(), |
| 1610 | VT, Ptr, SrcAS, DestAS); |
| 1611 | CSEMap.InsertNode(N, IP); |
| 1612 | AllNodes.push_back(N); |
| 1613 | return SDValue(N, 0); |
| 1614 | } |
Chris Lattner | decc267 | 2010-04-07 05:20:54 +0000 | [diff] [blame] | 1615 | |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1616 | /// getShiftAmountOperand - Return the specified value casted to |
| 1617 | /// the target's desired shift amount type. |
Owen Anderson | 6154f6c | 2011-03-07 18:29:47 +0000 | [diff] [blame] | 1618 | SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1619 | EVT OpTy = Op.getValueType(); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1620 | EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy); |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1621 | if (OpTy == ShTy || OpTy.isVector()) return Op; |
| 1622 | |
| 1623 | ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1624 | return getNode(Opcode, SDLoc(Op), ShTy, Op); |
Duncan Sands | 92abc62 | 2009-01-31 15:50:11 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Chris Lattner | 37ce9df | 2007-10-15 17:47:20 +0000 | [diff] [blame] | 1627 | /// CreateStackTemporary - Create a stack temporary, suitable for holding the |
| 1628 | /// specified value type. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1629 | SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) { |
Chris Lattner | 37ce9df | 2007-10-15 17:47:20 +0000 | [diff] [blame] | 1630 | MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo(); |
Dan Gohman | 4e918b2 | 2009-09-23 21:07:02 +0000 | [diff] [blame] | 1631 | unsigned ByteSize = VT.getStoreSize(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1632 | Type *Ty = VT.getTypeForEVT(*getContext()); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1633 | const TargetLowering *TLI = TM.getTargetLowering(); |
Mon P Wang | 364d73d | 2008-07-05 20:40:31 +0000 | [diff] [blame] | 1634 | unsigned StackAlign = |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1635 | std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1636 | |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 1637 | int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1638 | return getFrameIndex(FrameIdx, TLI->getPointerTy()); |
Chris Lattner | 37ce9df | 2007-10-15 17:47:20 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Duncan Sands | 47d9dcc | 2008-12-09 21:33:20 +0000 | [diff] [blame] | 1641 | /// CreateStackTemporary - Create a stack temporary suitable for holding |
| 1642 | /// either of the specified value types. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1643 | SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) { |
Duncan Sands | 47d9dcc | 2008-12-09 21:33:20 +0000 | [diff] [blame] | 1644 | unsigned Bytes = std::max(VT1.getStoreSizeInBits(), |
| 1645 | VT2.getStoreSizeInBits())/8; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1646 | Type *Ty1 = VT1.getTypeForEVT(*getContext()); |
| 1647 | Type *Ty2 = VT2.getTypeForEVT(*getContext()); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1648 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 1649 | const DataLayout *TD = TLI->getDataLayout(); |
Duncan Sands | 47d9dcc | 2008-12-09 21:33:20 +0000 | [diff] [blame] | 1650 | unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1), |
| 1651 | TD->getPrefTypeAlignment(Ty2)); |
| 1652 | |
| 1653 | MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 1654 | int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1655 | return getFrameIndex(FrameIdx, TLI->getPointerTy()); |
Duncan Sands | 47d9dcc | 2008-12-09 21:33:20 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1658 | SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1659 | SDValue N2, ISD::CondCode Cond, SDLoc dl) { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1660 | // These setcc operations always fold. |
| 1661 | switch (Cond) { |
| 1662 | default: break; |
| 1663 | case ISD::SETFALSE: |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 1664 | case ISD::SETFALSE2: return getConstant(0, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1665 | case ISD::SETTRUE: |
Tim Northover | a5eeb9d | 2013-09-06 12:38:12 +0000 | [diff] [blame] | 1666 | case ISD::SETTRUE2: { |
| 1667 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 1668 | TargetLowering::BooleanContent Cnt = TLI->getBooleanContents(VT.isVector()); |
| 1669 | return getConstant( |
| 1670 | Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT); |
| 1671 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1672 | |
Chris Lattner | a83385f | 2006-04-27 05:01:07 +0000 | [diff] [blame] | 1673 | case ISD::SETOEQ: |
| 1674 | case ISD::SETOGT: |
| 1675 | case ISD::SETOGE: |
| 1676 | case ISD::SETOLT: |
| 1677 | case ISD::SETOLE: |
| 1678 | case ISD::SETONE: |
| 1679 | case ISD::SETO: |
| 1680 | case ISD::SETUO: |
| 1681 | case ISD::SETUEQ: |
| 1682 | case ISD::SETUNE: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1683 | assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!"); |
Chris Lattner | a83385f | 2006-04-27 05:01:07 +0000 | [diff] [blame] | 1684 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1685 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1686 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1687 | if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) { |
Dan Gohman | 6c23150 | 2008-02-29 01:47:35 +0000 | [diff] [blame] | 1688 | const APInt &C2 = N2C->getAPIntValue(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1689 | if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) { |
Dan Gohman | 6c23150 | 2008-02-29 01:47:35 +0000 | [diff] [blame] | 1690 | const APInt &C1 = N1C->getAPIntValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1691 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1692 | switch (Cond) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1693 | default: llvm_unreachable("Unknown integer setcc!"); |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 1694 | case ISD::SETEQ: return getConstant(C1 == C2, VT); |
| 1695 | case ISD::SETNE: return getConstant(C1 != C2, VT); |
Dan Gohman | 6c23150 | 2008-02-29 01:47:35 +0000 | [diff] [blame] | 1696 | case ISD::SETULT: return getConstant(C1.ult(C2), VT); |
| 1697 | case ISD::SETUGT: return getConstant(C1.ugt(C2), VT); |
| 1698 | case ISD::SETULE: return getConstant(C1.ule(C2), VT); |
| 1699 | case ISD::SETUGE: return getConstant(C1.uge(C2), VT); |
| 1700 | case ISD::SETLT: return getConstant(C1.slt(C2), VT); |
| 1701 | case ISD::SETGT: return getConstant(C1.sgt(C2), VT); |
| 1702 | case ISD::SETLE: return getConstant(C1.sle(C2), VT); |
| 1703 | case ISD::SETGE: return getConstant(C1.sge(C2), VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1704 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1705 | } |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 1706 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1707 | if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) { |
| 1708 | if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) { |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1709 | APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF()); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1710 | switch (Cond) { |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1711 | default: break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1712 | case ISD::SETEQ: if (R==APFloat::cmpUnordered) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 1713 | return getUNDEF(VT); |
Dale Johannesen | ee84768 | 2007-08-31 17:03:33 +0000 | [diff] [blame] | 1714 | // fall through |
| 1715 | case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1716 | case ISD::SETNE: if (R==APFloat::cmpUnordered) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 1717 | return getUNDEF(VT); |
Dale Johannesen | ee84768 | 2007-08-31 17:03:33 +0000 | [diff] [blame] | 1718 | // fall through |
| 1719 | case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan || |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1720 | R==APFloat::cmpLessThan, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1721 | case ISD::SETLT: if (R==APFloat::cmpUnordered) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 1722 | return getUNDEF(VT); |
Dale Johannesen | ee84768 | 2007-08-31 17:03:33 +0000 | [diff] [blame] | 1723 | // fall through |
| 1724 | case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1725 | case ISD::SETGT: if (R==APFloat::cmpUnordered) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 1726 | return getUNDEF(VT); |
Dale Johannesen | ee84768 | 2007-08-31 17:03:33 +0000 | [diff] [blame] | 1727 | // fall through |
| 1728 | case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1729 | case ISD::SETLE: if (R==APFloat::cmpUnordered) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 1730 | return getUNDEF(VT); |
Dale Johannesen | ee84768 | 2007-08-31 17:03:33 +0000 | [diff] [blame] | 1731 | // fall through |
| 1732 | case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan || |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1733 | R==APFloat::cmpEqual, VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1734 | case ISD::SETGE: if (R==APFloat::cmpUnordered) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 1735 | return getUNDEF(VT); |
Dale Johannesen | ee84768 | 2007-08-31 17:03:33 +0000 | [diff] [blame] | 1736 | // fall through |
| 1737 | case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan || |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 1738 | R==APFloat::cmpEqual, VT); |
| 1739 | case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT); |
| 1740 | case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT); |
| 1741 | case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered || |
| 1742 | R==APFloat::cmpEqual, VT); |
| 1743 | case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT); |
| 1744 | case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered || |
| 1745 | R==APFloat::cmpLessThan, VT); |
| 1746 | case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan || |
| 1747 | R==APFloat::cmpUnordered, VT); |
| 1748 | case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT); |
| 1749 | case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1750 | } |
| 1751 | } else { |
| 1752 | // Ensure that the constant occurs on the RHS. |
Tom Stellard | 12d43f9 | 2013-09-28 02:50:38 +0000 | [diff] [blame] | 1753 | ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond); |
| 1754 | MVT CompVT = N1.getValueType().getSimpleVT(); |
| 1755 | if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT)) |
| 1756 | return SDValue(); |
| 1757 | |
| 1758 | return getSetCC(dl, VT, N2, N1, SwappedCond); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1759 | } |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 1762 | // Could not fold it. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1763 | return SDValue(); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1766 | /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We |
| 1767 | /// use this predicate to simplify operations downstream. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1768 | bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const { |
Chris Lattner | a4f7318 | 2009-07-07 23:28:46 +0000 | [diff] [blame] | 1769 | // This predicate is not safe for vector operations. |
| 1770 | if (Op.getValueType().isVector()) |
| 1771 | return false; |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 1772 | |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 1773 | unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1774 | return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth); |
| 1775 | } |
| 1776 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1777 | /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use |
| 1778 | /// this predicate to simplify operations downstream. Mask is known to be zero |
| 1779 | /// for bits that V cannot have. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1780 | bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask, |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1781 | unsigned Depth) const { |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1782 | APInt KnownZero, KnownOne; |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1783 | ComputeMaskedBits(Op, KnownZero, KnownOne, Depth); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1784 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1785 | return (KnownZero & Mask) == Mask; |
| 1786 | } |
| 1787 | |
| 1788 | /// ComputeMaskedBits - Determine which of the bits specified in Mask are |
| 1789 | /// known to be either zero or one and return them in the KnownZero/KnownOne |
| 1790 | /// bitsets. This code only analyzes bits in Mask, in order to short-circuit |
| 1791 | /// processing. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1792 | void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero, |
| 1793 | APInt &KnownOne, unsigned Depth) const { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1794 | const TargetLowering *TLI = TM.getTargetLowering(); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1795 | unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits(); |
Dan Gohman | d9fe41c | 2008-02-13 23:13:32 +0000 | [diff] [blame] | 1796 | |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 1797 | KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1798 | if (Depth == 6) |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1799 | return; // Limit search depth. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1800 | |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 1801 | APInt KnownZero2, KnownOne2; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1802 | |
| 1803 | switch (Op.getOpcode()) { |
| 1804 | case ISD::Constant: |
| 1805 | // We know all of the bits for a constant! |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1806 | KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue(); |
| 1807 | KnownZero = ~KnownOne; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1808 | return; |
| 1809 | case ISD::AND: |
| 1810 | // If either the LHS or the RHS are Zero, the result is zero. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1811 | ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1); |
| 1812 | ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1813 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1814 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1815 | |
| 1816 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 1817 | KnownOne &= KnownOne2; |
| 1818 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 1819 | KnownZero |= KnownZero2; |
| 1820 | return; |
| 1821 | case ISD::OR: |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1822 | ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1); |
| 1823 | ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1824 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1825 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 1826 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1827 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 1828 | KnownZero &= KnownZero2; |
| 1829 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 1830 | KnownOne |= KnownOne2; |
| 1831 | return; |
| 1832 | case ISD::XOR: { |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1833 | ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1); |
| 1834 | ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1835 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1836 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 1837 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1838 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 1839 | APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1840 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 1841 | KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2); |
| 1842 | KnownZero = KnownZeroOut; |
| 1843 | return; |
| 1844 | } |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1845 | case ISD::MUL: { |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1846 | ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1); |
| 1847 | ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1848 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1849 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 1850 | |
| 1851 | // If low bits are zero in either operand, output low known-0 bits. |
| 1852 | // Also compute a conserative estimate for high known-0 bits. |
| 1853 | // More trickiness is possible, but this is sufficient for the |
| 1854 | // interesting case of alignment computation. |
Jay Foad | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 1855 | KnownOne.clearAllBits(); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1856 | unsigned TrailZ = KnownZero.countTrailingOnes() + |
| 1857 | KnownZero2.countTrailingOnes(); |
| 1858 | unsigned LeadZ = std::max(KnownZero.countLeadingOnes() + |
Dan Gohman | 42ac929 | 2008-05-07 00:35:55 +0000 | [diff] [blame] | 1859 | KnownZero2.countLeadingOnes(), |
| 1860 | BitWidth) - BitWidth; |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1861 | |
| 1862 | TrailZ = std::min(TrailZ, BitWidth); |
| 1863 | LeadZ = std::min(LeadZ, BitWidth); |
| 1864 | KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) | |
| 1865 | APInt::getHighBitsSet(BitWidth, LeadZ); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1866 | return; |
| 1867 | } |
| 1868 | case ISD::UDIV: { |
| 1869 | // For the purposes of computing leading zeros we can conservatively |
| 1870 | // treat a udiv as a logical right shift by the power of 2 known to |
Dan Gohman | 1d9cd50 | 2008-05-02 21:30:02 +0000 | [diff] [blame] | 1871 | // be less than the denominator. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1872 | ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1873 | unsigned LeadZ = KnownZero2.countLeadingOnes(); |
| 1874 | |
Jay Foad | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 1875 | KnownOne2.clearAllBits(); |
| 1876 | KnownZero2.clearAllBits(); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1877 | ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1); |
Dan Gohman | 1d9cd50 | 2008-05-02 21:30:02 +0000 | [diff] [blame] | 1878 | unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros(); |
| 1879 | if (RHSUnknownLeadingOnes != BitWidth) |
| 1880 | LeadZ = std::min(BitWidth, |
| 1881 | LeadZ + BitWidth - RHSUnknownLeadingOnes - 1); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1882 | |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1883 | KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1884 | return; |
| 1885 | } |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1886 | case ISD::SELECT: |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1887 | ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1); |
| 1888 | ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1889 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1890 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 1891 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1892 | // Only known if known in both the LHS and RHS. |
| 1893 | KnownOne &= KnownOne2; |
| 1894 | KnownZero &= KnownZero2; |
| 1895 | return; |
| 1896 | case ISD::SELECT_CC: |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1897 | ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1); |
| 1898 | ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1899 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 1900 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
| 1901 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1902 | // Only known if known in both the LHS and RHS. |
| 1903 | KnownOne &= KnownOne2; |
| 1904 | KnownZero &= KnownZero2; |
| 1905 | return; |
Bill Wendling | 253174b | 2008-11-22 07:24:01 +0000 | [diff] [blame] | 1906 | case ISD::SADDO: |
| 1907 | case ISD::UADDO: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 1908 | case ISD::SSUBO: |
| 1909 | case ISD::USUBO: |
| 1910 | case ISD::SMULO: |
| 1911 | case ISD::UMULO: |
Bill Wendling | 253174b | 2008-11-22 07:24:01 +0000 | [diff] [blame] | 1912 | if (Op.getResNo() != 1) |
| 1913 | return; |
Duncan Sands | 0322808 | 2008-11-23 15:47:28 +0000 | [diff] [blame] | 1914 | // The boolean result conforms to getBooleanContents. Fall through. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1915 | case ISD::SETCC: |
| 1916 | // If we know the result of a setcc has the top bits zero, use this info. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 1917 | if (TLI->getBooleanContents(Op.getValueType().isVector()) == |
Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 1918 | TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1) |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 1919 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1920 | return; |
| 1921 | case ISD::SHL: |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1922 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1923 | if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1924 | unsigned ShAmt = SA->getZExtValue(); |
Dan Gohman | d4cf992 | 2008-02-26 18:50:50 +0000 | [diff] [blame] | 1925 | |
| 1926 | // If the shift count is an invalid immediate, don't do anything. |
| 1927 | if (ShAmt >= BitWidth) |
| 1928 | return; |
| 1929 | |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1930 | ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1931 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Dan Gohman | d4cf992 | 2008-02-26 18:50:50 +0000 | [diff] [blame] | 1932 | KnownZero <<= ShAmt; |
| 1933 | KnownOne <<= ShAmt; |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 1934 | // low bits known zero. |
Dan Gohman | d4cf992 | 2008-02-26 18:50:50 +0000 | [diff] [blame] | 1935 | KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1936 | } |
| 1937 | return; |
| 1938 | case ISD::SRL: |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1939 | // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1940 | if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1941 | unsigned ShAmt = SA->getZExtValue(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1942 | |
Dan Gohman | d4cf992 | 2008-02-26 18:50:50 +0000 | [diff] [blame] | 1943 | // If the shift count is an invalid immediate, don't do anything. |
| 1944 | if (ShAmt >= BitWidth) |
| 1945 | return; |
| 1946 | |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1947 | ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1948 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 1949 | KnownZero = KnownZero.lshr(ShAmt); |
| 1950 | KnownOne = KnownOne.lshr(ShAmt); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1951 | |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1952 | APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1953 | KnownZero |= HighBits; // High bits known zero. |
| 1954 | } |
| 1955 | return; |
| 1956 | case ISD::SRA: |
| 1957 | if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1958 | unsigned ShAmt = SA->getZExtValue(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1959 | |
Dan Gohman | d4cf992 | 2008-02-26 18:50:50 +0000 | [diff] [blame] | 1960 | // If the shift count is an invalid immediate, don't do anything. |
| 1961 | if (ShAmt >= BitWidth) |
| 1962 | return; |
| 1963 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1964 | // If any of the demanded bits are produced by the sign extension, we also |
| 1965 | // demand the input sign bit. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1966 | APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1967 | |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1968 | ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1969 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 1970 | KnownZero = KnownZero.lshr(ShAmt); |
| 1971 | KnownOne = KnownOne.lshr(ShAmt); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1972 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1973 | // Handle the sign bits. |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 1974 | APInt SignBit = APInt::getSignBit(BitWidth); |
| 1975 | SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1976 | |
Dan Gohman | ca93a43 | 2008-02-20 16:30:17 +0000 | [diff] [blame] | 1977 | if (KnownZero.intersects(SignBit)) { |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1978 | KnownZero |= HighBits; // New bits are known zero. |
Dan Gohman | ca93a43 | 2008-02-20 16:30:17 +0000 | [diff] [blame] | 1979 | } else if (KnownOne.intersects(SignBit)) { |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1980 | KnownOne |= HighBits; // New bits are known one. |
| 1981 | } |
| 1982 | } |
| 1983 | return; |
| 1984 | case ISD::SIGN_EXTEND_INREG: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1985 | EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 1986 | unsigned EBits = EVT.getScalarType().getSizeInBits(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1987 | |
| 1988 | // Sign extension. Compute the demanded bits in the result that are not |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1989 | // present in the input. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1990 | APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1991 | |
Dan Gohman | 977a76f | 2008-02-13 22:28:48 +0000 | [diff] [blame] | 1992 | APInt InSignBit = APInt::getSignBit(EBits); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1993 | APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1994 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1995 | // If the sign extended bits are demanded, we know that the sign |
| 1996 | // bit is demanded. |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1997 | InSignBit = InSignBit.zext(BitWidth); |
Dan Gohman | 977a76f | 2008-02-13 22:28:48 +0000 | [diff] [blame] | 1998 | if (NewBits.getBoolValue()) |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1999 | InputDemandedBits |= InSignBit; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2000 | |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2001 | ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); |
| 2002 | KnownOne &= InputDemandedBits; |
| 2003 | KnownZero &= InputDemandedBits; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2004 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
| 2005 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2006 | // If the sign bit of the input is known set or clear, then we know the |
| 2007 | // top bits of the result. |
Dan Gohman | ca93a43 | 2008-02-20 16:30:17 +0000 | [diff] [blame] | 2008 | if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2009 | KnownZero |= NewBits; |
| 2010 | KnownOne &= ~NewBits; |
Dan Gohman | ca93a43 | 2008-02-20 16:30:17 +0000 | [diff] [blame] | 2011 | } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2012 | KnownOne |= NewBits; |
| 2013 | KnownZero &= ~NewBits; |
| 2014 | } else { // Input sign bit unknown |
| 2015 | KnownZero &= ~NewBits; |
| 2016 | KnownOne &= ~NewBits; |
| 2017 | } |
| 2018 | return; |
| 2019 | } |
| 2020 | case ISD::CTTZ: |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 2021 | case ISD::CTTZ_ZERO_UNDEF: |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2022 | case ISD::CTLZ: |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 2023 | case ISD::CTLZ_ZERO_UNDEF: |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2024 | case ISD::CTPOP: { |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 2025 | unsigned LowBits = Log2_32(BitWidth)+1; |
| 2026 | KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits); |
Jay Foad | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 2027 | KnownOne.clearAllBits(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2028 | return; |
| 2029 | } |
| 2030 | case ISD::LOAD: { |
Rafael Espindola | 95d594c | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 2031 | LoadSDNode *LD = cast<LoadSDNode>(Op); |
Nadav Rotem | 7745175 | 2013-03-20 22:53:44 +0000 | [diff] [blame] | 2032 | // If this is a ZEXTLoad and we are looking at the loaded value. |
| 2033 | if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2034 | EVT VT = LD->getMemoryVT(); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 2035 | unsigned MemBits = VT.getScalarType().getSizeInBits(); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2036 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits); |
Rafael Espindola | 95d594c | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 2037 | } else if (const MDNode *Ranges = LD->getRanges()) { |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2038 | computeMaskedBitsLoad(*Ranges, KnownZero); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2039 | } |
| 2040 | return; |
| 2041 | } |
| 2042 | case ISD::ZERO_EXTEND: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2043 | EVT InVT = Op.getOperand(0).getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 2044 | unsigned InBits = InVT.getScalarType().getSizeInBits(); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2045 | APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 2046 | KnownZero = KnownZero.trunc(InBits); |
| 2047 | KnownOne = KnownOne.trunc(InBits); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2048 | ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 2049 | KnownZero = KnownZero.zext(BitWidth); |
| 2050 | KnownOne = KnownOne.zext(BitWidth); |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 2051 | KnownZero |= NewBits; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2052 | return; |
| 2053 | } |
| 2054 | case ISD::SIGN_EXTEND: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2055 | EVT InVT = Op.getOperand(0).getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 2056 | unsigned InBits = InVT.getScalarType().getSizeInBits(); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2057 | APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits); |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 2058 | |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 2059 | KnownZero = KnownZero.trunc(InBits); |
| 2060 | KnownOne = KnownOne.trunc(InBits); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2061 | ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); |
Dan Gohman | 977a76f | 2008-02-13 22:28:48 +0000 | [diff] [blame] | 2062 | |
| 2063 | // Note if the sign bit is known to be zero or one. |
| 2064 | bool SignBitKnownZero = KnownZero.isNegative(); |
| 2065 | bool SignBitKnownOne = KnownOne.isNegative(); |
| 2066 | assert(!(SignBitKnownZero && SignBitKnownOne) && |
| 2067 | "Sign bit can't be known to be both zero and one!"); |
| 2068 | |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 2069 | KnownZero = KnownZero.zext(BitWidth); |
| 2070 | KnownOne = KnownOne.zext(BitWidth); |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 2071 | |
Dan Gohman | 977a76f | 2008-02-13 22:28:48 +0000 | [diff] [blame] | 2072 | // If the sign bit is known zero or one, the top bits match. |
| 2073 | if (SignBitKnownZero) |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2074 | KnownZero |= NewBits; |
Dan Gohman | 977a76f | 2008-02-13 22:28:48 +0000 | [diff] [blame] | 2075 | else if (SignBitKnownOne) |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2076 | KnownOne |= NewBits; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2077 | return; |
| 2078 | } |
| 2079 | case ISD::ANY_EXTEND: { |
Evan Cheng | 2766a47 | 2012-12-06 19:13:27 +0000 | [diff] [blame] | 2080 | EVT InVT = Op.getOperand(0).getValueType(); |
| 2081 | unsigned InBits = InVT.getScalarType().getSizeInBits(); |
| 2082 | KnownZero = KnownZero.trunc(InBits); |
| 2083 | KnownOne = KnownOne.trunc(InBits); |
| 2084 | ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); |
| 2085 | KnownZero = KnownZero.zext(BitWidth); |
| 2086 | KnownOne = KnownOne.zext(BitWidth); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2087 | return; |
| 2088 | } |
| 2089 | case ISD::TRUNCATE: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2090 | EVT InVT = Op.getOperand(0).getValueType(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 2091 | unsigned InBits = InVT.getScalarType().getSizeInBits(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 2092 | KnownZero = KnownZero.zext(InBits); |
| 2093 | KnownOne = KnownOne.zext(InBits); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2094 | ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2095 | assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 2096 | KnownZero = KnownZero.trunc(BitWidth); |
| 2097 | KnownOne = KnownOne.trunc(BitWidth); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2098 | break; |
| 2099 | } |
| 2100 | case ISD::AssertZext: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2101 | EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2102 | APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits()); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2103 | ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); |
| 2104 | KnownZero |= (~InMask); |
Nadav Rotem | 7ee0e5a | 2012-07-16 18:34:53 +0000 | [diff] [blame] | 2105 | KnownOne &= (~KnownZero); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2106 | return; |
| 2107 | } |
Chris Lattner | d268a49 | 2007-12-22 21:26:52 +0000 | [diff] [blame] | 2108 | case ISD::FGETSIGN: |
| 2109 | // All bits are zero except the low bit. |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 2110 | KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1); |
Chris Lattner | d268a49 | 2007-12-22 21:26:52 +0000 | [diff] [blame] | 2111 | return; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2112 | |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 2113 | case ISD::SUB: { |
| 2114 | if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) { |
| 2115 | // We know that the top bits of C-X are clear if X contains less bits |
| 2116 | // than C (i.e. no wrap-around can happen). For example, 20-X is |
| 2117 | // positive if we can prove that X is >= 0 and < 16. |
| 2118 | if (CLHS->getAPIntValue().isNonNegative()) { |
| 2119 | unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros(); |
| 2120 | // NLZ can't be BitWidth with no sign bit |
| 2121 | APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2122 | ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 2123 | |
| 2124 | // If all of the MaskV bits are known to be zero, then we know the |
| 2125 | // output top bits are zero, because we now know that the output is |
| 2126 | // from [0-C]. |
| 2127 | if ((KnownZero2 & MaskV) == MaskV) { |
| 2128 | unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros(); |
| 2129 | // Top bits known zero. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2130 | KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 2131 | } |
| 2132 | } |
| 2133 | } |
| 2134 | } |
| 2135 | // fall through |
Chris Lattner | da60588 | 2010-12-19 20:38:28 +0000 | [diff] [blame] | 2136 | case ISD::ADD: |
| 2137 | case ISD::ADDE: { |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2138 | // Output known-0 bits are known if clear or set in both the low clear bits |
| 2139 | // common to both LHS & RHS. For example, 8+(X<<3) is known to have the |
| 2140 | // low 3 bits clear. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2141 | ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2142 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 2143 | unsigned KnownZeroOut = KnownZero2.countTrailingOnes(); |
| 2144 | |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2145 | ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2146 | assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 2147 | KnownZeroOut = std::min(KnownZeroOut, |
| 2148 | KnownZero2.countTrailingOnes()); |
| 2149 | |
Chris Lattner | da60588 | 2010-12-19 20:38:28 +0000 | [diff] [blame] | 2150 | if (Op.getOpcode() == ISD::ADD) { |
| 2151 | KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut); |
| 2152 | return; |
| 2153 | } |
| 2154 | |
| 2155 | // With ADDE, a carry bit may be added in, so we can only use this |
| 2156 | // information if we know (at least) that the low two bits are clear. We |
| 2157 | // then return to the caller that the low bit is unknown but that other bits |
| 2158 | // are known zero. |
| 2159 | if (KnownZeroOut >= 2) // ADDE |
| 2160 | KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2161 | return; |
| 2162 | } |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 2163 | case ISD::SREM: |
| 2164 | if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
Duncan Sands | 5c2873a | 2010-01-29 09:45:26 +0000 | [diff] [blame] | 2165 | const APInt &RA = Rem->getAPIntValue().abs(); |
| 2166 | if (RA.isPowerOf2()) { |
| 2167 | APInt LowBits = RA - 1; |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2168 | ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2169 | |
Duncan Sands | 5c2873a | 2010-01-29 09:45:26 +0000 | [diff] [blame] | 2170 | // The low bits of the first operand are unchanged by the srem. |
| 2171 | KnownZero = KnownZero2 & LowBits; |
| 2172 | KnownOne = KnownOne2 & LowBits; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2173 | |
Duncan Sands | 5c2873a | 2010-01-29 09:45:26 +0000 | [diff] [blame] | 2174 | // If the first operand is non-negative or has all low bits zero, then |
| 2175 | // the upper bits are all zero. |
| 2176 | if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits)) |
| 2177 | KnownZero |= ~LowBits; |
| 2178 | |
| 2179 | // If the first operand is negative and not all low bits are zero, then |
| 2180 | // the upper bits are all one. |
| 2181 | if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0)) |
| 2182 | KnownOne |= ~LowBits; |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 2183 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2184 | } |
| 2185 | } |
| 2186 | return; |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 2187 | case ISD::UREM: { |
| 2188 | if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
Dan Gohman | 9b44c1f | 2008-07-03 00:52:03 +0000 | [diff] [blame] | 2189 | const APInt &RA = Rem->getAPIntValue(); |
Dan Gohman | 23e1df8 | 2008-05-06 00:51:48 +0000 | [diff] [blame] | 2190 | if (RA.isPowerOf2()) { |
| 2191 | APInt LowBits = (RA - 1); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2192 | KnownZero |= ~LowBits; |
| 2193 | ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 2194 | assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?"); |
| 2195 | break; |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | // Since the result is less than or equal to either operand, any leading |
| 2200 | // zero bits in either operand must also exist in the result. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2201 | ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); |
| 2202 | ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 2203 | |
| 2204 | uint32_t Leaders = std::max(KnownZero.countLeadingOnes(), |
| 2205 | KnownZero2.countLeadingOnes()); |
Jay Foad | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 2206 | KnownOne.clearAllBits(); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2207 | KnownZero = APInt::getHighBitsSet(BitWidth, Leaders); |
Dan Gohman | 23e8b71 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 2208 | return; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2209 | } |
Chris Lattner | 0a9481f | 2011-02-13 22:25:43 +0000 | [diff] [blame] | 2210 | case ISD::FrameIndex: |
| 2211 | case ISD::TargetFrameIndex: |
| 2212 | if (unsigned Align = InferPtrAlignment(Op)) { |
| 2213 | // The low bits are known zero if the pointer is aligned. |
| 2214 | KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align)); |
| 2215 | return; |
| 2216 | } |
| 2217 | break; |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2218 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2219 | default: |
Evan Cheng | b5a55d9 | 2011-05-24 01:48:22 +0000 | [diff] [blame] | 2220 | if (Op.getOpcode() < ISD::BUILTIN_OP_END) |
| 2221 | break; |
| 2222 | // Fallthrough |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2223 | case ISD::INTRINSIC_WO_CHAIN: |
| 2224 | case ISD::INTRINSIC_W_CHAIN: |
| 2225 | case ISD::INTRINSIC_VOID: |
Evan Cheng | b5a55d9 | 2011-05-24 01:48:22 +0000 | [diff] [blame] | 2226 | // Allow the target to implement this method for its nodes. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2227 | TLI->computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2228 | return; |
| 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | /// ComputeNumSignBits - Return the number of times the sign bit of the |
| 2233 | /// register is replicated into the other bits. We know that at least 1 bit |
| 2234 | /// is always equal to the sign bit (itself), but other cases can give us |
| 2235 | /// information. For example, immediately after an "SRA X, 2", we know that |
| 2236 | /// the top 3 bits are all equal to each other, so we return 3. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2237 | unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{ |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2238 | const TargetLowering *TLI = TM.getTargetLowering(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2239 | EVT VT = Op.getValueType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2240 | assert(VT.isInteger() && "Invalid VT!"); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 2241 | unsigned VTBits = VT.getScalarType().getSizeInBits(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2242 | unsigned Tmp, Tmp2; |
Dan Gohman | a332f17 | 2008-05-23 02:28:01 +0000 | [diff] [blame] | 2243 | unsigned FirstAnswer = 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2244 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2245 | if (Depth == 6) |
| 2246 | return 1; // Limit search depth. |
| 2247 | |
| 2248 | switch (Op.getOpcode()) { |
| 2249 | default: break; |
| 2250 | case ISD::AssertSext: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2251 | Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2252 | return VTBits-Tmp+1; |
| 2253 | case ISD::AssertZext: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2254 | Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2255 | return VTBits-Tmp; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2256 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2257 | case ISD::Constant: { |
Dan Gohman | bb271ff | 2008-03-03 23:35:36 +0000 | [diff] [blame] | 2258 | const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue(); |
Cameron Zwarich | 9b6af8d | 2011-02-24 10:00:20 +0000 | [diff] [blame] | 2259 | return Val.getNumSignBits(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2260 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2261 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2262 | case ISD::SIGN_EXTEND: |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 2263 | Tmp = |
| 2264 | VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2265 | return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2266 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2267 | case ISD::SIGN_EXTEND_INREG: |
| 2268 | // Max of the input and what this extends. |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 2269 | Tmp = |
| 2270 | cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2271 | Tmp = VTBits-Tmp+1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2272 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2273 | Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1); |
| 2274 | return std::max(Tmp, Tmp2); |
| 2275 | |
| 2276 | case ISD::SRA: |
| 2277 | Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); |
| 2278 | // SRA X, C -> adds C sign bits. |
| 2279 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2280 | Tmp += C->getZExtValue(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2281 | if (Tmp > VTBits) Tmp = VTBits; |
| 2282 | } |
| 2283 | return Tmp; |
| 2284 | case ISD::SHL: |
| 2285 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 2286 | // shl destroys sign bits. |
| 2287 | Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2288 | if (C->getZExtValue() >= VTBits || // Bad shift. |
| 2289 | C->getZExtValue() >= Tmp) break; // Shifted all sign bits out. |
| 2290 | return Tmp - C->getZExtValue(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2291 | } |
| 2292 | break; |
| 2293 | case ISD::AND: |
| 2294 | case ISD::OR: |
| 2295 | case ISD::XOR: // NOT is handled here. |
Dan Gohman | a332f17 | 2008-05-23 02:28:01 +0000 | [diff] [blame] | 2296 | // Logical binary ops preserve the number of sign bits at the worst. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2297 | Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); |
Dan Gohman | a332f17 | 2008-05-23 02:28:01 +0000 | [diff] [blame] | 2298 | if (Tmp != 1) { |
| 2299 | Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); |
| 2300 | FirstAnswer = std::min(Tmp, Tmp2); |
| 2301 | // We computed what we know about the sign bits as our first |
| 2302 | // answer. Now proceed to the generic code that uses |
| 2303 | // ComputeMaskedBits, and pick whichever answer is better. |
| 2304 | } |
| 2305 | break; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2306 | |
| 2307 | case ISD::SELECT: |
Dan Gohman | c84941b | 2008-05-20 20:59:51 +0000 | [diff] [blame] | 2308 | Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2309 | if (Tmp == 1) return 1; // Early out. |
Dan Gohman | c84941b | 2008-05-20 20:59:51 +0000 | [diff] [blame] | 2310 | Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2311 | return std::min(Tmp, Tmp2); |
Bill Wendling | 253174b | 2008-11-22 07:24:01 +0000 | [diff] [blame] | 2312 | |
| 2313 | case ISD::SADDO: |
| 2314 | case ISD::UADDO: |
Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 2315 | case ISD::SSUBO: |
| 2316 | case ISD::USUBO: |
| 2317 | case ISD::SMULO: |
| 2318 | case ISD::UMULO: |
Bill Wendling | 253174b | 2008-11-22 07:24:01 +0000 | [diff] [blame] | 2319 | if (Op.getResNo() != 1) |
| 2320 | break; |
Duncan Sands | 0322808 | 2008-11-23 15:47:28 +0000 | [diff] [blame] | 2321 | // The boolean result conforms to getBooleanContents. Fall through. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2322 | case ISD::SETCC: |
| 2323 | // If setcc returns 0/-1, all bits are sign bits. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2324 | if (TLI->getBooleanContents(Op.getValueType().isVector()) == |
Duncan Sands | 0322808 | 2008-11-23 15:47:28 +0000 | [diff] [blame] | 2325 | TargetLowering::ZeroOrNegativeOneBooleanContent) |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2326 | return VTBits; |
| 2327 | break; |
| 2328 | case ISD::ROTL: |
| 2329 | case ISD::ROTR: |
| 2330 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2331 | unsigned RotAmt = C->getZExtValue() & (VTBits-1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2332 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2333 | // Handle rotate right by N like a rotate left by 32-N. |
| 2334 | if (Op.getOpcode() == ISD::ROTR) |
| 2335 | RotAmt = (VTBits-RotAmt) & (VTBits-1); |
| 2336 | |
| 2337 | // If we aren't rotating out all of the known-in sign bits, return the |
| 2338 | // number that are left. This handles rotl(sext(x), 1) for example. |
| 2339 | Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); |
| 2340 | if (Tmp > RotAmt+1) return Tmp-RotAmt; |
| 2341 | } |
| 2342 | break; |
| 2343 | case ISD::ADD: |
| 2344 | // Add can have at most one carry bit. Thus we know that the output |
| 2345 | // is, at worst, one more bit than the inputs. |
| 2346 | Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); |
| 2347 | if (Tmp == 1) return 1; // Early out. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2348 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2349 | // Special case decrementing a value (ADD X, -1): |
Dan Gohman | 0001e56 | 2009-02-24 02:00:40 +0000 | [diff] [blame] | 2350 | if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1))) |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2351 | if (CRHS->isAllOnesValue()) { |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 2352 | APInt KnownZero, KnownOne; |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2353 | ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2354 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2355 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 2356 | // sign bits set. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2357 | if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue()) |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2358 | return VTBits; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2359 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2360 | // If we are subtracting one from a positive number, there is no carry |
| 2361 | // out of the result. |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 2362 | if (KnownZero.isNegative()) |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2363 | return Tmp; |
| 2364 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2365 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2366 | Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); |
| 2367 | if (Tmp2 == 1) return 1; |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 2368 | return std::min(Tmp, Tmp2)-1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2369 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2370 | case ISD::SUB: |
| 2371 | Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1); |
| 2372 | if (Tmp2 == 1) return 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2373 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2374 | // Handle NEG. |
| 2375 | if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2376 | if (CLHS->isNullValue()) { |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 2377 | APInt KnownZero, KnownOne; |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2378 | ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2379 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 2380 | // sign bits set. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2381 | if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue()) |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2382 | return VTBits; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2383 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2384 | // If the input is known to be positive (the sign bit is known clear), |
| 2385 | // the output of the NEG has the same number of sign bits as the input. |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 2386 | if (KnownZero.isNegative()) |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2387 | return Tmp2; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2388 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2389 | // Otherwise, we treat this like a SUB. |
| 2390 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2391 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2392 | // Sub can have at most one carry bit. Thus we know that the output |
| 2393 | // is, at worst, one more bit than the inputs. |
| 2394 | Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1); |
| 2395 | if (Tmp == 1) return 1; // Early out. |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 2396 | return std::min(Tmp, Tmp2)-1; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2397 | case ISD::TRUNCATE: |
| 2398 | // FIXME: it's tricky to do anything useful for this, but it is an important |
| 2399 | // case for targets like X86. |
| 2400 | break; |
| 2401 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2402 | |
Nadav Rotem | 7745175 | 2013-03-20 22:53:44 +0000 | [diff] [blame] | 2403 | // If we are looking at the loaded value of the SDNode. |
| 2404 | if (Op.getResNo() == 0) { |
| 2405 | // Handle LOADX separately here. EXTLOAD case will fallthrough. |
| 2406 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) { |
| 2407 | unsigned ExtType = LD->getExtensionType(); |
| 2408 | switch (ExtType) { |
| 2409 | default: break; |
| 2410 | case ISD::SEXTLOAD: // '17' bits known |
| 2411 | Tmp = LD->getMemoryVT().getScalarType().getSizeInBits(); |
| 2412 | return VTBits-Tmp+1; |
| 2413 | case ISD::ZEXTLOAD: // '16' bits known |
| 2414 | Tmp = LD->getMemoryVT().getScalarType().getSizeInBits(); |
| 2415 | return VTBits-Tmp; |
| 2416 | } |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2417 | } |
| 2418 | } |
| 2419 | |
| 2420 | // Allow the target to implement this method for its nodes. |
| 2421 | if (Op.getOpcode() >= ISD::BUILTIN_OP_END || |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2422 | Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN || |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2423 | Op.getOpcode() == ISD::INTRINSIC_W_CHAIN || |
| 2424 | Op.getOpcode() == ISD::INTRINSIC_VOID) { |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 2425 | unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, Depth); |
Dan Gohman | a332f17 | 2008-05-23 02:28:01 +0000 | [diff] [blame] | 2426 | if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2427 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2428 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2429 | // Finally, if we can prove that the top bits of the result are 0's or 1's, |
| 2430 | // use this information. |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 2431 | APInt KnownZero, KnownOne; |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2432 | ComputeMaskedBits(Op, KnownZero, KnownOne, Depth); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2433 | |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 2434 | APInt Mask; |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 2435 | if (KnownZero.isNegative()) { // sign bit is 0 |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2436 | Mask = KnownZero; |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 2437 | } else if (KnownOne.isNegative()) { // sign bit is 1; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2438 | Mask = KnownOne; |
| 2439 | } else { |
| 2440 | // Nothing known. |
Dan Gohman | a332f17 | 2008-05-23 02:28:01 +0000 | [diff] [blame] | 2441 | return FirstAnswer; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2442 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2443 | |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2444 | // Okay, we know that the sign bit in Mask is set. Use CLZ to determine |
| 2445 | // the number of identical bits in the top of the input value. |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 2446 | Mask = ~Mask; |
| 2447 | Mask <<= Mask.getBitWidth()-VTBits; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2448 | // Return # leading zeros. We use 'min' here in case Val was zero before |
| 2449 | // shifting. We don't want to return '64' as for an i32 "0". |
Dan Gohman | a332f17 | 2008-05-23 02:28:01 +0000 | [diff] [blame] | 2450 | return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros())); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Chris Lattner | 0a9481f | 2011-02-13 22:25:43 +0000 | [diff] [blame] | 2453 | /// isBaseWithConstantOffset - Return true if the specified operand is an |
| 2454 | /// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an |
| 2455 | /// ISD::OR with a ConstantSDNode that is guaranteed to have the same |
| 2456 | /// semantics as an ADD. This handles the equivalence: |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 2457 | /// X|Cst == X+Cst iff X&Cst = 0. |
Chris Lattner | 0a9481f | 2011-02-13 22:25:43 +0000 | [diff] [blame] | 2458 | bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const { |
| 2459 | if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) || |
| 2460 | !isa<ConstantSDNode>(Op.getOperand(1))) |
| 2461 | return false; |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2462 | |
| 2463 | if (Op.getOpcode() == ISD::OR && |
Chris Lattner | 0a9481f | 2011-02-13 22:25:43 +0000 | [diff] [blame] | 2464 | !MaskedValueIsZero(Op.getOperand(0), |
| 2465 | cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue())) |
| 2466 | return false; |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2467 | |
Chris Lattner | 0a9481f | 2011-02-13 22:25:43 +0000 | [diff] [blame] | 2468 | return true; |
| 2469 | } |
| 2470 | |
| 2471 | |
Dan Gohman | 8d44b28 | 2009-09-03 20:34:31 +0000 | [diff] [blame] | 2472 | bool SelectionDAG::isKnownNeverNaN(SDValue Op) const { |
| 2473 | // If we're told that NaNs won't happen, assume they won't. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2474 | if (getTarget().Options.NoNaNsFPMath) |
Dan Gohman | 8d44b28 | 2009-09-03 20:34:31 +0000 | [diff] [blame] | 2475 | return true; |
| 2476 | |
| 2477 | // If the value is a constant, we can obviously see if it is a NaN or not. |
| 2478 | if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) |
| 2479 | return !C->getValueAPF().isNaN(); |
| 2480 | |
| 2481 | // TODO: Recognize more cases here. |
| 2482 | |
| 2483 | return false; |
| 2484 | } |
Chris Lattner | 51dabfb | 2006-10-14 00:41:01 +0000 | [diff] [blame] | 2485 | |
Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 2486 | bool SelectionDAG::isKnownNeverZero(SDValue Op) const { |
| 2487 | // If the value is a constant, we can obviously see if it is a zero or not. |
| 2488 | if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) |
| 2489 | return !C->isZero(); |
| 2490 | |
| 2491 | // TODO: Recognize more cases here. |
Evan Cheng | b5a55d9 | 2011-05-24 01:48:22 +0000 | [diff] [blame] | 2492 | switch (Op.getOpcode()) { |
| 2493 | default: break; |
| 2494 | case ISD::OR: |
| 2495 | if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) |
| 2496 | return !C->isNullValue(); |
| 2497 | break; |
| 2498 | } |
Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 2499 | |
| 2500 | return false; |
| 2501 | } |
| 2502 | |
| 2503 | bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const { |
| 2504 | // Check the obvious case. |
| 2505 | if (A == B) return true; |
| 2506 | |
| 2507 | // For for negative and positive zero. |
| 2508 | if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A)) |
| 2509 | if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B)) |
| 2510 | if (CA->isZero() && CB->isZero()) return true; |
| 2511 | |
| 2512 | // Otherwise they may not be equal. |
| 2513 | return false; |
| 2514 | } |
| 2515 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2516 | /// getNode - Gets or creates the specified node. |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 2517 | /// |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2518 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) { |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 2519 | FoldingSetNodeID ID; |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 2520 | AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0); |
Chris Lattner | 4a283e9 | 2006-08-11 18:38:11 +0000 | [diff] [blame] | 2521 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 2522 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2523 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 2524 | |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 2525 | SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), |
| 2526 | DL.getDebugLoc(), getVTList(VT)); |
Chris Lattner | 4a283e9 | 2006-08-11 18:38:11 +0000 | [diff] [blame] | 2527 | CSEMap.InsertNode(N, IP); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2528 | |
Chris Lattner | 4a283e9 | 2006-08-11 18:38:11 +0000 | [diff] [blame] | 2529 | AllNodes.push_back(N); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 2530 | #ifndef NDEBUG |
Duncan Sands | 59d2dad | 2010-11-20 11:25:00 +0000 | [diff] [blame] | 2531 | VerifySDNode(N); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 2532 | #endif |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2533 | return SDValue(N, 0); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2536 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2537 | EVT VT, SDValue Operand) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 2538 | // Constant fold unary operations with an integer constant operand. Even |
| 2539 | // opaque constant will be folded, because the folding of unary operations |
| 2540 | // doesn't create new constants with different values. Nevertheless, the |
| 2541 | // opaque flag is preserved during folding to prevent future folding with |
| 2542 | // other constants. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2543 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) { |
Dan Gohman | 6c23150 | 2008-02-29 01:47:35 +0000 | [diff] [blame] | 2544 | const APInt &Val = C->getAPIntValue(); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2545 | switch (Opcode) { |
| 2546 | default: break; |
Evan Cheng | eb49c4e | 2008-03-06 17:42:34 +0000 | [diff] [blame] | 2547 | case ISD::SIGN_EXTEND: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 2548 | return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT, |
| 2549 | C->isTargetOpcode(), C->isOpaque()); |
Chris Lattner | 4ed11b4 | 2005-09-02 00:17:32 +0000 | [diff] [blame] | 2550 | case ISD::ANY_EXTEND: |
Dan Gohman | 6c23150 | 2008-02-29 01:47:35 +0000 | [diff] [blame] | 2551 | case ISD::ZERO_EXTEND: |
Evan Cheng | eb49c4e | 2008-03-06 17:42:34 +0000 | [diff] [blame] | 2552 | case ISD::TRUNCATE: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 2553 | return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT, |
| 2554 | C->isTargetOpcode(), C->isOpaque()); |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 2555 | case ISD::UINT_TO_FP: |
| 2556 | case ISD::SINT_TO_FP: { |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2557 | APFloat apf(EVTToAPFloatSemantics(VT), |
| 2558 | APInt::getNullValue(VT.getSizeInBits())); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2559 | (void)apf.convertFromAPInt(Val, |
Dan Gohman | 6c23150 | 2008-02-29 01:47:35 +0000 | [diff] [blame] | 2560 | Opcode==ISD::SINT_TO_FP, |
| 2561 | APFloat::rmNearestTiesToEven); |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 2562 | return getConstantFP(apf, VT); |
| 2563 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2564 | case ISD::BITCAST: |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2565 | if (VT == MVT::f32 && C->getValueType(0) == MVT::i32) |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2566 | return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2567 | else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64) |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2568 | return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 2569 | break; |
Nate Begeman | 1b5db7a | 2006-01-16 08:07:10 +0000 | [diff] [blame] | 2570 | case ISD::BSWAP: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 2571 | return getConstant(Val.byteSwap(), VT, C->isTargetOpcode(), |
| 2572 | C->isOpaque()); |
Nate Begeman | 1b5db7a | 2006-01-16 08:07:10 +0000 | [diff] [blame] | 2573 | case ISD::CTPOP: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 2574 | return getConstant(Val.countPopulation(), VT, C->isTargetOpcode(), |
| 2575 | C->isOpaque()); |
Nate Begeman | 1b5db7a | 2006-01-16 08:07:10 +0000 | [diff] [blame] | 2576 | case ISD::CTLZ: |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 2577 | case ISD::CTLZ_ZERO_UNDEF: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 2578 | return getConstant(Val.countLeadingZeros(), VT, C->isTargetOpcode(), |
| 2579 | C->isOpaque()); |
Nate Begeman | 1b5db7a | 2006-01-16 08:07:10 +0000 | [diff] [blame] | 2580 | case ISD::CTTZ: |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 2581 | case ISD::CTTZ_ZERO_UNDEF: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 2582 | return getConstant(Val.countTrailingZeros(), VT, C->isTargetOpcode(), |
| 2583 | C->isOpaque()); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2584 | } |
| 2585 | } |
| 2586 | |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 2587 | // Constant fold unary operations with a floating point constant operand. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2588 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) { |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 2589 | APFloat V = C->getValueAPF(); // make copy |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 2590 | switch (Opcode) { |
| 2591 | case ISD::FNEG: |
| 2592 | V.changeSign(); |
| 2593 | return getConstantFP(V, VT); |
| 2594 | case ISD::FABS: |
| 2595 | V.clearSign(); |
| 2596 | return getConstantFP(V, VT); |
| 2597 | case ISD::FCEIL: { |
| 2598 | APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive); |
| 2599 | if (fs == APFloat::opOK || fs == APFloat::opInexact) |
Dale Johannesen | db44bf8 | 2007-10-16 23:38:29 +0000 | [diff] [blame] | 2600 | return getConstantFP(V, VT); |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 2601 | break; |
| 2602 | } |
| 2603 | case ISD::FTRUNC: { |
| 2604 | APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero); |
| 2605 | if (fs == APFloat::opOK || fs == APFloat::opInexact) |
Dale Johannesen | db44bf8 | 2007-10-16 23:38:29 +0000 | [diff] [blame] | 2606 | return getConstantFP(V, VT); |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 2607 | break; |
| 2608 | } |
| 2609 | case ISD::FFLOOR: { |
| 2610 | APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative); |
| 2611 | if (fs == APFloat::opOK || fs == APFloat::opInexact) |
Dale Johannesen | db44bf8 | 2007-10-16 23:38:29 +0000 | [diff] [blame] | 2612 | return getConstantFP(V, VT); |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 2613 | break; |
| 2614 | } |
| 2615 | case ISD::FP_EXTEND: { |
| 2616 | bool ignored; |
| 2617 | // This can return overflow, underflow, or inexact; we don't care. |
| 2618 | // FIXME need to be more flexible about rounding mode. |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 2619 | (void)V.convert(EVTToAPFloatSemantics(VT), |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 2620 | APFloat::rmNearestTiesToEven, &ignored); |
| 2621 | return getConstantFP(V, VT); |
| 2622 | } |
| 2623 | case ISD::FP_TO_SINT: |
| 2624 | case ISD::FP_TO_UINT: { |
| 2625 | integerPart x[2]; |
| 2626 | bool ignored; |
| 2627 | assert(integerPartWidth >= 64); |
| 2628 | // FIXME need to be more flexible about rounding mode. |
| 2629 | APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(), |
| 2630 | Opcode==ISD::FP_TO_SINT, |
| 2631 | APFloat::rmTowardZero, &ignored); |
| 2632 | if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 2633 | break; |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 2634 | APInt api(VT.getSizeInBits(), x); |
| 2635 | return getConstant(api, VT); |
| 2636 | } |
| 2637 | case ISD::BITCAST: |
| 2638 | if (VT == MVT::i32 && C->getValueType(0) == MVT::f32) |
| 2639 | return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT); |
| 2640 | else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64) |
| 2641 | return getConstant(V.bitcastToAPInt().getZExtValue(), VT); |
| 2642 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2643 | } |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 2644 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2645 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2646 | unsigned OpOpcode = Operand.getNode()->getOpcode(); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2647 | switch (Opcode) { |
Chris Lattner | a93ec3e | 2005-01-21 18:01:22 +0000 | [diff] [blame] | 2648 | case ISD::TokenFactor: |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2649 | case ISD::MERGE_VALUES: |
Dan Gohman | 7f8613e | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2650 | case ISD::CONCAT_VECTORS: |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2651 | return Operand; // Factor, merge or concat of one node? No need. |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2652 | case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node"); |
Chris Lattner | ff33cc4 | 2007-04-09 05:23:13 +0000 | [diff] [blame] | 2653 | case ISD::FP_EXTEND: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2654 | assert(VT.isFloatingPoint() && |
| 2655 | Operand.getValueType().isFloatingPoint() && "Invalid FP cast!"); |
Chris Lattner | 7e2e033 | 2008-01-16 17:59:31 +0000 | [diff] [blame] | 2656 | if (Operand.getValueType() == VT) return Operand; // noop conversion. |
Dan Gohman | 2e141d7 | 2009-12-14 23:40:38 +0000 | [diff] [blame] | 2657 | assert((!VT.isVector() || |
| 2658 | VT.getVectorNumElements() == |
| 2659 | Operand.getValueType().getVectorNumElements()) && |
| 2660 | "Vector element count mismatch!"); |
Chris Lattner | 5d03f21 | 2008-03-11 06:21:08 +0000 | [diff] [blame] | 2661 | if (Operand.getOpcode() == ISD::UNDEF) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2662 | return getUNDEF(VT); |
Chris Lattner | ff33cc4 | 2007-04-09 05:23:13 +0000 | [diff] [blame] | 2663 | break; |
Chris Lattner | 5d03f21 | 2008-03-11 06:21:08 +0000 | [diff] [blame] | 2664 | case ISD::SIGN_EXTEND: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2665 | assert(VT.isInteger() && Operand.getValueType().isInteger() && |
Chris Lattner | ff33cc4 | 2007-04-09 05:23:13 +0000 | [diff] [blame] | 2666 | "Invalid SIGN_EXTEND!"); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2667 | if (Operand.getValueType() == VT) return Operand; // noop extension |
Dan Gohman | 2e141d7 | 2009-12-14 23:40:38 +0000 | [diff] [blame] | 2668 | assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) && |
| 2669 | "Invalid sext node, dst < src!"); |
| 2670 | assert((!VT.isVector() || |
| 2671 | VT.getVectorNumElements() == |
| 2672 | Operand.getValueType().getVectorNumElements()) && |
| 2673 | "Vector element count mismatch!"); |
Nadav Rotem | 0c8607b | 2013-01-20 08:35:56 +0000 | [diff] [blame] | 2674 | if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) |
| 2675 | return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0)); |
| 2676 | else if (OpOpcode == ISD::UNDEF) |
Evan Cheng | bf34a5e | 2011-03-15 02:22:10 +0000 | [diff] [blame] | 2677 | // sext(undef) = 0, because the top bits will all be the same. |
| 2678 | return getConstant(0, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2679 | break; |
| 2680 | case ISD::ZERO_EXTEND: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2681 | assert(VT.isInteger() && Operand.getValueType().isInteger() && |
Chris Lattner | ff33cc4 | 2007-04-09 05:23:13 +0000 | [diff] [blame] | 2682 | "Invalid ZERO_EXTEND!"); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2683 | if (Operand.getValueType() == VT) return Operand; // noop extension |
Dan Gohman | 2e141d7 | 2009-12-14 23:40:38 +0000 | [diff] [blame] | 2684 | assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) && |
| 2685 | "Invalid zext node, dst < src!"); |
| 2686 | assert((!VT.isVector() || |
| 2687 | VT.getVectorNumElements() == |
| 2688 | Operand.getValueType().getVectorNumElements()) && |
| 2689 | "Vector element count mismatch!"); |
Chris Lattner | 5a6bace | 2005-04-07 19:43:53 +0000 | [diff] [blame] | 2690 | if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2691 | return getNode(ISD::ZERO_EXTEND, DL, VT, |
Dale Johannesen | dbfd8db | 2009-02-03 01:55:44 +0000 | [diff] [blame] | 2692 | Operand.getNode()->getOperand(0)); |
Evan Cheng | bf34a5e | 2011-03-15 02:22:10 +0000 | [diff] [blame] | 2693 | else if (OpOpcode == ISD::UNDEF) |
| 2694 | // zext(undef) = 0, because the top bits will be zero. |
| 2695 | return getConstant(0, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2696 | break; |
Chris Lattner | 4ed11b4 | 2005-09-02 00:17:32 +0000 | [diff] [blame] | 2697 | case ISD::ANY_EXTEND: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2698 | assert(VT.isInteger() && Operand.getValueType().isInteger() && |
Chris Lattner | ff33cc4 | 2007-04-09 05:23:13 +0000 | [diff] [blame] | 2699 | "Invalid ANY_EXTEND!"); |
Chris Lattner | 4ed11b4 | 2005-09-02 00:17:32 +0000 | [diff] [blame] | 2700 | if (Operand.getValueType() == VT) return Operand; // noop extension |
Dan Gohman | 2e141d7 | 2009-12-14 23:40:38 +0000 | [diff] [blame] | 2701 | assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) && |
| 2702 | "Invalid anyext node, dst < src!"); |
| 2703 | assert((!VT.isVector() || |
| 2704 | VT.getVectorNumElements() == |
| 2705 | Operand.getValueType().getVectorNumElements()) && |
| 2706 | "Vector element count mismatch!"); |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 2707 | |
Dan Gohman | 9e86a73 | 2010-06-18 00:08:30 +0000 | [diff] [blame] | 2708 | if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND || |
| 2709 | OpOpcode == ISD::ANY_EXTEND) |
Chris Lattner | 4ed11b4 | 2005-09-02 00:17:32 +0000 | [diff] [blame] | 2710 | // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x) |
Dale Johannesen | dbfd8db | 2009-02-03 01:55:44 +0000 | [diff] [blame] | 2711 | return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0)); |
Evan Cheng | 5ae1da9 | 2011-03-14 18:15:55 +0000 | [diff] [blame] | 2712 | else if (OpOpcode == ISD::UNDEF) |
| 2713 | return getUNDEF(VT); |
Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 2714 | |
| 2715 | // (ext (trunx x)) -> x |
| 2716 | if (OpOpcode == ISD::TRUNCATE) { |
| 2717 | SDValue OpOp = Operand.getNode()->getOperand(0); |
| 2718 | if (OpOp.getValueType() == VT) |
| 2719 | return OpOp; |
| 2720 | } |
Chris Lattner | 4ed11b4 | 2005-09-02 00:17:32 +0000 | [diff] [blame] | 2721 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2722 | case ISD::TRUNCATE: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2723 | assert(VT.isInteger() && Operand.getValueType().isInteger() && |
Chris Lattner | ff33cc4 | 2007-04-09 05:23:13 +0000 | [diff] [blame] | 2724 | "Invalid TRUNCATE!"); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2725 | if (Operand.getValueType() == VT) return Operand; // noop truncate |
Dan Gohman | 2e141d7 | 2009-12-14 23:40:38 +0000 | [diff] [blame] | 2726 | assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) && |
| 2727 | "Invalid truncate node, src < dst!"); |
| 2728 | assert((!VT.isVector() || |
| 2729 | VT.getVectorNumElements() == |
| 2730 | Operand.getValueType().getVectorNumElements()) && |
| 2731 | "Vector element count mismatch!"); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2732 | if (OpOpcode == ISD::TRUNCATE) |
Dale Johannesen | dbfd8db | 2009-02-03 01:55:44 +0000 | [diff] [blame] | 2733 | return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0)); |
Craig Topper | 799ea5c | 2012-01-15 01:05:11 +0000 | [diff] [blame] | 2734 | if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND || |
| 2735 | OpOpcode == ISD::ANY_EXTEND) { |
Chris Lattner | fd8c39b | 2005-01-07 21:56:24 +0000 | [diff] [blame] | 2736 | // If the source is smaller than the dest, we still need an extend. |
Dan Gohman | 2e141d7 | 2009-12-14 23:40:38 +0000 | [diff] [blame] | 2737 | if (Operand.getNode()->getOperand(0).getValueType().getScalarType() |
| 2738 | .bitsLT(VT.getScalarType())) |
Dale Johannesen | dbfd8db | 2009-02-03 01:55:44 +0000 | [diff] [blame] | 2739 | return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0)); |
Craig Topper | 799ea5c | 2012-01-15 01:05:11 +0000 | [diff] [blame] | 2740 | if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT)) |
Dale Johannesen | dbfd8db | 2009-02-03 01:55:44 +0000 | [diff] [blame] | 2741 | return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0)); |
Craig Topper | 799ea5c | 2012-01-15 01:05:11 +0000 | [diff] [blame] | 2742 | return Operand.getNode()->getOperand(0); |
Chris Lattner | fd8c39b | 2005-01-07 21:56:24 +0000 | [diff] [blame] | 2743 | } |
Craig Topper | 799ea5c | 2012-01-15 01:05:11 +0000 | [diff] [blame] | 2744 | if (OpOpcode == ISD::UNDEF) |
| 2745 | return getUNDEF(VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2746 | break; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2747 | case ISD::BITCAST: |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2748 | // Basic sanity checking. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2749 | assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits() |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2750 | && "Cannot BITCAST between types of different sizes!"); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2751 | if (VT == Operand.getValueType()) return Operand; // noop conversion. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2752 | if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x) |
| 2753 | return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0)); |
Chris Lattner | 08da55e | 2006-04-04 01:02:22 +0000 | [diff] [blame] | 2754 | if (OpOpcode == ISD::UNDEF) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2755 | return getUNDEF(VT); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2756 | break; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2757 | case ISD::SCALAR_TO_VECTOR: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2758 | assert(VT.isVector() && !Operand.getValueType().isVector() && |
Duncan Sands | b10b5ac | 2009-04-18 20:16:54 +0000 | [diff] [blame] | 2759 | (VT.getVectorElementType() == Operand.getValueType() || |
| 2760 | (VT.getVectorElementType().isInteger() && |
| 2761 | Operand.getValueType().isInteger() && |
| 2762 | VT.getVectorElementType().bitsLE(Operand.getValueType()))) && |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2763 | "Illegal SCALAR_TO_VECTOR node!"); |
Chris Lattner | f3ba434 | 2008-03-08 23:43:36 +0000 | [diff] [blame] | 2764 | if (OpOpcode == ISD::UNDEF) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2765 | return getUNDEF(VT); |
Chris Lattner | f3ba434 | 2008-03-08 23:43:36 +0000 | [diff] [blame] | 2766 | // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined. |
| 2767 | if (OpOpcode == ISD::EXTRACT_VECTOR_ELT && |
| 2768 | isa<ConstantSDNode>(Operand.getOperand(1)) && |
| 2769 | Operand.getConstantOperandVal(1) == 0 && |
| 2770 | Operand.getOperand(0).getValueType() == VT) |
| 2771 | return Operand.getOperand(0); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2772 | break; |
Chris Lattner | 485df9b | 2005-04-09 03:02:46 +0000 | [diff] [blame] | 2773 | case ISD::FNEG: |
Mon P Wang | a7b6cff | 2009-01-31 06:07:45 +0000 | [diff] [blame] | 2774 | // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0 |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2775 | if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB) |
Dale Johannesen | dbfd8db | 2009-02-03 01:55:44 +0000 | [diff] [blame] | 2776 | return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1), |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2777 | Operand.getNode()->getOperand(0)); |
Chris Lattner | 485df9b | 2005-04-09 03:02:46 +0000 | [diff] [blame] | 2778 | if (OpOpcode == ISD::FNEG) // --X -> X |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2779 | return Operand.getNode()->getOperand(0); |
Chris Lattner | 485df9b | 2005-04-09 03:02:46 +0000 | [diff] [blame] | 2780 | break; |
| 2781 | case ISD::FABS: |
| 2782 | if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X) |
Dale Johannesen | dbfd8db | 2009-02-03 01:55:44 +0000 | [diff] [blame] | 2783 | return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0)); |
Chris Lattner | 485df9b | 2005-04-09 03:02:46 +0000 | [diff] [blame] | 2784 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 2787 | SDNode *N; |
Chris Lattner | 0b3e525 | 2006-08-15 19:11:05 +0000 | [diff] [blame] | 2788 | SDVTList VTs = getVTList(VT); |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 2789 | if (VT != MVT::Glue) { // Don't CSE flag producing nodes |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 2790 | FoldingSetNodeID ID; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2791 | SDValue Ops[1] = { Operand }; |
Chris Lattner | 63e3f14 | 2007-02-04 07:28:00 +0000 | [diff] [blame] | 2792 | AddNodeIDNode(ID, Opcode, VTs, Ops, 1); |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 2793 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 2794 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2795 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 2796 | |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 2797 | N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(), |
| 2798 | DL.getDebugLoc(), VTs, Operand); |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 2799 | CSEMap.InsertNode(N, IP); |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 2800 | } else { |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 2801 | N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(), |
| 2802 | DL.getDebugLoc(), VTs, Operand); |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 2803 | } |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 2804 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2805 | AllNodes.push_back(N); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 2806 | #ifndef NDEBUG |
Duncan Sands | 59d2dad | 2010-11-20 11:25:00 +0000 | [diff] [blame] | 2807 | VerifySDNode(N); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 2808 | #endif |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2809 | return SDValue(N, 0); |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Benjamin Kramer | 4969310 | 2013-02-04 15:19:18 +0000 | [diff] [blame] | 2812 | SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT, |
| 2813 | SDNode *Cst1, SDNode *Cst2) { |
| 2814 | SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs; |
| 2815 | SmallVector<SDValue, 4> Outputs; |
| 2816 | EVT SVT = VT.getScalarType(); |
Bill Wendling | 688d1c4 | 2008-09-24 10:16:24 +0000 | [diff] [blame] | 2817 | |
Benjamin Kramer | 4969310 | 2013-02-04 15:19:18 +0000 | [diff] [blame] | 2818 | ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1); |
| 2819 | ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 2820 | if (Scalar1 && Scalar2 && (Scalar1->isOpaque() || Scalar2->isOpaque())) |
| 2821 | return SDValue(); |
| 2822 | |
| 2823 | if (Scalar1 && Scalar2) |
Benjamin Kramer | 4969310 | 2013-02-04 15:19:18 +0000 | [diff] [blame] | 2824 | // Scalar instruction. |
| 2825 | Inputs.push_back(std::make_pair(Scalar1, Scalar2)); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 2826 | else { |
Benjamin Kramer | 4969310 | 2013-02-04 15:19:18 +0000 | [diff] [blame] | 2827 | // For vectors extract each constant element into Inputs so we can constant |
| 2828 | // fold them individually. |
| 2829 | BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1); |
| 2830 | BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2); |
| 2831 | if (!BV1 || !BV2) |
| 2832 | return SDValue(); |
| 2833 | |
| 2834 | assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!"); |
| 2835 | |
| 2836 | for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) { |
| 2837 | ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I)); |
| 2838 | ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I)); |
| 2839 | if (!V1 || !V2) // Not a constant, bail. |
| 2840 | return SDValue(); |
| 2841 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 2842 | if (V1->isOpaque() || V2->isOpaque()) |
| 2843 | return SDValue(); |
| 2844 | |
Benjamin Kramer | 4969310 | 2013-02-04 15:19:18 +0000 | [diff] [blame] | 2845 | // Avoid BUILD_VECTOR nodes that perform implicit truncation. |
| 2846 | // FIXME: This is valid and could be handled by truncating the APInts. |
| 2847 | if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT) |
| 2848 | return SDValue(); |
| 2849 | |
| 2850 | Inputs.push_back(std::make_pair(V1, V2)); |
| 2851 | } |
Bill Wendling | 688d1c4 | 2008-09-24 10:16:24 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
Benjamin Kramer | 4969310 | 2013-02-04 15:19:18 +0000 | [diff] [blame] | 2854 | // We have a number of constant values, constant fold them element by element. |
| 2855 | for (unsigned I = 0, E = Inputs.size(); I != E; ++I) { |
| 2856 | const APInt &C1 = Inputs[I].first->getAPIntValue(); |
| 2857 | const APInt &C2 = Inputs[I].second->getAPIntValue(); |
| 2858 | |
| 2859 | switch (Opcode) { |
| 2860 | case ISD::ADD: |
| 2861 | Outputs.push_back(getConstant(C1 + C2, SVT)); |
| 2862 | break; |
| 2863 | case ISD::SUB: |
| 2864 | Outputs.push_back(getConstant(C1 - C2, SVT)); |
| 2865 | break; |
| 2866 | case ISD::MUL: |
| 2867 | Outputs.push_back(getConstant(C1 * C2, SVT)); |
| 2868 | break; |
| 2869 | case ISD::UDIV: |
| 2870 | if (!C2.getBoolValue()) |
| 2871 | return SDValue(); |
| 2872 | Outputs.push_back(getConstant(C1.udiv(C2), SVT)); |
| 2873 | break; |
| 2874 | case ISD::UREM: |
| 2875 | if (!C2.getBoolValue()) |
| 2876 | return SDValue(); |
| 2877 | Outputs.push_back(getConstant(C1.urem(C2), SVT)); |
| 2878 | break; |
| 2879 | case ISD::SDIV: |
| 2880 | if (!C2.getBoolValue()) |
| 2881 | return SDValue(); |
| 2882 | Outputs.push_back(getConstant(C1.sdiv(C2), SVT)); |
| 2883 | break; |
| 2884 | case ISD::SREM: |
| 2885 | if (!C2.getBoolValue()) |
| 2886 | return SDValue(); |
| 2887 | Outputs.push_back(getConstant(C1.srem(C2), SVT)); |
| 2888 | break; |
| 2889 | case ISD::AND: |
| 2890 | Outputs.push_back(getConstant(C1 & C2, SVT)); |
| 2891 | break; |
| 2892 | case ISD::OR: |
| 2893 | Outputs.push_back(getConstant(C1 | C2, SVT)); |
| 2894 | break; |
| 2895 | case ISD::XOR: |
| 2896 | Outputs.push_back(getConstant(C1 ^ C2, SVT)); |
| 2897 | break; |
| 2898 | case ISD::SHL: |
| 2899 | Outputs.push_back(getConstant(C1 << C2, SVT)); |
| 2900 | break; |
| 2901 | case ISD::SRL: |
| 2902 | Outputs.push_back(getConstant(C1.lshr(C2), SVT)); |
| 2903 | break; |
| 2904 | case ISD::SRA: |
| 2905 | Outputs.push_back(getConstant(C1.ashr(C2), SVT)); |
| 2906 | break; |
| 2907 | case ISD::ROTL: |
| 2908 | Outputs.push_back(getConstant(C1.rotl(C2), SVT)); |
| 2909 | break; |
| 2910 | case ISD::ROTR: |
| 2911 | Outputs.push_back(getConstant(C1.rotr(C2), SVT)); |
| 2912 | break; |
| 2913 | default: |
| 2914 | return SDValue(); |
| 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | // Handle the scalar case first. |
Silviu Baranga | 0206683 | 2013-04-25 09:32:33 +0000 | [diff] [blame] | 2919 | if (Scalar1 && Scalar2) |
Benjamin Kramer | 4969310 | 2013-02-04 15:19:18 +0000 | [diff] [blame] | 2920 | return Outputs.back(); |
| 2921 | |
| 2922 | // Otherwise build a big vector out of the scalar elements we generated. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2923 | return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs.data(), |
Benjamin Kramer | 4969310 | 2013-02-04 15:19:18 +0000 | [diff] [blame] | 2924 | Outputs.size()); |
Bill Wendling | 688d1c4 | 2008-09-24 10:16:24 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2927 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1, |
Benjamin Kramer | 4969310 | 2013-02-04 15:19:18 +0000 | [diff] [blame] | 2928 | SDValue N2) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2929 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
| 2930 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode()); |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 2931 | switch (Opcode) { |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 2932 | default: break; |
Chris Lattner | 39908e0 | 2005-01-19 18:01:40 +0000 | [diff] [blame] | 2933 | case ISD::TokenFactor: |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2934 | assert(VT == MVT::Other && N1.getValueType() == MVT::Other && |
| 2935 | N2.getValueType() == MVT::Other && "Invalid token factor!"); |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 2936 | // Fold trivial token factors. |
| 2937 | if (N1.getOpcode() == ISD::EntryToken) return N2; |
| 2938 | if (N2.getOpcode() == ISD::EntryToken) return N1; |
Dan Gohman | 38ac062 | 2008-10-01 15:11:19 +0000 | [diff] [blame] | 2939 | if (N1 == N2) return N1; |
Chris Lattner | 39908e0 | 2005-01-19 18:01:40 +0000 | [diff] [blame] | 2940 | break; |
Dan Gohman | 7f8613e | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2941 | case ISD::CONCAT_VECTORS: |
Nadav Rotem | b87bdac | 2012-07-15 08:38:23 +0000 | [diff] [blame] | 2942 | // Concat of UNDEFs is UNDEF. |
| 2943 | if (N1.getOpcode() == ISD::UNDEF && |
| 2944 | N2.getOpcode() == ISD::UNDEF) |
| 2945 | return getUNDEF(VT); |
| 2946 | |
Dan Gohman | 7f8613e | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2947 | // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to |
| 2948 | // one big BUILD_VECTOR. |
| 2949 | if (N1.getOpcode() == ISD::BUILD_VECTOR && |
| 2950 | N2.getOpcode() == ISD::BUILD_VECTOR) { |
Gabor Greif | 481c4c0 | 2010-07-22 17:18:03 +0000 | [diff] [blame] | 2951 | SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), |
| 2952 | N1.getNode()->op_end()); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 2953 | Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end()); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 2954 | return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size()); |
Dan Gohman | 7f8613e | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2955 | } |
| 2956 | break; |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 2957 | case ISD::AND: |
Dale Johannesen | 7899551 | 2010-05-15 18:38:02 +0000 | [diff] [blame] | 2958 | assert(VT.isInteger() && "This operator does not apply to FP types!"); |
| 2959 | assert(N1.getValueType() == N2.getValueType() && |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 2960 | N1.getValueType() == VT && "Binary operator types must match!"); |
| 2961 | // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's |
| 2962 | // worth handling here. |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2963 | if (N2C && N2C->isNullValue()) |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 2964 | return N2; |
Chris Lattner | 9967c15 | 2008-01-26 01:05:42 +0000 | [diff] [blame] | 2965 | if (N2C && N2C->isAllOnesValue()) // X & -1 -> X |
| 2966 | return N1; |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 2967 | break; |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 2968 | case ISD::OR: |
| 2969 | case ISD::XOR: |
Dan Gohman | 33b625b | 2008-06-02 22:27:05 +0000 | [diff] [blame] | 2970 | case ISD::ADD: |
| 2971 | case ISD::SUB: |
Dale Johannesen | 7899551 | 2010-05-15 18:38:02 +0000 | [diff] [blame] | 2972 | assert(VT.isInteger() && "This operator does not apply to FP types!"); |
| 2973 | assert(N1.getValueType() == N2.getValueType() && |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 2974 | N1.getValueType() == VT && "Binary operator types must match!"); |
Dan Gohman | 33b625b | 2008-06-02 22:27:05 +0000 | [diff] [blame] | 2975 | // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so |
| 2976 | // it's worth handling here. |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 2977 | if (N2C && N2C->isNullValue()) |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 2978 | return N1; |
| 2979 | break; |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 2980 | case ISD::UDIV: |
| 2981 | case ISD::UREM: |
Chris Lattner | e5eb6f8 | 2005-05-15 05:39:08 +0000 | [diff] [blame] | 2982 | case ISD::MULHU: |
| 2983 | case ISD::MULHS: |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 2984 | case ISD::MUL: |
| 2985 | case ISD::SDIV: |
| 2986 | case ISD::SREM: |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 2987 | assert(VT.isInteger() && "This operator does not apply to FP types!"); |
Dale Johannesen | 7899551 | 2010-05-15 18:38:02 +0000 | [diff] [blame] | 2988 | assert(N1.getValueType() == N2.getValueType() && |
| 2989 | N1.getValueType() == VT && "Binary operator types must match!"); |
| 2990 | break; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2991 | case ISD::FADD: |
| 2992 | case ISD::FSUB: |
| 2993 | case ISD::FMUL: |
| 2994 | case ISD::FDIV: |
| 2995 | case ISD::FREM: |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2996 | if (getTarget().Options.UnsafeFPMath) { |
Dan Gohman | a90c8e6 | 2009-01-23 19:10:37 +0000 | [diff] [blame] | 2997 | if (Opcode == ISD::FADD) { |
| 2998 | // 0+x --> x |
| 2999 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) |
| 3000 | if (CFP->getValueAPF().isZero()) |
| 3001 | return N2; |
| 3002 | // x+0 --> x |
| 3003 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) |
| 3004 | if (CFP->getValueAPF().isZero()) |
| 3005 | return N1; |
| 3006 | } else if (Opcode == ISD::FSUB) { |
| 3007 | // x-0 --> x |
| 3008 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) |
| 3009 | if (CFP->getValueAPF().isZero()) |
| 3010 | return N1; |
Michael Ilseman | 10def39 | 2012-09-10 17:00:37 +0000 | [diff] [blame] | 3011 | } else if (Opcode == ISD::FMUL) { |
| 3012 | ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1); |
| 3013 | SDValue V = N2; |
| 3014 | |
| 3015 | // If the first operand isn't the constant, try the second |
| 3016 | if (!CFP) { |
| 3017 | CFP = dyn_cast<ConstantFPSDNode>(N2); |
| 3018 | V = N1; |
| 3019 | } |
| 3020 | |
| 3021 | if (CFP) { |
| 3022 | // 0*x --> 0 |
| 3023 | if (CFP->isZero()) |
| 3024 | return SDValue(CFP,0); |
| 3025 | // 1*x --> x |
| 3026 | if (CFP->isExactlyValue(1.0)) |
| 3027 | return V; |
| 3028 | } |
Dan Gohman | a90c8e6 | 2009-01-23 19:10:37 +0000 | [diff] [blame] | 3029 | } |
Dan Gohman | 760f86f | 2009-01-22 21:58:43 +0000 | [diff] [blame] | 3030 | } |
Dale Johannesen | 7899551 | 2010-05-15 18:38:02 +0000 | [diff] [blame] | 3031 | assert(VT.isFloatingPoint() && "This operator only applies to FP types!"); |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 3032 | assert(N1.getValueType() == N2.getValueType() && |
| 3033 | N1.getValueType() == VT && "Binary operator types must match!"); |
| 3034 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3035 | case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match. |
| 3036 | assert(N1.getValueType() == VT && |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3037 | N1.getValueType().isFloatingPoint() && |
| 3038 | N2.getValueType().isFloatingPoint() && |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3039 | "Invalid FCOPYSIGN!"); |
| 3040 | break; |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 3041 | case ISD::SHL: |
| 3042 | case ISD::SRA: |
| 3043 | case ISD::SRL: |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 3044 | case ISD::ROTL: |
| 3045 | case ISD::ROTR: |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 3046 | assert(VT == N1.getValueType() && |
| 3047 | "Shift operators return type must be the same as their first arg"); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3048 | assert(VT.isInteger() && N2.getValueType().isInteger() && |
Chris Lattner | 349db17 | 2008-07-02 17:01:57 +0000 | [diff] [blame] | 3049 | "Shifts only work on integers"); |
Michael Liao | a6b20ce | 2013-03-01 18:40:30 +0000 | [diff] [blame] | 3050 | assert((!VT.isVector() || VT == N2.getValueType()) && |
| 3051 | "Vector shift amounts must be in the same as their first arg"); |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 3052 | // Verify that the shift amount VT is bit enough to hold valid shift |
| 3053 | // amounts. This catches things like trying to shift an i1024 value by an |
| 3054 | // i8, which is easy to fall into in generic code that uses |
| 3055 | // TLI.getShiftAmount(). |
| 3056 | assert(N2.getValueType().getSizeInBits() >= |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3057 | Log2_32_Ceil(N1.getValueType().getSizeInBits()) && |
Chris Lattner | e075118 | 2011-02-13 19:09:16 +0000 | [diff] [blame] | 3058 | "Invalid use of small shift amount with oversized value!"); |
Chris Lattner | 349db17 | 2008-07-02 17:01:57 +0000 | [diff] [blame] | 3059 | |
| 3060 | // Always fold shifts of i1 values so the code generator doesn't need to |
| 3061 | // handle them. Since we know the size of the shift has to be less than the |
| 3062 | // size of the value, the shift/rotate count is guaranteed to be zero. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3063 | if (VT == MVT::i1) |
Chris Lattner | 349db17 | 2008-07-02 17:01:57 +0000 | [diff] [blame] | 3064 | return N1; |
Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 3065 | if (N2C && N2C->isNullValue()) |
| 3066 | return N1; |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 3067 | break; |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3068 | case ISD::FP_ROUND_INREG: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3069 | EVT EVT = cast<VTSDNode>(N2)->getVT(); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3070 | assert(VT == N1.getValueType() && "Not an inreg round!"); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3071 | assert(VT.isFloatingPoint() && EVT.isFloatingPoint() && |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3072 | "Cannot FP_ROUND_INREG integer types"); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3073 | assert(EVT.isVector() == VT.isVector() && |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3074 | "FP_ROUND_INREG type should be vector iff the operand " |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3075 | "type is vector!"); |
| 3076 | assert((!EVT.isVector() || |
| 3077 | EVT.getVectorNumElements() == VT.getVectorNumElements()) && |
| 3078 | "Vector element counts must match in FP_ROUND_INREG"); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 3079 | assert(EVT.bitsLE(VT) && "Not rounding down!"); |
Duncan Sands | 17001ce | 2011-10-18 12:44:00 +0000 | [diff] [blame] | 3080 | (void)EVT; |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3081 | if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding. |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3082 | break; |
| 3083 | } |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3084 | case ISD::FP_ROUND: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3085 | assert(VT.isFloatingPoint() && |
| 3086 | N1.getValueType().isFloatingPoint() && |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 3087 | VT.bitsLE(N1.getValueType()) && |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3088 | isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!"); |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3089 | if (N1.getValueType() == VT) return N1; // noop conversion. |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3090 | break; |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 3091 | case ISD::AssertSext: |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3092 | case ISD::AssertZext: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3093 | EVT EVT = cast<VTSDNode>(N2)->getVT(); |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3094 | assert(VT == N1.getValueType() && "Not an inreg extend!"); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3095 | assert(VT.isInteger() && EVT.isInteger() && |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3096 | "Cannot *_EXTEND_INREG FP types"); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3097 | assert(!EVT.isVector() && |
| 3098 | "AssertSExt/AssertZExt type should be the vector element type " |
| 3099 | "rather than the vector type!"); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 3100 | assert(EVT.bitsLE(VT) && "Not extending!"); |
Duncan Sands | d885dbd | 2008-02-10 10:08:52 +0000 | [diff] [blame] | 3101 | if (VT == EVT) return N1; // noop assertion. |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3102 | break; |
| 3103 | } |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3104 | case ISD::SIGN_EXTEND_INREG: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3105 | EVT EVT = cast<VTSDNode>(N2)->getVT(); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3106 | assert(VT == N1.getValueType() && "Not an inreg extend!"); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3107 | assert(VT.isInteger() && EVT.isInteger() && |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3108 | "Cannot *_EXTEND_INREG FP types"); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3109 | assert(EVT.isVector() == VT.isVector() && |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 3110 | "SIGN_EXTEND_INREG type should be vector iff the operand " |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3111 | "type is vector!"); |
| 3112 | assert((!EVT.isVector() || |
| 3113 | EVT.getVectorNumElements() == VT.getVectorNumElements()) && |
| 3114 | "Vector element counts must match in SIGN_EXTEND_INREG"); |
| 3115 | assert(EVT.bitsLE(VT) && "Not extending!"); |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3116 | if (EVT == VT) return N1; // Not actually extending |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3117 | |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3118 | if (N1C) { |
Dan Gohman | 6c23150 | 2008-02-29 01:47:35 +0000 | [diff] [blame] | 3119 | APInt Val = N1C->getAPIntValue(); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3120 | unsigned FromBits = EVT.getScalarType().getSizeInBits(); |
Dan Gohman | 6c23150 | 2008-02-29 01:47:35 +0000 | [diff] [blame] | 3121 | Val <<= Val.getBitWidth()-FromBits; |
Evan Cheng | 433f6f6 | 2008-03-06 08:20:51 +0000 | [diff] [blame] | 3122 | Val = Val.ashr(Val.getBitWidth()-FromBits); |
Chris Lattner | b9ebacd | 2006-05-06 23:05:41 +0000 | [diff] [blame] | 3123 | return getConstant(Val, VT); |
| 3124 | } |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3125 | break; |
| 3126 | } |
| 3127 | case ISD::EXTRACT_VECTOR_ELT: |
Chris Lattner | f3ba434 | 2008-03-08 23:43:36 +0000 | [diff] [blame] | 3128 | // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF. |
| 3129 | if (N1.getOpcode() == ISD::UNDEF) |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 3130 | return getUNDEF(VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3131 | |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3132 | // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is |
| 3133 | // expanding copies of large vectors from registers. |
Dan Gohman | 6ab6422 | 2008-08-13 21:51:37 +0000 | [diff] [blame] | 3134 | if (N2C && |
| 3135 | N1.getOpcode() == ISD::CONCAT_VECTORS && |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3136 | N1.getNumOperands() > 0) { |
| 3137 | unsigned Factor = |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3138 | N1.getOperand(0).getValueType().getVectorNumElements(); |
Dale Johannesen | dbfd8db | 2009-02-03 01:55:44 +0000 | [diff] [blame] | 3139 | return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3140 | N1.getOperand(N2C->getZExtValue() / Factor), |
| 3141 | getConstant(N2C->getZExtValue() % Factor, |
| 3142 | N2.getValueType())); |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is |
| 3146 | // expanding large vector constants. |
Bob Wilson | b1303d0 | 2009-04-13 22:05:19 +0000 | [diff] [blame] | 3147 | if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) { |
| 3148 | SDValue Elt = N1.getOperand(N2C->getZExtValue()); |
James Molloy | 8cd08bf | 2012-09-10 14:01:21 +0000 | [diff] [blame] | 3149 | |
| 3150 | if (VT != Elt.getValueType()) |
Bob Wilson | b1303d0 | 2009-04-13 22:05:19 +0000 | [diff] [blame] | 3151 | // If the vector element type is not legal, the BUILD_VECTOR operands |
James Molloy | 8cd08bf | 2012-09-10 14:01:21 +0000 | [diff] [blame] | 3152 | // are promoted and implicitly truncated, and the result implicitly |
| 3153 | // extended. Make that explicit here. |
| 3154 | Elt = getAnyExtOrTrunc(Elt, DL, VT); |
Michael Ilseman | 06b9690 | 2012-09-10 16:56:31 +0000 | [diff] [blame] | 3155 | |
Bob Wilson | b1303d0 | 2009-04-13 22:05:19 +0000 | [diff] [blame] | 3156 | return Elt; |
| 3157 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3158 | |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3159 | // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector |
| 3160 | // operations are lowered to scalars. |
Dan Gohman | 6ab6422 | 2008-08-13 21:51:37 +0000 | [diff] [blame] | 3161 | if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) { |
Mon P Wang | 87c46d8 | 2010-02-01 22:15:09 +0000 | [diff] [blame] | 3162 | // If the indices are the same, return the inserted element else |
| 3163 | // if the indices are known different, extract the element from |
Dan Gohman | 197e88f | 2009-01-29 16:10:46 +0000 | [diff] [blame] | 3164 | // the original vector. |
Bill Wendling | d71bb56 | 2010-04-30 22:19:17 +0000 | [diff] [blame] | 3165 | SDValue N1Op2 = N1.getOperand(2); |
| 3166 | ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode()); |
| 3167 | |
| 3168 | if (N1Op2C && N2C) { |
| 3169 | if (N1Op2C->getZExtValue() == N2C->getZExtValue()) { |
| 3170 | if (VT == N1.getOperand(1).getValueType()) |
| 3171 | return N1.getOperand(1); |
| 3172 | else |
| 3173 | return getSExtOrTrunc(N1.getOperand(1), DL, VT); |
| 3174 | } |
| 3175 | |
Dale Johannesen | dbfd8db | 2009-02-03 01:55:44 +0000 | [diff] [blame] | 3176 | return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2); |
Bill Wendling | d71bb56 | 2010-04-30 22:19:17 +0000 | [diff] [blame] | 3177 | } |
Dan Gohman | 6ab6422 | 2008-08-13 21:51:37 +0000 | [diff] [blame] | 3178 | } |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3179 | break; |
| 3180 | case ISD::EXTRACT_ELEMENT: |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3181 | assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!"); |
Duncan Sands | 041cde2 | 2008-06-25 20:24:48 +0000 | [diff] [blame] | 3182 | assert(!N1.getValueType().isVector() && !VT.isVector() && |
| 3183 | (N1.getValueType().isInteger() == VT.isInteger()) && |
Eli Friedman | 6cdc1f4 | 2011-08-02 18:38:35 +0000 | [diff] [blame] | 3184 | N1.getValueType() != VT && |
Duncan Sands | 041cde2 | 2008-06-25 20:24:48 +0000 | [diff] [blame] | 3185 | "Wrong types for EXTRACT_ELEMENT!"); |
Duncan Sands | 25eb043 | 2008-03-12 20:30:08 +0000 | [diff] [blame] | 3186 | |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3187 | // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding |
| 3188 | // 64-bit integers into 32-bit parts. Instead of building the extract of |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3189 | // the BUILD_PAIR, only to have legalize rip it apart, just do it now. |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3190 | if (N1.getOpcode() == ISD::BUILD_PAIR) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3191 | return N1.getOperand(N2C->getZExtValue()); |
Duncan Sands | 25eb043 | 2008-03-12 20:30:08 +0000 | [diff] [blame] | 3192 | |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3193 | // EXTRACT_ELEMENT of a constant int is also very common. |
| 3194 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3195 | unsigned ElementSize = VT.getSizeInBits(); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3196 | unsigned Shift = ElementSize * N2C->getZExtValue(); |
Dan Gohman | 4c931fc | 2008-03-24 16:38:05 +0000 | [diff] [blame] | 3197 | APInt ShiftedVal = C->getAPIntValue().lshr(Shift); |
| 3198 | return getConstant(ShiftedVal.trunc(ElementSize), VT); |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3199 | } |
| 3200 | break; |
David Greene | 9158509 | 2011-01-26 15:38:49 +0000 | [diff] [blame] | 3201 | case ISD::EXTRACT_SUBVECTOR: { |
| 3202 | SDValue Index = N2; |
| 3203 | if (VT.isSimple() && N1.getValueType().isSimple()) { |
| 3204 | assert(VT.isVector() && N1.getValueType().isVector() && |
| 3205 | "Extract subvector VTs must be a vectors!"); |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 3206 | assert(VT.getVectorElementType() == |
| 3207 | N1.getValueType().getVectorElementType() && |
David Greene | 9158509 | 2011-01-26 15:38:49 +0000 | [diff] [blame] | 3208 | "Extract subvector VTs must have the same element type!"); |
Craig Topper | 0ff1190 | 2013-08-15 02:44:19 +0000 | [diff] [blame] | 3209 | assert(VT.getSimpleVT() <= N1.getSimpleValueType() && |
David Greene | 9158509 | 2011-01-26 15:38:49 +0000 | [diff] [blame] | 3210 | "Extract subvector must be from larger vector to smaller vector!"); |
| 3211 | |
Matt Beaumont-Gay | 9a14a36 | 2011-02-01 22:12:50 +0000 | [diff] [blame] | 3212 | if (isa<ConstantSDNode>(Index.getNode())) { |
| 3213 | assert((VT.getVectorNumElements() + |
| 3214 | cast<ConstantSDNode>(Index.getNode())->getZExtValue() |
David Greene | 9158509 | 2011-01-26 15:38:49 +0000 | [diff] [blame] | 3215 | <= N1.getValueType().getVectorNumElements()) |
| 3216 | && "Extract subvector overflow!"); |
| 3217 | } |
| 3218 | |
| 3219 | // Trivial extraction. |
Craig Topper | 0ff1190 | 2013-08-15 02:44:19 +0000 | [diff] [blame] | 3220 | if (VT.getSimpleVT() == N1.getSimpleValueType()) |
David Greene | 9158509 | 2011-01-26 15:38:49 +0000 | [diff] [blame] | 3221 | return N1; |
| 3222 | } |
Duncan Sands | f83b1f6 | 2008-02-20 17:38:09 +0000 | [diff] [blame] | 3223 | break; |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3224 | } |
David Greene | 9158509 | 2011-01-26 15:38:49 +0000 | [diff] [blame] | 3225 | } |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3226 | |
Benjamin Kramer | 4969310 | 2013-02-04 15:19:18 +0000 | [diff] [blame] | 3227 | // Perform trivial constant folding. |
| 3228 | SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode()); |
| 3229 | if (SV.getNode()) return SV; |
| 3230 | |
| 3231 | // Canonicalize constant to RHS if commutative. |
| 3232 | if (N1C && !N2C && isCommutativeBinOp(Opcode)) { |
| 3233 | std::swap(N1C, N2C); |
| 3234 | std::swap(N1, N2); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3237 | // Constant fold FP operations. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3238 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode()); |
| 3239 | ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode()); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3240 | if (N1CFP) { |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3241 | if (!N2CFP && isCommutativeBinOp(Opcode)) { |
Benjamin Kramer | 4969310 | 2013-02-04 15:19:18 +0000 | [diff] [blame] | 3242 | // Canonicalize constant to RHS if commutative. |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3243 | std::swap(N1CFP, N2CFP); |
| 3244 | std::swap(N1, N2); |
Ulrich Weigand | e669c93 | 2012-10-29 18:35:49 +0000 | [diff] [blame] | 3245 | } else if (N2CFP) { |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 3246 | APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF(); |
| 3247 | APFloat::opStatus s; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3248 | switch (Opcode) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3249 | case ISD::FADD: |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 3250 | s = V1.add(V2, APFloat::rmNearestTiesToEven); |
Chris Lattner | 8e1f7ac | 2008-01-22 19:09:33 +0000 | [diff] [blame] | 3251 | if (s != APFloat::opInvalidOp) |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 3252 | return getConstantFP(V1, VT); |
| 3253 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3254 | case ISD::FSUB: |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 3255 | s = V1.subtract(V2, APFloat::rmNearestTiesToEven); |
| 3256 | if (s!=APFloat::opInvalidOp) |
| 3257 | return getConstantFP(V1, VT); |
| 3258 | break; |
| 3259 | case ISD::FMUL: |
| 3260 | s = V1.multiply(V2, APFloat::rmNearestTiesToEven); |
| 3261 | if (s!=APFloat::opInvalidOp) |
| 3262 | return getConstantFP(V1, VT); |
| 3263 | break; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3264 | case ISD::FDIV: |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 3265 | s = V1.divide(V2, APFloat::rmNearestTiesToEven); |
| 3266 | if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero) |
| 3267 | return getConstantFP(V1, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3268 | break; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3269 | case ISD::FREM : |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 3270 | s = V1.mod(V2, APFloat::rmNearestTiesToEven); |
| 3271 | if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero) |
| 3272 | return getConstantFP(V1, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3273 | break; |
Dale Johannesen | c4dd3c3 | 2007-08-31 23:34:27 +0000 | [diff] [blame] | 3274 | case ISD::FCOPYSIGN: |
| 3275 | V1.copySign(V2); |
| 3276 | return getConstantFP(V1, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3277 | default: break; |
| 3278 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3279 | } |
Owen Anderson | 06886aa | 2012-04-10 22:46:53 +0000 | [diff] [blame] | 3280 | |
| 3281 | if (Opcode == ISD::FP_ROUND) { |
| 3282 | APFloat V = N1CFP->getValueAPF(); // make copy |
| 3283 | bool ignored; |
| 3284 | // This can return overflow, underflow, or inexact; we don't care. |
| 3285 | // FIXME need to be more flexible about rounding mode. |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 3286 | (void)V.convert(EVTToAPFloatSemantics(VT), |
Owen Anderson | 06886aa | 2012-04-10 22:46:53 +0000 | [diff] [blame] | 3287 | APFloat::rmNearestTiesToEven, &ignored); |
| 3288 | return getConstantFP(V, VT); |
| 3289 | } |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3290 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3291 | |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 3292 | // Canonicalize an UNDEF to the RHS, even over a constant. |
| 3293 | if (N1.getOpcode() == ISD::UNDEF) { |
| 3294 | if (isCommutativeBinOp(Opcode)) { |
| 3295 | std::swap(N1, N2); |
| 3296 | } else { |
| 3297 | switch (Opcode) { |
| 3298 | case ISD::FP_ROUND_INREG: |
| 3299 | case ISD::SIGN_EXTEND_INREG: |
| 3300 | case ISD::SUB: |
| 3301 | case ISD::FSUB: |
| 3302 | case ISD::FDIV: |
| 3303 | case ISD::FREM: |
Chris Lattner | 2cfd674 | 2006-05-08 17:29:49 +0000 | [diff] [blame] | 3304 | case ISD::SRA: |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 3305 | return N1; // fold op(undef, arg2) -> undef |
| 3306 | case ISD::UDIV: |
| 3307 | case ISD::SDIV: |
| 3308 | case ISD::UREM: |
| 3309 | case ISD::SREM: |
Chris Lattner | 2cfd674 | 2006-05-08 17:29:49 +0000 | [diff] [blame] | 3310 | case ISD::SRL: |
| 3311 | case ISD::SHL: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3312 | if (!VT.isVector()) |
Chris Lattner | 964dd86 | 2007-04-25 00:00:45 +0000 | [diff] [blame] | 3313 | return getConstant(0, VT); // fold op(undef, arg2) -> 0 |
| 3314 | // For vectors, we can't easily build an all zero vector, just return |
| 3315 | // the LHS. |
| 3316 | return N2; |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 3317 | } |
| 3318 | } |
| 3319 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3320 | |
| 3321 | // Fold a bunch of operators when the RHS is undef. |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 3322 | if (N2.getOpcode() == ISD::UNDEF) { |
| 3323 | switch (Opcode) { |
Evan Cheng | 26471c4 | 2008-03-25 20:08:07 +0000 | [diff] [blame] | 3324 | case ISD::XOR: |
| 3325 | if (N1.getOpcode() == ISD::UNDEF) |
| 3326 | // Handle undef ^ undef -> 0 special case. This is a common |
| 3327 | // idiom (misuse). |
| 3328 | return getConstant(0, VT); |
| 3329 | // fallthrough |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 3330 | case ISD::ADD: |
Chris Lattner | 175415e | 2007-03-04 20:01:46 +0000 | [diff] [blame] | 3331 | case ISD::ADDC: |
| 3332 | case ISD::ADDE: |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 3333 | case ISD::SUB: |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 3334 | case ISD::UDIV: |
| 3335 | case ISD::SDIV: |
| 3336 | case ISD::UREM: |
| 3337 | case ISD::SREM: |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 3338 | return N2; // fold op(arg1, undef) -> undef |
Dan Gohman | 77b81fe | 2009-06-04 17:12:12 +0000 | [diff] [blame] | 3339 | case ISD::FADD: |
| 3340 | case ISD::FSUB: |
| 3341 | case ISD::FMUL: |
| 3342 | case ISD::FDIV: |
| 3343 | case ISD::FREM: |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 3344 | if (getTarget().Options.UnsafeFPMath) |
Dan Gohman | 77b81fe | 2009-06-04 17:12:12 +0000 | [diff] [blame] | 3345 | return N2; |
| 3346 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3347 | case ISD::MUL: |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 3348 | case ISD::AND: |
Chris Lattner | 2cfd674 | 2006-05-08 17:29:49 +0000 | [diff] [blame] | 3349 | case ISD::SRL: |
| 3350 | case ISD::SHL: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3351 | if (!VT.isVector()) |
Chris Lattner | 964dd86 | 2007-04-25 00:00:45 +0000 | [diff] [blame] | 3352 | return getConstant(0, VT); // fold op(arg1, undef) -> 0 |
| 3353 | // For vectors, we can't easily build an all zero vector, just return |
| 3354 | // the LHS. |
| 3355 | return N1; |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 3356 | case ISD::OR: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3357 | if (!VT.isVector()) |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 3358 | return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT); |
Chris Lattner | 964dd86 | 2007-04-25 00:00:45 +0000 | [diff] [blame] | 3359 | // For vectors, we can't easily build an all one vector, just return |
| 3360 | // the LHS. |
| 3361 | return N1; |
Chris Lattner | 2cfd674 | 2006-05-08 17:29:49 +0000 | [diff] [blame] | 3362 | case ISD::SRA: |
| 3363 | return N1; |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 3364 | } |
| 3365 | } |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3366 | |
Chris Lattner | 27e9b41 | 2005-05-11 18:57:39 +0000 | [diff] [blame] | 3367 | // Memoize this node if possible. |
| 3368 | SDNode *N; |
Chris Lattner | 0b3e525 | 2006-08-15 19:11:05 +0000 | [diff] [blame] | 3369 | SDVTList VTs = getVTList(VT); |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 3370 | if (VT != MVT::Glue) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3371 | SDValue Ops[] = { N1, N2 }; |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 3372 | FoldingSetNodeID ID; |
Chris Lattner | 63e3f14 | 2007-02-04 07:28:00 +0000 | [diff] [blame] | 3373 | AddNodeIDNode(ID, Opcode, VTs, Ops, 2); |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 3374 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 3375 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3376 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 3377 | |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 3378 | N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(), |
| 3379 | DL.getDebugLoc(), VTs, N1, N2); |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 3380 | CSEMap.InsertNode(N, IP); |
Chris Lattner | 27e9b41 | 2005-05-11 18:57:39 +0000 | [diff] [blame] | 3381 | } else { |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 3382 | N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(), |
| 3383 | DL.getDebugLoc(), VTs, N1, N2); |
Chris Lattner | 27e9b41 | 2005-05-11 18:57:39 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3386 | AllNodes.push_back(N); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 3387 | #ifndef NDEBUG |
Duncan Sands | 59d2dad | 2010-11-20 11:25:00 +0000 | [diff] [blame] | 3388 | VerifySDNode(N); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 3389 | #endif |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3390 | return SDValue(N, 0); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3393 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 3394 | SDValue N1, SDValue N2, SDValue N3) { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3395 | // Perform various simplifications. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3396 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode()); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3397 | switch (Opcode) { |
Owen Anderson | 58dcd20 | 2013-05-09 22:27:13 +0000 | [diff] [blame] | 3398 | case ISD::FMA: { |
| 3399 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); |
| 3400 | ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2); |
| 3401 | ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3); |
| 3402 | if (N1CFP && N2CFP && N3CFP) { |
| 3403 | APFloat V1 = N1CFP->getValueAPF(); |
| 3404 | const APFloat &V2 = N2CFP->getValueAPF(); |
| 3405 | const APFloat &V3 = N3CFP->getValueAPF(); |
| 3406 | APFloat::opStatus s = |
| 3407 | V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven); |
| 3408 | if (s != APFloat::opInvalidOp) |
| 3409 | return getConstantFP(V1, VT); |
| 3410 | } |
| 3411 | break; |
| 3412 | } |
Dan Gohman | 7f8613e | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 3413 | case ISD::CONCAT_VECTORS: |
| 3414 | // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to |
| 3415 | // one big BUILD_VECTOR. |
| 3416 | if (N1.getOpcode() == ISD::BUILD_VECTOR && |
| 3417 | N2.getOpcode() == ISD::BUILD_VECTOR && |
| 3418 | N3.getOpcode() == ISD::BUILD_VECTOR) { |
Gabor Greif | 481c4c0 | 2010-07-22 17:18:03 +0000 | [diff] [blame] | 3419 | SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), |
| 3420 | N1.getNode()->op_end()); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 3421 | Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end()); |
| 3422 | Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end()); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 3423 | return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size()); |
Dan Gohman | 7f8613e | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 3424 | } |
| 3425 | break; |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3426 | case ISD::SETCC: { |
Chris Lattner | 51dabfb | 2006-10-14 00:41:01 +0000 | [diff] [blame] | 3427 | // Use FoldSetCC to simplify SETCC's. |
Dale Johannesen | ff97d4f | 2009-02-03 00:47:48 +0000 | [diff] [blame] | 3428 | SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3429 | if (Simp.getNode()) return Simp; |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3430 | break; |
| 3431 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3432 | case ISD::SELECT: |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 3433 | if (N1C) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3434 | if (N1C->getZExtValue()) |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 3435 | return N2; // select true, X, Y -> X |
| 3436 | return N3; // select false, X, Y -> Y |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 3437 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3438 | |
| 3439 | if (N2 == N3) return N2; // select C, X, X -> X |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3440 | break; |
Chris Lattner | fb194b9 | 2006-03-19 23:56:04 +0000 | [diff] [blame] | 3441 | case ISD::VECTOR_SHUFFLE: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3442 | llvm_unreachable("should use getVectorShuffle constructor!"); |
David Greene | cfe33c4 | 2011-01-26 19:13:22 +0000 | [diff] [blame] | 3443 | case ISD::INSERT_SUBVECTOR: { |
| 3444 | SDValue Index = N3; |
| 3445 | if (VT.isSimple() && N1.getValueType().isSimple() |
| 3446 | && N2.getValueType().isSimple()) { |
| 3447 | assert(VT.isVector() && N1.getValueType().isVector() && |
| 3448 | N2.getValueType().isVector() && |
| 3449 | "Insert subvector VTs must be a vectors"); |
| 3450 | assert(VT == N1.getValueType() && |
| 3451 | "Dest and insert subvector source types must match!"); |
Craig Topper | 0ff1190 | 2013-08-15 02:44:19 +0000 | [diff] [blame] | 3452 | assert(N2.getSimpleValueType() <= N1.getSimpleValueType() && |
David Greene | cfe33c4 | 2011-01-26 19:13:22 +0000 | [diff] [blame] | 3453 | "Insert subvector must be from smaller vector to larger vector!"); |
Matt Beaumont-Gay | 9a14a36 | 2011-02-01 22:12:50 +0000 | [diff] [blame] | 3454 | if (isa<ConstantSDNode>(Index.getNode())) { |
| 3455 | assert((N2.getValueType().getVectorNumElements() + |
| 3456 | cast<ConstantSDNode>(Index.getNode())->getZExtValue() |
David Greene | cfe33c4 | 2011-01-26 19:13:22 +0000 | [diff] [blame] | 3457 | <= VT.getVectorNumElements()) |
| 3458 | && "Insert subvector overflow!"); |
| 3459 | } |
| 3460 | |
| 3461 | // Trivial insertion. |
Craig Topper | 0ff1190 | 2013-08-15 02:44:19 +0000 | [diff] [blame] | 3462 | if (VT.getSimpleVT() == N2.getSimpleValueType()) |
David Greene | cfe33c4 | 2011-01-26 19:13:22 +0000 | [diff] [blame] | 3463 | return N2; |
| 3464 | } |
| 3465 | break; |
| 3466 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3467 | case ISD::BITCAST: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3468 | // Fold bit_convert nodes from a type to themselves. |
| 3469 | if (N1.getValueType() == VT) |
| 3470 | return N1; |
Chris Lattner | 4829b1c | 2007-04-12 05:58:43 +0000 | [diff] [blame] | 3471 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3472 | } |
| 3473 | |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 3474 | // Memoize node if it doesn't produce a flag. |
| 3475 | SDNode *N; |
Chris Lattner | 0b3e525 | 2006-08-15 19:11:05 +0000 | [diff] [blame] | 3476 | SDVTList VTs = getVTList(VT); |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 3477 | if (VT != MVT::Glue) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3478 | SDValue Ops[] = { N1, N2, N3 }; |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 3479 | FoldingSetNodeID ID; |
Chris Lattner | 63e3f14 | 2007-02-04 07:28:00 +0000 | [diff] [blame] | 3480 | AddNodeIDNode(ID, Opcode, VTs, Ops, 3); |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 3481 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 3482 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3483 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 3484 | |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 3485 | N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(), |
| 3486 | DL.getDebugLoc(), VTs, N1, N2, N3); |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 3487 | CSEMap.InsertNode(N, IP); |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 3488 | } else { |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 3489 | N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(), |
| 3490 | DL.getDebugLoc(), VTs, N1, N2, N3); |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 3491 | } |
Daniel Dunbar | 819309e | 2009-12-16 20:10:05 +0000 | [diff] [blame] | 3492 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3493 | AllNodes.push_back(N); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 3494 | #ifndef NDEBUG |
Duncan Sands | 59d2dad | 2010-11-20 11:25:00 +0000 | [diff] [blame] | 3495 | VerifySDNode(N); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 3496 | #endif |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3497 | return SDValue(N, 0); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3500 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 3501 | SDValue N1, SDValue N2, SDValue N3, |
| 3502 | SDValue N4) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3503 | SDValue Ops[] = { N1, N2, N3, N4 }; |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 3504 | return getNode(Opcode, DL, VT, Ops, 4); |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3507 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 3508 | SDValue N1, SDValue N2, SDValue N3, |
| 3509 | SDValue N4, SDValue N5) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3510 | SDValue Ops[] = { N1, N2, N3, N4, N5 }; |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 3511 | return getNode(Opcode, DL, VT, Ops, 5); |
Chris Lattner | 9fadb4c | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3514 | /// getStackArgumentTokenFactor - Compute a TokenFactor to force all |
| 3515 | /// the incoming stack arguments to be loaded from the stack. |
| 3516 | SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) { |
| 3517 | SmallVector<SDValue, 8> ArgChains; |
| 3518 | |
| 3519 | // Include the original chain at the beginning of the list. When this is |
| 3520 | // used by target LowerCall hooks, this helps legalize find the |
| 3521 | // CALLSEQ_BEGIN node. |
| 3522 | ArgChains.push_back(Chain); |
| 3523 | |
| 3524 | // Add a chain value for each stack argument. |
| 3525 | for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(), |
| 3526 | UE = getEntryNode().getNode()->use_end(); U != UE; ++U) |
| 3527 | if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U)) |
| 3528 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr())) |
| 3529 | if (FI->getIndex() < 0) |
| 3530 | ArgChains.push_back(SDValue(L, 1)); |
| 3531 | |
| 3532 | // Build a tokenfactor for all the chains. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3533 | return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3534 | &ArgChains[0], ArgChains.size()); |
| 3535 | } |
| 3536 | |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3537 | /// getMemsetValue - Vectorized representation of the memset value |
| 3538 | /// operand. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3539 | static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3540 | SDLoc dl) { |
Evan Cheng | f28f8bc | 2010-04-02 19:36:14 +0000 | [diff] [blame] | 3541 | assert(Value.getOpcode() != ISD::UNDEF); |
| 3542 | |
Chris Lattner | 5cf0b6e | 2010-02-24 22:44:06 +0000 | [diff] [blame] | 3543 | unsigned NumBits = VT.getScalarType().getSizeInBits(); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3544 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) { |
Benjamin Kramer | ad4da0f | 2013-02-20 13:00:06 +0000 | [diff] [blame] | 3545 | assert(C->getAPIntValue().getBitWidth() == 8); |
| 3546 | APInt Val = APInt::getSplat(NumBits, C->getAPIntValue()); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3547 | if (VT.isInteger()) |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3548 | return DAG.getConstant(Val, VT); |
Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 3549 | return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3550 | } |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3551 | |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 3552 | Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value); |
Benjamin Kramer | 8c06aa1 | 2011-01-02 19:44:58 +0000 | [diff] [blame] | 3553 | if (NumBits > 8) { |
| 3554 | // Use a multiplication with 0x010101... to extend the input to the |
| 3555 | // required length. |
Benjamin Kramer | ad4da0f | 2013-02-20 13:00:06 +0000 | [diff] [blame] | 3556 | APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01)); |
Benjamin Kramer | 8c06aa1 | 2011-01-02 19:44:58 +0000 | [diff] [blame] | 3557 | Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT)); |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | return Value; |
Rafael Espindola | 5c0d6ed | 2007-10-19 10:41:11 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3563 | /// getMemsetStringVal - Similar to getMemsetValue. Except this is only |
| 3564 | /// used when a memcpy is turned into a memset when the source is a constant |
| 3565 | /// string ptr. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3566 | static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG, |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3567 | const TargetLowering &TLI, StringRef Str) { |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3568 | // Handle vector with all elements zero. |
| 3569 | if (Str.empty()) { |
| 3570 | if (VT.isInteger()) |
| 3571 | return DAG.getConstant(0, VT); |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 3572 | else if (VT == MVT::f32 || VT == MVT::f64) |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3573 | return DAG.getConstantFP(0.0, VT); |
| 3574 | else if (VT.isVector()) { |
| 3575 | unsigned NumElts = VT.getVectorNumElements(); |
| 3576 | MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3577 | return DAG.getNode(ISD::BITCAST, dl, VT, |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3578 | DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(), |
| 3579 | EltVT, NumElts))); |
| 3580 | } else |
| 3581 | llvm_unreachable("Expected type!"); |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3582 | } |
| 3583 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3584 | assert(!VT.isVector() && "Can't handle vector type here!"); |
Evan Cheng | 4ff23d0 | 2013-01-10 22:13:27 +0000 | [diff] [blame] | 3585 | unsigned NumVTBits = VT.getSizeInBits(); |
| 3586 | unsigned NumVTBytes = NumVTBits / 8; |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3587 | unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size())); |
| 3588 | |
Evan Cheng | 4ff23d0 | 2013-01-10 22:13:27 +0000 | [diff] [blame] | 3589 | APInt Val(NumVTBits, 0); |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3590 | if (TLI.isLittleEndian()) { |
| 3591 | for (unsigned i = 0; i != NumBytes; ++i) |
| 3592 | Val |= (uint64_t)(unsigned char)Str[i] << i*8; |
| 3593 | } else { |
| 3594 | for (unsigned i = 0; i != NumBytes; ++i) |
| 3595 | Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3596 | } |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3597 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 3598 | // If the "cost" of materializing the integer immediate is less than the cost |
| 3599 | // of a load, then it is cost effective to turn the load into the immediate. |
| 3600 | Type *Ty = VT.getTypeForEVT(*DAG.getContext()); |
| 3601 | if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty)) |
Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 3602 | return DAG.getConstant(Val, VT); |
| 3603 | return SDValue(0, 0); |
Rafael Espindola | 5c0d6ed | 2007-10-19 10:41:11 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3606 | /// getMemBasePlusOffset - Returns base and offset node for the |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3607 | /// |
Andrew Trick | dd0fb01 | 2013-05-25 03:08:10 +0000 | [diff] [blame] | 3608 | static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl, |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3609 | SelectionDAG &DAG) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3610 | EVT VT = Base.getValueType(); |
Andrew Trick | dd0fb01 | 2013-05-25 03:08:10 +0000 | [diff] [blame] | 3611 | return DAG.getNode(ISD::ADD, dl, |
Dale Johannesen | dbfd8db | 2009-02-03 01:55:44 +0000 | [diff] [blame] | 3612 | VT, Base, DAG.getConstant(Offset, VT)); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3613 | } |
| 3614 | |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3615 | /// isMemSrcFromString - Returns true if memcpy source is a string constant. |
| 3616 | /// |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3617 | static bool isMemSrcFromString(SDValue Src, StringRef &Str) { |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3618 | unsigned SrcDelta = 0; |
| 3619 | GlobalAddressSDNode *G = NULL; |
| 3620 | if (Src.getOpcode() == ISD::GlobalAddress) |
| 3621 | G = cast<GlobalAddressSDNode>(Src); |
| 3622 | else if (Src.getOpcode() == ISD::ADD && |
| 3623 | Src.getOperand(0).getOpcode() == ISD::GlobalAddress && |
| 3624 | Src.getOperand(1).getOpcode() == ISD::Constant) { |
| 3625 | G = cast<GlobalAddressSDNode>(Src.getOperand(0)); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3626 | SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue(); |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3627 | } |
| 3628 | if (!G) |
| 3629 | return false; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3630 | |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3631 | return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false); |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3632 | } |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3633 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3634 | /// FindOptimalMemOpLowering - Determines the optimial series memory ops |
| 3635 | /// to replace the memset / memcpy. Return true if the number of memory ops |
| 3636 | /// is below the threshold. It returns the types of the sequence of |
| 3637 | /// memory ops to perform memset / memcpy by reference. |
| 3638 | static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps, |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3639 | unsigned Limit, uint64_t Size, |
| 3640 | unsigned DstAlign, unsigned SrcAlign, |
Evan Cheng | 946a3a9 | 2012-12-12 02:34:41 +0000 | [diff] [blame] | 3641 | bool IsMemset, |
| 3642 | bool ZeroMemset, |
Evan Cheng | c3b0c34 | 2010-04-08 07:37:57 +0000 | [diff] [blame] | 3643 | bool MemcpyStrSrc, |
Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 3644 | bool AllowOverlap, |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3645 | SelectionDAG &DAG, |
| 3646 | const TargetLowering &TLI) { |
| 3647 | assert((SrcAlign == 0 || SrcAlign >= DstAlign) && |
| 3648 | "Expecting memcpy / memset source to meet alignment requirement!"); |
Eric Christopher | 882e1e1 | 2011-07-06 22:41:18 +0000 | [diff] [blame] | 3649 | // If 'SrcAlign' is zero, that means the memory operation does not need to |
| 3650 | // load the value, i.e. memset or memcpy from constant string. Otherwise, |
| 3651 | // it's the inferred alignment of the source. 'DstAlign', on the other hand, |
| 3652 | // is the specified alignment of the memory operation. If it is zero, that |
| 3653 | // means it's possible to change the alignment of the destination. |
| 3654 | // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does |
| 3655 | // not need to be loaded. |
Evan Cheng | f28f8bc | 2010-04-02 19:36:14 +0000 | [diff] [blame] | 3656 | EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign, |
Evan Cheng | 946a3a9 | 2012-12-12 02:34:41 +0000 | [diff] [blame] | 3657 | IsMemset, ZeroMemset, MemcpyStrSrc, |
Dan Gohman | 4bcf0a9 | 2010-04-16 20:22:43 +0000 | [diff] [blame] | 3658 | DAG.getMachineFunction()); |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3659 | |
Chris Lattner | acee647 | 2010-03-07 07:45:08 +0000 | [diff] [blame] | 3660 | if (VT == MVT::Other) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 3661 | unsigned AS = 0; |
| 3662 | if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment(AS) || |
| 3663 | TLI.allowsUnalignedMemoryAccesses(VT, AS)) { |
Evan Cheng | 18141ee | 2010-04-05 23:33:29 +0000 | [diff] [blame] | 3664 | VT = TLI.getPointerTy(); |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3665 | } else { |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3666 | switch (DstAlign & 7) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3667 | case 0: VT = MVT::i64; break; |
| 3668 | case 4: VT = MVT::i32; break; |
| 3669 | case 2: VT = MVT::i16; break; |
| 3670 | default: VT = MVT::i8; break; |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3671 | } |
| 3672 | } |
| 3673 | |
Owen Anderson | 766b5ef | 2009-08-11 21:59:30 +0000 | [diff] [blame] | 3674 | MVT LVT = MVT::i64; |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3675 | while (!TLI.isTypeLegal(LVT)) |
Owen Anderson | 766b5ef | 2009-08-11 21:59:30 +0000 | [diff] [blame] | 3676 | LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3677 | assert(LVT.isInteger()); |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3678 | |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 3679 | if (VT.bitsGT(LVT)) |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3680 | VT = LVT; |
| 3681 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3682 | |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3683 | unsigned NumMemOps = 0; |
| 3684 | while (Size != 0) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3685 | unsigned VTSize = VT.getSizeInBits() / 8; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3686 | while (VTSize > Size) { |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3687 | // For now, only use non-vector load / store's for the left-over pieces. |
Evan Cheng | 61f4dfe | 2012-12-12 00:42:09 +0000 | [diff] [blame] | 3688 | EVT NewVT = VT; |
Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 3689 | unsigned NewVTSize; |
Evan Cheng | 61f4dfe | 2012-12-12 00:42:09 +0000 | [diff] [blame] | 3690 | |
| 3691 | bool Found = false; |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3692 | if (VT.isVector() || VT.isFloatingPoint()) { |
Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 3693 | NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32; |
Evan Cheng | 61f4dfe | 2012-12-12 00:42:09 +0000 | [diff] [blame] | 3694 | if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) && |
Evan Cheng | 7d34267 | 2012-12-12 01:32:07 +0000 | [diff] [blame] | 3695 | TLI.isSafeMemOpType(NewVT.getSimpleVT())) |
Evan Cheng | 61f4dfe | 2012-12-12 00:42:09 +0000 | [diff] [blame] | 3696 | Found = true; |
| 3697 | else if (NewVT == MVT::i64 && |
| 3698 | TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) && |
Evan Cheng | 7d34267 | 2012-12-12 01:32:07 +0000 | [diff] [blame] | 3699 | TLI.isSafeMemOpType(MVT::f64)) { |
Evan Cheng | 61f4dfe | 2012-12-12 00:42:09 +0000 | [diff] [blame] | 3700 | // i64 is usually not legal on 32-bit targets, but f64 may be. |
| 3701 | NewVT = MVT::f64; |
| 3702 | Found = true; |
Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 3703 | } |
Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 3704 | } |
| 3705 | |
Evan Cheng | 61f4dfe | 2012-12-12 00:42:09 +0000 | [diff] [blame] | 3706 | if (!Found) { |
| 3707 | do { |
| 3708 | NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1); |
| 3709 | if (NewVT == MVT::i8) |
| 3710 | break; |
Evan Cheng | 7d34267 | 2012-12-12 01:32:07 +0000 | [diff] [blame] | 3711 | } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT())); |
Evan Cheng | 61f4dfe | 2012-12-12 00:42:09 +0000 | [diff] [blame] | 3712 | } |
| 3713 | NewVTSize = NewVT.getSizeInBits() / 8; |
| 3714 | |
Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 3715 | // If the new VT cannot cover all of the remaining bits, then consider |
| 3716 | // issuing a (or a pair of) unaligned and overlapping load / store. |
| 3717 | // FIXME: Only does this for 64-bit or more since we don't have proper |
| 3718 | // cost model for unaligned load / store. |
| 3719 | bool Fast; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 3720 | unsigned AS = 0; |
Evan Cheng | a16e49d | 2012-12-12 20:43:23 +0000 | [diff] [blame] | 3721 | if (NumMemOps && AllowOverlap && |
| 3722 | VTSize >= 8 && NewVTSize < Size && |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 3723 | TLI.allowsUnalignedMemoryAccesses(VT, AS, &Fast) && Fast) |
Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 3724 | VTSize = Size; |
| 3725 | else { |
| 3726 | VT = NewVT; |
| 3727 | VTSize = NewVTSize; |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3728 | } |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3729 | } |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3730 | |
Evan Cheng | a16e49d | 2012-12-12 20:43:23 +0000 | [diff] [blame] | 3731 | if (++NumMemOps > Limit) |
| 3732 | return false; |
| 3733 | |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3734 | MemOps.push_back(VT); |
| 3735 | Size -= VTSize; |
| 3736 | } |
| 3737 | |
| 3738 | return true; |
| 3739 | } |
| 3740 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3741 | static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl, |
Evan Cheng | 87bd191 | 2010-03-30 18:08:53 +0000 | [diff] [blame] | 3742 | SDValue Chain, SDValue Dst, |
| 3743 | SDValue Src, uint64_t Size, |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3744 | unsigned Align, bool isVol, |
| 3745 | bool AlwaysInline, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 3746 | MachinePointerInfo DstPtrInfo, |
| 3747 | MachinePointerInfo SrcPtrInfo) { |
Evan Cheng | f28f8bc | 2010-04-02 19:36:14 +0000 | [diff] [blame] | 3748 | // Turn a memcpy of undef to nop. |
| 3749 | if (Src.getOpcode() == ISD::UNDEF) |
| 3750 | return Chain; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3751 | |
Dan Gohman | 21323f3 | 2008-05-29 19:42:22 +0000 | [diff] [blame] | 3752 | // Expand memcpy to a series of load and store ops if the size operand falls |
| 3753 | // below a certain threshold. |
Duncan Sands | 69300a2 | 2010-11-05 15:20:29 +0000 | [diff] [blame] | 3754 | // TODO: In the AlwaysInline case, if the size is big then generate a loop |
| 3755 | // rather than maybe a humongous number of loads and stores. |
Evan Cheng | f28f8bc | 2010-04-02 19:36:14 +0000 | [diff] [blame] | 3756 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3757 | std::vector<EVT> MemOps; |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3758 | bool DstAlignCanChange = false; |
Evan Cheng | 0521928 | 2011-01-06 06:52:41 +0000 | [diff] [blame] | 3759 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3760 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 3761 | bool OptSize = |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 3762 | MF.getFunction()->getAttributes(). |
| 3763 | hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize); |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3764 | FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst); |
| 3765 | if (FI && !MFI->isFixedObjectIndex(FI->getIndex())) |
| 3766 | DstAlignCanChange = true; |
| 3767 | unsigned SrcAlign = DAG.InferPtrAlignment(Src); |
| 3768 | if (Align > SrcAlign) |
| 3769 | SrcAlign = Align; |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3770 | StringRef Str; |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3771 | bool CopyFromStr = isMemSrcFromString(Src, Str); |
| 3772 | bool isZeroStr = CopyFromStr && Str.empty(); |
Evan Cheng | 0521928 | 2011-01-06 06:52:41 +0000 | [diff] [blame] | 3773 | unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3774 | |
Evan Cheng | 94107ba | 2010-04-01 18:19:11 +0000 | [diff] [blame] | 3775 | if (!FindOptimalMemOpLowering(MemOps, Limit, Size, |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3776 | (DstAlignCanChange ? 0 : Align), |
Evan Cheng | c3b0c34 | 2010-04-08 07:37:57 +0000 | [diff] [blame] | 3777 | (isZeroStr ? 0 : SrcAlign), |
Evan Cheng | 946a3a9 | 2012-12-12 02:34:41 +0000 | [diff] [blame] | 3778 | false, false, CopyFromStr, true, DAG, TLI)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3779 | return SDValue(); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3780 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3781 | if (DstAlignCanChange) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3782 | Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext()); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 3783 | unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty); |
Lang Hames | 2d95e43 | 2013-01-31 20:23:43 +0000 | [diff] [blame] | 3784 | |
| 3785 | // Don't promote to an alignment that would require dynamic stack |
Stephen Lin | 155615d | 2013-07-08 00:37:03 +0000 | [diff] [blame] | 3786 | // realignment. |
Lang Hames | 2d95e43 | 2013-01-31 20:23:43 +0000 | [diff] [blame] | 3787 | const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo(); |
| 3788 | if (!TRI->needsStackRealignment(MF)) |
| 3789 | while (NewAlign > Align && |
| 3790 | TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign)) |
| 3791 | NewAlign /= 2; |
| 3792 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3793 | if (NewAlign > Align) { |
| 3794 | // Give the stack frame object a larger alignment if needed. |
| 3795 | if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign) |
| 3796 | MFI->setObjectAlignment(FI->getIndex(), NewAlign); |
| 3797 | Align = NewAlign; |
| 3798 | } |
| 3799 | } |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3800 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3801 | SmallVector<SDValue, 8> OutChains; |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3802 | unsigned NumMemOps = MemOps.size(); |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3803 | uint64_t SrcOff = 0, DstOff = 0; |
Chris Lattner | 7453f8a | 2009-09-20 17:32:21 +0000 | [diff] [blame] | 3804 | for (unsigned i = 0; i != NumMemOps; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3805 | EVT VT = MemOps[i]; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3806 | unsigned VTSize = VT.getSizeInBits() / 8; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3807 | SDValue Value, Store; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3808 | |
Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 3809 | if (VTSize > Size) { |
| 3810 | // Issuing an unaligned load / store pair that overlaps with the previous |
| 3811 | // pair. Adjust the offset accordingly. |
| 3812 | assert(i == NumMemOps-1 && i != 0); |
| 3813 | SrcOff -= VTSize - Size; |
| 3814 | DstOff -= VTSize - Size; |
| 3815 | } |
| 3816 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3817 | if (CopyFromStr && |
| 3818 | (isZeroStr || (VT.isInteger() && !VT.isVector()))) { |
Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 3819 | // It's unlikely a store of a vector immediate can be done in a single |
| 3820 | // instruction. It would require a load from a constantpool first. |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3821 | // We only handle zero vectors here. |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3822 | // FIXME: Handle other cases where store of vector immediate is done in |
| 3823 | // a single instruction. |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3824 | Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff)); |
Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 3825 | if (Value.getNode()) |
| 3826 | Store = DAG.getStore(Chain, dl, Value, |
Andrew Trick | dd0fb01 | 2013-05-25 03:08:10 +0000 | [diff] [blame] | 3827 | getMemBasePlusOffset(Dst, DstOff, dl, DAG), |
Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 3828 | DstPtrInfo.getWithOffset(DstOff), isVol, |
| 3829 | false, Align); |
| 3830 | } |
| 3831 | |
| 3832 | if (!Store.getNode()) { |
Dale Johannesen | 08bc98e | 2009-06-22 20:59:07 +0000 | [diff] [blame] | 3833 | // The type might not be legal for the target. This should only happen |
| 3834 | // if the type is smaller than a legal type, as on PPC, so the right |
Dale Johannesen | 8539cfd | 2009-06-24 17:11:31 +0000 | [diff] [blame] | 3835 | // thing to do is generate a LoadExt/StoreTrunc pair. These simplify |
| 3836 | // to Load/Store if NVT==VT. |
Dale Johannesen | 08bc98e | 2009-06-22 20:59:07 +0000 | [diff] [blame] | 3837 | // FIXME does the case above also need this? |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 3838 | EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT); |
Dale Johannesen | 8539cfd | 2009-06-24 17:11:31 +0000 | [diff] [blame] | 3839 | assert(NVT.bitsGE(VT)); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 3840 | Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain, |
Andrew Trick | dd0fb01 | 2013-05-25 03:08:10 +0000 | [diff] [blame] | 3841 | getMemBasePlusOffset(Src, SrcOff, dl, DAG), |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 3842 | SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false, |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3843 | MinAlign(SrcAlign, SrcOff)); |
Dale Johannesen | 8539cfd | 2009-06-24 17:11:31 +0000 | [diff] [blame] | 3844 | Store = DAG.getTruncStore(Chain, dl, Value, |
Andrew Trick | dd0fb01 | 2013-05-25 03:08:10 +0000 | [diff] [blame] | 3845 | getMemBasePlusOffset(Dst, DstOff, dl, DAG), |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 3846 | DstPtrInfo.getWithOffset(DstOff), VT, isVol, |
| 3847 | false, Align); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3848 | } |
| 3849 | OutChains.push_back(Store); |
| 3850 | SrcOff += VTSize; |
| 3851 | DstOff += VTSize; |
Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 3852 | Size -= VTSize; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3853 | } |
| 3854 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3855 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3856 | &OutChains[0], OutChains.size()); |
| 3857 | } |
| 3858 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3859 | static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl, |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3860 | SDValue Chain, SDValue Dst, |
| 3861 | SDValue Src, uint64_t Size, |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3862 | unsigned Align, bool isVol, |
| 3863 | bool AlwaysInline, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 3864 | MachinePointerInfo DstPtrInfo, |
| 3865 | MachinePointerInfo SrcPtrInfo) { |
Evan Cheng | f28f8bc | 2010-04-02 19:36:14 +0000 | [diff] [blame] | 3866 | // Turn a memmove of undef to nop. |
| 3867 | if (Src.getOpcode() == ISD::UNDEF) |
| 3868 | return Chain; |
Dan Gohman | 21323f3 | 2008-05-29 19:42:22 +0000 | [diff] [blame] | 3869 | |
| 3870 | // Expand memmove to a series of load and store ops if the size operand falls |
| 3871 | // below a certain threshold. |
Evan Cheng | f28f8bc | 2010-04-02 19:36:14 +0000 | [diff] [blame] | 3872 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3873 | std::vector<EVT> MemOps; |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3874 | bool DstAlignCanChange = false; |
Evan Cheng | 0521928 | 2011-01-06 06:52:41 +0000 | [diff] [blame] | 3875 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3876 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 3877 | bool OptSize = MF.getFunction()->getAttributes(). |
| 3878 | hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize); |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3879 | FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst); |
| 3880 | if (FI && !MFI->isFixedObjectIndex(FI->getIndex())) |
| 3881 | DstAlignCanChange = true; |
| 3882 | unsigned SrcAlign = DAG.InferPtrAlignment(Src); |
| 3883 | if (Align > SrcAlign) |
| 3884 | SrcAlign = Align; |
Evan Cheng | 0521928 | 2011-01-06 06:52:41 +0000 | [diff] [blame] | 3885 | unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize); |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3886 | |
Evan Cheng | 94107ba | 2010-04-01 18:19:11 +0000 | [diff] [blame] | 3887 | if (!FindOptimalMemOpLowering(MemOps, Limit, Size, |
Evan Cheng | 946a3a9 | 2012-12-12 02:34:41 +0000 | [diff] [blame] | 3888 | (DstAlignCanChange ? 0 : Align), SrcAlign, |
| 3889 | false, false, false, false, DAG, TLI)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3890 | return SDValue(); |
Dan Gohman | 21323f3 | 2008-05-29 19:42:22 +0000 | [diff] [blame] | 3891 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3892 | if (DstAlignCanChange) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3893 | Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext()); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 3894 | unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty); |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3895 | if (NewAlign > Align) { |
| 3896 | // Give the stack frame object a larger alignment if needed. |
| 3897 | if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign) |
| 3898 | MFI->setObjectAlignment(FI->getIndex(), NewAlign); |
| 3899 | Align = NewAlign; |
| 3900 | } |
| 3901 | } |
Dan Gohman | 21323f3 | 2008-05-29 19:42:22 +0000 | [diff] [blame] | 3902 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3903 | uint64_t SrcOff = 0, DstOff = 0; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3904 | SmallVector<SDValue, 8> LoadValues; |
| 3905 | SmallVector<SDValue, 8> LoadChains; |
| 3906 | SmallVector<SDValue, 8> OutChains; |
Dan Gohman | 21323f3 | 2008-05-29 19:42:22 +0000 | [diff] [blame] | 3907 | unsigned NumMemOps = MemOps.size(); |
| 3908 | for (unsigned i = 0; i < NumMemOps; i++) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3909 | EVT VT = MemOps[i]; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3910 | unsigned VTSize = VT.getSizeInBits() / 8; |
Rafael Espindola | 8819c84 | 2013-10-01 13:32:03 +0000 | [diff] [blame] | 3911 | SDValue Value; |
Dan Gohman | 21323f3 | 2008-05-29 19:42:22 +0000 | [diff] [blame] | 3912 | |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 3913 | Value = DAG.getLoad(VT, dl, Chain, |
Andrew Trick | dd0fb01 | 2013-05-25 03:08:10 +0000 | [diff] [blame] | 3914 | getMemBasePlusOffset(Src, SrcOff, dl, DAG), |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 3915 | SrcPtrInfo.getWithOffset(SrcOff), isVol, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 3916 | false, false, SrcAlign); |
Dan Gohman | 21323f3 | 2008-05-29 19:42:22 +0000 | [diff] [blame] | 3917 | LoadValues.push_back(Value); |
| 3918 | LoadChains.push_back(Value.getValue(1)); |
| 3919 | SrcOff += VTSize; |
| 3920 | } |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3921 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Dan Gohman | 21323f3 | 2008-05-29 19:42:22 +0000 | [diff] [blame] | 3922 | &LoadChains[0], LoadChains.size()); |
| 3923 | OutChains.clear(); |
| 3924 | for (unsigned i = 0; i < NumMemOps; i++) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3925 | EVT VT = MemOps[i]; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3926 | unsigned VTSize = VT.getSizeInBits() / 8; |
Rafael Espindola | 8819c84 | 2013-10-01 13:32:03 +0000 | [diff] [blame] | 3927 | SDValue Store; |
Dan Gohman | 21323f3 | 2008-05-29 19:42:22 +0000 | [diff] [blame] | 3928 | |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 3929 | Store = DAG.getStore(Chain, dl, LoadValues[i], |
Andrew Trick | dd0fb01 | 2013-05-25 03:08:10 +0000 | [diff] [blame] | 3930 | getMemBasePlusOffset(Dst, DstOff, dl, DAG), |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 3931 | DstPtrInfo.getWithOffset(DstOff), isVol, false, Align); |
Dan Gohman | 21323f3 | 2008-05-29 19:42:22 +0000 | [diff] [blame] | 3932 | OutChains.push_back(Store); |
| 3933 | DstOff += VTSize; |
| 3934 | } |
| 3935 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3936 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Dan Gohman | 21323f3 | 2008-05-29 19:42:22 +0000 | [diff] [blame] | 3937 | &OutChains[0], OutChains.size()); |
| 3938 | } |
| 3939 | |
Serge Pavlov | 496f024 | 2013-09-17 16:24:42 +0000 | [diff] [blame] | 3940 | /// \brief Lower the call to 'memset' intrinsic function into a series of store |
| 3941 | /// operations. |
| 3942 | /// |
| 3943 | /// \param DAG Selection DAG where lowered code is placed. |
| 3944 | /// \param dl Link to corresponding IR location. |
| 3945 | /// \param Chain Control flow dependency. |
| 3946 | /// \param Dst Pointer to destination memory location. |
| 3947 | /// \param Src Value of byte to write into the memory. |
| 3948 | /// \param Size Number of bytes to write. |
| 3949 | /// \param Align Alignment of the destination in bytes. |
| 3950 | /// \param isVol True if destination is volatile. |
| 3951 | /// \param DstPtrInfo IR information on the memory pointer. |
| 3952 | /// \returns New head in the control flow, if lowering was successful, empty |
| 3953 | /// SDValue otherwise. |
| 3954 | /// |
| 3955 | /// The function tries to replace 'llvm.memset' intrinsic with several store |
| 3956 | /// operations and value calculation code. This is usually profitable for small |
| 3957 | /// memory size. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3958 | static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl, |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3959 | SDValue Chain, SDValue Dst, |
| 3960 | SDValue Src, uint64_t Size, |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 3961 | unsigned Align, bool isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 3962 | MachinePointerInfo DstPtrInfo) { |
Evan Cheng | f28f8bc | 2010-04-02 19:36:14 +0000 | [diff] [blame] | 3963 | // Turn a memset of undef to nop. |
| 3964 | if (Src.getOpcode() == ISD::UNDEF) |
| 3965 | return Chain; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3966 | |
| 3967 | // Expand memset to a series of load/store ops if the size operand |
| 3968 | // falls below a certain threshold. |
Evan Cheng | f28f8bc | 2010-04-02 19:36:14 +0000 | [diff] [blame] | 3969 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3970 | std::vector<EVT> MemOps; |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3971 | bool DstAlignCanChange = false; |
Evan Cheng | 0521928 | 2011-01-06 06:52:41 +0000 | [diff] [blame] | 3972 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3973 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 3974 | bool OptSize = MF.getFunction()->getAttributes(). |
| 3975 | hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize); |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3976 | FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst); |
| 3977 | if (FI && !MFI->isFixedObjectIndex(FI->getIndex())) |
| 3978 | DstAlignCanChange = true; |
Lang Hames | 15701f8 | 2011-10-26 23:50:43 +0000 | [diff] [blame] | 3979 | bool IsZeroVal = |
Evan Cheng | f28f8bc | 2010-04-02 19:36:14 +0000 | [diff] [blame] | 3980 | isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue(); |
Evan Cheng | 0521928 | 2011-01-06 06:52:41 +0000 | [diff] [blame] | 3981 | if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize), |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3982 | Size, (DstAlignCanChange ? 0 : Align), 0, |
Evan Cheng | 946a3a9 | 2012-12-12 02:34:41 +0000 | [diff] [blame] | 3983 | true, IsZeroVal, false, true, DAG, TLI)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3984 | return SDValue(); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3985 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3986 | if (DstAlignCanChange) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3987 | Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext()); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 3988 | unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty); |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 3989 | if (NewAlign > Align) { |
| 3990 | // Give the stack frame object a larger alignment if needed. |
| 3991 | if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign) |
| 3992 | MFI->setObjectAlignment(FI->getIndex(), NewAlign); |
| 3993 | Align = NewAlign; |
| 3994 | } |
| 3995 | } |
| 3996 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3997 | SmallVector<SDValue, 8> OutChains; |
Dan Gohman | 1f13c68 | 2008-04-28 17:15:20 +0000 | [diff] [blame] | 3998 | uint64_t DstOff = 0; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 3999 | unsigned NumMemOps = MemOps.size(); |
Benjamin Kramer | 8022036 | 2011-01-02 19:57:05 +0000 | [diff] [blame] | 4000 | |
| 4001 | // Find the largest store and generate the bit pattern for it. |
| 4002 | EVT LargestVT = MemOps[0]; |
| 4003 | for (unsigned i = 1; i < NumMemOps; i++) |
| 4004 | if (MemOps[i].bitsGT(LargestVT)) |
| 4005 | LargestVT = MemOps[i]; |
| 4006 | SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl); |
| 4007 | |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4008 | for (unsigned i = 0; i < NumMemOps; i++) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4009 | EVT VT = MemOps[i]; |
Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 4010 | unsigned VTSize = VT.getSizeInBits() / 8; |
| 4011 | if (VTSize > Size) { |
| 4012 | // Issuing an unaligned load / store pair that overlaps with the previous |
| 4013 | // pair. Adjust the offset accordingly. |
| 4014 | assert(i == NumMemOps-1 && i != 0); |
| 4015 | DstOff -= VTSize - Size; |
| 4016 | } |
Benjamin Kramer | 8022036 | 2011-01-02 19:57:05 +0000 | [diff] [blame] | 4017 | |
| 4018 | // If this store is smaller than the largest store see whether we can get |
| 4019 | // the smaller value for free with a truncate. |
| 4020 | SDValue Value = MemSetValue; |
| 4021 | if (VT.bitsLT(LargestVT)) { |
| 4022 | if (!LargestVT.isVector() && !VT.isVector() && |
| 4023 | TLI.isTruncateFree(LargestVT, VT)) |
| 4024 | Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue); |
| 4025 | else |
| 4026 | Value = getMemsetValue(Src, VT, DAG, dl); |
| 4027 | } |
| 4028 | assert(Value.getValueType() == VT && "Value with wrong type."); |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 4029 | SDValue Store = DAG.getStore(Chain, dl, Value, |
Andrew Trick | dd0fb01 | 2013-05-25 03:08:10 +0000 | [diff] [blame] | 4030 | getMemBasePlusOffset(Dst, DstOff, dl, DAG), |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4031 | DstPtrInfo.getWithOffset(DstOff), |
Dale Johannesen | b4ac285 | 2010-11-18 01:35:23 +0000 | [diff] [blame] | 4032 | isVol, false, Align); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4033 | OutChains.push_back(Store); |
Benjamin Kramer | 8022036 | 2011-01-02 19:57:05 +0000 | [diff] [blame] | 4034 | DstOff += VT.getSizeInBits() / 8; |
Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 4035 | Size -= VTSize; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4036 | } |
| 4037 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4038 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4039 | &OutChains[0], OutChains.size()); |
| 4040 | } |
| 4041 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4042 | SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4043 | SDValue Src, SDValue Size, |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4044 | unsigned Align, bool isVol, bool AlwaysInline, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4045 | MachinePointerInfo DstPtrInfo, |
| 4046 | MachinePointerInfo SrcPtrInfo) { |
Chandler Carruth | 7e6ffac | 2013-02-25 14:29:38 +0000 | [diff] [blame] | 4047 | assert(Align && "The SDAG layer expects explicit alignment and reserves 0"); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4048 | |
| 4049 | // Check to see if we should lower the memcpy to loads and stores first. |
| 4050 | // For cases within the target-specified limits, this is the best choice. |
| 4051 | ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); |
| 4052 | if (ConstantSize) { |
| 4053 | // Memcpy with size zero? Just return the original chain. |
| 4054 | if (ConstantSize->isNullValue()) |
| 4055 | return Chain; |
| 4056 | |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 4057 | SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src, |
| 4058 | ConstantSize->getZExtValue(),Align, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4059 | isVol, false, DstPtrInfo, SrcPtrInfo); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4060 | if (Result.getNode()) |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4061 | return Result; |
| 4062 | } |
| 4063 | |
| 4064 | // Then check to see if we should lower the memcpy with target-specific |
| 4065 | // code. If the target chooses to do this, this is the next best. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4066 | SDValue Result = |
Dan Gohman | ff7a562 | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 4067 | TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align, |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4068 | isVol, AlwaysInline, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4069 | DstPtrInfo, SrcPtrInfo); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4070 | if (Result.getNode()) |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4071 | return Result; |
| 4072 | |
| 4073 | // If we really need inline code and the target declined to provide it, |
| 4074 | // use a (potentially long) sequence of loads and stores. |
| 4075 | if (AlwaysInline) { |
| 4076 | assert(ConstantSize && "AlwaysInline requires a constant size!"); |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 4077 | return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src, |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4078 | ConstantSize->getZExtValue(), Align, isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4079 | true, DstPtrInfo, SrcPtrInfo); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
Dan Gohman | 7b55d36 | 2010-04-05 20:24:08 +0000 | [diff] [blame] | 4082 | // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc |
| 4083 | // memcpy is not guaranteed to be safe. libc memcpys aren't required to |
| 4084 | // respect volatile, so they may do things like read or write memory |
| 4085 | // beyond the given memory regions. But fixing this isn't easy, and most |
| 4086 | // people don't care. |
| 4087 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4088 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 4089 | |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4090 | // Emit a library call. |
| 4091 | TargetLowering::ArgListTy Args; |
| 4092 | TargetLowering::ArgListEntry Entry; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4093 | Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext()); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4094 | Entry.Node = Dst; Args.push_back(Entry); |
| 4095 | Entry.Node = Src; Args.push_back(Entry); |
| 4096 | Entry.Node = Size; Args.push_back(Entry); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4097 | // FIXME: pass in SDLoc |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 4098 | TargetLowering:: |
| 4099 | CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()), |
Anton Korobeynikov | 72977a4 | 2009-08-14 20:10:52 +0000 | [diff] [blame] | 4100 | false, false, false, false, 0, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4101 | TLI->getLibcallCallingConv(RTLIB::MEMCPY), |
Evan Cheng | 4bfcd4a | 2012-02-28 18:51:51 +0000 | [diff] [blame] | 4102 | /*isTailCall=*/false, |
| 4103 | /*doesNotReturn=*/false, /*isReturnValueUsed=*/false, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4104 | getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY), |
| 4105 | TLI->getPointerTy()), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4106 | Args, *this, dl); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4107 | std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 4108 | |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4109 | return CallResult.second; |
| 4110 | } |
| 4111 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4112 | SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4113 | SDValue Src, SDValue Size, |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4114 | unsigned Align, bool isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4115 | MachinePointerInfo DstPtrInfo, |
| 4116 | MachinePointerInfo SrcPtrInfo) { |
Chandler Carruth | 7e6ffac | 2013-02-25 14:29:38 +0000 | [diff] [blame] | 4117 | assert(Align && "The SDAG layer expects explicit alignment and reserves 0"); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4118 | |
Dan Gohman | 21323f3 | 2008-05-29 19:42:22 +0000 | [diff] [blame] | 4119 | // Check to see if we should lower the memmove to loads and stores first. |
| 4120 | // For cases within the target-specified limits, this is the best choice. |
| 4121 | ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); |
| 4122 | if (ConstantSize) { |
| 4123 | // Memmove with size zero? Just return the original chain. |
| 4124 | if (ConstantSize->isNullValue()) |
| 4125 | return Chain; |
| 4126 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4127 | SDValue Result = |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 4128 | getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src, |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4129 | ConstantSize->getZExtValue(), Align, isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4130 | false, DstPtrInfo, SrcPtrInfo); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4131 | if (Result.getNode()) |
Dan Gohman | 21323f3 | 2008-05-29 19:42:22 +0000 | [diff] [blame] | 4132 | return Result; |
| 4133 | } |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4134 | |
| 4135 | // Then check to see if we should lower the memmove with target-specific |
| 4136 | // code. If the target chooses to do this, this is the next best. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4137 | SDValue Result = |
Dan Gohman | ff7a562 | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 4138 | TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4139 | DstPtrInfo, SrcPtrInfo); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4140 | if (Result.getNode()) |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4141 | return Result; |
| 4142 | |
Mon P Wang | 01f0e85 | 2010-04-06 08:27:51 +0000 | [diff] [blame] | 4143 | // FIXME: If the memmove is volatile, lowering it to plain libc memmove may |
| 4144 | // not be safe. See memcpy above for more details. |
| 4145 | |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4146 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 4147 | |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4148 | // Emit a library call. |
| 4149 | TargetLowering::ArgListTy Args; |
| 4150 | TargetLowering::ArgListEntry Entry; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4151 | Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext()); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4152 | Entry.Node = Dst; Args.push_back(Entry); |
| 4153 | Entry.Node = Src; Args.push_back(Entry); |
| 4154 | Entry.Node = Size; Args.push_back(Entry); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4155 | // FIXME: pass in SDLoc |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 4156 | TargetLowering:: |
| 4157 | CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()), |
Anton Korobeynikov | 72977a4 | 2009-08-14 20:10:52 +0000 | [diff] [blame] | 4158 | false, false, false, false, 0, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4159 | TLI->getLibcallCallingConv(RTLIB::MEMMOVE), |
Evan Cheng | 4bfcd4a | 2012-02-28 18:51:51 +0000 | [diff] [blame] | 4160 | /*isTailCall=*/false, |
| 4161 | /*doesNotReturn=*/false, /*isReturnValueUsed=*/false, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4162 | getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE), |
| 4163 | TLI->getPointerTy()), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4164 | Args, *this, dl); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4165 | std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 4166 | |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4167 | return CallResult.second; |
| 4168 | } |
| 4169 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4170 | SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4171 | SDValue Src, SDValue Size, |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4172 | unsigned Align, bool isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4173 | MachinePointerInfo DstPtrInfo) { |
Chandler Carruth | 7e6ffac | 2013-02-25 14:29:38 +0000 | [diff] [blame] | 4174 | assert(Align && "The SDAG layer expects explicit alignment and reserves 0"); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4175 | |
| 4176 | // Check to see if we should lower the memset to stores first. |
| 4177 | // For cases within the target-specified limits, this is the best choice. |
| 4178 | ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); |
| 4179 | if (ConstantSize) { |
| 4180 | // Memset with size zero? Just return the original chain. |
| 4181 | if (ConstantSize->isNullValue()) |
| 4182 | return Chain; |
| 4183 | |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4184 | SDValue Result = |
| 4185 | getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4186 | Align, isVol, DstPtrInfo); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 4187 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4188 | if (Result.getNode()) |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4189 | return Result; |
| 4190 | } |
| 4191 | |
| 4192 | // Then check to see if we should lower the memset with target-specific |
| 4193 | // code. If the target chooses to do this, this is the next best. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4194 | SDValue Result = |
Dan Gohman | ff7a562 | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 4195 | TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4196 | DstPtrInfo); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4197 | if (Result.getNode()) |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4198 | return Result; |
| 4199 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4200 | // Emit a library call. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4201 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 4202 | Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext()); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4203 | TargetLowering::ArgListTy Args; |
| 4204 | TargetLowering::ArgListEntry Entry; |
| 4205 | Entry.Node = Dst; Entry.Ty = IntPtrTy; |
| 4206 | Args.push_back(Entry); |
| 4207 | // Extend or truncate the argument to be an i32 value for the call. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4208 | if (Src.getValueType().bitsGT(MVT::i32)) |
| 4209 | Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src); |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4210 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4211 | Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4212 | Entry.Node = Src; |
| 4213 | Entry.Ty = Type::getInt32Ty(*getContext()); |
| 4214 | Entry.isSExt = true; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4215 | Args.push_back(Entry); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4216 | Entry.Node = Size; |
| 4217 | Entry.Ty = IntPtrTy; |
| 4218 | Entry.isSExt = false; |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4219 | Args.push_back(Entry); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4220 | // FIXME: pass in SDLoc |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 4221 | TargetLowering:: |
| 4222 | CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()), |
Anton Korobeynikov | 72977a4 | 2009-08-14 20:10:52 +0000 | [diff] [blame] | 4223 | false, false, false, false, 0, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4224 | TLI->getLibcallCallingConv(RTLIB::MEMSET), |
Evan Cheng | 4bfcd4a | 2012-02-28 18:51:51 +0000 | [diff] [blame] | 4225 | /*isTailCall=*/false, |
| 4226 | /*doesNotReturn*/false, /*isReturnValueUsed=*/false, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4227 | getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET), |
| 4228 | TLI->getPointerTy()), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 4229 | Args, *this, dl); |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 4230 | std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 4231 | |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 4232 | return CallResult.second; |
Rafael Espindola | 5c0d6ed | 2007-10-19 10:41:11 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4235 | SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 4236 | SDVTList VTList, SDValue *Ops, unsigned NumOps, |
Amara Emerson | 268c743 | 2013-09-26 12:22:36 +0000 | [diff] [blame] | 4237 | MachineMemOperand *MMO, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 4238 | AtomicOrdering SuccessOrdering, |
| 4239 | AtomicOrdering FailureOrdering, |
Amara Emerson | 268c743 | 2013-09-26 12:22:36 +0000 | [diff] [blame] | 4240 | SynchronizationScope SynchScope) { |
| 4241 | FoldingSetNodeID ID; |
| 4242 | ID.AddInteger(MemVT.getRawBits()); |
| 4243 | AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps); |
| 4244 | ID.AddInteger(MMO->getPointerInfo().getAddrSpace()); |
| 4245 | void* IP = 0; |
| 4246 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { |
| 4247 | cast<AtomicSDNode>(E)->refineAlignment(MMO); |
| 4248 | return SDValue(E, 0); |
| 4249 | } |
Benjamin Kramer | fd40d51 | 2013-09-29 11:18:56 +0000 | [diff] [blame] | 4250 | |
| 4251 | // Allocate the operands array for the node out of the BumpPtrAllocator, since |
| 4252 | // SDNode doesn't have access to it. This memory will be "leaked" when |
| 4253 | // the node is deallocated, but recovered when the allocator is released. |
| 4254 | // If the number of operands is less than 5 we use AtomicSDNode's internal |
| 4255 | // storage. |
| 4256 | SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps) : 0; |
| 4257 | |
Amara Emerson | 268c743 | 2013-09-26 12:22:36 +0000 | [diff] [blame] | 4258 | SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(), |
| 4259 | dl.getDebugLoc(), VTList, MemVT, |
Benjamin Kramer | fd40d51 | 2013-09-29 11:18:56 +0000 | [diff] [blame] | 4260 | Ops, DynOps, NumOps, MMO, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 4261 | SuccessOrdering, FailureOrdering, |
| 4262 | SynchScope); |
Amara Emerson | 268c743 | 2013-09-26 12:22:36 +0000 | [diff] [blame] | 4263 | CSEMap.InsertNode(N, IP); |
| 4264 | AllNodes.push_back(N); |
| 4265 | return SDValue(N, 0); |
| 4266 | } |
| 4267 | |
| 4268 | SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 4269 | SDVTList VTList, SDValue *Ops, unsigned NumOps, |
| 4270 | MachineMemOperand *MMO, |
| 4271 | AtomicOrdering Ordering, |
| 4272 | SynchronizationScope SynchScope) { |
| 4273 | return getAtomic(Opcode, dl, MemVT, VTList, Ops, NumOps, MMO, Ordering, |
| 4274 | Ordering, SynchScope); |
| 4275 | } |
| 4276 | |
| 4277 | SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, |
Chris Lattner | 60bddc8 | 2010-09-21 04:53:42 +0000 | [diff] [blame] | 4278 | SDValue Chain, SDValue Ptr, SDValue Cmp, |
| 4279 | SDValue Swp, MachinePointerInfo PtrInfo, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 4280 | unsigned Alignment, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 4281 | AtomicOrdering SuccessOrdering, |
| 4282 | AtomicOrdering FailureOrdering, |
Michael Ilseman | 06b9690 | 2012-09-10 16:56:31 +0000 | [diff] [blame] | 4283 | SynchronizationScope SynchScope) { |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4284 | if (Alignment == 0) // Ensure that codegen never sees alignment 0 |
| 4285 | Alignment = getEVTAlignment(MemVT); |
| 4286 | |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 4287 | MachineFunction &MF = getMachineFunction(); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4288 | |
Jakob Stoklund Olesen | 36d29bc | 2012-08-28 03:11:32 +0000 | [diff] [blame] | 4289 | // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE. |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4290 | // For now, atomics are considered to be volatile always. |
Eli Friedman | 981a010 | 2011-09-07 02:23:42 +0000 | [diff] [blame] | 4291 | // FIXME: Volatile isn't really correct; we should keep track of atomic |
| 4292 | // orderings in the memoperand. |
Jakob Stoklund Olesen | 36d29bc | 2012-08-28 03:11:32 +0000 | [diff] [blame] | 4293 | unsigned Flags = MachineMemOperand::MOVolatile; |
| 4294 | if (Opcode != ISD::ATOMIC_STORE) |
| 4295 | Flags |= MachineMemOperand::MOLoad; |
| 4296 | if (Opcode != ISD::ATOMIC_LOAD) |
| 4297 | Flags |= MachineMemOperand::MOStore; |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4298 | |
| 4299 | MachineMemOperand *MMO = |
Chris Lattner | 60bddc8 | 2010-09-21 04:53:42 +0000 | [diff] [blame] | 4300 | MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4301 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 4302 | return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 4303 | SuccessOrdering, FailureOrdering, SynchScope); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4304 | } |
| 4305 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4306 | SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4307 | SDValue Chain, |
| 4308 | SDValue Ptr, SDValue Cmp, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 4309 | SDValue Swp, MachineMemOperand *MMO, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 4310 | AtomicOrdering SuccessOrdering, |
| 4311 | AtomicOrdering FailureOrdering, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 4312 | SynchronizationScope SynchScope) { |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4313 | assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op"); |
| 4314 | assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types"); |
| 4315 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4316 | EVT VT = Cmp.getValueType(); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4317 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4318 | SDVTList VTs = getVTList(VT, MVT::Other); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4319 | SDValue Ops[] = {Chain, Ptr, Cmp, Swp}; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 4320 | return getAtomic(Opcode, dl, MemVT, VTs, Ops, 4, MMO, SuccessOrdering, |
| 4321 | FailureOrdering, SynchScope); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4322 | } |
| 4323 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4324 | SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4325 | SDValue Chain, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4326 | SDValue Ptr, SDValue Val, |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4327 | const Value* PtrVal, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 4328 | unsigned Alignment, |
| 4329 | AtomicOrdering Ordering, |
| 4330 | SynchronizationScope SynchScope) { |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4331 | if (Alignment == 0) // Ensure that codegen never sees alignment 0 |
| 4332 | Alignment = getEVTAlignment(MemVT); |
| 4333 | |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 4334 | MachineFunction &MF = getMachineFunction(); |
Jakob Stoklund Olesen | 36d29bc | 2012-08-28 03:11:32 +0000 | [diff] [blame] | 4335 | // An atomic store does not load. An atomic load does not store. |
Eli Friedman | 981a010 | 2011-09-07 02:23:42 +0000 | [diff] [blame] | 4336 | // (An atomicrmw obviously both loads and stores.) |
Jakob Stoklund Olesen | 36d29bc | 2012-08-28 03:11:32 +0000 | [diff] [blame] | 4337 | // For now, atomics are considered to be volatile always, and they are |
| 4338 | // chained as such. |
Eli Friedman | 981a010 | 2011-09-07 02:23:42 +0000 | [diff] [blame] | 4339 | // FIXME: Volatile isn't really correct; we should keep track of atomic |
| 4340 | // orderings in the memoperand. |
Jakob Stoklund Olesen | 36d29bc | 2012-08-28 03:11:32 +0000 | [diff] [blame] | 4341 | unsigned Flags = MachineMemOperand::MOVolatile; |
| 4342 | if (Opcode != ISD::ATOMIC_STORE) |
| 4343 | Flags |= MachineMemOperand::MOLoad; |
| 4344 | if (Opcode != ISD::ATOMIC_LOAD) |
| 4345 | Flags |= MachineMemOperand::MOStore; |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4346 | |
| 4347 | MachineMemOperand *MMO = |
Chris Lattner | 93a95ae | 2010-09-21 04:46:39 +0000 | [diff] [blame] | 4348 | MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags, |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4349 | MemVT.getStoreSize(), Alignment); |
| 4350 | |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 4351 | return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO, |
| 4352 | Ordering, SynchScope); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4353 | } |
| 4354 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4355 | SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4356 | SDValue Chain, |
| 4357 | SDValue Ptr, SDValue Val, |
Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 4358 | MachineMemOperand *MMO, |
| 4359 | AtomicOrdering Ordering, |
| 4360 | SynchronizationScope SynchScope) { |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4361 | assert((Opcode == ISD::ATOMIC_LOAD_ADD || |
| 4362 | Opcode == ISD::ATOMIC_LOAD_SUB || |
| 4363 | Opcode == ISD::ATOMIC_LOAD_AND || |
| 4364 | Opcode == ISD::ATOMIC_LOAD_OR || |
| 4365 | Opcode == ISD::ATOMIC_LOAD_XOR || |
| 4366 | Opcode == ISD::ATOMIC_LOAD_NAND || |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4367 | Opcode == ISD::ATOMIC_LOAD_MIN || |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4368 | Opcode == ISD::ATOMIC_LOAD_MAX || |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4369 | Opcode == ISD::ATOMIC_LOAD_UMIN || |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4370 | Opcode == ISD::ATOMIC_LOAD_UMAX || |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 4371 | Opcode == ISD::ATOMIC_SWAP || |
| 4372 | Opcode == ISD::ATOMIC_STORE) && |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4373 | "Invalid Atomic Op"); |
| 4374 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4375 | EVT VT = Val.getValueType(); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4376 | |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 4377 | SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) : |
| 4378 | getVTList(VT, MVT::Other); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4379 | SDValue Ops[] = {Chain, Ptr, Val}; |
Amara Emerson | 268c743 | 2013-09-26 12:22:36 +0000 | [diff] [blame] | 4380 | return getAtomic(Opcode, dl, MemVT, VTs, Ops, 3, MMO, Ordering, SynchScope); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4383 | SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 4384 | EVT VT, SDValue Chain, |
| 4385 | SDValue Ptr, |
| 4386 | const Value* PtrVal, |
| 4387 | unsigned Alignment, |
| 4388 | AtomicOrdering Ordering, |
| 4389 | SynchronizationScope SynchScope) { |
| 4390 | if (Alignment == 0) // Ensure that codegen never sees alignment 0 |
| 4391 | Alignment = getEVTAlignment(MemVT); |
| 4392 | |
| 4393 | MachineFunction &MF = getMachineFunction(); |
Jakob Stoklund Olesen | 36d29bc | 2012-08-28 03:11:32 +0000 | [diff] [blame] | 4394 | // An atomic store does not load. An atomic load does not store. |
| 4395 | // (An atomicrmw obviously both loads and stores.) |
| 4396 | // For now, atomics are considered to be volatile always, and they are |
| 4397 | // chained as such. |
Eli Friedman | 981a010 | 2011-09-07 02:23:42 +0000 | [diff] [blame] | 4398 | // FIXME: Volatile isn't really correct; we should keep track of atomic |
| 4399 | // orderings in the memoperand. |
Jakob Stoklund Olesen | 36d29bc | 2012-08-28 03:11:32 +0000 | [diff] [blame] | 4400 | unsigned Flags = MachineMemOperand::MOVolatile; |
| 4401 | if (Opcode != ISD::ATOMIC_STORE) |
| 4402 | Flags |= MachineMemOperand::MOLoad; |
| 4403 | if (Opcode != ISD::ATOMIC_LOAD) |
| 4404 | Flags |= MachineMemOperand::MOStore; |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 4405 | |
| 4406 | MachineMemOperand *MMO = |
| 4407 | MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags, |
| 4408 | MemVT.getStoreSize(), Alignment); |
| 4409 | |
| 4410 | return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO, |
| 4411 | Ordering, SynchScope); |
| 4412 | } |
| 4413 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4414 | SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT, |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 4415 | EVT VT, SDValue Chain, |
| 4416 | SDValue Ptr, |
| 4417 | MachineMemOperand *MMO, |
| 4418 | AtomicOrdering Ordering, |
| 4419 | SynchronizationScope SynchScope) { |
| 4420 | assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op"); |
| 4421 | |
| 4422 | SDVTList VTs = getVTList(VT, MVT::Other); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 4423 | SDValue Ops[] = {Chain, Ptr}; |
Amara Emerson | 268c743 | 2013-09-26 12:22:36 +0000 | [diff] [blame] | 4424 | return getAtomic(Opcode, dl, MemVT, VTs, Ops, 2, MMO, Ordering, SynchScope); |
Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 4425 | } |
| 4426 | |
Duncan Sands | 4bdcb61 | 2008-07-02 17:40:58 +0000 | [diff] [blame] | 4427 | /// getMergeValues - Create a MERGE_VALUES node from the given operands. |
Dale Johannesen | 54c9452 | 2009-02-02 20:47:48 +0000 | [diff] [blame] | 4428 | SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4429 | SDLoc dl) { |
Dale Johannesen | 54c9452 | 2009-02-02 20:47:48 +0000 | [diff] [blame] | 4430 | if (NumOps == 1) |
| 4431 | return Ops[0]; |
| 4432 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4433 | SmallVector<EVT, 4> VTs; |
Dale Johannesen | 54c9452 | 2009-02-02 20:47:48 +0000 | [diff] [blame] | 4434 | VTs.reserve(NumOps); |
| 4435 | for (unsigned i = 0; i < NumOps; ++i) |
| 4436 | VTs.push_back(Ops[i].getValueType()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4437 | return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps), |
Dale Johannesen | 54c9452 | 2009-02-02 20:47:48 +0000 | [diff] [blame] | 4438 | Ops, NumOps); |
| 4439 | } |
| 4440 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4441 | SDValue |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4442 | SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4443 | const EVT *VTs, unsigned NumVTs, |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4444 | const SDValue *Ops, unsigned NumOps, |
Chris Lattner | e9ba5dd | 2010-09-21 04:57:15 +0000 | [diff] [blame] | 4445 | EVT MemVT, MachinePointerInfo PtrInfo, |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4446 | unsigned Align, bool Vol, |
| 4447 | bool ReadMem, bool WriteMem) { |
| 4448 | return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps, |
Chris Lattner | e9ba5dd | 2010-09-21 04:57:15 +0000 | [diff] [blame] | 4449 | MemVT, PtrInfo, Align, Vol, |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4450 | ReadMem, WriteMem); |
| 4451 | } |
| 4452 | |
| 4453 | SDValue |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4454 | SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList, |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4455 | const SDValue *Ops, unsigned NumOps, |
Chris Lattner | e9ba5dd | 2010-09-21 04:57:15 +0000 | [diff] [blame] | 4456 | EVT MemVT, MachinePointerInfo PtrInfo, |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4457 | unsigned Align, bool Vol, |
| 4458 | bool ReadMem, bool WriteMem) { |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4459 | if (Align == 0) // Ensure that codegen never sees alignment 0 |
| 4460 | Align = getEVTAlignment(MemVT); |
| 4461 | |
| 4462 | MachineFunction &MF = getMachineFunction(); |
| 4463 | unsigned Flags = 0; |
| 4464 | if (WriteMem) |
| 4465 | Flags |= MachineMemOperand::MOStore; |
| 4466 | if (ReadMem) |
| 4467 | Flags |= MachineMemOperand::MOLoad; |
| 4468 | if (Vol) |
| 4469 | Flags |= MachineMemOperand::MOVolatile; |
| 4470 | MachineMemOperand *MMO = |
Chris Lattner | e9ba5dd | 2010-09-21 04:57:15 +0000 | [diff] [blame] | 4471 | MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4472 | |
| 4473 | return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO); |
| 4474 | } |
| 4475 | |
| 4476 | SDValue |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4477 | SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList, |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4478 | const SDValue *Ops, unsigned NumOps, |
| 4479 | EVT MemVT, MachineMemOperand *MMO) { |
| 4480 | assert((Opcode == ISD::INTRINSIC_VOID || |
| 4481 | Opcode == ISD::INTRINSIC_W_CHAIN || |
Dale Johannesen | 1de4aa9 | 2010-10-26 23:11:10 +0000 | [diff] [blame] | 4482 | Opcode == ISD::PREFETCH || |
Nadav Rotem | c05d306 | 2012-09-06 09:17:37 +0000 | [diff] [blame] | 4483 | Opcode == ISD::LIFETIME_START || |
| 4484 | Opcode == ISD::LIFETIME_END || |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4485 | (Opcode <= INT_MAX && |
| 4486 | (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) && |
| 4487 | "Opcode is not a memory-accessing opcode!"); |
| 4488 | |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4489 | // Memoize the node unless it returns a flag. |
| 4490 | MemIntrinsicSDNode *N; |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 4491 | if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) { |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4492 | FoldingSetNodeID ID; |
| 4493 | AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps); |
Pete Cooper | 32ecfb4 | 2012-07-30 20:23:19 +0000 | [diff] [blame] | 4494 | ID.AddInteger(MMO->getPointerInfo().getAddrSpace()); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4495 | void *IP = 0; |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4496 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { |
| 4497 | cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4498 | return SDValue(E, 0); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4499 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4500 | |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4501 | N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(), |
| 4502 | dl.getDebugLoc(), VTList, Ops, |
| 4503 | NumOps, MemVT, MMO); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4504 | CSEMap.InsertNode(N, IP); |
| 4505 | } else { |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4506 | N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(), |
| 4507 | dl.getDebugLoc(), VTList, Ops, |
| 4508 | NumOps, MemVT, MMO); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4509 | } |
| 4510 | AllNodes.push_back(N); |
| 4511 | return SDValue(N, 0); |
| 4512 | } |
| 4513 | |
Chris Lattner | d0e139f | 2010-09-21 17:24:05 +0000 | [diff] [blame] | 4514 | /// InferPointerInfo - If the specified ptr/offset is a frame index, infer a |
| 4515 | /// MachinePointerInfo record from it. This is particularly useful because the |
| 4516 | /// code generator has many cases where it doesn't bother passing in a |
| 4517 | /// MachinePointerInfo to getLoad or getStore when it has "FI+Cst". |
| 4518 | static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) { |
| 4519 | // If this is FI+Offset, we can model it. |
| 4520 | if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) |
| 4521 | return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset); |
| 4522 | |
| 4523 | // If this is (FI+Offset1)+Offset2, we can model it. |
| 4524 | if (Ptr.getOpcode() != ISD::ADD || |
| 4525 | !isa<ConstantSDNode>(Ptr.getOperand(1)) || |
| 4526 | !isa<FrameIndexSDNode>(Ptr.getOperand(0))) |
| 4527 | return MachinePointerInfo(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4528 | |
Chris Lattner | d0e139f | 2010-09-21 17:24:05 +0000 | [diff] [blame] | 4529 | int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex(); |
| 4530 | return MachinePointerInfo::getFixedStack(FI, Offset+ |
| 4531 | cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue()); |
| 4532 | } |
| 4533 | |
| 4534 | /// InferPointerInfo - If the specified ptr/offset is a frame index, infer a |
| 4535 | /// MachinePointerInfo record from it. This is particularly useful because the |
| 4536 | /// code generator has many cases where it doesn't bother passing in a |
| 4537 | /// MachinePointerInfo to getLoad or getStore when it has "FI+Cst". |
| 4538 | static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) { |
| 4539 | // If the 'Offset' value isn't a constant, we can't handle this. |
| 4540 | if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp)) |
| 4541 | return InferPointerInfo(Ptr, OffsetNode->getSExtValue()); |
| 4542 | if (OffsetOp.getOpcode() == ISD::UNDEF) |
| 4543 | return InferPointerInfo(Ptr); |
| 4544 | return MachinePointerInfo(); |
| 4545 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4546 | |
Chris Lattner | d0e139f | 2010-09-21 17:24:05 +0000 | [diff] [blame] | 4547 | |
Chris Lattner | 5c5cb2a | 2010-09-21 05:10:45 +0000 | [diff] [blame] | 4548 | SDValue |
| 4549 | SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4550 | EVT VT, SDLoc dl, SDValue Chain, |
Chris Lattner | 5c5cb2a | 2010-09-21 05:10:45 +0000 | [diff] [blame] | 4551 | SDValue Ptr, SDValue Offset, |
| 4552 | MachinePointerInfo PtrInfo, EVT MemVT, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4553 | bool isVolatile, bool isNonTemporal, bool isInvariant, |
Rafael Espindola | 95d594c | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 4554 | unsigned Alignment, const MDNode *TBAAInfo, |
| 4555 | const MDNode *Ranges) { |
Michael Ilseman | 06b9690 | 2012-09-10 16:56:31 +0000 | [diff] [blame] | 4556 | assert(Chain.getValueType() == MVT::Other && |
Nadav Rotem | aeb86fa | 2011-07-14 10:37:54 +0000 | [diff] [blame] | 4557 | "Invalid chain type"); |
Chris Lattner | 5c5cb2a | 2010-09-21 05:10:45 +0000 | [diff] [blame] | 4558 | if (Alignment == 0) // Ensure that codegen never sees alignment 0 |
| 4559 | Alignment = getEVTAlignment(VT); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4560 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4561 | unsigned Flags = MachineMemOperand::MOLoad; |
| 4562 | if (isVolatile) |
| 4563 | Flags |= MachineMemOperand::MOVolatile; |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4564 | if (isNonTemporal) |
| 4565 | Flags |= MachineMemOperand::MONonTemporal; |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4566 | if (isInvariant) |
| 4567 | Flags |= MachineMemOperand::MOInvariant; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4568 | |
Chris Lattner | d0e139f | 2010-09-21 17:24:05 +0000 | [diff] [blame] | 4569 | // If we don't have a PtrInfo, infer the trivial frame index case to simplify |
| 4570 | // clients. |
| 4571 | if (PtrInfo.V == 0) |
| 4572 | PtrInfo = InferPointerInfo(Ptr, Offset); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4573 | |
Chris Lattner | d0e139f | 2010-09-21 17:24:05 +0000 | [diff] [blame] | 4574 | MachineFunction &MF = getMachineFunction(); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4575 | MachineMemOperand *MMO = |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 4576 | MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment, |
Rafael Espindola | 95d594c | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 4577 | TBAAInfo, Ranges); |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 4578 | return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4579 | } |
| 4580 | |
| 4581 | SDValue |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4582 | SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4583 | EVT VT, SDLoc dl, SDValue Chain, |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4584 | SDValue Ptr, SDValue Offset, EVT MemVT, |
| 4585 | MachineMemOperand *MMO) { |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4586 | if (VT == MemVT) { |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4587 | ExtType = ISD::NON_EXTLOAD; |
| 4588 | } else if (ExtType == ISD::NON_EXTLOAD) { |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4589 | assert(VT == MemVT && "Non-extending load from different memory type!"); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4590 | } else { |
| 4591 | // Extending load. |
Dan Gohman | 2e141d7 | 2009-12-14 23:40:38 +0000 | [diff] [blame] | 4592 | assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) && |
| 4593 | "Should only be an extending load, not truncating!"); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4594 | assert(VT.isInteger() == MemVT.isInteger() && |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4595 | "Cannot convert from FP to Int or Int -> FP!"); |
Dan Gohman | 2e141d7 | 2009-12-14 23:40:38 +0000 | [diff] [blame] | 4596 | assert(VT.isVector() == MemVT.isVector() && |
| 4597 | "Cannot use trunc store to convert to or from a vector!"); |
| 4598 | assert((!VT.isVector() || |
| 4599 | VT.getVectorNumElements() == MemVT.getVectorNumElements()) && |
| 4600 | "Cannot use trunc store to change the number of vector elements!"); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
| 4603 | bool Indexed = AM != ISD::UNINDEXED; |
| 4604 | assert((Indexed || Offset.getOpcode() == ISD::UNDEF) && |
| 4605 | "Unindexed load with an offset!"); |
| 4606 | |
| 4607 | SDVTList VTs = Indexed ? |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4608 | getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4609 | SDValue Ops[] = { Chain, Ptr, Offset }; |
| 4610 | FoldingSetNodeID ID; |
| 4611 | AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3); |
Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 4612 | ID.AddInteger(MemVT.getRawBits()); |
David Greene | 1157f79 | 2010-02-17 20:21:42 +0000 | [diff] [blame] | 4613 | ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(), |
Michael Ilseman | 06b9690 | 2012-09-10 16:56:31 +0000 | [diff] [blame] | 4614 | MMO->isNonTemporal(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4615 | MMO->isInvariant())); |
Pete Cooper | 32ecfb4 | 2012-07-30 20:23:19 +0000 | [diff] [blame] | 4616 | ID.AddInteger(MMO->getPointerInfo().getAddrSpace()); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4617 | void *IP = 0; |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4618 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { |
| 4619 | cast<LoadSDNode>(E)->refineAlignment(MMO); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4620 | return SDValue(E, 0); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4621 | } |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4622 | SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(), |
| 4623 | dl.getDebugLoc(), VTs, AM, ExtType, |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 4624 | MemVT, MMO); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4625 | CSEMap.InsertNode(N, IP); |
| 4626 | AllNodes.push_back(N); |
| 4627 | return SDValue(N, 0); |
| 4628 | } |
| 4629 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4630 | SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl, |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4631 | SDValue Chain, SDValue Ptr, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4632 | MachinePointerInfo PtrInfo, |
| 4633 | bool isVolatile, bool isNonTemporal, |
Michael Ilseman | 06b9690 | 2012-09-10 16:56:31 +0000 | [diff] [blame] | 4634 | bool isInvariant, unsigned Alignment, |
Rafael Espindola | 95d594c | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 4635 | const MDNode *TBAAInfo, |
| 4636 | const MDNode *Ranges) { |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4637 | SDValue Undef = getUNDEF(Ptr.getValueType()); |
| 4638 | return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef, |
Rafael Espindola | 95d594c | 2012-03-31 18:14:00 +0000 | [diff] [blame] | 4639 | PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment, |
| 4640 | TBAAInfo, Ranges); |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4641 | } |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4642 | |
Richard Sandiford | 66589dc | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 4643 | SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl, |
| 4644 | SDValue Chain, SDValue Ptr, |
| 4645 | MachineMemOperand *MMO) { |
| 4646 | SDValue Undef = getUNDEF(Ptr.getValueType()); |
| 4647 | return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef, |
| 4648 | VT, MMO); |
| 4649 | } |
| 4650 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4651 | SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT, |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4652 | SDValue Chain, SDValue Ptr, |
| 4653 | MachinePointerInfo PtrInfo, EVT MemVT, |
| 4654 | bool isVolatile, bool isNonTemporal, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 4655 | unsigned Alignment, const MDNode *TBAAInfo) { |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4656 | SDValue Undef = getUNDEF(Ptr.getValueType()); |
| 4657 | return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4658 | PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 4659 | TBAAInfo); |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
| 4662 | |
Richard Sandiford | 66589dc | 2013-10-28 11:17:59 +0000 | [diff] [blame] | 4663 | SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT, |
| 4664 | SDValue Chain, SDValue Ptr, EVT MemVT, |
| 4665 | MachineMemOperand *MMO) { |
| 4666 | SDValue Undef = getUNDEF(Ptr.getValueType()); |
| 4667 | return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, |
| 4668 | MemVT, MMO); |
| 4669 | } |
| 4670 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4671 | SDValue |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4672 | SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base, |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4673 | SDValue Offset, ISD::MemIndexedMode AM) { |
| 4674 | LoadSDNode *LD = cast<LoadSDNode>(OrigLoad); |
| 4675 | assert(LD->getOffset().getOpcode() == ISD::UNDEF && |
| 4676 | "Load is already a indexed load!"); |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 4677 | return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl, |
Chris Lattner | d0e139f | 2010-09-21 17:24:05 +0000 | [diff] [blame] | 4678 | LD->getChain(), Base, Offset, LD->getPointerInfo(), |
Michael Ilseman | 06b9690 | 2012-09-10 16:56:31 +0000 | [diff] [blame] | 4679 | LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4680 | false, LD->getAlignment()); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4683 | SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val, |
Chris Lattner | 5c5cb2a | 2010-09-21 05:10:45 +0000 | [diff] [blame] | 4684 | SDValue Ptr, MachinePointerInfo PtrInfo, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4685 | bool isVolatile, bool isNonTemporal, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 4686 | unsigned Alignment, const MDNode *TBAAInfo) { |
Michael Ilseman | 06b9690 | 2012-09-10 16:56:31 +0000 | [diff] [blame] | 4687 | assert(Chain.getValueType() == MVT::Other && |
Nadav Rotem | aeb86fa | 2011-07-14 10:37:54 +0000 | [diff] [blame] | 4688 | "Invalid chain type"); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4689 | if (Alignment == 0) // Ensure that codegen never sees alignment 0 |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4690 | Alignment = getEVTAlignment(Val.getValueType()); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4691 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4692 | unsigned Flags = MachineMemOperand::MOStore; |
| 4693 | if (isVolatile) |
| 4694 | Flags |= MachineMemOperand::MOVolatile; |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4695 | if (isNonTemporal) |
| 4696 | Flags |= MachineMemOperand::MONonTemporal; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4697 | |
Chris Lattner | d0e139f | 2010-09-21 17:24:05 +0000 | [diff] [blame] | 4698 | if (PtrInfo.V == 0) |
| 4699 | PtrInfo = InferPointerInfo(Ptr); |
| 4700 | |
| 4701 | MachineFunction &MF = getMachineFunction(); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4702 | MachineMemOperand *MMO = |
Chris Lattner | 5c5cb2a | 2010-09-21 05:10:45 +0000 | [diff] [blame] | 4703 | MF.getMachineMemOperand(PtrInfo, Flags, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 4704 | Val.getValueType().getStoreSize(), Alignment, |
| 4705 | TBAAInfo); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4706 | |
| 4707 | return getStore(Chain, dl, Val, Ptr, MMO); |
| 4708 | } |
| 4709 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4710 | SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val, |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4711 | SDValue Ptr, MachineMemOperand *MMO) { |
Michael Ilseman | 06b9690 | 2012-09-10 16:56:31 +0000 | [diff] [blame] | 4712 | assert(Chain.getValueType() == MVT::Other && |
Nadav Rotem | aeb86fa | 2011-07-14 10:37:54 +0000 | [diff] [blame] | 4713 | "Invalid chain type"); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4714 | EVT VT = Val.getValueType(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4715 | SDVTList VTs = getVTList(MVT::Other); |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 4716 | SDValue Undef = getUNDEF(Ptr.getValueType()); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4717 | SDValue Ops[] = { Chain, Val, Ptr, Undef }; |
| 4718 | FoldingSetNodeID ID; |
| 4719 | AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4720 | ID.AddInteger(VT.getRawBits()); |
David Greene | 1157f79 | 2010-02-17 20:21:42 +0000 | [diff] [blame] | 4721 | ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4722 | MMO->isNonTemporal(), MMO->isInvariant())); |
Pete Cooper | 32ecfb4 | 2012-07-30 20:23:19 +0000 | [diff] [blame] | 4723 | ID.AddInteger(MMO->getPointerInfo().getAddrSpace()); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4724 | void *IP = 0; |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4725 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { |
| 4726 | cast<StoreSDNode>(E)->refineAlignment(MMO); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4727 | return SDValue(E, 0); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4728 | } |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4729 | SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(), |
| 4730 | dl.getDebugLoc(), VTs, |
| 4731 | ISD::UNINDEXED, false, VT, MMO); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4732 | CSEMap.InsertNode(N, IP); |
| 4733 | AllNodes.push_back(N); |
| 4734 | return SDValue(N, 0); |
| 4735 | } |
| 4736 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4737 | SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val, |
Chris Lattner | 5c5cb2a | 2010-09-21 05:10:45 +0000 | [diff] [blame] | 4738 | SDValue Ptr, MachinePointerInfo PtrInfo, |
| 4739 | EVT SVT,bool isVolatile, bool isNonTemporal, |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 4740 | unsigned Alignment, |
| 4741 | const MDNode *TBAAInfo) { |
Michael Ilseman | 06b9690 | 2012-09-10 16:56:31 +0000 | [diff] [blame] | 4742 | assert(Chain.getValueType() == MVT::Other && |
Nadav Rotem | aeb86fa | 2011-07-14 10:37:54 +0000 | [diff] [blame] | 4743 | "Invalid chain type"); |
Chris Lattner | 5c5cb2a | 2010-09-21 05:10:45 +0000 | [diff] [blame] | 4744 | if (Alignment == 0) // Ensure that codegen never sees alignment 0 |
| 4745 | Alignment = getEVTAlignment(SVT); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4746 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4747 | unsigned Flags = MachineMemOperand::MOStore; |
| 4748 | if (isVolatile) |
| 4749 | Flags |= MachineMemOperand::MOVolatile; |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 4750 | if (isNonTemporal) |
| 4751 | Flags |= MachineMemOperand::MONonTemporal; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4752 | |
Chris Lattner | d0e139f | 2010-09-21 17:24:05 +0000 | [diff] [blame] | 4753 | if (PtrInfo.V == 0) |
| 4754 | PtrInfo = InferPointerInfo(Ptr); |
| 4755 | |
| 4756 | MachineFunction &MF = getMachineFunction(); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4757 | MachineMemOperand *MMO = |
Dan Gohman | f96e4bd | 2010-10-20 00:31:05 +0000 | [diff] [blame] | 4758 | MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment, |
| 4759 | TBAAInfo); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4760 | |
| 4761 | return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO); |
| 4762 | } |
| 4763 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4764 | SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val, |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4765 | SDValue Ptr, EVT SVT, |
| 4766 | MachineMemOperand *MMO) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4767 | EVT VT = Val.getValueType(); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4768 | |
Michael Ilseman | 06b9690 | 2012-09-10 16:56:31 +0000 | [diff] [blame] | 4769 | assert(Chain.getValueType() == MVT::Other && |
Nadav Rotem | aeb86fa | 2011-07-14 10:37:54 +0000 | [diff] [blame] | 4770 | "Invalid chain type"); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4771 | if (VT == SVT) |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4772 | return getStore(Chain, dl, Val, Ptr, MMO); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4773 | |
Dan Gohman | 2e141d7 | 2009-12-14 23:40:38 +0000 | [diff] [blame] | 4774 | assert(SVT.getScalarType().bitsLT(VT.getScalarType()) && |
| 4775 | "Should only be a truncating store, not extending!"); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4776 | assert(VT.isInteger() == SVT.isInteger() && |
| 4777 | "Can't do FP-INT conversion!"); |
Dan Gohman | 2e141d7 | 2009-12-14 23:40:38 +0000 | [diff] [blame] | 4778 | assert(VT.isVector() == SVT.isVector() && |
| 4779 | "Cannot use trunc store to convert to or from a vector!"); |
| 4780 | assert((!VT.isVector() || |
| 4781 | VT.getVectorNumElements() == SVT.getVectorNumElements()) && |
| 4782 | "Cannot use trunc store to change the number of vector elements!"); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4783 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4784 | SDVTList VTs = getVTList(MVT::Other); |
Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 4785 | SDValue Undef = getUNDEF(Ptr.getValueType()); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4786 | SDValue Ops[] = { Chain, Val, Ptr, Undef }; |
| 4787 | FoldingSetNodeID ID; |
| 4788 | AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4789 | ID.AddInteger(SVT.getRawBits()); |
David Greene | 1157f79 | 2010-02-17 20:21:42 +0000 | [diff] [blame] | 4790 | ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4791 | MMO->isNonTemporal(), MMO->isInvariant())); |
Pete Cooper | 32ecfb4 | 2012-07-30 20:23:19 +0000 | [diff] [blame] | 4792 | ID.AddInteger(MMO->getPointerInfo().getAddrSpace()); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4793 | void *IP = 0; |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4794 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { |
| 4795 | cast<StoreSDNode>(E)->refineAlignment(MMO); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4796 | return SDValue(E, 0); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4797 | } |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4798 | SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(), |
| 4799 | dl.getDebugLoc(), VTs, |
| 4800 | ISD::UNINDEXED, true, SVT, MMO); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4801 | CSEMap.InsertNode(N, IP); |
| 4802 | AllNodes.push_back(N); |
| 4803 | return SDValue(N, 0); |
| 4804 | } |
| 4805 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4806 | SDValue |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4807 | SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base, |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4808 | SDValue Offset, ISD::MemIndexedMode AM) { |
| 4809 | StoreSDNode *ST = cast<StoreSDNode>(OrigStore); |
| 4810 | assert(ST->getOffset().getOpcode() == ISD::UNDEF && |
| 4811 | "Store is already a indexed store!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4812 | SDVTList VTs = getVTList(Base.getValueType(), MVT::Other); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4813 | SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset }; |
| 4814 | FoldingSetNodeID ID; |
| 4815 | AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4816 | ID.AddInteger(ST->getMemoryVT().getRawBits()); |
Dan Gohman | a7ce741 | 2009-02-03 00:08:45 +0000 | [diff] [blame] | 4817 | ID.AddInteger(ST->getRawSubclassData()); |
Pete Cooper | 32ecfb4 | 2012-07-30 20:23:19 +0000 | [diff] [blame] | 4818 | ID.AddInteger(ST->getPointerInfo().getAddrSpace()); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4819 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 4820 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4821 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 4822 | |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4823 | SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(), |
| 4824 | dl.getDebugLoc(), VTs, AM, |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 4825 | ST->isTruncatingStore(), |
| 4826 | ST->getMemoryVT(), |
| 4827 | ST->getMemOperand()); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 4828 | CSEMap.InsertNode(N, IP); |
| 4829 | AllNodes.push_back(N); |
| 4830 | return SDValue(N, 0); |
| 4831 | } |
| 4832 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4833 | SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl, |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 4834 | SDValue Chain, SDValue Ptr, |
Rafael Espindola | 72d13ff | 2010-06-26 18:22:20 +0000 | [diff] [blame] | 4835 | SDValue SV, |
| 4836 | unsigned Align) { |
| 4837 | SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) }; |
| 4838 | return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4); |
Dale Johannesen | 0f502f6 | 2009-02-03 22:26:09 +0000 | [diff] [blame] | 4839 | } |
| 4840 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4841 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4842 | const SDUse *Ops, unsigned NumOps) { |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 4843 | switch (NumOps) { |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4844 | case 0: return getNode(Opcode, DL, VT); |
| 4845 | case 1: return getNode(Opcode, DL, VT, Ops[0]); |
| 4846 | case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]); |
| 4847 | case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]); |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 4848 | default: break; |
| 4849 | } |
| 4850 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4851 | // Copy from an SDUse array into an SDValue array for use with |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 4852 | // the regular getNode logic. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4853 | SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps); |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4854 | return getNode(Opcode, DL, VT, &NewOps[0], NumOps); |
Dan Gohman | 6d9cdd5 | 2008-07-07 18:26:29 +0000 | [diff] [blame] | 4855 | } |
| 4856 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4857 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4858 | const SDValue *Ops, unsigned NumOps) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 4859 | switch (NumOps) { |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4860 | case 0: return getNode(Opcode, DL, VT); |
| 4861 | case 1: return getNode(Opcode, DL, VT, Ops[0]); |
| 4862 | case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]); |
| 4863 | case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]); |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 4864 | default: break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 4865 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4866 | |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 4867 | switch (Opcode) { |
| 4868 | default: break; |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 4869 | case ISD::SELECT_CC: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 4870 | assert(NumOps == 5 && "SELECT_CC takes 5 operands!"); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 4871 | assert(Ops[0].getValueType() == Ops[1].getValueType() && |
| 4872 | "LHS and RHS of condition must have same type!"); |
| 4873 | assert(Ops[2].getValueType() == Ops[3].getValueType() && |
| 4874 | "True and False arms of SelectCC must have same type!"); |
| 4875 | assert(Ops[2].getValueType() == VT && |
| 4876 | "select_cc node must be of same type as true and false value!"); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 4877 | break; |
| 4878 | } |
| 4879 | case ISD::BR_CC: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 4880 | assert(NumOps == 5 && "BR_CC takes 5 operands!"); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 4881 | assert(Ops[2].getValueType() == Ops[3].getValueType() && |
| 4882 | "LHS/RHS of comparison should match types!"); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 4883 | break; |
| 4884 | } |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 4885 | } |
| 4886 | |
Chris Lattner | 385328c | 2005-05-14 07:42:29 +0000 | [diff] [blame] | 4887 | // Memoize nodes. |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 4888 | SDNode *N; |
Chris Lattner | 0b3e525 | 2006-08-15 19:11:05 +0000 | [diff] [blame] | 4889 | SDVTList VTs = getVTList(VT); |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4890 | |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 4891 | if (VT != MVT::Glue) { |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 4892 | FoldingSetNodeID ID; |
| 4893 | AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps); |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 4894 | void *IP = 0; |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4895 | |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 4896 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4897 | return SDValue(E, 0); |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4898 | |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4899 | N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), |
| 4900 | VTs, Ops, NumOps); |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 4901 | CSEMap.InsertNode(N, IP); |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 4902 | } else { |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4903 | N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), |
| 4904 | VTs, Ops, NumOps); |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 4905 | } |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4906 | |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 4907 | AllNodes.push_back(N); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 4908 | #ifndef NDEBUG |
Duncan Sands | 59d2dad | 2010-11-20 11:25:00 +0000 | [diff] [blame] | 4909 | VerifySDNode(N); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 4910 | #endif |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4911 | return SDValue(N, 0); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4914 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, |
Benjamin Kramer | 3853f74 | 2013-03-07 20:33:29 +0000 | [diff] [blame] | 4915 | ArrayRef<EVT> ResultTys, |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4916 | const SDValue *Ops, unsigned NumOps) { |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 4917 | return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()), |
Chris Lattner | 2fa6d3b | 2006-08-14 23:31:51 +0000 | [diff] [blame] | 4918 | Ops, NumOps); |
| 4919 | } |
| 4920 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4921 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4922 | const EVT *VTs, unsigned NumVTs, |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4923 | const SDValue *Ops, unsigned NumOps) { |
Chris Lattner | 2fa6d3b | 2006-08-14 23:31:51 +0000 | [diff] [blame] | 4924 | if (NumVTs == 1) |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4925 | return getNode(Opcode, DL, VTs[0], Ops, NumOps); |
| 4926 | return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4929 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList, |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4930 | const SDValue *Ops, unsigned NumOps) { |
Chris Lattner | be38416 | 2006-08-16 22:57:46 +0000 | [diff] [blame] | 4931 | if (VTList.NumVTs == 1) |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4932 | return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps); |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 4933 | |
Daniel Dunbar | cfb8a1b | 2009-07-19 01:38:38 +0000 | [diff] [blame] | 4934 | #if 0 |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 4935 | switch (Opcode) { |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 4936 | // FIXME: figure out how to safely handle things like |
| 4937 | // int foo(int x) { return 1 << (x & 255); } |
| 4938 | // int bar() { return foo(256); } |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 4939 | case ISD::SRA_PARTS: |
| 4940 | case ISD::SRL_PARTS: |
| 4941 | case ISD::SHL_PARTS: |
| 4942 | if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4943 | cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1) |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4944 | return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0)); |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 4945 | else if (N3.getOpcode() == ISD::AND) |
| 4946 | if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) { |
| 4947 | // If the and is only masking out bits that cannot effect the shift, |
| 4948 | // eliminate the and. |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 4949 | unsigned NumBits = VT.getScalarType().getSizeInBits()*2; |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 4950 | if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 4951 | return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0)); |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 4952 | } |
| 4953 | break; |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 4954 | } |
Daniel Dunbar | cfb8a1b | 2009-07-19 01:38:38 +0000 | [diff] [blame] | 4955 | #endif |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 4956 | |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 4957 | // Memoize the node unless it returns a flag. |
| 4958 | SDNode *N; |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 4959 | if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) { |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 4960 | FoldingSetNodeID ID; |
| 4961 | AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps); |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 4962 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 4963 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4964 | return SDValue(E, 0); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 4965 | |
Dan Gohman | 0e5f130 | 2008-07-07 23:02:41 +0000 | [diff] [blame] | 4966 | if (NumOps == 1) { |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4967 | N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(), |
| 4968 | DL.getDebugLoc(), VTList, Ops[0]); |
Dan Gohman | 0e5f130 | 2008-07-07 23:02:41 +0000 | [diff] [blame] | 4969 | } else if (NumOps == 2) { |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4970 | N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(), |
| 4971 | DL.getDebugLoc(), VTList, Ops[0], |
| 4972 | Ops[1]); |
Dan Gohman | 0e5f130 | 2008-07-07 23:02:41 +0000 | [diff] [blame] | 4973 | } else if (NumOps == 3) { |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4974 | N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(), |
| 4975 | DL.getDebugLoc(), VTList, Ops[0], |
| 4976 | Ops[1], Ops[2]); |
Dan Gohman | 0e5f130 | 2008-07-07 23:02:41 +0000 | [diff] [blame] | 4977 | } else { |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4978 | N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), |
| 4979 | VTList, Ops, NumOps); |
Dan Gohman | 0e5f130 | 2008-07-07 23:02:41 +0000 | [diff] [blame] | 4980 | } |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 4981 | CSEMap.InsertNode(N, IP); |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 4982 | } else { |
Dan Gohman | 0e5f130 | 2008-07-07 23:02:41 +0000 | [diff] [blame] | 4983 | if (NumOps == 1) { |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4984 | N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(), |
| 4985 | DL.getDebugLoc(), VTList, Ops[0]); |
Dan Gohman | 0e5f130 | 2008-07-07 23:02:41 +0000 | [diff] [blame] | 4986 | } else if (NumOps == 2) { |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4987 | N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(), |
| 4988 | DL.getDebugLoc(), VTList, Ops[0], |
| 4989 | Ops[1]); |
Dan Gohman | 0e5f130 | 2008-07-07 23:02:41 +0000 | [diff] [blame] | 4990 | } else if (NumOps == 3) { |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4991 | N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(), |
| 4992 | DL.getDebugLoc(), VTList, Ops[0], |
| 4993 | Ops[1], Ops[2]); |
Dan Gohman | 0e5f130 | 2008-07-07 23:02:41 +0000 | [diff] [blame] | 4994 | } else { |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 4995 | N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), |
| 4996 | VTList, Ops, NumOps); |
Dan Gohman | 0e5f130 | 2008-07-07 23:02:41 +0000 | [diff] [blame] | 4997 | } |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 4998 | } |
Chris Lattner | 5fa4fa4 | 2005-05-14 06:42:57 +0000 | [diff] [blame] | 4999 | AllNodes.push_back(N); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 5000 | #ifndef NDEBUG |
Duncan Sands | 59d2dad | 2010-11-20 11:25:00 +0000 | [diff] [blame] | 5001 | VerifySDNode(N); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 5002 | #endif |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5003 | return SDValue(N, 0); |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 5004 | } |
| 5005 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5006 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) { |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 5007 | return getNode(Opcode, DL, VTList, 0, 0); |
Dan Gohman | 08ce976 | 2007-10-08 15:49:58 +0000 | [diff] [blame] | 5008 | } |
| 5009 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5010 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList, |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 5011 | SDValue N1) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5012 | SDValue Ops[] = { N1 }; |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 5013 | return getNode(Opcode, DL, VTList, Ops, 1); |
Dan Gohman | 08ce976 | 2007-10-08 15:49:58 +0000 | [diff] [blame] | 5014 | } |
| 5015 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5016 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList, |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 5017 | SDValue N1, SDValue N2) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5018 | SDValue Ops[] = { N1, N2 }; |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 5019 | return getNode(Opcode, DL, VTList, Ops, 2); |
Dan Gohman | 08ce976 | 2007-10-08 15:49:58 +0000 | [diff] [blame] | 5020 | } |
| 5021 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5022 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList, |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 5023 | SDValue N1, SDValue N2, SDValue N3) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5024 | SDValue Ops[] = { N1, N2, N3 }; |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 5025 | return getNode(Opcode, DL, VTList, Ops, 3); |
Dan Gohman | 08ce976 | 2007-10-08 15:49:58 +0000 | [diff] [blame] | 5026 | } |
| 5027 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5028 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList, |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 5029 | SDValue N1, SDValue N2, SDValue N3, |
| 5030 | SDValue N4) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5031 | SDValue Ops[] = { N1, N2, N3, N4 }; |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 5032 | return getNode(Opcode, DL, VTList, Ops, 4); |
Dan Gohman | 08ce976 | 2007-10-08 15:49:58 +0000 | [diff] [blame] | 5033 | } |
| 5034 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5035 | SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList, |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 5036 | SDValue N1, SDValue N2, SDValue N3, |
| 5037 | SDValue N4, SDValue N5) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5038 | SDValue Ops[] = { N1, N2, N3, N4, N5 }; |
Bill Wendling | 7ade28c | 2009-01-28 22:17:52 +0000 | [diff] [blame] | 5039 | return getNode(Opcode, DL, VTList, Ops, 5); |
Dan Gohman | 08ce976 | 2007-10-08 15:49:58 +0000 | [diff] [blame] | 5040 | } |
| 5041 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5042 | SDVTList SelectionDAG::getVTList(EVT VT) { |
Duncan Sands | af47b11 | 2007-10-16 09:56:48 +0000 | [diff] [blame] | 5043 | return makeVTList(SDNode::getValueTypeList(VT), 1); |
Chris Lattner | a325511 | 2005-11-08 23:30:28 +0000 | [diff] [blame] | 5044 | } |
| 5045 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5046 | SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) { |
Wan Xiaofei | 8c955ea | 2013-10-22 08:02:02 +0000 | [diff] [blame] | 5047 | FoldingSetNodeID ID; |
| 5048 | ID.AddInteger(2U); |
| 5049 | ID.AddInteger(VT1.getRawBits()); |
| 5050 | ID.AddInteger(VT2.getRawBits()); |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5051 | |
Wan Xiaofei | 8c955ea | 2013-10-22 08:02:02 +0000 | [diff] [blame] | 5052 | void *IP = 0; |
| 5053 | SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP); |
| 5054 | if (Result == NULL) { |
| 5055 | EVT *Array = Allocator.Allocate<EVT>(2); |
| 5056 | Array[0] = VT1; |
| 5057 | Array[1] = VT2; |
| 5058 | Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2); |
| 5059 | VTListMap.InsertNode(Result, IP); |
| 5060 | } |
| 5061 | return Result->getSDVTList(); |
Chris Lattner | a325511 | 2005-11-08 23:30:28 +0000 | [diff] [blame] | 5062 | } |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5063 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5064 | SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) { |
Wan Xiaofei | 8c955ea | 2013-10-22 08:02:02 +0000 | [diff] [blame] | 5065 | FoldingSetNodeID ID; |
| 5066 | ID.AddInteger(3U); |
| 5067 | ID.AddInteger(VT1.getRawBits()); |
| 5068 | ID.AddInteger(VT2.getRawBits()); |
| 5069 | ID.AddInteger(VT3.getRawBits()); |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5070 | |
Wan Xiaofei | 8c955ea | 2013-10-22 08:02:02 +0000 | [diff] [blame] | 5071 | void *IP = 0; |
| 5072 | SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP); |
| 5073 | if (Result == NULL) { |
| 5074 | EVT *Array = Allocator.Allocate<EVT>(3); |
| 5075 | Array[0] = VT1; |
| 5076 | Array[1] = VT2; |
| 5077 | Array[2] = VT3; |
| 5078 | Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3); |
| 5079 | VTListMap.InsertNode(Result, IP); |
| 5080 | } |
| 5081 | return Result->getSDVTList(); |
Chris Lattner | 70046e9 | 2006-08-15 17:46:01 +0000 | [diff] [blame] | 5082 | } |
| 5083 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5084 | SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) { |
Wan Xiaofei | 8c955ea | 2013-10-22 08:02:02 +0000 | [diff] [blame] | 5085 | FoldingSetNodeID ID; |
| 5086 | ID.AddInteger(4U); |
| 5087 | ID.AddInteger(VT1.getRawBits()); |
| 5088 | ID.AddInteger(VT2.getRawBits()); |
| 5089 | ID.AddInteger(VT3.getRawBits()); |
| 5090 | ID.AddInteger(VT4.getRawBits()); |
Bill Wendling | 13d6d44 | 2008-12-01 23:28:22 +0000 | [diff] [blame] | 5091 | |
Wan Xiaofei | 8c955ea | 2013-10-22 08:02:02 +0000 | [diff] [blame] | 5092 | void *IP = 0; |
| 5093 | SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP); |
| 5094 | if (Result == NULL) { |
| 5095 | EVT *Array = Allocator.Allocate<EVT>(4); |
| 5096 | Array[0] = VT1; |
| 5097 | Array[1] = VT2; |
| 5098 | Array[2] = VT3; |
| 5099 | Array[3] = VT4; |
| 5100 | Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4); |
| 5101 | VTListMap.InsertNode(Result, IP); |
| 5102 | } |
| 5103 | return Result->getSDVTList(); |
Bill Wendling | 13d6d44 | 2008-12-01 23:28:22 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5106 | SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) { |
Wan Xiaofei | 8c955ea | 2013-10-22 08:02:02 +0000 | [diff] [blame] | 5107 | FoldingSetNodeID ID; |
| 5108 | ID.AddInteger(NumVTs); |
| 5109 | for (unsigned index = 0; index < NumVTs; index++) { |
| 5110 | ID.AddInteger(VTs[index].getRawBits()); |
Chris Lattner | 70046e9 | 2006-08-15 17:46:01 +0000 | [diff] [blame] | 5111 | } |
| 5112 | |
Wan Xiaofei | 8c955ea | 2013-10-22 08:02:02 +0000 | [diff] [blame] | 5113 | void *IP = 0; |
| 5114 | SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP); |
| 5115 | if (Result == NULL) { |
| 5116 | EVT *Array = Allocator.Allocate<EVT>(NumVTs); |
| 5117 | std::copy(VTs, VTs + NumVTs, Array); |
| 5118 | Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs); |
| 5119 | VTListMap.InsertNode(Result, IP); |
Chris Lattner | 70046e9 | 2006-08-15 17:46:01 +0000 | [diff] [blame] | 5120 | } |
Wan Xiaofei | 8c955ea | 2013-10-22 08:02:02 +0000 | [diff] [blame] | 5121 | return Result->getSDVTList(); |
Chris Lattner | 2fa6d3b | 2006-08-14 23:31:51 +0000 | [diff] [blame] | 5122 | } |
| 5123 | |
| 5124 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5125 | /// UpdateNodeOperands - *Mutate* the specified node in-place to have the |
| 5126 | /// specified operands. If the resultant node already exists in the DAG, |
| 5127 | /// this does not modify the specified node, instead it returns the node that |
| 5128 | /// already exists. If the resultant node does not exist in the DAG, the |
| 5129 | /// input node is returned. As a degenerate case, if you specify the same |
| 5130 | /// input operands as the node already has, the input node is returned. |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5131 | SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) { |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5132 | assert(N->getNumOperands() == 1 && "Update with wrong number of operands"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5133 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5134 | // Check to see if there is no change. |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5135 | if (Op == N->getOperand(0)) return N; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5136 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5137 | // See if the modified node already exists. |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 5138 | void *InsertPos = 0; |
| 5139 | if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos)) |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5140 | return Existing; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5141 | |
Dan Gohman | 79acd2b | 2008-07-21 22:38:59 +0000 | [diff] [blame] | 5142 | // Nope it doesn't. Remove the node from its current place in the maps. |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 5143 | if (InsertPos) |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 5144 | if (!RemoveNodeFromCSEMaps(N)) |
| 5145 | InsertPos = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5146 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5147 | // Now we update the operands. |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5148 | N->OperandList[0].set(Op); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5149 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5150 | // If this gets put into a CSE map, add it. |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 5151 | if (InsertPos) CSEMap.InsertNode(N, InsertPos); |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5152 | return N; |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5155 | SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) { |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5156 | assert(N->getNumOperands() == 2 && "Update with wrong number of operands"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5157 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5158 | // Check to see if there is no change. |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5159 | if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1)) |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5160 | return N; // No operands changed, just return the input node. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5161 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5162 | // See if the modified node already exists. |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 5163 | void *InsertPos = 0; |
| 5164 | if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos)) |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5165 | return Existing; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5166 | |
Dan Gohman | 79acd2b | 2008-07-21 22:38:59 +0000 | [diff] [blame] | 5167 | // Nope it doesn't. Remove the node from its current place in the maps. |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 5168 | if (InsertPos) |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 5169 | if (!RemoveNodeFromCSEMaps(N)) |
| 5170 | InsertPos = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5171 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5172 | // Now we update the operands. |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5173 | if (N->OperandList[0] != Op1) |
| 5174 | N->OperandList[0].set(Op1); |
| 5175 | if (N->OperandList[1] != Op2) |
| 5176 | N->OperandList[1].set(Op2); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5177 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5178 | // If this gets put into a CSE map, add it. |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 5179 | if (InsertPos) CSEMap.InsertNode(N, InsertPos); |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5180 | return N; |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5181 | } |
| 5182 | |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5183 | SDNode *SelectionDAG:: |
| 5184 | UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5185 | SDValue Ops[] = { Op1, Op2, Op3 }; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 5186 | return UpdateNodeOperands(N, Ops, 3); |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5187 | } |
| 5188 | |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5189 | SDNode *SelectionDAG:: |
| 5190 | UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5191 | SDValue Op3, SDValue Op4) { |
| 5192 | SDValue Ops[] = { Op1, Op2, Op3, Op4 }; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 5193 | return UpdateNodeOperands(N, Ops, 4); |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5194 | } |
| 5195 | |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5196 | SDNode *SelectionDAG:: |
| 5197 | UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5198 | SDValue Op3, SDValue Op4, SDValue Op5) { |
| 5199 | SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 }; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 5200 | return UpdateNodeOperands(N, Ops, 5); |
Chris Lattner | 809ec11 | 2006-01-28 10:09:25 +0000 | [diff] [blame] | 5201 | } |
| 5202 | |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5203 | SDNode *SelectionDAG:: |
| 5204 | UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 5205 | assert(N->getNumOperands() == NumOps && |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5206 | "Update with wrong number of operands"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5207 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5208 | // Check to see if there is no change. |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5209 | bool AnyChange = false; |
| 5210 | for (unsigned i = 0; i != NumOps; ++i) { |
| 5211 | if (Ops[i] != N->getOperand(i)) { |
| 5212 | AnyChange = true; |
| 5213 | break; |
| 5214 | } |
| 5215 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5216 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5217 | // No operands changed, just return the input node. |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5218 | if (!AnyChange) return N; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5219 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5220 | // See if the modified node already exists. |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 5221 | void *InsertPos = 0; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 5222 | if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos)) |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5223 | return Existing; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5224 | |
Dan Gohman | 7ceda16 | 2008-05-02 00:05:03 +0000 | [diff] [blame] | 5225 | // Nope it doesn't. Remove the node from its current place in the maps. |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 5226 | if (InsertPos) |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 5227 | if (!RemoveNodeFromCSEMaps(N)) |
| 5228 | InsertPos = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5229 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5230 | // Now we update the operands. |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5231 | for (unsigned i = 0; i != NumOps; ++i) |
| 5232 | if (N->OperandList[i] != Ops[i]) |
| 5233 | N->OperandList[i].set(Ops[i]); |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5234 | |
| 5235 | // If this gets put into a CSE map, add it. |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 5236 | if (InsertPos) CSEMap.InsertNode(N, InsertPos); |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 5237 | return N; |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 5238 | } |
| 5239 | |
Dan Gohman | 0fe9c6e | 2008-07-07 20:57:48 +0000 | [diff] [blame] | 5240 | /// DropOperands - Release the operands and set this node to have |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5241 | /// zero operands. |
Dan Gohman | 0fe9c6e | 2008-07-07 20:57:48 +0000 | [diff] [blame] | 5242 | void SDNode::DropOperands() { |
Dan Gohman | 0fe9c6e | 2008-07-07 20:57:48 +0000 | [diff] [blame] | 5243 | // Unlike the code in MorphNodeTo that does this, we don't need to |
| 5244 | // watch for dead nodes here. |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5245 | for (op_iterator I = op_begin(), E = op_end(); I != E; ) { |
| 5246 | SDUse &Use = *I++; |
| 5247 | Use.set(SDValue()); |
| 5248 | } |
Chris Lattner | d429bcd | 2007-02-04 02:49:29 +0000 | [diff] [blame] | 5249 | } |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 5250 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5251 | /// SelectNodeTo - These are wrappers around MorphNodeTo that accept a |
| 5252 | /// machine opcode. |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 5253 | /// |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5254 | SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5255 | EVT VT) { |
Chris Lattner | 0b3e525 | 2006-08-15 19:11:05 +0000 | [diff] [blame] | 5256 | SDVTList VTs = getVTList(VT); |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5257 | return SelectNodeTo(N, MachineOpc, VTs, 0, 0); |
Chris Lattner | 7651fa4 | 2005-08-24 23:00:29 +0000 | [diff] [blame] | 5258 | } |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 5259 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5260 | SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5261 | EVT VT, SDValue Op1) { |
Chris Lattner | 0b3e525 | 2006-08-15 19:11:05 +0000 | [diff] [blame] | 5262 | SDVTList VTs = getVTList(VT); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5263 | SDValue Ops[] = { Op1 }; |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5264 | return SelectNodeTo(N, MachineOpc, VTs, Ops, 1); |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 5265 | } |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 5266 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5267 | SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5268 | EVT VT, SDValue Op1, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5269 | SDValue Op2) { |
Chris Lattner | 0b3e525 | 2006-08-15 19:11:05 +0000 | [diff] [blame] | 5270 | SDVTList VTs = getVTList(VT); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5271 | SDValue Ops[] = { Op1, Op2 }; |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5272 | return SelectNodeTo(N, MachineOpc, VTs, Ops, 2); |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 5273 | } |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 5274 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5275 | SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5276 | EVT VT, SDValue Op1, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5277 | SDValue Op2, SDValue Op3) { |
Chris Lattner | 0b3e525 | 2006-08-15 19:11:05 +0000 | [diff] [blame] | 5278 | SDVTList VTs = getVTList(VT); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5279 | SDValue Ops[] = { Op1, Op2, Op3 }; |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5280 | return SelectNodeTo(N, MachineOpc, VTs, Ops, 3); |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 5281 | } |
Chris Lattner | c975e1d | 2005-08-21 22:30:30 +0000 | [diff] [blame] | 5282 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5283 | SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5284 | EVT VT, const SDValue *Ops, |
Evan Cheng | 694481e | 2006-08-27 08:08:54 +0000 | [diff] [blame] | 5285 | unsigned NumOps) { |
Chris Lattner | 0b3e525 | 2006-08-15 19:11:05 +0000 | [diff] [blame] | 5286 | SDVTList VTs = getVTList(VT); |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5287 | return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps); |
Dan Gohman | cd920d9 | 2008-07-02 23:23:19 +0000 | [diff] [blame] | 5288 | } |
| 5289 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5290 | SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5291 | EVT VT1, EVT VT2, const SDValue *Ops, |
Dan Gohman | cd920d9 | 2008-07-02 23:23:19 +0000 | [diff] [blame] | 5292 | unsigned NumOps) { |
| 5293 | SDVTList VTs = getVTList(VT1, VT2); |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5294 | return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps); |
Dan Gohman | cd920d9 | 2008-07-02 23:23:19 +0000 | [diff] [blame] | 5295 | } |
| 5296 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5297 | SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5298 | EVT VT1, EVT VT2) { |
Dan Gohman | cd920d9 | 2008-07-02 23:23:19 +0000 | [diff] [blame] | 5299 | SDVTList VTs = getVTList(VT1, VT2); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5300 | return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0); |
Dan Gohman | cd920d9 | 2008-07-02 23:23:19 +0000 | [diff] [blame] | 5301 | } |
| 5302 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5303 | SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5304 | EVT VT1, EVT VT2, EVT VT3, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5305 | const SDValue *Ops, unsigned NumOps) { |
Dan Gohman | cd920d9 | 2008-07-02 23:23:19 +0000 | [diff] [blame] | 5306 | SDVTList VTs = getVTList(VT1, VT2, VT3); |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5307 | return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps); |
Dan Gohman | cd920d9 | 2008-07-02 23:23:19 +0000 | [diff] [blame] | 5308 | } |
| 5309 | |
Bill Wendling | 13d6d44 | 2008-12-01 23:28:22 +0000 | [diff] [blame] | 5310 | SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5311 | EVT VT1, EVT VT2, EVT VT3, EVT VT4, |
Bill Wendling | 13d6d44 | 2008-12-01 23:28:22 +0000 | [diff] [blame] | 5312 | const SDValue *Ops, unsigned NumOps) { |
| 5313 | SDVTList VTs = getVTList(VT1, VT2, VT3, VT4); |
| 5314 | return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps); |
| 5315 | } |
| 5316 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5317 | SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5318 | EVT VT1, EVT VT2, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5319 | SDValue Op1) { |
Dan Gohman | cd920d9 | 2008-07-02 23:23:19 +0000 | [diff] [blame] | 5320 | SDVTList VTs = getVTList(VT1, VT2); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5321 | SDValue Ops[] = { Op1 }; |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5322 | return SelectNodeTo(N, MachineOpc, VTs, Ops, 1); |
Andrew Lenharth | 7cf11b4 | 2006-01-23 21:51:14 +0000 | [diff] [blame] | 5323 | } |
Andrew Lenharth | 8c6f1ee | 2006-01-23 20:59:12 +0000 | [diff] [blame] | 5324 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5325 | SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5326 | EVT VT1, EVT VT2, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5327 | SDValue Op1, SDValue Op2) { |
Chris Lattner | 0b3e525 | 2006-08-15 19:11:05 +0000 | [diff] [blame] | 5328 | SDVTList VTs = getVTList(VT1, VT2); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5329 | SDValue Ops[] = { Op1, Op2 }; |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5330 | return SelectNodeTo(N, MachineOpc, VTs, Ops, 2); |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5333 | SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5334 | EVT VT1, EVT VT2, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5335 | SDValue Op1, SDValue Op2, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5336 | SDValue Op3) { |
Chris Lattner | 0b3e525 | 2006-08-15 19:11:05 +0000 | [diff] [blame] | 5337 | SDVTList VTs = getVTList(VT1, VT2); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5338 | SDValue Ops[] = { Op1, Op2, Op3 }; |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5339 | return SelectNodeTo(N, MachineOpc, VTs, Ops, 3); |
Dan Gohman | cd920d9 | 2008-07-02 23:23:19 +0000 | [diff] [blame] | 5340 | } |
| 5341 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5342 | SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5343 | EVT VT1, EVT VT2, EVT VT3, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5344 | SDValue Op1, SDValue Op2, |
Bill Wendling | 13d6d44 | 2008-12-01 23:28:22 +0000 | [diff] [blame] | 5345 | SDValue Op3) { |
| 5346 | SDVTList VTs = getVTList(VT1, VT2, VT3); |
| 5347 | SDValue Ops[] = { Op1, Op2, Op3 }; |
| 5348 | return SelectNodeTo(N, MachineOpc, VTs, Ops, 3); |
| 5349 | } |
| 5350 | |
| 5351 | SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5352 | SDVTList VTs, const SDValue *Ops, |
Dan Gohman | cd920d9 | 2008-07-02 23:23:19 +0000 | [diff] [blame] | 5353 | unsigned NumOps) { |
Chris Lattner | 5857e0a | 2010-02-23 23:01:35 +0000 | [diff] [blame] | 5354 | N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps); |
| 5355 | // Reset the NodeID to -1. |
| 5356 | N->setNodeId(-1); |
| 5357 | return N; |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5358 | } |
| 5359 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5360 | /// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away |
Devang Patel | 0508d04 | 2011-12-15 18:21:18 +0000 | [diff] [blame] | 5361 | /// the line number information on the merged node since it is not possible to |
| 5362 | /// preserve the information that operation is associated with multiple lines. |
| 5363 | /// This will make the debugger working better at -O0, were there is a higher |
| 5364 | /// probability having other instructions associated with that line. |
| 5365 | /// |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5366 | /// For IROrder, we keep the smaller of the two |
| 5367 | SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) { |
Devang Patel | 0508d04 | 2011-12-15 18:21:18 +0000 | [diff] [blame] | 5368 | DebugLoc NLoc = N->getDebugLoc(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5369 | if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) && |
| 5370 | (OLoc.getDebugLoc() != NLoc)) { |
Devang Patel | 0508d04 | 2011-12-15 18:21:18 +0000 | [diff] [blame] | 5371 | N->setDebugLoc(DebugLoc()); |
| 5372 | } |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5373 | unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder()); |
| 5374 | N->setIROrder(Order); |
Devang Patel | 0508d04 | 2011-12-15 18:21:18 +0000 | [diff] [blame] | 5375 | return N; |
| 5376 | } |
| 5377 | |
Chris Lattner | 21221e3 | 2010-02-28 21:36:14 +0000 | [diff] [blame] | 5378 | /// MorphNodeTo - This *mutates* the specified node to have the specified |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5379 | /// return type, opcode, and operands. |
| 5380 | /// |
| 5381 | /// Note that MorphNodeTo returns the resultant node. If there is already a |
| 5382 | /// node of the specified opcode and operands, it returns that node instead of |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5383 | /// the current one. Note that the SDLoc need not be the same. |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5384 | /// |
| 5385 | /// Using MorphNodeTo is faster than creating a new node and swapping it in |
| 5386 | /// with ReplaceAllUsesWith both because it often avoids allocating a new |
Gabor Greif | dc71563 | 2008-08-30 22:16:05 +0000 | [diff] [blame] | 5387 | /// node, and because it doesn't require CSE recalculation for any of |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5388 | /// the node's users. |
| 5389 | /// |
| 5390 | SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5391 | SDVTList VTs, const SDValue *Ops, |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5392 | unsigned NumOps) { |
Dan Gohman | cd920d9 | 2008-07-02 23:23:19 +0000 | [diff] [blame] | 5393 | // If an identical node already exists, use it. |
Chris Lattner | a568285 | 2006-08-07 23:03:03 +0000 | [diff] [blame] | 5394 | void *IP = 0; |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 5395 | if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) { |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5396 | FoldingSetNodeID ID; |
| 5397 | AddNodeIDNode(ID, Opc, VTs, Ops, NumOps); |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 5398 | if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5399 | return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N)); |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5400 | } |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 5401 | |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 5402 | if (!RemoveNodeFromCSEMaps(N)) |
| 5403 | IP = 0; |
Chris Lattner | 67612a1 | 2007-02-04 02:32:44 +0000 | [diff] [blame] | 5404 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5405 | // Start the morphing. |
| 5406 | N->NodeType = Opc; |
| 5407 | N->ValueList = VTs.VTs; |
| 5408 | N->NumValues = VTs.NumVTs; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5409 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5410 | // Clear the operands list, updating used nodes to remove this from their |
| 5411 | // use list. Keep track of any operands that become dead as a result. |
| 5412 | SmallPtrSet<SDNode*, 16> DeadNodeSet; |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5413 | for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) { |
| 5414 | SDUse &Use = *I++; |
| 5415 | SDNode *Used = Use.getNode(); |
| 5416 | Use.set(SDValue()); |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5417 | if (Used->use_empty()) |
| 5418 | DeadNodeSet.insert(Used); |
| 5419 | } |
| 5420 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5421 | if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) { |
| 5422 | // Initialize the memory references information. |
| 5423 | MN->setMemRefs(0, 0); |
| 5424 | // If NumOps is larger than the # of operands we can have in a |
| 5425 | // MachineSDNode, reallocate the operand list. |
| 5426 | if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) { |
| 5427 | if (MN->OperandsNeedDelete) |
| 5428 | delete[] MN->OperandList; |
| 5429 | if (NumOps > array_lengthof(MN->LocalOperands)) |
| 5430 | // We're creating a final node that will live unmorphed for the |
| 5431 | // remainder of the current SelectionDAG iteration, so we can allocate |
| 5432 | // the operands directly out of a pool with no recycling metadata. |
| 5433 | MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps), |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 5434 | Ops, NumOps); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5435 | else |
| 5436 | MN->InitOperands(MN->LocalOperands, Ops, NumOps); |
| 5437 | MN->OperandsNeedDelete = false; |
| 5438 | } else |
| 5439 | MN->InitOperands(MN->OperandList, Ops, NumOps); |
| 5440 | } else { |
| 5441 | // If NumOps is larger than the # of operands we currently have, reallocate |
| 5442 | // the operand list. |
| 5443 | if (NumOps > N->NumOperands) { |
| 5444 | if (N->OperandsNeedDelete) |
| 5445 | delete[] N->OperandList; |
| 5446 | N->InitOperands(new SDUse[NumOps], Ops, NumOps); |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5447 | N->OperandsNeedDelete = true; |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5448 | } else |
Anton Korobeynikov | 443b215 | 2009-10-22 00:15:17 +0000 | [diff] [blame] | 5449 | N->InitOperands(N->OperandList, Ops, NumOps); |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5450 | } |
| 5451 | |
| 5452 | // Delete any nodes that are still dead after adding the uses for the |
| 5453 | // new operands. |
Chris Lattner | 7d892d6 | 2010-03-01 07:43:08 +0000 | [diff] [blame] | 5454 | if (!DeadNodeSet.empty()) { |
| 5455 | SmallVector<SDNode *, 16> DeadNodes; |
| 5456 | for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(), |
| 5457 | E = DeadNodeSet.end(); I != E; ++I) |
| 5458 | if ((*I)->use_empty()) |
| 5459 | DeadNodes.push_back(*I); |
| 5460 | RemoveDeadNodes(DeadNodes); |
| 5461 | } |
Dan Gohman | 0fe9c6e | 2008-07-07 20:57:48 +0000 | [diff] [blame] | 5462 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5463 | if (IP) |
| 5464 | CSEMap.InsertNode(N, IP); // Memoize the new node. |
Evan Cheng | 95514ba | 2006-08-26 08:00:10 +0000 | [diff] [blame] | 5465 | return N; |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 5466 | } |
| 5467 | |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 5468 | |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 5469 | /// getMachineNode - These are used for target selectors to create a new node |
| 5470 | /// with specified return type(s), MachineInstr opcode, and operands. |
Evan Cheng | 6ae46c4 | 2006-02-09 07:15:23 +0000 | [diff] [blame] | 5471 | /// |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 5472 | /// Note that getMachineNode returns the resultant node. If there is already a |
Evan Cheng | 6ae46c4 | 2006-02-09 07:15:23 +0000 | [diff] [blame] | 5473 | /// node of the specified opcode and operands, it returns that node instead of |
| 5474 | /// the current one. |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5475 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5476 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) { |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5477 | SDVTList VTs = getVTList(VT); |
Dmitri Gribenko | 5c332db | 2013-05-05 00:40:33 +0000 | [diff] [blame] | 5478 | return getMachineNode(Opcode, dl, VTs, None); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 5479 | } |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5480 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5481 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5482 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) { |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5483 | SDVTList VTs = getVTList(VT); |
| 5484 | SDValue Ops[] = { Op1 }; |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5485 | return getMachineNode(Opcode, dl, VTs, Ops); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 5486 | } |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5487 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5488 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5489 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5490 | SDValue Op1, SDValue Op2) { |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5491 | SDVTList VTs = getVTList(VT); |
| 5492 | SDValue Ops[] = { Op1, Op2 }; |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5493 | return getMachineNode(Opcode, dl, VTs, Ops); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 5494 | } |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5495 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5496 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5497 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5498 | SDValue Op1, SDValue Op2, SDValue Op3) { |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5499 | SDVTList VTs = getVTList(VT); |
| 5500 | SDValue Ops[] = { Op1, Op2, Op3 }; |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5501 | return getMachineNode(Opcode, dl, VTs, Ops); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 5502 | } |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5503 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5504 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5505 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5506 | ArrayRef<SDValue> Ops) { |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5507 | SDVTList VTs = getVTList(VT); |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5508 | return getMachineNode(Opcode, dl, VTs, Ops); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 5509 | } |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5510 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5511 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5512 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) { |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5513 | SDVTList VTs = getVTList(VT1, VT2); |
Dmitri Gribenko | 5c332db | 2013-05-05 00:40:33 +0000 | [diff] [blame] | 5514 | return getMachineNode(Opcode, dl, VTs, None); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 5515 | } |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5516 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5517 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5518 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5519 | EVT VT1, EVT VT2, SDValue Op1) { |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5520 | SDVTList VTs = getVTList(VT1, VT2); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5521 | SDValue Ops[] = { Op1 }; |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5522 | return getMachineNode(Opcode, dl, VTs, Ops); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 5523 | } |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5524 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5525 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5526 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5527 | EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) { |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5528 | SDVTList VTs = getVTList(VT1, VT2); |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5529 | SDValue Ops[] = { Op1, Op2 }; |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5530 | return getMachineNode(Opcode, dl, VTs, Ops); |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5531 | } |
| 5532 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5533 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5534 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5535 | EVT VT1, EVT VT2, SDValue Op1, |
| 5536 | SDValue Op2, SDValue Op3) { |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5537 | SDVTList VTs = getVTList(VT1, VT2); |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5538 | SDValue Ops[] = { Op1, Op2, Op3 }; |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5539 | return getMachineNode(Opcode, dl, VTs, Ops); |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5540 | } |
| 5541 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5542 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5543 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5544 | EVT VT1, EVT VT2, |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5545 | ArrayRef<SDValue> Ops) { |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5546 | SDVTList VTs = getVTList(VT1, VT2); |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5547 | return getMachineNode(Opcode, dl, VTs, Ops); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 5548 | } |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5549 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5550 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5551 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5552 | EVT VT1, EVT VT2, EVT VT3, |
| 5553 | SDValue Op1, SDValue Op2) { |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5554 | SDVTList VTs = getVTList(VT1, VT2, VT3); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 5555 | SDValue Ops[] = { Op1, Op2 }; |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5556 | return getMachineNode(Opcode, dl, VTs, Ops); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 5557 | } |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5558 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5559 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5560 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5561 | EVT VT1, EVT VT2, EVT VT3, |
| 5562 | SDValue Op1, SDValue Op2, SDValue Op3) { |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5563 | SDVTList VTs = getVTList(VT1, VT2, VT3); |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5564 | SDValue Ops[] = { Op1, Op2, Op3 }; |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5565 | return getMachineNode(Opcode, dl, VTs, Ops); |
Evan Cheng | 6ae46c4 | 2006-02-09 07:15:23 +0000 | [diff] [blame] | 5566 | } |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5567 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5568 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5569 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5570 | EVT VT1, EVT VT2, EVT VT3, |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5571 | ArrayRef<SDValue> Ops) { |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5572 | SDVTList VTs = getVTList(VT1, VT2, VT3); |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5573 | return getMachineNode(Opcode, dl, VTs, Ops); |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 5574 | } |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5575 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5576 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5577 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5578 | EVT VT2, EVT VT3, EVT VT4, |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5579 | ArrayRef<SDValue> Ops) { |
Dan Gohman | fc16657 | 2009-04-09 23:54:40 +0000 | [diff] [blame] | 5580 | SDVTList VTs = getVTList(VT1, VT2, VT3, VT4); |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5581 | return getMachineNode(Opcode, dl, VTs, Ops); |
Bill Wendling | 56ab1a2 | 2009-01-29 09:01:55 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5584 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5585 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, |
Benjamin Kramer | 3853f74 | 2013-03-07 20:33:29 +0000 | [diff] [blame] | 5586 | ArrayRef<EVT> ResultTys, |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5587 | ArrayRef<SDValue> Ops) { |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5588 | SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size()); |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5589 | return getMachineNode(Opcode, dl, VTs, Ops); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5590 | } |
| 5591 | |
Dan Gohman | c81b783 | 2009-10-10 01:29:16 +0000 | [diff] [blame] | 5592 | MachineSDNode * |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5593 | SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs, |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5594 | ArrayRef<SDValue> OpsArray) { |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 5595 | bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue; |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5596 | MachineSDNode *N; |
Ted Kremenek | 584520e | 2011-01-23 17:05:06 +0000 | [diff] [blame] | 5597 | void *IP = 0; |
Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 5598 | const SDValue *Ops = OpsArray.data(); |
| 5599 | unsigned NumOps = OpsArray.size(); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5600 | |
| 5601 | if (DoCSE) { |
| 5602 | FoldingSetNodeID ID; |
| 5603 | AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps); |
| 5604 | IP = 0; |
Devang Patel | 0508d04 | 2011-12-15 18:21:18 +0000 | [diff] [blame] | 5605 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5606 | return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL)); |
Devang Patel | 0508d04 | 2011-12-15 18:21:18 +0000 | [diff] [blame] | 5607 | } |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5608 | } |
| 5609 | |
| 5610 | // Allocate a new MachineSDNode. |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 5611 | N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(), |
| 5612 | DL.getDebugLoc(), VTs); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5613 | |
| 5614 | // Initialize the operands list. |
| 5615 | if (NumOps > array_lengthof(N->LocalOperands)) |
| 5616 | // We're creating a final node that will live unmorphed for the |
| 5617 | // remainder of the current SelectionDAG iteration, so we can allocate |
| 5618 | // the operands directly out of a pool with no recycling metadata. |
| 5619 | N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps), |
| 5620 | Ops, NumOps); |
| 5621 | else |
| 5622 | N->InitOperands(N->LocalOperands, Ops, NumOps); |
| 5623 | N->OperandsNeedDelete = false; |
| 5624 | |
| 5625 | if (DoCSE) |
| 5626 | CSEMap.InsertNode(N, IP); |
| 5627 | |
| 5628 | AllNodes.push_back(N); |
| 5629 | #ifndef NDEBUG |
Duncan Sands | 59d2dad | 2010-11-20 11:25:00 +0000 | [diff] [blame] | 5630 | VerifyMachineNode(N); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 5631 | #endif |
| 5632 | return N; |
Dale Johannesen | e8c1733 | 2009-01-29 00:47:48 +0000 | [diff] [blame] | 5633 | } |
Evan Cheng | 6ae46c4 | 2006-02-09 07:15:23 +0000 | [diff] [blame] | 5634 | |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 5635 | /// getTargetExtractSubreg - A convenience function for creating |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 5636 | /// TargetOpcode::EXTRACT_SUBREG nodes. |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 5637 | SDValue |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5638 | SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT, |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 5639 | SDValue Operand) { |
| 5640 | SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 5641 | SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 5642 | VT, Operand, SRIdxVal); |
Dan Gohman | 6a402dc | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 5643 | return SDValue(Subreg, 0); |
| 5644 | } |
| 5645 | |
Bob Wilson | 5fcbf0d | 2009-10-08 18:49:46 +0000 | [diff] [blame] | 5646 | /// getTargetInsertSubreg - A convenience function for creating |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 5647 | /// TargetOpcode::INSERT_SUBREG nodes. |
Bob Wilson | 5fcbf0d | 2009-10-08 18:49:46 +0000 | [diff] [blame] | 5648 | SDValue |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5649 | SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT, |
Bob Wilson | 5fcbf0d | 2009-10-08 18:49:46 +0000 | [diff] [blame] | 5650 | SDValue Operand, SDValue Subreg) { |
| 5651 | SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 5652 | SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL, |
Bob Wilson | 5fcbf0d | 2009-10-08 18:49:46 +0000 | [diff] [blame] | 5653 | VT, Operand, Subreg, SRIdxVal); |
| 5654 | return SDValue(Result, 0); |
| 5655 | } |
| 5656 | |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 5657 | /// getNodeIfExists - Get the specified node if it's already available, or |
| 5658 | /// else return NULL. |
| 5659 | SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5660 | const SDValue *Ops, unsigned NumOps) { |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 5661 | if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) { |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 5662 | FoldingSetNodeID ID; |
| 5663 | AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps); |
| 5664 | void *IP = 0; |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 5665 | if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) |
Evan Cheng | 08b1173 | 2008-03-22 01:55:50 +0000 | [diff] [blame] | 5666 | return E; |
| 5667 | } |
| 5668 | return NULL; |
| 5669 | } |
| 5670 | |
Evan Cheng | 31441b7 | 2010-03-29 20:48:30 +0000 | [diff] [blame] | 5671 | /// getDbgValue - Creates a SDDbgValue node. |
| 5672 | /// |
| 5673 | SDDbgValue * |
| 5674 | SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off, |
| 5675 | DebugLoc DL, unsigned O) { |
| 5676 | return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O); |
| 5677 | } |
| 5678 | |
| 5679 | SDDbgValue * |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 5680 | SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off, |
Evan Cheng | 31441b7 | 2010-03-29 20:48:30 +0000 | [diff] [blame] | 5681 | DebugLoc DL, unsigned O) { |
| 5682 | return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O); |
| 5683 | } |
| 5684 | |
| 5685 | SDDbgValue * |
| 5686 | SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off, |
| 5687 | DebugLoc DL, unsigned O) { |
| 5688 | return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O); |
| 5689 | } |
| 5690 | |
Dan Gohman | a72d2a2 | 2010-03-03 21:33:37 +0000 | [diff] [blame] | 5691 | namespace { |
| 5692 | |
Dan Gohman | ba72b0c | 2010-03-04 19:11:28 +0000 | [diff] [blame] | 5693 | /// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node |
Dan Gohman | a72d2a2 | 2010-03-03 21:33:37 +0000 | [diff] [blame] | 5694 | /// pointed to by a use iterator is deleted, increment the use iterator |
| 5695 | /// so that it doesn't dangle. |
| 5696 | /// |
Dan Gohman | a72d2a2 | 2010-03-03 21:33:37 +0000 | [diff] [blame] | 5697 | class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener { |
Dan Gohman | a72d2a2 | 2010-03-03 21:33:37 +0000 | [diff] [blame] | 5698 | SDNode::use_iterator &UI; |
| 5699 | SDNode::use_iterator &UE; |
| 5700 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 5701 | void NodeDeleted(SDNode *N, SDNode *E) override { |
Dan Gohman | a72d2a2 | 2010-03-03 21:33:37 +0000 | [diff] [blame] | 5702 | // Increment the iterator as needed. |
| 5703 | while (UI != UE && N == *UI) |
| 5704 | ++UI; |
Dan Gohman | a72d2a2 | 2010-03-03 21:33:37 +0000 | [diff] [blame] | 5705 | } |
| 5706 | |
| 5707 | public: |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5708 | RAUWUpdateListener(SelectionDAG &d, |
Dan Gohman | a72d2a2 | 2010-03-03 21:33:37 +0000 | [diff] [blame] | 5709 | SDNode::use_iterator &ui, |
| 5710 | SDNode::use_iterator &ue) |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5711 | : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {} |
Dan Gohman | a72d2a2 | 2010-03-03 21:33:37 +0000 | [diff] [blame] | 5712 | }; |
| 5713 | |
| 5714 | } |
| 5715 | |
Evan Cheng | 99157a0 | 2006-08-07 22:13:29 +0000 | [diff] [blame] | 5716 | /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 5717 | /// This can cause recursive merging of nodes in the DAG. |
| 5718 | /// |
Chris Lattner | 11d049c | 2008-02-03 03:35:22 +0000 | [diff] [blame] | 5719 | /// This version assumes From has a single result value. |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 5720 | /// |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5721 | void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5722 | SDNode *From = FromN.getNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5723 | assert(From->getNumValues() == 1 && FromN.getResNo() == 0 && |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 5724 | "Cannot replace with this method!"); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5725 | assert(From != To.getNode() && "Cannot replace uses of with self"); |
Roman Levenstein | dc1adac | 2008-04-07 10:06:32 +0000 | [diff] [blame] | 5726 | |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5727 | // Iterate over all the existing uses of From. New uses will be added |
| 5728 | // to the beginning of the use list, which we avoid visiting. |
| 5729 | // This specifically avoids visiting uses of From that arise while the |
| 5730 | // replacement is happening, because any such uses would be the result |
| 5731 | // of CSE: If an existing node looks like From after one of its operands |
| 5732 | // is replaced by To, we don't want to replace of all its users with To |
| 5733 | // too. See PR3018 for more info. |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 5734 | SDNode::use_iterator UI = From->use_begin(), UE = From->use_end(); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5735 | RAUWUpdateListener Listener(*this, UI, UE); |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 5736 | while (UI != UE) { |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5737 | SDNode *User = *UI; |
Roman Levenstein | dc1adac | 2008-04-07 10:06:32 +0000 | [diff] [blame] | 5738 | |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 5739 | // This node is about to morph, remove its old self from the CSE maps. |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5740 | RemoveNodeFromCSEMaps(User); |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 5741 | |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5742 | // A user can appear in a use list multiple times, and when this |
| 5743 | // happens the uses are usually next to each other in the list. |
| 5744 | // To help reduce the number of CSE recomputations, process all |
| 5745 | // the uses of this user that we can find this way. |
| 5746 | do { |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5747 | SDUse &Use = UI.getUse(); |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5748 | ++UI; |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5749 | Use.set(To); |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5750 | } while (UI != UE && *UI == User); |
| 5751 | |
| 5752 | // Now that we have modified User, add it back to the CSE maps. If it |
| 5753 | // already exists there, recursively merge the results together. |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5754 | AddModifiedNodeToCSEMaps(User); |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 5755 | } |
Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 5756 | |
| 5757 | // If we just RAUW'd the root, take note. |
| 5758 | if (FromN == getRoot()) |
| 5759 | setRoot(To); |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 5760 | } |
| 5761 | |
| 5762 | /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. |
| 5763 | /// This can cause recursive merging of nodes in the DAG. |
| 5764 | /// |
Dan Gohman | c23e496 | 2009-04-15 20:06:30 +0000 | [diff] [blame] | 5765 | /// This version assumes that for each value of From, there is a |
| 5766 | /// corresponding value in To in the same position with the same type. |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 5767 | /// |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5768 | void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) { |
Dan Gohman | c23e496 | 2009-04-15 20:06:30 +0000 | [diff] [blame] | 5769 | #ifndef NDEBUG |
| 5770 | for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) |
| 5771 | assert((!From->hasAnyUseOfValue(i) || |
| 5772 | From->getValueType(i) == To->getValueType(i)) && |
| 5773 | "Cannot use this version of ReplaceAllUsesWith!"); |
| 5774 | #endif |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5775 | |
| 5776 | // Handle the trivial case. |
| 5777 | if (From == To) |
| 5778 | return; |
| 5779 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 5780 | // Iterate over just the existing users of From. See the comments in |
| 5781 | // the ReplaceAllUsesWith above. |
| 5782 | SDNode::use_iterator UI = From->use_begin(), UE = From->use_end(); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5783 | RAUWUpdateListener Listener(*this, UI, UE); |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 5784 | while (UI != UE) { |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5785 | SDNode *User = *UI; |
Roman Levenstein | dc1adac | 2008-04-07 10:06:32 +0000 | [diff] [blame] | 5786 | |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 5787 | // This node is about to morph, remove its old self from the CSE maps. |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5788 | RemoveNodeFromCSEMaps(User); |
Roman Levenstein | dc1adac | 2008-04-07 10:06:32 +0000 | [diff] [blame] | 5789 | |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5790 | // A user can appear in a use list multiple times, and when this |
| 5791 | // happens the uses are usually next to each other in the list. |
| 5792 | // To help reduce the number of CSE recomputations, process all |
| 5793 | // the uses of this user that we can find this way. |
| 5794 | do { |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5795 | SDUse &Use = UI.getUse(); |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5796 | ++UI; |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5797 | Use.setNode(To); |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5798 | } while (UI != UE && *UI == User); |
| 5799 | |
| 5800 | // Now that we have modified User, add it back to the CSE maps. If it |
| 5801 | // already exists there, recursively merge the results together. |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5802 | AddModifiedNodeToCSEMaps(User); |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 5803 | } |
Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 5804 | |
| 5805 | // If we just RAUW'd the root, take note. |
| 5806 | if (From == getRoot().getNode()) |
| 5807 | setRoot(SDValue(To, getRoot().getResNo())); |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 5808 | } |
| 5809 | |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 5810 | /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. |
| 5811 | /// This can cause recursive merging of nodes in the DAG. |
| 5812 | /// |
| 5813 | /// This version can replace From with any result values. To must match the |
| 5814 | /// number and types of values returned by From. |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5815 | void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) { |
Chris Lattner | 11d049c | 2008-02-03 03:35:22 +0000 | [diff] [blame] | 5816 | if (From->getNumValues() == 1) // Handle the simple case efficiently. |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5817 | return ReplaceAllUsesWith(SDValue(From, 0), To[0]); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 5818 | |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 5819 | // Iterate over just the existing users of From. See the comments in |
| 5820 | // the ReplaceAllUsesWith above. |
| 5821 | SDNode::use_iterator UI = From->use_begin(), UE = From->use_end(); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5822 | RAUWUpdateListener Listener(*this, UI, UE); |
Dan Gohman | dbe664a | 2009-01-19 21:44:21 +0000 | [diff] [blame] | 5823 | while (UI != UE) { |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5824 | SDNode *User = *UI; |
Roman Levenstein | dc1adac | 2008-04-07 10:06:32 +0000 | [diff] [blame] | 5825 | |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 5826 | // This node is about to morph, remove its old self from the CSE maps. |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5827 | RemoveNodeFromCSEMaps(User); |
Roman Levenstein | dc1adac | 2008-04-07 10:06:32 +0000 | [diff] [blame] | 5828 | |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5829 | // A user can appear in a use list multiple times, and when this |
| 5830 | // happens the uses are usually next to each other in the list. |
| 5831 | // To help reduce the number of CSE recomputations, process all |
| 5832 | // the uses of this user that we can find this way. |
| 5833 | do { |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5834 | SDUse &Use = UI.getUse(); |
| 5835 | const SDValue &ToOp = To[Use.getResNo()]; |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5836 | ++UI; |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5837 | Use.set(ToOp); |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5838 | } while (UI != UE && *UI == User); |
| 5839 | |
| 5840 | // Now that we have modified User, add it back to the CSE maps. If it |
| 5841 | // already exists there, recursively merge the results together. |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5842 | AddModifiedNodeToCSEMaps(User); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 5843 | } |
Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 5844 | |
| 5845 | // If we just RAUW'd the root, take note. |
| 5846 | if (From == getRoot().getNode()) |
| 5847 | setRoot(SDValue(To[getRoot().getResNo()])); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 5848 | } |
| 5849 | |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 5850 | /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5851 | /// uses of other values produced by From.getNode() alone. The Deleted |
| 5852 | /// vector is handled the same way as for ReplaceAllUsesWith. |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5853 | void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){ |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5854 | // Handle the really simple, really trivial case efficiently. |
| 5855 | if (From == To) return; |
| 5856 | |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 5857 | // Handle the simple, trivial, case efficiently. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5858 | if (From.getNode()->getNumValues() == 1) { |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5859 | ReplaceAllUsesWith(From, To); |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 5860 | return; |
| 5861 | } |
Chris Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 5862 | |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5863 | // Iterate over just the existing users of From. See the comments in |
| 5864 | // the ReplaceAllUsesWith above. |
| 5865 | SDNode::use_iterator UI = From.getNode()->use_begin(), |
| 5866 | UE = From.getNode()->use_end(); |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5867 | RAUWUpdateListener Listener(*this, UI, UE); |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5868 | while (UI != UE) { |
| 5869 | SDNode *User = *UI; |
| 5870 | bool UserRemovedFromCSEMaps = false; |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 5871 | |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5872 | // A user can appear in a use list multiple times, and when this |
| 5873 | // happens the uses are usually next to each other in the list. |
| 5874 | // To help reduce the number of CSE recomputations, process all |
| 5875 | // the uses of this user that we can find this way. |
| 5876 | do { |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5877 | SDUse &Use = UI.getUse(); |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5878 | |
| 5879 | // Skip uses of different values from the same node. |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5880 | if (Use.getResNo() != From.getResNo()) { |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5881 | ++UI; |
| 5882 | continue; |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 5883 | } |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5884 | |
| 5885 | // If this node hasn't been modified yet, it's still in the CSE maps, |
| 5886 | // so remove its old self from the CSE maps. |
| 5887 | if (!UserRemovedFromCSEMaps) { |
| 5888 | RemoveNodeFromCSEMaps(User); |
| 5889 | UserRemovedFromCSEMaps = true; |
| 5890 | } |
| 5891 | |
| 5892 | ++UI; |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5893 | Use.set(To); |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5894 | } while (UI != UE && *UI == User); |
| 5895 | |
| 5896 | // We are iterating over all uses of the From node, so if a use |
| 5897 | // doesn't use the specific value, no changes are made. |
| 5898 | if (!UserRemovedFromCSEMaps) |
| 5899 | continue; |
| 5900 | |
Chris Lattner | 01d029b | 2007-10-15 06:10:22 +0000 | [diff] [blame] | 5901 | // Now that we have modified User, add it back to the CSE maps. If it |
| 5902 | // already exists there, recursively merge the results together. |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5903 | AddModifiedNodeToCSEMaps(User); |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5904 | } |
Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 5905 | |
| 5906 | // If we just RAUW'd the root, take note. |
| 5907 | if (From == getRoot()) |
| 5908 | setRoot(To); |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5909 | } |
| 5910 | |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5911 | namespace { |
| 5912 | /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith |
| 5913 | /// to record information about a use. |
| 5914 | struct UseMemo { |
| 5915 | SDNode *User; |
| 5916 | unsigned Index; |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5917 | SDUse *Use; |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5918 | }; |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5919 | |
| 5920 | /// operator< - Sort Memos by User. |
| 5921 | bool operator<(const UseMemo &L, const UseMemo &R) { |
| 5922 | return (intptr_t)L.User < (intptr_t)R.User; |
| 5923 | } |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5924 | } |
| 5925 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5926 | /// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5927 | /// uses of other values produced by From.getNode() alone. The same value |
| 5928 | /// may appear in both the From and To list. The Deleted vector is |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5929 | /// handled the same way as for ReplaceAllUsesWith. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5930 | void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From, |
| 5931 | const SDValue *To, |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5932 | unsigned Num){ |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5933 | // Handle the simple, trivial case efficiently. |
| 5934 | if (Num == 1) |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5935 | return ReplaceAllUsesOfValueWith(*From, *To); |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5936 | |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5937 | // Read up all the uses and make records of them. This helps |
| 5938 | // processing new uses that are introduced during the |
| 5939 | // replacement process. |
| 5940 | SmallVector<UseMemo, 4> Uses; |
| 5941 | for (unsigned i = 0; i != Num; ++i) { |
| 5942 | unsigned FromResNo = From[i].getResNo(); |
| 5943 | SDNode *FromNode = From[i].getNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5944 | for (SDNode::use_iterator UI = FromNode->use_begin(), |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5945 | E = FromNode->use_end(); UI != E; ++UI) { |
| 5946 | SDUse &Use = UI.getUse(); |
| 5947 | if (Use.getResNo() == FromResNo) { |
| 5948 | UseMemo Memo = { *UI, i, &Use }; |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5949 | Uses.push_back(Memo); |
| 5950 | } |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5951 | } |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5952 | } |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5953 | |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5954 | // Sort the uses, so that all the uses from a given User are together. |
| 5955 | std::sort(Uses.begin(), Uses.end()); |
| 5956 | |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5957 | for (unsigned UseIndex = 0, UseIndexEnd = Uses.size(); |
| 5958 | UseIndex != UseIndexEnd; ) { |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5959 | // We know that this user uses some value of From. If it is the right |
| 5960 | // value, update it. |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5961 | SDNode *User = Uses[UseIndex].User; |
| 5962 | |
| 5963 | // This node is about to morph, remove its old self from the CSE maps. |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5964 | RemoveNodeFromCSEMaps(User); |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5965 | |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5966 | // The Uses array is sorted, so all the uses for a given User |
| 5967 | // are next to each other in the list. |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5968 | // To help reduce the number of CSE recomputations, process all |
| 5969 | // the uses of this user that we can find this way. |
| 5970 | do { |
| 5971 | unsigned i = Uses[UseIndex].Index; |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5972 | SDUse &Use = *Uses[UseIndex].Use; |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5973 | ++UseIndex; |
| 5974 | |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 5975 | Use.set(To[i]); |
Dan Gohman | 3994610 | 2009-01-25 16:29:12 +0000 | [diff] [blame] | 5976 | } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User); |
| 5977 | |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 5978 | // Now that we have modified User, add it back to the CSE maps. If it |
| 5979 | // already exists there, recursively merge the results together. |
Jakob Stoklund Olesen | bc7d448 | 2012-04-20 22:08:46 +0000 | [diff] [blame] | 5980 | AddModifiedNodeToCSEMaps(User); |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 5981 | } |
| 5982 | } |
| 5983 | |
Evan Cheng | e6f35d8 | 2006-08-01 08:20:41 +0000 | [diff] [blame] | 5984 | /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG |
Evan Cheng | c384d6c | 2006-08-02 22:00:34 +0000 | [diff] [blame] | 5985 | /// based on their topological order. It returns the maximum id and a vector |
| 5986 | /// of the SDNodes* in assigned order by reference. |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 5987 | unsigned SelectionDAG::AssignTopologicalOrder() { |
Evan Cheng | c384d6c | 2006-08-02 22:00:34 +0000 | [diff] [blame] | 5988 | |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 5989 | unsigned DAGSize = 0; |
Evan Cheng | e6f35d8 | 2006-08-01 08:20:41 +0000 | [diff] [blame] | 5990 | |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 5991 | // SortedPos tracks the progress of the algorithm. Nodes before it are |
| 5992 | // sorted, nodes after it are unsorted. When the algorithm completes |
| 5993 | // it is at the end of the list. |
| 5994 | allnodes_iterator SortedPos = allnodes_begin(); |
| 5995 | |
Dan Gohman | 3ebd0ee | 2008-11-21 19:10:41 +0000 | [diff] [blame] | 5996 | // Visit all the nodes. Move nodes with no operands to the front of |
| 5997 | // the list immediately. Annotate nodes that do have operands with their |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 5998 | // operand count. Before we do this, the Node Id fields of the nodes |
| 5999 | // may contain arbitrary values. After, the Node Id fields for nodes |
| 6000 | // before SortedPos will contain the topological sort index, and the |
| 6001 | // Node Id fields for nodes At SortedPos and after will contain the |
| 6002 | // count of outstanding operands. |
| 6003 | for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) { |
| 6004 | SDNode *N = I++; |
David Greene | cf495bc | 2010-01-20 20:13:31 +0000 | [diff] [blame] | 6005 | checkForCycles(N); |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 6006 | unsigned Degree = N->getNumOperands(); |
| 6007 | if (Degree == 0) { |
| 6008 | // A node with no uses, add it to the result array immediately. |
| 6009 | N->setNodeId(DAGSize++); |
| 6010 | allnodes_iterator Q = N; |
| 6011 | if (Q != SortedPos) |
| 6012 | SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q)); |
David Greene | 221925e | 2010-01-20 00:59:23 +0000 | [diff] [blame] | 6013 | assert(SortedPos != AllNodes.end() && "Overran node list"); |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 6014 | ++SortedPos; |
| 6015 | } else { |
| 6016 | // Temporarily use the Node Id as scratch space for the degree count. |
| 6017 | N->setNodeId(Degree); |
Evan Cheng | e6f35d8 | 2006-08-01 08:20:41 +0000 | [diff] [blame] | 6018 | } |
| 6019 | } |
| 6020 | |
Chad Rosier | f496dea | 2012-05-21 17:13:41 +0000 | [diff] [blame] | 6021 | // Visit all the nodes. As we iterate, move nodes into sorted order, |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 6022 | // such that by the time the end is reached all nodes will be sorted. |
| 6023 | for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) { |
| 6024 | SDNode *N = I; |
David Greene | cf495bc | 2010-01-20 20:13:31 +0000 | [diff] [blame] | 6025 | checkForCycles(N); |
David Greene | 221925e | 2010-01-20 00:59:23 +0000 | [diff] [blame] | 6026 | // N is in sorted position, so all its uses have one less operand |
| 6027 | // that needs to be sorted. |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 6028 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
| 6029 | UI != UE; ++UI) { |
| 6030 | SDNode *P = *UI; |
| 6031 | unsigned Degree = P->getNodeId(); |
David Greene | 221925e | 2010-01-20 00:59:23 +0000 | [diff] [blame] | 6032 | assert(Degree != 0 && "Invalid node degree"); |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 6033 | --Degree; |
| 6034 | if (Degree == 0) { |
| 6035 | // All of P's operands are sorted, so P may sorted now. |
| 6036 | P->setNodeId(DAGSize++); |
| 6037 | if (P != SortedPos) |
| 6038 | SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P)); |
David Greene | 221925e | 2010-01-20 00:59:23 +0000 | [diff] [blame] | 6039 | assert(SortedPos != AllNodes.end() && "Overran node list"); |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 6040 | ++SortedPos; |
| 6041 | } else { |
| 6042 | // Update P's outstanding operand count. |
| 6043 | P->setNodeId(Degree); |
| 6044 | } |
| 6045 | } |
David Greene | 221925e | 2010-01-20 00:59:23 +0000 | [diff] [blame] | 6046 | if (I == SortedPos) { |
David Greene | 3914370 | 2010-02-09 23:03:05 +0000 | [diff] [blame] | 6047 | #ifndef NDEBUG |
| 6048 | SDNode *S = ++I; |
| 6049 | dbgs() << "Overran sorted position:\n"; |
David Greene | 221925e | 2010-01-20 00:59:23 +0000 | [diff] [blame] | 6050 | S->dumprFull(); |
David Greene | 3914370 | 2010-02-09 23:03:05 +0000 | [diff] [blame] | 6051 | #endif |
| 6052 | llvm_unreachable(0); |
David Greene | 221925e | 2010-01-20 00:59:23 +0000 | [diff] [blame] | 6053 | } |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 6054 | } |
| 6055 | |
| 6056 | assert(SortedPos == AllNodes.end() && |
| 6057 | "Topological sort incomplete!"); |
| 6058 | assert(AllNodes.front().getOpcode() == ISD::EntryToken && |
| 6059 | "First node in topological sort is not the entry token!"); |
| 6060 | assert(AllNodes.front().getNodeId() == 0 && |
| 6061 | "First node in topological sort has non-zero id!"); |
| 6062 | assert(AllNodes.front().getNumOperands() == 0 && |
| 6063 | "First node in topological sort has operands!"); |
| 6064 | assert(AllNodes.back().getNodeId() == (int)DAGSize-1 && |
| 6065 | "Last node in topologic sort has unexpected id!"); |
| 6066 | assert(AllNodes.back().use_empty() && |
| 6067 | "Last node in topologic sort has users!"); |
Dan Gohman | 3ebd0ee | 2008-11-21 19:10:41 +0000 | [diff] [blame] | 6068 | assert(DAGSize == allnodes_size() && "Node count mismatch!"); |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 6069 | return DAGSize; |
Evan Cheng | e6f35d8 | 2006-08-01 08:20:41 +0000 | [diff] [blame] | 6070 | } |
| 6071 | |
Evan Cheng | bfcb305 | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 6072 | /// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the |
| 6073 | /// value is produced by SD. |
Dale Johannesen | fdb42fa | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 6074 | void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) { |
| 6075 | DbgInfo->add(DB, SD, isParameter); |
Evan Cheng | bfcb305 | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 6076 | if (SD) |
| 6077 | SD->setHasDebugValue(true); |
Dale Johannesen | bfdf7f3 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 6078 | } |
Evan Cheng | 091cba1 | 2006-07-27 06:39:06 +0000 | [diff] [blame] | 6079 | |
Devang Patel | a2e868d | 2011-01-25 23:27:42 +0000 | [diff] [blame] | 6080 | /// TransferDbgValues - Transfer SDDbgValues. |
| 6081 | void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) { |
| 6082 | if (From == To || !From.getNode()->getHasDebugValue()) |
| 6083 | return; |
| 6084 | SDNode *FromNode = From.getNode(); |
| 6085 | SDNode *ToNode = To.getNode(); |
Benjamin Kramer | 22a54c1 | 2011-06-18 13:13:44 +0000 | [diff] [blame] | 6086 | ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode); |
Devang Patel | a778f5c | 2011-02-18 22:43:42 +0000 | [diff] [blame] | 6087 | SmallVector<SDDbgValue *, 2> ClonedDVs; |
Benjamin Kramer | 22a54c1 | 2011-06-18 13:13:44 +0000 | [diff] [blame] | 6088 | for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end(); |
Devang Patel | a2e868d | 2011-01-25 23:27:42 +0000 | [diff] [blame] | 6089 | I != E; ++I) { |
Devang Patel | a778f5c | 2011-02-18 22:43:42 +0000 | [diff] [blame] | 6090 | SDDbgValue *Dbg = *I; |
| 6091 | if (Dbg->getKind() == SDDbgValue::SDNODE) { |
| 6092 | SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(), |
| 6093 | Dbg->getOffset(), Dbg->getDebugLoc(), |
| 6094 | Dbg->getOrder()); |
| 6095 | ClonedDVs.push_back(Clone); |
Devang Patel | a2e868d | 2011-01-25 23:27:42 +0000 | [diff] [blame] | 6096 | } |
| 6097 | } |
Craig Topper | f22fd3f | 2013-07-03 05:11:49 +0000 | [diff] [blame] | 6098 | for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(), |
Devang Patel | a778f5c | 2011-02-18 22:43:42 +0000 | [diff] [blame] | 6099 | E = ClonedDVs.end(); I != E; ++I) |
| 6100 | AddDbgValue(*I, ToNode, false); |
Devang Patel | a2e868d | 2011-01-25 23:27:42 +0000 | [diff] [blame] | 6101 | } |
| 6102 | |
Jim Laskey | 58b968b | 2005-08-17 20:08:02 +0000 | [diff] [blame] | 6103 | //===----------------------------------------------------------------------===// |
| 6104 | // SDNode Class |
| 6105 | //===----------------------------------------------------------------------===// |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 6106 | |
Chris Lattner | 48b8592 | 2007-02-04 02:41:42 +0000 | [diff] [blame] | 6107 | HandleSDNode::~HandleSDNode() { |
Dan Gohman | 0fe9c6e | 2008-07-07 20:57:48 +0000 | [diff] [blame] | 6108 | DropOperands(); |
Chris Lattner | 48b8592 | 2007-02-04 02:41:42 +0000 | [diff] [blame] | 6109 | } |
| 6110 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6111 | GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order, |
| 6112 | DebugLoc DL, const GlobalValue *GA, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6113 | EVT VT, int64_t o, unsigned char TF) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6114 | : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) { |
Dan Gohman | 383b5f6 | 2010-04-17 15:32:28 +0000 | [diff] [blame] | 6115 | TheGlobal = GA; |
Lauro Ramos Venancio | 2c5c111 | 2007-04-21 20:56:26 +0000 | [diff] [blame] | 6116 | } |
Chris Lattner | 48b8592 | 2007-02-04 02:41:42 +0000 | [diff] [blame] | 6117 | |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 6118 | AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT, |
| 6119 | SDValue X, unsigned SrcAS, |
| 6120 | unsigned DestAS) |
| 6121 | : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X), |
| 6122 | SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {} |
| 6123 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6124 | MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs, |
| 6125 | EVT memvt, MachineMemOperand *mmo) |
| 6126 | : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) { |
David Greene | 1157f79 | 2010-02-17 20:21:42 +0000 | [diff] [blame] | 6127 | SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 6128 | MMO->isNonTemporal(), MMO->isInvariant()); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 6129 | assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!"); |
David Greene | 1157f79 | 2010-02-17 20:21:42 +0000 | [diff] [blame] | 6130 | assert(isNonTemporal() == MMO->isNonTemporal() && |
| 6131 | "Non-temporal encoding error!"); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 6132 | assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!"); |
Dale Johannesen | 3edb43e | 2009-01-28 21:18:29 +0000 | [diff] [blame] | 6133 | } |
| 6134 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6135 | MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs, |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6136 | const SDValue *Ops, unsigned NumOps, EVT memvt, |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 6137 | MachineMemOperand *mmo) |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6138 | : SDNode(Opc, Order, dl, VTs, Ops, NumOps), |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 6139 | MemoryVT(memvt), MMO(mmo) { |
David Greene | 1157f79 | 2010-02-17 20:21:42 +0000 | [diff] [blame] | 6140 | SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 6141 | MMO->isNonTemporal(), MMO->isInvariant()); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 6142 | assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!"); |
| 6143 | assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!"); |
Mon P Wang | 2887310 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 6144 | } |
| 6145 | |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 6146 | /// Profile - Gather unique data for the node. |
| 6147 | /// |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 6148 | void SDNode::Profile(FoldingSetNodeID &ID) const { |
Jim Laskey | 583bd47 | 2006-10-27 23:46:08 +0000 | [diff] [blame] | 6149 | AddNodeIDNode(ID, this); |
| 6150 | } |
| 6151 | |
Owen Anderson | d8110fb | 2009-08-25 22:27:22 +0000 | [diff] [blame] | 6152 | namespace { |
| 6153 | struct EVTArray { |
| 6154 | std::vector<EVT> VTs; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6155 | |
Owen Anderson | d8110fb | 2009-08-25 22:27:22 +0000 | [diff] [blame] | 6156 | EVTArray() { |
| 6157 | VTs.reserve(MVT::LAST_VALUETYPE); |
| 6158 | for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i) |
| 6159 | VTs.push_back(MVT((MVT::SimpleValueType)i)); |
| 6160 | } |
| 6161 | }; |
| 6162 | } |
| 6163 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6164 | static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs; |
Owen Anderson | d8110fb | 2009-08-25 22:27:22 +0000 | [diff] [blame] | 6165 | static ManagedStatic<EVTArray> SimpleVTArray; |
Chris Lattner | 895a55e | 2009-08-22 04:07:34 +0000 | [diff] [blame] | 6166 | static ManagedStatic<sys::SmartMutex<true> > VTMutex; |
Owen Anderson | b445908 | 2009-06-25 17:09:00 +0000 | [diff] [blame] | 6167 | |
Chris Lattner | a325511 | 2005-11-08 23:30:28 +0000 | [diff] [blame] | 6168 | /// getValueTypeList - Return a pointer to the specified value type. |
| 6169 | /// |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6170 | const EVT *SDNode::getValueTypeList(EVT VT) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 6171 | if (VT.isExtended()) { |
Owen Anderson | 02b1034 | 2009-08-22 06:32:36 +0000 | [diff] [blame] | 6172 | sys::SmartScopedLock<true> Lock(*VTMutex); |
Owen Anderson | b445908 | 2009-06-25 17:09:00 +0000 | [diff] [blame] | 6173 | return &(*EVTs->insert(VT).first); |
Duncan Sands | af47b11 | 2007-10-16 09:56:48 +0000 | [diff] [blame] | 6174 | } else { |
Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 6175 | assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE && |
Duncan Sands | ad017dc | 2010-05-10 04:54:28 +0000 | [diff] [blame] | 6176 | "Value type out of range!"); |
Owen Anderson | d8110fb | 2009-08-25 22:27:22 +0000 | [diff] [blame] | 6177 | return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy]; |
Duncan Sands | af47b11 | 2007-10-16 09:56:48 +0000 | [diff] [blame] | 6178 | } |
Chris Lattner | a325511 | 2005-11-08 23:30:28 +0000 | [diff] [blame] | 6179 | } |
Duncan Sands | af47b11 | 2007-10-16 09:56:48 +0000 | [diff] [blame] | 6180 | |
Chris Lattner | 5c88456 | 2005-01-12 18:37:47 +0000 | [diff] [blame] | 6181 | /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the |
| 6182 | /// indicated value. This method ignores uses of other values defined by this |
| 6183 | /// operation. |
Evan Cheng | 4ee6211 | 2006-02-05 06:29:23 +0000 | [diff] [blame] | 6184 | bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const { |
Chris Lattner | 5c88456 | 2005-01-12 18:37:47 +0000 | [diff] [blame] | 6185 | assert(Value < getNumValues() && "Bad value!"); |
| 6186 | |
Roman Levenstein | dc1adac | 2008-04-07 10:06:32 +0000 | [diff] [blame] | 6187 | // TODO: Only iterate over uses of a given value of the node |
| 6188 | for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) { |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 6189 | if (UI.getUse().getResNo() == Value) { |
Roman Levenstein | dc1adac | 2008-04-07 10:06:32 +0000 | [diff] [blame] | 6190 | if (NUses == 0) |
| 6191 | return false; |
| 6192 | --NUses; |
| 6193 | } |
Chris Lattner | 5c88456 | 2005-01-12 18:37:47 +0000 | [diff] [blame] | 6194 | } |
| 6195 | |
| 6196 | // Found exactly the right number of uses? |
| 6197 | return NUses == 0; |
| 6198 | } |
| 6199 | |
| 6200 | |
Evan Cheng | 33d5595 | 2007-08-02 05:29:38 +0000 | [diff] [blame] | 6201 | /// hasAnyUseOfValue - Return true if there are any use of the indicated |
| 6202 | /// value. This method ignores uses of other values defined by this operation. |
| 6203 | bool SDNode::hasAnyUseOfValue(unsigned Value) const { |
| 6204 | assert(Value < getNumValues() && "Bad value!"); |
| 6205 | |
Dan Gohman | 1373c1c | 2008-07-09 22:39:01 +0000 | [diff] [blame] | 6206 | for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 6207 | if (UI.getUse().getResNo() == Value) |
Dan Gohman | 1373c1c | 2008-07-09 22:39:01 +0000 | [diff] [blame] | 6208 | return true; |
Evan Cheng | 33d5595 | 2007-08-02 05:29:38 +0000 | [diff] [blame] | 6209 | |
| 6210 | return false; |
| 6211 | } |
| 6212 | |
| 6213 | |
Dan Gohman | 2a62995 | 2008-07-27 18:06:42 +0000 | [diff] [blame] | 6214 | /// isOnlyUserOf - Return true if this node is the only use of N. |
Evan Cheng | e6e97e6 | 2006-11-03 07:31:32 +0000 | [diff] [blame] | 6215 | /// |
Dan Gohman | 2a62995 | 2008-07-27 18:06:42 +0000 | [diff] [blame] | 6216 | bool SDNode::isOnlyUserOf(SDNode *N) const { |
Evan Cheng | 4ee6211 | 2006-02-05 06:29:23 +0000 | [diff] [blame] | 6217 | bool Seen = false; |
| 6218 | for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 6219 | SDNode *User = *I; |
Evan Cheng | 4ee6211 | 2006-02-05 06:29:23 +0000 | [diff] [blame] | 6220 | if (User == this) |
| 6221 | Seen = true; |
| 6222 | else |
| 6223 | return false; |
| 6224 | } |
| 6225 | |
| 6226 | return Seen; |
| 6227 | } |
| 6228 | |
Evan Cheng | e6e97e6 | 2006-11-03 07:31:32 +0000 | [diff] [blame] | 6229 | /// isOperand - Return true if this node is an operand of N. |
| 6230 | /// |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6231 | bool SDValue::isOperandOf(SDNode *N) const { |
Evan Cheng | bfa284f | 2006-03-03 06:42:32 +0000 | [diff] [blame] | 6232 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 6233 | if (*this == N->getOperand(i)) |
| 6234 | return true; |
| 6235 | return false; |
| 6236 | } |
| 6237 | |
Evan Cheng | 917be68 | 2008-03-04 00:41:45 +0000 | [diff] [blame] | 6238 | bool SDNode::isOperandOf(SDNode *N) const { |
Evan Cheng | 80d8eaa | 2006-03-03 06:24:54 +0000 | [diff] [blame] | 6239 | for (unsigned i = 0, e = N->NumOperands; i != e; ++i) |
Dan Gohman | e7852d0 | 2009-01-26 04:35:06 +0000 | [diff] [blame] | 6240 | if (this == N->OperandList[i].getNode()) |
Evan Cheng | 80d8eaa | 2006-03-03 06:24:54 +0000 | [diff] [blame] | 6241 | return true; |
| 6242 | return false; |
| 6243 | } |
Evan Cheng | 4ee6211 | 2006-02-05 06:29:23 +0000 | [diff] [blame] | 6244 | |
Chris Lattner | 572dee7 | 2008-01-16 05:49:24 +0000 | [diff] [blame] | 6245 | /// reachesChainWithoutSideEffects - Return true if this operand (which must |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6246 | /// be a chain) reaches the specified operand without crossing any |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6247 | /// side-effecting instructions on any chain path. In practice, this looks |
| 6248 | /// through token factors and non-volatile loads. In order to remain efficient, |
Owen Anderson | 14ac1dd | 2010-09-18 04:45:14 +0000 | [diff] [blame] | 6249 | /// this only looks a couple of nodes in, it does not do an exhaustive search. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6250 | bool SDValue::reachesChainWithoutSideEffects(SDValue Dest, |
Chris Lattner | 572dee7 | 2008-01-16 05:49:24 +0000 | [diff] [blame] | 6251 | unsigned Depth) const { |
| 6252 | if (*this == Dest) return true; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6253 | |
Chris Lattner | 572dee7 | 2008-01-16 05:49:24 +0000 | [diff] [blame] | 6254 | // Don't search too deeply, we just want to be able to see through |
| 6255 | // TokenFactor's etc. |
| 6256 | if (Depth == 0) return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6257 | |
Chris Lattner | 572dee7 | 2008-01-16 05:49:24 +0000 | [diff] [blame] | 6258 | // If this is a token factor, all inputs to the TF happen in parallel. If any |
Owen Anderson | 14ac1dd | 2010-09-18 04:45:14 +0000 | [diff] [blame] | 6259 | // of the operands of the TF does not reach dest, then we cannot do the xform. |
Chris Lattner | 572dee7 | 2008-01-16 05:49:24 +0000 | [diff] [blame] | 6260 | if (getOpcode() == ISD::TokenFactor) { |
| 6261 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Owen Anderson | 14ac1dd | 2010-09-18 04:45:14 +0000 | [diff] [blame] | 6262 | if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1)) |
| 6263 | return false; |
| 6264 | return true; |
Chris Lattner | 572dee7 | 2008-01-16 05:49:24 +0000 | [diff] [blame] | 6265 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6266 | |
Chris Lattner | 572dee7 | 2008-01-16 05:49:24 +0000 | [diff] [blame] | 6267 | // Loads don't have side effects, look through them. |
| 6268 | if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) { |
| 6269 | if (!Ld->isVolatile()) |
| 6270 | return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1); |
| 6271 | } |
| 6272 | return false; |
| 6273 | } |
| 6274 | |
Lang Hames | 944520f | 2011-07-07 04:31:51 +0000 | [diff] [blame] | 6275 | /// hasPredecessor - Return true if N is a predecessor of this node. |
| 6276 | /// N is either an operand of this node, or can be reached by recursively |
| 6277 | /// traversing up the operands. |
| 6278 | /// NOTE: This is an expensive method. Use it carefully. |
| 6279 | bool SDNode::hasPredecessor(const SDNode *N) const { |
| 6280 | SmallPtrSet<const SDNode *, 32> Visited; |
| 6281 | SmallVector<const SDNode *, 16> Worklist; |
| 6282 | return hasPredecessorHelper(N, Visited, Worklist); |
| 6283 | } |
Dan Gohman | 6688d61 | 2009-10-28 03:44:30 +0000 | [diff] [blame] | 6284 | |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 6285 | bool |
| 6286 | SDNode::hasPredecessorHelper(const SDNode *N, |
| 6287 | SmallPtrSet<const SDNode *, 32> &Visited, |
| 6288 | SmallVectorImpl<const SDNode *> &Worklist) const { |
Lang Hames | 944520f | 2011-07-07 04:31:51 +0000 | [diff] [blame] | 6289 | if (Visited.empty()) { |
| 6290 | Worklist.push_back(this); |
| 6291 | } else { |
| 6292 | // Take a look in the visited set. If we've already encountered this node |
| 6293 | // we needn't search further. |
| 6294 | if (Visited.count(N)) |
| 6295 | return true; |
| 6296 | } |
| 6297 | |
| 6298 | // Haven't visited N yet. Continue the search. |
| 6299 | while (!Worklist.empty()) { |
| 6300 | const SDNode *M = Worklist.pop_back_val(); |
| 6301 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
| 6302 | SDNode *Op = M->getOperand(i).getNode(); |
Dan Gohman | 6688d61 | 2009-10-28 03:44:30 +0000 | [diff] [blame] | 6303 | if (Visited.insert(Op)) |
| 6304 | Worklist.push_back(Op); |
Lang Hames | 944520f | 2011-07-07 04:31:51 +0000 | [diff] [blame] | 6305 | if (Op == N) |
| 6306 | return true; |
Dan Gohman | 6688d61 | 2009-10-28 03:44:30 +0000 | [diff] [blame] | 6307 | } |
Lang Hames | 944520f | 2011-07-07 04:31:51 +0000 | [diff] [blame] | 6308 | } |
Dan Gohman | 6688d61 | 2009-10-28 03:44:30 +0000 | [diff] [blame] | 6309 | |
| 6310 | return false; |
Evan Cheng | c5fc57d | 2006-11-03 03:05:24 +0000 | [diff] [blame] | 6311 | } |
| 6312 | |
Evan Cheng | c548428 | 2006-10-04 00:56:09 +0000 | [diff] [blame] | 6313 | uint64_t SDNode::getConstantOperandVal(unsigned Num) const { |
| 6314 | assert(Num < NumOperands && "Invalid child # of SDNode!"); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 6315 | return cast<ConstantSDNode>(OperandList[Num])->getZExtValue(); |
Evan Cheng | c548428 | 2006-10-04 00:56:09 +0000 | [diff] [blame] | 6316 | } |
| 6317 | |
Mon P Wang | cd6e725 | 2009-11-30 02:42:02 +0000 | [diff] [blame] | 6318 | SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) { |
| 6319 | assert(N->getNumValues() == 1 && |
| 6320 | "Can't unroll a vector with multiple results!"); |
| 6321 | |
| 6322 | EVT VT = N->getValueType(0); |
| 6323 | unsigned NE = VT.getVectorNumElements(); |
| 6324 | EVT EltVT = VT.getVectorElementType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6325 | SDLoc dl(N); |
Mon P Wang | cd6e725 | 2009-11-30 02:42:02 +0000 | [diff] [blame] | 6326 | |
| 6327 | SmallVector<SDValue, 8> Scalars; |
| 6328 | SmallVector<SDValue, 4> Operands(N->getNumOperands()); |
| 6329 | |
| 6330 | // If ResNE is 0, fully unroll the vector op. |
| 6331 | if (ResNE == 0) |
| 6332 | ResNE = NE; |
| 6333 | else if (NE > ResNE) |
| 6334 | NE = ResNE; |
| 6335 | |
| 6336 | unsigned i; |
| 6337 | for (i= 0; i != NE; ++i) { |
Bill Wendling | d71bb56 | 2010-04-30 22:19:17 +0000 | [diff] [blame] | 6338 | for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) { |
Mon P Wang | cd6e725 | 2009-11-30 02:42:02 +0000 | [diff] [blame] | 6339 | SDValue Operand = N->getOperand(j); |
| 6340 | EVT OperandVT = Operand.getValueType(); |
| 6341 | if (OperandVT.isVector()) { |
| 6342 | // A vector operand; extract a single element. |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6343 | const TargetLowering *TLI = TM.getTargetLowering(); |
Mon P Wang | cd6e725 | 2009-11-30 02:42:02 +0000 | [diff] [blame] | 6344 | EVT OperandEltVT = OperandVT.getVectorElementType(); |
| 6345 | Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
| 6346 | OperandEltVT, |
| 6347 | Operand, |
Tom Stellard | 425b76c | 2013-08-05 22:22:01 +0000 | [diff] [blame] | 6348 | getConstant(i, TLI->getVectorIdxTy())); |
Mon P Wang | cd6e725 | 2009-11-30 02:42:02 +0000 | [diff] [blame] | 6349 | } else { |
| 6350 | // A scalar operand; just use it as is. |
| 6351 | Operands[j] = Operand; |
| 6352 | } |
| 6353 | } |
| 6354 | |
| 6355 | switch (N->getOpcode()) { |
| 6356 | default: |
| 6357 | Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, |
| 6358 | &Operands[0], Operands.size())); |
| 6359 | break; |
Nadav Rotem | aec5861 | 2011-09-13 19:17:42 +0000 | [diff] [blame] | 6360 | case ISD::VSELECT: |
| 6361 | Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, |
| 6362 | &Operands[0], Operands.size())); |
| 6363 | break; |
Mon P Wang | cd6e725 | 2009-11-30 02:42:02 +0000 | [diff] [blame] | 6364 | case ISD::SHL: |
| 6365 | case ISD::SRA: |
| 6366 | case ISD::SRL: |
| 6367 | case ISD::ROTL: |
| 6368 | case ISD::ROTR: |
| 6369 | Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0], |
Jack Carter | 3af4d25 | 2013-09-09 22:02:08 +0000 | [diff] [blame] | 6370 | getShiftAmountOperand(Operands[0].getValueType(), |
| 6371 | Operands[1]))); |
Mon P Wang | cd6e725 | 2009-11-30 02:42:02 +0000 | [diff] [blame] | 6372 | break; |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 6373 | case ISD::SIGN_EXTEND_INREG: |
| 6374 | case ISD::FP_ROUND_INREG: { |
| 6375 | EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType(); |
| 6376 | Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, |
| 6377 | Operands[0], |
| 6378 | getValueType(ExtVT))); |
| 6379 | } |
Mon P Wang | cd6e725 | 2009-11-30 02:42:02 +0000 | [diff] [blame] | 6380 | } |
| 6381 | } |
| 6382 | |
| 6383 | for (; i < ResNE; ++i) |
| 6384 | Scalars.push_back(getUNDEF(EltVT)); |
| 6385 | |
| 6386 | return getNode(ISD::BUILD_VECTOR, dl, |
| 6387 | EVT::getVectorVT(*getContext(), EltVT, ResNE), |
| 6388 | &Scalars[0], Scalars.size()); |
| 6389 | } |
| 6390 | |
Evan Cheng | 64fa4a9 | 2009-12-09 01:36:00 +0000 | [diff] [blame] | 6391 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6392 | /// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a |
| 6393 | /// location that is 'Dist' units away from the location that the 'Base' load |
Evan Cheng | 64fa4a9 | 2009-12-09 01:36:00 +0000 | [diff] [blame] | 6394 | /// is loading from. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6395 | bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base, |
Evan Cheng | 64fa4a9 | 2009-12-09 01:36:00 +0000 | [diff] [blame] | 6396 | unsigned Bytes, int Dist) const { |
| 6397 | if (LD->getChain() != Base->getChain()) |
| 6398 | return false; |
| 6399 | EVT VT = LD->getValueType(0); |
| 6400 | if (VT.getSizeInBits() / 8 != Bytes) |
| 6401 | return false; |
| 6402 | |
| 6403 | SDValue Loc = LD->getOperand(1); |
| 6404 | SDValue BaseLoc = Base->getOperand(1); |
| 6405 | if (Loc.getOpcode() == ISD::FrameIndex) { |
| 6406 | if (BaseLoc.getOpcode() != ISD::FrameIndex) |
| 6407 | return false; |
| 6408 | const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo(); |
| 6409 | int FI = cast<FrameIndexSDNode>(Loc)->getIndex(); |
| 6410 | int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex(); |
| 6411 | int FS = MFI->getObjectSize(FI); |
| 6412 | int BFS = MFI->getObjectSize(BFI); |
| 6413 | if (FS != BFS || FS != (int)Bytes) return false; |
| 6414 | return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes); |
| 6415 | } |
Chris Lattner | 0a9481f | 2011-02-13 22:25:43 +0000 | [diff] [blame] | 6416 | |
| 6417 | // Handle X+C |
| 6418 | if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc && |
| 6419 | cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes) |
| 6420 | return true; |
Evan Cheng | 64fa4a9 | 2009-12-09 01:36:00 +0000 | [diff] [blame] | 6421 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6422 | const GlobalValue *GV1 = NULL; |
| 6423 | const GlobalValue *GV2 = NULL; |
Evan Cheng | 64fa4a9 | 2009-12-09 01:36:00 +0000 | [diff] [blame] | 6424 | int64_t Offset1 = 0; |
| 6425 | int64_t Offset2 = 0; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6426 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 6427 | bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1); |
| 6428 | bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2); |
Evan Cheng | 64fa4a9 | 2009-12-09 01:36:00 +0000 | [diff] [blame] | 6429 | if (isGA1 && isGA2 && GV1 == GV2) |
| 6430 | return Offset1 == (Offset2 + Dist*Bytes); |
| 6431 | return false; |
| 6432 | } |
| 6433 | |
| 6434 | |
Evan Cheng | f2dc5c7 | 2009-12-09 01:04:59 +0000 | [diff] [blame] | 6435 | /// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if |
| 6436 | /// it cannot be inferred. |
Evan Cheng | 7ced2e0 | 2009-12-09 01:10:37 +0000 | [diff] [blame] | 6437 | unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const { |
Evan Cheng | 7bd6478 | 2009-12-09 01:53:58 +0000 | [diff] [blame] | 6438 | // If this is a GlobalAddress + cst, return the alignment. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 6439 | const GlobalValue *GV; |
Evan Cheng | 7bd6478 | 2009-12-09 01:53:58 +0000 | [diff] [blame] | 6440 | int64_t GVOffset = 0; |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6441 | const TargetLowering *TLI = TM.getTargetLowering(); |
| 6442 | if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) { |
Matt Arsenault | e6e8112 | 2013-11-16 20:50:54 +0000 | [diff] [blame] | 6443 | unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType()); |
Eli Friedman | c4c2a02 | 2011-11-28 22:48:22 +0000 | [diff] [blame] | 6444 | APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 6445 | llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne, |
Bill Wendling | ba54bca | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 6446 | TLI->getDataLayout()); |
Eli Friedman | c4c2a02 | 2011-11-28 22:48:22 +0000 | [diff] [blame] | 6447 | unsigned AlignBits = KnownZero.countTrailingOnes(); |
| 6448 | unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0; |
| 6449 | if (Align) |
| 6450 | return MinAlign(Align, GVOffset); |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 6451 | } |
Evan Cheng | 7bd6478 | 2009-12-09 01:53:58 +0000 | [diff] [blame] | 6452 | |
Evan Cheng | f2dc5c7 | 2009-12-09 01:04:59 +0000 | [diff] [blame] | 6453 | // If this is a direct reference to a stack slot, use information about the |
| 6454 | // stack slot's alignment. |
| 6455 | int FrameIdx = 1 << 31; |
| 6456 | int64_t FrameOffset = 0; |
| 6457 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) { |
| 6458 | FrameIdx = FI->getIndex(); |
Chris Lattner | 0a9481f | 2011-02-13 22:25:43 +0000 | [diff] [blame] | 6459 | } else if (isBaseWithConstantOffset(Ptr) && |
Evan Cheng | f2dc5c7 | 2009-12-09 01:04:59 +0000 | [diff] [blame] | 6460 | isa<FrameIndexSDNode>(Ptr.getOperand(0))) { |
Chris Lattner | 0a9481f | 2011-02-13 22:25:43 +0000 | [diff] [blame] | 6461 | // Handle FI+Cst |
Evan Cheng | f2dc5c7 | 2009-12-09 01:04:59 +0000 | [diff] [blame] | 6462 | FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex(); |
| 6463 | FrameOffset = Ptr.getConstantOperandVal(1); |
| 6464 | } |
| 6465 | |
| 6466 | if (FrameIdx != (1 << 31)) { |
Evan Cheng | f2dc5c7 | 2009-12-09 01:04:59 +0000 | [diff] [blame] | 6467 | const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo(); |
Evan Cheng | de2ace1 | 2009-12-09 01:17:24 +0000 | [diff] [blame] | 6468 | unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx), |
| 6469 | FrameOffset); |
Evan Cheng | de2ace1 | 2009-12-09 01:17:24 +0000 | [diff] [blame] | 6470 | return FIInfoAlign; |
Evan Cheng | f2dc5c7 | 2009-12-09 01:04:59 +0000 | [diff] [blame] | 6471 | } |
| 6472 | |
| 6473 | return 0; |
| 6474 | } |
| 6475 | |
Bill Wendling | ee287ca | 2013-11-22 05:17:44 +0000 | [diff] [blame] | 6476 | /// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type |
| 6477 | /// which is split (or expanded) into two not necessarily identical pieces. |
| 6478 | std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const { |
| 6479 | // Currently all types are split in half. |
| 6480 | EVT LoVT, HiVT; |
| 6481 | if (!VT.isVector()) { |
| 6482 | LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT); |
| 6483 | } else { |
| 6484 | unsigned NumElements = VT.getVectorNumElements(); |
| 6485 | assert(!(NumElements & 1) && "Splitting vector, but not in half!"); |
| 6486 | LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(), |
| 6487 | NumElements/2); |
| 6488 | } |
| 6489 | return std::make_pair(LoVT, HiVT); |
| 6490 | } |
| 6491 | |
| 6492 | /// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the |
| 6493 | /// low/high part. |
| 6494 | std::pair<SDValue, SDValue> |
| 6495 | SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, |
| 6496 | const EVT &HiVT) { |
| 6497 | assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <= |
| 6498 | N.getValueType().getVectorNumElements() && |
| 6499 | "More vector elements requested than available!"); |
| 6500 | SDValue Lo, Hi; |
| 6501 | Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N, |
| 6502 | getConstant(0, TLI->getVectorIdxTy())); |
| 6503 | Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N, |
| 6504 | getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy())); |
| 6505 | return std::make_pair(Lo, Hi); |
| 6506 | } |
| 6507 | |
Sanjiv Gupta | a3518a1 | 2009-04-29 04:43:24 +0000 | [diff] [blame] | 6508 | // getAddressSpace - Return the address space this GlobalAddress belongs to. |
| 6509 | unsigned GlobalAddressSDNode::getAddressSpace() const { |
| 6510 | return getGlobal()->getType()->getAddressSpace(); |
| 6511 | } |
| 6512 | |
| 6513 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 6514 | Type *ConstantPoolSDNode::getType() const { |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 6515 | if (isMachineConstantPoolEntry()) |
| 6516 | return Val.MachineCPVal->getType(); |
| 6517 | return Val.ConstVal->getType(); |
| 6518 | } |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 6519 | |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6520 | bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, |
| 6521 | APInt &SplatUndef, |
| 6522 | unsigned &SplatBitSize, |
| 6523 | bool &HasAnyUndefs, |
Dale Johannesen | 1e60881 | 2009-11-13 01:45:18 +0000 | [diff] [blame] | 6524 | unsigned MinSplatBits, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 6525 | bool isBigEndian) const { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6526 | EVT VT = getValueType(0); |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6527 | assert(VT.isVector() && "Expected a vector type"); |
| 6528 | unsigned sz = VT.getSizeInBits(); |
| 6529 | if (MinSplatBits > sz) |
| 6530 | return false; |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 6531 | |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6532 | SplatValue = APInt(sz, 0); |
| 6533 | SplatUndef = APInt(sz, 0); |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 6534 | |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6535 | // Get the bits. Bits with undefined values (when the corresponding element |
| 6536 | // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared |
| 6537 | // in SplatValue. If any of the values are not constant, give up and return |
| 6538 | // false. |
| 6539 | unsigned int nOps = getNumOperands(); |
| 6540 | assert(nOps > 0 && "isConstantSplat has 0-size build vector"); |
| 6541 | unsigned EltBitSize = VT.getVectorElementType().getSizeInBits(); |
Dale Johannesen | 1e60881 | 2009-11-13 01:45:18 +0000 | [diff] [blame] | 6542 | |
| 6543 | for (unsigned j = 0; j < nOps; ++j) { |
| 6544 | unsigned i = isBigEndian ? nOps-1-j : j; |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 6545 | SDValue OpVal = getOperand(i); |
Dale Johannesen | 1e60881 | 2009-11-13 01:45:18 +0000 | [diff] [blame] | 6546 | unsigned BitPos = j * EltBitSize; |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 6547 | |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6548 | if (OpVal.getOpcode() == ISD::UNDEF) |
Dale Johannesen | 1e60881 | 2009-11-13 01:45:18 +0000 | [diff] [blame] | 6549 | SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize); |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6550 | else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 6551 | SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize). |
Dan Gohman | 58c2587 | 2010-04-12 02:24:01 +0000 | [diff] [blame] | 6552 | zextOrTrunc(sz) << BitPos; |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6553 | else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) |
Bob Wilson | d344b88 | 2009-03-04 17:47:01 +0000 | [diff] [blame] | 6554 | SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos; |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6555 | else |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 6556 | return false; |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 6557 | } |
| 6558 | |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6559 | // The build_vector is all constants or undefs. Find the smallest element |
| 6560 | // size that splats the vector. |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 6561 | |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6562 | HasAnyUndefs = (SplatUndef != 0); |
| 6563 | while (sz > 8) { |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 6564 | |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6565 | unsigned HalfSize = sz / 2; |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 6566 | APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize); |
| 6567 | APInt LowValue = SplatValue.trunc(HalfSize); |
| 6568 | APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize); |
| 6569 | APInt LowUndef = SplatUndef.trunc(HalfSize); |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 6570 | |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6571 | // If the two halves do not match (ignoring undef bits), stop here. |
| 6572 | if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) || |
| 6573 | MinSplatBits > HalfSize) |
| 6574 | break; |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 6575 | |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6576 | SplatValue = HighValue | LowValue; |
| 6577 | SplatUndef = HighUndef & LowUndef; |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 6578 | |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6579 | sz = HalfSize; |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 6580 | } |
| 6581 | |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 6582 | SplatBitSize = sz; |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 6583 | return true; |
| 6584 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6585 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 6586 | ConstantSDNode *BuildVectorSDNode::getConstantSplatValue() const { |
| 6587 | SDValue Op0 = getOperand(0); |
| 6588 | if (Op0.getOpcode() != ISD::Constant) |
| 6589 | return nullptr; |
| 6590 | |
| 6591 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) |
| 6592 | if (getOperand(i) != Op0) |
| 6593 | return nullptr; |
| 6594 | |
| 6595 | return cast<ConstantSDNode>(Op0); |
| 6596 | } |
| 6597 | |
| 6598 | bool BuildVectorSDNode::isConstant() const { |
| 6599 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 6600 | unsigned Opc = getOperand(i).getOpcode(); |
| 6601 | if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP) |
| 6602 | return false; |
| 6603 | } |
| 6604 | return true; |
| 6605 | } |
| 6606 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 6607 | bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 6608 | // Find the first non-undef value in the shuffle mask. |
| 6609 | unsigned i, e; |
| 6610 | for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i) |
| 6611 | /* search */; |
| 6612 | |
Nate Begeman | a641575 | 2009-04-29 18:13:31 +0000 | [diff] [blame] | 6613 | assert(i != e && "VECTOR_SHUFFLE node with all undef indices!"); |
Eric Christopher | 4d7c18c | 2009-08-22 00:40:45 +0000 | [diff] [blame] | 6614 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 6615 | // Make sure all remaining elements are either undef or the same as the first |
| 6616 | // non-undef value. |
| 6617 | for (int Idx = Mask[i]; i != e; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6618 | if (Mask[i] >= 0 && Mask[i] != Idx) |
| 6619 | return false; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6620 | return true; |
| 6621 | } |
David Greene | cf495bc | 2010-01-20 20:13:31 +0000 | [diff] [blame] | 6622 | |
Chris Lattner | 46ca5ef | 2010-02-24 21:34:04 +0000 | [diff] [blame] | 6623 | #ifdef XDEBUG |
David Greene | cf495bc | 2010-01-20 20:13:31 +0000 | [diff] [blame] | 6624 | static void checkForCyclesHelper(const SDNode *N, |
Chris Lattner | 46ca5ef | 2010-02-24 21:34:04 +0000 | [diff] [blame] | 6625 | SmallPtrSet<const SDNode*, 32> &Visited, |
| 6626 | SmallPtrSet<const SDNode*, 32> &Checked) { |
| 6627 | // If this node has already been checked, don't check it again. |
| 6628 | if (Checked.count(N)) |
David Greene | e3d97c7 | 2010-02-23 17:37:50 +0000 | [diff] [blame] | 6629 | return; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6630 | |
Chris Lattner | 46ca5ef | 2010-02-24 21:34:04 +0000 | [diff] [blame] | 6631 | // If a node has already been visited on this depth-first walk, reject it as |
| 6632 | // a cycle. |
| 6633 | if (!Visited.insert(N)) { |
David Greene | cf495bc | 2010-01-20 20:13:31 +0000 | [diff] [blame] | 6634 | dbgs() << "Offending node:\n"; |
| 6635 | N->dumprFull(); |
Chris Lattner | 46ca5ef | 2010-02-24 21:34:04 +0000 | [diff] [blame] | 6636 | errs() << "Detected cycle in SelectionDAG\n"; |
| 6637 | abort(); |
David Greene | cf495bc | 2010-01-20 20:13:31 +0000 | [diff] [blame] | 6638 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6639 | |
Chris Lattner | 46ca5ef | 2010-02-24 21:34:04 +0000 | [diff] [blame] | 6640 | for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 6641 | checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6642 | |
Chris Lattner | 46ca5ef | 2010-02-24 21:34:04 +0000 | [diff] [blame] | 6643 | Checked.insert(N); |
| 6644 | Visited.erase(N); |
David Greene | cf495bc | 2010-01-20 20:13:31 +0000 | [diff] [blame] | 6645 | } |
Chris Lattner | 46ca5ef | 2010-02-24 21:34:04 +0000 | [diff] [blame] | 6646 | #endif |
David Greene | cf495bc | 2010-01-20 20:13:31 +0000 | [diff] [blame] | 6647 | |
| 6648 | void llvm::checkForCycles(const llvm::SDNode *N) { |
| 6649 | #ifdef XDEBUG |
Alp Toker | 18a988e | 2013-10-29 02:35:28 +0000 | [diff] [blame] | 6650 | assert(N && "Checking nonexistent SDNode"); |
Chris Lattner | 46ca5ef | 2010-02-24 21:34:04 +0000 | [diff] [blame] | 6651 | SmallPtrSet<const SDNode*, 32> visited; |
| 6652 | SmallPtrSet<const SDNode*, 32> checked; |
David Greene | e3d97c7 | 2010-02-23 17:37:50 +0000 | [diff] [blame] | 6653 | checkForCyclesHelper(N, visited, checked); |
David Greene | cf495bc | 2010-01-20 20:13:31 +0000 | [diff] [blame] | 6654 | #endif |
| 6655 | } |
| 6656 | |
| 6657 | void llvm::checkForCycles(const llvm::SelectionDAG *DAG) { |
| 6658 | checkForCycles(DAG->getRoot().getNode()); |
| 6659 | } |