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