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