1//===-- SelectionDAGDumper.cpp - Implement SelectionDAG::dump() -----------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements the SelectionDAG::dump method and friends.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "ScheduleDAGSDNodes.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/CodeGen/MachineConstantPool.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineModuleInfo.h"
20#include "llvm/IR/DebugInfo.h"
21#include "llvm/IR/Function.h"
22#include "llvm/IR/Intrinsics.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/Support/GraphWriter.h"
25#include "llvm/Support/raw_ostream.h"
26#include "llvm/Target/TargetInstrInfo.h"
27#include "llvm/Target/TargetIntrinsicInfo.h"
28#include "llvm/Target/TargetMachine.h"
29#include "llvm/Target/TargetRegisterInfo.h"
30#include "llvm/Target/TargetSubtargetInfo.h"
31using namespace llvm;
32
33std::string SDNode::getOperationName(const SelectionDAG *G) const {
34  switch (getOpcode()) {
35  default:
36    if (getOpcode() < ISD::BUILTIN_OP_END)
37      return "<<Unknown DAG Node>>";
38    if (isMachineOpcode()) {
39      if (G)
40        if (const TargetInstrInfo *TII = G->getSubtarget().getInstrInfo())
41          if (getMachineOpcode() < TII->getNumOpcodes())
42            return TII->getName(getMachineOpcode());
43      return "<<Unknown Machine Node #" + utostr(getOpcode()) + ">>";
44    }
45    if (G) {
46      const TargetLowering &TLI = G->getTargetLoweringInfo();
47      const char *Name = TLI.getTargetNodeName(getOpcode());
48      if (Name) return Name;
49      return "<<Unknown Target Node #" + utostr(getOpcode()) + ">>";
50    }
51    return "<<Unknown Node #" + utostr(getOpcode()) + ">>";
52
53#ifndef NDEBUG
54  case ISD::DELETED_NODE:               return "<<Deleted Node!>>";
55#endif
56  case ISD::PREFETCH:                   return "Prefetch";
57  case ISD::ATOMIC_FENCE:               return "AtomicFence";
58  case ISD::ATOMIC_CMP_SWAP:            return "AtomicCmpSwap";
59  case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: return "AtomicCmpSwapWithSuccess";
60  case ISD::ATOMIC_SWAP:                return "AtomicSwap";
61  case ISD::ATOMIC_LOAD_ADD:            return "AtomicLoadAdd";
62  case ISD::ATOMIC_LOAD_SUB:            return "AtomicLoadSub";
63  case ISD::ATOMIC_LOAD_AND:            return "AtomicLoadAnd";
64  case ISD::ATOMIC_LOAD_OR:             return "AtomicLoadOr";
65  case ISD::ATOMIC_LOAD_XOR:            return "AtomicLoadXor";
66  case ISD::ATOMIC_LOAD_NAND:           return "AtomicLoadNand";
67  case ISD::ATOMIC_LOAD_MIN:            return "AtomicLoadMin";
68  case ISD::ATOMIC_LOAD_MAX:            return "AtomicLoadMax";
69  case ISD::ATOMIC_LOAD_UMIN:           return "AtomicLoadUMin";
70  case ISD::ATOMIC_LOAD_UMAX:           return "AtomicLoadUMax";
71  case ISD::ATOMIC_LOAD:                return "AtomicLoad";
72  case ISD::ATOMIC_STORE:               return "AtomicStore";
73  case ISD::PCMARKER:                   return "PCMarker";
74  case ISD::READCYCLECOUNTER:           return "ReadCycleCounter";
75  case ISD::SRCVALUE:                   return "SrcValue";
76  case ISD::MDNODE_SDNODE:              return "MDNode";
77  case ISD::EntryToken:                 return "EntryToken";
78  case ISD::TokenFactor:                return "TokenFactor";
79  case ISD::AssertSext:                 return "AssertSext";
80  case ISD::AssertZext:                 return "AssertZext";
81
82  case ISD::BasicBlock:                 return "BasicBlock";
83  case ISD::VALUETYPE:                  return "ValueType";
84  case ISD::Register:                   return "Register";
85  case ISD::RegisterMask:               return "RegisterMask";
86  case ISD::Constant:
87    if (cast<ConstantSDNode>(this)->isOpaque())
88      return "OpaqueConstant";
89    return "Constant";
90  case ISD::ConstantFP:                 return "ConstantFP";
91  case ISD::GlobalAddress:              return "GlobalAddress";
92  case ISD::GlobalTLSAddress:           return "GlobalTLSAddress";
93  case ISD::FrameIndex:                 return "FrameIndex";
94  case ISD::JumpTable:                  return "JumpTable";
95  case ISD::GLOBAL_OFFSET_TABLE:        return "GLOBAL_OFFSET_TABLE";
96  case ISD::RETURNADDR:                 return "RETURNADDR";
97  case ISD::FRAMEADDR:                  return "FRAMEADDR";
98  case ISD::FRAME_ALLOC_RECOVER:        return "FRAME_ALLOC_RECOVER";
99  case ISD::READ_REGISTER:              return "READ_REGISTER";
100  case ISD::WRITE_REGISTER:             return "WRITE_REGISTER";
101  case ISD::FRAME_TO_ARGS_OFFSET:       return "FRAME_TO_ARGS_OFFSET";
102  case ISD::EH_RETURN:                  return "EH_RETURN";
103  case ISD::EH_SJLJ_SETJMP:             return "EH_SJLJ_SETJMP";
104  case ISD::EH_SJLJ_LONGJMP:            return "EH_SJLJ_LONGJMP";
105  case ISD::ConstantPool:               return "ConstantPool";
106  case ISD::TargetIndex:                return "TargetIndex";
107  case ISD::ExternalSymbol:             return "ExternalSymbol";
108  case ISD::BlockAddress:               return "BlockAddress";
109  case ISD::INTRINSIC_WO_CHAIN:
110  case ISD::INTRINSIC_VOID:
111  case ISD::INTRINSIC_W_CHAIN: {
112    unsigned OpNo = getOpcode() == ISD::INTRINSIC_WO_CHAIN ? 0 : 1;
113    unsigned IID = cast<ConstantSDNode>(getOperand(OpNo))->getZExtValue();
114    if (IID < Intrinsic::num_intrinsics)
115      return Intrinsic::getName((Intrinsic::ID)IID);
116    else if (const TargetIntrinsicInfo *TII = G->getTarget().getIntrinsicInfo())
117      return TII->getName(IID);
118    llvm_unreachable("Invalid intrinsic ID");
119  }
120
121  case ISD::BUILD_VECTOR:               return "BUILD_VECTOR";
122  case ISD::TargetConstant:
123    if (cast<ConstantSDNode>(this)->isOpaque())
124      return "OpaqueTargetConstant";
125    return "TargetConstant";
126  case ISD::TargetConstantFP:           return "TargetConstantFP";
127  case ISD::TargetGlobalAddress:        return "TargetGlobalAddress";
128  case ISD::TargetGlobalTLSAddress:     return "TargetGlobalTLSAddress";
129  case ISD::TargetFrameIndex:           return "TargetFrameIndex";
130  case ISD::TargetJumpTable:            return "TargetJumpTable";
131  case ISD::TargetConstantPool:         return "TargetConstantPool";
132  case ISD::TargetExternalSymbol:       return "TargetExternalSymbol";
133  case ISD::TargetBlockAddress:         return "TargetBlockAddress";
134
135  case ISD::CopyToReg:                  return "CopyToReg";
136  case ISD::CopyFromReg:                return "CopyFromReg";
137  case ISD::UNDEF:                      return "undef";
138  case ISD::MERGE_VALUES:               return "merge_values";
139  case ISD::INLINEASM:                  return "inlineasm";
140  case ISD::EH_LABEL:                   return "eh_label";
141  case ISD::HANDLENODE:                 return "handlenode";
142
143  // Unary operators
144  case ISD::FABS:                       return "fabs";
145  case ISD::FMINNUM:                    return "fminnum";
146  case ISD::FMAXNUM:                    return "fmaxnum";
147  case ISD::FNEG:                       return "fneg";
148  case ISD::FSQRT:                      return "fsqrt";
149  case ISD::FSIN:                       return "fsin";
150  case ISD::FCOS:                       return "fcos";
151  case ISD::FSINCOS:                    return "fsincos";
152  case ISD::FTRUNC:                     return "ftrunc";
153  case ISD::FFLOOR:                     return "ffloor";
154  case ISD::FCEIL:                      return "fceil";
155  case ISD::FRINT:                      return "frint";
156  case ISD::FNEARBYINT:                 return "fnearbyint";
157  case ISD::FROUND:                     return "fround";
158  case ISD::FEXP:                       return "fexp";
159  case ISD::FEXP2:                      return "fexp2";
160  case ISD::FLOG:                       return "flog";
161  case ISD::FLOG2:                      return "flog2";
162  case ISD::FLOG10:                     return "flog10";
163
164  // Binary operators
165  case ISD::ADD:                        return "add";
166  case ISD::SUB:                        return "sub";
167  case ISD::MUL:                        return "mul";
168  case ISD::MULHU:                      return "mulhu";
169  case ISD::MULHS:                      return "mulhs";
170  case ISD::SDIV:                       return "sdiv";
171  case ISD::UDIV:                       return "udiv";
172  case ISD::SREM:                       return "srem";
173  case ISD::UREM:                       return "urem";
174  case ISD::SMUL_LOHI:                  return "smul_lohi";
175  case ISD::UMUL_LOHI:                  return "umul_lohi";
176  case ISD::SDIVREM:                    return "sdivrem";
177  case ISD::UDIVREM:                    return "udivrem";
178  case ISD::AND:                        return "and";
179  case ISD::OR:                         return "or";
180  case ISD::XOR:                        return "xor";
181  case ISD::SHL:                        return "shl";
182  case ISD::SRA:                        return "sra";
183  case ISD::SRL:                        return "srl";
184  case ISD::ROTL:                       return "rotl";
185  case ISD::ROTR:                       return "rotr";
186  case ISD::FADD:                       return "fadd";
187  case ISD::FSUB:                       return "fsub";
188  case ISD::FMUL:                       return "fmul";
189  case ISD::FDIV:                       return "fdiv";
190  case ISD::FMA:                        return "fma";
191  case ISD::FMAD:                       return "fmad";
192  case ISD::FREM:                       return "frem";
193  case ISD::FCOPYSIGN:                  return "fcopysign";
194  case ISD::FGETSIGN:                   return "fgetsign";
195  case ISD::FPOW:                       return "fpow";
196
197  case ISD::FPOWI:                      return "fpowi";
198  case ISD::SETCC:                      return "setcc";
199  case ISD::SELECT:                     return "select";
200  case ISD::VSELECT:                    return "vselect";
201  case ISD::SELECT_CC:                  return "select_cc";
202  case ISD::INSERT_VECTOR_ELT:          return "insert_vector_elt";
203  case ISD::EXTRACT_VECTOR_ELT:         return "extract_vector_elt";
204  case ISD::CONCAT_VECTORS:             return "concat_vectors";
205  case ISD::INSERT_SUBVECTOR:           return "insert_subvector";
206  case ISD::EXTRACT_SUBVECTOR:          return "extract_subvector";
207  case ISD::SCALAR_TO_VECTOR:           return "scalar_to_vector";
208  case ISD::VECTOR_SHUFFLE:             return "vector_shuffle";
209  case ISD::CARRY_FALSE:                return "carry_false";
210  case ISD::ADDC:                       return "addc";
211  case ISD::ADDE:                       return "adde";
212  case ISD::SADDO:                      return "saddo";
213  case ISD::UADDO:                      return "uaddo";
214  case ISD::SSUBO:                      return "ssubo";
215  case ISD::USUBO:                      return "usubo";
216  case ISD::SMULO:                      return "smulo";
217  case ISD::UMULO:                      return "umulo";
218  case ISD::SUBC:                       return "subc";
219  case ISD::SUBE:                       return "sube";
220  case ISD::SHL_PARTS:                  return "shl_parts";
221  case ISD::SRA_PARTS:                  return "sra_parts";
222  case ISD::SRL_PARTS:                  return "srl_parts";
223
224  // Conversion operators.
225  case ISD::SIGN_EXTEND:                return "sign_extend";
226  case ISD::ZERO_EXTEND:                return "zero_extend";
227  case ISD::ANY_EXTEND:                 return "any_extend";
228  case ISD::SIGN_EXTEND_INREG:          return "sign_extend_inreg";
229  case ISD::ANY_EXTEND_VECTOR_INREG:    return "any_extend_vector_inreg";
230  case ISD::SIGN_EXTEND_VECTOR_INREG:   return "sign_extend_vector_inreg";
231  case ISD::ZERO_EXTEND_VECTOR_INREG:   return "zero_extend_vector_inreg";
232  case ISD::TRUNCATE:                   return "truncate";
233  case ISD::FP_ROUND:                   return "fp_round";
234  case ISD::FLT_ROUNDS_:                return "flt_rounds";
235  case ISD::FP_ROUND_INREG:             return "fp_round_inreg";
236  case ISD::FP_EXTEND:                  return "fp_extend";
237
238  case ISD::SINT_TO_FP:                 return "sint_to_fp";
239  case ISD::UINT_TO_FP:                 return "uint_to_fp";
240  case ISD::FP_TO_SINT:                 return "fp_to_sint";
241  case ISD::FP_TO_UINT:                 return "fp_to_uint";
242  case ISD::BITCAST:                    return "bitcast";
243  case ISD::ADDRSPACECAST:              return "addrspacecast";
244  case ISD::FP16_TO_FP:                 return "fp16_to_fp";
245  case ISD::FP_TO_FP16:                 return "fp_to_fp16";
246
247  case ISD::CONVERT_RNDSAT: {
248    switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
249    default: llvm_unreachable("Unknown cvt code!");
250    case ISD::CVT_FF:                   return "cvt_ff";
251    case ISD::CVT_FS:                   return "cvt_fs";
252    case ISD::CVT_FU:                   return "cvt_fu";
253    case ISD::CVT_SF:                   return "cvt_sf";
254    case ISD::CVT_UF:                   return "cvt_uf";
255    case ISD::CVT_SS:                   return "cvt_ss";
256    case ISD::CVT_SU:                   return "cvt_su";
257    case ISD::CVT_US:                   return "cvt_us";
258    case ISD::CVT_UU:                   return "cvt_uu";
259    }
260  }
261
262    // Control flow instructions
263  case ISD::BR:                         return "br";
264  case ISD::BRIND:                      return "brind";
265  case ISD::BR_JT:                      return "br_jt";
266  case ISD::BRCOND:                     return "brcond";
267  case ISD::BR_CC:                      return "br_cc";
268  case ISD::CALLSEQ_START:              return "callseq_start";
269  case ISD::CALLSEQ_END:                return "callseq_end";
270
271    // Other operators
272  case ISD::LOAD:                       return "load";
273  case ISD::STORE:                      return "store";
274  case ISD::MLOAD:                      return "masked_load";
275  case ISD::MSTORE:                     return "masked_store";
276  case ISD::VAARG:                      return "vaarg";
277  case ISD::VACOPY:                     return "vacopy";
278  case ISD::VAEND:                      return "vaend";
279  case ISD::VASTART:                    return "vastart";
280  case ISD::DYNAMIC_STACKALLOC:         return "dynamic_stackalloc";
281  case ISD::EXTRACT_ELEMENT:            return "extract_element";
282  case ISD::BUILD_PAIR:                 return "build_pair";
283  case ISD::STACKSAVE:                  return "stacksave";
284  case ISD::STACKRESTORE:               return "stackrestore";
285  case ISD::TRAP:                       return "trap";
286  case ISD::DEBUGTRAP:                  return "debugtrap";
287  case ISD::LIFETIME_START:             return "lifetime.start";
288  case ISD::LIFETIME_END:               return "lifetime.end";
289
290  // Bit manipulation
291  case ISD::BSWAP:                      return "bswap";
292  case ISD::CTPOP:                      return "ctpop";
293  case ISD::CTTZ:                       return "cttz";
294  case ISD::CTTZ_ZERO_UNDEF:            return "cttz_zero_undef";
295  case ISD::CTLZ:                       return "ctlz";
296  case ISD::CTLZ_ZERO_UNDEF:            return "ctlz_zero_undef";
297
298  // Trampolines
299  case ISD::INIT_TRAMPOLINE:            return "init_trampoline";
300  case ISD::ADJUST_TRAMPOLINE:          return "adjust_trampoline";
301
302  case ISD::CONDCODE:
303    switch (cast<CondCodeSDNode>(this)->get()) {
304    default: llvm_unreachable("Unknown setcc condition!");
305    case ISD::SETOEQ:                   return "setoeq";
306    case ISD::SETOGT:                   return "setogt";
307    case ISD::SETOGE:                   return "setoge";
308    case ISD::SETOLT:                   return "setolt";
309    case ISD::SETOLE:                   return "setole";
310    case ISD::SETONE:                   return "setone";
311
312    case ISD::SETO:                     return "seto";
313    case ISD::SETUO:                    return "setuo";
314    case ISD::SETUEQ:                   return "setue";
315    case ISD::SETUGT:                   return "setugt";
316    case ISD::SETUGE:                   return "setuge";
317    case ISD::SETULT:                   return "setult";
318    case ISD::SETULE:                   return "setule";
319    case ISD::SETUNE:                   return "setune";
320
321    case ISD::SETEQ:                    return "seteq";
322    case ISD::SETGT:                    return "setgt";
323    case ISD::SETGE:                    return "setge";
324    case ISD::SETLT:                    return "setlt";
325    case ISD::SETLE:                    return "setle";
326    case ISD::SETNE:                    return "setne";
327
328    case ISD::SETTRUE:                  return "settrue";
329    case ISD::SETTRUE2:                 return "settrue2";
330    case ISD::SETFALSE:                 return "setfalse";
331    case ISD::SETFALSE2:                return "setfalse2";
332    }
333  }
334}
335
336const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
337  switch (AM) {
338  default:              return "";
339  case ISD::PRE_INC:    return "<pre-inc>";
340  case ISD::PRE_DEC:    return "<pre-dec>";
341  case ISD::POST_INC:   return "<post-inc>";
342  case ISD::POST_DEC:   return "<post-dec>";
343  }
344}
345
346void SDNode::dump() const { dump(nullptr); }
347void SDNode::dump(const SelectionDAG *G) const {
348  print(dbgs(), G);
349  dbgs() << '\n';
350}
351
352void SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const {
353  OS << (const void*)this << ": ";
354
355  for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
356    if (i) OS << ",";
357    if (getValueType(i) == MVT::Other)
358      OS << "ch";
359    else
360      OS << getValueType(i).getEVTString();
361  }
362  OS << " = " << getOperationName(G);
363}
364
365void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
366  if (const MachineSDNode *MN = dyn_cast<MachineSDNode>(this)) {
367    if (!MN->memoperands_empty()) {
368      OS << "<";
369      OS << "Mem:";
370      for (MachineSDNode::mmo_iterator i = MN->memoperands_begin(),
371           e = MN->memoperands_end(); i != e; ++i) {
372        OS << **i;
373        if (std::next(i) != e)
374          OS << " ";
375      }
376      OS << ">";
377    }
378  } else if (const ShuffleVectorSDNode *SVN =
379               dyn_cast<ShuffleVectorSDNode>(this)) {
380    OS << "<";
381    for (unsigned i = 0, e = ValueList[0].getVectorNumElements(); i != e; ++i) {
382      int Idx = SVN->getMaskElt(i);
383      if (i) OS << ",";
384      if (Idx < 0)
385        OS << "u";
386      else
387        OS << Idx;
388    }
389    OS << ">";
390  } else if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
391    OS << '<' << CSDN->getAPIntValue() << '>';
392  } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
393    if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
394      OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
395    else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
396      OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
397    else {
398      OS << "<APFloat(";
399      CSDN->getValueAPF().bitcastToAPInt().dump();
400      OS << ")>";
401    }
402  } else if (const GlobalAddressSDNode *GADN =
403             dyn_cast<GlobalAddressSDNode>(this)) {
404    int64_t offset = GADN->getOffset();
405    OS << '<';
406    GADN->getGlobal()->printAsOperand(OS);
407    OS << '>';
408    if (offset > 0)
409      OS << " + " << offset;
410    else
411      OS << " " << offset;
412    if (unsigned int TF = GADN->getTargetFlags())
413      OS << " [TF=" << TF << ']';
414  } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
415    OS << "<" << FIDN->getIndex() << ">";
416  } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
417    OS << "<" << JTDN->getIndex() << ">";
418    if (unsigned int TF = JTDN->getTargetFlags())
419      OS << " [TF=" << TF << ']';
420  } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
421    int offset = CP->getOffset();
422    if (CP->isMachineConstantPoolEntry())
423      OS << "<" << *CP->getMachineCPVal() << ">";
424    else
425      OS << "<" << *CP->getConstVal() << ">";
426    if (offset > 0)
427      OS << " + " << offset;
428    else
429      OS << " " << offset;
430    if (unsigned int TF = CP->getTargetFlags())
431      OS << " [TF=" << TF << ']';
432  } else if (const TargetIndexSDNode *TI = dyn_cast<TargetIndexSDNode>(this)) {
433    OS << "<" << TI->getIndex() << '+' << TI->getOffset() << ">";
434    if (unsigned TF = TI->getTargetFlags())
435      OS << " [TF=" << TF << ']';
436  } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
437    OS << "<";
438    const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
439    if (LBB)
440      OS << LBB->getName() << " ";
441    OS << (const void*)BBDN->getBasicBlock() << ">";
442  } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
443    OS << ' ' << PrintReg(R->getReg(),
444                          G ? G->getSubtarget().getRegisterInfo() : nullptr);
445  } else if (const ExternalSymbolSDNode *ES =
446             dyn_cast<ExternalSymbolSDNode>(this)) {
447    OS << "'" << ES->getSymbol() << "'";
448    if (unsigned int TF = ES->getTargetFlags())
449      OS << " [TF=" << TF << ']';
450  } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
451    if (M->getValue())
452      OS << "<" << M->getValue() << ">";
453    else
454      OS << "<null>";
455  } else if (const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(this)) {
456    if (MD->getMD())
457      OS << "<" << MD->getMD() << ">";
458    else
459      OS << "<null>";
460  } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
461    OS << ":" << N->getVT().getEVTString();
462  }
463  else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
464    OS << "<" << *LD->getMemOperand();
465
466    bool doExt = true;
467    switch (LD->getExtensionType()) {
468    default: doExt = false; break;
469    case ISD::EXTLOAD:  OS << ", anyext"; break;
470    case ISD::SEXTLOAD: OS << ", sext"; break;
471    case ISD::ZEXTLOAD: OS << ", zext"; break;
472    }
473    if (doExt)
474      OS << " from " << LD->getMemoryVT().getEVTString();
475
476    const char *AM = getIndexedModeName(LD->getAddressingMode());
477    if (*AM)
478      OS << ", " << AM;
479
480    OS << ">";
481  } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
482    OS << "<" << *ST->getMemOperand();
483
484    if (ST->isTruncatingStore())
485      OS << ", trunc to " << ST->getMemoryVT().getEVTString();
486
487    const char *AM = getIndexedModeName(ST->getAddressingMode());
488    if (*AM)
489      OS << ", " << AM;
490
491    OS << ">";
492  } else if (const MemSDNode* M = dyn_cast<MemSDNode>(this)) {
493    OS << "<" << *M->getMemOperand() << ">";
494  } else if (const BlockAddressSDNode *BA =
495               dyn_cast<BlockAddressSDNode>(this)) {
496    int64_t offset = BA->getOffset();
497    OS << "<";
498    BA->getBlockAddress()->getFunction()->printAsOperand(OS, false);
499    OS << ", ";
500    BA->getBlockAddress()->getBasicBlock()->printAsOperand(OS, false);
501    OS << ">";
502    if (offset > 0)
503      OS << " + " << offset;
504    else
505      OS << " " << offset;
506    if (unsigned int TF = BA->getTargetFlags())
507      OS << " [TF=" << TF << ']';
508  } else if (const AddrSpaceCastSDNode *ASC =
509               dyn_cast<AddrSpaceCastSDNode>(this)) {
510    OS << '['
511       << ASC->getSrcAddressSpace()
512       << " -> "
513       << ASC->getDestAddressSpace()
514       << ']';
515  }
516
517  if (unsigned Order = getIROrder())
518      OS << " [ORD=" << Order << ']';
519
520  if (getNodeId() != -1)
521    OS << " [ID=" << getNodeId() << ']';
522
523  if (!G)
524    return;
525
526  MDLocation *L = getDebugLoc();
527  if (!L)
528    return;
529
530  if (auto *Scope = L->getScope())
531    OS << Scope->getFilename();
532  else
533    OS << "<unknown>";
534  OS << ':' << L->getLine();
535  if (unsigned C = L->getColumn())
536    OS << ':' << C;
537}
538
539static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
540  for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
541    if (N->getOperand(i).getNode()->hasOneUse())
542      DumpNodes(N->getOperand(i).getNode(), indent+2, G);
543    else
544      dbgs() << "\n" << std::string(indent+2, ' ')
545             << (void*)N->getOperand(i).getNode() << ": <multiple use>";
546
547  dbgs() << '\n';
548  dbgs().indent(indent);
549  N->dump(G);
550}
551
552void SelectionDAG::dump() const {
553  dbgs() << "SelectionDAG has " << AllNodes.size() << " nodes:";
554
555  for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
556       I != E; ++I) {
557    const SDNode *N = I;
558    if (!N->hasOneUse() && N != getRoot().getNode())
559      DumpNodes(N, 2, this);
560  }
561
562  if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
563  dbgs() << "\n\n";
564}
565
566void SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const {
567  print_types(OS, G);
568  print_details(OS, G);
569}
570
571typedef SmallPtrSet<const SDNode *, 128> VisitedSDNodeSet;
572static void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent,
573                       const SelectionDAG *G, VisitedSDNodeSet &once) {
574  if (!once.insert(N).second) // If we've been here before, return now.
575    return;
576
577  // Dump the current SDNode, but don't end the line yet.
578  OS.indent(indent);
579  N->printr(OS, G);
580
581  // Having printed this SDNode, walk the children:
582  for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
583    const SDNode *child = N->getOperand(i).getNode();
584
585    if (i) OS << ",";
586    OS << " ";
587
588    if (child->getNumOperands() == 0) {
589      // This child has no grandchildren; print it inline right here.
590      child->printr(OS, G);
591      once.insert(child);
592    } else {         // Just the address. FIXME: also print the child's opcode.
593      OS << (const void*)child;
594      if (unsigned RN = N->getOperand(i).getResNo())
595        OS << ":" << RN;
596    }
597  }
598
599  OS << "\n";
600
601  // Dump children that have grandchildren on their own line(s).
602  for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
603    const SDNode *child = N->getOperand(i).getNode();
604    DumpNodesr(OS, child, indent+2, G, once);
605  }
606}
607
608void SDNode::dumpr() const {
609  VisitedSDNodeSet once;
610  DumpNodesr(dbgs(), this, 0, nullptr, once);
611}
612
613void SDNode::dumpr(const SelectionDAG *G) const {
614  VisitedSDNodeSet once;
615  DumpNodesr(dbgs(), this, 0, G, once);
616}
617
618static void printrWithDepthHelper(raw_ostream &OS, const SDNode *N,
619                                  const SelectionDAG *G, unsigned depth,
620                                  unsigned indent) {
621  if (depth == 0)
622    return;
623
624  OS.indent(indent);
625
626  N->print(OS, G);
627
628  if (depth < 1)
629    return;
630
631  for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
632    // Don't follow chain operands.
633    if (N->getOperand(i).getValueType() == MVT::Other)
634      continue;
635    OS << '\n';
636    printrWithDepthHelper(OS, N->getOperand(i).getNode(), G, depth-1, indent+2);
637  }
638}
639
640void SDNode::printrWithDepth(raw_ostream &OS, const SelectionDAG *G,
641                            unsigned depth) const {
642  printrWithDepthHelper(OS, this, G, depth, 0);
643}
644
645void SDNode::printrFull(raw_ostream &OS, const SelectionDAG *G) const {
646  // Don't print impossibly deep things.
647  printrWithDepth(OS, G, 10);
648}
649
650void SDNode::dumprWithDepth(const SelectionDAG *G, unsigned depth) const {
651  printrWithDepth(dbgs(), G, depth);
652}
653
654void SDNode::dumprFull(const SelectionDAG *G) const {
655  // Don't print impossibly deep things.
656  dumprWithDepth(G, 10);
657}
658
659void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
660  print_types(OS, G);
661  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
662    if (i) OS << ", "; else OS << " ";
663    OS << (void*)getOperand(i).getNode();
664    if (unsigned RN = getOperand(i).getResNo())
665      OS << ":" << RN;
666  }
667  print_details(OS, G);
668}
669