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