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