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