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