1 //===- SelectionDAGDumper.cpp - Implement SelectionDAG::dump() ------------===//
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::dump method and friends.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/APFloat.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/None.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/CodeGen/ISDOpcodes.h"
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineMemOperand.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
24 #include "llvm/CodeGen/SelectionDAGNodes.h"
25 #include "llvm/CodeGen/TargetInstrInfo.h"
26 #include "llvm/CodeGen/TargetLowering.h"
27 #include "llvm/CodeGen/TargetRegisterInfo.h"
28 #include "llvm/CodeGen/TargetSubtargetInfo.h"
29 #include "llvm/CodeGen/ValueTypes.h"
30 #include "llvm/Config/llvm-config.h"
31 #include "llvm/IR/BasicBlock.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/DebugInfoMetadata.h"
34 #include "llvm/IR/DebugLoc.h"
35 #include "llvm/IR/Function.h"
36 #include "llvm/IR/Intrinsics.h"
37 #include "llvm/IR/ModuleSlotTracker.h"
38 #include "llvm/IR/Value.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/Compiler.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/ErrorHandling.h"
44 #include "llvm/Support/MachineValueType.h"
45 #include "llvm/Support/Printable.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include "llvm/Target/TargetIntrinsicInfo.h"
48 #include "llvm/Target/TargetMachine.h"
49 #include <cstdint>
50 #include <iterator>
51 
52 using namespace llvm;
53 
54 static cl::opt<bool>
55 VerboseDAGDumping("dag-dump-verbose", cl::Hidden,
56                   cl::desc("Display more information when dumping selection "
57                            "DAG nodes."));
58 
59 std::string SDNode::getOperationName(const SelectionDAG *G) const {
60   switch (getOpcode()) {
61   default:
62     if (getOpcode() < ISD::BUILTIN_OP_END)
63       return "<<Unknown DAG Node>>";
64     if (isMachineOpcode()) {
65       if (G)
66         if (const TargetInstrInfo *TII = G->getSubtarget().getInstrInfo())
67           if (getMachineOpcode() < TII->getNumOpcodes())
68             return TII->getName(getMachineOpcode());
69       return "<<Unknown Machine Node #" + utostr(getOpcode()) + ">>";
70     }
71     if (G) {
72       const TargetLowering &TLI = G->getTargetLoweringInfo();
73       const char *Name = TLI.getTargetNodeName(getOpcode());
74       if (Name) return Name;
75       return "<<Unknown Target Node #" + utostr(getOpcode()) + ">>";
76     }
77     return "<<Unknown Node #" + utostr(getOpcode()) + ">>";
78 
79 #ifndef NDEBUG
80   case ISD::DELETED_NODE:               return "<<Deleted Node!>>";
81 #endif
82   case ISD::PREFETCH:                   return "Prefetch";
83   case ISD::ATOMIC_FENCE:               return "AtomicFence";
84   case ISD::ATOMIC_CMP_SWAP:            return "AtomicCmpSwap";
85   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: return "AtomicCmpSwapWithSuccess";
86   case ISD::ATOMIC_SWAP:                return "AtomicSwap";
87   case ISD::ATOMIC_LOAD_ADD:            return "AtomicLoadAdd";
88   case ISD::ATOMIC_LOAD_SUB:            return "AtomicLoadSub";
89   case ISD::ATOMIC_LOAD_AND:            return "AtomicLoadAnd";
90   case ISD::ATOMIC_LOAD_CLR:            return "AtomicLoadClr";
91   case ISD::ATOMIC_LOAD_OR:             return "AtomicLoadOr";
92   case ISD::ATOMIC_LOAD_XOR:            return "AtomicLoadXor";
93   case ISD::ATOMIC_LOAD_NAND:           return "AtomicLoadNand";
94   case ISD::ATOMIC_LOAD_MIN:            return "AtomicLoadMin";
95   case ISD::ATOMIC_LOAD_MAX:            return "AtomicLoadMax";
96   case ISD::ATOMIC_LOAD_UMIN:           return "AtomicLoadUMin";
97   case ISD::ATOMIC_LOAD_UMAX:           return "AtomicLoadUMax";
98   case ISD::ATOMIC_LOAD:                return "AtomicLoad";
99   case ISD::ATOMIC_STORE:               return "AtomicStore";
100   case ISD::PCMARKER:                   return "PCMarker";
101   case ISD::READCYCLECOUNTER:           return "ReadCycleCounter";
102   case ISD::SRCVALUE:                   return "SrcValue";
103   case ISD::MDNODE_SDNODE:              return "MDNode";
104   case ISD::EntryToken:                 return "EntryToken";
105   case ISD::TokenFactor:                return "TokenFactor";
106   case ISD::AssertSext:                 return "AssertSext";
107   case ISD::AssertZext:                 return "AssertZext";
108 
109   case ISD::BasicBlock:                 return "BasicBlock";
110   case ISD::VALUETYPE:                  return "ValueType";
111   case ISD::Register:                   return "Register";
112   case ISD::RegisterMask:               return "RegisterMask";
113   case ISD::Constant:
114     if (cast<ConstantSDNode>(this)->isOpaque())
115       return "OpaqueConstant";
116     return "Constant";
117   case ISD::ConstantFP:                 return "ConstantFP";
118   case ISD::GlobalAddress:              return "GlobalAddress";
119   case ISD::GlobalTLSAddress:           return "GlobalTLSAddress";
120   case ISD::FrameIndex:                 return "FrameIndex";
121   case ISD::JumpTable:                  return "JumpTable";
122   case ISD::GLOBAL_OFFSET_TABLE:        return "GLOBAL_OFFSET_TABLE";
123   case ISD::RETURNADDR:                 return "RETURNADDR";
124   case ISD::ADDROFRETURNADDR:           return "ADDROFRETURNADDR";
125   case ISD::FRAMEADDR:                  return "FRAMEADDR";
126   case ISD::LOCAL_RECOVER:              return "LOCAL_RECOVER";
127   case ISD::READ_REGISTER:              return "READ_REGISTER";
128   case ISD::WRITE_REGISTER:             return "WRITE_REGISTER";
129   case ISD::FRAME_TO_ARGS_OFFSET:       return "FRAME_TO_ARGS_OFFSET";
130   case ISD::EH_DWARF_CFA:               return "EH_DWARF_CFA";
131   case ISD::EH_RETURN:                  return "EH_RETURN";
132   case ISD::EH_SJLJ_SETJMP:             return "EH_SJLJ_SETJMP";
133   case ISD::EH_SJLJ_LONGJMP:            return "EH_SJLJ_LONGJMP";
134   case ISD::EH_SJLJ_SETUP_DISPATCH:     return "EH_SJLJ_SETUP_DISPATCH";
135   case ISD::ConstantPool:               return "ConstantPool";
136   case ISD::TargetIndex:                return "TargetIndex";
137   case ISD::ExternalSymbol:             return "ExternalSymbol";
138   case ISD::BlockAddress:               return "BlockAddress";
139   case ISD::INTRINSIC_WO_CHAIN:
140   case ISD::INTRINSIC_VOID:
141   case ISD::INTRINSIC_W_CHAIN: {
142     unsigned OpNo = getOpcode() == ISD::INTRINSIC_WO_CHAIN ? 0 : 1;
143     unsigned IID = cast<ConstantSDNode>(getOperand(OpNo))->getZExtValue();
144     if (IID < Intrinsic::num_intrinsics)
145       return Intrinsic::getName((Intrinsic::ID)IID, None);
146     else if (const TargetIntrinsicInfo *TII = G->getTarget().getIntrinsicInfo())
147       return TII->getName(IID);
148     llvm_unreachable("Invalid intrinsic ID");
149   }
150 
151   case ISD::BUILD_VECTOR:               return "BUILD_VECTOR";
152   case ISD::TargetConstant:
153     if (cast<ConstantSDNode>(this)->isOpaque())
154       return "OpaqueTargetConstant";
155     return "TargetConstant";
156   case ISD::TargetConstantFP:           return "TargetConstantFP";
157   case ISD::TargetGlobalAddress:        return "TargetGlobalAddress";
158   case ISD::TargetGlobalTLSAddress:     return "TargetGlobalTLSAddress";
159   case ISD::TargetFrameIndex:           return "TargetFrameIndex";
160   case ISD::TargetJumpTable:            return "TargetJumpTable";
161   case ISD::TargetConstantPool:         return "TargetConstantPool";
162   case ISD::TargetExternalSymbol:       return "TargetExternalSymbol";
163   case ISD::MCSymbol:                   return "MCSymbol";
164   case ISD::TargetBlockAddress:         return "TargetBlockAddress";
165 
166   case ISD::CopyToReg:                  return "CopyToReg";
167   case ISD::CopyFromReg:                return "CopyFromReg";
168   case ISD::UNDEF:                      return "undef";
169   case ISD::MERGE_VALUES:               return "merge_values";
170   case ISD::INLINEASM:                  return "inlineasm";
171   case ISD::EH_LABEL:                   return "eh_label";
172   case ISD::HANDLENODE:                 return "handlenode";
173 
174   // Unary operators
175   case ISD::FABS:                       return "fabs";
176   case ISD::FMINNUM:                    return "fminnum";
177   case ISD::FMAXNUM:                    return "fmaxnum";
178   case ISD::FMINNAN:                    return "fminnan";
179   case ISD::FMAXNAN:                    return "fmaxnan";
180   case ISD::FNEG:                       return "fneg";
181   case ISD::FSQRT:                      return "fsqrt";
182   case ISD::STRICT_FSQRT:               return "strict_fsqrt";
183   case ISD::FSIN:                       return "fsin";
184   case ISD::STRICT_FSIN:                return "strict_fsin";
185   case ISD::FCOS:                       return "fcos";
186   case ISD::STRICT_FCOS:                return "strict_fcos";
187   case ISD::FSINCOS:                    return "fsincos";
188   case ISD::FTRUNC:                     return "ftrunc";
189   case ISD::FFLOOR:                     return "ffloor";
190   case ISD::FCEIL:                      return "fceil";
191   case ISD::FRINT:                      return "frint";
192   case ISD::STRICT_FRINT:               return "strict_frint";
193   case ISD::FNEARBYINT:                 return "fnearbyint";
194   case ISD::STRICT_FNEARBYINT:          return "strict_fnearbyint";
195   case ISD::FROUND:                     return "fround";
196   case ISD::FEXP:                       return "fexp";
197   case ISD::STRICT_FEXP:                return "strict_fexp";
198   case ISD::FEXP2:                      return "fexp2";
199   case ISD::STRICT_FEXP2:               return "strict_fexp2";
200   case ISD::FLOG:                       return "flog";
201   case ISD::STRICT_FLOG:                return "strict_flog";
202   case ISD::FLOG2:                      return "flog2";
203   case ISD::STRICT_FLOG2:               return "strict_flog2";
204   case ISD::FLOG10:                     return "flog10";
205   case ISD::STRICT_FLOG10:              return "strict_flog10";
206 
207   // Binary operators
208   case ISD::ADD:                        return "add";
209   case ISD::SUB:                        return "sub";
210   case ISD::MUL:                        return "mul";
211   case ISD::MULHU:                      return "mulhu";
212   case ISD::MULHS:                      return "mulhs";
213   case ISD::SDIV:                       return "sdiv";
214   case ISD::UDIV:                       return "udiv";
215   case ISD::SREM:                       return "srem";
216   case ISD::UREM:                       return "urem";
217   case ISD::SMUL_LOHI:                  return "smul_lohi";
218   case ISD::UMUL_LOHI:                  return "umul_lohi";
219   case ISD::SDIVREM:                    return "sdivrem";
220   case ISD::UDIVREM:                    return "udivrem";
221   case ISD::AND:                        return "and";
222   case ISD::OR:                         return "or";
223   case ISD::XOR:                        return "xor";
224   case ISD::SHL:                        return "shl";
225   case ISD::SRA:                        return "sra";
226   case ISD::SRL:                        return "srl";
227   case ISD::ROTL:                       return "rotl";
228   case ISD::ROTR:                       return "rotr";
229   case ISD::FADD:                       return "fadd";
230   case ISD::STRICT_FADD:                return "strict_fadd";
231   case ISD::FSUB:                       return "fsub";
232   case ISD::STRICT_FSUB:                return "strict_fsub";
233   case ISD::FMUL:                       return "fmul";
234   case ISD::STRICT_FMUL:                return "strict_fmul";
235   case ISD::FDIV:                       return "fdiv";
236   case ISD::STRICT_FDIV:                return "strict_fdiv";
237   case ISD::FMA:                        return "fma";
238   case ISD::STRICT_FMA:                 return "strict_fma";
239   case ISD::FMAD:                       return "fmad";
240   case ISD::FREM:                       return "frem";
241   case ISD::STRICT_FREM:                return "strict_frem";
242   case ISD::FCOPYSIGN:                  return "fcopysign";
243   case ISD::FGETSIGN:                   return "fgetsign";
244   case ISD::FCANONICALIZE:              return "fcanonicalize";
245   case ISD::FPOW:                       return "fpow";
246   case ISD::STRICT_FPOW:                return "strict_fpow";
247   case ISD::SMIN:                       return "smin";
248   case ISD::SMAX:                       return "smax";
249   case ISD::UMIN:                       return "umin";
250   case ISD::UMAX:                       return "umax";
251 
252   case ISD::FPOWI:                      return "fpowi";
253   case ISD::STRICT_FPOWI:               return "strict_fpowi";
254   case ISD::SETCC:                      return "setcc";
255   case ISD::SETCCCARRY:                 return "setcccarry";
256   case ISD::SELECT:                     return "select";
257   case ISD::VSELECT:                    return "vselect";
258   case ISD::SELECT_CC:                  return "select_cc";
259   case ISD::INSERT_VECTOR_ELT:          return "insert_vector_elt";
260   case ISD::EXTRACT_VECTOR_ELT:         return "extract_vector_elt";
261   case ISD::CONCAT_VECTORS:             return "concat_vectors";
262   case ISD::INSERT_SUBVECTOR:           return "insert_subvector";
263   case ISD::EXTRACT_SUBVECTOR:          return "extract_subvector";
264   case ISD::SCALAR_TO_VECTOR:           return "scalar_to_vector";
265   case ISD::VECTOR_SHUFFLE:             return "vector_shuffle";
266   case ISD::CARRY_FALSE:                return "carry_false";
267   case ISD::ADDC:                       return "addc";
268   case ISD::ADDE:                       return "adde";
269   case ISD::ADDCARRY:                   return "addcarry";
270   case ISD::SADDO:                      return "saddo";
271   case ISD::UADDO:                      return "uaddo";
272   case ISD::SSUBO:                      return "ssubo";
273   case ISD::USUBO:                      return "usubo";
274   case ISD::SMULO:                      return "smulo";
275   case ISD::UMULO:                      return "umulo";
276   case ISD::SUBC:                       return "subc";
277   case ISD::SUBE:                       return "sube";
278   case ISD::SUBCARRY:                   return "subcarry";
279   case ISD::SHL_PARTS:                  return "shl_parts";
280   case ISD::SRA_PARTS:                  return "sra_parts";
281   case ISD::SRL_PARTS:                  return "srl_parts";
282 
283   // Conversion operators.
284   case ISD::SIGN_EXTEND:                return "sign_extend";
285   case ISD::ZERO_EXTEND:                return "zero_extend";
286   case ISD::ANY_EXTEND:                 return "any_extend";
287   case ISD::SIGN_EXTEND_INREG:          return "sign_extend_inreg";
288   case ISD::ANY_EXTEND_VECTOR_INREG:    return "any_extend_vector_inreg";
289   case ISD::SIGN_EXTEND_VECTOR_INREG:   return "sign_extend_vector_inreg";
290   case ISD::ZERO_EXTEND_VECTOR_INREG:   return "zero_extend_vector_inreg";
291   case ISD::TRUNCATE:                   return "truncate";
292   case ISD::FP_ROUND:                   return "fp_round";
293   case ISD::FLT_ROUNDS_:                return "flt_rounds";
294   case ISD::FP_ROUND_INREG:             return "fp_round_inreg";
295   case ISD::FP_EXTEND:                  return "fp_extend";
296 
297   case ISD::SINT_TO_FP:                 return "sint_to_fp";
298   case ISD::UINT_TO_FP:                 return "uint_to_fp";
299   case ISD::FP_TO_SINT:                 return "fp_to_sint";
300   case ISD::FP_TO_UINT:                 return "fp_to_uint";
301   case ISD::BITCAST:                    return "bitcast";
302   case ISD::ADDRSPACECAST:              return "addrspacecast";
303   case ISD::FP16_TO_FP:                 return "fp16_to_fp";
304   case ISD::FP_TO_FP16:                 return "fp_to_fp16";
305 
306     // Control flow instructions
307   case ISD::BR:                         return "br";
308   case ISD::BRIND:                      return "brind";
309   case ISD::BR_JT:                      return "br_jt";
310   case ISD::BRCOND:                     return "brcond";
311   case ISD::BR_CC:                      return "br_cc";
312   case ISD::CALLSEQ_START:              return "callseq_start";
313   case ISD::CALLSEQ_END:                return "callseq_end";
314 
315     // EH instructions
316   case ISD::CATCHRET:                   return "catchret";
317   case ISD::CLEANUPRET:                 return "cleanupret";
318 
319     // Other operators
320   case ISD::LOAD:                       return "load";
321   case ISD::STORE:                      return "store";
322   case ISD::MLOAD:                      return "masked_load";
323   case ISD::MSTORE:                     return "masked_store";
324   case ISD::MGATHER:                    return "masked_gather";
325   case ISD::MSCATTER:                   return "masked_scatter";
326   case ISD::VAARG:                      return "vaarg";
327   case ISD::VACOPY:                     return "vacopy";
328   case ISD::VAEND:                      return "vaend";
329   case ISD::VASTART:                    return "vastart";
330   case ISD::DYNAMIC_STACKALLOC:         return "dynamic_stackalloc";
331   case ISD::EXTRACT_ELEMENT:            return "extract_element";
332   case ISD::BUILD_PAIR:                 return "build_pair";
333   case ISD::STACKSAVE:                  return "stacksave";
334   case ISD::STACKRESTORE:               return "stackrestore";
335   case ISD::TRAP:                       return "trap";
336   case ISD::DEBUGTRAP:                  return "debugtrap";
337   case ISD::LIFETIME_START:             return "lifetime.start";
338   case ISD::LIFETIME_END:               return "lifetime.end";
339   case ISD::GC_TRANSITION_START:        return "gc_transition.start";
340   case ISD::GC_TRANSITION_END:          return "gc_transition.end";
341   case ISD::GET_DYNAMIC_AREA_OFFSET:    return "get.dynamic.area.offset";
342 
343   // Bit manipulation
344   case ISD::ABS:                        return "abs";
345   case ISD::BITREVERSE:                 return "bitreverse";
346   case ISD::BSWAP:                      return "bswap";
347   case ISD::CTPOP:                      return "ctpop";
348   case ISD::CTTZ:                       return "cttz";
349   case ISD::CTTZ_ZERO_UNDEF:            return "cttz_zero_undef";
350   case ISD::CTLZ:                       return "ctlz";
351   case ISD::CTLZ_ZERO_UNDEF:            return "ctlz_zero_undef";
352 
353   // Trampolines
354   case ISD::INIT_TRAMPOLINE:            return "init_trampoline";
355   case ISD::ADJUST_TRAMPOLINE:          return "adjust_trampoline";
356 
357   case ISD::CONDCODE:
358     switch (cast<CondCodeSDNode>(this)->get()) {
359     default: llvm_unreachable("Unknown setcc condition!");
360     case ISD::SETOEQ:                   return "setoeq";
361     case ISD::SETOGT:                   return "setogt";
362     case ISD::SETOGE:                   return "setoge";
363     case ISD::SETOLT:                   return "setolt";
364     case ISD::SETOLE:                   return "setole";
365     case ISD::SETONE:                   return "setone";
366 
367     case ISD::SETO:                     return "seto";
368     case ISD::SETUO:                    return "setuo";
369     case ISD::SETUEQ:                   return "setueq";
370     case ISD::SETUGT:                   return "setugt";
371     case ISD::SETUGE:                   return "setuge";
372     case ISD::SETULT:                   return "setult";
373     case ISD::SETULE:                   return "setule";
374     case ISD::SETUNE:                   return "setune";
375 
376     case ISD::SETEQ:                    return "seteq";
377     case ISD::SETGT:                    return "setgt";
378     case ISD::SETGE:                    return "setge";
379     case ISD::SETLT:                    return "setlt";
380     case ISD::SETLE:                    return "setle";
381     case ISD::SETNE:                    return "setne";
382 
383     case ISD::SETTRUE:                  return "settrue";
384     case ISD::SETTRUE2:                 return "settrue2";
385     case ISD::SETFALSE:                 return "setfalse";
386     case ISD::SETFALSE2:                return "setfalse2";
387     }
388   case ISD::VECREDUCE_FADD:             return "vecreduce_fadd";
389   case ISD::VECREDUCE_STRICT_FADD:      return "vecreduce_strict_fadd";
390   case ISD::VECREDUCE_FMUL:             return "vecreduce_fmul";
391   case ISD::VECREDUCE_STRICT_FMUL:      return "vecreduce_strict_fmul";
392   case ISD::VECREDUCE_ADD:              return "vecreduce_add";
393   case ISD::VECREDUCE_MUL:              return "vecreduce_mul";
394   case ISD::VECREDUCE_AND:              return "vecreduce_and";
395   case ISD::VECREDUCE_OR:               return "vecreduce_or";
396   case ISD::VECREDUCE_XOR:              return "vecreduce_xor";
397   case ISD::VECREDUCE_SMAX:             return "vecreduce_smax";
398   case ISD::VECREDUCE_SMIN:             return "vecreduce_smin";
399   case ISD::VECREDUCE_UMAX:             return "vecreduce_umax";
400   case ISD::VECREDUCE_UMIN:             return "vecreduce_umin";
401   case ISD::VECREDUCE_FMAX:             return "vecreduce_fmax";
402   case ISD::VECREDUCE_FMIN:             return "vecreduce_fmin";
403   }
404 }
405 
406 const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
407   switch (AM) {
408   default:              return "";
409   case ISD::PRE_INC:    return "<pre-inc>";
410   case ISD::PRE_DEC:    return "<pre-dec>";
411   case ISD::POST_INC:   return "<post-inc>";
412   case ISD::POST_DEC:   return "<post-dec>";
413   }
414 }
415 
416 static Printable PrintNodeId(const SDNode &Node) {
417   return Printable([&Node](raw_ostream &OS) {
418 #ifndef NDEBUG
419     OS << 't' << Node.PersistentId;
420 #else
421     OS << (const void*)&Node;
422 #endif
423   });
424 }
425 
426 // Print the MMO with more information from the SelectionDAG.
427 static void printMemOperand(raw_ostream &OS, const MachineMemOperand &MMO,
428                             const MachineFunction *MF, const Module *M,
429                             const MachineFrameInfo *MFI,
430                             const TargetInstrInfo *TII, LLVMContext &Ctx) {
431   ModuleSlotTracker MST(M);
432   if (MF)
433     MST.incorporateFunction(MF->getFunction());
434   SmallVector<StringRef, 0> SSNs;
435   MMO.print(OS, MST, SSNs, Ctx, MFI, TII);
436 }
437 
438 static void printMemOperand(raw_ostream &OS, const MachineMemOperand &MMO,
439                             const SelectionDAG *G) {
440   if (G) {
441     const MachineFunction *MF = &G->getMachineFunction();
442     return printMemOperand(OS, MMO, MF, MF->getFunction().getParent(),
443                            &MF->getFrameInfo(), G->getSubtarget().getInstrInfo(),
444                            *G->getContext());
445   } else {
446     LLVMContext Ctx;
447     return printMemOperand(OS, MMO, /*MF=*/nullptr, /*M=*/nullptr,
448                            /*MFI=*/nullptr, /*TII=*/nullptr, Ctx);
449   }
450 }
451 
452 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
453 LLVM_DUMP_METHOD void SDNode::dump() const { dump(nullptr); }
454 
455 LLVM_DUMP_METHOD void SDNode::dump(const SelectionDAG *G) const {
456   print(dbgs(), G);
457   dbgs() << '\n';
458 }
459 #endif
460 
461 void SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const {
462   for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
463     if (i) OS << ",";
464     if (getValueType(i) == MVT::Other)
465       OS << "ch";
466     else
467       OS << getValueType(i).getEVTString();
468   }
469 }
470 
471 void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
472   if (getFlags().hasNoUnsignedWrap())
473     OS << " nuw";
474 
475   if (getFlags().hasNoSignedWrap())
476     OS << " nsw";
477 
478   if (getFlags().hasExact())
479     OS << " exact";
480 
481   if (getFlags().hasNoNaNs())
482     OS << " nnan";
483 
484   if (getFlags().hasNoInfs())
485     OS << " ninf";
486 
487   if (getFlags().hasNoSignedZeros())
488     OS << " nsz";
489 
490   if (getFlags().hasAllowReciprocal())
491     OS << " arcp";
492 
493   if (getFlags().hasAllowContract())
494     OS << " contract";
495 
496   if (getFlags().hasApproximateFuncs())
497     OS << " afn";
498 
499   if (getFlags().hasAllowReassociation())
500     OS << " reassoc";
501 
502   if (getFlags().hasVectorReduction())
503     OS << " vector-reduction";
504 
505   if (const MachineSDNode *MN = dyn_cast<MachineSDNode>(this)) {
506     if (!MN->memoperands_empty()) {
507       OS << "<";
508       OS << "Mem:";
509       for (MachineSDNode::mmo_iterator i = MN->memoperands_begin(),
510            e = MN->memoperands_end(); i != e; ++i) {
511         printMemOperand(OS, **i, G);
512         if (std::next(i) != e)
513           OS << " ";
514       }
515       OS << ">";
516     }
517   } else if (const ShuffleVectorSDNode *SVN =
518                dyn_cast<ShuffleVectorSDNode>(this)) {
519     OS << "<";
520     for (unsigned i = 0, e = ValueList[0].getVectorNumElements(); i != e; ++i) {
521       int Idx = SVN->getMaskElt(i);
522       if (i) OS << ",";
523       if (Idx < 0)
524         OS << "u";
525       else
526         OS << Idx;
527     }
528     OS << ">";
529   } else if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
530     OS << '<' << CSDN->getAPIntValue() << '>';
531   } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
532     if (&CSDN->getValueAPF().getSemantics() == &APFloat::IEEEsingle())
533       OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
534     else if (&CSDN->getValueAPF().getSemantics() == &APFloat::IEEEdouble())
535       OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
536     else {
537       OS << "<APFloat(";
538       CSDN->getValueAPF().bitcastToAPInt().print(OS, false);
539       OS << ")>";
540     }
541   } else if (const GlobalAddressSDNode *GADN =
542              dyn_cast<GlobalAddressSDNode>(this)) {
543     int64_t offset = GADN->getOffset();
544     OS << '<';
545     GADN->getGlobal()->printAsOperand(OS);
546     OS << '>';
547     if (offset > 0)
548       OS << " + " << offset;
549     else
550       OS << " " << offset;
551     if (unsigned int TF = GADN->getTargetFlags())
552       OS << " [TF=" << TF << ']';
553   } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
554     OS << "<" << FIDN->getIndex() << ">";
555   } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
556     OS << "<" << JTDN->getIndex() << ">";
557     if (unsigned int TF = JTDN->getTargetFlags())
558       OS << " [TF=" << TF << ']';
559   } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
560     int offset = CP->getOffset();
561     if (CP->isMachineConstantPoolEntry())
562       OS << "<" << *CP->getMachineCPVal() << ">";
563     else
564       OS << "<" << *CP->getConstVal() << ">";
565     if (offset > 0)
566       OS << " + " << offset;
567     else
568       OS << " " << offset;
569     if (unsigned int TF = CP->getTargetFlags())
570       OS << " [TF=" << TF << ']';
571   } else if (const TargetIndexSDNode *TI = dyn_cast<TargetIndexSDNode>(this)) {
572     OS << "<" << TI->getIndex() << '+' << TI->getOffset() << ">";
573     if (unsigned TF = TI->getTargetFlags())
574       OS << " [TF=" << TF << ']';
575   } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
576     OS << "<";
577     const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
578     if (LBB)
579       OS << LBB->getName() << " ";
580     OS << (const void*)BBDN->getBasicBlock() << ">";
581   } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
582     OS << ' ' << printReg(R->getReg(),
583                           G ? G->getSubtarget().getRegisterInfo() : nullptr);
584   } else if (const ExternalSymbolSDNode *ES =
585              dyn_cast<ExternalSymbolSDNode>(this)) {
586     OS << "'" << ES->getSymbol() << "'";
587     if (unsigned int TF = ES->getTargetFlags())
588       OS << " [TF=" << TF << ']';
589   } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
590     if (M->getValue())
591       OS << "<" << M->getValue() << ">";
592     else
593       OS << "<null>";
594   } else if (const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(this)) {
595     if (MD->getMD())
596       OS << "<" << MD->getMD() << ">";
597     else
598       OS << "<null>";
599   } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
600     OS << ":" << N->getVT().getEVTString();
601   }
602   else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
603     OS << "<";
604 
605     printMemOperand(OS, *LD->getMemOperand(), G);
606 
607     bool doExt = true;
608     switch (LD->getExtensionType()) {
609     default: doExt = false; break;
610     case ISD::EXTLOAD:  OS << ", anyext"; break;
611     case ISD::SEXTLOAD: OS << ", sext"; break;
612     case ISD::ZEXTLOAD: OS << ", zext"; break;
613     }
614     if (doExt)
615       OS << " from " << LD->getMemoryVT().getEVTString();
616 
617     const char *AM = getIndexedModeName(LD->getAddressingMode());
618     if (*AM)
619       OS << ", " << AM;
620 
621     OS << ">";
622   } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
623     OS << "<";
624     printMemOperand(OS, *ST->getMemOperand(), G);
625 
626     if (ST->isTruncatingStore())
627       OS << ", trunc to " << ST->getMemoryVT().getEVTString();
628 
629     const char *AM = getIndexedModeName(ST->getAddressingMode());
630     if (*AM)
631       OS << ", " << AM;
632 
633     OS << ">";
634   } else if (const MemSDNode* M = dyn_cast<MemSDNode>(this)) {
635     OS << "<";
636     printMemOperand(OS, *M->getMemOperand(), G);
637     OS << ">";
638   } else if (const BlockAddressSDNode *BA =
639                dyn_cast<BlockAddressSDNode>(this)) {
640     int64_t offset = BA->getOffset();
641     OS << "<";
642     BA->getBlockAddress()->getFunction()->printAsOperand(OS, false);
643     OS << ", ";
644     BA->getBlockAddress()->getBasicBlock()->printAsOperand(OS, false);
645     OS << ">";
646     if (offset > 0)
647       OS << " + " << offset;
648     else
649       OS << " " << offset;
650     if (unsigned int TF = BA->getTargetFlags())
651       OS << " [TF=" << TF << ']';
652   } else if (const AddrSpaceCastSDNode *ASC =
653                dyn_cast<AddrSpaceCastSDNode>(this)) {
654     OS << '['
655        << ASC->getSrcAddressSpace()
656        << " -> "
657        << ASC->getDestAddressSpace()
658        << ']';
659   }
660 
661   if (VerboseDAGDumping) {
662     if (unsigned Order = getIROrder())
663         OS << " [ORD=" << Order << ']';
664 
665     if (getNodeId() != -1)
666       OS << " [ID=" << getNodeId() << ']';
667     if (!(isa<ConstantSDNode>(this) || (isa<ConstantFPSDNode>(this))))
668       OS << "# D:" << isDivergent();
669 
670     if (!G)
671       return;
672 
673     DILocation *L = getDebugLoc();
674     if (!L)
675       return;
676 
677     if (auto *Scope = L->getScope())
678       OS << Scope->getFilename();
679     else
680       OS << "<unknown>";
681     OS << ':' << L->getLine();
682     if (unsigned C = L->getColumn())
683       OS << ':' << C;
684   }
685 }
686 
687 /// Return true if this node is so simple that we should just print it inline
688 /// if it appears as an operand.
689 static bool shouldPrintInline(const SDNode &Node) {
690   if (Node.getOpcode() == ISD::EntryToken)
691     return false;
692   return Node.getNumOperands() == 0;
693 }
694 
695 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
696 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
697   for (const SDValue &Op : N->op_values()) {
698     if (shouldPrintInline(*Op.getNode()))
699       continue;
700     if (Op.getNode()->hasOneUse())
701       DumpNodes(Op.getNode(), indent+2, G);
702   }
703 
704   dbgs().indent(indent);
705   N->dump(G);
706 }
707 
708 LLVM_DUMP_METHOD void SelectionDAG::dump() const {
709   dbgs() << "SelectionDAG has " << AllNodes.size() << " nodes:\n";
710 
711   for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
712        I != E; ++I) {
713     const SDNode *N = &*I;
714     if (!N->hasOneUse() && N != getRoot().getNode() &&
715         (!shouldPrintInline(*N) || N->use_empty()))
716       DumpNodes(N, 2, this);
717   }
718 
719   if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
720   dbgs() << "\n\n";
721 }
722 #endif
723 
724 void SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const {
725   OS << PrintNodeId(*this) << ": ";
726   print_types(OS, G);
727   OS << " = " << getOperationName(G);
728   print_details(OS, G);
729 }
730 
731 static bool printOperand(raw_ostream &OS, const SelectionDAG *G,
732                          const SDValue Value) {
733   if (!Value.getNode()) {
734     OS << "<null>";
735     return false;
736   } else if (shouldPrintInline(*Value.getNode())) {
737     OS << Value->getOperationName(G) << ':';
738     Value->print_types(OS, G);
739     Value->print_details(OS, G);
740     return true;
741   } else {
742     OS << PrintNodeId(*Value.getNode());
743     if (unsigned RN = Value.getResNo())
744       OS << ':' << RN;
745     return false;
746   }
747 }
748 
749 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
750 using VisitedSDNodeSet = SmallPtrSet<const SDNode *, 32>;
751 
752 static void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent,
753                        const SelectionDAG *G, VisitedSDNodeSet &once) {
754   if (!once.insert(N).second) // If we've been here before, return now.
755     return;
756 
757   // Dump the current SDNode, but don't end the line yet.
758   OS.indent(indent);
759   N->printr(OS, G);
760 
761   // Having printed this SDNode, walk the children:
762   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
763     if (i) OS << ",";
764     OS << " ";
765 
766     const SDValue Op = N->getOperand(i);
767     bool printedInline = printOperand(OS, G, Op);
768     if (printedInline)
769       once.insert(Op.getNode());
770   }
771 
772   OS << "\n";
773 
774   // Dump children that have grandchildren on their own line(s).
775   for (const SDValue &Op : N->op_values())
776     DumpNodesr(OS, Op.getNode(), indent+2, G, once);
777 }
778 
779 LLVM_DUMP_METHOD void SDNode::dumpr() const {
780   VisitedSDNodeSet once;
781   DumpNodesr(dbgs(), this, 0, nullptr, once);
782 }
783 
784 LLVM_DUMP_METHOD void SDNode::dumpr(const SelectionDAG *G) const {
785   VisitedSDNodeSet once;
786   DumpNodesr(dbgs(), this, 0, G, once);
787 }
788 #endif
789 
790 static void printrWithDepthHelper(raw_ostream &OS, const SDNode *N,
791                                   const SelectionDAG *G, unsigned depth,
792                                   unsigned indent) {
793   if (depth == 0)
794     return;
795 
796   OS.indent(indent);
797 
798   N->print(OS, G);
799 
800   if (depth < 1)
801     return;
802 
803   for (const SDValue &Op : N->op_values()) {
804     // Don't follow chain operands.
805     if (Op.getValueType() == MVT::Other)
806       continue;
807     OS << '\n';
808     printrWithDepthHelper(OS, Op.getNode(), G, depth-1, indent+2);
809   }
810 }
811 
812 void SDNode::printrWithDepth(raw_ostream &OS, const SelectionDAG *G,
813                             unsigned depth) const {
814   printrWithDepthHelper(OS, this, G, depth, 0);
815 }
816 
817 void SDNode::printrFull(raw_ostream &OS, const SelectionDAG *G) const {
818   // Don't print impossibly deep things.
819   printrWithDepth(OS, G, 10);
820 }
821 
822 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
823 LLVM_DUMP_METHOD
824 void SDNode::dumprWithDepth(const SelectionDAG *G, unsigned depth) const {
825   printrWithDepth(dbgs(), G, depth);
826 }
827 
828 LLVM_DUMP_METHOD void SDNode::dumprFull(const SelectionDAG *G) const {
829   // Don't print impossibly deep things.
830   dumprWithDepth(G, 10);
831 }
832 #endif
833 
834 void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
835   printr(OS, G);
836   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
837     if (i) OS << ", "; else OS << " ";
838     printOperand(OS, G, getOperand(i));
839   }
840   if (DebugLoc DL = getDebugLoc()) {
841     OS << ", ";
842     DL.print(OS);
843   }
844 }
845