1 //===- MIRPrinter.cpp - MIR serialization format printer ------------------===// 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 file implements the class that prints out the LLVM IR and machine 11 // functions using the MIR serialization format. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/ADT/None.h" 17 #include "llvm/ADT/SmallBitVector.h" 18 #include "llvm/ADT/SmallPtrSet.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/STLExtras.h" 21 #include "llvm/ADT/StringExtras.h" 22 #include "llvm/ADT/StringRef.h" 23 #include "llvm/ADT/Twine.h" 24 #include "llvm/CodeGen/GlobalISel/RegisterBank.h" 25 #include "llvm/CodeGen/MachineBasicBlock.h" 26 #include "llvm/CodeGen/MachineConstantPool.h" 27 #include "llvm/CodeGen/MachineFrameInfo.h" 28 #include "llvm/CodeGen/MachineFunction.h" 29 #include "llvm/CodeGen/MachineInstr.h" 30 #include "llvm/CodeGen/MachineJumpTableInfo.h" 31 #include "llvm/CodeGen/MachineMemOperand.h" 32 #include "llvm/CodeGen/MachineOperand.h" 33 #include "llvm/CodeGen/MachineRegisterInfo.h" 34 #include "llvm/CodeGen/MIRPrinter.h" 35 #include "llvm/CodeGen/MIRYamlMapping.h" 36 #include "llvm/CodeGen/PseudoSourceValue.h" 37 #include "llvm/IR/BasicBlock.h" 38 #include "llvm/IR/Constants.h" 39 #include "llvm/IR/DebugInfo.h" 40 #include "llvm/IR/DebugLoc.h" 41 #include "llvm/IR/Function.h" 42 #include "llvm/IR/GlobalValue.h" 43 #include "llvm/IR/InstrTypes.h" 44 #include "llvm/IR/Instructions.h" 45 #include "llvm/IR/Intrinsics.h" 46 #include "llvm/IR/IRPrintingPasses.h" 47 #include "llvm/IR/Module.h" 48 #include "llvm/IR/ModuleSlotTracker.h" 49 #include "llvm/IR/Value.h" 50 #include "llvm/MC/LaneBitmask.h" 51 #include "llvm/MC/MCDwarf.h" 52 #include "llvm/MC/MCSymbol.h" 53 #include "llvm/Support/AtomicOrdering.h" 54 #include "llvm/Support/BranchProbability.h" 55 #include "llvm/Support/Casting.h" 56 #include "llvm/Support/CommandLine.h" 57 #include "llvm/Support/ErrorHandling.h" 58 #include "llvm/Support/Format.h" 59 #include "llvm/Support/LowLevelTypeImpl.h" 60 #include "llvm/Support/raw_ostream.h" 61 #include "llvm/Support/YAMLTraits.h" 62 #include "llvm/Target/TargetInstrInfo.h" 63 #include "llvm/Target/TargetIntrinsicInfo.h" 64 #include "llvm/Target/TargetMachine.h" 65 #include "llvm/Target/TargetRegisterInfo.h" 66 #include "llvm/Target/TargetSubtargetInfo.h" 67 #include <algorithm> 68 #include <cassert> 69 #include <cinttypes> 70 #include <cstdint> 71 #include <iterator> 72 #include <string> 73 #include <utility> 74 #include <vector> 75 76 using namespace llvm; 77 78 static cl::opt<bool> SimplifyMIR("simplify-mir", 79 cl::desc("Leave out unnecessary information when printing MIR")); 80 81 namespace { 82 83 /// This structure describes how to print out stack object references. 84 struct FrameIndexOperand { 85 std::string Name; 86 unsigned ID; 87 bool IsFixed; 88 89 FrameIndexOperand(StringRef Name, unsigned ID, bool IsFixed) 90 : Name(Name.str()), ID(ID), IsFixed(IsFixed) {} 91 92 /// Return an ordinary stack object reference. 93 static FrameIndexOperand create(StringRef Name, unsigned ID) { 94 return FrameIndexOperand(Name, ID, /*IsFixed=*/false); 95 } 96 97 /// Return a fixed stack object reference. 98 static FrameIndexOperand createFixed(unsigned ID) { 99 return FrameIndexOperand("", ID, /*IsFixed=*/true); 100 } 101 }; 102 103 } // end anonymous namespace 104 105 namespace llvm { 106 107 /// This class prints out the machine functions using the MIR serialization 108 /// format. 109 class MIRPrinter { 110 raw_ostream &OS; 111 DenseMap<const uint32_t *, unsigned> RegisterMaskIds; 112 /// Maps from stack object indices to operand indices which will be used when 113 /// printing frame index machine operands. 114 DenseMap<int, FrameIndexOperand> StackObjectOperandMapping; 115 116 public: 117 MIRPrinter(raw_ostream &OS) : OS(OS) {} 118 119 void print(const MachineFunction &MF); 120 121 void convert(yaml::MachineFunction &MF, const MachineRegisterInfo &RegInfo, 122 const TargetRegisterInfo *TRI); 123 void convert(ModuleSlotTracker &MST, yaml::MachineFrameInfo &YamlMFI, 124 const MachineFrameInfo &MFI); 125 void convert(yaml::MachineFunction &MF, 126 const MachineConstantPool &ConstantPool); 127 void convert(ModuleSlotTracker &MST, yaml::MachineJumpTable &YamlJTI, 128 const MachineJumpTableInfo &JTI); 129 void convertStackObjects(yaml::MachineFunction &YMF, 130 const MachineFunction &MF, ModuleSlotTracker &MST); 131 132 private: 133 void initRegisterMaskIds(const MachineFunction &MF); 134 }; 135 136 /// This class prints out the machine instructions using the MIR serialization 137 /// format. 138 class MIPrinter { 139 raw_ostream &OS; 140 ModuleSlotTracker &MST; 141 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds; 142 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping; 143 /// Synchronization scope names registered with LLVMContext. 144 SmallVector<StringRef, 8> SSNs; 145 146 bool canPredictBranchProbabilities(const MachineBasicBlock &MBB) const; 147 bool canPredictSuccessors(const MachineBasicBlock &MBB) const; 148 149 public: 150 MIPrinter(raw_ostream &OS, ModuleSlotTracker &MST, 151 const DenseMap<const uint32_t *, unsigned> &RegisterMaskIds, 152 const DenseMap<int, FrameIndexOperand> &StackObjectOperandMapping) 153 : OS(OS), MST(MST), RegisterMaskIds(RegisterMaskIds), 154 StackObjectOperandMapping(StackObjectOperandMapping) {} 155 156 void print(const MachineBasicBlock &MBB); 157 158 void print(const MachineInstr &MI); 159 void printMBBReference(const MachineBasicBlock &MBB); 160 void printIRBlockReference(const BasicBlock &BB); 161 void printIRValueReference(const Value &V); 162 void printStackObjectReference(int FrameIndex); 163 void printOffset(int64_t Offset); 164 void printTargetFlags(const MachineOperand &Op); 165 void print(const MachineOperand &Op, const TargetRegisterInfo *TRI, 166 unsigned I, bool ShouldPrintRegisterTies, 167 LLT TypeToPrint, bool IsDef = false); 168 void print(const LLVMContext &Context, const TargetInstrInfo &TII, 169 const MachineMemOperand &Op); 170 void printSyncScope(const LLVMContext &Context, SyncScope::ID SSID); 171 172 void print(const MCCFIInstruction &CFI, const TargetRegisterInfo *TRI); 173 }; 174 175 } // end namespace llvm 176 177 namespace llvm { 178 namespace yaml { 179 180 /// This struct serializes the LLVM IR module. 181 template <> struct BlockScalarTraits<Module> { 182 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) { 183 Mod.print(OS, nullptr); 184 } 185 186 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) { 187 llvm_unreachable("LLVM Module is supposed to be parsed separately"); 188 return ""; 189 } 190 }; 191 192 } // end namespace yaml 193 } // end namespace llvm 194 195 static void printReg(unsigned Reg, raw_ostream &OS, 196 const TargetRegisterInfo *TRI) { 197 // TODO: Print Stack Slots. 198 if (!Reg) 199 OS << '_'; 200 else if (TargetRegisterInfo::isVirtualRegister(Reg)) 201 OS << '%' << TargetRegisterInfo::virtReg2Index(Reg); 202 else if (Reg < TRI->getNumRegs()) 203 OS << '%' << StringRef(TRI->getName(Reg)).lower(); 204 else 205 llvm_unreachable("Can't print this kind of register yet"); 206 } 207 208 static void printReg(unsigned Reg, yaml::StringValue &Dest, 209 const TargetRegisterInfo *TRI) { 210 raw_string_ostream OS(Dest.Value); 211 printReg(Reg, OS, TRI); 212 } 213 214 void MIRPrinter::print(const MachineFunction &MF) { 215 initRegisterMaskIds(MF); 216 217 yaml::MachineFunction YamlMF; 218 YamlMF.Name = MF.getName(); 219 YamlMF.Alignment = MF.getAlignment(); 220 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice(); 221 222 YamlMF.Legalized = MF.getProperties().hasProperty( 223 MachineFunctionProperties::Property::Legalized); 224 YamlMF.RegBankSelected = MF.getProperties().hasProperty( 225 MachineFunctionProperties::Property::RegBankSelected); 226 YamlMF.Selected = MF.getProperties().hasProperty( 227 MachineFunctionProperties::Property::Selected); 228 229 convert(YamlMF, MF.getRegInfo(), MF.getSubtarget().getRegisterInfo()); 230 ModuleSlotTracker MST(MF.getFunction()->getParent()); 231 MST.incorporateFunction(*MF.getFunction()); 232 convert(MST, YamlMF.FrameInfo, MF.getFrameInfo()); 233 convertStackObjects(YamlMF, MF, MST); 234 if (const auto *ConstantPool = MF.getConstantPool()) 235 convert(YamlMF, *ConstantPool); 236 if (const auto *JumpTableInfo = MF.getJumpTableInfo()) 237 convert(MST, YamlMF.JumpTableInfo, *JumpTableInfo); 238 raw_string_ostream StrOS(YamlMF.Body.Value.Value); 239 bool IsNewlineNeeded = false; 240 for (const auto &MBB : MF) { 241 if (IsNewlineNeeded) 242 StrOS << "\n"; 243 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping) 244 .print(MBB); 245 IsNewlineNeeded = true; 246 } 247 StrOS.flush(); 248 yaml::Output Out(OS); 249 if (!SimplifyMIR) 250 Out.setWriteDefaultValues(true); 251 Out << YamlMF; 252 } 253 254 static void printCustomRegMask(const uint32_t *RegMask, raw_ostream &OS, 255 const TargetRegisterInfo *TRI) { 256 assert(RegMask && "Can't print an empty register mask"); 257 OS << StringRef("CustomRegMask("); 258 259 bool IsRegInRegMaskFound = false; 260 for (int I = 0, E = TRI->getNumRegs(); I < E; I++) { 261 // Check whether the register is asserted in regmask. 262 if (RegMask[I / 32] & (1u << (I % 32))) { 263 if (IsRegInRegMaskFound) 264 OS << ','; 265 printReg(I, OS, TRI); 266 IsRegInRegMaskFound = true; 267 } 268 } 269 270 OS << ')'; 271 } 272 273 void MIRPrinter::convert(yaml::MachineFunction &MF, 274 const MachineRegisterInfo &RegInfo, 275 const TargetRegisterInfo *TRI) { 276 MF.TracksRegLiveness = RegInfo.tracksLiveness(); 277 278 // Print the virtual register definitions. 279 for (unsigned I = 0, E = RegInfo.getNumVirtRegs(); I < E; ++I) { 280 unsigned Reg = TargetRegisterInfo::index2VirtReg(I); 281 yaml::VirtualRegisterDefinition VReg; 282 VReg.ID = I; 283 if (RegInfo.getRegClassOrNull(Reg)) 284 VReg.Class = 285 StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower(); 286 else if (RegInfo.getRegBankOrNull(Reg)) 287 VReg.Class = StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower(); 288 else { 289 VReg.Class = std::string("_"); 290 assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) && 291 "Generic registers must have a valid type"); 292 } 293 unsigned PreferredReg = RegInfo.getSimpleHint(Reg); 294 if (PreferredReg) 295 printReg(PreferredReg, VReg.PreferredRegister, TRI); 296 MF.VirtualRegisters.push_back(VReg); 297 } 298 299 // Print the live ins. 300 for (auto I = RegInfo.livein_begin(), E = RegInfo.livein_end(); I != E; ++I) { 301 yaml::MachineFunctionLiveIn LiveIn; 302 printReg(I->first, LiveIn.Register, TRI); 303 if (I->second) 304 printReg(I->second, LiveIn.VirtualRegister, TRI); 305 MF.LiveIns.push_back(LiveIn); 306 } 307 308 // Prints the callee saved registers. 309 if (RegInfo.isUpdatedCSRsInitialized()) { 310 const MCPhysReg *CalleeSavedRegs = RegInfo.getCalleeSavedRegs(); 311 std::vector<yaml::FlowStringValue> CalleeSavedRegisters; 312 for (const MCPhysReg *I = CalleeSavedRegs; *I; ++I) { 313 yaml::FlowStringValue Reg; 314 printReg(*I, Reg, TRI); 315 CalleeSavedRegisters.push_back(Reg); 316 } 317 MF.CalleeSavedRegisters = CalleeSavedRegisters; 318 } 319 } 320 321 void MIRPrinter::convert(ModuleSlotTracker &MST, 322 yaml::MachineFrameInfo &YamlMFI, 323 const MachineFrameInfo &MFI) { 324 YamlMFI.IsFrameAddressTaken = MFI.isFrameAddressTaken(); 325 YamlMFI.IsReturnAddressTaken = MFI.isReturnAddressTaken(); 326 YamlMFI.HasStackMap = MFI.hasStackMap(); 327 YamlMFI.HasPatchPoint = MFI.hasPatchPoint(); 328 YamlMFI.StackSize = MFI.getStackSize(); 329 YamlMFI.OffsetAdjustment = MFI.getOffsetAdjustment(); 330 YamlMFI.MaxAlignment = MFI.getMaxAlignment(); 331 YamlMFI.AdjustsStack = MFI.adjustsStack(); 332 YamlMFI.HasCalls = MFI.hasCalls(); 333 YamlMFI.MaxCallFrameSize = MFI.isMaxCallFrameSizeComputed() 334 ? MFI.getMaxCallFrameSize() : ~0u; 335 YamlMFI.HasOpaqueSPAdjustment = MFI.hasOpaqueSPAdjustment(); 336 YamlMFI.HasVAStart = MFI.hasVAStart(); 337 YamlMFI.HasMustTailInVarArgFunc = MFI.hasMustTailInVarArgFunc(); 338 if (MFI.getSavePoint()) { 339 raw_string_ostream StrOS(YamlMFI.SavePoint.Value); 340 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping) 341 .printMBBReference(*MFI.getSavePoint()); 342 } 343 if (MFI.getRestorePoint()) { 344 raw_string_ostream StrOS(YamlMFI.RestorePoint.Value); 345 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping) 346 .printMBBReference(*MFI.getRestorePoint()); 347 } 348 } 349 350 void MIRPrinter::convertStackObjects(yaml::MachineFunction &YMF, 351 const MachineFunction &MF, 352 ModuleSlotTracker &MST) { 353 const MachineFrameInfo &MFI = MF.getFrameInfo(); 354 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 355 // Process fixed stack objects. 356 unsigned ID = 0; 357 for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) { 358 if (MFI.isDeadObjectIndex(I)) 359 continue; 360 361 yaml::FixedMachineStackObject YamlObject; 362 YamlObject.ID = ID; 363 YamlObject.Type = MFI.isSpillSlotObjectIndex(I) 364 ? yaml::FixedMachineStackObject::SpillSlot 365 : yaml::FixedMachineStackObject::DefaultType; 366 YamlObject.Offset = MFI.getObjectOffset(I); 367 YamlObject.Size = MFI.getObjectSize(I); 368 YamlObject.Alignment = MFI.getObjectAlignment(I); 369 YamlObject.StackID = MFI.getStackID(I); 370 YamlObject.IsImmutable = MFI.isImmutableObjectIndex(I); 371 YamlObject.IsAliased = MFI.isAliasedObjectIndex(I); 372 YMF.FixedStackObjects.push_back(YamlObject); 373 StackObjectOperandMapping.insert( 374 std::make_pair(I, FrameIndexOperand::createFixed(ID++))); 375 } 376 377 // Process ordinary stack objects. 378 ID = 0; 379 for (int I = 0, E = MFI.getObjectIndexEnd(); I < E; ++I) { 380 if (MFI.isDeadObjectIndex(I)) 381 continue; 382 383 yaml::MachineStackObject YamlObject; 384 YamlObject.ID = ID; 385 if (const auto *Alloca = MFI.getObjectAllocation(I)) 386 YamlObject.Name.Value = 387 Alloca->hasName() ? Alloca->getName() : "<unnamed alloca>"; 388 YamlObject.Type = MFI.isSpillSlotObjectIndex(I) 389 ? yaml::MachineStackObject::SpillSlot 390 : MFI.isVariableSizedObjectIndex(I) 391 ? yaml::MachineStackObject::VariableSized 392 : yaml::MachineStackObject::DefaultType; 393 YamlObject.Offset = MFI.getObjectOffset(I); 394 YamlObject.Size = MFI.getObjectSize(I); 395 YamlObject.Alignment = MFI.getObjectAlignment(I); 396 YamlObject.StackID = MFI.getStackID(I); 397 398 YMF.StackObjects.push_back(YamlObject); 399 StackObjectOperandMapping.insert(std::make_pair( 400 I, FrameIndexOperand::create(YamlObject.Name.Value, ID++))); 401 } 402 403 for (const auto &CSInfo : MFI.getCalleeSavedInfo()) { 404 yaml::StringValue Reg; 405 printReg(CSInfo.getReg(), Reg, TRI); 406 auto StackObjectInfo = StackObjectOperandMapping.find(CSInfo.getFrameIdx()); 407 assert(StackObjectInfo != StackObjectOperandMapping.end() && 408 "Invalid stack object index"); 409 const FrameIndexOperand &StackObject = StackObjectInfo->second; 410 if (StackObject.IsFixed) 411 YMF.FixedStackObjects[StackObject.ID].CalleeSavedRegister = Reg; 412 else 413 YMF.StackObjects[StackObject.ID].CalleeSavedRegister = Reg; 414 } 415 for (unsigned I = 0, E = MFI.getLocalFrameObjectCount(); I < E; ++I) { 416 auto LocalObject = MFI.getLocalFrameObjectMap(I); 417 auto StackObjectInfo = StackObjectOperandMapping.find(LocalObject.first); 418 assert(StackObjectInfo != StackObjectOperandMapping.end() && 419 "Invalid stack object index"); 420 const FrameIndexOperand &StackObject = StackObjectInfo->second; 421 assert(!StackObject.IsFixed && "Expected a locally mapped stack object"); 422 YMF.StackObjects[StackObject.ID].LocalOffset = LocalObject.second; 423 } 424 425 // Print the stack object references in the frame information class after 426 // converting the stack objects. 427 if (MFI.hasStackProtectorIndex()) { 428 raw_string_ostream StrOS(YMF.FrameInfo.StackProtector.Value); 429 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping) 430 .printStackObjectReference(MFI.getStackProtectorIndex()); 431 } 432 433 // Print the debug variable information. 434 for (const MachineFunction::VariableDbgInfo &DebugVar : 435 MF.getVariableDbgInfo()) { 436 auto StackObjectInfo = StackObjectOperandMapping.find(DebugVar.Slot); 437 assert(StackObjectInfo != StackObjectOperandMapping.end() && 438 "Invalid stack object index"); 439 const FrameIndexOperand &StackObject = StackObjectInfo->second; 440 assert(!StackObject.IsFixed && "Expected a non-fixed stack object"); 441 auto &Object = YMF.StackObjects[StackObject.ID]; 442 { 443 raw_string_ostream StrOS(Object.DebugVar.Value); 444 DebugVar.Var->printAsOperand(StrOS, MST); 445 } 446 { 447 raw_string_ostream StrOS(Object.DebugExpr.Value); 448 DebugVar.Expr->printAsOperand(StrOS, MST); 449 } 450 { 451 raw_string_ostream StrOS(Object.DebugLoc.Value); 452 DebugVar.Loc->printAsOperand(StrOS, MST); 453 } 454 } 455 } 456 457 void MIRPrinter::convert(yaml::MachineFunction &MF, 458 const MachineConstantPool &ConstantPool) { 459 unsigned ID = 0; 460 for (const MachineConstantPoolEntry &Constant : ConstantPool.getConstants()) { 461 std::string Str; 462 raw_string_ostream StrOS(Str); 463 if (Constant.isMachineConstantPoolEntry()) { 464 Constant.Val.MachineCPVal->print(StrOS); 465 } else { 466 Constant.Val.ConstVal->printAsOperand(StrOS); 467 } 468 469 yaml::MachineConstantPoolValue YamlConstant; 470 YamlConstant.ID = ID++; 471 YamlConstant.Value = StrOS.str(); 472 YamlConstant.Alignment = Constant.getAlignment(); 473 YamlConstant.IsTargetSpecific = Constant.isMachineConstantPoolEntry(); 474 475 MF.Constants.push_back(YamlConstant); 476 } 477 } 478 479 void MIRPrinter::convert(ModuleSlotTracker &MST, 480 yaml::MachineJumpTable &YamlJTI, 481 const MachineJumpTableInfo &JTI) { 482 YamlJTI.Kind = JTI.getEntryKind(); 483 unsigned ID = 0; 484 for (const auto &Table : JTI.getJumpTables()) { 485 std::string Str; 486 yaml::MachineJumpTable::Entry Entry; 487 Entry.ID = ID++; 488 for (const auto *MBB : Table.MBBs) { 489 raw_string_ostream StrOS(Str); 490 MIPrinter(StrOS, MST, RegisterMaskIds, StackObjectOperandMapping) 491 .printMBBReference(*MBB); 492 Entry.Blocks.push_back(StrOS.str()); 493 Str.clear(); 494 } 495 YamlJTI.Entries.push_back(Entry); 496 } 497 } 498 499 void MIRPrinter::initRegisterMaskIds(const MachineFunction &MF) { 500 const auto *TRI = MF.getSubtarget().getRegisterInfo(); 501 unsigned I = 0; 502 for (const uint32_t *Mask : TRI->getRegMasks()) 503 RegisterMaskIds.insert(std::make_pair(Mask, I++)); 504 } 505 506 void llvm::guessSuccessors(const MachineBasicBlock &MBB, 507 SmallVectorImpl<MachineBasicBlock*> &Result, 508 bool &IsFallthrough) { 509 SmallPtrSet<MachineBasicBlock*,8> Seen; 510 511 for (const MachineInstr &MI : MBB) { 512 if (MI.isPHI()) 513 continue; 514 for (const MachineOperand &MO : MI.operands()) { 515 if (!MO.isMBB()) 516 continue; 517 MachineBasicBlock *Succ = MO.getMBB(); 518 auto RP = Seen.insert(Succ); 519 if (RP.second) 520 Result.push_back(Succ); 521 } 522 } 523 MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr(); 524 IsFallthrough = I == MBB.end() || !I->isBarrier(); 525 } 526 527 bool 528 MIPrinter::canPredictBranchProbabilities(const MachineBasicBlock &MBB) const { 529 if (MBB.succ_size() <= 1) 530 return true; 531 if (!MBB.hasSuccessorProbabilities()) 532 return true; 533 534 SmallVector<BranchProbability,8> Normalized(MBB.Probs.begin(), 535 MBB.Probs.end()); 536 BranchProbability::normalizeProbabilities(Normalized.begin(), 537 Normalized.end()); 538 SmallVector<BranchProbability,8> Equal(Normalized.size()); 539 BranchProbability::normalizeProbabilities(Equal.begin(), Equal.end()); 540 541 return std::equal(Normalized.begin(), Normalized.end(), Equal.begin()); 542 } 543 544 bool MIPrinter::canPredictSuccessors(const MachineBasicBlock &MBB) const { 545 SmallVector<MachineBasicBlock*,8> GuessedSuccs; 546 bool GuessedFallthrough; 547 guessSuccessors(MBB, GuessedSuccs, GuessedFallthrough); 548 if (GuessedFallthrough) { 549 const MachineFunction &MF = *MBB.getParent(); 550 MachineFunction::const_iterator NextI = std::next(MBB.getIterator()); 551 if (NextI != MF.end()) { 552 MachineBasicBlock *Next = const_cast<MachineBasicBlock*>(&*NextI); 553 if (!is_contained(GuessedSuccs, Next)) 554 GuessedSuccs.push_back(Next); 555 } 556 } 557 if (GuessedSuccs.size() != MBB.succ_size()) 558 return false; 559 return std::equal(MBB.succ_begin(), MBB.succ_end(), GuessedSuccs.begin()); 560 } 561 562 void MIPrinter::print(const MachineBasicBlock &MBB) { 563 assert(MBB.getNumber() >= 0 && "Invalid MBB number"); 564 OS << "bb." << MBB.getNumber(); 565 bool HasAttributes = false; 566 if (const auto *BB = MBB.getBasicBlock()) { 567 if (BB->hasName()) { 568 OS << "." << BB->getName(); 569 } else { 570 HasAttributes = true; 571 OS << " ("; 572 int Slot = MST.getLocalSlot(BB); 573 if (Slot == -1) 574 OS << "<ir-block badref>"; 575 else 576 OS << (Twine("%ir-block.") + Twine(Slot)).str(); 577 } 578 } 579 if (MBB.hasAddressTaken()) { 580 OS << (HasAttributes ? ", " : " ("); 581 OS << "address-taken"; 582 HasAttributes = true; 583 } 584 if (MBB.isEHPad()) { 585 OS << (HasAttributes ? ", " : " ("); 586 OS << "landing-pad"; 587 HasAttributes = true; 588 } 589 if (MBB.getAlignment()) { 590 OS << (HasAttributes ? ", " : " ("); 591 OS << "align " << MBB.getAlignment(); 592 HasAttributes = true; 593 } 594 if (HasAttributes) 595 OS << ")"; 596 OS << ":\n"; 597 598 bool HasLineAttributes = false; 599 // Print the successors 600 bool canPredictProbs = canPredictBranchProbabilities(MBB); 601 // Even if the list of successors is empty, if we cannot guess it, 602 // we need to print it to tell the parser that the list is empty. 603 // This is needed, because MI model unreachable as empty blocks 604 // with an empty successor list. If the parser would see that 605 // without the successor list, it would guess the code would 606 // fallthrough. 607 if ((!MBB.succ_empty() && !SimplifyMIR) || !canPredictProbs || 608 !canPredictSuccessors(MBB)) { 609 OS.indent(2) << "successors: "; 610 for (auto I = MBB.succ_begin(), E = MBB.succ_end(); I != E; ++I) { 611 if (I != MBB.succ_begin()) 612 OS << ", "; 613 printMBBReference(**I); 614 if (!SimplifyMIR || !canPredictProbs) 615 OS << '(' 616 << format("0x%08" PRIx32, MBB.getSuccProbability(I).getNumerator()) 617 << ')'; 618 } 619 OS << "\n"; 620 HasLineAttributes = true; 621 } 622 623 // Print the live in registers. 624 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 625 if (MRI.tracksLiveness() && !MBB.livein_empty()) { 626 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); 627 OS.indent(2) << "liveins: "; 628 bool First = true; 629 for (const auto &LI : MBB.liveins()) { 630 if (!First) 631 OS << ", "; 632 First = false; 633 printReg(LI.PhysReg, OS, &TRI); 634 if (!LI.LaneMask.all()) 635 OS << ":0x" << PrintLaneMask(LI.LaneMask); 636 } 637 OS << "\n"; 638 HasLineAttributes = true; 639 } 640 641 if (HasLineAttributes) 642 OS << "\n"; 643 bool IsInBundle = false; 644 for (auto I = MBB.instr_begin(), E = MBB.instr_end(); I != E; ++I) { 645 const MachineInstr &MI = *I; 646 if (IsInBundle && !MI.isInsideBundle()) { 647 OS.indent(2) << "}\n"; 648 IsInBundle = false; 649 } 650 OS.indent(IsInBundle ? 4 : 2); 651 print(MI); 652 if (!IsInBundle && MI.getFlag(MachineInstr::BundledSucc)) { 653 OS << " {"; 654 IsInBundle = true; 655 } 656 OS << "\n"; 657 } 658 if (IsInBundle) 659 OS.indent(2) << "}\n"; 660 } 661 662 /// Return true when an instruction has tied register that can't be determined 663 /// by the instruction's descriptor. 664 static bool hasComplexRegisterTies(const MachineInstr &MI) { 665 const MCInstrDesc &MCID = MI.getDesc(); 666 for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) { 667 const auto &Operand = MI.getOperand(I); 668 if (!Operand.isReg() || Operand.isDef()) 669 // Ignore the defined registers as MCID marks only the uses as tied. 670 continue; 671 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO); 672 int TiedIdx = Operand.isTied() ? int(MI.findTiedOperandIdx(I)) : -1; 673 if (ExpectedTiedIdx != TiedIdx) 674 return true; 675 } 676 return false; 677 } 678 679 static LLT getTypeToPrint(const MachineInstr &MI, unsigned OpIdx, 680 SmallBitVector &PrintedTypes, 681 const MachineRegisterInfo &MRI) { 682 const MachineOperand &Op = MI.getOperand(OpIdx); 683 if (!Op.isReg()) 684 return LLT{}; 685 686 if (MI.isVariadic() || OpIdx >= MI.getNumExplicitOperands()) 687 return MRI.getType(Op.getReg()); 688 689 auto &OpInfo = MI.getDesc().OpInfo[OpIdx]; 690 if (!OpInfo.isGenericType()) 691 return MRI.getType(Op.getReg()); 692 693 if (PrintedTypes[OpInfo.getGenericTypeIndex()]) 694 return LLT{}; 695 696 PrintedTypes.set(OpInfo.getGenericTypeIndex()); 697 return MRI.getType(Op.getReg()); 698 } 699 700 void MIPrinter::print(const MachineInstr &MI) { 701 const auto *MF = MI.getParent()->getParent(); 702 const auto &MRI = MF->getRegInfo(); 703 const auto &SubTarget = MF->getSubtarget(); 704 const auto *TRI = SubTarget.getRegisterInfo(); 705 assert(TRI && "Expected target register info"); 706 const auto *TII = SubTarget.getInstrInfo(); 707 assert(TII && "Expected target instruction info"); 708 if (MI.isCFIInstruction()) 709 assert(MI.getNumOperands() == 1 && "Expected 1 operand in CFI instruction"); 710 711 SmallBitVector PrintedTypes(8); 712 bool ShouldPrintRegisterTies = hasComplexRegisterTies(MI); 713 unsigned I = 0, E = MI.getNumOperands(); 714 for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() && 715 !MI.getOperand(I).isImplicit(); 716 ++I) { 717 if (I) 718 OS << ", "; 719 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies, 720 getTypeToPrint(MI, I, PrintedTypes, MRI), 721 /*IsDef=*/true); 722 } 723 724 if (I) 725 OS << " = "; 726 if (MI.getFlag(MachineInstr::FrameSetup)) 727 OS << "frame-setup "; 728 OS << TII->getName(MI.getOpcode()); 729 if (I < E) 730 OS << ' '; 731 732 bool NeedComma = false; 733 for (; I < E; ++I) { 734 if (NeedComma) 735 OS << ", "; 736 print(MI.getOperand(I), TRI, I, ShouldPrintRegisterTies, 737 getTypeToPrint(MI, I, PrintedTypes, MRI)); 738 NeedComma = true; 739 } 740 741 if (MI.getDebugLoc()) { 742 if (NeedComma) 743 OS << ','; 744 OS << " debug-location "; 745 MI.getDebugLoc()->printAsOperand(OS, MST); 746 } 747 748 if (!MI.memoperands_empty()) { 749 OS << " :: "; 750 const LLVMContext &Context = MF->getFunction()->getContext(); 751 bool NeedComma = false; 752 for (const auto *Op : MI.memoperands()) { 753 if (NeedComma) 754 OS << ", "; 755 print(Context, *TII, *Op); 756 NeedComma = true; 757 } 758 } 759 } 760 761 void MIPrinter::printMBBReference(const MachineBasicBlock &MBB) { 762 OS << "%bb." << MBB.getNumber(); 763 if (const auto *BB = MBB.getBasicBlock()) { 764 if (BB->hasName()) 765 OS << '.' << BB->getName(); 766 } 767 } 768 769 static void printIRSlotNumber(raw_ostream &OS, int Slot) { 770 if (Slot == -1) 771 OS << "<badref>"; 772 else 773 OS << Slot; 774 } 775 776 void MIPrinter::printIRBlockReference(const BasicBlock &BB) { 777 OS << "%ir-block."; 778 if (BB.hasName()) { 779 printLLVMNameWithoutPrefix(OS, BB.getName()); 780 return; 781 } 782 const Function *F = BB.getParent(); 783 int Slot; 784 if (F == MST.getCurrentFunction()) { 785 Slot = MST.getLocalSlot(&BB); 786 } else { 787 ModuleSlotTracker CustomMST(F->getParent(), 788 /*ShouldInitializeAllMetadata=*/false); 789 CustomMST.incorporateFunction(*F); 790 Slot = CustomMST.getLocalSlot(&BB); 791 } 792 printIRSlotNumber(OS, Slot); 793 } 794 795 void MIPrinter::printIRValueReference(const Value &V) { 796 if (isa<GlobalValue>(V)) { 797 V.printAsOperand(OS, /*PrintType=*/false, MST); 798 return; 799 } 800 if (isa<Constant>(V)) { 801 // Machine memory operands can load/store to/from constant value pointers. 802 OS << '`'; 803 V.printAsOperand(OS, /*PrintType=*/true, MST); 804 OS << '`'; 805 return; 806 } 807 OS << "%ir."; 808 if (V.hasName()) { 809 printLLVMNameWithoutPrefix(OS, V.getName()); 810 return; 811 } 812 printIRSlotNumber(OS, MST.getLocalSlot(&V)); 813 } 814 815 void MIPrinter::printStackObjectReference(int FrameIndex) { 816 auto ObjectInfo = StackObjectOperandMapping.find(FrameIndex); 817 assert(ObjectInfo != StackObjectOperandMapping.end() && 818 "Invalid frame index"); 819 const FrameIndexOperand &Operand = ObjectInfo->second; 820 if (Operand.IsFixed) { 821 OS << "%fixed-stack." << Operand.ID; 822 return; 823 } 824 OS << "%stack." << Operand.ID; 825 if (!Operand.Name.empty()) 826 OS << '.' << Operand.Name; 827 } 828 829 void MIPrinter::printOffset(int64_t Offset) { 830 if (Offset == 0) 831 return; 832 if (Offset < 0) { 833 OS << " - " << -Offset; 834 return; 835 } 836 OS << " + " << Offset; 837 } 838 839 static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) { 840 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags(); 841 for (const auto &I : Flags) { 842 if (I.first == TF) { 843 return I.second; 844 } 845 } 846 return nullptr; 847 } 848 849 void MIPrinter::printTargetFlags(const MachineOperand &Op) { 850 if (!Op.getTargetFlags()) 851 return; 852 const auto *TII = 853 Op.getParent()->getParent()->getParent()->getSubtarget().getInstrInfo(); 854 assert(TII && "expected instruction info"); 855 auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags()); 856 OS << "target-flags("; 857 const bool HasDirectFlags = Flags.first; 858 const bool HasBitmaskFlags = Flags.second; 859 if (!HasDirectFlags && !HasBitmaskFlags) { 860 OS << "<unknown>) "; 861 return; 862 } 863 if (HasDirectFlags) { 864 if (const auto *Name = getTargetFlagName(TII, Flags.first)) 865 OS << Name; 866 else 867 OS << "<unknown target flag>"; 868 } 869 if (!HasBitmaskFlags) { 870 OS << ") "; 871 return; 872 } 873 bool IsCommaNeeded = HasDirectFlags; 874 unsigned BitMask = Flags.second; 875 auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags(); 876 for (const auto &Mask : BitMasks) { 877 // Check if the flag's bitmask has the bits of the current mask set. 878 if ((BitMask & Mask.first) == Mask.first) { 879 if (IsCommaNeeded) 880 OS << ", "; 881 IsCommaNeeded = true; 882 OS << Mask.second; 883 // Clear the bits which were serialized from the flag's bitmask. 884 BitMask &= ~(Mask.first); 885 } 886 } 887 if (BitMask) { 888 // When the resulting flag's bitmask isn't zero, we know that we didn't 889 // serialize all of the bit flags. 890 if (IsCommaNeeded) 891 OS << ", "; 892 OS << "<unknown bitmask target flag>"; 893 } 894 OS << ") "; 895 } 896 897 static const char *getTargetIndexName(const MachineFunction &MF, int Index) { 898 const auto *TII = MF.getSubtarget().getInstrInfo(); 899 assert(TII && "expected instruction info"); 900 auto Indices = TII->getSerializableTargetIndices(); 901 for (const auto &I : Indices) { 902 if (I.first == Index) { 903 return I.second; 904 } 905 } 906 return nullptr; 907 } 908 909 void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI, 910 unsigned I, bool ShouldPrintRegisterTies, LLT TypeToPrint, 911 bool IsDef) { 912 printTargetFlags(Op); 913 switch (Op.getType()) { 914 case MachineOperand::MO_Register: 915 if (Op.isImplicit()) 916 OS << (Op.isDef() ? "implicit-def " : "implicit "); 917 else if (!IsDef && Op.isDef()) 918 // Print the 'def' flag only when the operand is defined after '='. 919 OS << "def "; 920 if (Op.isInternalRead()) 921 OS << "internal "; 922 if (Op.isDead()) 923 OS << "dead "; 924 if (Op.isKill()) 925 OS << "killed "; 926 if (Op.isUndef()) 927 OS << "undef "; 928 if (Op.isEarlyClobber()) 929 OS << "early-clobber "; 930 if (Op.isDebug()) 931 OS << "debug-use "; 932 printReg(Op.getReg(), OS, TRI); 933 // Print the sub register. 934 if (Op.getSubReg() != 0) 935 OS << '.' << TRI->getSubRegIndexName(Op.getSubReg()); 936 if (ShouldPrintRegisterTies && Op.isTied() && !Op.isDef()) 937 OS << "(tied-def " << Op.getParent()->findTiedOperandIdx(I) << ")"; 938 if (TypeToPrint.isValid()) 939 OS << '(' << TypeToPrint << ')'; 940 break; 941 case MachineOperand::MO_Immediate: 942 OS << Op.getImm(); 943 break; 944 case MachineOperand::MO_CImmediate: 945 Op.getCImm()->printAsOperand(OS, /*PrintType=*/true, MST); 946 break; 947 case MachineOperand::MO_FPImmediate: 948 Op.getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST); 949 break; 950 case MachineOperand::MO_MachineBasicBlock: 951 printMBBReference(*Op.getMBB()); 952 break; 953 case MachineOperand::MO_FrameIndex: 954 printStackObjectReference(Op.getIndex()); 955 break; 956 case MachineOperand::MO_ConstantPoolIndex: 957 OS << "%const." << Op.getIndex(); 958 printOffset(Op.getOffset()); 959 break; 960 case MachineOperand::MO_TargetIndex: 961 OS << "target-index("; 962 if (const auto *Name = getTargetIndexName( 963 *Op.getParent()->getParent()->getParent(), Op.getIndex())) 964 OS << Name; 965 else 966 OS << "<unknown>"; 967 OS << ')'; 968 printOffset(Op.getOffset()); 969 break; 970 case MachineOperand::MO_JumpTableIndex: 971 OS << "%jump-table." << Op.getIndex(); 972 break; 973 case MachineOperand::MO_ExternalSymbol: { 974 StringRef Name = Op.getSymbolName(); 975 OS << '$'; 976 if (Name.empty()) { 977 OS << "\"\""; 978 } else { 979 printLLVMNameWithoutPrefix(OS, Name); 980 } 981 printOffset(Op.getOffset()); 982 break; 983 } 984 case MachineOperand::MO_GlobalAddress: 985 Op.getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST); 986 printOffset(Op.getOffset()); 987 break; 988 case MachineOperand::MO_BlockAddress: 989 OS << "blockaddress("; 990 Op.getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false, 991 MST); 992 OS << ", "; 993 printIRBlockReference(*Op.getBlockAddress()->getBasicBlock()); 994 OS << ')'; 995 printOffset(Op.getOffset()); 996 break; 997 case MachineOperand::MO_RegisterMask: { 998 auto RegMaskInfo = RegisterMaskIds.find(Op.getRegMask()); 999 if (RegMaskInfo != RegisterMaskIds.end()) 1000 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower(); 1001 else 1002 printCustomRegMask(Op.getRegMask(), OS, TRI); 1003 break; 1004 } 1005 case MachineOperand::MO_RegisterLiveOut: { 1006 const uint32_t *RegMask = Op.getRegLiveOut(); 1007 OS << "liveout("; 1008 bool IsCommaNeeded = false; 1009 for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) { 1010 if (RegMask[Reg / 32] & (1U << (Reg % 32))) { 1011 if (IsCommaNeeded) 1012 OS << ", "; 1013 printReg(Reg, OS, TRI); 1014 IsCommaNeeded = true; 1015 } 1016 } 1017 OS << ")"; 1018 break; 1019 } 1020 case MachineOperand::MO_Metadata: 1021 Op.getMetadata()->printAsOperand(OS, MST); 1022 break; 1023 case MachineOperand::MO_MCSymbol: 1024 OS << "<mcsymbol " << *Op.getMCSymbol() << ">"; 1025 break; 1026 case MachineOperand::MO_CFIIndex: { 1027 const MachineFunction &MF = *Op.getParent()->getParent()->getParent(); 1028 print(MF.getFrameInstructions()[Op.getCFIIndex()], TRI); 1029 break; 1030 } 1031 case MachineOperand::MO_IntrinsicID: { 1032 Intrinsic::ID ID = Op.getIntrinsicID(); 1033 if (ID < Intrinsic::num_intrinsics) 1034 OS << "intrinsic(@" << Intrinsic::getName(ID, None) << ')'; 1035 else { 1036 const MachineFunction &MF = *Op.getParent()->getParent()->getParent(); 1037 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo(); 1038 OS << "intrinsic(@" << TII->getName(ID) << ')'; 1039 } 1040 break; 1041 } 1042 case MachineOperand::MO_Predicate: { 1043 auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate()); 1044 OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred(" 1045 << CmpInst::getPredicateName(Pred) << ')'; 1046 break; 1047 } 1048 } 1049 } 1050 1051 static const char *getTargetMMOFlagName(const TargetInstrInfo &TII, 1052 unsigned TMMOFlag) { 1053 auto Flags = TII.getSerializableMachineMemOperandTargetFlags(); 1054 for (const auto &I : Flags) { 1055 if (I.first == TMMOFlag) { 1056 return I.second; 1057 } 1058 } 1059 return nullptr; 1060 } 1061 1062 void MIPrinter::print(const LLVMContext &Context, const TargetInstrInfo &TII, 1063 const MachineMemOperand &Op) { 1064 OS << '('; 1065 if (Op.isVolatile()) 1066 OS << "volatile "; 1067 if (Op.isNonTemporal()) 1068 OS << "non-temporal "; 1069 if (Op.isDereferenceable()) 1070 OS << "dereferenceable "; 1071 if (Op.isInvariant()) 1072 OS << "invariant "; 1073 if (Op.getFlags() & MachineMemOperand::MOTargetFlag1) 1074 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag1) 1075 << "\" "; 1076 if (Op.getFlags() & MachineMemOperand::MOTargetFlag2) 1077 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag2) 1078 << "\" "; 1079 if (Op.getFlags() & MachineMemOperand::MOTargetFlag3) 1080 OS << '"' << getTargetMMOFlagName(TII, MachineMemOperand::MOTargetFlag3) 1081 << "\" "; 1082 if (Op.isLoad()) 1083 OS << "load "; 1084 else { 1085 assert(Op.isStore() && "Non load machine operand must be a store"); 1086 OS << "store "; 1087 } 1088 1089 printSyncScope(Context, Op.getSyncScopeID()); 1090 1091 if (Op.getOrdering() != AtomicOrdering::NotAtomic) 1092 OS << toIRString(Op.getOrdering()) << ' '; 1093 if (Op.getFailureOrdering() != AtomicOrdering::NotAtomic) 1094 OS << toIRString(Op.getFailureOrdering()) << ' '; 1095 1096 OS << Op.getSize(); 1097 if (const Value *Val = Op.getValue()) { 1098 OS << (Op.isLoad() ? " from " : " into "); 1099 printIRValueReference(*Val); 1100 } else if (const PseudoSourceValue *PVal = Op.getPseudoValue()) { 1101 OS << (Op.isLoad() ? " from " : " into "); 1102 assert(PVal && "Expected a pseudo source value"); 1103 switch (PVal->kind()) { 1104 case PseudoSourceValue::Stack: 1105 OS << "stack"; 1106 break; 1107 case PseudoSourceValue::GOT: 1108 OS << "got"; 1109 break; 1110 case PseudoSourceValue::JumpTable: 1111 OS << "jump-table"; 1112 break; 1113 case PseudoSourceValue::ConstantPool: 1114 OS << "constant-pool"; 1115 break; 1116 case PseudoSourceValue::FixedStack: 1117 printStackObjectReference( 1118 cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex()); 1119 break; 1120 case PseudoSourceValue::GlobalValueCallEntry: 1121 OS << "call-entry "; 1122 cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand( 1123 OS, /*PrintType=*/false, MST); 1124 break; 1125 case PseudoSourceValue::ExternalSymbolCallEntry: 1126 OS << "call-entry $"; 1127 printLLVMNameWithoutPrefix( 1128 OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol()); 1129 break; 1130 case PseudoSourceValue::TargetCustom: 1131 llvm_unreachable("TargetCustom pseudo source values are not supported"); 1132 break; 1133 } 1134 } 1135 printOffset(Op.getOffset()); 1136 if (Op.getBaseAlignment() != Op.getSize()) 1137 OS << ", align " << Op.getBaseAlignment(); 1138 auto AAInfo = Op.getAAInfo(); 1139 if (AAInfo.TBAA) { 1140 OS << ", !tbaa "; 1141 AAInfo.TBAA->printAsOperand(OS, MST); 1142 } 1143 if (AAInfo.Scope) { 1144 OS << ", !alias.scope "; 1145 AAInfo.Scope->printAsOperand(OS, MST); 1146 } 1147 if (AAInfo.NoAlias) { 1148 OS << ", !noalias "; 1149 AAInfo.NoAlias->printAsOperand(OS, MST); 1150 } 1151 if (Op.getRanges()) { 1152 OS << ", !range "; 1153 Op.getRanges()->printAsOperand(OS, MST); 1154 } 1155 OS << ')'; 1156 } 1157 1158 void MIPrinter::printSyncScope(const LLVMContext &Context, SyncScope::ID SSID) { 1159 switch (SSID) { 1160 case SyncScope::System: { 1161 break; 1162 } 1163 default: { 1164 if (SSNs.empty()) 1165 Context.getSyncScopeNames(SSNs); 1166 1167 OS << "syncscope(\""; 1168 PrintEscapedString(SSNs[SSID], OS); 1169 OS << "\") "; 1170 break; 1171 } 1172 } 1173 } 1174 1175 static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS, 1176 const TargetRegisterInfo *TRI) { 1177 int Reg = TRI->getLLVMRegNum(DwarfReg, true); 1178 if (Reg == -1) { 1179 OS << "<badreg>"; 1180 return; 1181 } 1182 printReg(Reg, OS, TRI); 1183 } 1184 1185 void MIPrinter::print(const MCCFIInstruction &CFI, 1186 const TargetRegisterInfo *TRI) { 1187 switch (CFI.getOperation()) { 1188 case MCCFIInstruction::OpSameValue: 1189 OS << "same_value "; 1190 if (CFI.getLabel()) 1191 OS << "<mcsymbol> "; 1192 printCFIRegister(CFI.getRegister(), OS, TRI); 1193 break; 1194 case MCCFIInstruction::OpOffset: 1195 OS << "offset "; 1196 if (CFI.getLabel()) 1197 OS << "<mcsymbol> "; 1198 printCFIRegister(CFI.getRegister(), OS, TRI); 1199 OS << ", " << CFI.getOffset(); 1200 break; 1201 case MCCFIInstruction::OpDefCfaRegister: 1202 OS << "def_cfa_register "; 1203 if (CFI.getLabel()) 1204 OS << "<mcsymbol> "; 1205 printCFIRegister(CFI.getRegister(), OS, TRI); 1206 break; 1207 case MCCFIInstruction::OpDefCfaOffset: 1208 OS << "def_cfa_offset "; 1209 if (CFI.getLabel()) 1210 OS << "<mcsymbol> "; 1211 OS << CFI.getOffset(); 1212 break; 1213 case MCCFIInstruction::OpDefCfa: 1214 OS << "def_cfa "; 1215 if (CFI.getLabel()) 1216 OS << "<mcsymbol> "; 1217 printCFIRegister(CFI.getRegister(), OS, TRI); 1218 OS << ", " << CFI.getOffset(); 1219 break; 1220 default: 1221 // TODO: Print the other CFI Operations. 1222 OS << "<unserializable cfi operation>"; 1223 break; 1224 } 1225 } 1226 1227 void llvm::printMIR(raw_ostream &OS, const Module &M) { 1228 yaml::Output Out(OS); 1229 Out << const_cast<Module &>(M); 1230 } 1231 1232 void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) { 1233 MIRPrinter Printer(OS); 1234 Printer.print(MF); 1235 } 1236