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