1 //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This implements the SelectionDAG class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "SDNodeDbgValue.h"
16 #include "llvm/ADT/APSInt.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/CodeGen/MachineBasicBlock.h"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineModuleInfo.h"
27 #include "llvm/CodeGen/SelectionDAGTargetInfo.h"
28 #include "llvm/IR/CallingConv.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/DebugInfo.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/Function.h"
34 #include "llvm/IR/GlobalAlias.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/Intrinsics.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/ManagedStatic.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Support/Mutex.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Target/TargetInstrInfo.h"
44 #include "llvm/Target/TargetIntrinsicInfo.h"
45 #include "llvm/Target/TargetLowering.h"
46 #include "llvm/Target/TargetMachine.h"
47 #include "llvm/Target/TargetOptions.h"
48 #include "llvm/Target/TargetRegisterInfo.h"
49 #include "llvm/Target/TargetSubtargetInfo.h"
50 #include <algorithm>
51 #include <cmath>
52 #include <utility>
53
54 using namespace llvm;
55
56 /// makeVTList - Return an instance of the SDVTList struct initialized with the
57 /// specified members.
makeVTList(const EVT * VTs,unsigned NumVTs)58 static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
59 SDVTList Res = {VTs, NumVTs};
60 return Res;
61 }
62
63 // Default null implementations of the callbacks.
NodeDeleted(SDNode *,SDNode *)64 void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
NodeUpdated(SDNode *)65 void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
66
67 //===----------------------------------------------------------------------===//
68 // ConstantFPSDNode Class
69 //===----------------------------------------------------------------------===//
70
71 /// isExactlyValue - We don't rely on operator== working on double values, as
72 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
73 /// As such, this method can be used to do an exact bit-for-bit comparison of
74 /// two floating point values.
isExactlyValue(const APFloat & V) const75 bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
76 return getValueAPF().bitwiseIsEqual(V);
77 }
78
isValueValidForType(EVT VT,const APFloat & Val)79 bool ConstantFPSDNode::isValueValidForType(EVT VT,
80 const APFloat& Val) {
81 assert(VT.isFloatingPoint() && "Can only convert between FP types");
82
83 // convert modifies in place, so make a copy.
84 APFloat Val2 = APFloat(Val);
85 bool losesInfo;
86 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
87 APFloat::rmNearestTiesToEven,
88 &losesInfo);
89 return !losesInfo;
90 }
91
92 //===----------------------------------------------------------------------===//
93 // ISD Namespace
94 //===----------------------------------------------------------------------===//
95
isConstantSplatVector(const SDNode * N,APInt & SplatVal)96 bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
97 auto *BV = dyn_cast<BuildVectorSDNode>(N);
98 if (!BV)
99 return false;
100
101 APInt SplatUndef;
102 unsigned SplatBitSize;
103 bool HasUndefs;
104 EVT EltVT = N->getValueType(0).getVectorElementType();
105 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs) &&
106 EltVT.getSizeInBits() >= SplatBitSize;
107 }
108
109 // FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
110 // specializations of the more general isConstantSplatVector()?
111
isBuildVectorAllOnes(const SDNode * N)112 bool ISD::isBuildVectorAllOnes(const SDNode *N) {
113 // Look through a bit convert.
114 while (N->getOpcode() == ISD::BITCAST)
115 N = N->getOperand(0).getNode();
116
117 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
118
119 unsigned i = 0, e = N->getNumOperands();
120
121 // Skip over all of the undef values.
122 while (i != e && N->getOperand(i).isUndef())
123 ++i;
124
125 // Do not accept an all-undef vector.
126 if (i == e) return false;
127
128 // Do not accept build_vectors that aren't all constants or which have non-~0
129 // elements. We have to be a bit careful here, as the type of the constant
130 // may not be the same as the type of the vector elements due to type
131 // legalization (the elements are promoted to a legal type for the target and
132 // a vector of a type may be legal when the base element type is not).
133 // We only want to check enough bits to cover the vector elements, because
134 // we care if the resultant vector is all ones, not whether the individual
135 // constants are.
136 SDValue NotZero = N->getOperand(i);
137 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
138 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
139 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
140 return false;
141 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
142 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
143 return false;
144 } else
145 return false;
146
147 // Okay, we have at least one ~0 value, check to see if the rest match or are
148 // undefs. Even with the above element type twiddling, this should be OK, as
149 // the same type legalization should have applied to all the elements.
150 for (++i; i != e; ++i)
151 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
152 return false;
153 return true;
154 }
155
isBuildVectorAllZeros(const SDNode * N)156 bool ISD::isBuildVectorAllZeros(const SDNode *N) {
157 // Look through a bit convert.
158 while (N->getOpcode() == ISD::BITCAST)
159 N = N->getOperand(0).getNode();
160
161 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
162
163 bool IsAllUndef = true;
164 for (const SDValue &Op : N->op_values()) {
165 if (Op.isUndef())
166 continue;
167 IsAllUndef = false;
168 // Do not accept build_vectors that aren't all constants or which have non-0
169 // elements. We have to be a bit careful here, as the type of the constant
170 // may not be the same as the type of the vector elements due to type
171 // legalization (the elements are promoted to a legal type for the target
172 // and a vector of a type may be legal when the base element type is not).
173 // We only want to check enough bits to cover the vector elements, because
174 // we care if the resultant vector is all zeros, not whether the individual
175 // constants are.
176 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
177 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
178 if (CN->getAPIntValue().countTrailingZeros() < EltSize)
179 return false;
180 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
181 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingZeros() < EltSize)
182 return false;
183 } else
184 return false;
185 }
186
187 // Do not accept an all-undef vector.
188 if (IsAllUndef)
189 return false;
190 return true;
191 }
192
isBuildVectorOfConstantSDNodes(const SDNode * N)193 bool ISD::isBuildVectorOfConstantSDNodes(const SDNode *N) {
194 if (N->getOpcode() != ISD::BUILD_VECTOR)
195 return false;
196
197 for (const SDValue &Op : N->op_values()) {
198 if (Op.isUndef())
199 continue;
200 if (!isa<ConstantSDNode>(Op))
201 return false;
202 }
203 return true;
204 }
205
isBuildVectorOfConstantFPSDNodes(const SDNode * N)206 bool ISD::isBuildVectorOfConstantFPSDNodes(const SDNode *N) {
207 if (N->getOpcode() != ISD::BUILD_VECTOR)
208 return false;
209
210 for (const SDValue &Op : N->op_values()) {
211 if (Op.isUndef())
212 continue;
213 if (!isa<ConstantFPSDNode>(Op))
214 return false;
215 }
216 return true;
217 }
218
allOperandsUndef(const SDNode * N)219 bool ISD::allOperandsUndef(const SDNode *N) {
220 // Return false if the node has no operands.
221 // This is "logically inconsistent" with the definition of "all" but
222 // is probably the desired behavior.
223 if (N->getNumOperands() == 0)
224 return false;
225
226 for (const SDValue &Op : N->op_values())
227 if (!Op.isUndef())
228 return false;
229
230 return true;
231 }
232
getExtForLoadExtType(bool IsFP,ISD::LoadExtType ExtType)233 ISD::NodeType ISD::getExtForLoadExtType(bool IsFP, ISD::LoadExtType ExtType) {
234 switch (ExtType) {
235 case ISD::EXTLOAD:
236 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
237 case ISD::SEXTLOAD:
238 return ISD::SIGN_EXTEND;
239 case ISD::ZEXTLOAD:
240 return ISD::ZERO_EXTEND;
241 default:
242 break;
243 }
244
245 llvm_unreachable("Invalid LoadExtType");
246 }
247
getSetCCSwappedOperands(ISD::CondCode Operation)248 ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
249 // To perform this operation, we just need to swap the L and G bits of the
250 // operation.
251 unsigned OldL = (Operation >> 2) & 1;
252 unsigned OldG = (Operation >> 1) & 1;
253 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
254 (OldL << 1) | // New G bit
255 (OldG << 2)); // New L bit.
256 }
257
getSetCCInverse(ISD::CondCode Op,bool isInteger)258 ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
259 unsigned Operation = Op;
260 if (isInteger)
261 Operation ^= 7; // Flip L, G, E bits, but not U.
262 else
263 Operation ^= 15; // Flip all of the condition bits.
264
265 if (Operation > ISD::SETTRUE2)
266 Operation &= ~8; // Don't let N and U bits get set.
267
268 return ISD::CondCode(Operation);
269 }
270
271
272 /// For an integer comparison, return 1 if the comparison is a signed operation
273 /// and 2 if the result is an unsigned comparison. Return zero if the operation
274 /// does not depend on the sign of the input (setne and seteq).
isSignedOp(ISD::CondCode Opcode)275 static int isSignedOp(ISD::CondCode Opcode) {
276 switch (Opcode) {
277 default: llvm_unreachable("Illegal integer setcc operation!");
278 case ISD::SETEQ:
279 case ISD::SETNE: return 0;
280 case ISD::SETLT:
281 case ISD::SETLE:
282 case ISD::SETGT:
283 case ISD::SETGE: return 1;
284 case ISD::SETULT:
285 case ISD::SETULE:
286 case ISD::SETUGT:
287 case ISD::SETUGE: return 2;
288 }
289 }
290
getSetCCOrOperation(ISD::CondCode Op1,ISD::CondCode Op2,bool isInteger)291 ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
292 bool isInteger) {
293 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
294 // Cannot fold a signed integer setcc with an unsigned integer setcc.
295 return ISD::SETCC_INVALID;
296
297 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
298
299 // If the N and U bits get set then the resultant comparison DOES suddenly
300 // care about orderedness, and is true when ordered.
301 if (Op > ISD::SETTRUE2)
302 Op &= ~16; // Clear the U bit if the N bit is set.
303
304 // Canonicalize illegal integer setcc's.
305 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
306 Op = ISD::SETNE;
307
308 return ISD::CondCode(Op);
309 }
310
getSetCCAndOperation(ISD::CondCode Op1,ISD::CondCode Op2,bool isInteger)311 ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
312 bool isInteger) {
313 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
314 // Cannot fold a signed setcc with an unsigned setcc.
315 return ISD::SETCC_INVALID;
316
317 // Combine all of the condition bits.
318 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
319
320 // Canonicalize illegal integer setcc's.
321 if (isInteger) {
322 switch (Result) {
323 default: break;
324 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
325 case ISD::SETOEQ: // SETEQ & SETU[LG]E
326 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
327 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
328 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
329 }
330 }
331
332 return Result;
333 }
334
335 //===----------------------------------------------------------------------===//
336 // SDNode Profile Support
337 //===----------------------------------------------------------------------===//
338
339 /// AddNodeIDOpcode - Add the node opcode to the NodeID data.
340 ///
AddNodeIDOpcode(FoldingSetNodeID & ID,unsigned OpC)341 static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
342 ID.AddInteger(OpC);
343 }
344
345 /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
346 /// solely with their pointer.
AddNodeIDValueTypes(FoldingSetNodeID & ID,SDVTList VTList)347 static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
348 ID.AddPointer(VTList.VTs);
349 }
350
351 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
352 ///
AddNodeIDOperands(FoldingSetNodeID & ID,ArrayRef<SDValue> Ops)353 static void AddNodeIDOperands(FoldingSetNodeID &ID,
354 ArrayRef<SDValue> Ops) {
355 for (auto& Op : Ops) {
356 ID.AddPointer(Op.getNode());
357 ID.AddInteger(Op.getResNo());
358 }
359 }
360
361 /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
362 ///
AddNodeIDOperands(FoldingSetNodeID & ID,ArrayRef<SDUse> Ops)363 static void AddNodeIDOperands(FoldingSetNodeID &ID,
364 ArrayRef<SDUse> Ops) {
365 for (auto& Op : Ops) {
366 ID.AddPointer(Op.getNode());
367 ID.AddInteger(Op.getResNo());
368 }
369 }
370
AddNodeIDNode(FoldingSetNodeID & ID,unsigned short OpC,SDVTList VTList,ArrayRef<SDValue> OpList)371 static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned short OpC,
372 SDVTList VTList, ArrayRef<SDValue> OpList) {
373 AddNodeIDOpcode(ID, OpC);
374 AddNodeIDValueTypes(ID, VTList);
375 AddNodeIDOperands(ID, OpList);
376 }
377
378 /// If this is an SDNode with special info, add this info to the NodeID data.
AddNodeIDCustom(FoldingSetNodeID & ID,const SDNode * N)379 static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
380 switch (N->getOpcode()) {
381 case ISD::TargetExternalSymbol:
382 case ISD::ExternalSymbol:
383 case ISD::MCSymbol:
384 llvm_unreachable("Should only be used on nodes with operands");
385 default: break; // Normal nodes don't need extra info.
386 case ISD::TargetConstant:
387 case ISD::Constant: {
388 const ConstantSDNode *C = cast<ConstantSDNode>(N);
389 ID.AddPointer(C->getConstantIntValue());
390 ID.AddBoolean(C->isOpaque());
391 break;
392 }
393 case ISD::TargetConstantFP:
394 case ISD::ConstantFP: {
395 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
396 break;
397 }
398 case ISD::TargetGlobalAddress:
399 case ISD::GlobalAddress:
400 case ISD::TargetGlobalTLSAddress:
401 case ISD::GlobalTLSAddress: {
402 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
403 ID.AddPointer(GA->getGlobal());
404 ID.AddInteger(GA->getOffset());
405 ID.AddInteger(GA->getTargetFlags());
406 ID.AddInteger(GA->getAddressSpace());
407 break;
408 }
409 case ISD::BasicBlock:
410 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
411 break;
412 case ISD::Register:
413 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
414 break;
415 case ISD::RegisterMask:
416 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
417 break;
418 case ISD::SRCVALUE:
419 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
420 break;
421 case ISD::FrameIndex:
422 case ISD::TargetFrameIndex:
423 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
424 break;
425 case ISD::JumpTable:
426 case ISD::TargetJumpTable:
427 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
428 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
429 break;
430 case ISD::ConstantPool:
431 case ISD::TargetConstantPool: {
432 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
433 ID.AddInteger(CP->getAlignment());
434 ID.AddInteger(CP->getOffset());
435 if (CP->isMachineConstantPoolEntry())
436 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
437 else
438 ID.AddPointer(CP->getConstVal());
439 ID.AddInteger(CP->getTargetFlags());
440 break;
441 }
442 case ISD::TargetIndex: {
443 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
444 ID.AddInteger(TI->getIndex());
445 ID.AddInteger(TI->getOffset());
446 ID.AddInteger(TI->getTargetFlags());
447 break;
448 }
449 case ISD::LOAD: {
450 const LoadSDNode *LD = cast<LoadSDNode>(N);
451 ID.AddInteger(LD->getMemoryVT().getRawBits());
452 ID.AddInteger(LD->getRawSubclassData());
453 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
454 break;
455 }
456 case ISD::STORE: {
457 const StoreSDNode *ST = cast<StoreSDNode>(N);
458 ID.AddInteger(ST->getMemoryVT().getRawBits());
459 ID.AddInteger(ST->getRawSubclassData());
460 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
461 break;
462 }
463 case ISD::ATOMIC_CMP_SWAP:
464 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
465 case ISD::ATOMIC_SWAP:
466 case ISD::ATOMIC_LOAD_ADD:
467 case ISD::ATOMIC_LOAD_SUB:
468 case ISD::ATOMIC_LOAD_AND:
469 case ISD::ATOMIC_LOAD_OR:
470 case ISD::ATOMIC_LOAD_XOR:
471 case ISD::ATOMIC_LOAD_NAND:
472 case ISD::ATOMIC_LOAD_MIN:
473 case ISD::ATOMIC_LOAD_MAX:
474 case ISD::ATOMIC_LOAD_UMIN:
475 case ISD::ATOMIC_LOAD_UMAX:
476 case ISD::ATOMIC_LOAD:
477 case ISD::ATOMIC_STORE: {
478 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
479 ID.AddInteger(AT->getMemoryVT().getRawBits());
480 ID.AddInteger(AT->getRawSubclassData());
481 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
482 break;
483 }
484 case ISD::PREFETCH: {
485 const MemSDNode *PF = cast<MemSDNode>(N);
486 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
487 break;
488 }
489 case ISD::VECTOR_SHUFFLE: {
490 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
491 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
492 i != e; ++i)
493 ID.AddInteger(SVN->getMaskElt(i));
494 break;
495 }
496 case ISD::TargetBlockAddress:
497 case ISD::BlockAddress: {
498 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
499 ID.AddPointer(BA->getBlockAddress());
500 ID.AddInteger(BA->getOffset());
501 ID.AddInteger(BA->getTargetFlags());
502 break;
503 }
504 } // end switch (N->getOpcode())
505
506 // Target specific memory nodes could also have address spaces to check.
507 if (N->isTargetMemoryOpcode())
508 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
509 }
510
511 /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
512 /// data.
AddNodeIDNode(FoldingSetNodeID & ID,const SDNode * N)513 static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
514 AddNodeIDOpcode(ID, N->getOpcode());
515 // Add the return value info.
516 AddNodeIDValueTypes(ID, N->getVTList());
517 // Add the operand info.
518 AddNodeIDOperands(ID, N->ops());
519
520 // Handle SDNode leafs with special info.
521 AddNodeIDCustom(ID, N);
522 }
523
524 /// encodeMemSDNodeFlags - Generic routine for computing a value for use in
525 /// the CSE map that carries volatility, temporalness, indexing mode, and
526 /// extension/truncation information.
527 ///
528 static inline unsigned
encodeMemSDNodeFlags(int ConvType,ISD::MemIndexedMode AM,bool isVolatile,bool isNonTemporal,bool isInvariant)529 encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
530 bool isNonTemporal, bool isInvariant) {
531 assert((ConvType & 3) == ConvType &&
532 "ConvType may not require more than 2 bits!");
533 assert((AM & 7) == AM &&
534 "AM may not require more than 3 bits!");
535 return ConvType |
536 (AM << 2) |
537 (isVolatile << 5) |
538 (isNonTemporal << 6) |
539 (isInvariant << 7);
540 }
541
542 //===----------------------------------------------------------------------===//
543 // SelectionDAG Class
544 //===----------------------------------------------------------------------===//
545
546 /// doNotCSE - Return true if CSE should not be performed for this node.
doNotCSE(SDNode * N)547 static bool doNotCSE(SDNode *N) {
548 if (N->getValueType(0) == MVT::Glue)
549 return true; // Never CSE anything that produces a flag.
550
551 switch (N->getOpcode()) {
552 default: break;
553 case ISD::HANDLENODE:
554 case ISD::EH_LABEL:
555 return true; // Never CSE these nodes.
556 }
557
558 // Check that remaining values produced are not flags.
559 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
560 if (N->getValueType(i) == MVT::Glue)
561 return true; // Never CSE anything that produces a flag.
562
563 return false;
564 }
565
566 /// RemoveDeadNodes - This method deletes all unreachable nodes in the
567 /// SelectionDAG.
RemoveDeadNodes()568 void SelectionDAG::RemoveDeadNodes() {
569 // Create a dummy node (which is not added to allnodes), that adds a reference
570 // to the root node, preventing it from being deleted.
571 HandleSDNode Dummy(getRoot());
572
573 SmallVector<SDNode*, 128> DeadNodes;
574
575 // Add all obviously-dead nodes to the DeadNodes worklist.
576 for (SDNode &Node : allnodes())
577 if (Node.use_empty())
578 DeadNodes.push_back(&Node);
579
580 RemoveDeadNodes(DeadNodes);
581
582 // If the root changed (e.g. it was a dead load, update the root).
583 setRoot(Dummy.getValue());
584 }
585
586 /// RemoveDeadNodes - This method deletes the unreachable nodes in the
587 /// given list, and any nodes that become unreachable as a result.
RemoveDeadNodes(SmallVectorImpl<SDNode * > & DeadNodes)588 void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
589
590 // Process the worklist, deleting the nodes and adding their uses to the
591 // worklist.
592 while (!DeadNodes.empty()) {
593 SDNode *N = DeadNodes.pop_back_val();
594
595 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
596 DUL->NodeDeleted(N, nullptr);
597
598 // Take the node out of the appropriate CSE map.
599 RemoveNodeFromCSEMaps(N);
600
601 // Next, brutally remove the operand list. This is safe to do, as there are
602 // no cycles in the graph.
603 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
604 SDUse &Use = *I++;
605 SDNode *Operand = Use.getNode();
606 Use.set(SDValue());
607
608 // Now that we removed this operand, see if there are no uses of it left.
609 if (Operand->use_empty())
610 DeadNodes.push_back(Operand);
611 }
612
613 DeallocateNode(N);
614 }
615 }
616
RemoveDeadNode(SDNode * N)617 void SelectionDAG::RemoveDeadNode(SDNode *N){
618 SmallVector<SDNode*, 16> DeadNodes(1, N);
619
620 // Create a dummy node that adds a reference to the root node, preventing
621 // it from being deleted. (This matters if the root is an operand of the
622 // dead node.)
623 HandleSDNode Dummy(getRoot());
624
625 RemoveDeadNodes(DeadNodes);
626 }
627
DeleteNode(SDNode * N)628 void SelectionDAG::DeleteNode(SDNode *N) {
629 // First take this out of the appropriate CSE map.
630 RemoveNodeFromCSEMaps(N);
631
632 // Finally, remove uses due to operands of this node, remove from the
633 // AllNodes list, and delete the node.
634 DeleteNodeNotInCSEMaps(N);
635 }
636
DeleteNodeNotInCSEMaps(SDNode * N)637 void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
638 assert(N->getIterator() != AllNodes.begin() &&
639 "Cannot delete the entry node!");
640 assert(N->use_empty() && "Cannot delete a node that is not dead!");
641
642 // Drop all of the operands and decrement used node's use counts.
643 N->DropOperands();
644
645 DeallocateNode(N);
646 }
647
erase(const SDNode * Node)648 void SDDbgInfo::erase(const SDNode *Node) {
649 DbgValMapType::iterator I = DbgValMap.find(Node);
650 if (I == DbgValMap.end())
651 return;
652 for (auto &Val: I->second)
653 Val->setIsInvalidated();
654 DbgValMap.erase(I);
655 }
656
DeallocateNode(SDNode * N)657 void SelectionDAG::DeallocateNode(SDNode *N) {
658 // If we have operands, deallocate them.
659 removeOperands(N);
660
661 // Set the opcode to DELETED_NODE to help catch bugs when node
662 // memory is reallocated.
663 N->NodeType = ISD::DELETED_NODE;
664
665 NodeAllocator.Deallocate(AllNodes.remove(N));
666
667 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
668 // them and forget about that node.
669 DbgInfo->erase(N);
670 }
671
672 #ifndef NDEBUG
673 /// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
VerifySDNode(SDNode * N)674 static void VerifySDNode(SDNode *N) {
675 switch (N->getOpcode()) {
676 default:
677 break;
678 case ISD::BUILD_PAIR: {
679 EVT VT = N->getValueType(0);
680 assert(N->getNumValues() == 1 && "Too many results!");
681 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
682 "Wrong return type!");
683 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
684 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
685 "Mismatched operand types!");
686 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
687 "Wrong operand type!");
688 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
689 "Wrong return type size");
690 break;
691 }
692 case ISD::BUILD_VECTOR: {
693 assert(N->getNumValues() == 1 && "Too many results!");
694 assert(N->getValueType(0).isVector() && "Wrong return type!");
695 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
696 "Wrong number of operands!");
697 EVT EltVT = N->getValueType(0).getVectorElementType();
698 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
699 assert((I->getValueType() == EltVT ||
700 (EltVT.isInteger() && I->getValueType().isInteger() &&
701 EltVT.bitsLE(I->getValueType()))) &&
702 "Wrong operand type!");
703 assert(I->getValueType() == N->getOperand(0).getValueType() &&
704 "Operands must all have the same type");
705 }
706 break;
707 }
708 }
709 }
710 #endif // NDEBUG
711
712 /// \brief Insert a newly allocated node into the DAG.
713 ///
714 /// Handles insertion into the all nodes list and CSE map, as well as
715 /// verification and other common operations when a new node is allocated.
InsertNode(SDNode * N)716 void SelectionDAG::InsertNode(SDNode *N) {
717 AllNodes.push_back(N);
718 #ifndef NDEBUG
719 N->PersistentId = NextPersistentId++;
720 VerifySDNode(N);
721 #endif
722 }
723
724 /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
725 /// correspond to it. This is useful when we're about to delete or repurpose
726 /// the node. We don't want future request for structurally identical nodes
727 /// to return N anymore.
RemoveNodeFromCSEMaps(SDNode * N)728 bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
729 bool Erased = false;
730 switch (N->getOpcode()) {
731 case ISD::HANDLENODE: return false; // noop.
732 case ISD::CONDCODE:
733 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
734 "Cond code doesn't exist!");
735 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
736 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
737 break;
738 case ISD::ExternalSymbol:
739 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
740 break;
741 case ISD::TargetExternalSymbol: {
742 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
743 Erased = TargetExternalSymbols.erase(
744 std::pair<std::string,unsigned char>(ESN->getSymbol(),
745 ESN->getTargetFlags()));
746 break;
747 }
748 case ISD::MCSymbol: {
749 auto *MCSN = cast<MCSymbolSDNode>(N);
750 Erased = MCSymbols.erase(MCSN->getMCSymbol());
751 break;
752 }
753 case ISD::VALUETYPE: {
754 EVT VT = cast<VTSDNode>(N)->getVT();
755 if (VT.isExtended()) {
756 Erased = ExtendedValueTypeNodes.erase(VT);
757 } else {
758 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
759 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
760 }
761 break;
762 }
763 default:
764 // Remove it from the CSE Map.
765 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
766 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
767 Erased = CSEMap.RemoveNode(N);
768 break;
769 }
770 #ifndef NDEBUG
771 // Verify that the node was actually in one of the CSE maps, unless it has a
772 // flag result (which cannot be CSE'd) or is one of the special cases that are
773 // not subject to CSE.
774 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
775 !N->isMachineOpcode() && !doNotCSE(N)) {
776 N->dump(this);
777 dbgs() << "\n";
778 llvm_unreachable("Node is not in map!");
779 }
780 #endif
781 return Erased;
782 }
783
784 /// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
785 /// maps and modified in place. Add it back to the CSE maps, unless an identical
786 /// node already exists, in which case transfer all its users to the existing
787 /// node. This transfer can potentially trigger recursive merging.
788 ///
789 void
AddModifiedNodeToCSEMaps(SDNode * N)790 SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
791 // For node types that aren't CSE'd, just act as if no identical node
792 // already exists.
793 if (!doNotCSE(N)) {
794 SDNode *Existing = CSEMap.GetOrInsertNode(N);
795 if (Existing != N) {
796 // If there was already an existing matching node, use ReplaceAllUsesWith
797 // to replace the dead one with the existing one. This can cause
798 // recursive merging of other unrelated nodes down the line.
799 ReplaceAllUsesWith(N, Existing);
800
801 // N is now dead. Inform the listeners and delete it.
802 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
803 DUL->NodeDeleted(N, Existing);
804 DeleteNodeNotInCSEMaps(N);
805 return;
806 }
807 }
808
809 // If the node doesn't already exist, we updated it. Inform listeners.
810 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
811 DUL->NodeUpdated(N);
812 }
813
814 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
815 /// were replaced with those specified. If this node is never memoized,
816 /// return null, otherwise return a pointer to the slot it would take. If a
817 /// node already exists with these operands, the slot will be non-null.
FindModifiedNodeSlot(SDNode * N,SDValue Op,void * & InsertPos)818 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
819 void *&InsertPos) {
820 if (doNotCSE(N))
821 return nullptr;
822
823 SDValue Ops[] = { Op };
824 FoldingSetNodeID ID;
825 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
826 AddNodeIDCustom(ID, N);
827 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
828 if (Node)
829 if (const SDNodeFlags *Flags = N->getFlags())
830 Node->intersectFlagsWith(Flags);
831 return Node;
832 }
833
834 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
835 /// were replaced with those specified. If this node is never memoized,
836 /// return null, otherwise return a pointer to the slot it would take. If a
837 /// node already exists with these operands, the slot will be non-null.
FindModifiedNodeSlot(SDNode * N,SDValue Op1,SDValue Op2,void * & InsertPos)838 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
839 SDValue Op1, SDValue Op2,
840 void *&InsertPos) {
841 if (doNotCSE(N))
842 return nullptr;
843
844 SDValue Ops[] = { Op1, Op2 };
845 FoldingSetNodeID ID;
846 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
847 AddNodeIDCustom(ID, N);
848 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
849 if (Node)
850 if (const SDNodeFlags *Flags = N->getFlags())
851 Node->intersectFlagsWith(Flags);
852 return Node;
853 }
854
855
856 /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
857 /// were replaced with those specified. If this node is never memoized,
858 /// return null, otherwise return a pointer to the slot it would take. If a
859 /// node already exists with these operands, the slot will be non-null.
FindModifiedNodeSlot(SDNode * N,ArrayRef<SDValue> Ops,void * & InsertPos)860 SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
861 void *&InsertPos) {
862 if (doNotCSE(N))
863 return nullptr;
864
865 FoldingSetNodeID ID;
866 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
867 AddNodeIDCustom(ID, N);
868 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
869 if (Node)
870 if (const SDNodeFlags *Flags = N->getFlags())
871 Node->intersectFlagsWith(Flags);
872 return Node;
873 }
874
getEVTAlignment(EVT VT) const875 unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
876 Type *Ty = VT == MVT::iPTR ?
877 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
878 VT.getTypeForEVT(*getContext());
879
880 return getDataLayout().getABITypeAlignment(Ty);
881 }
882
883 // EntryNode could meaningfully have debug info if we can find it...
SelectionDAG(const TargetMachine & tm,CodeGenOpt::Level OL)884 SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
885 : TM(tm), TSI(nullptr), TLI(nullptr), OptLevel(OL),
886 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
887 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
888 UpdateListeners(nullptr) {
889 InsertNode(&EntryNode);
890 DbgInfo = new SDDbgInfo();
891 }
892
init(MachineFunction & mf)893 void SelectionDAG::init(MachineFunction &mf) {
894 MF = &mf;
895 TLI = getSubtarget().getTargetLowering();
896 TSI = getSubtarget().getSelectionDAGInfo();
897 Context = &mf.getFunction()->getContext();
898 }
899
~SelectionDAG()900 SelectionDAG::~SelectionDAG() {
901 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
902 allnodes_clear();
903 OperandRecycler.clear(OperandAllocator);
904 delete DbgInfo;
905 }
906
allnodes_clear()907 void SelectionDAG::allnodes_clear() {
908 assert(&*AllNodes.begin() == &EntryNode);
909 AllNodes.remove(AllNodes.begin());
910 while (!AllNodes.empty())
911 DeallocateNode(&AllNodes.front());
912 #ifndef NDEBUG
913 NextPersistentId = 0;
914 #endif
915 }
916
GetBinarySDNode(unsigned Opcode,const SDLoc & DL,SDVTList VTs,SDValue N1,SDValue N2,const SDNodeFlags * Flags)917 SDNode *SelectionDAG::GetBinarySDNode(unsigned Opcode, const SDLoc &DL,
918 SDVTList VTs, SDValue N1, SDValue N2,
919 const SDNodeFlags *Flags) {
920 SDValue Ops[] = {N1, N2};
921
922 if (isBinOpWithFlags(Opcode)) {
923 // If no flags were passed in, use a default flags object.
924 SDNodeFlags F;
925 if (Flags == nullptr)
926 Flags = &F;
927
928 auto *FN = newSDNode<BinaryWithFlagsSDNode>(Opcode, DL.getIROrder(),
929 DL.getDebugLoc(), VTs, *Flags);
930 createOperands(FN, Ops);
931
932 return FN;
933 }
934
935 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
936 createOperands(N, Ops);
937 return N;
938 }
939
FindNodeOrInsertPos(const FoldingSetNodeID & ID,void * & InsertPos)940 SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
941 void *&InsertPos) {
942 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
943 if (N) {
944 switch (N->getOpcode()) {
945 default: break;
946 case ISD::Constant:
947 case ISD::ConstantFP:
948 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
949 "debug location. Use another overload.");
950 }
951 }
952 return N;
953 }
954
FindNodeOrInsertPos(const FoldingSetNodeID & ID,const SDLoc & DL,void * & InsertPos)955 SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
956 const SDLoc &DL, void *&InsertPos) {
957 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
958 if (N) {
959 switch (N->getOpcode()) {
960 case ISD::Constant:
961 case ISD::ConstantFP:
962 // Erase debug location from the node if the node is used at several
963 // different places. Do not propagate one location to all uses as it
964 // will cause a worse single stepping debugging experience.
965 if (N->getDebugLoc() != DL.getDebugLoc())
966 N->setDebugLoc(DebugLoc());
967 break;
968 default:
969 // When the node's point of use is located earlier in the instruction
970 // sequence than its prior point of use, update its debug info to the
971 // earlier location.
972 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
973 N->setDebugLoc(DL.getDebugLoc());
974 break;
975 }
976 }
977 return N;
978 }
979
clear()980 void SelectionDAG::clear() {
981 allnodes_clear();
982 OperandRecycler.clear(OperandAllocator);
983 OperandAllocator.Reset();
984 CSEMap.clear();
985
986 ExtendedValueTypeNodes.clear();
987 ExternalSymbols.clear();
988 TargetExternalSymbols.clear();
989 MCSymbols.clear();
990 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
991 static_cast<CondCodeSDNode*>(nullptr));
992 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
993 static_cast<SDNode*>(nullptr));
994
995 EntryNode.UseList = nullptr;
996 InsertNode(&EntryNode);
997 Root = getEntryNode();
998 DbgInfo->clear();
999 }
1000
getAnyExtOrTrunc(SDValue Op,const SDLoc & DL,EVT VT)1001 SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
1002 return VT.bitsGT(Op.getValueType()) ?
1003 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1004 getNode(ISD::TRUNCATE, DL, VT, Op);
1005 }
1006
getSExtOrTrunc(SDValue Op,const SDLoc & DL,EVT VT)1007 SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
1008 return VT.bitsGT(Op.getValueType()) ?
1009 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1010 getNode(ISD::TRUNCATE, DL, VT, Op);
1011 }
1012
getZExtOrTrunc(SDValue Op,const SDLoc & DL,EVT VT)1013 SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT) {
1014 return VT.bitsGT(Op.getValueType()) ?
1015 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1016 getNode(ISD::TRUNCATE, DL, VT, Op);
1017 }
1018
getBoolExtOrTrunc(SDValue Op,const SDLoc & SL,EVT VT,EVT OpVT)1019 SDValue SelectionDAG::getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT,
1020 EVT OpVT) {
1021 if (VT.bitsLE(Op.getValueType()))
1022 return getNode(ISD::TRUNCATE, SL, VT, Op);
1023
1024 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1025 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1026 }
1027
getZeroExtendInReg(SDValue Op,const SDLoc & DL,EVT VT)1028 SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT) {
1029 assert(!VT.isVector() &&
1030 "getZeroExtendInReg should use the vector element type instead of "
1031 "the vector type!");
1032 if (Op.getValueType() == VT) return Op;
1033 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
1034 APInt Imm = APInt::getLowBitsSet(BitWidth,
1035 VT.getSizeInBits());
1036 return getNode(ISD::AND, DL, Op.getValueType(), Op,
1037 getConstant(Imm, DL, Op.getValueType()));
1038 }
1039
getAnyExtendVectorInReg(SDValue Op,const SDLoc & DL,EVT VT)1040 SDValue SelectionDAG::getAnyExtendVectorInReg(SDValue Op, const SDLoc &DL,
1041 EVT VT) {
1042 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1043 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1044 "The sizes of the input and result must match in order to perform the "
1045 "extend in-register.");
1046 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1047 "The destination vector type must have fewer lanes than the input.");
1048 return getNode(ISD::ANY_EXTEND_VECTOR_INREG, DL, VT, Op);
1049 }
1050
getSignExtendVectorInReg(SDValue Op,const SDLoc & DL,EVT VT)1051 SDValue SelectionDAG::getSignExtendVectorInReg(SDValue Op, const SDLoc &DL,
1052 EVT VT) {
1053 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1054 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1055 "The sizes of the input and result must match in order to perform the "
1056 "extend in-register.");
1057 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1058 "The destination vector type must have fewer lanes than the input.");
1059 return getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, VT, Op);
1060 }
1061
getZeroExtendVectorInReg(SDValue Op,const SDLoc & DL,EVT VT)1062 SDValue SelectionDAG::getZeroExtendVectorInReg(SDValue Op, const SDLoc &DL,
1063 EVT VT) {
1064 assert(VT.isVector() && "This DAG node is restricted to vector types.");
1065 assert(VT.getSizeInBits() == Op.getValueType().getSizeInBits() &&
1066 "The sizes of the input and result must match in order to perform the "
1067 "extend in-register.");
1068 assert(VT.getVectorNumElements() < Op.getValueType().getVectorNumElements() &&
1069 "The destination vector type must have fewer lanes than the input.");
1070 return getNode(ISD::ZERO_EXTEND_VECTOR_INREG, DL, VT, Op);
1071 }
1072
1073 /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1074 ///
getNOT(const SDLoc & DL,SDValue Val,EVT VT)1075 SDValue SelectionDAG::getNOT(const SDLoc &DL, SDValue Val, EVT VT) {
1076 EVT EltVT = VT.getScalarType();
1077 SDValue NegOne =
1078 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL, VT);
1079 return getNode(ISD::XOR, DL, VT, Val, NegOne);
1080 }
1081
getLogicalNOT(const SDLoc & DL,SDValue Val,EVT VT)1082 SDValue SelectionDAG::getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT) {
1083 EVT EltVT = VT.getScalarType();
1084 SDValue TrueValue;
1085 switch (TLI->getBooleanContents(VT)) {
1086 case TargetLowering::ZeroOrOneBooleanContent:
1087 case TargetLowering::UndefinedBooleanContent:
1088 TrueValue = getConstant(1, DL, VT);
1089 break;
1090 case TargetLowering::ZeroOrNegativeOneBooleanContent:
1091 TrueValue = getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), DL,
1092 VT);
1093 break;
1094 }
1095 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1096 }
1097
getConstant(uint64_t Val,const SDLoc & DL,EVT VT,bool isT,bool isO)1098 SDValue SelectionDAG::getConstant(uint64_t Val, const SDLoc &DL, EVT VT,
1099 bool isT, bool isO) {
1100 EVT EltVT = VT.getScalarType();
1101 assert((EltVT.getSizeInBits() >= 64 ||
1102 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1103 "getConstant with a uint64_t value that doesn't fit in the type!");
1104 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
1105 }
1106
getConstant(const APInt & Val,const SDLoc & DL,EVT VT,bool isT,bool isO)1107 SDValue SelectionDAG::getConstant(const APInt &Val, const SDLoc &DL, EVT VT,
1108 bool isT, bool isO) {
1109 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1110 }
1111
getConstant(const ConstantInt & Val,const SDLoc & DL,EVT VT,bool isT,bool isO)1112 SDValue SelectionDAG::getConstant(const ConstantInt &Val, const SDLoc &DL,
1113 EVT VT, bool isT, bool isO) {
1114 assert(VT.isInteger() && "Cannot create FP integer constant!");
1115
1116 EVT EltVT = VT.getScalarType();
1117 const ConstantInt *Elt = &Val;
1118
1119 // In some cases the vector type is legal but the element type is illegal and
1120 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1121 // inserted value (the type does not need to match the vector element type).
1122 // Any extra bits introduced will be truncated away.
1123 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1124 TargetLowering::TypePromoteInteger) {
1125 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1126 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1127 Elt = ConstantInt::get(*getContext(), NewVal);
1128 }
1129 // In other cases the element type is illegal and needs to be expanded, for
1130 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1131 // the value into n parts and use a vector type with n-times the elements.
1132 // Then bitcast to the type requested.
1133 // Legalizing constants too early makes the DAGCombiner's job harder so we
1134 // only legalize if the DAG tells us we must produce legal types.
1135 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1136 TLI->getTypeAction(*getContext(), EltVT) ==
1137 TargetLowering::TypeExpandInteger) {
1138 const APInt &NewVal = Elt->getValue();
1139 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1140 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1141 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1142 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1143
1144 // Check the temporary vector is the correct size. If this fails then
1145 // getTypeToTransformTo() probably returned a type whose size (in bits)
1146 // isn't a power-of-2 factor of the requested type size.
1147 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1148
1149 SmallVector<SDValue, 2> EltParts;
1150 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1151 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1152 .trunc(ViaEltSizeInBits), DL,
1153 ViaEltVT, isT, isO));
1154 }
1155
1156 // EltParts is currently in little endian order. If we actually want
1157 // big-endian order then reverse it now.
1158 if (getDataLayout().isBigEndian())
1159 std::reverse(EltParts.begin(), EltParts.end());
1160
1161 // The elements must be reversed when the element order is different
1162 // to the endianness of the elements (because the BITCAST is itself a
1163 // vector shuffle in this situation). However, we do not need any code to
1164 // perform this reversal because getConstant() is producing a vector
1165 // splat.
1166 // This situation occurs in MIPS MSA.
1167
1168 SmallVector<SDValue, 8> Ops;
1169 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1170 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1171
1172 SDValue Result = getNode(ISD::BITCAST, DL, VT,
1173 getNode(ISD::BUILD_VECTOR, DL, ViaVecVT, Ops));
1174 return Result;
1175 }
1176
1177 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1178 "APInt size does not match type size!");
1179 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1180 FoldingSetNodeID ID;
1181 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
1182 ID.AddPointer(Elt);
1183 ID.AddBoolean(isO);
1184 void *IP = nullptr;
1185 SDNode *N = nullptr;
1186 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1187 if (!VT.isVector())
1188 return SDValue(N, 0);
1189
1190 if (!N) {
1191 N = newSDNode<ConstantSDNode>(isT, isO, Elt, DL.getDebugLoc(), EltVT);
1192 CSEMap.InsertNode(N, IP);
1193 InsertNode(N);
1194 }
1195
1196 SDValue Result(N, 0);
1197 if (VT.isVector())
1198 Result = getSplatBuildVector(VT, DL, Result);
1199 return Result;
1200 }
1201
getIntPtrConstant(uint64_t Val,const SDLoc & DL,bool isTarget)1202 SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, const SDLoc &DL,
1203 bool isTarget) {
1204 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1205 }
1206
getConstantFP(const APFloat & V,const SDLoc & DL,EVT VT,bool isTarget)1207 SDValue SelectionDAG::getConstantFP(const APFloat &V, const SDLoc &DL, EVT VT,
1208 bool isTarget) {
1209 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1210 }
1211
getConstantFP(const ConstantFP & V,const SDLoc & DL,EVT VT,bool isTarget)1212 SDValue SelectionDAG::getConstantFP(const ConstantFP &V, const SDLoc &DL,
1213 EVT VT, bool isTarget) {
1214 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1215
1216 EVT EltVT = VT.getScalarType();
1217
1218 // Do the map lookup using the actual bit pattern for the floating point
1219 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1220 // we don't have issues with SNANs.
1221 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1222 FoldingSetNodeID ID;
1223 AddNodeIDNode(ID, Opc, getVTList(EltVT), None);
1224 ID.AddPointer(&V);
1225 void *IP = nullptr;
1226 SDNode *N = nullptr;
1227 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1228 if (!VT.isVector())
1229 return SDValue(N, 0);
1230
1231 if (!N) {
1232 N = newSDNode<ConstantFPSDNode>(isTarget, &V, DL.getDebugLoc(), EltVT);
1233 CSEMap.InsertNode(N, IP);
1234 InsertNode(N);
1235 }
1236
1237 SDValue Result(N, 0);
1238 if (VT.isVector())
1239 Result = getSplatBuildVector(VT, DL, Result);
1240 return Result;
1241 }
1242
getConstantFP(double Val,const SDLoc & DL,EVT VT,bool isTarget)1243 SDValue SelectionDAG::getConstantFP(double Val, const SDLoc &DL, EVT VT,
1244 bool isTarget) {
1245 EVT EltVT = VT.getScalarType();
1246 if (EltVT == MVT::f32)
1247 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1248 else if (EltVT == MVT::f64)
1249 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1250 else if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1251 EltVT == MVT::f16) {
1252 bool Ignored;
1253 APFloat APF = APFloat(Val);
1254 APF.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
1255 &Ignored);
1256 return getConstantFP(APF, DL, VT, isTarget);
1257 } else
1258 llvm_unreachable("Unsupported type in getConstantFP");
1259 }
1260
getGlobalAddress(const GlobalValue * GV,const SDLoc & DL,EVT VT,int64_t Offset,bool isTargetGA,unsigned char TargetFlags)1261 SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, const SDLoc &DL,
1262 EVT VT, int64_t Offset, bool isTargetGA,
1263 unsigned char TargetFlags) {
1264 assert((TargetFlags == 0 || isTargetGA) &&
1265 "Cannot set target flags on target-independent globals");
1266
1267 // Truncate (with sign-extension) the offset value to the pointer size.
1268 unsigned BitWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
1269 if (BitWidth < 64)
1270 Offset = SignExtend64(Offset, BitWidth);
1271
1272 unsigned Opc;
1273 if (GV->isThreadLocal())
1274 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1275 else
1276 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1277
1278 FoldingSetNodeID ID;
1279 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1280 ID.AddPointer(GV);
1281 ID.AddInteger(Offset);
1282 ID.AddInteger(TargetFlags);
1283 ID.AddInteger(GV->getType()->getAddressSpace());
1284 void *IP = nullptr;
1285 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1286 return SDValue(E, 0);
1287
1288 auto *N = newSDNode<GlobalAddressSDNode>(
1289 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VT, Offset, TargetFlags);
1290 CSEMap.InsertNode(N, IP);
1291 InsertNode(N);
1292 return SDValue(N, 0);
1293 }
1294
getFrameIndex(int FI,EVT VT,bool isTarget)1295 SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1296 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1297 FoldingSetNodeID ID;
1298 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1299 ID.AddInteger(FI);
1300 void *IP = nullptr;
1301 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1302 return SDValue(E, 0);
1303
1304 auto *N = newSDNode<FrameIndexSDNode>(FI, VT, isTarget);
1305 CSEMap.InsertNode(N, IP);
1306 InsertNode(N);
1307 return SDValue(N, 0);
1308 }
1309
getJumpTable(int JTI,EVT VT,bool isTarget,unsigned char TargetFlags)1310 SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1311 unsigned char TargetFlags) {
1312 assert((TargetFlags == 0 || isTarget) &&
1313 "Cannot set target flags on target-independent jump tables");
1314 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1315 FoldingSetNodeID ID;
1316 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1317 ID.AddInteger(JTI);
1318 ID.AddInteger(TargetFlags);
1319 void *IP = nullptr;
1320 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1321 return SDValue(E, 0);
1322
1323 auto *N = newSDNode<JumpTableSDNode>(JTI, VT, isTarget, TargetFlags);
1324 CSEMap.InsertNode(N, IP);
1325 InsertNode(N);
1326 return SDValue(N, 0);
1327 }
1328
getConstantPool(const Constant * C,EVT VT,unsigned Alignment,int Offset,bool isTarget,unsigned char TargetFlags)1329 SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
1330 unsigned Alignment, int Offset,
1331 bool isTarget,
1332 unsigned char TargetFlags) {
1333 assert((TargetFlags == 0 || isTarget) &&
1334 "Cannot set target flags on target-independent globals");
1335 if (Alignment == 0)
1336 Alignment = getDataLayout().getPrefTypeAlignment(C->getType());
1337 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1338 FoldingSetNodeID ID;
1339 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1340 ID.AddInteger(Alignment);
1341 ID.AddInteger(Offset);
1342 ID.AddPointer(C);
1343 ID.AddInteger(TargetFlags);
1344 void *IP = nullptr;
1345 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1346 return SDValue(E, 0);
1347
1348 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VT, Offset, Alignment,
1349 TargetFlags);
1350 CSEMap.InsertNode(N, IP);
1351 InsertNode(N);
1352 return SDValue(N, 0);
1353 }
1354
1355
getConstantPool(MachineConstantPoolValue * C,EVT VT,unsigned Alignment,int Offset,bool isTarget,unsigned char TargetFlags)1356 SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
1357 unsigned Alignment, int Offset,
1358 bool isTarget,
1359 unsigned char TargetFlags) {
1360 assert((TargetFlags == 0 || isTarget) &&
1361 "Cannot set target flags on target-independent globals");
1362 if (Alignment == 0)
1363 Alignment = getDataLayout().getPrefTypeAlignment(C->getType());
1364 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1365 FoldingSetNodeID ID;
1366 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1367 ID.AddInteger(Alignment);
1368 ID.AddInteger(Offset);
1369 C->addSelectionDAGCSEId(ID);
1370 ID.AddInteger(TargetFlags);
1371 void *IP = nullptr;
1372 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1373 return SDValue(E, 0);
1374
1375 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VT, Offset, Alignment,
1376 TargetFlags);
1377 CSEMap.InsertNode(N, IP);
1378 InsertNode(N);
1379 return SDValue(N, 0);
1380 }
1381
getTargetIndex(int Index,EVT VT,int64_t Offset,unsigned char TargetFlags)1382 SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1383 unsigned char TargetFlags) {
1384 FoldingSetNodeID ID;
1385 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), None);
1386 ID.AddInteger(Index);
1387 ID.AddInteger(Offset);
1388 ID.AddInteger(TargetFlags);
1389 void *IP = nullptr;
1390 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1391 return SDValue(E, 0);
1392
1393 auto *N = newSDNode<TargetIndexSDNode>(Index, VT, Offset, TargetFlags);
1394 CSEMap.InsertNode(N, IP);
1395 InsertNode(N);
1396 return SDValue(N, 0);
1397 }
1398
getBasicBlock(MachineBasicBlock * MBB)1399 SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
1400 FoldingSetNodeID ID;
1401 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), None);
1402 ID.AddPointer(MBB);
1403 void *IP = nullptr;
1404 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1405 return SDValue(E, 0);
1406
1407 auto *N = newSDNode<BasicBlockSDNode>(MBB);
1408 CSEMap.InsertNode(N, IP);
1409 InsertNode(N);
1410 return SDValue(N, 0);
1411 }
1412
getValueType(EVT VT)1413 SDValue SelectionDAG::getValueType(EVT VT) {
1414 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1415 ValueTypeNodes.size())
1416 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
1417
1418 SDNode *&N = VT.isExtended() ?
1419 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
1420
1421 if (N) return SDValue(N, 0);
1422 N = newSDNode<VTSDNode>(VT);
1423 InsertNode(N);
1424 return SDValue(N, 0);
1425 }
1426
getExternalSymbol(const char * Sym,EVT VT)1427 SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
1428 SDNode *&N = ExternalSymbols[Sym];
1429 if (N) return SDValue(N, 0);
1430 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, VT);
1431 InsertNode(N);
1432 return SDValue(N, 0);
1433 }
1434
getMCSymbol(MCSymbol * Sym,EVT VT)1435 SDValue SelectionDAG::getMCSymbol(MCSymbol *Sym, EVT VT) {
1436 SDNode *&N = MCSymbols[Sym];
1437 if (N)
1438 return SDValue(N, 0);
1439 N = newSDNode<MCSymbolSDNode>(Sym, VT);
1440 InsertNode(N);
1441 return SDValue(N, 0);
1442 }
1443
getTargetExternalSymbol(const char * Sym,EVT VT,unsigned char TargetFlags)1444 SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
1445 unsigned char TargetFlags) {
1446 SDNode *&N =
1447 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1448 TargetFlags)];
1449 if (N) return SDValue(N, 0);
1450 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, VT);
1451 InsertNode(N);
1452 return SDValue(N, 0);
1453 }
1454
getCondCode(ISD::CondCode Cond)1455 SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
1456 if ((unsigned)Cond >= CondCodeNodes.size())
1457 CondCodeNodes.resize(Cond+1);
1458
1459 if (!CondCodeNodes[Cond]) {
1460 auto *N = newSDNode<CondCodeSDNode>(Cond);
1461 CondCodeNodes[Cond] = N;
1462 InsertNode(N);
1463 }
1464
1465 return SDValue(CondCodeNodes[Cond], 0);
1466 }
1467
1468 /// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
1469 /// point at N1 to point at N2 and indices that point at N2 to point at N1.
commuteShuffle(SDValue & N1,SDValue & N2,MutableArrayRef<int> M)1470 static void commuteShuffle(SDValue &N1, SDValue &N2, MutableArrayRef<int> M) {
1471 std::swap(N1, N2);
1472 ShuffleVectorSDNode::commuteMask(M);
1473 }
1474
getVectorShuffle(EVT VT,const SDLoc & dl,SDValue N1,SDValue N2,ArrayRef<int> Mask)1475 SDValue SelectionDAG::getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1,
1476 SDValue N2, ArrayRef<int> Mask) {
1477 assert(VT.getVectorNumElements() == Mask.size() &&
1478 "Must have the same number of vector elements as mask elements!");
1479 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1480 "Invalid VECTOR_SHUFFLE");
1481
1482 // Canonicalize shuffle undef, undef -> undef
1483 if (N1.isUndef() && N2.isUndef())
1484 return getUNDEF(VT);
1485
1486 // Validate that all indices in Mask are within the range of the elements
1487 // input to the shuffle.
1488 int NElts = Mask.size();
1489 assert(all_of(Mask, [&](int M) { return M < (NElts * 2); }) &&
1490 "Index out of range");
1491
1492 // Copy the mask so we can do any needed cleanup.
1493 SmallVector<int, 8> MaskVec(Mask.begin(), Mask.end());
1494
1495 // Canonicalize shuffle v, v -> v, undef
1496 if (N1 == N2) {
1497 N2 = getUNDEF(VT);
1498 for (int i = 0; i != NElts; ++i)
1499 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
1500 }
1501
1502 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1503 if (N1.isUndef())
1504 commuteShuffle(N1, N2, MaskVec);
1505
1506 // If shuffling a splat, try to blend the splat instead. We do this here so
1507 // that even when this arises during lowering we don't have to re-handle it.
1508 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
1509 BitVector UndefElements;
1510 SDValue Splat = BV->getSplatValue(&UndefElements);
1511 if (!Splat)
1512 return;
1513
1514 for (int i = 0; i < NElts; ++i) {
1515 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
1516 continue;
1517
1518 // If this input comes from undef, mark it as such.
1519 if (UndefElements[MaskVec[i] - Offset]) {
1520 MaskVec[i] = -1;
1521 continue;
1522 }
1523
1524 // If we can blend a non-undef lane, use that instead.
1525 if (!UndefElements[i])
1526 MaskVec[i] = i + Offset;
1527 }
1528 };
1529 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
1530 BlendSplat(N1BV, 0);
1531 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
1532 BlendSplat(N2BV, NElts);
1533
1534 // Canonicalize all index into lhs, -> shuffle lhs, undef
1535 // Canonicalize all index into rhs, -> shuffle rhs, undef
1536 bool AllLHS = true, AllRHS = true;
1537 bool N2Undef = N2.isUndef();
1538 for (int i = 0; i != NElts; ++i) {
1539 if (MaskVec[i] >= NElts) {
1540 if (N2Undef)
1541 MaskVec[i] = -1;
1542 else
1543 AllLHS = false;
1544 } else if (MaskVec[i] >= 0) {
1545 AllRHS = false;
1546 }
1547 }
1548 if (AllLHS && AllRHS)
1549 return getUNDEF(VT);
1550 if (AllLHS && !N2Undef)
1551 N2 = getUNDEF(VT);
1552 if (AllRHS) {
1553 N1 = getUNDEF(VT);
1554 commuteShuffle(N1, N2, MaskVec);
1555 }
1556 // Reset our undef status after accounting for the mask.
1557 N2Undef = N2.isUndef();
1558 // Re-check whether both sides ended up undef.
1559 if (N1.isUndef() && N2Undef)
1560 return getUNDEF(VT);
1561
1562 // If Identity shuffle return that node.
1563 bool Identity = true, AllSame = true;
1564 for (int i = 0; i != NElts; ++i) {
1565 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
1566 if (MaskVec[i] != MaskVec[0]) AllSame = false;
1567 }
1568 if (Identity && NElts)
1569 return N1;
1570
1571 // Shuffling a constant splat doesn't change the result.
1572 if (N2Undef) {
1573 SDValue V = N1;
1574
1575 // Look through any bitcasts. We check that these don't change the number
1576 // (and size) of elements and just changes their types.
1577 while (V.getOpcode() == ISD::BITCAST)
1578 V = V->getOperand(0);
1579
1580 // A splat should always show up as a build vector node.
1581 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
1582 BitVector UndefElements;
1583 SDValue Splat = BV->getSplatValue(&UndefElements);
1584 // If this is a splat of an undef, shuffling it is also undef.
1585 if (Splat && Splat.isUndef())
1586 return getUNDEF(VT);
1587
1588 bool SameNumElts =
1589 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
1590
1591 // We only have a splat which can skip shuffles if there is a splatted
1592 // value and no undef lanes rearranged by the shuffle.
1593 if (Splat && UndefElements.none()) {
1594 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
1595 // number of elements match or the value splatted is a zero constant.
1596 if (SameNumElts)
1597 return N1;
1598 if (auto *C = dyn_cast<ConstantSDNode>(Splat))
1599 if (C->isNullValue())
1600 return N1;
1601 }
1602
1603 // If the shuffle itself creates a splat, build the vector directly.
1604 if (AllSame && SameNumElts) {
1605 EVT BuildVT = BV->getValueType(0);
1606 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
1607 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
1608
1609 // We may have jumped through bitcasts, so the type of the
1610 // BUILD_VECTOR may not match the type of the shuffle.
1611 if (BuildVT != VT)
1612 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
1613 return NewBV;
1614 }
1615 }
1616 }
1617
1618 FoldingSetNodeID ID;
1619 SDValue Ops[2] = { N1, N2 };
1620 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops);
1621 for (int i = 0; i != NElts; ++i)
1622 ID.AddInteger(MaskVec[i]);
1623
1624 void* IP = nullptr;
1625 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
1626 return SDValue(E, 0);
1627
1628 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1629 // SDNode doesn't have access to it. This memory will be "leaked" when
1630 // the node is deallocated, but recovered when the NodeAllocator is released.
1631 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1632 std::copy(MaskVec.begin(), MaskVec.end(), MaskAlloc);
1633
1634 auto *N = newSDNode<ShuffleVectorSDNode>(VT, dl.getIROrder(),
1635 dl.getDebugLoc(), MaskAlloc);
1636 createOperands(N, Ops);
1637
1638 CSEMap.InsertNode(N, IP);
1639 InsertNode(N);
1640 return SDValue(N, 0);
1641 }
1642
getCommutedVectorShuffle(const ShuffleVectorSDNode & SV)1643 SDValue SelectionDAG::getCommutedVectorShuffle(const ShuffleVectorSDNode &SV) {
1644 MVT VT = SV.getSimpleValueType(0);
1645 SmallVector<int, 8> MaskVec(SV.getMask().begin(), SV.getMask().end());
1646 ShuffleVectorSDNode::commuteMask(MaskVec);
1647
1648 SDValue Op0 = SV.getOperand(0);
1649 SDValue Op1 = SV.getOperand(1);
1650 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
1651 }
1652
getConvertRndSat(EVT VT,const SDLoc & dl,SDValue Val,SDValue DTy,SDValue STy,SDValue Rnd,SDValue Sat,ISD::CvtCode Code)1653 SDValue SelectionDAG::getConvertRndSat(EVT VT, const SDLoc &dl, SDValue Val,
1654 SDValue DTy, SDValue STy, SDValue Rnd,
1655 SDValue Sat, ISD::CvtCode Code) {
1656 // If the src and dest types are the same and the conversion is between
1657 // integer types of the same sign or two floats, no conversion is necessary.
1658 if (DTy == STy &&
1659 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
1660 return Val;
1661
1662 FoldingSetNodeID ID;
1663 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1664 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), Ops);
1665 void* IP = nullptr;
1666 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
1667 return SDValue(E, 0);
1668
1669 auto *N =
1670 newSDNode<CvtRndSatSDNode>(VT, dl.getIROrder(), dl.getDebugLoc(), Code);
1671 createOperands(N, Ops);
1672
1673 CSEMap.InsertNode(N, IP);
1674 InsertNode(N);
1675 return SDValue(N, 0);
1676 }
1677
getRegister(unsigned RegNo,EVT VT)1678 SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
1679 FoldingSetNodeID ID;
1680 AddNodeIDNode(ID, ISD::Register, getVTList(VT), None);
1681 ID.AddInteger(RegNo);
1682 void *IP = nullptr;
1683 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1684 return SDValue(E, 0);
1685
1686 auto *N = newSDNode<RegisterSDNode>(RegNo, VT);
1687 CSEMap.InsertNode(N, IP);
1688 InsertNode(N);
1689 return SDValue(N, 0);
1690 }
1691
getRegisterMask(const uint32_t * RegMask)1692 SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1693 FoldingSetNodeID ID;
1694 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), None);
1695 ID.AddPointer(RegMask);
1696 void *IP = nullptr;
1697 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1698 return SDValue(E, 0);
1699
1700 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
1701 CSEMap.InsertNode(N, IP);
1702 InsertNode(N);
1703 return SDValue(N, 0);
1704 }
1705
getEHLabel(const SDLoc & dl,SDValue Root,MCSymbol * Label)1706 SDValue SelectionDAG::getEHLabel(const SDLoc &dl, SDValue Root,
1707 MCSymbol *Label) {
1708 FoldingSetNodeID ID;
1709 SDValue Ops[] = { Root };
1710 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), Ops);
1711 ID.AddPointer(Label);
1712 void *IP = nullptr;
1713 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1714 return SDValue(E, 0);
1715
1716 auto *N = newSDNode<EHLabelSDNode>(dl.getIROrder(), dl.getDebugLoc(), Label);
1717 createOperands(N, Ops);
1718
1719 CSEMap.InsertNode(N, IP);
1720 InsertNode(N);
1721 return SDValue(N, 0);
1722 }
1723
getBlockAddress(const BlockAddress * BA,EVT VT,int64_t Offset,bool isTarget,unsigned char TargetFlags)1724 SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
1725 int64_t Offset,
1726 bool isTarget,
1727 unsigned char TargetFlags) {
1728 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1729
1730 FoldingSetNodeID ID;
1731 AddNodeIDNode(ID, Opc, getVTList(VT), None);
1732 ID.AddPointer(BA);
1733 ID.AddInteger(Offset);
1734 ID.AddInteger(TargetFlags);
1735 void *IP = nullptr;
1736 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1737 return SDValue(E, 0);
1738
1739 auto *N = newSDNode<BlockAddressSDNode>(Opc, VT, BA, Offset, TargetFlags);
1740 CSEMap.InsertNode(N, IP);
1741 InsertNode(N);
1742 return SDValue(N, 0);
1743 }
1744
getSrcValue(const Value * V)1745 SDValue SelectionDAG::getSrcValue(const Value *V) {
1746 assert((!V || V->getType()->isPointerTy()) &&
1747 "SrcValue is not a pointer?");
1748
1749 FoldingSetNodeID ID;
1750 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), None);
1751 ID.AddPointer(V);
1752
1753 void *IP = nullptr;
1754 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1755 return SDValue(E, 0);
1756
1757 auto *N = newSDNode<SrcValueSDNode>(V);
1758 CSEMap.InsertNode(N, IP);
1759 InsertNode(N);
1760 return SDValue(N, 0);
1761 }
1762
getMDNode(const MDNode * MD)1763 SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1764 FoldingSetNodeID ID;
1765 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), None);
1766 ID.AddPointer(MD);
1767
1768 void *IP = nullptr;
1769 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1770 return SDValue(E, 0);
1771
1772 auto *N = newSDNode<MDNodeSDNode>(MD);
1773 CSEMap.InsertNode(N, IP);
1774 InsertNode(N);
1775 return SDValue(N, 0);
1776 }
1777
getBitcast(EVT VT,SDValue V)1778 SDValue SelectionDAG::getBitcast(EVT VT, SDValue V) {
1779 if (VT == V.getValueType())
1780 return V;
1781
1782 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
1783 }
1784
getAddrSpaceCast(const SDLoc & dl,EVT VT,SDValue Ptr,unsigned SrcAS,unsigned DestAS)1785 SDValue SelectionDAG::getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr,
1786 unsigned SrcAS, unsigned DestAS) {
1787 SDValue Ops[] = {Ptr};
1788 FoldingSetNodeID ID;
1789 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), Ops);
1790 ID.AddInteger(SrcAS);
1791 ID.AddInteger(DestAS);
1792
1793 void *IP = nullptr;
1794 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
1795 return SDValue(E, 0);
1796
1797 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
1798 VT, SrcAS, DestAS);
1799 createOperands(N, Ops);
1800
1801 CSEMap.InsertNode(N, IP);
1802 InsertNode(N);
1803 return SDValue(N, 0);
1804 }
1805
1806 /// getShiftAmountOperand - Return the specified value casted to
1807 /// the target's desired shift amount type.
getShiftAmountOperand(EVT LHSTy,SDValue Op)1808 SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
1809 EVT OpTy = Op.getValueType();
1810 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
1811 if (OpTy == ShTy || OpTy.isVector()) return Op;
1812
1813 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
1814 }
1815
expandVAArg(SDNode * Node)1816 SDValue SelectionDAG::expandVAArg(SDNode *Node) {
1817 SDLoc dl(Node);
1818 const TargetLowering &TLI = getTargetLoweringInfo();
1819 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1820 EVT VT = Node->getValueType(0);
1821 SDValue Tmp1 = Node->getOperand(0);
1822 SDValue Tmp2 = Node->getOperand(1);
1823 unsigned Align = Node->getConstantOperandVal(3);
1824
1825 SDValue VAListLoad =
1826 getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1, Tmp2,
1827 MachinePointerInfo(V), false, false, false, 0);
1828 SDValue VAList = VAListLoad;
1829
1830 if (Align > TLI.getMinStackArgumentAlignment()) {
1831 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1832
1833 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
1834 getConstant(Align - 1, dl, VAList.getValueType()));
1835
1836 VAList = getNode(ISD::AND, dl, VAList.getValueType(), VAList,
1837 getConstant(-(int64_t)Align, dl, VAList.getValueType()));
1838 }
1839
1840 // Increment the pointer, VAList, to the next vaarg
1841 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
1842 getConstant(getDataLayout().getTypeAllocSize(
1843 VT.getTypeForEVT(*getContext())),
1844 dl, VAList.getValueType()));
1845 // Store the incremented VAList to the legalized pointer
1846 Tmp1 = getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2,
1847 MachinePointerInfo(V), false, false, 0);
1848 // Load the actual argument out of the pointer VAList
1849 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo(),
1850 false, false, false, 0);
1851 }
1852
expandVACopy(SDNode * Node)1853 SDValue SelectionDAG::expandVACopy(SDNode *Node) {
1854 SDLoc dl(Node);
1855 const TargetLowering &TLI = getTargetLoweringInfo();
1856 // This defaults to loading a pointer from the input and storing it to the
1857 // output, returning the chain.
1858 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
1859 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
1860 SDValue Tmp1 = getLoad(TLI.getPointerTy(getDataLayout()), dl,
1861 Node->getOperand(0), Node->getOperand(2),
1862 MachinePointerInfo(VS), false, false, false, 0);
1863 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
1864 MachinePointerInfo(VD), false, false, 0);
1865 }
1866
CreateStackTemporary(EVT VT,unsigned minAlign)1867 SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
1868 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1869 unsigned ByteSize = VT.getStoreSize();
1870 Type *Ty = VT.getTypeForEVT(*getContext());
1871 unsigned StackAlign =
1872 std::max((unsigned)getDataLayout().getPrefTypeAlignment(Ty), minAlign);
1873
1874 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
1875 return getFrameIndex(FrameIdx, TLI->getPointerTy(getDataLayout()));
1876 }
1877
CreateStackTemporary(EVT VT1,EVT VT2)1878 SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
1879 unsigned Bytes = std::max(VT1.getStoreSize(), VT2.getStoreSize());
1880 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1881 Type *Ty2 = VT2.getTypeForEVT(*getContext());
1882 const DataLayout &DL = getDataLayout();
1883 unsigned Align =
1884 std::max(DL.getPrefTypeAlignment(Ty1), DL.getPrefTypeAlignment(Ty2));
1885
1886 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1887 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
1888 return getFrameIndex(FrameIdx, TLI->getPointerTy(getDataLayout()));
1889 }
1890
FoldSetCC(EVT VT,SDValue N1,SDValue N2,ISD::CondCode Cond,const SDLoc & dl)1891 SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1, SDValue N2,
1892 ISD::CondCode Cond, const SDLoc &dl) {
1893 // These setcc operations always fold.
1894 switch (Cond) {
1895 default: break;
1896 case ISD::SETFALSE:
1897 case ISD::SETFALSE2: return getConstant(0, dl, VT);
1898 case ISD::SETTRUE:
1899 case ISD::SETTRUE2: {
1900 TargetLowering::BooleanContent Cnt =
1901 TLI->getBooleanContents(N1->getValueType(0));
1902 return getConstant(
1903 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, dl,
1904 VT);
1905 }
1906
1907 case ISD::SETOEQ:
1908 case ISD::SETOGT:
1909 case ISD::SETOGE:
1910 case ISD::SETOLT:
1911 case ISD::SETOLE:
1912 case ISD::SETONE:
1913 case ISD::SETO:
1914 case ISD::SETUO:
1915 case ISD::SETUEQ:
1916 case ISD::SETUNE:
1917 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
1918 break;
1919 }
1920
1921 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
1922 const APInt &C2 = N2C->getAPIntValue();
1923 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
1924 const APInt &C1 = N1C->getAPIntValue();
1925
1926 switch (Cond) {
1927 default: llvm_unreachable("Unknown integer setcc!");
1928 case ISD::SETEQ: return getConstant(C1 == C2, dl, VT);
1929 case ISD::SETNE: return getConstant(C1 != C2, dl, VT);
1930 case ISD::SETULT: return getConstant(C1.ult(C2), dl, VT);
1931 case ISD::SETUGT: return getConstant(C1.ugt(C2), dl, VT);
1932 case ISD::SETULE: return getConstant(C1.ule(C2), dl, VT);
1933 case ISD::SETUGE: return getConstant(C1.uge(C2), dl, VT);
1934 case ISD::SETLT: return getConstant(C1.slt(C2), dl, VT);
1935 case ISD::SETGT: return getConstant(C1.sgt(C2), dl, VT);
1936 case ISD::SETLE: return getConstant(C1.sle(C2), dl, VT);
1937 case ISD::SETGE: return getConstant(C1.sge(C2), dl, VT);
1938 }
1939 }
1940 }
1941 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1)) {
1942 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2)) {
1943 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
1944 switch (Cond) {
1945 default: break;
1946 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1947 return getUNDEF(VT);
1948 // fall through
1949 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, dl, VT);
1950 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1951 return getUNDEF(VT);
1952 // fall through
1953 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
1954 R==APFloat::cmpLessThan, dl, VT);
1955 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1956 return getUNDEF(VT);
1957 // fall through
1958 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, dl, VT);
1959 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1960 return getUNDEF(VT);
1961 // fall through
1962 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, dl, VT);
1963 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1964 return getUNDEF(VT);
1965 // fall through
1966 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
1967 R==APFloat::cmpEqual, dl, VT);
1968 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1969 return getUNDEF(VT);
1970 // fall through
1971 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
1972 R==APFloat::cmpEqual, dl, VT);
1973 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, dl, VT);
1974 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, dl, VT);
1975 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1976 R==APFloat::cmpEqual, dl, VT);
1977 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, dl, VT);
1978 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1979 R==APFloat::cmpLessThan, dl, VT);
1980 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1981 R==APFloat::cmpUnordered, dl, VT);
1982 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, dl, VT);
1983 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, dl, VT);
1984 }
1985 } else {
1986 // Ensure that the constant occurs on the RHS.
1987 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1988 MVT CompVT = N1.getValueType().getSimpleVT();
1989 if (!TLI->isCondCodeLegal(SwappedCond, CompVT))
1990 return SDValue();
1991
1992 return getSetCC(dl, VT, N2, N1, SwappedCond);
1993 }
1994 }
1995
1996 // Could not fold it.
1997 return SDValue();
1998 }
1999
2000 /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2001 /// use this predicate to simplify operations downstream.
SignBitIsZero(SDValue Op,unsigned Depth) const2002 bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
2003 // This predicate is not safe for vector operations.
2004 if (Op.getValueType().isVector())
2005 return false;
2006
2007 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
2008 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
2009 }
2010
2011 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2012 /// this predicate to simplify operations downstream. Mask is known to be zero
2013 /// for bits that V cannot have.
MaskedValueIsZero(SDValue Op,const APInt & Mask,unsigned Depth) const2014 bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
2015 unsigned Depth) const {
2016 APInt KnownZero, KnownOne;
2017 computeKnownBits(Op, KnownZero, KnownOne, Depth);
2018 return (KnownZero & Mask) == Mask;
2019 }
2020
2021 /// Determine which bits of Op are known to be either zero or one and return
2022 /// them in the KnownZero/KnownOne bitsets.
computeKnownBits(SDValue Op,APInt & KnownZero,APInt & KnownOne,unsigned Depth) const2023 void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
2024 APInt &KnownOne, unsigned Depth) const {
2025 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
2026
2027 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
2028 if (Depth == 6)
2029 return; // Limit search depth.
2030
2031 APInt KnownZero2, KnownOne2;
2032
2033 switch (Op.getOpcode()) {
2034 case ISD::Constant:
2035 // We know all of the bits for a constant!
2036 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
2037 KnownZero = ~KnownOne;
2038 break;
2039 case ISD::AND:
2040 // If either the LHS or the RHS are Zero, the result is zero.
2041 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2042 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2043
2044 // Output known-1 bits are only known if set in both the LHS & RHS.
2045 KnownOne &= KnownOne2;
2046 // Output known-0 are known to be clear if zero in either the LHS | RHS.
2047 KnownZero |= KnownZero2;
2048 break;
2049 case ISD::OR:
2050 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2051 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2052
2053 // Output known-0 bits are only known if clear in both the LHS & RHS.
2054 KnownZero &= KnownZero2;
2055 // Output known-1 are known to be set if set in either the LHS | RHS.
2056 KnownOne |= KnownOne2;
2057 break;
2058 case ISD::XOR: {
2059 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2060 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2061
2062 // Output known-0 bits are known if clear or set in both the LHS & RHS.
2063 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
2064 // Output known-1 are known to be set if set in only one of the LHS, RHS.
2065 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
2066 KnownZero = KnownZeroOut;
2067 break;
2068 }
2069 case ISD::MUL: {
2070 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2071 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2072
2073 // If low bits are zero in either operand, output low known-0 bits.
2074 // Also compute a conserative estimate for high known-0 bits.
2075 // More trickiness is possible, but this is sufficient for the
2076 // interesting case of alignment computation.
2077 KnownOne.clearAllBits();
2078 unsigned TrailZ = KnownZero.countTrailingOnes() +
2079 KnownZero2.countTrailingOnes();
2080 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
2081 KnownZero2.countLeadingOnes(),
2082 BitWidth) - BitWidth;
2083
2084 TrailZ = std::min(TrailZ, BitWidth);
2085 LeadZ = std::min(LeadZ, BitWidth);
2086 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
2087 APInt::getHighBitsSet(BitWidth, LeadZ);
2088 break;
2089 }
2090 case ISD::UDIV: {
2091 // For the purposes of computing leading zeros we can conservatively
2092 // treat a udiv as a logical right shift by the power of 2 known to
2093 // be less than the denominator.
2094 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2095 unsigned LeadZ = KnownZero2.countLeadingOnes();
2096
2097 KnownOne2.clearAllBits();
2098 KnownZero2.clearAllBits();
2099 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2100 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
2101 if (RHSUnknownLeadingOnes != BitWidth)
2102 LeadZ = std::min(BitWidth,
2103 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
2104
2105 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
2106 break;
2107 }
2108 case ISD::SELECT:
2109 computeKnownBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
2110 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2111
2112 // Only known if known in both the LHS and RHS.
2113 KnownOne &= KnownOne2;
2114 KnownZero &= KnownZero2;
2115 break;
2116 case ISD::SELECT_CC:
2117 computeKnownBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
2118 computeKnownBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
2119
2120 // Only known if known in both the LHS and RHS.
2121 KnownOne &= KnownOne2;
2122 KnownZero &= KnownZero2;
2123 break;
2124 case ISD::SADDO:
2125 case ISD::UADDO:
2126 case ISD::SSUBO:
2127 case ISD::USUBO:
2128 case ISD::SMULO:
2129 case ISD::UMULO:
2130 if (Op.getResNo() != 1)
2131 break;
2132 // The boolean result conforms to getBooleanContents.
2133 // If we know the result of a setcc has the top bits zero, use this info.
2134 // We know that we have an integer-based boolean since these operations
2135 // are only available for integer.
2136 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2137 TargetLowering::ZeroOrOneBooleanContent &&
2138 BitWidth > 1)
2139 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2140 break;
2141 case ISD::SETCC:
2142 // If we know the result of a setcc has the top bits zero, use this info.
2143 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2144 TargetLowering::ZeroOrOneBooleanContent &&
2145 BitWidth > 1)
2146 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2147 break;
2148 case ISD::SHL:
2149 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
2150 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2151 unsigned ShAmt = SA->getZExtValue();
2152
2153 // If the shift count is an invalid immediate, don't do anything.
2154 if (ShAmt >= BitWidth)
2155 break;
2156
2157 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2158 KnownZero <<= ShAmt;
2159 KnownOne <<= ShAmt;
2160 // low bits known zero.
2161 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
2162 }
2163 break;
2164 case ISD::SRL:
2165 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
2166 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2167 unsigned ShAmt = SA->getZExtValue();
2168
2169 // If the shift count is an invalid immediate, don't do anything.
2170 if (ShAmt >= BitWidth)
2171 break;
2172
2173 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2174 KnownZero = KnownZero.lshr(ShAmt);
2175 KnownOne = KnownOne.lshr(ShAmt);
2176
2177 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
2178 KnownZero |= HighBits; // High bits known zero.
2179 }
2180 break;
2181 case ISD::SRA:
2182 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2183 unsigned ShAmt = SA->getZExtValue();
2184
2185 // If the shift count is an invalid immediate, don't do anything.
2186 if (ShAmt >= BitWidth)
2187 break;
2188
2189 // If any of the demanded bits are produced by the sign extension, we also
2190 // demand the input sign bit.
2191 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
2192
2193 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2194 KnownZero = KnownZero.lshr(ShAmt);
2195 KnownOne = KnownOne.lshr(ShAmt);
2196
2197 // Handle the sign bits.
2198 APInt SignBit = APInt::getSignBit(BitWidth);
2199 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
2200
2201 if (KnownZero.intersects(SignBit)) {
2202 KnownZero |= HighBits; // New bits are known zero.
2203 } else if (KnownOne.intersects(SignBit)) {
2204 KnownOne |= HighBits; // New bits are known one.
2205 }
2206 }
2207 break;
2208 case ISD::SIGN_EXTEND_INREG: {
2209 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2210 unsigned EBits = EVT.getScalarType().getSizeInBits();
2211
2212 // Sign extension. Compute the demanded bits in the result that are not
2213 // present in the input.
2214 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
2215
2216 APInt InSignBit = APInt::getSignBit(EBits);
2217 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
2218
2219 // If the sign extended bits are demanded, we know that the sign
2220 // bit is demanded.
2221 InSignBit = InSignBit.zext(BitWidth);
2222 if (NewBits.getBoolValue())
2223 InputDemandedBits |= InSignBit;
2224
2225 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2226 KnownOne &= InputDemandedBits;
2227 KnownZero &= InputDemandedBits;
2228
2229 // If the sign bit of the input is known set or clear, then we know the
2230 // top bits of the result.
2231 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
2232 KnownZero |= NewBits;
2233 KnownOne &= ~NewBits;
2234 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
2235 KnownOne |= NewBits;
2236 KnownZero &= ~NewBits;
2237 } else { // Input sign bit unknown
2238 KnownZero &= ~NewBits;
2239 KnownOne &= ~NewBits;
2240 }
2241 break;
2242 }
2243 case ISD::CTTZ:
2244 case ISD::CTTZ_ZERO_UNDEF:
2245 case ISD::CTLZ:
2246 case ISD::CTLZ_ZERO_UNDEF:
2247 case ISD::CTPOP: {
2248 unsigned LowBits = Log2_32(BitWidth)+1;
2249 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
2250 KnownOne.clearAllBits();
2251 break;
2252 }
2253 case ISD::LOAD: {
2254 LoadSDNode *LD = cast<LoadSDNode>(Op);
2255 // If this is a ZEXTLoad and we are looking at the loaded value.
2256 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
2257 EVT VT = LD->getMemoryVT();
2258 unsigned MemBits = VT.getScalarType().getSizeInBits();
2259 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
2260 } else if (const MDNode *Ranges = LD->getRanges()) {
2261 if (LD->getExtensionType() == ISD::NON_EXTLOAD)
2262 computeKnownBitsFromRangeMetadata(*Ranges, KnownZero, KnownOne);
2263 }
2264 break;
2265 }
2266 case ISD::ZERO_EXTEND: {
2267 EVT InVT = Op.getOperand(0).getValueType();
2268 unsigned InBits = InVT.getScalarType().getSizeInBits();
2269 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
2270 KnownZero = KnownZero.trunc(InBits);
2271 KnownOne = KnownOne.trunc(InBits);
2272 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2273 KnownZero = KnownZero.zext(BitWidth);
2274 KnownOne = KnownOne.zext(BitWidth);
2275 KnownZero |= NewBits;
2276 break;
2277 }
2278 case ISD::SIGN_EXTEND: {
2279 EVT InVT = Op.getOperand(0).getValueType();
2280 unsigned InBits = InVT.getScalarType().getSizeInBits();
2281 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
2282
2283 KnownZero = KnownZero.trunc(InBits);
2284 KnownOne = KnownOne.trunc(InBits);
2285 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2286
2287 // Note if the sign bit is known to be zero or one.
2288 bool SignBitKnownZero = KnownZero.isNegative();
2289 bool SignBitKnownOne = KnownOne.isNegative();
2290
2291 KnownZero = KnownZero.zext(BitWidth);
2292 KnownOne = KnownOne.zext(BitWidth);
2293
2294 // If the sign bit is known zero or one, the top bits match.
2295 if (SignBitKnownZero)
2296 KnownZero |= NewBits;
2297 else if (SignBitKnownOne)
2298 KnownOne |= NewBits;
2299 break;
2300 }
2301 case ISD::ANY_EXTEND: {
2302 EVT InVT = Op.getOperand(0).getValueType();
2303 unsigned InBits = InVT.getScalarType().getSizeInBits();
2304 KnownZero = KnownZero.trunc(InBits);
2305 KnownOne = KnownOne.trunc(InBits);
2306 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2307 KnownZero = KnownZero.zext(BitWidth);
2308 KnownOne = KnownOne.zext(BitWidth);
2309 break;
2310 }
2311 case ISD::TRUNCATE: {
2312 EVT InVT = Op.getOperand(0).getValueType();
2313 unsigned InBits = InVT.getScalarType().getSizeInBits();
2314 KnownZero = KnownZero.zext(InBits);
2315 KnownOne = KnownOne.zext(InBits);
2316 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2317 KnownZero = KnownZero.trunc(BitWidth);
2318 KnownOne = KnownOne.trunc(BitWidth);
2319 break;
2320 }
2321 case ISD::AssertZext: {
2322 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2323 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
2324 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2325 KnownZero |= (~InMask);
2326 KnownOne &= (~KnownZero);
2327 break;
2328 }
2329 case ISD::FGETSIGN:
2330 // All bits are zero except the low bit.
2331 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
2332 break;
2333
2334 case ISD::SUB: {
2335 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2336 // We know that the top bits of C-X are clear if X contains less bits
2337 // than C (i.e. no wrap-around can happen). For example, 20-X is
2338 // positive if we can prove that X is >= 0 and < 16.
2339 if (CLHS->getAPIntValue().isNonNegative()) {
2340 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2341 // NLZ can't be BitWidth with no sign bit
2342 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
2343 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2344
2345 // If all of the MaskV bits are known to be zero, then we know the
2346 // output top bits are zero, because we now know that the output is
2347 // from [0-C].
2348 if ((KnownZero2 & MaskV) == MaskV) {
2349 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2350 // Top bits known zero.
2351 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
2352 }
2353 }
2354 }
2355 }
2356 // fall through
2357 case ISD::ADD:
2358 case ISD::ADDE: {
2359 // Output known-0 bits are known if clear or set in both the low clear bits
2360 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2361 // low 3 bits clear.
2362 // Output known-0 bits are also known if the top bits of each input are
2363 // known to be clear. For example, if one input has the top 10 bits clear
2364 // and the other has the top 8 bits clear, we know the top 7 bits of the
2365 // output must be clear.
2366 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2367 unsigned KnownZeroHigh = KnownZero2.countLeadingOnes();
2368 unsigned KnownZeroLow = KnownZero2.countTrailingOnes();
2369
2370 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2371 KnownZeroHigh = std::min(KnownZeroHigh,
2372 KnownZero2.countLeadingOnes());
2373 KnownZeroLow = std::min(KnownZeroLow,
2374 KnownZero2.countTrailingOnes());
2375
2376 if (Op.getOpcode() == ISD::ADD) {
2377 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroLow);
2378 if (KnownZeroHigh > 1)
2379 KnownZero |= APInt::getHighBitsSet(BitWidth, KnownZeroHigh - 1);
2380 break;
2381 }
2382
2383 // With ADDE, a carry bit may be added in, so we can only use this
2384 // information if we know (at least) that the low two bits are clear. We
2385 // then return to the caller that the low bit is unknown but that other bits
2386 // are known zero.
2387 if (KnownZeroLow >= 2) // ADDE
2388 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroLow);
2389 break;
2390 }
2391 case ISD::SREM:
2392 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2393 const APInt &RA = Rem->getAPIntValue().abs();
2394 if (RA.isPowerOf2()) {
2395 APInt LowBits = RA - 1;
2396 computeKnownBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
2397
2398 // The low bits of the first operand are unchanged by the srem.
2399 KnownZero = KnownZero2 & LowBits;
2400 KnownOne = KnownOne2 & LowBits;
2401
2402 // If the first operand is non-negative or has all low bits zero, then
2403 // the upper bits are all zero.
2404 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2405 KnownZero |= ~LowBits;
2406
2407 // If the first operand is negative and not all low bits are zero, then
2408 // the upper bits are all one.
2409 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2410 KnownOne |= ~LowBits;
2411 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2412 }
2413 }
2414 break;
2415 case ISD::UREM: {
2416 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2417 const APInt &RA = Rem->getAPIntValue();
2418 if (RA.isPowerOf2()) {
2419 APInt LowBits = (RA - 1);
2420 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth + 1);
2421
2422 // The upper bits are all zero, the lower ones are unchanged.
2423 KnownZero = KnownZero2 | ~LowBits;
2424 KnownOne = KnownOne2 & LowBits;
2425 break;
2426 }
2427 }
2428
2429 // Since the result is less than or equal to either operand, any leading
2430 // zero bits in either operand must also exist in the result.
2431 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2432 computeKnownBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
2433
2434 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2435 KnownZero2.countLeadingOnes());
2436 KnownOne.clearAllBits();
2437 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
2438 break;
2439 }
2440 case ISD::EXTRACT_ELEMENT: {
2441 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2442 const unsigned Index =
2443 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2444 const unsigned BitWidth = Op.getValueType().getSizeInBits();
2445
2446 // Remove low part of known bits mask
2447 KnownZero = KnownZero.getHiBits(KnownZero.getBitWidth() - Index * BitWidth);
2448 KnownOne = KnownOne.getHiBits(KnownOne.getBitWidth() - Index * BitWidth);
2449
2450 // Remove high part of known bit mask
2451 KnownZero = KnownZero.trunc(BitWidth);
2452 KnownOne = KnownOne.trunc(BitWidth);
2453 break;
2454 }
2455 case ISD::BSWAP: {
2456 computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
2457 KnownZero = KnownZero2.byteSwap();
2458 KnownOne = KnownOne2.byteSwap();
2459 break;
2460 }
2461 case ISD::SMIN:
2462 case ISD::SMAX:
2463 case ISD::UMIN:
2464 case ISD::UMAX: {
2465 APInt Op0Zero, Op0One;
2466 APInt Op1Zero, Op1One;
2467 computeKnownBits(Op.getOperand(0), Op0Zero, Op0One, Depth);
2468 computeKnownBits(Op.getOperand(1), Op1Zero, Op1One, Depth);
2469
2470 KnownZero = Op0Zero & Op1Zero;
2471 KnownOne = Op0One & Op1One;
2472 break;
2473 }
2474 case ISD::FrameIndex:
2475 case ISD::TargetFrameIndex:
2476 if (unsigned Align = InferPtrAlignment(Op)) {
2477 // The low bits are known zero if the pointer is aligned.
2478 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2479 break;
2480 }
2481 break;
2482
2483 default:
2484 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2485 break;
2486 // Fallthrough
2487 case ISD::INTRINSIC_WO_CHAIN:
2488 case ISD::INTRINSIC_W_CHAIN:
2489 case ISD::INTRINSIC_VOID:
2490 // Allow the target to implement this method for its nodes.
2491 TLI->computeKnownBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
2492 break;
2493 }
2494
2495 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
2496 }
2497
isKnownToBeAPowerOfTwo(SDValue Val) const2498 bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val) const {
2499 // A left-shift of a constant one will have exactly one bit set because
2500 // shifting the bit off the end is undefined.
2501 if (Val.getOpcode() == ISD::SHL) {
2502 auto *C = dyn_cast<ConstantSDNode>(Val.getOperand(0));
2503 if (C && C->getAPIntValue() == 1)
2504 return true;
2505 }
2506
2507 // Similarly, a logical right-shift of a constant sign-bit will have exactly
2508 // one bit set.
2509 if (Val.getOpcode() == ISD::SRL) {
2510 auto *C = dyn_cast<ConstantSDNode>(Val.getOperand(0));
2511 if (C && C->getAPIntValue().isSignBit())
2512 return true;
2513 }
2514
2515 // More could be done here, though the above checks are enough
2516 // to handle some common cases.
2517
2518 // Fall back to computeKnownBits to catch other known cases.
2519 EVT OpVT = Val.getValueType();
2520 unsigned BitWidth = OpVT.getScalarType().getSizeInBits();
2521 APInt KnownZero, KnownOne;
2522 computeKnownBits(Val, KnownZero, KnownOne);
2523 return (KnownZero.countPopulation() == BitWidth - 1) &&
2524 (KnownOne.countPopulation() == 1);
2525 }
2526
ComputeNumSignBits(SDValue Op,unsigned Depth) const2527 unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const {
2528 EVT VT = Op.getValueType();
2529 assert(VT.isInteger() && "Invalid VT!");
2530 unsigned VTBits = VT.getScalarType().getSizeInBits();
2531 unsigned Tmp, Tmp2;
2532 unsigned FirstAnswer = 1;
2533
2534 if (Depth == 6)
2535 return 1; // Limit search depth.
2536
2537 switch (Op.getOpcode()) {
2538 default: break;
2539 case ISD::AssertSext:
2540 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
2541 return VTBits-Tmp+1;
2542 case ISD::AssertZext:
2543 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
2544 return VTBits-Tmp;
2545
2546 case ISD::Constant: {
2547 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
2548 return Val.getNumSignBits();
2549 }
2550
2551 case ISD::SIGN_EXTEND:
2552 Tmp =
2553 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
2554 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
2555
2556 case ISD::SIGN_EXTEND_INREG:
2557 // Max of the input and what this extends.
2558 Tmp =
2559 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
2560 Tmp = VTBits-Tmp+1;
2561
2562 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2563 return std::max(Tmp, Tmp2);
2564
2565 case ISD::SRA:
2566 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2567 // SRA X, C -> adds C sign bits.
2568 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2569 Tmp += C->getZExtValue();
2570 if (Tmp > VTBits) Tmp = VTBits;
2571 }
2572 return Tmp;
2573 case ISD::SHL:
2574 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2575 // shl destroys sign bits.
2576 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2577 if (C->getZExtValue() >= VTBits || // Bad shift.
2578 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2579 return Tmp - C->getZExtValue();
2580 }
2581 break;
2582 case ISD::AND:
2583 case ISD::OR:
2584 case ISD::XOR: // NOT is handled here.
2585 // Logical binary ops preserve the number of sign bits at the worst.
2586 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2587 if (Tmp != 1) {
2588 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2589 FirstAnswer = std::min(Tmp, Tmp2);
2590 // We computed what we know about the sign bits as our first
2591 // answer. Now proceed to the generic code that uses
2592 // computeKnownBits, and pick whichever answer is better.
2593 }
2594 break;
2595
2596 case ISD::SELECT:
2597 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2598 if (Tmp == 1) return 1; // Early out.
2599 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
2600 return std::min(Tmp, Tmp2);
2601 case ISD::SELECT_CC:
2602 Tmp = ComputeNumSignBits(Op.getOperand(2), Depth+1);
2603 if (Tmp == 1) return 1; // Early out.
2604 Tmp2 = ComputeNumSignBits(Op.getOperand(3), Depth+1);
2605 return std::min(Tmp, Tmp2);
2606 case ISD::SMIN:
2607 case ISD::SMAX:
2608 case ISD::UMIN:
2609 case ISD::UMAX:
2610 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
2611 if (Tmp == 1)
2612 return 1; // Early out.
2613 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
2614 return std::min(Tmp, Tmp2);
2615 case ISD::SADDO:
2616 case ISD::UADDO:
2617 case ISD::SSUBO:
2618 case ISD::USUBO:
2619 case ISD::SMULO:
2620 case ISD::UMULO:
2621 if (Op.getResNo() != 1)
2622 break;
2623 // The boolean result conforms to getBooleanContents. Fall through.
2624 // If setcc returns 0/-1, all bits are sign bits.
2625 // We know that we have an integer-based boolean since these operations
2626 // are only available for integer.
2627 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
2628 TargetLowering::ZeroOrNegativeOneBooleanContent)
2629 return VTBits;
2630 break;
2631 case ISD::SETCC:
2632 // If setcc returns 0/-1, all bits are sign bits.
2633 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
2634 TargetLowering::ZeroOrNegativeOneBooleanContent)
2635 return VTBits;
2636 break;
2637 case ISD::ROTL:
2638 case ISD::ROTR:
2639 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2640 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
2641
2642 // Handle rotate right by N like a rotate left by 32-N.
2643 if (Op.getOpcode() == ISD::ROTR)
2644 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2645
2646 // If we aren't rotating out all of the known-in sign bits, return the
2647 // number that are left. This handles rotl(sext(x), 1) for example.
2648 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2649 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2650 }
2651 break;
2652 case ISD::ADD:
2653 // Add can have at most one carry bit. Thus we know that the output
2654 // is, at worst, one more bit than the inputs.
2655 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2656 if (Tmp == 1) return 1; // Early out.
2657
2658 // Special case decrementing a value (ADD X, -1):
2659 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2660 if (CRHS->isAllOnesValue()) {
2661 APInt KnownZero, KnownOne;
2662 computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2663
2664 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2665 // sign bits set.
2666 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
2667 return VTBits;
2668
2669 // If we are subtracting one from a positive number, there is no carry
2670 // out of the result.
2671 if (KnownZero.isNegative())
2672 return Tmp;
2673 }
2674
2675 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2676 if (Tmp2 == 1) return 1;
2677 return std::min(Tmp, Tmp2)-1;
2678
2679 case ISD::SUB:
2680 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2681 if (Tmp2 == 1) return 1;
2682
2683 // Handle NEG.
2684 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
2685 if (CLHS->isNullValue()) {
2686 APInt KnownZero, KnownOne;
2687 computeKnownBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
2688 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2689 // sign bits set.
2690 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
2691 return VTBits;
2692
2693 // If the input is known to be positive (the sign bit is known clear),
2694 // the output of the NEG has the same number of sign bits as the input.
2695 if (KnownZero.isNegative())
2696 return Tmp2;
2697
2698 // Otherwise, we treat this like a SUB.
2699 }
2700
2701 // Sub can have at most one carry bit. Thus we know that the output
2702 // is, at worst, one more bit than the inputs.
2703 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2704 if (Tmp == 1) return 1; // Early out.
2705 return std::min(Tmp, Tmp2)-1;
2706 case ISD::TRUNCATE:
2707 // FIXME: it's tricky to do anything useful for this, but it is an important
2708 // case for targets like X86.
2709 break;
2710 case ISD::EXTRACT_ELEMENT: {
2711 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2712 const int BitWidth = Op.getValueType().getSizeInBits();
2713 const int Items =
2714 Op.getOperand(0).getValueType().getSizeInBits() / BitWidth;
2715
2716 // Get reverse index (starting from 1), Op1 value indexes elements from
2717 // little end. Sign starts at big end.
2718 const int rIndex = Items - 1 -
2719 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2720
2721 // If the sign portion ends in our element the subtraction gives correct
2722 // result. Otherwise it gives either negative or > bitwidth result
2723 return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
2724 }
2725 }
2726
2727 // If we are looking at the loaded value of the SDNode.
2728 if (Op.getResNo() == 0) {
2729 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2730 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2731 unsigned ExtType = LD->getExtensionType();
2732 switch (ExtType) {
2733 default: break;
2734 case ISD::SEXTLOAD: // '17' bits known
2735 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2736 return VTBits-Tmp+1;
2737 case ISD::ZEXTLOAD: // '16' bits known
2738 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2739 return VTBits-Tmp;
2740 }
2741 }
2742 }
2743
2744 // Allow the target to implement this method for its nodes.
2745 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
2746 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
2747 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2748 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2749 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, *this, Depth);
2750 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
2751 }
2752
2753 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2754 // use this information.
2755 APInt KnownZero, KnownOne;
2756 computeKnownBits(Op, KnownZero, KnownOne, Depth);
2757
2758 APInt Mask;
2759 if (KnownZero.isNegative()) { // sign bit is 0
2760 Mask = KnownZero;
2761 } else if (KnownOne.isNegative()) { // sign bit is 1;
2762 Mask = KnownOne;
2763 } else {
2764 // Nothing known.
2765 return FirstAnswer;
2766 }
2767
2768 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2769 // the number of identical bits in the top of the input value.
2770 Mask = ~Mask;
2771 Mask <<= Mask.getBitWidth()-VTBits;
2772 // Return # leading zeros. We use 'min' here in case Val was zero before
2773 // shifting. We don't want to return '64' as for an i32 "0".
2774 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
2775 }
2776
isBaseWithConstantOffset(SDValue Op) const2777 bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2778 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2779 !isa<ConstantSDNode>(Op.getOperand(1)))
2780 return false;
2781
2782 if (Op.getOpcode() == ISD::OR &&
2783 !MaskedValueIsZero(Op.getOperand(0),
2784 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2785 return false;
2786
2787 return true;
2788 }
2789
isKnownNeverNaN(SDValue Op) const2790 bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2791 // If we're told that NaNs won't happen, assume they won't.
2792 if (getTarget().Options.NoNaNsFPMath)
2793 return true;
2794
2795 // If the value is a constant, we can obviously see if it is a NaN or not.
2796 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2797 return !C->getValueAPF().isNaN();
2798
2799 // TODO: Recognize more cases here.
2800
2801 return false;
2802 }
2803
isKnownNeverZero(SDValue Op) const2804 bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2805 // If the value is a constant, we can obviously see if it is a zero or not.
2806 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2807 return !C->isZero();
2808
2809 // TODO: Recognize more cases here.
2810 switch (Op.getOpcode()) {
2811 default: break;
2812 case ISD::OR:
2813 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2814 return !C->isNullValue();
2815 break;
2816 }
2817
2818 return false;
2819 }
2820
isEqualTo(SDValue A,SDValue B) const2821 bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2822 // Check the obvious case.
2823 if (A == B) return true;
2824
2825 // For for negative and positive zero.
2826 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2827 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2828 if (CA->isZero() && CB->isZero()) return true;
2829
2830 // Otherwise they may not be equal.
2831 return false;
2832 }
2833
haveNoCommonBitsSet(SDValue A,SDValue B) const2834 bool SelectionDAG::haveNoCommonBitsSet(SDValue A, SDValue B) const {
2835 assert(A.getValueType() == B.getValueType() &&
2836 "Values must have the same type");
2837 APInt AZero, AOne;
2838 APInt BZero, BOne;
2839 computeKnownBits(A, AZero, AOne);
2840 computeKnownBits(B, BZero, BOne);
2841 return (AZero | BZero).isAllOnesValue();
2842 }
2843
FoldCONCAT_VECTORS(const SDLoc & DL,EVT VT,ArrayRef<SDValue> Ops,llvm::SelectionDAG & DAG)2844 static SDValue FoldCONCAT_VECTORS(const SDLoc &DL, EVT VT,
2845 ArrayRef<SDValue> Ops,
2846 llvm::SelectionDAG &DAG) {
2847 if (Ops.size() == 1)
2848 return Ops[0];
2849
2850 // Concat of UNDEFs is UNDEF.
2851 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
2852 return DAG.getUNDEF(VT);
2853
2854 // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
2855 // simplified to one big BUILD_VECTOR.
2856 // FIXME: Add support for SCALAR_TO_VECTOR as well.
2857 EVT SVT = VT.getScalarType();
2858 SmallVector<SDValue, 16> Elts;
2859 for (SDValue Op : Ops) {
2860 EVT OpVT = Op.getValueType();
2861 if (Op.isUndef())
2862 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
2863 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
2864 Elts.append(Op->op_begin(), Op->op_end());
2865 else
2866 return SDValue();
2867 }
2868
2869 // BUILD_VECTOR requires all inputs to be of the same type, find the
2870 // maximum type and extend them all.
2871 for (SDValue Op : Elts)
2872 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
2873
2874 if (SVT.bitsGT(VT.getScalarType()))
2875 for (SDValue &Op : Elts)
2876 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
2877 ? DAG.getZExtOrTrunc(Op, DL, SVT)
2878 : DAG.getSExtOrTrunc(Op, DL, SVT);
2879
2880 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Elts);
2881 }
2882
2883 /// Gets or creates the specified node.
getNode(unsigned Opcode,const SDLoc & DL,EVT VT)2884 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
2885 FoldingSetNodeID ID;
2886 AddNodeIDNode(ID, Opcode, getVTList(VT), None);
2887 void *IP = nullptr;
2888 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
2889 return SDValue(E, 0);
2890
2891 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(),
2892 getVTList(VT));
2893 CSEMap.InsertNode(N, IP);
2894
2895 InsertNode(N);
2896 return SDValue(N, 0);
2897 }
2898
getNode(unsigned Opcode,const SDLoc & DL,EVT VT,SDValue Operand)2899 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
2900 SDValue Operand) {
2901 // Constant fold unary operations with an integer constant operand. Even
2902 // opaque constant will be folded, because the folding of unary operations
2903 // doesn't create new constants with different values. Nevertheless, the
2904 // opaque flag is preserved during folding to prevent future folding with
2905 // other constants.
2906 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand)) {
2907 const APInt &Val = C->getAPIntValue();
2908 switch (Opcode) {
2909 default: break;
2910 case ISD::SIGN_EXTEND:
2911 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
2912 C->isTargetOpcode(), C->isOpaque());
2913 case ISD::ANY_EXTEND:
2914 case ISD::ZERO_EXTEND:
2915 case ISD::TRUNCATE:
2916 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
2917 C->isTargetOpcode(), C->isOpaque());
2918 case ISD::UINT_TO_FP:
2919 case ISD::SINT_TO_FP: {
2920 APFloat apf(EVTToAPFloatSemantics(VT),
2921 APInt::getNullValue(VT.getSizeInBits()));
2922 (void)apf.convertFromAPInt(Val,
2923 Opcode==ISD::SINT_TO_FP,
2924 APFloat::rmNearestTiesToEven);
2925 return getConstantFP(apf, DL, VT);
2926 }
2927 case ISD::BITCAST:
2928 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
2929 return getConstantFP(APFloat(APFloat::IEEEhalf, Val), DL, VT);
2930 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
2931 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), DL, VT);
2932 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
2933 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), DL, VT);
2934 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
2935 return getConstantFP(APFloat(APFloat::IEEEquad, Val), DL, VT);
2936 break;
2937 case ISD::BSWAP:
2938 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
2939 C->isOpaque());
2940 case ISD::CTPOP:
2941 return getConstant(Val.countPopulation(), DL, VT, C->isTargetOpcode(),
2942 C->isOpaque());
2943 case ISD::CTLZ:
2944 case ISD::CTLZ_ZERO_UNDEF:
2945 return getConstant(Val.countLeadingZeros(), DL, VT, C->isTargetOpcode(),
2946 C->isOpaque());
2947 case ISD::CTTZ:
2948 case ISD::CTTZ_ZERO_UNDEF:
2949 return getConstant(Val.countTrailingZeros(), DL, VT, C->isTargetOpcode(),
2950 C->isOpaque());
2951 }
2952 }
2953
2954 // Constant fold unary operations with a floating point constant operand.
2955 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand)) {
2956 APFloat V = C->getValueAPF(); // make copy
2957 switch (Opcode) {
2958 case ISD::FNEG:
2959 V.changeSign();
2960 return getConstantFP(V, DL, VT);
2961 case ISD::FABS:
2962 V.clearSign();
2963 return getConstantFP(V, DL, VT);
2964 case ISD::FCEIL: {
2965 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2966 if (fs == APFloat::opOK || fs == APFloat::opInexact)
2967 return getConstantFP(V, DL, VT);
2968 break;
2969 }
2970 case ISD::FTRUNC: {
2971 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2972 if (fs == APFloat::opOK || fs == APFloat::opInexact)
2973 return getConstantFP(V, DL, VT);
2974 break;
2975 }
2976 case ISD::FFLOOR: {
2977 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2978 if (fs == APFloat::opOK || fs == APFloat::opInexact)
2979 return getConstantFP(V, DL, VT);
2980 break;
2981 }
2982 case ISD::FP_EXTEND: {
2983 bool ignored;
2984 // This can return overflow, underflow, or inexact; we don't care.
2985 // FIXME need to be more flexible about rounding mode.
2986 (void)V.convert(EVTToAPFloatSemantics(VT),
2987 APFloat::rmNearestTiesToEven, &ignored);
2988 return getConstantFP(V, DL, VT);
2989 }
2990 case ISD::FP_TO_SINT:
2991 case ISD::FP_TO_UINT: {
2992 integerPart x[2];
2993 bool ignored;
2994 static_assert(integerPartWidth >= 64, "APFloat parts too small!");
2995 // FIXME need to be more flexible about rounding mode.
2996 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2997 Opcode==ISD::FP_TO_SINT,
2998 APFloat::rmTowardZero, &ignored);
2999 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
3000 break;
3001 APInt api(VT.getSizeInBits(), x);
3002 return getConstant(api, DL, VT);
3003 }
3004 case ISD::BITCAST:
3005 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
3006 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
3007 else if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
3008 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL, VT);
3009 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
3010 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
3011 break;
3012 }
3013 }
3014
3015 // Constant fold unary operations with a vector integer or float operand.
3016 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Operand)) {
3017 if (BV->isConstant()) {
3018 switch (Opcode) {
3019 default:
3020 // FIXME: Entirely reasonable to perform folding of other unary
3021 // operations here as the need arises.
3022 break;
3023 case ISD::FNEG:
3024 case ISD::FABS:
3025 case ISD::FCEIL:
3026 case ISD::FTRUNC:
3027 case ISD::FFLOOR:
3028 case ISD::FP_EXTEND:
3029 case ISD::FP_TO_SINT:
3030 case ISD::FP_TO_UINT:
3031 case ISD::TRUNCATE:
3032 case ISD::UINT_TO_FP:
3033 case ISD::SINT_TO_FP:
3034 case ISD::BSWAP:
3035 case ISD::CTLZ:
3036 case ISD::CTLZ_ZERO_UNDEF:
3037 case ISD::CTTZ:
3038 case ISD::CTTZ_ZERO_UNDEF:
3039 case ISD::CTPOP: {
3040 SDValue Ops = { Operand };
3041 if (SDValue Fold = FoldConstantVectorArithmetic(Opcode, DL, VT, Ops))
3042 return Fold;
3043 }
3044 }
3045 }
3046 }
3047
3048 unsigned OpOpcode = Operand.getNode()->getOpcode();
3049 switch (Opcode) {
3050 case ISD::TokenFactor:
3051 case ISD::MERGE_VALUES:
3052 case ISD::CONCAT_VECTORS:
3053 return Operand; // Factor, merge or concat of one node? No need.
3054 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
3055 case ISD::FP_EXTEND:
3056 assert(VT.isFloatingPoint() &&
3057 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
3058 if (Operand.getValueType() == VT) return Operand; // noop conversion.
3059 assert((!VT.isVector() ||
3060 VT.getVectorNumElements() ==
3061 Operand.getValueType().getVectorNumElements()) &&
3062 "Vector element count mismatch!");
3063 assert(Operand.getValueType().bitsLT(VT) &&
3064 "Invalid fpext node, dst < src!");
3065 if (Operand.isUndef())
3066 return getUNDEF(VT);
3067 break;
3068 case ISD::SIGN_EXTEND:
3069 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
3070 "Invalid SIGN_EXTEND!");
3071 if (Operand.getValueType() == VT) return Operand; // noop extension
3072 assert((!VT.isVector() ||
3073 VT.getVectorNumElements() ==
3074 Operand.getValueType().getVectorNumElements()) &&
3075 "Vector element count mismatch!");
3076 assert(Operand.getValueType().bitsLT(VT) &&
3077 "Invalid sext node, dst < src!");
3078 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
3079 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
3080 else if (OpOpcode == ISD::UNDEF)
3081 // sext(undef) = 0, because the top bits will all be the same.
3082 return getConstant(0, DL, VT);
3083 break;
3084 case ISD::ZERO_EXTEND:
3085 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
3086 "Invalid ZERO_EXTEND!");
3087 if (Operand.getValueType() == VT) return Operand; // noop extension
3088 assert((!VT.isVector() ||
3089 VT.getVectorNumElements() ==
3090 Operand.getValueType().getVectorNumElements()) &&
3091 "Vector element count mismatch!");
3092 assert(Operand.getValueType().bitsLT(VT) &&
3093 "Invalid zext node, dst < src!");
3094 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
3095 return getNode(ISD::ZERO_EXTEND, DL, VT,
3096 Operand.getNode()->getOperand(0));
3097 else if (OpOpcode == ISD::UNDEF)
3098 // zext(undef) = 0, because the top bits will be zero.
3099 return getConstant(0, DL, VT);
3100 break;
3101 case ISD::ANY_EXTEND:
3102 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
3103 "Invalid ANY_EXTEND!");
3104 if (Operand.getValueType() == VT) return Operand; // noop extension
3105 assert((!VT.isVector() ||
3106 VT.getVectorNumElements() ==
3107 Operand.getValueType().getVectorNumElements()) &&
3108 "Vector element count mismatch!");
3109 assert(Operand.getValueType().bitsLT(VT) &&
3110 "Invalid anyext node, dst < src!");
3111
3112 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
3113 OpOpcode == ISD::ANY_EXTEND)
3114 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
3115 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
3116 else if (OpOpcode == ISD::UNDEF)
3117 return getUNDEF(VT);
3118
3119 // (ext (trunx x)) -> x
3120 if (OpOpcode == ISD::TRUNCATE) {
3121 SDValue OpOp = Operand.getNode()->getOperand(0);
3122 if (OpOp.getValueType() == VT)
3123 return OpOp;
3124 }
3125 break;
3126 case ISD::TRUNCATE:
3127 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
3128 "Invalid TRUNCATE!");
3129 if (Operand.getValueType() == VT) return Operand; // noop truncate
3130 assert((!VT.isVector() ||
3131 VT.getVectorNumElements() ==
3132 Operand.getValueType().getVectorNumElements()) &&
3133 "Vector element count mismatch!");
3134 assert(Operand.getValueType().bitsGT(VT) &&
3135 "Invalid truncate node, src < dst!");
3136 if (OpOpcode == ISD::TRUNCATE)
3137 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
3138 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
3139 OpOpcode == ISD::ANY_EXTEND) {
3140 // If the source is smaller than the dest, we still need an extend.
3141 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
3142 .bitsLT(VT.getScalarType()))
3143 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
3144 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
3145 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
3146 return Operand.getNode()->getOperand(0);
3147 }
3148 if (OpOpcode == ISD::UNDEF)
3149 return getUNDEF(VT);
3150 break;
3151 case ISD::BSWAP:
3152 assert(VT.isInteger() && VT == Operand.getValueType() &&
3153 "Invalid BSWAP!");
3154 assert((VT.getScalarSizeInBits() % 16 == 0) &&
3155 "BSWAP types must be a multiple of 16 bits!");
3156 if (OpOpcode == ISD::UNDEF)
3157 return getUNDEF(VT);
3158 break;
3159 case ISD::BITREVERSE:
3160 assert(VT.isInteger() && VT == Operand.getValueType() &&
3161 "Invalid BITREVERSE!");
3162 if (OpOpcode == ISD::UNDEF)
3163 return getUNDEF(VT);
3164 break;
3165 case ISD::BITCAST:
3166 // Basic sanity checking.
3167 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
3168 && "Cannot BITCAST between types of different sizes!");
3169 if (VT == Operand.getValueType()) return Operand; // noop conversion.
3170 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
3171 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
3172 if (OpOpcode == ISD::UNDEF)
3173 return getUNDEF(VT);
3174 break;
3175 case ISD::SCALAR_TO_VECTOR:
3176 assert(VT.isVector() && !Operand.getValueType().isVector() &&
3177 (VT.getVectorElementType() == Operand.getValueType() ||
3178 (VT.getVectorElementType().isInteger() &&
3179 Operand.getValueType().isInteger() &&
3180 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
3181 "Illegal SCALAR_TO_VECTOR node!");
3182 if (OpOpcode == ISD::UNDEF)
3183 return getUNDEF(VT);
3184 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
3185 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
3186 isa<ConstantSDNode>(Operand.getOperand(1)) &&
3187 Operand.getConstantOperandVal(1) == 0 &&
3188 Operand.getOperand(0).getValueType() == VT)
3189 return Operand.getOperand(0);
3190 break;
3191 case ISD::FNEG:
3192 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
3193 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
3194 // FIXME: FNEG has no fast-math-flags to propagate; use the FSUB's flags?
3195 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
3196 Operand.getNode()->getOperand(0),
3197 &cast<BinaryWithFlagsSDNode>(Operand.getNode())->Flags);
3198 if (OpOpcode == ISD::FNEG) // --X -> X
3199 return Operand.getNode()->getOperand(0);
3200 break;
3201 case ISD::FABS:
3202 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
3203 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
3204 break;
3205 }
3206
3207 SDNode *N;
3208 SDVTList VTs = getVTList(VT);
3209 SDValue Ops[] = {Operand};
3210 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
3211 FoldingSetNodeID ID;
3212 AddNodeIDNode(ID, Opcode, VTs, Ops);
3213 void *IP = nullptr;
3214 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
3215 return SDValue(E, 0);
3216
3217 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
3218 createOperands(N, Ops);
3219 CSEMap.InsertNode(N, IP);
3220 } else {
3221 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
3222 createOperands(N, Ops);
3223 }
3224
3225 InsertNode(N);
3226 return SDValue(N, 0);
3227 }
3228
FoldValue(unsigned Opcode,const APInt & C1,const APInt & C2)3229 static std::pair<APInt, bool> FoldValue(unsigned Opcode, const APInt &C1,
3230 const APInt &C2) {
3231 switch (Opcode) {
3232 case ISD::ADD: return std::make_pair(C1 + C2, true);
3233 case ISD::SUB: return std::make_pair(C1 - C2, true);
3234 case ISD::MUL: return std::make_pair(C1 * C2, true);
3235 case ISD::AND: return std::make_pair(C1 & C2, true);
3236 case ISD::OR: return std::make_pair(C1 | C2, true);
3237 case ISD::XOR: return std::make_pair(C1 ^ C2, true);
3238 case ISD::SHL: return std::make_pair(C1 << C2, true);
3239 case ISD::SRL: return std::make_pair(C1.lshr(C2), true);
3240 case ISD::SRA: return std::make_pair(C1.ashr(C2), true);
3241 case ISD::ROTL: return std::make_pair(C1.rotl(C2), true);
3242 case ISD::ROTR: return std::make_pair(C1.rotr(C2), true);
3243 case ISD::SMIN: return std::make_pair(C1.sle(C2) ? C1 : C2, true);
3244 case ISD::SMAX: return std::make_pair(C1.sge(C2) ? C1 : C2, true);
3245 case ISD::UMIN: return std::make_pair(C1.ule(C2) ? C1 : C2, true);
3246 case ISD::UMAX: return std::make_pair(C1.uge(C2) ? C1 : C2, true);
3247 case ISD::UDIV:
3248 if (!C2.getBoolValue())
3249 break;
3250 return std::make_pair(C1.udiv(C2), true);
3251 case ISD::UREM:
3252 if (!C2.getBoolValue())
3253 break;
3254 return std::make_pair(C1.urem(C2), true);
3255 case ISD::SDIV:
3256 if (!C2.getBoolValue())
3257 break;
3258 return std::make_pair(C1.sdiv(C2), true);
3259 case ISD::SREM:
3260 if (!C2.getBoolValue())
3261 break;
3262 return std::make_pair(C1.srem(C2), true);
3263 }
3264 return std::make_pair(APInt(1, 0), false);
3265 }
3266
FoldConstantArithmetic(unsigned Opcode,const SDLoc & DL,EVT VT,const ConstantSDNode * Cst1,const ConstantSDNode * Cst2)3267 SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL,
3268 EVT VT, const ConstantSDNode *Cst1,
3269 const ConstantSDNode *Cst2) {
3270 if (Cst1->isOpaque() || Cst2->isOpaque())
3271 return SDValue();
3272
3273 std::pair<APInt, bool> Folded = FoldValue(Opcode, Cst1->getAPIntValue(),
3274 Cst2->getAPIntValue());
3275 if (!Folded.second)
3276 return SDValue();
3277 return getConstant(Folded.first, DL, VT);
3278 }
3279
FoldSymbolOffset(unsigned Opcode,EVT VT,const GlobalAddressSDNode * GA,const SDNode * N2)3280 SDValue SelectionDAG::FoldSymbolOffset(unsigned Opcode, EVT VT,
3281 const GlobalAddressSDNode *GA,
3282 const SDNode *N2) {
3283 if (GA->getOpcode() != ISD::GlobalAddress)
3284 return SDValue();
3285 if (!TLI->isOffsetFoldingLegal(GA))
3286 return SDValue();
3287 const ConstantSDNode *Cst2 = dyn_cast<ConstantSDNode>(N2);
3288 if (!Cst2)
3289 return SDValue();
3290 int64_t Offset = Cst2->getSExtValue();
3291 switch (Opcode) {
3292 case ISD::ADD: break;
3293 case ISD::SUB: Offset = -uint64_t(Offset); break;
3294 default: return SDValue();
3295 }
3296 return getGlobalAddress(GA->getGlobal(), SDLoc(Cst2), VT,
3297 GA->getOffset() + uint64_t(Offset));
3298 }
3299
FoldConstantArithmetic(unsigned Opcode,const SDLoc & DL,EVT VT,SDNode * Cst1,SDNode * Cst2)3300 SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL,
3301 EVT VT, SDNode *Cst1,
3302 SDNode *Cst2) {
3303 // If the opcode is a target-specific ISD node, there's nothing we can
3304 // do here and the operand rules may not line up with the below, so
3305 // bail early.
3306 if (Opcode >= ISD::BUILTIN_OP_END)
3307 return SDValue();
3308
3309 // Handle the case of two scalars.
3310 if (const ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1)) {
3311 if (const ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2)) {
3312 SDValue Folded = FoldConstantArithmetic(Opcode, DL, VT, Scalar1, Scalar2);
3313 assert((!Folded || !VT.isVector()) &&
3314 "Can't fold vectors ops with scalar operands");
3315 return Folded;
3316 }
3317 }
3318
3319 // fold (add Sym, c) -> Sym+c
3320 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Cst1))
3321 return FoldSymbolOffset(Opcode, VT, GA, Cst2);
3322 if (isCommutativeBinOp(Opcode))
3323 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Cst2))
3324 return FoldSymbolOffset(Opcode, VT, GA, Cst1);
3325
3326 // For vectors extract each constant element into Inputs so we can constant
3327 // fold them individually.
3328 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
3329 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
3330 if (!BV1 || !BV2)
3331 return SDValue();
3332
3333 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
3334
3335 EVT SVT = VT.getScalarType();
3336 SmallVector<SDValue, 4> Outputs;
3337 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
3338 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
3339 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
3340 if (!V1 || !V2) // Not a constant, bail.
3341 return SDValue();
3342
3343 if (V1->isOpaque() || V2->isOpaque())
3344 return SDValue();
3345
3346 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
3347 // FIXME: This is valid and could be handled by truncating the APInts.
3348 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
3349 return SDValue();
3350
3351 // Fold one vector element.
3352 std::pair<APInt, bool> Folded = FoldValue(Opcode, V1->getAPIntValue(),
3353 V2->getAPIntValue());
3354 if (!Folded.second)
3355 return SDValue();
3356 Outputs.push_back(getConstant(Folded.first, DL, SVT));
3357 }
3358
3359 assert(VT.getVectorNumElements() == Outputs.size() &&
3360 "Vector size mismatch!");
3361
3362 // We may have a vector type but a scalar result. Create a splat.
3363 Outputs.resize(VT.getVectorNumElements(), Outputs.back());
3364
3365 // Build a big vector out of the scalar elements we generated.
3366 return getBuildVector(VT, SDLoc(), Outputs);
3367 }
3368
FoldConstantVectorArithmetic(unsigned Opcode,const SDLoc & DL,EVT VT,ArrayRef<SDValue> Ops,const SDNodeFlags * Flags)3369 SDValue SelectionDAG::FoldConstantVectorArithmetic(unsigned Opcode,
3370 const SDLoc &DL, EVT VT,
3371 ArrayRef<SDValue> Ops,
3372 const SDNodeFlags *Flags) {
3373 // If the opcode is a target-specific ISD node, there's nothing we can
3374 // do here and the operand rules may not line up with the below, so
3375 // bail early.
3376 if (Opcode >= ISD::BUILTIN_OP_END)
3377 return SDValue();
3378
3379 // We can only fold vectors - maybe merge with FoldConstantArithmetic someday?
3380 if (!VT.isVector())
3381 return SDValue();
3382
3383 unsigned NumElts = VT.getVectorNumElements();
3384
3385 auto IsScalarOrSameVectorSize = [&](const SDValue &Op) {
3386 return !Op.getValueType().isVector() ||
3387 Op.getValueType().getVectorNumElements() == NumElts;
3388 };
3389
3390 auto IsConstantBuildVectorOrUndef = [&](const SDValue &Op) {
3391 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Op);
3392 return (Op.isUndef()) || (Op.getOpcode() == ISD::CONDCODE) ||
3393 (BV && BV->isConstant());
3394 };
3395
3396 // All operands must be vector types with the same number of elements as
3397 // the result type and must be either UNDEF or a build vector of constant
3398 // or UNDEF scalars.
3399 if (!std::all_of(Ops.begin(), Ops.end(), IsConstantBuildVectorOrUndef) ||
3400 !std::all_of(Ops.begin(), Ops.end(), IsScalarOrSameVectorSize))
3401 return SDValue();
3402
3403 // If we are comparing vectors, then the result needs to be a i1 boolean
3404 // that is then sign-extended back to the legal result type.
3405 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
3406
3407 // Find legal integer scalar type for constant promotion and
3408 // ensure that its scalar size is at least as large as source.
3409 EVT LegalSVT = VT.getScalarType();
3410 if (LegalSVT.isInteger()) {
3411 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3412 if (LegalSVT.bitsLT(VT.getScalarType()))
3413 return SDValue();
3414 }
3415
3416 // Constant fold each scalar lane separately.
3417 SmallVector<SDValue, 4> ScalarResults;
3418 for (unsigned i = 0; i != NumElts; i++) {
3419 SmallVector<SDValue, 4> ScalarOps;
3420 for (SDValue Op : Ops) {
3421 EVT InSVT = Op.getValueType().getScalarType();
3422 BuildVectorSDNode *InBV = dyn_cast<BuildVectorSDNode>(Op);
3423 if (!InBV) {
3424 // We've checked that this is UNDEF or a constant of some kind.
3425 if (Op.isUndef())
3426 ScalarOps.push_back(getUNDEF(InSVT));
3427 else
3428 ScalarOps.push_back(Op);
3429 continue;
3430 }
3431
3432 SDValue ScalarOp = InBV->getOperand(i);
3433 EVT ScalarVT = ScalarOp.getValueType();
3434
3435 // Build vector (integer) scalar operands may need implicit
3436 // truncation - do this before constant folding.
3437 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT))
3438 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
3439
3440 ScalarOps.push_back(ScalarOp);
3441 }
3442
3443 // Constant fold the scalar operands.
3444 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
3445
3446 // Legalize the (integer) scalar constant if necessary.
3447 if (LegalSVT != SVT)
3448 ScalarResult = getNode(ISD::SIGN_EXTEND, DL, LegalSVT, ScalarResult);
3449
3450 // Scalar folding only succeeded if the result is a constant or UNDEF.
3451 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
3452 ScalarResult.getOpcode() != ISD::ConstantFP)
3453 return SDValue();
3454 ScalarResults.push_back(ScalarResult);
3455 }
3456
3457 return getBuildVector(VT, DL, ScalarResults);
3458 }
3459
getNode(unsigned Opcode,const SDLoc & DL,EVT VT,SDValue N1,SDValue N2,const SDNodeFlags * Flags)3460 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
3461 SDValue N1, SDValue N2,
3462 const SDNodeFlags *Flags) {
3463 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3464 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
3465 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3466 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3467
3468 // Canonicalize constant to RHS if commutative.
3469 if (isCommutativeBinOp(Opcode)) {
3470 if (N1C && !N2C) {
3471 std::swap(N1C, N2C);
3472 std::swap(N1, N2);
3473 } else if (N1CFP && !N2CFP) {
3474 std::swap(N1CFP, N2CFP);
3475 std::swap(N1, N2);
3476 }
3477 }
3478
3479 switch (Opcode) {
3480 default: break;
3481 case ISD::TokenFactor:
3482 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
3483 N2.getValueType() == MVT::Other && "Invalid token factor!");
3484 // Fold trivial token factors.
3485 if (N1.getOpcode() == ISD::EntryToken) return N2;
3486 if (N2.getOpcode() == ISD::EntryToken) return N1;
3487 if (N1 == N2) return N1;
3488 break;
3489 case ISD::CONCAT_VECTORS: {
3490 // Attempt to fold CONCAT_VECTORS into BUILD_VECTOR or UNDEF.
3491 SDValue Ops[] = {N1, N2};
3492 if (SDValue V = FoldCONCAT_VECTORS(DL, VT, Ops, *this))
3493 return V;
3494 break;
3495 }
3496 case ISD::AND:
3497 assert(VT.isInteger() && "This operator does not apply to FP types!");
3498 assert(N1.getValueType() == N2.getValueType() &&
3499 N1.getValueType() == VT && "Binary operator types must match!");
3500 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
3501 // worth handling here.
3502 if (N2C && N2C->isNullValue())
3503 return N2;
3504 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
3505 return N1;
3506 break;
3507 case ISD::OR:
3508 case ISD::XOR:
3509 case ISD::ADD:
3510 case ISD::SUB:
3511 assert(VT.isInteger() && "This operator does not apply to FP types!");
3512 assert(N1.getValueType() == N2.getValueType() &&
3513 N1.getValueType() == VT && "Binary operator types must match!");
3514 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
3515 // it's worth handling here.
3516 if (N2C && N2C->isNullValue())
3517 return N1;
3518 break;
3519 case ISD::UDIV:
3520 case ISD::UREM:
3521 case ISD::MULHU:
3522 case ISD::MULHS:
3523 case ISD::MUL:
3524 case ISD::SDIV:
3525 case ISD::SREM:
3526 case ISD::SMIN:
3527 case ISD::SMAX:
3528 case ISD::UMIN:
3529 case ISD::UMAX:
3530 assert(VT.isInteger() && "This operator does not apply to FP types!");
3531 assert(N1.getValueType() == N2.getValueType() &&
3532 N1.getValueType() == VT && "Binary operator types must match!");
3533 break;
3534 case ISD::FADD:
3535 case ISD::FSUB:
3536 case ISD::FMUL:
3537 case ISD::FDIV:
3538 case ISD::FREM:
3539 if (getTarget().Options.UnsafeFPMath) {
3540 if (Opcode == ISD::FADD) {
3541 // x+0 --> x
3542 if (N2CFP && N2CFP->getValueAPF().isZero())
3543 return N1;
3544 } else if (Opcode == ISD::FSUB) {
3545 // x-0 --> x
3546 if (N2CFP && N2CFP->getValueAPF().isZero())
3547 return N1;
3548 } else if (Opcode == ISD::FMUL) {
3549 // x*0 --> 0
3550 if (N2CFP && N2CFP->isZero())
3551 return N2;
3552 // x*1 --> x
3553 if (N2CFP && N2CFP->isExactlyValue(1.0))
3554 return N1;
3555 }
3556 }
3557 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
3558 assert(N1.getValueType() == N2.getValueType() &&
3559 N1.getValueType() == VT && "Binary operator types must match!");
3560 break;
3561 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
3562 assert(N1.getValueType() == VT &&
3563 N1.getValueType().isFloatingPoint() &&
3564 N2.getValueType().isFloatingPoint() &&
3565 "Invalid FCOPYSIGN!");
3566 break;
3567 case ISD::SHL:
3568 case ISD::SRA:
3569 case ISD::SRL:
3570 case ISD::ROTL:
3571 case ISD::ROTR:
3572 assert(VT == N1.getValueType() &&
3573 "Shift operators return type must be the same as their first arg");
3574 assert(VT.isInteger() && N2.getValueType().isInteger() &&
3575 "Shifts only work on integers");
3576 assert((!VT.isVector() || VT == N2.getValueType()) &&
3577 "Vector shift amounts must be in the same as their first arg");
3578 // Verify that the shift amount VT is bit enough to hold valid shift
3579 // amounts. This catches things like trying to shift an i1024 value by an
3580 // i8, which is easy to fall into in generic code that uses
3581 // TLI.getShiftAmount().
3582 assert(N2.getValueType().getSizeInBits() >=
3583 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
3584 "Invalid use of small shift amount with oversized value!");
3585
3586 // Always fold shifts of i1 values so the code generator doesn't need to
3587 // handle them. Since we know the size of the shift has to be less than the
3588 // size of the value, the shift/rotate count is guaranteed to be zero.
3589 if (VT == MVT::i1)
3590 return N1;
3591 if (N2C && N2C->isNullValue())
3592 return N1;
3593 break;
3594 case ISD::FP_ROUND_INREG: {
3595 EVT EVT = cast<VTSDNode>(N2)->getVT();
3596 assert(VT == N1.getValueType() && "Not an inreg round!");
3597 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
3598 "Cannot FP_ROUND_INREG integer types");
3599 assert(EVT.isVector() == VT.isVector() &&
3600 "FP_ROUND_INREG type should be vector iff the operand "
3601 "type is vector!");
3602 assert((!EVT.isVector() ||
3603 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3604 "Vector element counts must match in FP_ROUND_INREG");
3605 assert(EVT.bitsLE(VT) && "Not rounding down!");
3606 (void)EVT;
3607 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
3608 break;
3609 }
3610 case ISD::FP_ROUND:
3611 assert(VT.isFloatingPoint() &&
3612 N1.getValueType().isFloatingPoint() &&
3613 VT.bitsLE(N1.getValueType()) &&
3614 N2C && "Invalid FP_ROUND!");
3615 if (N1.getValueType() == VT) return N1; // noop conversion.
3616 break;
3617 case ISD::AssertSext:
3618 case ISD::AssertZext: {
3619 EVT EVT = cast<VTSDNode>(N2)->getVT();
3620 assert(VT == N1.getValueType() && "Not an inreg extend!");
3621 assert(VT.isInteger() && EVT.isInteger() &&
3622 "Cannot *_EXTEND_INREG FP types");
3623 assert(!EVT.isVector() &&
3624 "AssertSExt/AssertZExt type should be the vector element type "
3625 "rather than the vector type!");
3626 assert(EVT.bitsLE(VT) && "Not extending!");
3627 if (VT == EVT) return N1; // noop assertion.
3628 break;
3629 }
3630 case ISD::SIGN_EXTEND_INREG: {
3631 EVT EVT = cast<VTSDNode>(N2)->getVT();
3632 assert(VT == N1.getValueType() && "Not an inreg extend!");
3633 assert(VT.isInteger() && EVT.isInteger() &&
3634 "Cannot *_EXTEND_INREG FP types");
3635 assert(EVT.isVector() == VT.isVector() &&
3636 "SIGN_EXTEND_INREG type should be vector iff the operand "
3637 "type is vector!");
3638 assert((!EVT.isVector() ||
3639 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3640 "Vector element counts must match in SIGN_EXTEND_INREG");
3641 assert(EVT.bitsLE(VT) && "Not extending!");
3642 if (EVT == VT) return N1; // Not actually extending
3643
3644 auto SignExtendInReg = [&](APInt Val) {
3645 unsigned FromBits = EVT.getScalarType().getSizeInBits();
3646 Val <<= Val.getBitWidth() - FromBits;
3647 Val = Val.ashr(Val.getBitWidth() - FromBits);
3648 return getConstant(Val, DL, VT.getScalarType());
3649 };
3650
3651 if (N1C) {
3652 const APInt &Val = N1C->getAPIntValue();
3653 return SignExtendInReg(Val);
3654 }
3655 if (ISD::isBuildVectorOfConstantSDNodes(N1.getNode())) {
3656 SmallVector<SDValue, 8> Ops;
3657 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
3658 SDValue Op = N1.getOperand(i);
3659 if (Op.isUndef()) {
3660 Ops.push_back(getUNDEF(VT.getScalarType()));
3661 continue;
3662 }
3663 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3664 APInt Val = C->getAPIntValue();
3665 Val = Val.zextOrTrunc(VT.getScalarSizeInBits());
3666 Ops.push_back(SignExtendInReg(Val));
3667 continue;
3668 }
3669 break;
3670 }
3671 if (Ops.size() == VT.getVectorNumElements())
3672 return getBuildVector(VT, DL, Ops);
3673 }
3674 break;
3675 }
3676 case ISD::EXTRACT_VECTOR_ELT:
3677 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3678 if (N1.isUndef())
3679 return getUNDEF(VT);
3680
3681 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF
3682 if (N2C && N2C->getZExtValue() >= N1.getValueType().getVectorNumElements())
3683 return getUNDEF(VT);
3684
3685 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3686 // expanding copies of large vectors from registers.
3687 if (N2C &&
3688 N1.getOpcode() == ISD::CONCAT_VECTORS &&
3689 N1.getNumOperands() > 0) {
3690 unsigned Factor =
3691 N1.getOperand(0).getValueType().getVectorNumElements();
3692 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
3693 N1.getOperand(N2C->getZExtValue() / Factor),
3694 getConstant(N2C->getZExtValue() % Factor, DL,
3695 N2.getValueType()));
3696 }
3697
3698 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3699 // expanding large vector constants.
3700 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3701 SDValue Elt = N1.getOperand(N2C->getZExtValue());
3702
3703 if (VT != Elt.getValueType())
3704 // If the vector element type is not legal, the BUILD_VECTOR operands
3705 // are promoted and implicitly truncated, and the result implicitly
3706 // extended. Make that explicit here.
3707 Elt = getAnyExtOrTrunc(Elt, DL, VT);
3708
3709 return Elt;
3710 }
3711
3712 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3713 // operations are lowered to scalars.
3714 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
3715 // If the indices are the same, return the inserted element else
3716 // if the indices are known different, extract the element from
3717 // the original vector.
3718 SDValue N1Op2 = N1.getOperand(2);
3719 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
3720
3721 if (N1Op2C && N2C) {
3722 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3723 if (VT == N1.getOperand(1).getValueType())
3724 return N1.getOperand(1);
3725 else
3726 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3727 }
3728
3729 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
3730 }
3731 }
3732 break;
3733 case ISD::EXTRACT_ELEMENT:
3734 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
3735 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3736 (N1.getValueType().isInteger() == VT.isInteger()) &&
3737 N1.getValueType() != VT &&
3738 "Wrong types for EXTRACT_ELEMENT!");
3739
3740 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3741 // 64-bit integers into 32-bit parts. Instead of building the extract of
3742 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
3743 if (N1.getOpcode() == ISD::BUILD_PAIR)
3744 return N1.getOperand(N2C->getZExtValue());
3745
3746 // EXTRACT_ELEMENT of a constant int is also very common.
3747 if (N1C) {
3748 unsigned ElementSize = VT.getSizeInBits();
3749 unsigned Shift = ElementSize * N2C->getZExtValue();
3750 APInt ShiftedVal = N1C->getAPIntValue().lshr(Shift);
3751 return getConstant(ShiftedVal.trunc(ElementSize), DL, VT);
3752 }
3753 break;
3754 case ISD::EXTRACT_SUBVECTOR:
3755 if (VT.isSimple() && N1.getValueType().isSimple()) {
3756 assert(VT.isVector() && N1.getValueType().isVector() &&
3757 "Extract subvector VTs must be a vectors!");
3758 assert(VT.getVectorElementType() ==
3759 N1.getValueType().getVectorElementType() &&
3760 "Extract subvector VTs must have the same element type!");
3761 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
3762 "Extract subvector must be from larger vector to smaller vector!");
3763
3764 if (N2C) {
3765 assert((VT.getVectorNumElements() + N2C->getZExtValue()
3766 <= N1.getValueType().getVectorNumElements())
3767 && "Extract subvector overflow!");
3768 }
3769
3770 // Trivial extraction.
3771 if (VT.getSimpleVT() == N1.getSimpleValueType())
3772 return N1;
3773 }
3774 break;
3775 }
3776
3777 // Perform trivial constant folding.
3778 if (SDValue SV =
3779 FoldConstantArithmetic(Opcode, DL, VT, N1.getNode(), N2.getNode()))
3780 return SV;
3781
3782 // Constant fold FP operations.
3783 bool HasFPExceptions = TLI->hasFloatingPointExceptions();
3784 if (N1CFP) {
3785 if (N2CFP) {
3786 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3787 APFloat::opStatus s;
3788 switch (Opcode) {
3789 case ISD::FADD:
3790 s = V1.add(V2, APFloat::rmNearestTiesToEven);
3791 if (!HasFPExceptions || s != APFloat::opInvalidOp)
3792 return getConstantFP(V1, DL, VT);
3793 break;
3794 case ISD::FSUB:
3795 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3796 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
3797 return getConstantFP(V1, DL, VT);
3798 break;
3799 case ISD::FMUL:
3800 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3801 if (!HasFPExceptions || s!=APFloat::opInvalidOp)
3802 return getConstantFP(V1, DL, VT);
3803 break;
3804 case ISD::FDIV:
3805 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3806 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3807 s!=APFloat::opDivByZero)) {
3808 return getConstantFP(V1, DL, VT);
3809 }
3810 break;
3811 case ISD::FREM :
3812 s = V1.mod(V2);
3813 if (!HasFPExceptions || (s!=APFloat::opInvalidOp &&
3814 s!=APFloat::opDivByZero)) {
3815 return getConstantFP(V1, DL, VT);
3816 }
3817 break;
3818 case ISD::FCOPYSIGN:
3819 V1.copySign(V2);
3820 return getConstantFP(V1, DL, VT);
3821 default: break;
3822 }
3823 }
3824
3825 if (Opcode == ISD::FP_ROUND) {
3826 APFloat V = N1CFP->getValueAPF(); // make copy
3827 bool ignored;
3828 // This can return overflow, underflow, or inexact; we don't care.
3829 // FIXME need to be more flexible about rounding mode.
3830 (void)V.convert(EVTToAPFloatSemantics(VT),
3831 APFloat::rmNearestTiesToEven, &ignored);
3832 return getConstantFP(V, DL, VT);
3833 }
3834 }
3835
3836 // Canonicalize an UNDEF to the RHS, even over a constant.
3837 if (N1.isUndef()) {
3838 if (isCommutativeBinOp(Opcode)) {
3839 std::swap(N1, N2);
3840 } else {
3841 switch (Opcode) {
3842 case ISD::FP_ROUND_INREG:
3843 case ISD::SIGN_EXTEND_INREG:
3844 case ISD::SUB:
3845 case ISD::FSUB:
3846 case ISD::FDIV:
3847 case ISD::FREM:
3848 case ISD::SRA:
3849 return N1; // fold op(undef, arg2) -> undef
3850 case ISD::UDIV:
3851 case ISD::SDIV:
3852 case ISD::UREM:
3853 case ISD::SREM:
3854 case ISD::SRL:
3855 case ISD::SHL:
3856 if (!VT.isVector())
3857 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
3858 // For vectors, we can't easily build an all zero vector, just return
3859 // the LHS.
3860 return N2;
3861 }
3862 }
3863 }
3864
3865 // Fold a bunch of operators when the RHS is undef.
3866 if (N2.isUndef()) {
3867 switch (Opcode) {
3868 case ISD::XOR:
3869 if (N1.isUndef())
3870 // Handle undef ^ undef -> 0 special case. This is a common
3871 // idiom (misuse).
3872 return getConstant(0, DL, VT);
3873 // fallthrough
3874 case ISD::ADD:
3875 case ISD::ADDC:
3876 case ISD::ADDE:
3877 case ISD::SUB:
3878 case ISD::UDIV:
3879 case ISD::SDIV:
3880 case ISD::UREM:
3881 case ISD::SREM:
3882 return N2; // fold op(arg1, undef) -> undef
3883 case ISD::FADD:
3884 case ISD::FSUB:
3885 case ISD::FMUL:
3886 case ISD::FDIV:
3887 case ISD::FREM:
3888 if (getTarget().Options.UnsafeFPMath)
3889 return N2;
3890 break;
3891 case ISD::MUL:
3892 case ISD::AND:
3893 case ISD::SRL:
3894 case ISD::SHL:
3895 if (!VT.isVector())
3896 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
3897 // For vectors, we can't easily build an all zero vector, just return
3898 // the LHS.
3899 return N1;
3900 case ISD::OR:
3901 if (!VT.isVector())
3902 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), DL, VT);
3903 // For vectors, we can't easily build an all one vector, just return
3904 // the LHS.
3905 return N1;
3906 case ISD::SRA:
3907 return N1;
3908 }
3909 }
3910
3911 // Memoize this node if possible.
3912 SDNode *N;
3913 SDVTList VTs = getVTList(VT);
3914 if (VT != MVT::Glue) {
3915 SDValue Ops[] = {N1, N2};
3916 FoldingSetNodeID ID;
3917 AddNodeIDNode(ID, Opcode, VTs, Ops);
3918 void *IP = nullptr;
3919 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
3920 if (Flags)
3921 E->intersectFlagsWith(Flags);
3922 return SDValue(E, 0);
3923 }
3924
3925 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, Flags);
3926 CSEMap.InsertNode(N, IP);
3927 } else {
3928 N = GetBinarySDNode(Opcode, DL, VTs, N1, N2, Flags);
3929 }
3930
3931 InsertNode(N);
3932 return SDValue(N, 0);
3933 }
3934
getNode(unsigned Opcode,const SDLoc & DL,EVT VT,SDValue N1,SDValue N2,SDValue N3)3935 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
3936 SDValue N1, SDValue N2, SDValue N3) {
3937 // Perform various simplifications.
3938 switch (Opcode) {
3939 case ISD::FMA: {
3940 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3941 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3942 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3943 if (N1CFP && N2CFP && N3CFP) {
3944 APFloat V1 = N1CFP->getValueAPF();
3945 const APFloat &V2 = N2CFP->getValueAPF();
3946 const APFloat &V3 = N3CFP->getValueAPF();
3947 APFloat::opStatus s =
3948 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3949 if (!TLI->hasFloatingPointExceptions() || s != APFloat::opInvalidOp)
3950 return getConstantFP(V1, DL, VT);
3951 }
3952 break;
3953 }
3954 case ISD::CONCAT_VECTORS: {
3955 // Attempt to fold CONCAT_VECTORS into BUILD_VECTOR or UNDEF.
3956 SDValue Ops[] = {N1, N2, N3};
3957 if (SDValue V = FoldCONCAT_VECTORS(DL, VT, Ops, *this))
3958 return V;
3959 break;
3960 }
3961 case ISD::SETCC: {
3962 // Use FoldSetCC to simplify SETCC's.
3963 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
3964 return V;
3965 // Vector constant folding.
3966 SDValue Ops[] = {N1, N2, N3};
3967 if (SDValue V = FoldConstantVectorArithmetic(Opcode, DL, VT, Ops))
3968 return V;
3969 break;
3970 }
3971 case ISD::SELECT:
3972 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
3973 if (N1C->getZExtValue())
3974 return N2; // select true, X, Y -> X
3975 return N3; // select false, X, Y -> Y
3976 }
3977
3978 if (N2 == N3) return N2; // select C, X, X -> X
3979 break;
3980 case ISD::VECTOR_SHUFFLE:
3981 llvm_unreachable("should use getVectorShuffle constructor!");
3982 case ISD::INSERT_SUBVECTOR: {
3983 SDValue Index = N3;
3984 if (VT.isSimple() && N1.getValueType().isSimple()
3985 && N2.getValueType().isSimple()) {
3986 assert(VT.isVector() && N1.getValueType().isVector() &&
3987 N2.getValueType().isVector() &&
3988 "Insert subvector VTs must be a vectors");
3989 assert(VT == N1.getValueType() &&
3990 "Dest and insert subvector source types must match!");
3991 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
3992 "Insert subvector must be from smaller vector to larger vector!");
3993 if (isa<ConstantSDNode>(Index)) {
3994 assert((N2.getValueType().getVectorNumElements() +
3995 cast<ConstantSDNode>(Index)->getZExtValue()
3996 <= VT.getVectorNumElements())
3997 && "Insert subvector overflow!");
3998 }
3999
4000 // Trivial insertion.
4001 if (VT.getSimpleVT() == N2.getSimpleValueType())
4002 return N2;
4003 }
4004 break;
4005 }
4006 case ISD::BITCAST:
4007 // Fold bit_convert nodes from a type to themselves.
4008 if (N1.getValueType() == VT)
4009 return N1;
4010 break;
4011 }
4012
4013 // Memoize node if it doesn't produce a flag.
4014 SDNode *N;
4015 SDVTList VTs = getVTList(VT);
4016 SDValue Ops[] = {N1, N2, N3};
4017 if (VT != MVT::Glue) {
4018 FoldingSetNodeID ID;
4019 AddNodeIDNode(ID, Opcode, VTs, Ops);
4020 void *IP = nullptr;
4021 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
4022 return SDValue(E, 0);
4023
4024 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
4025 createOperands(N, Ops);
4026 CSEMap.InsertNode(N, IP);
4027 } else {
4028 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
4029 createOperands(N, Ops);
4030 }
4031
4032 InsertNode(N);
4033 return SDValue(N, 0);
4034 }
4035
getNode(unsigned Opcode,const SDLoc & DL,EVT VT,SDValue N1,SDValue N2,SDValue N3,SDValue N4)4036 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
4037 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
4038 SDValue Ops[] = { N1, N2, N3, N4 };
4039 return getNode(Opcode, DL, VT, Ops);
4040 }
4041
getNode(unsigned Opcode,const SDLoc & DL,EVT VT,SDValue N1,SDValue N2,SDValue N3,SDValue N4,SDValue N5)4042 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
4043 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
4044 SDValue N5) {
4045 SDValue Ops[] = { N1, N2, N3, N4, N5 };
4046 return getNode(Opcode, DL, VT, Ops);
4047 }
4048
4049 /// getStackArgumentTokenFactor - Compute a TokenFactor to force all
4050 /// the incoming stack arguments to be loaded from the stack.
getStackArgumentTokenFactor(SDValue Chain)4051 SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
4052 SmallVector<SDValue, 8> ArgChains;
4053
4054 // Include the original chain at the beginning of the list. When this is
4055 // used by target LowerCall hooks, this helps legalize find the
4056 // CALLSEQ_BEGIN node.
4057 ArgChains.push_back(Chain);
4058
4059 // Add a chain value for each stack argument.
4060 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
4061 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
4062 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
4063 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
4064 if (FI->getIndex() < 0)
4065 ArgChains.push_back(SDValue(L, 1));
4066
4067 // Build a tokenfactor for all the chains.
4068 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
4069 }
4070
4071 /// getMemsetValue - Vectorized representation of the memset value
4072 /// operand.
getMemsetValue(SDValue Value,EVT VT,SelectionDAG & DAG,const SDLoc & dl)4073 static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
4074 const SDLoc &dl) {
4075 assert(!Value.isUndef());
4076
4077 unsigned NumBits = VT.getScalarType().getSizeInBits();
4078 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
4079 assert(C->getAPIntValue().getBitWidth() == 8);
4080 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
4081 if (VT.isInteger())
4082 return DAG.getConstant(Val, dl, VT);
4083 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
4084 VT);
4085 }
4086
4087 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
4088 EVT IntVT = VT.getScalarType();
4089 if (!IntVT.isInteger())
4090 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
4091
4092 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
4093 if (NumBits > 8) {
4094 // Use a multiplication with 0x010101... to extend the input to the
4095 // required length.
4096 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
4097 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
4098 DAG.getConstant(Magic, dl, IntVT));
4099 }
4100
4101 if (VT != Value.getValueType() && !VT.isInteger())
4102 Value = DAG.getBitcast(VT.getScalarType(), Value);
4103 if (VT != Value.getValueType())
4104 Value = DAG.getSplatBuildVector(VT, dl, Value);
4105
4106 return Value;
4107 }
4108
4109 /// getMemsetStringVal - Similar to getMemsetValue. Except this is only
4110 /// used when a memcpy is turned into a memset when the source is a constant
4111 /// string ptr.
getMemsetStringVal(EVT VT,const SDLoc & dl,SelectionDAG & DAG,const TargetLowering & TLI,StringRef Str)4112 static SDValue getMemsetStringVal(EVT VT, const SDLoc &dl, SelectionDAG &DAG,
4113 const TargetLowering &TLI, StringRef Str) {
4114 // Handle vector with all elements zero.
4115 if (Str.empty()) {
4116 if (VT.isInteger())
4117 return DAG.getConstant(0, dl, VT);
4118 else if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
4119 return DAG.getConstantFP(0.0, dl, VT);
4120 else if (VT.isVector()) {
4121 unsigned NumElts = VT.getVectorNumElements();
4122 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
4123 return DAG.getNode(ISD::BITCAST, dl, VT,
4124 DAG.getConstant(0, dl,
4125 EVT::getVectorVT(*DAG.getContext(),
4126 EltVT, NumElts)));
4127 } else
4128 llvm_unreachable("Expected type!");
4129 }
4130
4131 assert(!VT.isVector() && "Can't handle vector type here!");
4132 unsigned NumVTBits = VT.getSizeInBits();
4133 unsigned NumVTBytes = NumVTBits / 8;
4134 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
4135
4136 APInt Val(NumVTBits, 0);
4137 if (DAG.getDataLayout().isLittleEndian()) {
4138 for (unsigned i = 0; i != NumBytes; ++i)
4139 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
4140 } else {
4141 for (unsigned i = 0; i != NumBytes; ++i)
4142 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
4143 }
4144
4145 // If the "cost" of materializing the integer immediate is less than the cost
4146 // of a load, then it is cost effective to turn the load into the immediate.
4147 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
4148 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
4149 return DAG.getConstant(Val, dl, VT);
4150 return SDValue(nullptr, 0);
4151 }
4152
getMemBasePlusOffset(SDValue Base,unsigned Offset,const SDLoc & DL)4153 SDValue SelectionDAG::getMemBasePlusOffset(SDValue Base, unsigned Offset,
4154 const SDLoc &DL) {
4155 EVT VT = Base.getValueType();
4156 return getNode(ISD::ADD, DL, VT, Base, getConstant(Offset, DL, VT));
4157 }
4158
4159 /// isMemSrcFromString - Returns true if memcpy source is a string constant.
4160 ///
isMemSrcFromString(SDValue Src,StringRef & Str)4161 static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
4162 uint64_t SrcDelta = 0;
4163 GlobalAddressSDNode *G = nullptr;
4164 if (Src.getOpcode() == ISD::GlobalAddress)
4165 G = cast<GlobalAddressSDNode>(Src);
4166 else if (Src.getOpcode() == ISD::ADD &&
4167 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
4168 Src.getOperand(1).getOpcode() == ISD::Constant) {
4169 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
4170 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
4171 }
4172 if (!G)
4173 return false;
4174
4175 return getConstantStringInfo(G->getGlobal(), Str,
4176 SrcDelta + G->getOffset(), false);
4177 }
4178
4179 /// Determines the optimal series of memory ops to replace the memset / memcpy.
4180 /// Return true if the number of memory ops is below the threshold (Limit).
4181 /// It returns the types of the sequence of memory ops to perform
4182 /// memset / memcpy by reference.
FindOptimalMemOpLowering(std::vector<EVT> & MemOps,unsigned Limit,uint64_t Size,unsigned DstAlign,unsigned SrcAlign,bool IsMemset,bool ZeroMemset,bool MemcpyStrSrc,bool AllowOverlap,unsigned DstAS,unsigned SrcAS,SelectionDAG & DAG,const TargetLowering & TLI)4183 static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
4184 unsigned Limit, uint64_t Size,
4185 unsigned DstAlign, unsigned SrcAlign,
4186 bool IsMemset,
4187 bool ZeroMemset,
4188 bool MemcpyStrSrc,
4189 bool AllowOverlap,
4190 unsigned DstAS, unsigned SrcAS,
4191 SelectionDAG &DAG,
4192 const TargetLowering &TLI) {
4193 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
4194 "Expecting memcpy / memset source to meet alignment requirement!");
4195 // If 'SrcAlign' is zero, that means the memory operation does not need to
4196 // load the value, i.e. memset or memcpy from constant string. Otherwise,
4197 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
4198 // is the specified alignment of the memory operation. If it is zero, that
4199 // means it's possible to change the alignment of the destination.
4200 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
4201 // not need to be loaded.
4202 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
4203 IsMemset, ZeroMemset, MemcpyStrSrc,
4204 DAG.getMachineFunction());
4205
4206 if (VT == MVT::Other) {
4207 if (DstAlign >= DAG.getDataLayout().getPointerPrefAlignment(DstAS) ||
4208 TLI.allowsMisalignedMemoryAccesses(VT, DstAS, DstAlign)) {
4209 VT = TLI.getPointerTy(DAG.getDataLayout(), DstAS);
4210 } else {
4211 switch (DstAlign & 7) {
4212 case 0: VT = MVT::i64; break;
4213 case 4: VT = MVT::i32; break;
4214 case 2: VT = MVT::i16; break;
4215 default: VT = MVT::i8; break;
4216 }
4217 }
4218
4219 MVT LVT = MVT::i64;
4220 while (!TLI.isTypeLegal(LVT))
4221 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
4222 assert(LVT.isInteger());
4223
4224 if (VT.bitsGT(LVT))
4225 VT = LVT;
4226 }
4227
4228 unsigned NumMemOps = 0;
4229 while (Size != 0) {
4230 unsigned VTSize = VT.getSizeInBits() / 8;
4231 while (VTSize > Size) {
4232 // For now, only use non-vector load / store's for the left-over pieces.
4233 EVT NewVT = VT;
4234 unsigned NewVTSize;
4235
4236 bool Found = false;
4237 if (VT.isVector() || VT.isFloatingPoint()) {
4238 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
4239 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
4240 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
4241 Found = true;
4242 else if (NewVT == MVT::i64 &&
4243 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
4244 TLI.isSafeMemOpType(MVT::f64)) {
4245 // i64 is usually not legal on 32-bit targets, but f64 may be.
4246 NewVT = MVT::f64;
4247 Found = true;
4248 }
4249 }
4250
4251 if (!Found) {
4252 do {
4253 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
4254 if (NewVT == MVT::i8)
4255 break;
4256 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
4257 }
4258 NewVTSize = NewVT.getSizeInBits() / 8;
4259
4260 // If the new VT cannot cover all of the remaining bits, then consider
4261 // issuing a (or a pair of) unaligned and overlapping load / store.
4262 // FIXME: Only does this for 64-bit or more since we don't have proper
4263 // cost model for unaligned load / store.
4264 bool Fast;
4265 if (NumMemOps && AllowOverlap &&
4266 VTSize >= 8 && NewVTSize < Size &&
4267 TLI.allowsMisalignedMemoryAccesses(VT, DstAS, DstAlign, &Fast) && Fast)
4268 VTSize = Size;
4269 else {
4270 VT = NewVT;
4271 VTSize = NewVTSize;
4272 }
4273 }
4274
4275 if (++NumMemOps > Limit)
4276 return false;
4277
4278 MemOps.push_back(VT);
4279 Size -= VTSize;
4280 }
4281
4282 return true;
4283 }
4284
shouldLowerMemFuncForSize(const MachineFunction & MF)4285 static bool shouldLowerMemFuncForSize(const MachineFunction &MF) {
4286 // On Darwin, -Os means optimize for size without hurting performance, so
4287 // only really optimize for size when -Oz (MinSize) is used.
4288 if (MF.getTarget().getTargetTriple().isOSDarwin())
4289 return MF.getFunction()->optForMinSize();
4290 return MF.getFunction()->optForSize();
4291 }
4292
getMemcpyLoadsAndStores(SelectionDAG & DAG,const SDLoc & dl,SDValue Chain,SDValue Dst,SDValue Src,uint64_t Size,unsigned Align,bool isVol,bool AlwaysInline,MachinePointerInfo DstPtrInfo,MachinePointerInfo SrcPtrInfo)4293 static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl,
4294 SDValue Chain, SDValue Dst, SDValue Src,
4295 uint64_t Size, unsigned Align,
4296 bool isVol, bool AlwaysInline,
4297 MachinePointerInfo DstPtrInfo,
4298 MachinePointerInfo SrcPtrInfo) {
4299 // Turn a memcpy of undef to nop.
4300 if (Src.isUndef())
4301 return Chain;
4302
4303 // Expand memcpy to a series of load and store ops if the size operand falls
4304 // below a certain threshold.
4305 // TODO: In the AlwaysInline case, if the size is big then generate a loop
4306 // rather than maybe a humongous number of loads and stores.
4307 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4308 std::vector<EVT> MemOps;
4309 bool DstAlignCanChange = false;
4310 MachineFunction &MF = DAG.getMachineFunction();
4311 MachineFrameInfo *MFI = MF.getFrameInfo();
4312 bool OptSize = shouldLowerMemFuncForSize(MF);
4313 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4314 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4315 DstAlignCanChange = true;
4316 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4317 if (Align > SrcAlign)
4318 SrcAlign = Align;
4319 StringRef Str;
4320 bool CopyFromStr = isMemSrcFromString(Src, Str);
4321 bool isZeroStr = CopyFromStr && Str.empty();
4322 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
4323
4324 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
4325 (DstAlignCanChange ? 0 : Align),
4326 (isZeroStr ? 0 : SrcAlign),
4327 false, false, CopyFromStr, true,
4328 DstPtrInfo.getAddrSpace(),
4329 SrcPtrInfo.getAddrSpace(),
4330 DAG, TLI))
4331 return SDValue();
4332
4333 if (DstAlignCanChange) {
4334 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
4335 unsigned NewAlign = (unsigned)DAG.getDataLayout().getABITypeAlignment(Ty);
4336
4337 // Don't promote to an alignment that would require dynamic stack
4338 // realignment.
4339 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
4340 if (!TRI->needsStackRealignment(MF))
4341 while (NewAlign > Align &&
4342 DAG.getDataLayout().exceedsNaturalStackAlignment(NewAlign))
4343 NewAlign /= 2;
4344
4345 if (NewAlign > Align) {
4346 // Give the stack frame object a larger alignment if needed.
4347 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4348 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4349 Align = NewAlign;
4350 }
4351 }
4352
4353 SmallVector<SDValue, 8> OutChains;
4354 unsigned NumMemOps = MemOps.size();
4355 uint64_t SrcOff = 0, DstOff = 0;
4356 for (unsigned i = 0; i != NumMemOps; ++i) {
4357 EVT VT = MemOps[i];
4358 unsigned VTSize = VT.getSizeInBits() / 8;
4359 SDValue Value, Store;
4360
4361 if (VTSize > Size) {
4362 // Issuing an unaligned load / store pair that overlaps with the previous
4363 // pair. Adjust the offset accordingly.
4364 assert(i == NumMemOps-1 && i != 0);
4365 SrcOff -= VTSize - Size;
4366 DstOff -= VTSize - Size;
4367 }
4368
4369 if (CopyFromStr &&
4370 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
4371 // It's unlikely a store of a vector immediate can be done in a single
4372 // instruction. It would require a load from a constantpool first.
4373 // We only handle zero vectors here.
4374 // FIXME: Handle other cases where store of vector immediate is done in
4375 // a single instruction.
4376 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
4377 if (Value.getNode())
4378 Store = DAG.getStore(Chain, dl, Value,
4379 DAG.getMemBasePlusOffset(Dst, DstOff, dl),
4380 DstPtrInfo.getWithOffset(DstOff), isVol,
4381 false, Align);
4382 }
4383
4384 if (!Store.getNode()) {
4385 // The type might not be legal for the target. This should only happen
4386 // if the type is smaller than a legal type, as on PPC, so the right
4387 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
4388 // to Load/Store if NVT==VT.
4389 // FIXME does the case above also need this?
4390 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4391 assert(NVT.bitsGE(VT));
4392 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
4393 DAG.getMemBasePlusOffset(Src, SrcOff, dl),
4394 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
4395 false, MinAlign(SrcAlign, SrcOff));
4396 OutChains.push_back(Value.getValue(1));
4397 Store = DAG.getTruncStore(Chain, dl, Value,
4398 DAG.getMemBasePlusOffset(Dst, DstOff, dl),
4399 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
4400 false, Align);
4401 }
4402 OutChains.push_back(Store);
4403 SrcOff += VTSize;
4404 DstOff += VTSize;
4405 Size -= VTSize;
4406 }
4407
4408 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
4409 }
4410
getMemmoveLoadsAndStores(SelectionDAG & DAG,const SDLoc & dl,SDValue Chain,SDValue Dst,SDValue Src,uint64_t Size,unsigned Align,bool isVol,bool AlwaysInline,MachinePointerInfo DstPtrInfo,MachinePointerInfo SrcPtrInfo)4411 static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl,
4412 SDValue Chain, SDValue Dst, SDValue Src,
4413 uint64_t Size, unsigned Align,
4414 bool isVol, bool AlwaysInline,
4415 MachinePointerInfo DstPtrInfo,
4416 MachinePointerInfo SrcPtrInfo) {
4417 // Turn a memmove of undef to nop.
4418 if (Src.isUndef())
4419 return Chain;
4420
4421 // Expand memmove to a series of load and store ops if the size operand falls
4422 // below a certain threshold.
4423 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4424 std::vector<EVT> MemOps;
4425 bool DstAlignCanChange = false;
4426 MachineFunction &MF = DAG.getMachineFunction();
4427 MachineFrameInfo *MFI = MF.getFrameInfo();
4428 bool OptSize = shouldLowerMemFuncForSize(MF);
4429 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4430 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4431 DstAlignCanChange = true;
4432 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
4433 if (Align > SrcAlign)
4434 SrcAlign = Align;
4435 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
4436
4437 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
4438 (DstAlignCanChange ? 0 : Align), SrcAlign,
4439 false, false, false, false,
4440 DstPtrInfo.getAddrSpace(),
4441 SrcPtrInfo.getAddrSpace(),
4442 DAG, TLI))
4443 return SDValue();
4444
4445 if (DstAlignCanChange) {
4446 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
4447 unsigned NewAlign = (unsigned)DAG.getDataLayout().getABITypeAlignment(Ty);
4448 if (NewAlign > Align) {
4449 // Give the stack frame object a larger alignment if needed.
4450 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4451 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4452 Align = NewAlign;
4453 }
4454 }
4455
4456 uint64_t SrcOff = 0, DstOff = 0;
4457 SmallVector<SDValue, 8> LoadValues;
4458 SmallVector<SDValue, 8> LoadChains;
4459 SmallVector<SDValue, 8> OutChains;
4460 unsigned NumMemOps = MemOps.size();
4461 for (unsigned i = 0; i < NumMemOps; i++) {
4462 EVT VT = MemOps[i];
4463 unsigned VTSize = VT.getSizeInBits() / 8;
4464 SDValue Value;
4465
4466 Value = DAG.getLoad(VT, dl, Chain,
4467 DAG.getMemBasePlusOffset(Src, SrcOff, dl),
4468 SrcPtrInfo.getWithOffset(SrcOff), isVol,
4469 false, false, SrcAlign);
4470 LoadValues.push_back(Value);
4471 LoadChains.push_back(Value.getValue(1));
4472 SrcOff += VTSize;
4473 }
4474 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
4475 OutChains.clear();
4476 for (unsigned i = 0; i < NumMemOps; i++) {
4477 EVT VT = MemOps[i];
4478 unsigned VTSize = VT.getSizeInBits() / 8;
4479 SDValue Store;
4480
4481 Store = DAG.getStore(Chain, dl, LoadValues[i],
4482 DAG.getMemBasePlusOffset(Dst, DstOff, dl),
4483 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
4484 OutChains.push_back(Store);
4485 DstOff += VTSize;
4486 }
4487
4488 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
4489 }
4490
4491 /// \brief Lower the call to 'memset' intrinsic function into a series of store
4492 /// operations.
4493 ///
4494 /// \param DAG Selection DAG where lowered code is placed.
4495 /// \param dl Link to corresponding IR location.
4496 /// \param Chain Control flow dependency.
4497 /// \param Dst Pointer to destination memory location.
4498 /// \param Src Value of byte to write into the memory.
4499 /// \param Size Number of bytes to write.
4500 /// \param Align Alignment of the destination in bytes.
4501 /// \param isVol True if destination is volatile.
4502 /// \param DstPtrInfo IR information on the memory pointer.
4503 /// \returns New head in the control flow, if lowering was successful, empty
4504 /// SDValue otherwise.
4505 ///
4506 /// The function tries to replace 'llvm.memset' intrinsic with several store
4507 /// operations and value calculation code. This is usually profitable for small
4508 /// memory size.
getMemsetStores(SelectionDAG & DAG,const SDLoc & dl,SDValue Chain,SDValue Dst,SDValue Src,uint64_t Size,unsigned Align,bool isVol,MachinePointerInfo DstPtrInfo)4509 static SDValue getMemsetStores(SelectionDAG &DAG, const SDLoc &dl,
4510 SDValue Chain, SDValue Dst, SDValue Src,
4511 uint64_t Size, unsigned Align, bool isVol,
4512 MachinePointerInfo DstPtrInfo) {
4513 // Turn a memset of undef to nop.
4514 if (Src.isUndef())
4515 return Chain;
4516
4517 // Expand memset to a series of load/store ops if the size operand
4518 // falls below a certain threshold.
4519 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4520 std::vector<EVT> MemOps;
4521 bool DstAlignCanChange = false;
4522 MachineFunction &MF = DAG.getMachineFunction();
4523 MachineFrameInfo *MFI = MF.getFrameInfo();
4524 bool OptSize = shouldLowerMemFuncForSize(MF);
4525 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
4526 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
4527 DstAlignCanChange = true;
4528 bool IsZeroVal =
4529 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
4530 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
4531 Size, (DstAlignCanChange ? 0 : Align), 0,
4532 true, IsZeroVal, false, true,
4533 DstPtrInfo.getAddrSpace(), ~0u,
4534 DAG, TLI))
4535 return SDValue();
4536
4537 if (DstAlignCanChange) {
4538 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
4539 unsigned NewAlign = (unsigned)DAG.getDataLayout().getABITypeAlignment(Ty);
4540 if (NewAlign > Align) {
4541 // Give the stack frame object a larger alignment if needed.
4542 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
4543 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
4544 Align = NewAlign;
4545 }
4546 }
4547
4548 SmallVector<SDValue, 8> OutChains;
4549 uint64_t DstOff = 0;
4550 unsigned NumMemOps = MemOps.size();
4551
4552 // Find the largest store and generate the bit pattern for it.
4553 EVT LargestVT = MemOps[0];
4554 for (unsigned i = 1; i < NumMemOps; i++)
4555 if (MemOps[i].bitsGT(LargestVT))
4556 LargestVT = MemOps[i];
4557 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
4558
4559 for (unsigned i = 0; i < NumMemOps; i++) {
4560 EVT VT = MemOps[i];
4561 unsigned VTSize = VT.getSizeInBits() / 8;
4562 if (VTSize > Size) {
4563 // Issuing an unaligned load / store pair that overlaps with the previous
4564 // pair. Adjust the offset accordingly.
4565 assert(i == NumMemOps-1 && i != 0);
4566 DstOff -= VTSize - Size;
4567 }
4568
4569 // If this store is smaller than the largest store see whether we can get
4570 // the smaller value for free with a truncate.
4571 SDValue Value = MemSetValue;
4572 if (VT.bitsLT(LargestVT)) {
4573 if (!LargestVT.isVector() && !VT.isVector() &&
4574 TLI.isTruncateFree(LargestVT, VT))
4575 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
4576 else
4577 Value = getMemsetValue(Src, VT, DAG, dl);
4578 }
4579 assert(Value.getValueType() == VT && "Value with wrong type.");
4580 SDValue Store = DAG.getStore(Chain, dl, Value,
4581 DAG.getMemBasePlusOffset(Dst, DstOff, dl),
4582 DstPtrInfo.getWithOffset(DstOff),
4583 isVol, false, Align);
4584 OutChains.push_back(Store);
4585 DstOff += VT.getSizeInBits() / 8;
4586 Size -= VTSize;
4587 }
4588
4589 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
4590 }
4591
checkAddrSpaceIsValidForLibcall(const TargetLowering * TLI,unsigned AS)4592 static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI,
4593 unsigned AS) {
4594 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
4595 // pointer operands can be losslessly bitcasted to pointers of address space 0
4596 if (AS != 0 && !TLI->isNoopAddrSpaceCast(AS, 0)) {
4597 report_fatal_error("cannot lower memory intrinsic in address space " +
4598 Twine(AS));
4599 }
4600 }
4601
getMemcpy(SDValue Chain,const SDLoc & dl,SDValue Dst,SDValue Src,SDValue Size,unsigned Align,bool isVol,bool AlwaysInline,bool isTailCall,MachinePointerInfo DstPtrInfo,MachinePointerInfo SrcPtrInfo)4602 SDValue SelectionDAG::getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst,
4603 SDValue Src, SDValue Size, unsigned Align,
4604 bool isVol, bool AlwaysInline, bool isTailCall,
4605 MachinePointerInfo DstPtrInfo,
4606 MachinePointerInfo SrcPtrInfo) {
4607 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
4608
4609 // Check to see if we should lower the memcpy to loads and stores first.
4610 // For cases within the target-specified limits, this is the best choice.
4611 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4612 if (ConstantSize) {
4613 // Memcpy with size zero? Just return the original chain.
4614 if (ConstantSize->isNullValue())
4615 return Chain;
4616
4617 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4618 ConstantSize->getZExtValue(),Align,
4619 isVol, false, DstPtrInfo, SrcPtrInfo);
4620 if (Result.getNode())
4621 return Result;
4622 }
4623
4624 // Then check to see if we should lower the memcpy with target-specific
4625 // code. If the target chooses to do this, this is the next best.
4626 if (TSI) {
4627 SDValue Result = TSI->EmitTargetCodeForMemcpy(
4628 *this, dl, Chain, Dst, Src, Size, Align, isVol, AlwaysInline,
4629 DstPtrInfo, SrcPtrInfo);
4630 if (Result.getNode())
4631 return Result;
4632 }
4633
4634 // If we really need inline code and the target declined to provide it,
4635 // use a (potentially long) sequence of loads and stores.
4636 if (AlwaysInline) {
4637 assert(ConstantSize && "AlwaysInline requires a constant size!");
4638 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4639 ConstantSize->getZExtValue(), Align, isVol,
4640 true, DstPtrInfo, SrcPtrInfo);
4641 }
4642
4643 checkAddrSpaceIsValidForLibcall(TLI, DstPtrInfo.getAddrSpace());
4644 checkAddrSpaceIsValidForLibcall(TLI, SrcPtrInfo.getAddrSpace());
4645
4646 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4647 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4648 // respect volatile, so they may do things like read or write memory
4649 // beyond the given memory regions. But fixing this isn't easy, and most
4650 // people don't care.
4651
4652 // Emit a library call.
4653 TargetLowering::ArgListTy Args;
4654 TargetLowering::ArgListEntry Entry;
4655 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
4656 Entry.Node = Dst; Args.push_back(Entry);
4657 Entry.Node = Src; Args.push_back(Entry);
4658 Entry.Node = Size; Args.push_back(Entry);
4659 // FIXME: pass in SDLoc
4660 TargetLowering::CallLoweringInfo CLI(*this);
4661 CLI.setDebugLoc(dl)
4662 .setChain(Chain)
4663 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
4664 Dst.getValueType().getTypeForEVT(*getContext()),
4665 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
4666 TLI->getPointerTy(getDataLayout())),
4667 std::move(Args))
4668 .setDiscardResult()
4669 .setTailCall(isTailCall);
4670
4671 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
4672 return CallResult.second;
4673 }
4674
getMemmove(SDValue Chain,const SDLoc & dl,SDValue Dst,SDValue Src,SDValue Size,unsigned Align,bool isVol,bool isTailCall,MachinePointerInfo DstPtrInfo,MachinePointerInfo SrcPtrInfo)4675 SDValue SelectionDAG::getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst,
4676 SDValue Src, SDValue Size, unsigned Align,
4677 bool isVol, bool isTailCall,
4678 MachinePointerInfo DstPtrInfo,
4679 MachinePointerInfo SrcPtrInfo) {
4680 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
4681
4682 // Check to see if we should lower the memmove to loads and stores first.
4683 // For cases within the target-specified limits, this is the best choice.
4684 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4685 if (ConstantSize) {
4686 // Memmove with size zero? Just return the original chain.
4687 if (ConstantSize->isNullValue())
4688 return Chain;
4689
4690 SDValue Result =
4691 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
4692 ConstantSize->getZExtValue(), Align, isVol,
4693 false, DstPtrInfo, SrcPtrInfo);
4694 if (Result.getNode())
4695 return Result;
4696 }
4697
4698 // Then check to see if we should lower the memmove with target-specific
4699 // code. If the target chooses to do this, this is the next best.
4700 if (TSI) {
4701 SDValue Result = TSI->EmitTargetCodeForMemmove(
4702 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo, SrcPtrInfo);
4703 if (Result.getNode())
4704 return Result;
4705 }
4706
4707 checkAddrSpaceIsValidForLibcall(TLI, DstPtrInfo.getAddrSpace());
4708 checkAddrSpaceIsValidForLibcall(TLI, SrcPtrInfo.getAddrSpace());
4709
4710 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4711 // not be safe. See memcpy above for more details.
4712
4713 // Emit a library call.
4714 TargetLowering::ArgListTy Args;
4715 TargetLowering::ArgListEntry Entry;
4716 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
4717 Entry.Node = Dst; Args.push_back(Entry);
4718 Entry.Node = Src; Args.push_back(Entry);
4719 Entry.Node = Size; Args.push_back(Entry);
4720 // FIXME: pass in SDLoc
4721 TargetLowering::CallLoweringInfo CLI(*this);
4722 CLI.setDebugLoc(dl)
4723 .setChain(Chain)
4724 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
4725 Dst.getValueType().getTypeForEVT(*getContext()),
4726 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
4727 TLI->getPointerTy(getDataLayout())),
4728 std::move(Args))
4729 .setDiscardResult()
4730 .setTailCall(isTailCall);
4731
4732 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
4733 return CallResult.second;
4734 }
4735
getMemset(SDValue Chain,const SDLoc & dl,SDValue Dst,SDValue Src,SDValue Size,unsigned Align,bool isVol,bool isTailCall,MachinePointerInfo DstPtrInfo)4736 SDValue SelectionDAG::getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst,
4737 SDValue Src, SDValue Size, unsigned Align,
4738 bool isVol, bool isTailCall,
4739 MachinePointerInfo DstPtrInfo) {
4740 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
4741
4742 // Check to see if we should lower the memset to stores first.
4743 // For cases within the target-specified limits, this is the best choice.
4744 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4745 if (ConstantSize) {
4746 // Memset with size zero? Just return the original chain.
4747 if (ConstantSize->isNullValue())
4748 return Chain;
4749
4750 SDValue Result =
4751 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
4752 Align, isVol, DstPtrInfo);
4753
4754 if (Result.getNode())
4755 return Result;
4756 }
4757
4758 // Then check to see if we should lower the memset with target-specific
4759 // code. If the target chooses to do this, this is the next best.
4760 if (TSI) {
4761 SDValue Result = TSI->EmitTargetCodeForMemset(
4762 *this, dl, Chain, Dst, Src, Size, Align, isVol, DstPtrInfo);
4763 if (Result.getNode())
4764 return Result;
4765 }
4766
4767 checkAddrSpaceIsValidForLibcall(TLI, DstPtrInfo.getAddrSpace());
4768
4769 // Emit a library call.
4770 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
4771 TargetLowering::ArgListTy Args;
4772 TargetLowering::ArgListEntry Entry;
4773 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4774 Args.push_back(Entry);
4775 Entry.Node = Src;
4776 Entry.Ty = Src.getValueType().getTypeForEVT(*getContext());
4777 Args.push_back(Entry);
4778 Entry.Node = Size;
4779 Entry.Ty = IntPtrTy;
4780 Args.push_back(Entry);
4781
4782 // FIXME: pass in SDLoc
4783 TargetLowering::CallLoweringInfo CLI(*this);
4784 CLI.setDebugLoc(dl)
4785 .setChain(Chain)
4786 .setCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
4787 Dst.getValueType().getTypeForEVT(*getContext()),
4788 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
4789 TLI->getPointerTy(getDataLayout())),
4790 std::move(Args))
4791 .setDiscardResult()
4792 .setTailCall(isTailCall);
4793
4794 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
4795 return CallResult.second;
4796 }
4797
getAtomic(unsigned Opcode,const SDLoc & dl,EVT MemVT,SDVTList VTList,ArrayRef<SDValue> Ops,MachineMemOperand * MMO,AtomicOrdering SuccessOrdering,AtomicOrdering FailureOrdering,SynchronizationScope SynchScope)4798 SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
4799 SDVTList VTList, ArrayRef<SDValue> Ops,
4800 MachineMemOperand *MMO,
4801 AtomicOrdering SuccessOrdering,
4802 AtomicOrdering FailureOrdering,
4803 SynchronizationScope SynchScope) {
4804 FoldingSetNodeID ID;
4805 ID.AddInteger(MemVT.getRawBits());
4806 AddNodeIDNode(ID, Opcode, VTList, Ops);
4807 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
4808 void* IP = nullptr;
4809 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
4810 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4811 return SDValue(E, 0);
4812 }
4813
4814 auto *N = newSDNode<AtomicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
4815 VTList, MemVT, MMO, SuccessOrdering,
4816 FailureOrdering, SynchScope);
4817 createOperands(N, Ops);
4818
4819 CSEMap.InsertNode(N, IP);
4820 InsertNode(N);
4821 return SDValue(N, 0);
4822 }
4823
getAtomic(unsigned Opcode,const SDLoc & dl,EVT MemVT,SDVTList VTList,ArrayRef<SDValue> Ops,MachineMemOperand * MMO,AtomicOrdering Ordering,SynchronizationScope SynchScope)4824 SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
4825 SDVTList VTList, ArrayRef<SDValue> Ops,
4826 MachineMemOperand *MMO, AtomicOrdering Ordering,
4827 SynchronizationScope SynchScope) {
4828 return getAtomic(Opcode, dl, MemVT, VTList, Ops, MMO, Ordering,
4829 Ordering, SynchScope);
4830 }
4831
getAtomicCmpSwap(unsigned Opcode,const SDLoc & dl,EVT MemVT,SDVTList VTs,SDValue Chain,SDValue Ptr,SDValue Cmp,SDValue Swp,MachinePointerInfo PtrInfo,unsigned Alignment,AtomicOrdering SuccessOrdering,AtomicOrdering FailureOrdering,SynchronizationScope SynchScope)4832 SDValue SelectionDAG::getAtomicCmpSwap(
4833 unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain,
4834 SDValue Ptr, SDValue Cmp, SDValue Swp, MachinePointerInfo PtrInfo,
4835 unsigned Alignment, AtomicOrdering SuccessOrdering,
4836 AtomicOrdering FailureOrdering, SynchronizationScope SynchScope) {
4837 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4838 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4839 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4840
4841 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4842 Alignment = getEVTAlignment(MemVT);
4843
4844 MachineFunction &MF = getMachineFunction();
4845
4846 // FIXME: Volatile isn't really correct; we should keep track of atomic
4847 // orderings in the memoperand.
4848 unsigned Flags = MachineMemOperand::MOVolatile;
4849 Flags |= MachineMemOperand::MOLoad;
4850 Flags |= MachineMemOperand::MOStore;
4851
4852 MachineMemOperand *MMO =
4853 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
4854
4855 return getAtomicCmpSwap(Opcode, dl, MemVT, VTs, Chain, Ptr, Cmp, Swp, MMO,
4856 SuccessOrdering, FailureOrdering, SynchScope);
4857 }
4858
getAtomicCmpSwap(unsigned Opcode,const SDLoc & dl,EVT MemVT,SDVTList VTs,SDValue Chain,SDValue Ptr,SDValue Cmp,SDValue Swp,MachineMemOperand * MMO,AtomicOrdering SuccessOrdering,AtomicOrdering FailureOrdering,SynchronizationScope SynchScope)4859 SDValue SelectionDAG::getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl,
4860 EVT MemVT, SDVTList VTs, SDValue Chain,
4861 SDValue Ptr, SDValue Cmp, SDValue Swp,
4862 MachineMemOperand *MMO,
4863 AtomicOrdering SuccessOrdering,
4864 AtomicOrdering FailureOrdering,
4865 SynchronizationScope SynchScope) {
4866 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
4867 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
4868 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4869
4870 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
4871 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO,
4872 SuccessOrdering, FailureOrdering, SynchScope);
4873 }
4874
getAtomic(unsigned Opcode,const SDLoc & dl,EVT MemVT,SDValue Chain,SDValue Ptr,SDValue Val,const Value * PtrVal,unsigned Alignment,AtomicOrdering Ordering,SynchronizationScope SynchScope)4875 SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
4876 SDValue Chain, SDValue Ptr, SDValue Val,
4877 const Value *PtrVal, unsigned Alignment,
4878 AtomicOrdering Ordering,
4879 SynchronizationScope SynchScope) {
4880 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4881 Alignment = getEVTAlignment(MemVT);
4882
4883 MachineFunction &MF = getMachineFunction();
4884 // An atomic store does not load. An atomic load does not store.
4885 // (An atomicrmw obviously both loads and stores.)
4886 // For now, atomics are considered to be volatile always, and they are
4887 // chained as such.
4888 // FIXME: Volatile isn't really correct; we should keep track of atomic
4889 // orderings in the memoperand.
4890 unsigned Flags = MachineMemOperand::MOVolatile;
4891 if (Opcode != ISD::ATOMIC_STORE)
4892 Flags |= MachineMemOperand::MOLoad;
4893 if (Opcode != ISD::ATOMIC_LOAD)
4894 Flags |= MachineMemOperand::MOStore;
4895
4896 MachineMemOperand *MMO =
4897 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4898 MemVT.getStoreSize(), Alignment);
4899
4900 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4901 Ordering, SynchScope);
4902 }
4903
getAtomic(unsigned Opcode,const SDLoc & dl,EVT MemVT,SDValue Chain,SDValue Ptr,SDValue Val,MachineMemOperand * MMO,AtomicOrdering Ordering,SynchronizationScope SynchScope)4904 SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
4905 SDValue Chain, SDValue Ptr, SDValue Val,
4906 MachineMemOperand *MMO, AtomicOrdering Ordering,
4907 SynchronizationScope SynchScope) {
4908 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4909 Opcode == ISD::ATOMIC_LOAD_SUB ||
4910 Opcode == ISD::ATOMIC_LOAD_AND ||
4911 Opcode == ISD::ATOMIC_LOAD_OR ||
4912 Opcode == ISD::ATOMIC_LOAD_XOR ||
4913 Opcode == ISD::ATOMIC_LOAD_NAND ||
4914 Opcode == ISD::ATOMIC_LOAD_MIN ||
4915 Opcode == ISD::ATOMIC_LOAD_MAX ||
4916 Opcode == ISD::ATOMIC_LOAD_UMIN ||
4917 Opcode == ISD::ATOMIC_LOAD_UMAX ||
4918 Opcode == ISD::ATOMIC_SWAP ||
4919 Opcode == ISD::ATOMIC_STORE) &&
4920 "Invalid Atomic Op");
4921
4922 EVT VT = Val.getValueType();
4923
4924 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4925 getVTList(VT, MVT::Other);
4926 SDValue Ops[] = {Chain, Ptr, Val};
4927 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
4928 }
4929
getAtomic(unsigned Opcode,const SDLoc & dl,EVT MemVT,EVT VT,SDValue Chain,SDValue Ptr,MachineMemOperand * MMO,AtomicOrdering Ordering,SynchronizationScope SynchScope)4930 SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
4931 EVT VT, SDValue Chain, SDValue Ptr,
4932 MachineMemOperand *MMO, AtomicOrdering Ordering,
4933 SynchronizationScope SynchScope) {
4934 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4935
4936 SDVTList VTs = getVTList(VT, MVT::Other);
4937 SDValue Ops[] = {Chain, Ptr};
4938 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO, Ordering, SynchScope);
4939 }
4940
4941 /// getMergeValues - Create a MERGE_VALUES node from the given operands.
getMergeValues(ArrayRef<SDValue> Ops,const SDLoc & dl)4942 SDValue SelectionDAG::getMergeValues(ArrayRef<SDValue> Ops, const SDLoc &dl) {
4943 if (Ops.size() == 1)
4944 return Ops[0];
4945
4946 SmallVector<EVT, 4> VTs;
4947 VTs.reserve(Ops.size());
4948 for (unsigned i = 0; i < Ops.size(); ++i)
4949 VTs.push_back(Ops[i].getValueType());
4950 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
4951 }
4952
getMemIntrinsicNode(unsigned Opcode,const SDLoc & dl,SDVTList VTList,ArrayRef<SDValue> Ops,EVT MemVT,MachinePointerInfo PtrInfo,unsigned Align,bool Vol,bool ReadMem,bool WriteMem,unsigned Size)4953 SDValue SelectionDAG::getMemIntrinsicNode(
4954 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
4955 EVT MemVT, MachinePointerInfo PtrInfo, unsigned Align, bool Vol,
4956 bool ReadMem, bool WriteMem, unsigned Size) {
4957 if (Align == 0) // Ensure that codegen never sees alignment 0
4958 Align = getEVTAlignment(MemVT);
4959
4960 MachineFunction &MF = getMachineFunction();
4961 unsigned Flags = 0;
4962 if (WriteMem)
4963 Flags |= MachineMemOperand::MOStore;
4964 if (ReadMem)
4965 Flags |= MachineMemOperand::MOLoad;
4966 if (Vol)
4967 Flags |= MachineMemOperand::MOVolatile;
4968 if (!Size)
4969 Size = MemVT.getStoreSize();
4970 MachineMemOperand *MMO =
4971 MF.getMachineMemOperand(PtrInfo, Flags, Size, Align);
4972
4973 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
4974 }
4975
getMemIntrinsicNode(unsigned Opcode,const SDLoc & dl,SDVTList VTList,ArrayRef<SDValue> Ops,EVT MemVT,MachineMemOperand * MMO)4976 SDValue SelectionDAG::getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl,
4977 SDVTList VTList,
4978 ArrayRef<SDValue> Ops, EVT MemVT,
4979 MachineMemOperand *MMO) {
4980 assert((Opcode == ISD::INTRINSIC_VOID ||
4981 Opcode == ISD::INTRINSIC_W_CHAIN ||
4982 Opcode == ISD::PREFETCH ||
4983 Opcode == ISD::LIFETIME_START ||
4984 Opcode == ISD::LIFETIME_END ||
4985 (Opcode <= INT_MAX &&
4986 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4987 "Opcode is not a memory-accessing opcode!");
4988
4989 // Memoize the node unless it returns a flag.
4990 MemIntrinsicSDNode *N;
4991 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
4992 FoldingSetNodeID ID;
4993 AddNodeIDNode(ID, Opcode, VTList, Ops);
4994 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
4995 void *IP = nullptr;
4996 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
4997 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
4998 return SDValue(E, 0);
4999 }
5000
5001 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
5002 VTList, MemVT, MMO);
5003 createOperands(N, Ops);
5004
5005 CSEMap.InsertNode(N, IP);
5006 } else {
5007 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
5008 VTList, MemVT, MMO);
5009 createOperands(N, Ops);
5010 }
5011 InsertNode(N);
5012 return SDValue(N, 0);
5013 }
5014
5015 /// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
5016 /// MachinePointerInfo record from it. This is particularly useful because the
5017 /// code generator has many cases where it doesn't bother passing in a
5018 /// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
InferPointerInfo(SelectionDAG & DAG,SDValue Ptr,int64_t Offset=0)5019 static MachinePointerInfo InferPointerInfo(SelectionDAG &DAG, SDValue Ptr,
5020 int64_t Offset = 0) {
5021 // If this is FI+Offset, we can model it.
5022 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
5023 return MachinePointerInfo::getFixedStack(DAG.getMachineFunction(),
5024 FI->getIndex(), Offset);
5025
5026 // If this is (FI+Offset1)+Offset2, we can model it.
5027 if (Ptr.getOpcode() != ISD::ADD ||
5028 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
5029 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
5030 return MachinePointerInfo();
5031
5032 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
5033 return MachinePointerInfo::getFixedStack(
5034 DAG.getMachineFunction(), FI,
5035 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
5036 }
5037
5038 /// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
5039 /// MachinePointerInfo record from it. This is particularly useful because the
5040 /// code generator has many cases where it doesn't bother passing in a
5041 /// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
InferPointerInfo(SelectionDAG & DAG,SDValue Ptr,SDValue OffsetOp)5042 static MachinePointerInfo InferPointerInfo(SelectionDAG &DAG, SDValue Ptr,
5043 SDValue OffsetOp) {
5044 // If the 'Offset' value isn't a constant, we can't handle this.
5045 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
5046 return InferPointerInfo(DAG, Ptr, OffsetNode->getSExtValue());
5047 if (OffsetOp.isUndef())
5048 return InferPointerInfo(DAG, Ptr);
5049 return MachinePointerInfo();
5050 }
5051
getLoad(ISD::MemIndexedMode AM,ISD::LoadExtType ExtType,EVT VT,const SDLoc & dl,SDValue Chain,SDValue Ptr,SDValue Offset,MachinePointerInfo PtrInfo,EVT MemVT,bool isVolatile,bool isNonTemporal,bool isInvariant,unsigned Alignment,const AAMDNodes & AAInfo,const MDNode * Ranges)5052 SDValue SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
5053 EVT VT, const SDLoc &dl, SDValue Chain,
5054 SDValue Ptr, SDValue Offset,
5055 MachinePointerInfo PtrInfo, EVT MemVT,
5056 bool isVolatile, bool isNonTemporal,
5057 bool isInvariant, unsigned Alignment,
5058 const AAMDNodes &AAInfo, const MDNode *Ranges) {
5059 assert(Chain.getValueType() == MVT::Other &&
5060 "Invalid chain type");
5061 if (Alignment == 0) // Ensure that codegen never sees alignment 0
5062 Alignment = getEVTAlignment(VT);
5063
5064 unsigned Flags = MachineMemOperand::MOLoad;
5065 if (isVolatile)
5066 Flags |= MachineMemOperand::MOVolatile;
5067 if (isNonTemporal)
5068 Flags |= MachineMemOperand::MONonTemporal;
5069 if (isInvariant)
5070 Flags |= MachineMemOperand::MOInvariant;
5071
5072 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
5073 // clients.
5074 if (PtrInfo.V.isNull())
5075 PtrInfo = InferPointerInfo(*this, Ptr, Offset);
5076
5077 MachineFunction &MF = getMachineFunction();
5078 MachineMemOperand *MMO =
5079 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
5080 AAInfo, Ranges);
5081 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
5082 }
5083
getLoad(ISD::MemIndexedMode AM,ISD::LoadExtType ExtType,EVT VT,const SDLoc & dl,SDValue Chain,SDValue Ptr,SDValue Offset,EVT MemVT,MachineMemOperand * MMO)5084 SDValue SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
5085 EVT VT, const SDLoc &dl, SDValue Chain,
5086 SDValue Ptr, SDValue Offset, EVT MemVT,
5087 MachineMemOperand *MMO) {
5088 if (VT == MemVT) {
5089 ExtType = ISD::NON_EXTLOAD;
5090 } else if (ExtType == ISD::NON_EXTLOAD) {
5091 assert(VT == MemVT && "Non-extending load from different memory type!");
5092 } else {
5093 // Extending load.
5094 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
5095 "Should only be an extending load, not truncating!");
5096 assert(VT.isInteger() == MemVT.isInteger() &&
5097 "Cannot convert from FP to Int or Int -> FP!");
5098 assert(VT.isVector() == MemVT.isVector() &&
5099 "Cannot use an ext load to convert to or from a vector!");
5100 assert((!VT.isVector() ||
5101 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
5102 "Cannot use an ext load to change the number of vector elements!");
5103 }
5104
5105 bool Indexed = AM != ISD::UNINDEXED;
5106 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
5107
5108 SDVTList VTs = Indexed ?
5109 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
5110 SDValue Ops[] = { Chain, Ptr, Offset };
5111 FoldingSetNodeID ID;
5112 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
5113 ID.AddInteger(MemVT.getRawBits());
5114 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
5115 MMO->isNonTemporal(),
5116 MMO->isInvariant()));
5117 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5118 void *IP = nullptr;
5119 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
5120 cast<LoadSDNode>(E)->refineAlignment(MMO);
5121 return SDValue(E, 0);
5122 }
5123 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
5124 ExtType, MemVT, MMO);
5125 createOperands(N, Ops);
5126
5127 CSEMap.InsertNode(N, IP);
5128 InsertNode(N);
5129 return SDValue(N, 0);
5130 }
5131
getLoad(EVT VT,const SDLoc & dl,SDValue Chain,SDValue Ptr,MachinePointerInfo PtrInfo,bool isVolatile,bool isNonTemporal,bool isInvariant,unsigned Alignment,const AAMDNodes & AAInfo,const MDNode * Ranges)5132 SDValue SelectionDAG::getLoad(EVT VT, const SDLoc &dl, SDValue Chain,
5133 SDValue Ptr, MachinePointerInfo PtrInfo,
5134 bool isVolatile, bool isNonTemporal,
5135 bool isInvariant, unsigned Alignment,
5136 const AAMDNodes &AAInfo, const MDNode *Ranges) {
5137 SDValue Undef = getUNDEF(Ptr.getValueType());
5138 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
5139 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
5140 AAInfo, Ranges);
5141 }
5142
getLoad(EVT VT,const SDLoc & dl,SDValue Chain,SDValue Ptr,MachineMemOperand * MMO)5143 SDValue SelectionDAG::getLoad(EVT VT, const SDLoc &dl, SDValue Chain,
5144 SDValue Ptr, MachineMemOperand *MMO) {
5145 SDValue Undef = getUNDEF(Ptr.getValueType());
5146 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
5147 VT, MMO);
5148 }
5149
getExtLoad(ISD::LoadExtType ExtType,const SDLoc & dl,EVT VT,SDValue Chain,SDValue Ptr,MachinePointerInfo PtrInfo,EVT MemVT,bool isVolatile,bool isNonTemporal,bool isInvariant,unsigned Alignment,const AAMDNodes & AAInfo)5150 SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl,
5151 EVT VT, SDValue Chain, SDValue Ptr,
5152 MachinePointerInfo PtrInfo, EVT MemVT,
5153 bool isVolatile, bool isNonTemporal,
5154 bool isInvariant, unsigned Alignment,
5155 const AAMDNodes &AAInfo) {
5156 SDValue Undef = getUNDEF(Ptr.getValueType());
5157 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
5158 PtrInfo, MemVT, isVolatile, isNonTemporal, isInvariant,
5159 Alignment, AAInfo);
5160 }
5161
getExtLoad(ISD::LoadExtType ExtType,const SDLoc & dl,EVT VT,SDValue Chain,SDValue Ptr,EVT MemVT,MachineMemOperand * MMO)5162 SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl,
5163 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
5164 MachineMemOperand *MMO) {
5165 SDValue Undef = getUNDEF(Ptr.getValueType());
5166 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
5167 MemVT, MMO);
5168 }
5169
getIndexedLoad(SDValue OrigLoad,const SDLoc & dl,SDValue Base,SDValue Offset,ISD::MemIndexedMode AM)5170 SDValue SelectionDAG::getIndexedLoad(SDValue OrigLoad, const SDLoc &dl,
5171 SDValue Base, SDValue Offset,
5172 ISD::MemIndexedMode AM) {
5173 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
5174 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
5175 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
5176 LD->getChain(), Base, Offset, LD->getPointerInfo(),
5177 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
5178 false, LD->getAlignment());
5179 }
5180
getStore(SDValue Chain,const SDLoc & dl,SDValue Val,SDValue Ptr,MachinePointerInfo PtrInfo,bool isVolatile,bool isNonTemporal,unsigned Alignment,const AAMDNodes & AAInfo)5181 SDValue SelectionDAG::getStore(SDValue Chain, const SDLoc &dl, SDValue Val,
5182 SDValue Ptr, MachinePointerInfo PtrInfo,
5183 bool isVolatile, bool isNonTemporal,
5184 unsigned Alignment, const AAMDNodes &AAInfo) {
5185 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
5186 if (Alignment == 0) // Ensure that codegen never sees alignment 0
5187 Alignment = getEVTAlignment(Val.getValueType());
5188
5189 unsigned Flags = MachineMemOperand::MOStore;
5190 if (isVolatile)
5191 Flags |= MachineMemOperand::MOVolatile;
5192 if (isNonTemporal)
5193 Flags |= MachineMemOperand::MONonTemporal;
5194
5195 if (PtrInfo.V.isNull())
5196 PtrInfo = InferPointerInfo(*this, Ptr);
5197
5198 MachineFunction &MF = getMachineFunction();
5199 MachineMemOperand *MMO =
5200 MF.getMachineMemOperand(PtrInfo, Flags,
5201 Val.getValueType().getStoreSize(), Alignment,
5202 AAInfo);
5203
5204 return getStore(Chain, dl, Val, Ptr, MMO);
5205 }
5206
getStore(SDValue Chain,const SDLoc & dl,SDValue Val,SDValue Ptr,MachineMemOperand * MMO)5207 SDValue SelectionDAG::getStore(SDValue Chain, const SDLoc &dl, SDValue Val,
5208 SDValue Ptr, MachineMemOperand *MMO) {
5209 assert(Chain.getValueType() == MVT::Other &&
5210 "Invalid chain type");
5211 EVT VT = Val.getValueType();
5212 SDVTList VTs = getVTList(MVT::Other);
5213 SDValue Undef = getUNDEF(Ptr.getValueType());
5214 SDValue Ops[] = { Chain, Val, Ptr, Undef };
5215 FoldingSetNodeID ID;
5216 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
5217 ID.AddInteger(VT.getRawBits());
5218 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5219 MMO->isNonTemporal(), MMO->isInvariant()));
5220 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5221 void *IP = nullptr;
5222 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
5223 cast<StoreSDNode>(E)->refineAlignment(MMO);
5224 return SDValue(E, 0);
5225 }
5226 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
5227 ISD::UNINDEXED, false, VT, MMO);
5228 createOperands(N, Ops);
5229
5230 CSEMap.InsertNode(N, IP);
5231 InsertNode(N);
5232 return SDValue(N, 0);
5233 }
5234
getTruncStore(SDValue Chain,const SDLoc & dl,SDValue Val,SDValue Ptr,MachinePointerInfo PtrInfo,EVT SVT,bool isVolatile,bool isNonTemporal,unsigned Alignment,const AAMDNodes & AAInfo)5235 SDValue SelectionDAG::getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val,
5236 SDValue Ptr, MachinePointerInfo PtrInfo,
5237 EVT SVT, bool isVolatile,
5238 bool isNonTemporal, unsigned Alignment,
5239 const AAMDNodes &AAInfo) {
5240 assert(Chain.getValueType() == MVT::Other &&
5241 "Invalid chain type");
5242 if (Alignment == 0) // Ensure that codegen never sees alignment 0
5243 Alignment = getEVTAlignment(SVT);
5244
5245 unsigned Flags = MachineMemOperand::MOStore;
5246 if (isVolatile)
5247 Flags |= MachineMemOperand::MOVolatile;
5248 if (isNonTemporal)
5249 Flags |= MachineMemOperand::MONonTemporal;
5250
5251 if (PtrInfo.V.isNull())
5252 PtrInfo = InferPointerInfo(*this, Ptr);
5253
5254 MachineFunction &MF = getMachineFunction();
5255 MachineMemOperand *MMO =
5256 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
5257 AAInfo);
5258
5259 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
5260 }
5261
getTruncStore(SDValue Chain,const SDLoc & dl,SDValue Val,SDValue Ptr,EVT SVT,MachineMemOperand * MMO)5262 SDValue SelectionDAG::getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val,
5263 SDValue Ptr, EVT SVT,
5264 MachineMemOperand *MMO) {
5265 EVT VT = Val.getValueType();
5266
5267 assert(Chain.getValueType() == MVT::Other &&
5268 "Invalid chain type");
5269 if (VT == SVT)
5270 return getStore(Chain, dl, Val, Ptr, MMO);
5271
5272 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
5273 "Should only be a truncating store, not extending!");
5274 assert(VT.isInteger() == SVT.isInteger() &&
5275 "Can't do FP-INT conversion!");
5276 assert(VT.isVector() == SVT.isVector() &&
5277 "Cannot use trunc store to convert to or from a vector!");
5278 assert((!VT.isVector() ||
5279 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
5280 "Cannot use trunc store to change the number of vector elements!");
5281
5282 SDVTList VTs = getVTList(MVT::Other);
5283 SDValue Undef = getUNDEF(Ptr.getValueType());
5284 SDValue Ops[] = { Chain, Val, Ptr, Undef };
5285 FoldingSetNodeID ID;
5286 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
5287 ID.AddInteger(SVT.getRawBits());
5288 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
5289 MMO->isNonTemporal(), MMO->isInvariant()));
5290 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5291 void *IP = nullptr;
5292 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
5293 cast<StoreSDNode>(E)->refineAlignment(MMO);
5294 return SDValue(E, 0);
5295 }
5296 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
5297 ISD::UNINDEXED, true, SVT, MMO);
5298 createOperands(N, Ops);
5299
5300 CSEMap.InsertNode(N, IP);
5301 InsertNode(N);
5302 return SDValue(N, 0);
5303 }
5304
getIndexedStore(SDValue OrigStore,const SDLoc & dl,SDValue Base,SDValue Offset,ISD::MemIndexedMode AM)5305 SDValue SelectionDAG::getIndexedStore(SDValue OrigStore, const SDLoc &dl,
5306 SDValue Base, SDValue Offset,
5307 ISD::MemIndexedMode AM) {
5308 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
5309 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
5310 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
5311 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
5312 FoldingSetNodeID ID;
5313 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
5314 ID.AddInteger(ST->getMemoryVT().getRawBits());
5315 ID.AddInteger(ST->getRawSubclassData());
5316 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
5317 void *IP = nullptr;
5318 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
5319 return SDValue(E, 0);
5320
5321 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
5322 ST->isTruncatingStore(), ST->getMemoryVT(),
5323 ST->getMemOperand());
5324 createOperands(N, Ops);
5325
5326 CSEMap.InsertNode(N, IP);
5327 InsertNode(N);
5328 return SDValue(N, 0);
5329 }
5330
getMaskedLoad(EVT VT,const SDLoc & dl,SDValue Chain,SDValue Ptr,SDValue Mask,SDValue Src0,EVT MemVT,MachineMemOperand * MMO,ISD::LoadExtType ExtTy)5331 SDValue SelectionDAG::getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain,
5332 SDValue Ptr, SDValue Mask, SDValue Src0,
5333 EVT MemVT, MachineMemOperand *MMO,
5334 ISD::LoadExtType ExtTy) {
5335
5336 SDVTList VTs = getVTList(VT, MVT::Other);
5337 SDValue Ops[] = { Chain, Ptr, Mask, Src0 };
5338 FoldingSetNodeID ID;
5339 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
5340 ID.AddInteger(VT.getRawBits());
5341 ID.AddInteger(encodeMemSDNodeFlags(ExtTy, ISD::UNINDEXED,
5342 MMO->isVolatile(),
5343 MMO->isNonTemporal(),
5344 MMO->isInvariant()));
5345 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5346 void *IP = nullptr;
5347 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
5348 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
5349 return SDValue(E, 0);
5350 }
5351 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
5352 ExtTy, MemVT, MMO);
5353 createOperands(N, Ops);
5354
5355 CSEMap.InsertNode(N, IP);
5356 InsertNode(N);
5357 return SDValue(N, 0);
5358 }
5359
getMaskedStore(SDValue Chain,const SDLoc & dl,SDValue Val,SDValue Ptr,SDValue Mask,EVT MemVT,MachineMemOperand * MMO,bool isTrunc)5360 SDValue SelectionDAG::getMaskedStore(SDValue Chain, const SDLoc &dl,
5361 SDValue Val, SDValue Ptr, SDValue Mask,
5362 EVT MemVT, MachineMemOperand *MMO,
5363 bool isTrunc) {
5364 assert(Chain.getValueType() == MVT::Other &&
5365 "Invalid chain type");
5366 EVT VT = Val.getValueType();
5367 SDVTList VTs = getVTList(MVT::Other);
5368 SDValue Ops[] = { Chain, Ptr, Mask, Val };
5369 FoldingSetNodeID ID;
5370 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
5371 ID.AddInteger(VT.getRawBits());
5372 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5373 MMO->isNonTemporal(), MMO->isInvariant()));
5374 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5375 void *IP = nullptr;
5376 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
5377 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
5378 return SDValue(E, 0);
5379 }
5380 auto *N = newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
5381 isTrunc, MemVT, MMO);
5382 createOperands(N, Ops);
5383
5384 CSEMap.InsertNode(N, IP);
5385 InsertNode(N);
5386 return SDValue(N, 0);
5387 }
5388
getMaskedGather(SDVTList VTs,EVT VT,const SDLoc & dl,ArrayRef<SDValue> Ops,MachineMemOperand * MMO)5389 SDValue SelectionDAG::getMaskedGather(SDVTList VTs, EVT VT, const SDLoc &dl,
5390 ArrayRef<SDValue> Ops,
5391 MachineMemOperand *MMO) {
5392 assert(Ops.size() == 5 && "Incompatible number of operands");
5393
5394 FoldingSetNodeID ID;
5395 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
5396 ID.AddInteger(VT.getRawBits());
5397 ID.AddInteger(encodeMemSDNodeFlags(ISD::NON_EXTLOAD, ISD::UNINDEXED,
5398 MMO->isVolatile(),
5399 MMO->isNonTemporal(),
5400 MMO->isInvariant()));
5401 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5402 void *IP = nullptr;
5403 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
5404 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
5405 return SDValue(E, 0);
5406 }
5407
5408 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
5409 VTs, VT, MMO);
5410 createOperands(N, Ops);
5411
5412 assert(N->getValue().getValueType() == N->getValueType(0) &&
5413 "Incompatible type of the PassThru value in MaskedGatherSDNode");
5414 assert(N->getMask().getValueType().getVectorNumElements() ==
5415 N->getValueType(0).getVectorNumElements() &&
5416 "Vector width mismatch between mask and data");
5417 assert(N->getIndex().getValueType().getVectorNumElements() ==
5418 N->getValueType(0).getVectorNumElements() &&
5419 "Vector width mismatch between index and data");
5420
5421 CSEMap.InsertNode(N, IP);
5422 InsertNode(N);
5423 return SDValue(N, 0);
5424 }
5425
getMaskedScatter(SDVTList VTs,EVT VT,const SDLoc & dl,ArrayRef<SDValue> Ops,MachineMemOperand * MMO)5426 SDValue SelectionDAG::getMaskedScatter(SDVTList VTs, EVT VT, const SDLoc &dl,
5427 ArrayRef<SDValue> Ops,
5428 MachineMemOperand *MMO) {
5429 assert(Ops.size() == 5 && "Incompatible number of operands");
5430
5431 FoldingSetNodeID ID;
5432 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
5433 ID.AddInteger(VT.getRawBits());
5434 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
5435 MMO->isNonTemporal(),
5436 MMO->isInvariant()));
5437 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
5438 void *IP = nullptr;
5439 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
5440 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
5441 return SDValue(E, 0);
5442 }
5443 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
5444 VTs, VT, MMO);
5445 createOperands(N, Ops);
5446
5447 assert(N->getMask().getValueType().getVectorNumElements() ==
5448 N->getValue().getValueType().getVectorNumElements() &&
5449 "Vector width mismatch between mask and data");
5450 assert(N->getIndex().getValueType().getVectorNumElements() ==
5451 N->getValue().getValueType().getVectorNumElements() &&
5452 "Vector width mismatch between index and data");
5453
5454 CSEMap.InsertNode(N, IP);
5455 InsertNode(N);
5456 return SDValue(N, 0);
5457 }
5458
getVAArg(EVT VT,const SDLoc & dl,SDValue Chain,SDValue Ptr,SDValue SV,unsigned Align)5459 SDValue SelectionDAG::getVAArg(EVT VT, const SDLoc &dl, SDValue Chain,
5460 SDValue Ptr, SDValue SV, unsigned Align) {
5461 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
5462 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
5463 }
5464
getNode(unsigned Opcode,const SDLoc & DL,EVT VT,ArrayRef<SDUse> Ops)5465 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5466 ArrayRef<SDUse> Ops) {
5467 switch (Ops.size()) {
5468 case 0: return getNode(Opcode, DL, VT);
5469 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
5470 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
5471 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
5472 default: break;
5473 }
5474
5475 // Copy from an SDUse array into an SDValue array for use with
5476 // the regular getNode logic.
5477 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
5478 return getNode(Opcode, DL, VT, NewOps);
5479 }
5480
getNode(unsigned Opcode,const SDLoc & DL,EVT VT,ArrayRef<SDValue> Ops,const SDNodeFlags * Flags)5481 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5482 ArrayRef<SDValue> Ops, const SDNodeFlags *Flags) {
5483 unsigned NumOps = Ops.size();
5484 switch (NumOps) {
5485 case 0: return getNode(Opcode, DL, VT);
5486 case 1: return getNode(Opcode, DL, VT, Ops[0]);
5487 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
5488 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
5489 default: break;
5490 }
5491
5492 switch (Opcode) {
5493 default: break;
5494 case ISD::CONCAT_VECTORS: {
5495 // Attempt to fold CONCAT_VECTORS into BUILD_VECTOR or UNDEF.
5496 if (SDValue V = FoldCONCAT_VECTORS(DL, VT, Ops, *this))
5497 return V;
5498 break;
5499 }
5500 case ISD::SELECT_CC: {
5501 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
5502 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
5503 "LHS and RHS of condition must have same type!");
5504 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5505 "True and False arms of SelectCC must have same type!");
5506 assert(Ops[2].getValueType() == VT &&
5507 "select_cc node must be of same type as true and false value!");
5508 break;
5509 }
5510 case ISD::BR_CC: {
5511 assert(NumOps == 5 && "BR_CC takes 5 operands!");
5512 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
5513 "LHS/RHS of comparison should match types!");
5514 break;
5515 }
5516 }
5517
5518 // Memoize nodes.
5519 SDNode *N;
5520 SDVTList VTs = getVTList(VT);
5521
5522 if (VT != MVT::Glue) {
5523 FoldingSetNodeID ID;
5524 AddNodeIDNode(ID, Opcode, VTs, Ops);
5525 void *IP = nullptr;
5526
5527 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
5528 return SDValue(E, 0);
5529
5530 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
5531 createOperands(N, Ops);
5532
5533 CSEMap.InsertNode(N, IP);
5534 } else {
5535 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
5536 createOperands(N, Ops);
5537 }
5538
5539 InsertNode(N);
5540 return SDValue(N, 0);
5541 }
5542
getNode(unsigned Opcode,const SDLoc & DL,ArrayRef<EVT> ResultTys,ArrayRef<SDValue> Ops)5543 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
5544 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
5545 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
5546 }
5547
getNode(unsigned Opcode,const SDLoc & DL,SDVTList VTList,ArrayRef<SDValue> Ops)5548 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
5549 ArrayRef<SDValue> Ops) {
5550 if (VTList.NumVTs == 1)
5551 return getNode(Opcode, DL, VTList.VTs[0], Ops);
5552
5553 #if 0
5554 switch (Opcode) {
5555 // FIXME: figure out how to safely handle things like
5556 // int foo(int x) { return 1 << (x & 255); }
5557 // int bar() { return foo(256); }
5558 case ISD::SRA_PARTS:
5559 case ISD::SRL_PARTS:
5560 case ISD::SHL_PARTS:
5561 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
5562 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
5563 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
5564 else if (N3.getOpcode() == ISD::AND)
5565 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
5566 // If the and is only masking out bits that cannot effect the shift,
5567 // eliminate the and.
5568 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
5569 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
5570 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
5571 }
5572 break;
5573 }
5574 #endif
5575
5576 // Memoize the node unless it returns a flag.
5577 SDNode *N;
5578 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
5579 FoldingSetNodeID ID;
5580 AddNodeIDNode(ID, Opcode, VTList, Ops);
5581 void *IP = nullptr;
5582 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
5583 return SDValue(E, 0);
5584
5585 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
5586 createOperands(N, Ops);
5587 CSEMap.InsertNode(N, IP);
5588 } else {
5589 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
5590 createOperands(N, Ops);
5591 }
5592 InsertNode(N);
5593 return SDValue(N, 0);
5594 }
5595
getNode(unsigned Opcode,const SDLoc & DL,SDVTList VTList)5596 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
5597 SDVTList VTList) {
5598 return getNode(Opcode, DL, VTList, None);
5599 }
5600
getNode(unsigned Opcode,const SDLoc & DL,SDVTList VTList,SDValue N1)5601 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
5602 SDValue N1) {
5603 SDValue Ops[] = { N1 };
5604 return getNode(Opcode, DL, VTList, Ops);
5605 }
5606
getNode(unsigned Opcode,const SDLoc & DL,SDVTList VTList,SDValue N1,SDValue N2)5607 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
5608 SDValue N1, SDValue N2) {
5609 SDValue Ops[] = { N1, N2 };
5610 return getNode(Opcode, DL, VTList, Ops);
5611 }
5612
getNode(unsigned Opcode,const SDLoc & DL,SDVTList VTList,SDValue N1,SDValue N2,SDValue N3)5613 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
5614 SDValue N1, SDValue N2, SDValue N3) {
5615 SDValue Ops[] = { N1, N2, N3 };
5616 return getNode(Opcode, DL, VTList, Ops);
5617 }
5618
getNode(unsigned Opcode,const SDLoc & DL,SDVTList VTList,SDValue N1,SDValue N2,SDValue N3,SDValue N4)5619 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
5620 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
5621 SDValue Ops[] = { N1, N2, N3, N4 };
5622 return getNode(Opcode, DL, VTList, Ops);
5623 }
5624
getNode(unsigned Opcode,const SDLoc & DL,SDVTList VTList,SDValue N1,SDValue N2,SDValue N3,SDValue N4,SDValue N5)5625 SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
5626 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
5627 SDValue N5) {
5628 SDValue Ops[] = { N1, N2, N3, N4, N5 };
5629 return getNode(Opcode, DL, VTList, Ops);
5630 }
5631
getVTList(EVT VT)5632 SDVTList SelectionDAG::getVTList(EVT VT) {
5633 return makeVTList(SDNode::getValueTypeList(VT), 1);
5634 }
5635
getVTList(EVT VT1,EVT VT2)5636 SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
5637 FoldingSetNodeID ID;
5638 ID.AddInteger(2U);
5639 ID.AddInteger(VT1.getRawBits());
5640 ID.AddInteger(VT2.getRawBits());
5641
5642 void *IP = nullptr;
5643 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5644 if (!Result) {
5645 EVT *Array = Allocator.Allocate<EVT>(2);
5646 Array[0] = VT1;
5647 Array[1] = VT2;
5648 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
5649 VTListMap.InsertNode(Result, IP);
5650 }
5651 return Result->getSDVTList();
5652 }
5653
getVTList(EVT VT1,EVT VT2,EVT VT3)5654 SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
5655 FoldingSetNodeID ID;
5656 ID.AddInteger(3U);
5657 ID.AddInteger(VT1.getRawBits());
5658 ID.AddInteger(VT2.getRawBits());
5659 ID.AddInteger(VT3.getRawBits());
5660
5661 void *IP = nullptr;
5662 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5663 if (!Result) {
5664 EVT *Array = Allocator.Allocate<EVT>(3);
5665 Array[0] = VT1;
5666 Array[1] = VT2;
5667 Array[2] = VT3;
5668 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5669 VTListMap.InsertNode(Result, IP);
5670 }
5671 return Result->getSDVTList();
5672 }
5673
getVTList(EVT VT1,EVT VT2,EVT VT3,EVT VT4)5674 SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
5675 FoldingSetNodeID ID;
5676 ID.AddInteger(4U);
5677 ID.AddInteger(VT1.getRawBits());
5678 ID.AddInteger(VT2.getRawBits());
5679 ID.AddInteger(VT3.getRawBits());
5680 ID.AddInteger(VT4.getRawBits());
5681
5682 void *IP = nullptr;
5683 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5684 if (!Result) {
5685 EVT *Array = Allocator.Allocate<EVT>(4);
5686 Array[0] = VT1;
5687 Array[1] = VT2;
5688 Array[2] = VT3;
5689 Array[3] = VT4;
5690 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5691 VTListMap.InsertNode(Result, IP);
5692 }
5693 return Result->getSDVTList();
5694 }
5695
getVTList(ArrayRef<EVT> VTs)5696 SDVTList SelectionDAG::getVTList(ArrayRef<EVT> VTs) {
5697 unsigned NumVTs = VTs.size();
5698 FoldingSetNodeID ID;
5699 ID.AddInteger(NumVTs);
5700 for (unsigned index = 0; index < NumVTs; index++) {
5701 ID.AddInteger(VTs[index].getRawBits());
5702 }
5703
5704 void *IP = nullptr;
5705 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5706 if (!Result) {
5707 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
5708 std::copy(VTs.begin(), VTs.end(), Array);
5709 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5710 VTListMap.InsertNode(Result, IP);
5711 }
5712 return Result->getSDVTList();
5713 }
5714
5715
5716 /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5717 /// specified operands. If the resultant node already exists in the DAG,
5718 /// this does not modify the specified node, instead it returns the node that
5719 /// already exists. If the resultant node does not exist in the DAG, the
5720 /// input node is returned. As a degenerate case, if you specify the same
5721 /// input operands as the node already has, the input node is returned.
UpdateNodeOperands(SDNode * N,SDValue Op)5722 SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
5723 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
5724
5725 // Check to see if there is no change.
5726 if (Op == N->getOperand(0)) return N;
5727
5728 // See if the modified node already exists.
5729 void *InsertPos = nullptr;
5730 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
5731 return Existing;
5732
5733 // Nope it doesn't. Remove the node from its current place in the maps.
5734 if (InsertPos)
5735 if (!RemoveNodeFromCSEMaps(N))
5736 InsertPos = nullptr;
5737
5738 // Now we update the operands.
5739 N->OperandList[0].set(Op);
5740
5741 // If this gets put into a CSE map, add it.
5742 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
5743 return N;
5744 }
5745
UpdateNodeOperands(SDNode * N,SDValue Op1,SDValue Op2)5746 SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
5747 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
5748
5749 // Check to see if there is no change.
5750 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
5751 return N; // No operands changed, just return the input node.
5752
5753 // See if the modified node already exists.
5754 void *InsertPos = nullptr;
5755 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
5756 return Existing;
5757
5758 // Nope it doesn't. Remove the node from its current place in the maps.
5759 if (InsertPos)
5760 if (!RemoveNodeFromCSEMaps(N))
5761 InsertPos = nullptr;
5762
5763 // Now we update the operands.
5764 if (N->OperandList[0] != Op1)
5765 N->OperandList[0].set(Op1);
5766 if (N->OperandList[1] != Op2)
5767 N->OperandList[1].set(Op2);
5768
5769 // If this gets put into a CSE map, add it.
5770 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
5771 return N;
5772 }
5773
5774 SDNode *SelectionDAG::
UpdateNodeOperands(SDNode * N,SDValue Op1,SDValue Op2,SDValue Op3)5775 UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
5776 SDValue Ops[] = { Op1, Op2, Op3 };
5777 return UpdateNodeOperands(N, Ops);
5778 }
5779
5780 SDNode *SelectionDAG::
UpdateNodeOperands(SDNode * N,SDValue Op1,SDValue Op2,SDValue Op3,SDValue Op4)5781 UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
5782 SDValue Op3, SDValue Op4) {
5783 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
5784 return UpdateNodeOperands(N, Ops);
5785 }
5786
5787 SDNode *SelectionDAG::
UpdateNodeOperands(SDNode * N,SDValue Op1,SDValue Op2,SDValue Op3,SDValue Op4,SDValue Op5)5788 UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
5789 SDValue Op3, SDValue Op4, SDValue Op5) {
5790 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
5791 return UpdateNodeOperands(N, Ops);
5792 }
5793
5794 SDNode *SelectionDAG::
UpdateNodeOperands(SDNode * N,ArrayRef<SDValue> Ops)5795 UpdateNodeOperands(SDNode *N, ArrayRef<SDValue> Ops) {
5796 unsigned NumOps = Ops.size();
5797 assert(N->getNumOperands() == NumOps &&
5798 "Update with wrong number of operands");
5799
5800 // If no operands changed just return the input node.
5801 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
5802 return N;
5803
5804 // See if the modified node already exists.
5805 void *InsertPos = nullptr;
5806 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
5807 return Existing;
5808
5809 // Nope it doesn't. Remove the node from its current place in the maps.
5810 if (InsertPos)
5811 if (!RemoveNodeFromCSEMaps(N))
5812 InsertPos = nullptr;
5813
5814 // Now we update the operands.
5815 for (unsigned i = 0; i != NumOps; ++i)
5816 if (N->OperandList[i] != Ops[i])
5817 N->OperandList[i].set(Ops[i]);
5818
5819 // If this gets put into a CSE map, add it.
5820 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
5821 return N;
5822 }
5823
5824 /// DropOperands - Release the operands and set this node to have
5825 /// zero operands.
DropOperands()5826 void SDNode::DropOperands() {
5827 // Unlike the code in MorphNodeTo that does this, we don't need to
5828 // watch for dead nodes here.
5829 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5830 SDUse &Use = *I++;
5831 Use.set(SDValue());
5832 }
5833 }
5834
5835 /// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5836 /// machine opcode.
5837 ///
SelectNodeTo(SDNode * N,unsigned MachineOpc,EVT VT)5838 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5839 EVT VT) {
5840 SDVTList VTs = getVTList(VT);
5841 return SelectNodeTo(N, MachineOpc, VTs, None);
5842 }
5843
SelectNodeTo(SDNode * N,unsigned MachineOpc,EVT VT,SDValue Op1)5844 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5845 EVT VT, SDValue Op1) {
5846 SDVTList VTs = getVTList(VT);
5847 SDValue Ops[] = { Op1 };
5848 return SelectNodeTo(N, MachineOpc, VTs, Ops);
5849 }
5850
SelectNodeTo(SDNode * N,unsigned MachineOpc,EVT VT,SDValue Op1,SDValue Op2)5851 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5852 EVT VT, SDValue Op1,
5853 SDValue Op2) {
5854 SDVTList VTs = getVTList(VT);
5855 SDValue Ops[] = { Op1, Op2 };
5856 return SelectNodeTo(N, MachineOpc, VTs, Ops);
5857 }
5858
SelectNodeTo(SDNode * N,unsigned MachineOpc,EVT VT,SDValue Op1,SDValue Op2,SDValue Op3)5859 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5860 EVT VT, SDValue Op1,
5861 SDValue Op2, SDValue Op3) {
5862 SDVTList VTs = getVTList(VT);
5863 SDValue Ops[] = { Op1, Op2, Op3 };
5864 return SelectNodeTo(N, MachineOpc, VTs, Ops);
5865 }
5866
SelectNodeTo(SDNode * N,unsigned MachineOpc,EVT VT,ArrayRef<SDValue> Ops)5867 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5868 EVT VT, ArrayRef<SDValue> Ops) {
5869 SDVTList VTs = getVTList(VT);
5870 return SelectNodeTo(N, MachineOpc, VTs, Ops);
5871 }
5872
SelectNodeTo(SDNode * N,unsigned MachineOpc,EVT VT1,EVT VT2,ArrayRef<SDValue> Ops)5873 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5874 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
5875 SDVTList VTs = getVTList(VT1, VT2);
5876 return SelectNodeTo(N, MachineOpc, VTs, Ops);
5877 }
5878
SelectNodeTo(SDNode * N,unsigned MachineOpc,EVT VT1,EVT VT2)5879 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5880 EVT VT1, EVT VT2) {
5881 SDVTList VTs = getVTList(VT1, VT2);
5882 return SelectNodeTo(N, MachineOpc, VTs, None);
5883 }
5884
SelectNodeTo(SDNode * N,unsigned MachineOpc,EVT VT1,EVT VT2,EVT VT3,ArrayRef<SDValue> Ops)5885 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5886 EVT VT1, EVT VT2, EVT VT3,
5887 ArrayRef<SDValue> Ops) {
5888 SDVTList VTs = getVTList(VT1, VT2, VT3);
5889 return SelectNodeTo(N, MachineOpc, VTs, Ops);
5890 }
5891
SelectNodeTo(SDNode * N,unsigned MachineOpc,EVT VT1,EVT VT2,EVT VT3,EVT VT4,ArrayRef<SDValue> Ops)5892 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5893 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
5894 ArrayRef<SDValue> Ops) {
5895 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
5896 return SelectNodeTo(N, MachineOpc, VTs, Ops);
5897 }
5898
SelectNodeTo(SDNode * N,unsigned MachineOpc,EVT VT1,EVT VT2,SDValue Op1)5899 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5900 EVT VT1, EVT VT2,
5901 SDValue Op1) {
5902 SDVTList VTs = getVTList(VT1, VT2);
5903 SDValue Ops[] = { Op1 };
5904 return SelectNodeTo(N, MachineOpc, VTs, Ops);
5905 }
5906
SelectNodeTo(SDNode * N,unsigned MachineOpc,EVT VT1,EVT VT2,SDValue Op1,SDValue Op2)5907 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5908 EVT VT1, EVT VT2,
5909 SDValue Op1, SDValue Op2) {
5910 SDVTList VTs = getVTList(VT1, VT2);
5911 SDValue Ops[] = { Op1, Op2 };
5912 return SelectNodeTo(N, MachineOpc, VTs, Ops);
5913 }
5914
SelectNodeTo(SDNode * N,unsigned MachineOpc,EVT VT1,EVT VT2,SDValue Op1,SDValue Op2,SDValue Op3)5915 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5916 EVT VT1, EVT VT2,
5917 SDValue Op1, SDValue Op2,
5918 SDValue Op3) {
5919 SDVTList VTs = getVTList(VT1, VT2);
5920 SDValue Ops[] = { Op1, Op2, Op3 };
5921 return SelectNodeTo(N, MachineOpc, VTs, Ops);
5922 }
5923
SelectNodeTo(SDNode * N,unsigned MachineOpc,EVT VT1,EVT VT2,EVT VT3,SDValue Op1,SDValue Op2,SDValue Op3)5924 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5925 EVT VT1, EVT VT2, EVT VT3,
5926 SDValue Op1, SDValue Op2,
5927 SDValue Op3) {
5928 SDVTList VTs = getVTList(VT1, VT2, VT3);
5929 SDValue Ops[] = { Op1, Op2, Op3 };
5930 return SelectNodeTo(N, MachineOpc, VTs, Ops);
5931 }
5932
SelectNodeTo(SDNode * N,unsigned MachineOpc,SDVTList VTs,ArrayRef<SDValue> Ops)5933 SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
5934 SDVTList VTs,ArrayRef<SDValue> Ops) {
5935 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
5936 // Reset the NodeID to -1.
5937 New->setNodeId(-1);
5938 if (New != N) {
5939 ReplaceAllUsesWith(N, New);
5940 RemoveDeadNode(N);
5941 }
5942 return New;
5943 }
5944
5945 /// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
5946 /// the line number information on the merged node since it is not possible to
5947 /// preserve the information that operation is associated with multiple lines.
5948 /// This will make the debugger working better at -O0, were there is a higher
5949 /// probability having other instructions associated with that line.
5950 ///
5951 /// For IROrder, we keep the smaller of the two
UpdadeSDLocOnMergedSDNode(SDNode * N,const SDLoc & OLoc)5952 SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, const SDLoc &OLoc) {
5953 DebugLoc NLoc = N->getDebugLoc();
5954 if (NLoc && OptLevel == CodeGenOpt::None && OLoc.getDebugLoc() != NLoc) {
5955 N->setDebugLoc(DebugLoc());
5956 }
5957 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5958 N->setIROrder(Order);
5959 return N;
5960 }
5961
5962 /// MorphNodeTo - This *mutates* the specified node to have the specified
5963 /// return type, opcode, and operands.
5964 ///
5965 /// Note that MorphNodeTo returns the resultant node. If there is already a
5966 /// node of the specified opcode and operands, it returns that node instead of
5967 /// the current one. Note that the SDLoc need not be the same.
5968 ///
5969 /// Using MorphNodeTo is faster than creating a new node and swapping it in
5970 /// with ReplaceAllUsesWith both because it often avoids allocating a new
5971 /// node, and because it doesn't require CSE recalculation for any of
5972 /// the node's users.
5973 ///
5974 /// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
5975 /// As a consequence it isn't appropriate to use from within the DAG combiner or
5976 /// the legalizer which maintain worklists that would need to be updated when
5977 /// deleting things.
MorphNodeTo(SDNode * N,unsigned Opc,SDVTList VTs,ArrayRef<SDValue> Ops)5978 SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
5979 SDVTList VTs, ArrayRef<SDValue> Ops) {
5980 // If an identical node already exists, use it.
5981 void *IP = nullptr;
5982 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
5983 FoldingSetNodeID ID;
5984 AddNodeIDNode(ID, Opc, VTs, Ops);
5985 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
5986 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
5987 }
5988
5989 if (!RemoveNodeFromCSEMaps(N))
5990 IP = nullptr;
5991
5992 // Start the morphing.
5993 N->NodeType = Opc;
5994 N->ValueList = VTs.VTs;
5995 N->NumValues = VTs.NumVTs;
5996
5997 // Clear the operands list, updating used nodes to remove this from their
5998 // use list. Keep track of any operands that become dead as a result.
5999 SmallPtrSet<SDNode*, 16> DeadNodeSet;
6000 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
6001 SDUse &Use = *I++;
6002 SDNode *Used = Use.getNode();
6003 Use.set(SDValue());
6004 if (Used->use_empty())
6005 DeadNodeSet.insert(Used);
6006 }
6007
6008 // For MachineNode, initialize the memory references information.
6009 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
6010 MN->setMemRefs(nullptr, nullptr);
6011
6012 // Swap for an appropriately sized array from the recycler.
6013 removeOperands(N);
6014 createOperands(N, Ops);
6015
6016 // Delete any nodes that are still dead after adding the uses for the
6017 // new operands.
6018 if (!DeadNodeSet.empty()) {
6019 SmallVector<SDNode *, 16> DeadNodes;
6020 for (SDNode *N : DeadNodeSet)
6021 if (N->use_empty())
6022 DeadNodes.push_back(N);
6023 RemoveDeadNodes(DeadNodes);
6024 }
6025
6026 if (IP)
6027 CSEMap.InsertNode(N, IP); // Memoize the new node.
6028 return N;
6029 }
6030
6031
6032 /// getMachineNode - These are used for target selectors to create a new node
6033 /// with specified return type(s), MachineInstr opcode, and operands.
6034 ///
6035 /// Note that getMachineNode returns the resultant node. If there is already a
6036 /// node of the specified opcode and operands, it returns that node instead of
6037 /// the current one.
getMachineNode(unsigned Opcode,const SDLoc & dl,EVT VT)6038 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6039 EVT VT) {
6040 SDVTList VTs = getVTList(VT);
6041 return getMachineNode(Opcode, dl, VTs, None);
6042 }
6043
getMachineNode(unsigned Opcode,const SDLoc & dl,EVT VT,SDValue Op1)6044 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6045 EVT VT, SDValue Op1) {
6046 SDVTList VTs = getVTList(VT);
6047 SDValue Ops[] = { Op1 };
6048 return getMachineNode(Opcode, dl, VTs, Ops);
6049 }
6050
getMachineNode(unsigned Opcode,const SDLoc & dl,EVT VT,SDValue Op1,SDValue Op2)6051 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6052 EVT VT, SDValue Op1, SDValue Op2) {
6053 SDVTList VTs = getVTList(VT);
6054 SDValue Ops[] = { Op1, Op2 };
6055 return getMachineNode(Opcode, dl, VTs, Ops);
6056 }
6057
getMachineNode(unsigned Opcode,const SDLoc & dl,EVT VT,SDValue Op1,SDValue Op2,SDValue Op3)6058 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6059 EVT VT, SDValue Op1, SDValue Op2,
6060 SDValue Op3) {
6061 SDVTList VTs = getVTList(VT);
6062 SDValue Ops[] = { Op1, Op2, Op3 };
6063 return getMachineNode(Opcode, dl, VTs, Ops);
6064 }
6065
getMachineNode(unsigned Opcode,const SDLoc & dl,EVT VT,ArrayRef<SDValue> Ops)6066 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6067 EVT VT, ArrayRef<SDValue> Ops) {
6068 SDVTList VTs = getVTList(VT);
6069 return getMachineNode(Opcode, dl, VTs, Ops);
6070 }
6071
getMachineNode(unsigned Opcode,const SDLoc & dl,EVT VT1,EVT VT2)6072 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6073 EVT VT1, EVT VT2) {
6074 SDVTList VTs = getVTList(VT1, VT2);
6075 return getMachineNode(Opcode, dl, VTs, None);
6076 }
6077
getMachineNode(unsigned Opcode,const SDLoc & dl,EVT VT1,EVT VT2,SDValue Op1)6078 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6079 EVT VT1, EVT VT2, SDValue Op1) {
6080 SDVTList VTs = getVTList(VT1, VT2);
6081 SDValue Ops[] = { Op1 };
6082 return getMachineNode(Opcode, dl, VTs, Ops);
6083 }
6084
getMachineNode(unsigned Opcode,const SDLoc & dl,EVT VT1,EVT VT2,SDValue Op1,SDValue Op2)6085 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6086 EVT VT1, EVT VT2, SDValue Op1,
6087 SDValue Op2) {
6088 SDVTList VTs = getVTList(VT1, VT2);
6089 SDValue Ops[] = { Op1, Op2 };
6090 return getMachineNode(Opcode, dl, VTs, Ops);
6091 }
6092
getMachineNode(unsigned Opcode,const SDLoc & dl,EVT VT1,EVT VT2,SDValue Op1,SDValue Op2,SDValue Op3)6093 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6094 EVT VT1, EVT VT2, SDValue Op1,
6095 SDValue Op2, SDValue Op3) {
6096 SDVTList VTs = getVTList(VT1, VT2);
6097 SDValue Ops[] = { Op1, Op2, Op3 };
6098 return getMachineNode(Opcode, dl, VTs, Ops);
6099 }
6100
getMachineNode(unsigned Opcode,const SDLoc & dl,EVT VT1,EVT VT2,ArrayRef<SDValue> Ops)6101 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6102 EVT VT1, EVT VT2,
6103 ArrayRef<SDValue> Ops) {
6104 SDVTList VTs = getVTList(VT1, VT2);
6105 return getMachineNode(Opcode, dl, VTs, Ops);
6106 }
6107
getMachineNode(unsigned Opcode,const SDLoc & dl,EVT VT1,EVT VT2,EVT VT3,SDValue Op1,SDValue Op2)6108 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6109 EVT VT1, EVT VT2, EVT VT3,
6110 SDValue Op1, SDValue Op2) {
6111 SDVTList VTs = getVTList(VT1, VT2, VT3);
6112 SDValue Ops[] = { Op1, Op2 };
6113 return getMachineNode(Opcode, dl, VTs, Ops);
6114 }
6115
getMachineNode(unsigned Opcode,const SDLoc & dl,EVT VT1,EVT VT2,EVT VT3,SDValue Op1,SDValue Op2,SDValue Op3)6116 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6117 EVT VT1, EVT VT2, EVT VT3,
6118 SDValue Op1, SDValue Op2,
6119 SDValue Op3) {
6120 SDVTList VTs = getVTList(VT1, VT2, VT3);
6121 SDValue Ops[] = { Op1, Op2, Op3 };
6122 return getMachineNode(Opcode, dl, VTs, Ops);
6123 }
6124
getMachineNode(unsigned Opcode,const SDLoc & dl,EVT VT1,EVT VT2,EVT VT3,ArrayRef<SDValue> Ops)6125 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6126 EVT VT1, EVT VT2, EVT VT3,
6127 ArrayRef<SDValue> Ops) {
6128 SDVTList VTs = getVTList(VT1, VT2, VT3);
6129 return getMachineNode(Opcode, dl, VTs, Ops);
6130 }
6131
getMachineNode(unsigned Opcode,const SDLoc & dl,EVT VT1,EVT VT2,EVT VT3,EVT VT4,ArrayRef<SDValue> Ops)6132 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6133 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
6134 ArrayRef<SDValue> Ops) {
6135 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
6136 return getMachineNode(Opcode, dl, VTs, Ops);
6137 }
6138
getMachineNode(unsigned Opcode,const SDLoc & dl,ArrayRef<EVT> ResultTys,ArrayRef<SDValue> Ops)6139 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &dl,
6140 ArrayRef<EVT> ResultTys,
6141 ArrayRef<SDValue> Ops) {
6142 SDVTList VTs = getVTList(ResultTys);
6143 return getMachineNode(Opcode, dl, VTs, Ops);
6144 }
6145
getMachineNode(unsigned Opcode,const SDLoc & DL,SDVTList VTs,ArrayRef<SDValue> Ops)6146 MachineSDNode *SelectionDAG::getMachineNode(unsigned Opcode, const SDLoc &DL,
6147 SDVTList VTs,
6148 ArrayRef<SDValue> Ops) {
6149 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
6150 MachineSDNode *N;
6151 void *IP = nullptr;
6152
6153 if (DoCSE) {
6154 FoldingSetNodeID ID;
6155 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
6156 IP = nullptr;
6157 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6158 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
6159 }
6160 }
6161
6162 // Allocate a new MachineSDNode.
6163 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6164 createOperands(N, Ops);
6165
6166 if (DoCSE)
6167 CSEMap.InsertNode(N, IP);
6168
6169 InsertNode(N);
6170 return N;
6171 }
6172
6173 /// getTargetExtractSubreg - A convenience function for creating
6174 /// TargetOpcode::EXTRACT_SUBREG nodes.
getTargetExtractSubreg(int SRIdx,const SDLoc & DL,EVT VT,SDValue Operand)6175 SDValue SelectionDAG::getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT,
6176 SDValue Operand) {
6177 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
6178 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
6179 VT, Operand, SRIdxVal);
6180 return SDValue(Subreg, 0);
6181 }
6182
6183 /// getTargetInsertSubreg - A convenience function for creating
6184 /// TargetOpcode::INSERT_SUBREG nodes.
getTargetInsertSubreg(int SRIdx,const SDLoc & DL,EVT VT,SDValue Operand,SDValue Subreg)6185 SDValue SelectionDAG::getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT,
6186 SDValue Operand, SDValue Subreg) {
6187 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
6188 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
6189 VT, Operand, Subreg, SRIdxVal);
6190 return SDValue(Result, 0);
6191 }
6192
6193 /// getNodeIfExists - Get the specified node if it's already available, or
6194 /// else return NULL.
getNodeIfExists(unsigned Opcode,SDVTList VTList,ArrayRef<SDValue> Ops,const SDNodeFlags * Flags)6195 SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
6196 ArrayRef<SDValue> Ops,
6197 const SDNodeFlags *Flags) {
6198 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
6199 FoldingSetNodeID ID;
6200 AddNodeIDNode(ID, Opcode, VTList, Ops);
6201 void *IP = nullptr;
6202 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
6203 if (Flags)
6204 E->intersectFlagsWith(Flags);
6205 return E;
6206 }
6207 }
6208 return nullptr;
6209 }
6210
6211 /// getDbgValue - Creates a SDDbgValue node.
6212 ///
6213 /// SDNode
getDbgValue(MDNode * Var,MDNode * Expr,SDNode * N,unsigned R,bool IsIndirect,uint64_t Off,const DebugLoc & DL,unsigned O)6214 SDDbgValue *SelectionDAG::getDbgValue(MDNode *Var, MDNode *Expr, SDNode *N,
6215 unsigned R, bool IsIndirect, uint64_t Off,
6216 const DebugLoc &DL, unsigned O) {
6217 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
6218 "Expected inlined-at fields to agree");
6219 return new (DbgInfo->getAlloc())
6220 SDDbgValue(Var, Expr, N, R, IsIndirect, Off, DL, O);
6221 }
6222
6223 /// Constant
getConstantDbgValue(MDNode * Var,MDNode * Expr,const Value * C,uint64_t Off,const DebugLoc & DL,unsigned O)6224 SDDbgValue *SelectionDAG::getConstantDbgValue(MDNode *Var, MDNode *Expr,
6225 const Value *C, uint64_t Off,
6226 const DebugLoc &DL, unsigned O) {
6227 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
6228 "Expected inlined-at fields to agree");
6229 return new (DbgInfo->getAlloc()) SDDbgValue(Var, Expr, C, Off, DL, O);
6230 }
6231
6232 /// FrameIndex
getFrameIndexDbgValue(MDNode * Var,MDNode * Expr,unsigned FI,uint64_t Off,const DebugLoc & DL,unsigned O)6233 SDDbgValue *SelectionDAG::getFrameIndexDbgValue(MDNode *Var, MDNode *Expr,
6234 unsigned FI, uint64_t Off,
6235 const DebugLoc &DL,
6236 unsigned O) {
6237 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
6238 "Expected inlined-at fields to agree");
6239 return new (DbgInfo->getAlloc()) SDDbgValue(Var, Expr, FI, Off, DL, O);
6240 }
6241
6242 namespace {
6243
6244 /// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
6245 /// pointed to by a use iterator is deleted, increment the use iterator
6246 /// so that it doesn't dangle.
6247 ///
6248 class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
6249 SDNode::use_iterator &UI;
6250 SDNode::use_iterator &UE;
6251
NodeDeleted(SDNode * N,SDNode * E)6252 void NodeDeleted(SDNode *N, SDNode *E) override {
6253 // Increment the iterator as needed.
6254 while (UI != UE && N == *UI)
6255 ++UI;
6256 }
6257
6258 public:
RAUWUpdateListener(SelectionDAG & d,SDNode::use_iterator & ui,SDNode::use_iterator & ue)6259 RAUWUpdateListener(SelectionDAG &d,
6260 SDNode::use_iterator &ui,
6261 SDNode::use_iterator &ue)
6262 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
6263 };
6264
6265 }
6266
6267 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6268 /// This can cause recursive merging of nodes in the DAG.
6269 ///
6270 /// This version assumes From has a single result value.
6271 ///
ReplaceAllUsesWith(SDValue FromN,SDValue To)6272 void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
6273 SDNode *From = FromN.getNode();
6274 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
6275 "Cannot replace with this method!");
6276 assert(From != To.getNode() && "Cannot replace uses of with self");
6277
6278 // Iterate over all the existing uses of From. New uses will be added
6279 // to the beginning of the use list, which we avoid visiting.
6280 // This specifically avoids visiting uses of From that arise while the
6281 // replacement is happening, because any such uses would be the result
6282 // of CSE: If an existing node looks like From after one of its operands
6283 // is replaced by To, we don't want to replace of all its users with To
6284 // too. See PR3018 for more info.
6285 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
6286 RAUWUpdateListener Listener(*this, UI, UE);
6287 while (UI != UE) {
6288 SDNode *User = *UI;
6289
6290 // This node is about to morph, remove its old self from the CSE maps.
6291 RemoveNodeFromCSEMaps(User);
6292
6293 // A user can appear in a use list multiple times, and when this
6294 // happens the uses are usually next to each other in the list.
6295 // To help reduce the number of CSE recomputations, process all
6296 // the uses of this user that we can find this way.
6297 do {
6298 SDUse &Use = UI.getUse();
6299 ++UI;
6300 Use.set(To);
6301 } while (UI != UE && *UI == User);
6302
6303 // Now that we have modified User, add it back to the CSE maps. If it
6304 // already exists there, recursively merge the results together.
6305 AddModifiedNodeToCSEMaps(User);
6306 }
6307
6308 // Preserve Debug Values
6309 TransferDbgValues(FromN, To);
6310
6311 // If we just RAUW'd the root, take note.
6312 if (FromN == getRoot())
6313 setRoot(To);
6314 }
6315
6316 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6317 /// This can cause recursive merging of nodes in the DAG.
6318 ///
6319 /// This version assumes that for each value of From, there is a
6320 /// corresponding value in To in the same position with the same type.
6321 ///
ReplaceAllUsesWith(SDNode * From,SDNode * To)6322 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
6323 #ifndef NDEBUG
6324 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
6325 assert((!From->hasAnyUseOfValue(i) ||
6326 From->getValueType(i) == To->getValueType(i)) &&
6327 "Cannot use this version of ReplaceAllUsesWith!");
6328 #endif
6329
6330 // Handle the trivial case.
6331 if (From == To)
6332 return;
6333
6334 // Preserve Debug Info. Only do this if there's a use.
6335 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
6336 if (From->hasAnyUseOfValue(i)) {
6337 assert((i < To->getNumValues()) && "Invalid To location");
6338 TransferDbgValues(SDValue(From, i), SDValue(To, i));
6339 }
6340
6341 // Iterate over just the existing users of From. See the comments in
6342 // the ReplaceAllUsesWith above.
6343 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
6344 RAUWUpdateListener Listener(*this, UI, UE);
6345 while (UI != UE) {
6346 SDNode *User = *UI;
6347
6348 // This node is about to morph, remove its old self from the CSE maps.
6349 RemoveNodeFromCSEMaps(User);
6350
6351 // A user can appear in a use list multiple times, and when this
6352 // happens the uses are usually next to each other in the list.
6353 // To help reduce the number of CSE recomputations, process all
6354 // the uses of this user that we can find this way.
6355 do {
6356 SDUse &Use = UI.getUse();
6357 ++UI;
6358 Use.setNode(To);
6359 } while (UI != UE && *UI == User);
6360
6361 // Now that we have modified User, add it back to the CSE maps. If it
6362 // already exists there, recursively merge the results together.
6363 AddModifiedNodeToCSEMaps(User);
6364 }
6365
6366 // If we just RAUW'd the root, take note.
6367 if (From == getRoot().getNode())
6368 setRoot(SDValue(To, getRoot().getResNo()));
6369 }
6370
6371 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
6372 /// This can cause recursive merging of nodes in the DAG.
6373 ///
6374 /// This version can replace From with any result values. To must match the
6375 /// number and types of values returned by From.
ReplaceAllUsesWith(SDNode * From,const SDValue * To)6376 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
6377 if (From->getNumValues() == 1) // Handle the simple case efficiently.
6378 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
6379
6380 // Preserve Debug Info.
6381 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
6382 TransferDbgValues(SDValue(From, i), *To);
6383
6384 // Iterate over just the existing users of From. See the comments in
6385 // the ReplaceAllUsesWith above.
6386 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
6387 RAUWUpdateListener Listener(*this, UI, UE);
6388 while (UI != UE) {
6389 SDNode *User = *UI;
6390
6391 // This node is about to morph, remove its old self from the CSE maps.
6392 RemoveNodeFromCSEMaps(User);
6393
6394 // A user can appear in a use list multiple times, and when this
6395 // happens the uses are usually next to each other in the list.
6396 // To help reduce the number of CSE recomputations, process all
6397 // the uses of this user that we can find this way.
6398 do {
6399 SDUse &Use = UI.getUse();
6400 const SDValue &ToOp = To[Use.getResNo()];
6401 ++UI;
6402 Use.set(ToOp);
6403 } while (UI != UE && *UI == User);
6404
6405 // Now that we have modified User, add it back to the CSE maps. If it
6406 // already exists there, recursively merge the results together.
6407 AddModifiedNodeToCSEMaps(User);
6408 }
6409
6410 // If we just RAUW'd the root, take note.
6411 if (From == getRoot().getNode())
6412 setRoot(SDValue(To[getRoot().getResNo()]));
6413 }
6414
6415 /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
6416 /// uses of other values produced by From.getNode() alone. The Deleted
6417 /// vector is handled the same way as for ReplaceAllUsesWith.
ReplaceAllUsesOfValueWith(SDValue From,SDValue To)6418 void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
6419 // Handle the really simple, really trivial case efficiently.
6420 if (From == To) return;
6421
6422 // Handle the simple, trivial, case efficiently.
6423 if (From.getNode()->getNumValues() == 1) {
6424 ReplaceAllUsesWith(From, To);
6425 return;
6426 }
6427
6428 // Preserve Debug Info.
6429 TransferDbgValues(From, To);
6430
6431 // Iterate over just the existing users of From. See the comments in
6432 // the ReplaceAllUsesWith above.
6433 SDNode::use_iterator UI = From.getNode()->use_begin(),
6434 UE = From.getNode()->use_end();
6435 RAUWUpdateListener Listener(*this, UI, UE);
6436 while (UI != UE) {
6437 SDNode *User = *UI;
6438 bool UserRemovedFromCSEMaps = false;
6439
6440 // A user can appear in a use list multiple times, and when this
6441 // happens the uses are usually next to each other in the list.
6442 // To help reduce the number of CSE recomputations, process all
6443 // the uses of this user that we can find this way.
6444 do {
6445 SDUse &Use = UI.getUse();
6446
6447 // Skip uses of different values from the same node.
6448 if (Use.getResNo() != From.getResNo()) {
6449 ++UI;
6450 continue;
6451 }
6452
6453 // If this node hasn't been modified yet, it's still in the CSE maps,
6454 // so remove its old self from the CSE maps.
6455 if (!UserRemovedFromCSEMaps) {
6456 RemoveNodeFromCSEMaps(User);
6457 UserRemovedFromCSEMaps = true;
6458 }
6459
6460 ++UI;
6461 Use.set(To);
6462 } while (UI != UE && *UI == User);
6463
6464 // We are iterating over all uses of the From node, so if a use
6465 // doesn't use the specific value, no changes are made.
6466 if (!UserRemovedFromCSEMaps)
6467 continue;
6468
6469 // Now that we have modified User, add it back to the CSE maps. If it
6470 // already exists there, recursively merge the results together.
6471 AddModifiedNodeToCSEMaps(User);
6472 }
6473
6474 // If we just RAUW'd the root, take note.
6475 if (From == getRoot())
6476 setRoot(To);
6477 }
6478
6479 namespace {
6480 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
6481 /// to record information about a use.
6482 struct UseMemo {
6483 SDNode *User;
6484 unsigned Index;
6485 SDUse *Use;
6486 };
6487
6488 /// operator< - Sort Memos by User.
operator <(const UseMemo & L,const UseMemo & R)6489 bool operator<(const UseMemo &L, const UseMemo &R) {
6490 return (intptr_t)L.User < (intptr_t)R.User;
6491 }
6492 }
6493
6494 /// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
6495 /// uses of other values produced by From.getNode() alone. The same value
6496 /// may appear in both the From and To list. The Deleted vector is
6497 /// handled the same way as for ReplaceAllUsesWith.
ReplaceAllUsesOfValuesWith(const SDValue * From,const SDValue * To,unsigned Num)6498 void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
6499 const SDValue *To,
6500 unsigned Num){
6501 // Handle the simple, trivial case efficiently.
6502 if (Num == 1)
6503 return ReplaceAllUsesOfValueWith(*From, *To);
6504
6505 TransferDbgValues(*From, *To);
6506
6507 // Read up all the uses and make records of them. This helps
6508 // processing new uses that are introduced during the
6509 // replacement process.
6510 SmallVector<UseMemo, 4> Uses;
6511 for (unsigned i = 0; i != Num; ++i) {
6512 unsigned FromResNo = From[i].getResNo();
6513 SDNode *FromNode = From[i].getNode();
6514 for (SDNode::use_iterator UI = FromNode->use_begin(),
6515 E = FromNode->use_end(); UI != E; ++UI) {
6516 SDUse &Use = UI.getUse();
6517 if (Use.getResNo() == FromResNo) {
6518 UseMemo Memo = { *UI, i, &Use };
6519 Uses.push_back(Memo);
6520 }
6521 }
6522 }
6523
6524 // Sort the uses, so that all the uses from a given User are together.
6525 std::sort(Uses.begin(), Uses.end());
6526
6527 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
6528 UseIndex != UseIndexEnd; ) {
6529 // We know that this user uses some value of From. If it is the right
6530 // value, update it.
6531 SDNode *User = Uses[UseIndex].User;
6532
6533 // This node is about to morph, remove its old self from the CSE maps.
6534 RemoveNodeFromCSEMaps(User);
6535
6536 // The Uses array is sorted, so all the uses for a given User
6537 // are next to each other in the list.
6538 // To help reduce the number of CSE recomputations, process all
6539 // the uses of this user that we can find this way.
6540 do {
6541 unsigned i = Uses[UseIndex].Index;
6542 SDUse &Use = *Uses[UseIndex].Use;
6543 ++UseIndex;
6544
6545 Use.set(To[i]);
6546 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
6547
6548 // Now that we have modified User, add it back to the CSE maps. If it
6549 // already exists there, recursively merge the results together.
6550 AddModifiedNodeToCSEMaps(User);
6551 }
6552 }
6553
6554 /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
6555 /// based on their topological order. It returns the maximum id and a vector
6556 /// of the SDNodes* in assigned order by reference.
AssignTopologicalOrder()6557 unsigned SelectionDAG::AssignTopologicalOrder() {
6558
6559 unsigned DAGSize = 0;
6560
6561 // SortedPos tracks the progress of the algorithm. Nodes before it are
6562 // sorted, nodes after it are unsorted. When the algorithm completes
6563 // it is at the end of the list.
6564 allnodes_iterator SortedPos = allnodes_begin();
6565
6566 // Visit all the nodes. Move nodes with no operands to the front of
6567 // the list immediately. Annotate nodes that do have operands with their
6568 // operand count. Before we do this, the Node Id fields of the nodes
6569 // may contain arbitrary values. After, the Node Id fields for nodes
6570 // before SortedPos will contain the topological sort index, and the
6571 // Node Id fields for nodes At SortedPos and after will contain the
6572 // count of outstanding operands.
6573 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
6574 SDNode *N = &*I++;
6575 checkForCycles(N, this);
6576 unsigned Degree = N->getNumOperands();
6577 if (Degree == 0) {
6578 // A node with no uses, add it to the result array immediately.
6579 N->setNodeId(DAGSize++);
6580 allnodes_iterator Q(N);
6581 if (Q != SortedPos)
6582 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
6583 assert(SortedPos != AllNodes.end() && "Overran node list");
6584 ++SortedPos;
6585 } else {
6586 // Temporarily use the Node Id as scratch space for the degree count.
6587 N->setNodeId(Degree);
6588 }
6589 }
6590
6591 // Visit all the nodes. As we iterate, move nodes into sorted order,
6592 // such that by the time the end is reached all nodes will be sorted.
6593 for (SDNode &Node : allnodes()) {
6594 SDNode *N = &Node;
6595 checkForCycles(N, this);
6596 // N is in sorted position, so all its uses have one less operand
6597 // that needs to be sorted.
6598 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
6599 UI != UE; ++UI) {
6600 SDNode *P = *UI;
6601 unsigned Degree = P->getNodeId();
6602 assert(Degree != 0 && "Invalid node degree");
6603 --Degree;
6604 if (Degree == 0) {
6605 // All of P's operands are sorted, so P may sorted now.
6606 P->setNodeId(DAGSize++);
6607 if (P->getIterator() != SortedPos)
6608 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
6609 assert(SortedPos != AllNodes.end() && "Overran node list");
6610 ++SortedPos;
6611 } else {
6612 // Update P's outstanding operand count.
6613 P->setNodeId(Degree);
6614 }
6615 }
6616 if (Node.getIterator() == SortedPos) {
6617 #ifndef NDEBUG
6618 allnodes_iterator I(N);
6619 SDNode *S = &*++I;
6620 dbgs() << "Overran sorted position:\n";
6621 S->dumprFull(this); dbgs() << "\n";
6622 dbgs() << "Checking if this is due to cycles\n";
6623 checkForCycles(this, true);
6624 #endif
6625 llvm_unreachable(nullptr);
6626 }
6627 }
6628
6629 assert(SortedPos == AllNodes.end() &&
6630 "Topological sort incomplete!");
6631 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
6632 "First node in topological sort is not the entry token!");
6633 assert(AllNodes.front().getNodeId() == 0 &&
6634 "First node in topological sort has non-zero id!");
6635 assert(AllNodes.front().getNumOperands() == 0 &&
6636 "First node in topological sort has operands!");
6637 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6638 "Last node in topologic sort has unexpected id!");
6639 assert(AllNodes.back().use_empty() &&
6640 "Last node in topologic sort has users!");
6641 assert(DAGSize == allnodes_size() && "Node count mismatch!");
6642 return DAGSize;
6643 }
6644
6645 /// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6646 /// value is produced by SD.
AddDbgValue(SDDbgValue * DB,SDNode * SD,bool isParameter)6647 void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6648 if (SD) {
6649 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
6650 SD->setHasDebugValue(true);
6651 }
6652 DbgInfo->add(DB, SD, isParameter);
6653 }
6654
6655 /// TransferDbgValues - Transfer SDDbgValues. Called in replace nodes.
TransferDbgValues(SDValue From,SDValue To)6656 void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6657 if (From == To || !From.getNode()->getHasDebugValue())
6658 return;
6659 SDNode *FromNode = From.getNode();
6660 SDNode *ToNode = To.getNode();
6661 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
6662 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
6663 I != E; ++I) {
6664 SDDbgValue *Dbg = *I;
6665 // Only add Dbgvalues attached to same ResNo.
6666 if (Dbg->getKind() == SDDbgValue::SDNODE &&
6667 Dbg->getResNo() == From.getResNo()) {
6668 SDDbgValue *Clone =
6669 getDbgValue(Dbg->getVariable(), Dbg->getExpression(), ToNode,
6670 To.getResNo(), Dbg->isIndirect(), Dbg->getOffset(),
6671 Dbg->getDebugLoc(), Dbg->getOrder());
6672 AddDbgValue(Clone, ToNode, false);
6673 }
6674 }
6675 }
6676
6677 //===----------------------------------------------------------------------===//
6678 // SDNode Class
6679 //===----------------------------------------------------------------------===//
6680
isNullConstant(SDValue V)6681 bool llvm::isNullConstant(SDValue V) {
6682 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
6683 return Const != nullptr && Const->isNullValue();
6684 }
6685
isNullFPConstant(SDValue V)6686 bool llvm::isNullFPConstant(SDValue V) {
6687 ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
6688 return Const != nullptr && Const->isZero() && !Const->isNegative();
6689 }
6690
isAllOnesConstant(SDValue V)6691 bool llvm::isAllOnesConstant(SDValue V) {
6692 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
6693 return Const != nullptr && Const->isAllOnesValue();
6694 }
6695
isOneConstant(SDValue V)6696 bool llvm::isOneConstant(SDValue V) {
6697 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
6698 return Const != nullptr && Const->isOne();
6699 }
6700
isBitwiseNot(SDValue V)6701 bool llvm::isBitwiseNot(SDValue V) {
6702 return V.getOpcode() == ISD::XOR && isAllOnesConstant(V.getOperand(1));
6703 }
6704
~HandleSDNode()6705 HandleSDNode::~HandleSDNode() {
6706 DropOperands();
6707 }
6708
GlobalAddressSDNode(unsigned Opc,unsigned Order,const DebugLoc & DL,const GlobalValue * GA,EVT VT,int64_t o,unsigned char TF)6709 GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6710 const DebugLoc &DL,
6711 const GlobalValue *GA, EVT VT,
6712 int64_t o, unsigned char TF)
6713 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
6714 TheGlobal = GA;
6715 }
6716
AddrSpaceCastSDNode(unsigned Order,const DebugLoc & dl,EVT VT,unsigned SrcAS,unsigned DestAS)6717 AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, const DebugLoc &dl,
6718 EVT VT, unsigned SrcAS,
6719 unsigned DestAS)
6720 : SDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT)),
6721 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6722
MemSDNode(unsigned Opc,unsigned Order,const DebugLoc & dl,SDVTList VTs,EVT memvt,MachineMemOperand * mmo)6723 MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
6724 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
6725 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
6726 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
6727 MMO->isNonTemporal(), MMO->isInvariant());
6728 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
6729 assert(isNonTemporal() == MMO->isNonTemporal() &&
6730 "Non-temporal encoding error!");
6731 // We check here that the size of the memory operand fits within the size of
6732 // the MMO. This is because the MMO might indicate only a possible address
6733 // range instead of specifying the affected memory addresses precisely.
6734 assert(memvt.getStoreSize() <= MMO->getSize() && "Size mismatch!");
6735 }
6736
6737 /// Profile - Gather unique data for the node.
6738 ///
Profile(FoldingSetNodeID & ID) const6739 void SDNode::Profile(FoldingSetNodeID &ID) const {
6740 AddNodeIDNode(ID, this);
6741 }
6742
6743 namespace {
6744 struct EVTArray {
6745 std::vector<EVT> VTs;
6746
EVTArray__anonbd9970280911::EVTArray6747 EVTArray() {
6748 VTs.reserve(MVT::LAST_VALUETYPE);
6749 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6750 VTs.push_back(MVT((MVT::SimpleValueType)i));
6751 }
6752 };
6753 }
6754
6755 static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
6756 static ManagedStatic<EVTArray> SimpleVTArray;
6757 static ManagedStatic<sys::SmartMutex<true> > VTMutex;
6758
6759 /// getValueTypeList - Return a pointer to the specified value type.
6760 ///
getValueTypeList(EVT VT)6761 const EVT *SDNode::getValueTypeList(EVT VT) {
6762 if (VT.isExtended()) {
6763 sys::SmartScopedLock<true> Lock(*VTMutex);
6764 return &(*EVTs->insert(VT).first);
6765 } else {
6766 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
6767 "Value type out of range!");
6768 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
6769 }
6770 }
6771
6772 /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6773 /// indicated value. This method ignores uses of other values defined by this
6774 /// operation.
hasNUsesOfValue(unsigned NUses,unsigned Value) const6775 bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
6776 assert(Value < getNumValues() && "Bad value!");
6777
6778 // TODO: Only iterate over uses of a given value of the node
6779 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
6780 if (UI.getUse().getResNo() == Value) {
6781 if (NUses == 0)
6782 return false;
6783 --NUses;
6784 }
6785 }
6786
6787 // Found exactly the right number of uses?
6788 return NUses == 0;
6789 }
6790
6791
6792 /// hasAnyUseOfValue - Return true if there are any use of the indicated
6793 /// value. This method ignores uses of other values defined by this operation.
hasAnyUseOfValue(unsigned Value) const6794 bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6795 assert(Value < getNumValues() && "Bad value!");
6796
6797 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
6798 if (UI.getUse().getResNo() == Value)
6799 return true;
6800
6801 return false;
6802 }
6803
6804
6805 /// isOnlyUserOf - Return true if this node is the only use of N.
6806 ///
isOnlyUserOf(const SDNode * N) const6807 bool SDNode::isOnlyUserOf(const SDNode *N) const {
6808 bool Seen = false;
6809 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
6810 SDNode *User = *I;
6811 if (User == this)
6812 Seen = true;
6813 else
6814 return false;
6815 }
6816
6817 return Seen;
6818 }
6819
6820 /// isOperand - Return true if this node is an operand of N.
6821 ///
isOperandOf(const SDNode * N) const6822 bool SDValue::isOperandOf(const SDNode *N) const {
6823 for (const SDValue &Op : N->op_values())
6824 if (*this == Op)
6825 return true;
6826 return false;
6827 }
6828
isOperandOf(const SDNode * N) const6829 bool SDNode::isOperandOf(const SDNode *N) const {
6830 for (const SDValue &Op : N->op_values())
6831 if (this == Op.getNode())
6832 return true;
6833 return false;
6834 }
6835
6836 /// reachesChainWithoutSideEffects - Return true if this operand (which must
6837 /// be a chain) reaches the specified operand without crossing any
6838 /// side-effecting instructions on any chain path. In practice, this looks
6839 /// through token factors and non-volatile loads. In order to remain efficient,
6840 /// this only looks a couple of nodes in, it does not do an exhaustive search.
reachesChainWithoutSideEffects(SDValue Dest,unsigned Depth) const6841 bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
6842 unsigned Depth) const {
6843 if (*this == Dest) return true;
6844
6845 // Don't search too deeply, we just want to be able to see through
6846 // TokenFactor's etc.
6847 if (Depth == 0) return false;
6848
6849 // If this is a token factor, all inputs to the TF happen in parallel. If any
6850 // of the operands of the TF does not reach dest, then we cannot do the xform.
6851 if (getOpcode() == ISD::TokenFactor) {
6852 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
6853 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6854 return false;
6855 return true;
6856 }
6857
6858 // Loads don't have side effects, look through them.
6859 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6860 if (!Ld->isVolatile())
6861 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6862 }
6863 return false;
6864 }
6865
hasPredecessor(const SDNode * N) const6866 bool SDNode::hasPredecessor(const SDNode *N) const {
6867 SmallPtrSet<const SDNode *, 32> Visited;
6868 SmallVector<const SDNode *, 16> Worklist;
6869 Worklist.push_back(this);
6870 return hasPredecessorHelper(N, Visited, Worklist);
6871 }
6872
getConstantOperandVal(unsigned Num) const6873 uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6874 assert(Num < NumOperands && "Invalid child # of SDNode!");
6875 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
6876 }
6877
getFlags() const6878 const SDNodeFlags *SDNode::getFlags() const {
6879 if (auto *FlagsNode = dyn_cast<BinaryWithFlagsSDNode>(this))
6880 return &FlagsNode->Flags;
6881 return nullptr;
6882 }
6883
intersectFlagsWith(const SDNodeFlags * Flags)6884 void SDNode::intersectFlagsWith(const SDNodeFlags *Flags) {
6885 if (auto *FlagsNode = dyn_cast<BinaryWithFlagsSDNode>(this))
6886 FlagsNode->Flags.intersectWith(Flags);
6887 }
6888
UnrollVectorOp(SDNode * N,unsigned ResNE)6889 SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6890 assert(N->getNumValues() == 1 &&
6891 "Can't unroll a vector with multiple results!");
6892
6893 EVT VT = N->getValueType(0);
6894 unsigned NE = VT.getVectorNumElements();
6895 EVT EltVT = VT.getVectorElementType();
6896 SDLoc dl(N);
6897
6898 SmallVector<SDValue, 8> Scalars;
6899 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6900
6901 // If ResNE is 0, fully unroll the vector op.
6902 if (ResNE == 0)
6903 ResNE = NE;
6904 else if (NE > ResNE)
6905 NE = ResNE;
6906
6907 unsigned i;
6908 for (i= 0; i != NE; ++i) {
6909 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
6910 SDValue Operand = N->getOperand(j);
6911 EVT OperandVT = Operand.getValueType();
6912 if (OperandVT.isVector()) {
6913 // A vector operand; extract a single element.
6914 EVT OperandEltVT = OperandVT.getVectorElementType();
6915 Operands[j] =
6916 getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT, Operand,
6917 getConstant(i, dl, TLI->getVectorIdxTy(getDataLayout())));
6918 } else {
6919 // A scalar operand; just use it as is.
6920 Operands[j] = Operand;
6921 }
6922 }
6923
6924 switch (N->getOpcode()) {
6925 default: {
6926 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
6927 N->getFlags()));
6928 break;
6929 }
6930 case ISD::VSELECT:
6931 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
6932 break;
6933 case ISD::SHL:
6934 case ISD::SRA:
6935 case ISD::SRL:
6936 case ISD::ROTL:
6937 case ISD::ROTR:
6938 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
6939 getShiftAmountOperand(Operands[0].getValueType(),
6940 Operands[1])));
6941 break;
6942 case ISD::SIGN_EXTEND_INREG:
6943 case ISD::FP_ROUND_INREG: {
6944 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6945 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6946 Operands[0],
6947 getValueType(ExtVT)));
6948 }
6949 }
6950 }
6951
6952 for (; i < ResNE; ++i)
6953 Scalars.push_back(getUNDEF(EltVT));
6954
6955 return getNode(ISD::BUILD_VECTOR, dl,
6956 EVT::getVectorVT(*getContext(), EltVT, ResNE), Scalars);
6957 }
6958
areNonVolatileConsecutiveLoads(LoadSDNode * LD,LoadSDNode * Base,unsigned Bytes,int Dist) const6959 bool SelectionDAG::areNonVolatileConsecutiveLoads(LoadSDNode *LD,
6960 LoadSDNode *Base,
6961 unsigned Bytes,
6962 int Dist) const {
6963 if (LD->isVolatile() || Base->isVolatile())
6964 return false;
6965 if (LD->isIndexed() || Base->isIndexed())
6966 return false;
6967 if (LD->getChain() != Base->getChain())
6968 return false;
6969 EVT VT = LD->getValueType(0);
6970 if (VT.getSizeInBits() / 8 != Bytes)
6971 return false;
6972
6973 SDValue Loc = LD->getOperand(1);
6974 SDValue BaseLoc = Base->getOperand(1);
6975 if (Loc.getOpcode() == ISD::FrameIndex) {
6976 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6977 return false;
6978 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6979 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6980 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6981 int FS = MFI->getObjectSize(FI);
6982 int BFS = MFI->getObjectSize(BFI);
6983 if (FS != BFS || FS != (int)Bytes) return false;
6984 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6985 }
6986
6987 // Handle X + C.
6988 if (isBaseWithConstantOffset(Loc)) {
6989 int64_t LocOffset = cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue();
6990 if (Loc.getOperand(0) == BaseLoc) {
6991 // If the base location is a simple address with no offset itself, then
6992 // the second load's first add operand should be the base address.
6993 if (LocOffset == Dist * (int)Bytes)
6994 return true;
6995 } else if (isBaseWithConstantOffset(BaseLoc)) {
6996 // The base location itself has an offset, so subtract that value from the
6997 // second load's offset before comparing to distance * size.
6998 int64_t BOffset =
6999 cast<ConstantSDNode>(BaseLoc.getOperand(1))->getSExtValue();
7000 if (Loc.getOperand(0) == BaseLoc.getOperand(0)) {
7001 if ((LocOffset - BOffset) == Dist * (int)Bytes)
7002 return true;
7003 }
7004 }
7005 }
7006 const GlobalValue *GV1 = nullptr;
7007 const GlobalValue *GV2 = nullptr;
7008 int64_t Offset1 = 0;
7009 int64_t Offset2 = 0;
7010 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
7011 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
7012 if (isGA1 && isGA2 && GV1 == GV2)
7013 return Offset1 == (Offset2 + Dist*Bytes);
7014 return false;
7015 }
7016
7017
7018 /// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
7019 /// it cannot be inferred.
InferPtrAlignment(SDValue Ptr) const7020 unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
7021 // If this is a GlobalAddress + cst, return the alignment.
7022 const GlobalValue *GV;
7023 int64_t GVOffset = 0;
7024 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
7025 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
7026 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
7027 llvm::computeKnownBits(const_cast<GlobalValue *>(GV), KnownZero, KnownOne,
7028 getDataLayout());
7029 unsigned AlignBits = KnownZero.countTrailingOnes();
7030 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
7031 if (Align)
7032 return MinAlign(Align, GVOffset);
7033 }
7034
7035 // If this is a direct reference to a stack slot, use information about the
7036 // stack slot's alignment.
7037 int FrameIdx = 1 << 31;
7038 int64_t FrameOffset = 0;
7039 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
7040 FrameIdx = FI->getIndex();
7041 } else if (isBaseWithConstantOffset(Ptr) &&
7042 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
7043 // Handle FI+Cst
7044 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
7045 FrameOffset = Ptr.getConstantOperandVal(1);
7046 }
7047
7048 if (FrameIdx != (1 << 31)) {
7049 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
7050 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
7051 FrameOffset);
7052 return FIInfoAlign;
7053 }
7054
7055 return 0;
7056 }
7057
7058 /// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
7059 /// which is split (or expanded) into two not necessarily identical pieces.
GetSplitDestVTs(const EVT & VT) const7060 std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
7061 // Currently all types are split in half.
7062 EVT LoVT, HiVT;
7063 if (!VT.isVector()) {
7064 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
7065 } else {
7066 unsigned NumElements = VT.getVectorNumElements();
7067 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
7068 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
7069 NumElements/2);
7070 }
7071 return std::make_pair(LoVT, HiVT);
7072 }
7073
7074 /// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
7075 /// low/high part.
7076 std::pair<SDValue, SDValue>
SplitVector(const SDValue & N,const SDLoc & DL,const EVT & LoVT,const EVT & HiVT)7077 SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
7078 const EVT &HiVT) {
7079 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
7080 N.getValueType().getVectorNumElements() &&
7081 "More vector elements requested than available!");
7082 SDValue Lo, Hi;
7083 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
7084 getConstant(0, DL, TLI->getVectorIdxTy(getDataLayout())));
7085 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
7086 getConstant(LoVT.getVectorNumElements(), DL,
7087 TLI->getVectorIdxTy(getDataLayout())));
7088 return std::make_pair(Lo, Hi);
7089 }
7090
ExtractVectorElements(SDValue Op,SmallVectorImpl<SDValue> & Args,unsigned Start,unsigned Count)7091 void SelectionDAG::ExtractVectorElements(SDValue Op,
7092 SmallVectorImpl<SDValue> &Args,
7093 unsigned Start, unsigned Count) {
7094 EVT VT = Op.getValueType();
7095 if (Count == 0)
7096 Count = VT.getVectorNumElements();
7097
7098 EVT EltVT = VT.getVectorElementType();
7099 EVT IdxTy = TLI->getVectorIdxTy(getDataLayout());
7100 SDLoc SL(Op);
7101 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
7102 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
7103 Op, getConstant(i, SL, IdxTy)));
7104 }
7105 }
7106
7107 // getAddressSpace - Return the address space this GlobalAddress belongs to.
getAddressSpace() const7108 unsigned GlobalAddressSDNode::getAddressSpace() const {
7109 return getGlobal()->getType()->getAddressSpace();
7110 }
7111
7112
getType() const7113 Type *ConstantPoolSDNode::getType() const {
7114 if (isMachineConstantPoolEntry())
7115 return Val.MachineCPVal->getType();
7116 return Val.ConstVal->getType();
7117 }
7118
isConstantSplat(APInt & SplatValue,APInt & SplatUndef,unsigned & SplatBitSize,bool & HasAnyUndefs,unsigned MinSplatBits,bool isBigEndian) const7119 bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
7120 APInt &SplatUndef,
7121 unsigned &SplatBitSize,
7122 bool &HasAnyUndefs,
7123 unsigned MinSplatBits,
7124 bool isBigEndian) const {
7125 EVT VT = getValueType(0);
7126 assert(VT.isVector() && "Expected a vector type");
7127 unsigned sz = VT.getSizeInBits();
7128 if (MinSplatBits > sz)
7129 return false;
7130
7131 SplatValue = APInt(sz, 0);
7132 SplatUndef = APInt(sz, 0);
7133
7134 // Get the bits. Bits with undefined values (when the corresponding element
7135 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
7136 // in SplatValue. If any of the values are not constant, give up and return
7137 // false.
7138 unsigned int nOps = getNumOperands();
7139 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
7140 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
7141
7142 for (unsigned j = 0; j < nOps; ++j) {
7143 unsigned i = isBigEndian ? nOps-1-j : j;
7144 SDValue OpVal = getOperand(i);
7145 unsigned BitPos = j * EltBitSize;
7146
7147 if (OpVal.isUndef())
7148 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
7149 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
7150 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
7151 zextOrTrunc(sz) << BitPos;
7152 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
7153 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
7154 else
7155 return false;
7156 }
7157
7158 // The build_vector is all constants or undefs. Find the smallest element
7159 // size that splats the vector.
7160
7161 HasAnyUndefs = (SplatUndef != 0);
7162 while (sz > 8) {
7163
7164 unsigned HalfSize = sz / 2;
7165 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
7166 APInt LowValue = SplatValue.trunc(HalfSize);
7167 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
7168 APInt LowUndef = SplatUndef.trunc(HalfSize);
7169
7170 // If the two halves do not match (ignoring undef bits), stop here.
7171 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
7172 MinSplatBits > HalfSize)
7173 break;
7174
7175 SplatValue = HighValue | LowValue;
7176 SplatUndef = HighUndef & LowUndef;
7177
7178 sz = HalfSize;
7179 }
7180
7181 SplatBitSize = sz;
7182 return true;
7183 }
7184
getSplatValue(BitVector * UndefElements) const7185 SDValue BuildVectorSDNode::getSplatValue(BitVector *UndefElements) const {
7186 if (UndefElements) {
7187 UndefElements->clear();
7188 UndefElements->resize(getNumOperands());
7189 }
7190 SDValue Splatted;
7191 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
7192 SDValue Op = getOperand(i);
7193 if (Op.isUndef()) {
7194 if (UndefElements)
7195 (*UndefElements)[i] = true;
7196 } else if (!Splatted) {
7197 Splatted = Op;
7198 } else if (Splatted != Op) {
7199 return SDValue();
7200 }
7201 }
7202
7203 if (!Splatted) {
7204 assert(getOperand(0).isUndef() &&
7205 "Can only have a splat without a constant for all undefs.");
7206 return getOperand(0);
7207 }
7208
7209 return Splatted;
7210 }
7211
7212 ConstantSDNode *
getConstantSplatNode(BitVector * UndefElements) const7213 BuildVectorSDNode::getConstantSplatNode(BitVector *UndefElements) const {
7214 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
7215 }
7216
7217 ConstantFPSDNode *
getConstantFPSplatNode(BitVector * UndefElements) const7218 BuildVectorSDNode::getConstantFPSplatNode(BitVector *UndefElements) const {
7219 return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
7220 }
7221
7222 int32_t
getConstantFPSplatPow2ToLog2Int(BitVector * UndefElements,uint32_t BitWidth) const7223 BuildVectorSDNode::getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements,
7224 uint32_t BitWidth) const {
7225 if (ConstantFPSDNode *CN =
7226 dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
7227 bool IsExact;
7228 APSInt IntVal(BitWidth);
7229 const APFloat &APF = CN->getValueAPF();
7230 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
7231 APFloat::opOK ||
7232 !IsExact)
7233 return -1;
7234
7235 return IntVal.exactLogBase2();
7236 }
7237 return -1;
7238 }
7239
isConstant() const7240 bool BuildVectorSDNode::isConstant() const {
7241 for (const SDValue &Op : op_values()) {
7242 unsigned Opc = Op.getOpcode();
7243 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
7244 return false;
7245 }
7246 return true;
7247 }
7248
isSplatMask(const int * Mask,EVT VT)7249 bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
7250 // Find the first non-undef value in the shuffle mask.
7251 unsigned i, e;
7252 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
7253 /* search */;
7254
7255 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
7256
7257 // Make sure all remaining elements are either undef or the same as the first
7258 // non-undef value.
7259 for (int Idx = Mask[i]; i != e; ++i)
7260 if (Mask[i] >= 0 && Mask[i] != Idx)
7261 return false;
7262 return true;
7263 }
7264
7265 // \brief Returns the SDNode if it is a constant integer BuildVector
7266 // or constant integer.
isConstantIntBuildVectorOrConstantInt(SDValue N)7267 SDNode *SelectionDAG::isConstantIntBuildVectorOrConstantInt(SDValue N) {
7268 if (isa<ConstantSDNode>(N))
7269 return N.getNode();
7270 if (ISD::isBuildVectorOfConstantSDNodes(N.getNode()))
7271 return N.getNode();
7272 // Treat a GlobalAddress supporting constant offset folding as a
7273 // constant integer.
7274 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N))
7275 if (GA->getOpcode() == ISD::GlobalAddress &&
7276 TLI->isOffsetFoldingLegal(GA))
7277 return GA;
7278 return nullptr;
7279 }
7280
7281 #ifndef NDEBUG
checkForCyclesHelper(const SDNode * N,SmallPtrSetImpl<const SDNode * > & Visited,SmallPtrSetImpl<const SDNode * > & Checked,const llvm::SelectionDAG * DAG)7282 static void checkForCyclesHelper(const SDNode *N,
7283 SmallPtrSetImpl<const SDNode*> &Visited,
7284 SmallPtrSetImpl<const SDNode*> &Checked,
7285 const llvm::SelectionDAG *DAG) {
7286 // If this node has already been checked, don't check it again.
7287 if (Checked.count(N))
7288 return;
7289
7290 // If a node has already been visited on this depth-first walk, reject it as
7291 // a cycle.
7292 if (!Visited.insert(N).second) {
7293 errs() << "Detected cycle in SelectionDAG\n";
7294 dbgs() << "Offending node:\n";
7295 N->dumprFull(DAG); dbgs() << "\n";
7296 abort();
7297 }
7298
7299 for (const SDValue &Op : N->op_values())
7300 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
7301
7302 Checked.insert(N);
7303 Visited.erase(N);
7304 }
7305 #endif
7306
checkForCycles(const llvm::SDNode * N,const llvm::SelectionDAG * DAG,bool force)7307 void llvm::checkForCycles(const llvm::SDNode *N,
7308 const llvm::SelectionDAG *DAG,
7309 bool force) {
7310 #ifndef NDEBUG
7311 bool check = force;
7312 #ifdef EXPENSIVE_CHECKS
7313 check = true;
7314 #endif // EXPENSIVE_CHECKS
7315 if (check) {
7316 assert(N && "Checking nonexistent SDNode");
7317 SmallPtrSet<const SDNode*, 32> visited;
7318 SmallPtrSet<const SDNode*, 32> checked;
7319 checkForCyclesHelper(N, visited, checked, DAG);
7320 }
7321 #endif // !NDEBUG
7322 }
7323
checkForCycles(const llvm::SelectionDAG * DAG,bool force)7324 void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
7325 checkForCycles(DAG->getRoot().getNode(), DAG, force);
7326 }
7327