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