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