1 //===- SelectionDAGDumper.cpp - Implement SelectionDAG::dump() ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This implements the SelectionDAG::dump method and friends.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/APFloat.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/None.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/CodeGen/ISDOpcodes.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/CodeGen/MachineMemOperand.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/SelectionDAGNodes.h"
24 #include "llvm/CodeGen/TargetInstrInfo.h"
25 #include "llvm/CodeGen/TargetLowering.h"
26 #include "llvm/CodeGen/TargetRegisterInfo.h"
27 #include "llvm/CodeGen/TargetSubtargetInfo.h"
28 #include "llvm/CodeGen/ValueTypes.h"
29 #include "llvm/Config/llvm-config.h"
30 #include "llvm/IR/BasicBlock.h"
31 #include "llvm/IR/Constants.h"
32 #include "llvm/IR/DebugInfoMetadata.h"
33 #include "llvm/IR/DebugLoc.h"
34 #include "llvm/IR/Function.h"
35 #include "llvm/IR/Intrinsics.h"
36 #include "llvm/IR/ModuleSlotTracker.h"
37 #include "llvm/IR/Value.h"
38 #include "llvm/Support/Casting.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/Compiler.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/MachineValueType.h"
44 #include "llvm/Support/Printable.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include "llvm/Target/TargetIntrinsicInfo.h"
47 #include "llvm/Target/TargetMachine.h"
48 #include "SDNodeDbgValue.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_FADD:           return "AtomicLoadFAdd";
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::SPONENTRY:                  return "SPONENTRY";
128   case ISD::LOCAL_RECOVER:              return "LOCAL_RECOVER";
129   case ISD::READ_REGISTER:              return "READ_REGISTER";
130   case ISD::WRITE_REGISTER:             return "WRITE_REGISTER";
131   case ISD::FRAME_TO_ARGS_OFFSET:       return "FRAME_TO_ARGS_OFFSET";
132   case ISD::EH_DWARF_CFA:               return "EH_DWARF_CFA";
133   case ISD::EH_RETURN:                  return "EH_RETURN";
134   case ISD::EH_SJLJ_SETJMP:             return "EH_SJLJ_SETJMP";
135   case ISD::EH_SJLJ_LONGJMP:            return "EH_SJLJ_LONGJMP";
136   case ISD::EH_SJLJ_SETUP_DISPATCH:     return "EH_SJLJ_SETUP_DISPATCH";
137   case ISD::ConstantPool:               return "ConstantPool";
138   case ISD::TargetIndex:                return "TargetIndex";
139   case ISD::ExternalSymbol:             return "ExternalSymbol";
140   case ISD::BlockAddress:               return "BlockAddress";
141   case ISD::INTRINSIC_WO_CHAIN:
142   case ISD::INTRINSIC_VOID:
143   case ISD::INTRINSIC_W_CHAIN: {
144     unsigned OpNo = getOpcode() == ISD::INTRINSIC_WO_CHAIN ? 0 : 1;
145     unsigned IID = cast<ConstantSDNode>(getOperand(OpNo))->getZExtValue();
146     if (IID < Intrinsic::num_intrinsics)
147       return Intrinsic::getName((Intrinsic::ID)IID, None);
148     else if (!G)
149       return "Unknown intrinsic";
150     else if (const TargetIntrinsicInfo *TII = G->getTarget().getIntrinsicInfo())
151       return TII->getName(IID);
152     llvm_unreachable("Invalid intrinsic ID");
153   }
154 
155   case ISD::BUILD_VECTOR:               return "BUILD_VECTOR";
156   case ISD::TargetConstant:
157     if (cast<ConstantSDNode>(this)->isOpaque())
158       return "OpaqueTargetConstant";
159     return "TargetConstant";
160   case ISD::TargetConstantFP:           return "TargetConstantFP";
161   case ISD::TargetGlobalAddress:        return "TargetGlobalAddress";
162   case ISD::TargetGlobalTLSAddress:     return "TargetGlobalTLSAddress";
163   case ISD::TargetFrameIndex:           return "TargetFrameIndex";
164   case ISD::TargetJumpTable:            return "TargetJumpTable";
165   case ISD::TargetConstantPool:         return "TargetConstantPool";
166   case ISD::TargetExternalSymbol:       return "TargetExternalSymbol";
167   case ISD::MCSymbol:                   return "MCSymbol";
168   case ISD::TargetBlockAddress:         return "TargetBlockAddress";
169 
170   case ISD::CopyToReg:                  return "CopyToReg";
171   case ISD::CopyFromReg:                return "CopyFromReg";
172   case ISD::UNDEF:                      return "undef";
173   case ISD::MERGE_VALUES:               return "merge_values";
174   case ISD::INLINEASM:                  return "inlineasm";
175   case ISD::INLINEASM_BR:               return "inlineasm_br";
176   case ISD::EH_LABEL:                   return "eh_label";
177   case ISD::ANNOTATION_LABEL:           return "annotation_label";
178   case ISD::HANDLENODE:                 return "handlenode";
179 
180   // Unary operators
181   case ISD::FABS:                       return "fabs";
182   case ISD::FMINNUM:                    return "fminnum";
183   case ISD::STRICT_FMINNUM:             return "strict_fminnum";
184   case ISD::FMAXNUM:                    return "fmaxnum";
185   case ISD::STRICT_FMAXNUM:             return "strict_fmaxnum";
186   case ISD::FMINNUM_IEEE:               return "fminnum_ieee";
187   case ISD::FMAXNUM_IEEE:               return "fmaxnum_ieee";
188   case ISD::FMINIMUM:                   return "fminimum";
189   case ISD::STRICT_FMINIMUM:            return "strict_fminimum";
190   case ISD::FMAXIMUM:                   return "fmaximum";
191   case ISD::STRICT_FMAXIMUM:            return "strict_fmaximum";
192   case ISD::FNEG:                       return "fneg";
193   case ISD::FSQRT:                      return "fsqrt";
194   case ISD::STRICT_FSQRT:               return "strict_fsqrt";
195   case ISD::FCBRT:                      return "fcbrt";
196   case ISD::FSIN:                       return "fsin";
197   case ISD::STRICT_FSIN:                return "strict_fsin";
198   case ISD::FCOS:                       return "fcos";
199   case ISD::STRICT_FCOS:                return "strict_fcos";
200   case ISD::FSINCOS:                    return "fsincos";
201   case ISD::FTRUNC:                     return "ftrunc";
202   case ISD::STRICT_FTRUNC:              return "strict_ftrunc";
203   case ISD::FFLOOR:                     return "ffloor";
204   case ISD::STRICT_FFLOOR:              return "strict_ffloor";
205   case ISD::FCEIL:                      return "fceil";
206   case ISD::STRICT_FCEIL:               return "strict_fceil";
207   case ISD::FRINT:                      return "frint";
208   case ISD::STRICT_FRINT:               return "strict_frint";
209   case ISD::FNEARBYINT:                 return "fnearbyint";
210   case ISD::STRICT_FNEARBYINT:          return "strict_fnearbyint";
211   case ISD::FROUND:                     return "fround";
212   case ISD::STRICT_FROUND:              return "strict_fround";
213   case ISD::FEXP:                       return "fexp";
214   case ISD::STRICT_FEXP:                return "strict_fexp";
215   case ISD::FEXP2:                      return "fexp2";
216   case ISD::STRICT_FEXP2:               return "strict_fexp2";
217   case ISD::FLOG:                       return "flog";
218   case ISD::STRICT_FLOG:                return "strict_flog";
219   case ISD::FLOG2:                      return "flog2";
220   case ISD::STRICT_FLOG2:               return "strict_flog2";
221   case ISD::FLOG10:                     return "flog10";
222   case ISD::STRICT_FLOG10:              return "strict_flog10";
223 
224   // Binary operators
225   case ISD::ADD:                        return "add";
226   case ISD::SUB:                        return "sub";
227   case ISD::MUL:                        return "mul";
228   case ISD::MULHU:                      return "mulhu";
229   case ISD::MULHS:                      return "mulhs";
230   case ISD::SDIV:                       return "sdiv";
231   case ISD::UDIV:                       return "udiv";
232   case ISD::SREM:                       return "srem";
233   case ISD::UREM:                       return "urem";
234   case ISD::SMUL_LOHI:                  return "smul_lohi";
235   case ISD::UMUL_LOHI:                  return "umul_lohi";
236   case ISD::SDIVREM:                    return "sdivrem";
237   case ISD::UDIVREM:                    return "udivrem";
238   case ISD::AND:                        return "and";
239   case ISD::OR:                         return "or";
240   case ISD::XOR:                        return "xor";
241   case ISD::SHL:                        return "shl";
242   case ISD::SRA:                        return "sra";
243   case ISD::SRL:                        return "srl";
244   case ISD::ROTL:                       return "rotl";
245   case ISD::ROTR:                       return "rotr";
246   case ISD::FSHL:                       return "fshl";
247   case ISD::FSHR:                       return "fshr";
248   case ISD::FADD:                       return "fadd";
249   case ISD::STRICT_FADD:                return "strict_fadd";
250   case ISD::FSUB:                       return "fsub";
251   case ISD::STRICT_FSUB:                return "strict_fsub";
252   case ISD::FMUL:                       return "fmul";
253   case ISD::STRICT_FMUL:                return "strict_fmul";
254   case ISD::FDIV:                       return "fdiv";
255   case ISD::STRICT_FDIV:                return "strict_fdiv";
256   case ISD::FMA:                        return "fma";
257   case ISD::STRICT_FMA:                 return "strict_fma";
258   case ISD::FMAD:                       return "fmad";
259   case ISD::FREM:                       return "frem";
260   case ISD::STRICT_FREM:                return "strict_frem";
261   case ISD::FCOPYSIGN:                  return "fcopysign";
262   case ISD::FGETSIGN:                   return "fgetsign";
263   case ISD::FCANONICALIZE:              return "fcanonicalize";
264   case ISD::FPOW:                       return "fpow";
265   case ISD::STRICT_FPOW:                return "strict_fpow";
266   case ISD::SMIN:                       return "smin";
267   case ISD::SMAX:                       return "smax";
268   case ISD::UMIN:                       return "umin";
269   case ISD::UMAX:                       return "umax";
270 
271   case ISD::FPOWI:                      return "fpowi";
272   case ISD::STRICT_FPOWI:               return "strict_fpowi";
273   case ISD::SETCC:                      return "setcc";
274   case ISD::SETCCCARRY:                 return "setcccarry";
275   case ISD::STRICT_FSETCC:              return "strict_fsetcc";
276   case ISD::STRICT_FSETCCS:             return "strict_fsetccs";
277   case ISD::SELECT:                     return "select";
278   case ISD::VSELECT:                    return "vselect";
279   case ISD::SELECT_CC:                  return "select_cc";
280   case ISD::INSERT_VECTOR_ELT:          return "insert_vector_elt";
281   case ISD::EXTRACT_VECTOR_ELT:         return "extract_vector_elt";
282   case ISD::CONCAT_VECTORS:             return "concat_vectors";
283   case ISD::INSERT_SUBVECTOR:           return "insert_subvector";
284   case ISD::EXTRACT_SUBVECTOR:          return "extract_subvector";
285   case ISD::SCALAR_TO_VECTOR:           return "scalar_to_vector";
286   case ISD::VECTOR_SHUFFLE:             return "vector_shuffle";
287   case ISD::SPLAT_VECTOR:               return "splat_vector";
288   case ISD::CARRY_FALSE:                return "carry_false";
289   case ISD::ADDC:                       return "addc";
290   case ISD::ADDE:                       return "adde";
291   case ISD::ADDCARRY:                   return "addcarry";
292   case ISD::SADDO:                      return "saddo";
293   case ISD::UADDO:                      return "uaddo";
294   case ISD::SSUBO:                      return "ssubo";
295   case ISD::USUBO:                      return "usubo";
296   case ISD::SMULO:                      return "smulo";
297   case ISD::UMULO:                      return "umulo";
298   case ISD::SUBC:                       return "subc";
299   case ISD::SUBE:                       return "sube";
300   case ISD::SUBCARRY:                   return "subcarry";
301   case ISD::SHL_PARTS:                  return "shl_parts";
302   case ISD::SRA_PARTS:                  return "sra_parts";
303   case ISD::SRL_PARTS:                  return "srl_parts";
304 
305   case ISD::SADDSAT:                    return "saddsat";
306   case ISD::UADDSAT:                    return "uaddsat";
307   case ISD::SSUBSAT:                    return "ssubsat";
308   case ISD::USUBSAT:                    return "usubsat";
309 
310   case ISD::SMULFIX:                    return "smulfix";
311   case ISD::SMULFIXSAT:                 return "smulfixsat";
312   case ISD::UMULFIX:                    return "umulfix";
313   case ISD::UMULFIXSAT:                 return "umulfixsat";
314 
315   case ISD::SDIVFIX:                    return "sdivfix";
316   case ISD::UDIVFIX:                    return "udivfix";
317 
318   // Conversion operators.
319   case ISD::SIGN_EXTEND:                return "sign_extend";
320   case ISD::ZERO_EXTEND:                return "zero_extend";
321   case ISD::ANY_EXTEND:                 return "any_extend";
322   case ISD::SIGN_EXTEND_INREG:          return "sign_extend_inreg";
323   case ISD::ANY_EXTEND_VECTOR_INREG:    return "any_extend_vector_inreg";
324   case ISD::SIGN_EXTEND_VECTOR_INREG:   return "sign_extend_vector_inreg";
325   case ISD::ZERO_EXTEND_VECTOR_INREG:   return "zero_extend_vector_inreg";
326   case ISD::TRUNCATE:                   return "truncate";
327   case ISD::FP_ROUND:                   return "fp_round";
328   case ISD::STRICT_FP_ROUND:            return "strict_fp_round";
329   case ISD::FLT_ROUNDS_:                return "flt_rounds";
330   case ISD::FP_EXTEND:                  return "fp_extend";
331   case ISD::STRICT_FP_EXTEND:           return "strict_fp_extend";
332 
333   case ISD::SINT_TO_FP:                 return "sint_to_fp";
334   case ISD::STRICT_SINT_TO_FP:          return "strict_sint_to_fp";
335   case ISD::UINT_TO_FP:                 return "uint_to_fp";
336   case ISD::STRICT_UINT_TO_FP:          return "strict_uint_to_fp";
337   case ISD::FP_TO_SINT:                 return "fp_to_sint";
338   case ISD::STRICT_FP_TO_SINT:          return "strict_fp_to_sint";
339   case ISD::FP_TO_UINT:                 return "fp_to_uint";
340   case ISD::STRICT_FP_TO_UINT:          return "strict_fp_to_uint";
341   case ISD::BITCAST:                    return "bitcast";
342   case ISD::ADDRSPACECAST:              return "addrspacecast";
343   case ISD::FP16_TO_FP:                 return "fp16_to_fp";
344   case ISD::FP_TO_FP16:                 return "fp_to_fp16";
345   case ISD::LROUND:                     return "lround";
346   case ISD::STRICT_LROUND:              return "strict_lround";
347   case ISD::LLROUND:                    return "llround";
348   case ISD::STRICT_LLROUND:             return "strict_llround";
349   case ISD::LRINT:                      return "lrint";
350   case ISD::STRICT_LRINT:               return "strict_lrint";
351   case ISD::LLRINT:                     return "llrint";
352   case ISD::STRICT_LLRINT:              return "strict_llrint";
353 
354     // Control flow instructions
355   case ISD::BR:                         return "br";
356   case ISD::BRIND:                      return "brind";
357   case ISD::BR_JT:                      return "br_jt";
358   case ISD::BRCOND:                     return "brcond";
359   case ISD::BR_CC:                      return "br_cc";
360   case ISD::CALLSEQ_START:              return "callseq_start";
361   case ISD::CALLSEQ_END:                return "callseq_end";
362 
363     // EH instructions
364   case ISD::CATCHRET:                   return "catchret";
365   case ISD::CLEANUPRET:                 return "cleanupret";
366 
367     // Other operators
368   case ISD::LOAD:                       return "load";
369   case ISD::STORE:                      return "store";
370   case ISD::MLOAD:                      return "masked_load";
371   case ISD::MSTORE:                     return "masked_store";
372   case ISD::MGATHER:                    return "masked_gather";
373   case ISD::MSCATTER:                   return "masked_scatter";
374   case ISD::VAARG:                      return "vaarg";
375   case ISD::VACOPY:                     return "vacopy";
376   case ISD::VAEND:                      return "vaend";
377   case ISD::VASTART:                    return "vastart";
378   case ISD::DYNAMIC_STACKALLOC:         return "dynamic_stackalloc";
379   case ISD::EXTRACT_ELEMENT:            return "extract_element";
380   case ISD::BUILD_PAIR:                 return "build_pair";
381   case ISD::STACKSAVE:                  return "stacksave";
382   case ISD::STACKRESTORE:               return "stackrestore";
383   case ISD::TRAP:                       return "trap";
384   case ISD::DEBUGTRAP:                  return "debugtrap";
385   case ISD::LIFETIME_START:             return "lifetime.start";
386   case ISD::LIFETIME_END:               return "lifetime.end";
387   case ISD::GC_TRANSITION_START:        return "gc_transition.start";
388   case ISD::GC_TRANSITION_END:          return "gc_transition.end";
389   case ISD::GET_DYNAMIC_AREA_OFFSET:    return "get.dynamic.area.offset";
390 
391   // Bit manipulation
392   case ISD::ABS:                        return "abs";
393   case ISD::BITREVERSE:                 return "bitreverse";
394   case ISD::BSWAP:                      return "bswap";
395   case ISD::CTPOP:                      return "ctpop";
396   case ISD::CTTZ:                       return "cttz";
397   case ISD::CTTZ_ZERO_UNDEF:            return "cttz_zero_undef";
398   case ISD::CTLZ:                       return "ctlz";
399   case ISD::CTLZ_ZERO_UNDEF:            return "ctlz_zero_undef";
400 
401   // Trampolines
402   case ISD::INIT_TRAMPOLINE:            return "init_trampoline";
403   case ISD::ADJUST_TRAMPOLINE:          return "adjust_trampoline";
404 
405   case ISD::CONDCODE:
406     switch (cast<CondCodeSDNode>(this)->get()) {
407     default: llvm_unreachable("Unknown setcc condition!");
408     case ISD::SETOEQ:                   return "setoeq";
409     case ISD::SETOGT:                   return "setogt";
410     case ISD::SETOGE:                   return "setoge";
411     case ISD::SETOLT:                   return "setolt";
412     case ISD::SETOLE:                   return "setole";
413     case ISD::SETONE:                   return "setone";
414 
415     case ISD::SETO:                     return "seto";
416     case ISD::SETUO:                    return "setuo";
417     case ISD::SETUEQ:                   return "setueq";
418     case ISD::SETUGT:                   return "setugt";
419     case ISD::SETUGE:                   return "setuge";
420     case ISD::SETULT:                   return "setult";
421     case ISD::SETULE:                   return "setule";
422     case ISD::SETUNE:                   return "setune";
423 
424     case ISD::SETEQ:                    return "seteq";
425     case ISD::SETGT:                    return "setgt";
426     case ISD::SETGE:                    return "setge";
427     case ISD::SETLT:                    return "setlt";
428     case ISD::SETLE:                    return "setle";
429     case ISD::SETNE:                    return "setne";
430 
431     case ISD::SETTRUE:                  return "settrue";
432     case ISD::SETTRUE2:                 return "settrue2";
433     case ISD::SETFALSE:                 return "setfalse";
434     case ISD::SETFALSE2:                return "setfalse2";
435     }
436   case ISD::VECREDUCE_FADD:             return "vecreduce_fadd";
437   case ISD::VECREDUCE_STRICT_FADD:      return "vecreduce_strict_fadd";
438   case ISD::VECREDUCE_FMUL:             return "vecreduce_fmul";
439   case ISD::VECREDUCE_STRICT_FMUL:      return "vecreduce_strict_fmul";
440   case ISD::VECREDUCE_ADD:              return "vecreduce_add";
441   case ISD::VECREDUCE_MUL:              return "vecreduce_mul";
442   case ISD::VECREDUCE_AND:              return "vecreduce_and";
443   case ISD::VECREDUCE_OR:               return "vecreduce_or";
444   case ISD::VECREDUCE_XOR:              return "vecreduce_xor";
445   case ISD::VECREDUCE_SMAX:             return "vecreduce_smax";
446   case ISD::VECREDUCE_SMIN:             return "vecreduce_smin";
447   case ISD::VECREDUCE_UMAX:             return "vecreduce_umax";
448   case ISD::VECREDUCE_UMIN:             return "vecreduce_umin";
449   case ISD::VECREDUCE_FMAX:             return "vecreduce_fmax";
450   case ISD::VECREDUCE_FMIN:             return "vecreduce_fmin";
451   }
452 }
453 
454 const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
455   switch (AM) {
456   default:              return "";
457   case ISD::PRE_INC:    return "<pre-inc>";
458   case ISD::PRE_DEC:    return "<pre-dec>";
459   case ISD::POST_INC:   return "<post-inc>";
460   case ISD::POST_DEC:   return "<post-dec>";
461   }
462 }
463 
464 static Printable PrintNodeId(const SDNode &Node) {
465   return Printable([&Node](raw_ostream &OS) {
466 #ifndef NDEBUG
467     OS << 't' << Node.PersistentId;
468 #else
469     OS << (const void*)&Node;
470 #endif
471   });
472 }
473 
474 // Print the MMO with more information from the SelectionDAG.
475 static void printMemOperand(raw_ostream &OS, const MachineMemOperand &MMO,
476                             const MachineFunction *MF, const Module *M,
477                             const MachineFrameInfo *MFI,
478                             const TargetInstrInfo *TII, LLVMContext &Ctx) {
479   ModuleSlotTracker MST(M);
480   if (MF)
481     MST.incorporateFunction(MF->getFunction());
482   SmallVector<StringRef, 0> SSNs;
483   MMO.print(OS, MST, SSNs, Ctx, MFI, TII);
484 }
485 
486 static void printMemOperand(raw_ostream &OS, const MachineMemOperand &MMO,
487                             const SelectionDAG *G) {
488   if (G) {
489     const MachineFunction *MF = &G->getMachineFunction();
490     return printMemOperand(OS, MMO, MF, MF->getFunction().getParent(),
491                            &MF->getFrameInfo(), G->getSubtarget().getInstrInfo(),
492                            *G->getContext());
493   } else {
494     LLVMContext Ctx;
495     return printMemOperand(OS, MMO, /*MF=*/nullptr, /*M=*/nullptr,
496                            /*MFI=*/nullptr, /*TII=*/nullptr, Ctx);
497   }
498 }
499 
500 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
501 LLVM_DUMP_METHOD void SDNode::dump() const { dump(nullptr); }
502 
503 LLVM_DUMP_METHOD void SDNode::dump(const SelectionDAG *G) const {
504   print(dbgs(), G);
505   dbgs() << '\n';
506 }
507 #endif
508 
509 void SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const {
510   for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
511     if (i) OS << ",";
512     if (getValueType(i) == MVT::Other)
513       OS << "ch";
514     else
515       OS << getValueType(i).getEVTString();
516   }
517 }
518 
519 void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
520   if (getFlags().hasNoUnsignedWrap())
521     OS << " nuw";
522 
523   if (getFlags().hasNoSignedWrap())
524     OS << " nsw";
525 
526   if (getFlags().hasExact())
527     OS << " exact";
528 
529   if (getFlags().hasNoNaNs())
530     OS << " nnan";
531 
532   if (getFlags().hasNoInfs())
533     OS << " ninf";
534 
535   if (getFlags().hasNoSignedZeros())
536     OS << " nsz";
537 
538   if (getFlags().hasAllowReciprocal())
539     OS << " arcp";
540 
541   if (getFlags().hasAllowContract())
542     OS << " contract";
543 
544   if (getFlags().hasApproximateFuncs())
545     OS << " afn";
546 
547   if (getFlags().hasAllowReassociation())
548     OS << " reassoc";
549 
550   if (getFlags().hasVectorReduction())
551     OS << " vector-reduction";
552 
553   if (getFlags().hasNoFPExcept())
554     OS << " nofpexcept";
555 
556   if (const MachineSDNode *MN = dyn_cast<MachineSDNode>(this)) {
557     if (!MN->memoperands_empty()) {
558       OS << "<";
559       OS << "Mem:";
560       for (MachineSDNode::mmo_iterator i = MN->memoperands_begin(),
561            e = MN->memoperands_end(); i != e; ++i) {
562         printMemOperand(OS, **i, G);
563         if (std::next(i) != e)
564           OS << " ";
565       }
566       OS << ">";
567     }
568   } else if (const ShuffleVectorSDNode *SVN =
569                dyn_cast<ShuffleVectorSDNode>(this)) {
570     OS << "<";
571     for (unsigned i = 0, e = ValueList[0].getVectorNumElements(); i != e; ++i) {
572       int Idx = SVN->getMaskElt(i);
573       if (i) OS << ",";
574       if (Idx < 0)
575         OS << "u";
576       else
577         OS << Idx;
578     }
579     OS << ">";
580   } else if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
581     OS << '<' << CSDN->getAPIntValue() << '>';
582   } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
583     if (&CSDN->getValueAPF().getSemantics() == &APFloat::IEEEsingle())
584       OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
585     else if (&CSDN->getValueAPF().getSemantics() == &APFloat::IEEEdouble())
586       OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
587     else {
588       OS << "<APFloat(";
589       CSDN->getValueAPF().bitcastToAPInt().print(OS, false);
590       OS << ")>";
591     }
592   } else if (const GlobalAddressSDNode *GADN =
593              dyn_cast<GlobalAddressSDNode>(this)) {
594     int64_t offset = GADN->getOffset();
595     OS << '<';
596     GADN->getGlobal()->printAsOperand(OS);
597     OS << '>';
598     if (offset > 0)
599       OS << " + " << offset;
600     else
601       OS << " " << offset;
602     if (unsigned int TF = GADN->getTargetFlags())
603       OS << " [TF=" << TF << ']';
604   } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
605     OS << "<" << FIDN->getIndex() << ">";
606   } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
607     OS << "<" << JTDN->getIndex() << ">";
608     if (unsigned int TF = JTDN->getTargetFlags())
609       OS << " [TF=" << TF << ']';
610   } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
611     int offset = CP->getOffset();
612     if (CP->isMachineConstantPoolEntry())
613       OS << "<" << *CP->getMachineCPVal() << ">";
614     else
615       OS << "<" << *CP->getConstVal() << ">";
616     if (offset > 0)
617       OS << " + " << offset;
618     else
619       OS << " " << offset;
620     if (unsigned int TF = CP->getTargetFlags())
621       OS << " [TF=" << TF << ']';
622   } else if (const TargetIndexSDNode *TI = dyn_cast<TargetIndexSDNode>(this)) {
623     OS << "<" << TI->getIndex() << '+' << TI->getOffset() << ">";
624     if (unsigned TF = TI->getTargetFlags())
625       OS << " [TF=" << TF << ']';
626   } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
627     OS << "<";
628     const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
629     if (LBB)
630       OS << LBB->getName() << " ";
631     OS << (const void*)BBDN->getBasicBlock() << ">";
632   } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
633     OS << ' ' << printReg(R->getReg(),
634                           G ? G->getSubtarget().getRegisterInfo() : nullptr);
635   } else if (const ExternalSymbolSDNode *ES =
636              dyn_cast<ExternalSymbolSDNode>(this)) {
637     OS << "'" << ES->getSymbol() << "'";
638     if (unsigned int TF = ES->getTargetFlags())
639       OS << " [TF=" << TF << ']';
640   } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
641     if (M->getValue())
642       OS << "<" << M->getValue() << ">";
643     else
644       OS << "<null>";
645   } else if (const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(this)) {
646     if (MD->getMD())
647       OS << "<" << MD->getMD() << ">";
648     else
649       OS << "<null>";
650   } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
651     OS << ":" << N->getVT().getEVTString();
652   }
653   else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
654     OS << "<";
655 
656     printMemOperand(OS, *LD->getMemOperand(), G);
657 
658     bool doExt = true;
659     switch (LD->getExtensionType()) {
660     default: doExt = false; break;
661     case ISD::EXTLOAD:  OS << ", anyext"; break;
662     case ISD::SEXTLOAD: OS << ", sext"; break;
663     case ISD::ZEXTLOAD: OS << ", zext"; break;
664     }
665     if (doExt)
666       OS << " from " << LD->getMemoryVT().getEVTString();
667 
668     const char *AM = getIndexedModeName(LD->getAddressingMode());
669     if (*AM)
670       OS << ", " << AM;
671 
672     OS << ">";
673   } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
674     OS << "<";
675     printMemOperand(OS, *ST->getMemOperand(), G);
676 
677     if (ST->isTruncatingStore())
678       OS << ", trunc to " << ST->getMemoryVT().getEVTString();
679 
680     const char *AM = getIndexedModeName(ST->getAddressingMode());
681     if (*AM)
682       OS << ", " << AM;
683 
684     OS << ">";
685   } else if (const MaskedLoadSDNode *MLd = dyn_cast<MaskedLoadSDNode>(this)) {
686     OS << "<";
687 
688     printMemOperand(OS, *MLd->getMemOperand(), G);
689 
690     bool doExt = true;
691     switch (MLd->getExtensionType()) {
692     default: doExt = false; break;
693     case ISD::EXTLOAD:  OS << ", anyext"; break;
694     case ISD::SEXTLOAD: OS << ", sext"; break;
695     case ISD::ZEXTLOAD: OS << ", zext"; break;
696     }
697     if (doExt)
698       OS << " from " << MLd->getMemoryVT().getEVTString();
699 
700     const char *AM = getIndexedModeName(MLd->getAddressingMode());
701     if (*AM)
702       OS << ", " << AM;
703 
704     if (MLd->isExpandingLoad())
705       OS << ", expanding";
706 
707     OS << ">";
708   } else if (const MaskedStoreSDNode *MSt = dyn_cast<MaskedStoreSDNode>(this)) {
709     OS << "<";
710     printMemOperand(OS, *MSt->getMemOperand(), G);
711 
712     if (MSt->isTruncatingStore())
713       OS << ", trunc to " << MSt->getMemoryVT().getEVTString();
714 
715     const char *AM = getIndexedModeName(MSt->getAddressingMode());
716     if (*AM)
717       OS << ", " << AM;
718 
719     if (MSt->isCompressingStore())
720       OS << ", compressing";
721 
722     OS << ">";
723   } else if (const MemSDNode* M = dyn_cast<MemSDNode>(this)) {
724     OS << "<";
725     printMemOperand(OS, *M->getMemOperand(), G);
726     OS << ">";
727   } else if (const BlockAddressSDNode *BA =
728                dyn_cast<BlockAddressSDNode>(this)) {
729     int64_t offset = BA->getOffset();
730     OS << "<";
731     BA->getBlockAddress()->getFunction()->printAsOperand(OS, false);
732     OS << ", ";
733     BA->getBlockAddress()->getBasicBlock()->printAsOperand(OS, false);
734     OS << ">";
735     if (offset > 0)
736       OS << " + " << offset;
737     else
738       OS << " " << offset;
739     if (unsigned int TF = BA->getTargetFlags())
740       OS << " [TF=" << TF << ']';
741   } else if (const AddrSpaceCastSDNode *ASC =
742                dyn_cast<AddrSpaceCastSDNode>(this)) {
743     OS << '['
744        << ASC->getSrcAddressSpace()
745        << " -> "
746        << ASC->getDestAddressSpace()
747        << ']';
748   } else if (const LifetimeSDNode *LN = dyn_cast<LifetimeSDNode>(this)) {
749     if (LN->hasOffset())
750       OS << "<" << LN->getOffset() << " to " << LN->getOffset() + LN->getSize() << ">";
751   }
752 
753   if (VerboseDAGDumping) {
754     if (unsigned Order = getIROrder())
755         OS << " [ORD=" << Order << ']';
756 
757     if (getNodeId() != -1)
758       OS << " [ID=" << getNodeId() << ']';
759     if (!(isa<ConstantSDNode>(this) || (isa<ConstantFPSDNode>(this))))
760       OS << " # D:" << isDivergent();
761 
762     if (G && !G->GetDbgValues(this).empty()) {
763       OS << " [NoOfDbgValues=" << G->GetDbgValues(this).size() << ']';
764       for (SDDbgValue *Dbg : G->GetDbgValues(this))
765         if (!Dbg->isInvalidated())
766           Dbg->print(OS);
767     } else if (getHasDebugValue())
768       OS << " [NoOfDbgValues>0]";
769   }
770 }
771 
772 LLVM_DUMP_METHOD void SDDbgValue::print(raw_ostream &OS) const {
773   OS << " DbgVal(Order=" << getOrder() << ')';
774   if (isInvalidated()) OS << "(Invalidated)";
775   if (isEmitted()) OS << "(Emitted)";
776   switch (getKind()) {
777   case SDNODE:
778     if (getSDNode())
779       OS << "(SDNODE=" << PrintNodeId(*getSDNode()) << ':' <<  getResNo() << ')';
780     else
781       OS << "(SDNODE)";
782     break;
783   case CONST:
784     OS << "(CONST)";
785     break;
786   case FRAMEIX:
787     OS << "(FRAMEIX=" << getFrameIx() << ')';
788     break;
789   case VREG:
790     OS << "(VREG=" << getVReg() << ')';
791     break;
792   }
793   if (isIndirect()) OS << "(Indirect)";
794   OS << ":\"" << Var->getName() << '"';
795 #ifndef NDEBUG
796   if (Expr->getNumElements())
797     Expr->dump();
798 #endif
799 }
800 
801 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
802 LLVM_DUMP_METHOD void SDDbgValue::dump() const {
803   if (isInvalidated())
804     return;
805   print(dbgs());
806   dbgs() << "\n";
807 }
808 #endif
809 
810 /// Return true if this node is so simple that we should just print it inline
811 /// if it appears as an operand.
812 static bool shouldPrintInline(const SDNode &Node, const SelectionDAG *G) {
813   // Avoid lots of cluttering when inline printing nodes with associated
814   // DbgValues in verbose mode.
815   if (VerboseDAGDumping && G && !G->GetDbgValues(&Node).empty())
816     return false;
817   if (Node.getOpcode() == ISD::EntryToken)
818     return false;
819   return Node.getNumOperands() == 0;
820 }
821 
822 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
823 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
824   for (const SDValue &Op : N->op_values()) {
825     if (shouldPrintInline(*Op.getNode(), G))
826       continue;
827     if (Op.getNode()->hasOneUse())
828       DumpNodes(Op.getNode(), indent+2, G);
829   }
830 
831   dbgs().indent(indent);
832   N->dump(G);
833 }
834 
835 LLVM_DUMP_METHOD void SelectionDAG::dump() const {
836   dbgs() << "SelectionDAG has " << AllNodes.size() << " nodes:\n";
837 
838   for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
839        I != E; ++I) {
840     const SDNode *N = &*I;
841     if (!N->hasOneUse() && N != getRoot().getNode() &&
842         (!shouldPrintInline(*N, this) || N->use_empty()))
843       DumpNodes(N, 2, this);
844   }
845 
846   if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
847   dbgs() << "\n";
848 
849   if (VerboseDAGDumping) {
850     if (DbgBegin() != DbgEnd())
851       dbgs() << "SDDbgValues:\n";
852     for (auto *Dbg : make_range(DbgBegin(), DbgEnd()))
853       Dbg->dump();
854     if (ByvalParmDbgBegin() != ByvalParmDbgEnd())
855       dbgs() << "Byval SDDbgValues:\n";
856     for (auto *Dbg : make_range(ByvalParmDbgBegin(), ByvalParmDbgEnd()))
857       Dbg->dump();
858   }
859   dbgs() << "\n";
860 }
861 #endif
862 
863 void SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const {
864   OS << PrintNodeId(*this) << ": ";
865   print_types(OS, G);
866   OS << " = " << getOperationName(G);
867   print_details(OS, G);
868 }
869 
870 static bool printOperand(raw_ostream &OS, const SelectionDAG *G,
871                          const SDValue Value) {
872   if (!Value.getNode()) {
873     OS << "<null>";
874     return false;
875   } else if (shouldPrintInline(*Value.getNode(), G)) {
876     OS << Value->getOperationName(G) << ':';
877     Value->print_types(OS, G);
878     Value->print_details(OS, G);
879     return true;
880   } else {
881     OS << PrintNodeId(*Value.getNode());
882     if (unsigned RN = Value.getResNo())
883       OS << ':' << RN;
884     return false;
885   }
886 }
887 
888 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
889 using VisitedSDNodeSet = SmallPtrSet<const SDNode *, 32>;
890 
891 static void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent,
892                        const SelectionDAG *G, VisitedSDNodeSet &once) {
893   if (!once.insert(N).second) // If we've been here before, return now.
894     return;
895 
896   // Dump the current SDNode, but don't end the line yet.
897   OS.indent(indent);
898   N->printr(OS, G);
899 
900   // Having printed this SDNode, walk the children:
901   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
902     if (i) OS << ",";
903     OS << " ";
904 
905     const SDValue Op = N->getOperand(i);
906     bool printedInline = printOperand(OS, G, Op);
907     if (printedInline)
908       once.insert(Op.getNode());
909   }
910 
911   OS << "\n";
912 
913   // Dump children that have grandchildren on their own line(s).
914   for (const SDValue &Op : N->op_values())
915     DumpNodesr(OS, Op.getNode(), indent+2, G, once);
916 }
917 
918 LLVM_DUMP_METHOD void SDNode::dumpr() const {
919   VisitedSDNodeSet once;
920   DumpNodesr(dbgs(), this, 0, nullptr, once);
921 }
922 
923 LLVM_DUMP_METHOD void SDNode::dumpr(const SelectionDAG *G) const {
924   VisitedSDNodeSet once;
925   DumpNodesr(dbgs(), this, 0, G, once);
926 }
927 #endif
928 
929 static void printrWithDepthHelper(raw_ostream &OS, const SDNode *N,
930                                   const SelectionDAG *G, unsigned depth,
931                                   unsigned indent) {
932   if (depth == 0)
933     return;
934 
935   OS.indent(indent);
936 
937   N->print(OS, G);
938 
939   if (depth < 1)
940     return;
941 
942   for (const SDValue &Op : N->op_values()) {
943     // Don't follow chain operands.
944     if (Op.getValueType() == MVT::Other)
945       continue;
946     OS << '\n';
947     printrWithDepthHelper(OS, Op.getNode(), G, depth-1, indent+2);
948   }
949 }
950 
951 void SDNode::printrWithDepth(raw_ostream &OS, const SelectionDAG *G,
952                             unsigned depth) const {
953   printrWithDepthHelper(OS, this, G, depth, 0);
954 }
955 
956 void SDNode::printrFull(raw_ostream &OS, const SelectionDAG *G) const {
957   // Don't print impossibly deep things.
958   printrWithDepth(OS, G, 10);
959 }
960 
961 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
962 LLVM_DUMP_METHOD
963 void SDNode::dumprWithDepth(const SelectionDAG *G, unsigned depth) const {
964   printrWithDepth(dbgs(), G, depth);
965 }
966 
967 LLVM_DUMP_METHOD void SDNode::dumprFull(const SelectionDAG *G) const {
968   // Don't print impossibly deep things.
969   dumprWithDepth(G, 10);
970 }
971 #endif
972 
973 void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
974   printr(OS, G);
975   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
976     if (i) OS << ", "; else OS << " ";
977     printOperand(OS, G, getOperand(i));
978   }
979   if (DebugLoc DL = getDebugLoc()) {
980     OS << ", ";
981     DL.print(OS);
982   }
983 }
984