MachineFunction.cpp revision 5dfa26795da9c521babd598ef911e6d95bd20d37
1//===-- MachineFunction.cpp -----------------------------------------------===// 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// Collect native machine code information for a function. This allows 11// target-specific information about the generated code to be stored with each 12// function. 13// 14//===----------------------------------------------------------------------===// 15 16#include "llvm/DerivedTypes.h" 17#include "llvm/Function.h" 18#include "llvm/Instructions.h" 19#include "llvm/Config/config.h" 20#include "llvm/CodeGen/MachineConstantPool.h" 21#include "llvm/CodeGen/MachineFunction.h" 22#include "llvm/CodeGen/MachineFunctionPass.h" 23#include "llvm/CodeGen/MachineFrameInfo.h" 24#include "llvm/CodeGen/MachineInstr.h" 25#include "llvm/CodeGen/MachineJumpTableInfo.h" 26#include "llvm/CodeGen/MachineRegisterInfo.h" 27#include "llvm/CodeGen/Passes.h" 28#include "llvm/MC/MCAsmInfo.h" 29#include "llvm/MC/MCContext.h" 30#include "llvm/Analysis/DebugInfo.h" 31#include "llvm/Support/Debug.h" 32#include "llvm/Target/TargetData.h" 33#include "llvm/Target/TargetLowering.h" 34#include "llvm/Target/TargetMachine.h" 35#include "llvm/Target/TargetFrameInfo.h" 36#include "llvm/ADT/SmallString.h" 37#include "llvm/ADT/STLExtras.h" 38#include "llvm/Support/GraphWriter.h" 39#include "llvm/Support/raw_ostream.h" 40using namespace llvm; 41 42namespace { 43 struct Printer : public MachineFunctionPass { 44 static char ID; 45 46 raw_ostream &OS; 47 const std::string Banner; 48 49 Printer(raw_ostream &os, const std::string &banner) 50 : MachineFunctionPass(&ID), OS(os), Banner(banner) {} 51 52 const char *getPassName() const { return "MachineFunction Printer"; } 53 54 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 55 AU.setPreservesAll(); 56 MachineFunctionPass::getAnalysisUsage(AU); 57 } 58 59 bool runOnMachineFunction(MachineFunction &MF) { 60 OS << "# " << Banner << ":\n"; 61 MF.print(OS); 62 return false; 63 } 64 }; 65 char Printer::ID = 0; 66} 67 68/// Returns a newly-created MachineFunction Printer pass. The default banner is 69/// empty. 70/// 71FunctionPass *llvm::createMachineFunctionPrinterPass(raw_ostream &OS, 72 const std::string &Banner){ 73 return new Printer(OS, Banner); 74} 75 76//===----------------------------------------------------------------------===// 77// MachineFunction implementation 78//===----------------------------------------------------------------------===// 79 80// Out of line virtual method. 81MachineFunctionInfo::~MachineFunctionInfo() {} 82 83void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) { 84 MBB->getParent()->DeleteMachineBasicBlock(MBB); 85} 86 87MachineFunction::MachineFunction(Function *F, const TargetMachine &TM, 88 unsigned FunctionNum) 89 : Fn(F), Target(TM) { 90 if (TM.getRegisterInfo()) 91 RegInfo = new (Allocator.Allocate<MachineRegisterInfo>()) 92 MachineRegisterInfo(*TM.getRegisterInfo()); 93 else 94 RegInfo = 0; 95 MFInfo = 0; 96 FrameInfo = new (Allocator.Allocate<MachineFrameInfo>()) 97 MachineFrameInfo(*TM.getFrameInfo()); 98 if (Fn->hasFnAttr(Attribute::StackAlignment)) 99 FrameInfo->setMaxAlignment(Attribute::getStackAlignmentFromAttrs( 100 Fn->getAttributes().getFnAttributes())); 101 ConstantPool = new (Allocator.Allocate<MachineConstantPool>()) 102 MachineConstantPool(TM.getTargetData()); 103 Alignment = TM.getTargetLowering()->getFunctionAlignment(F); 104 FunctionNumber = FunctionNum; 105 JumpTableInfo = 0; 106} 107 108MachineFunction::~MachineFunction() { 109 BasicBlocks.clear(); 110 InstructionRecycler.clear(Allocator); 111 BasicBlockRecycler.clear(Allocator); 112 if (RegInfo) { 113 RegInfo->~MachineRegisterInfo(); 114 Allocator.Deallocate(RegInfo); 115 } 116 if (MFInfo) { 117 MFInfo->~MachineFunctionInfo(); 118 Allocator.Deallocate(MFInfo); 119 } 120 FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo); 121 ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool); 122 123 if (JumpTableInfo) { 124 JumpTableInfo->~MachineJumpTableInfo(); 125 Allocator.Deallocate(JumpTableInfo); 126 } 127} 128 129/// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it 130/// does already exist, allocate one. 131MachineJumpTableInfo *MachineFunction:: 132getOrCreateJumpTableInfo(unsigned EntryKind) { 133 if (JumpTableInfo) return JumpTableInfo; 134 135 JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>()) 136 MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind); 137 return JumpTableInfo; 138} 139 140/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and 141/// recomputes them. This guarantees that the MBB numbers are sequential, 142/// dense, and match the ordering of the blocks within the function. If a 143/// specific MachineBasicBlock is specified, only that block and those after 144/// it are renumbered. 145void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { 146 if (empty()) { MBBNumbering.clear(); return; } 147 MachineFunction::iterator MBBI, E = end(); 148 if (MBB == 0) 149 MBBI = begin(); 150 else 151 MBBI = MBB; 152 153 // Figure out the block number this should have. 154 unsigned BlockNo = 0; 155 if (MBBI != begin()) 156 BlockNo = prior(MBBI)->getNumber()+1; 157 158 for (; MBBI != E; ++MBBI, ++BlockNo) { 159 if (MBBI->getNumber() != (int)BlockNo) { 160 // Remove use of the old number. 161 if (MBBI->getNumber() != -1) { 162 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && 163 "MBB number mismatch!"); 164 MBBNumbering[MBBI->getNumber()] = 0; 165 } 166 167 // If BlockNo is already taken, set that block's number to -1. 168 if (MBBNumbering[BlockNo]) 169 MBBNumbering[BlockNo]->setNumber(-1); 170 171 MBBNumbering[BlockNo] = MBBI; 172 MBBI->setNumber(BlockNo); 173 } 174 } 175 176 // Okay, all the blocks are renumbered. If we have compactified the block 177 // numbering, shrink MBBNumbering now. 178 assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); 179 MBBNumbering.resize(BlockNo); 180} 181 182/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead 183/// of `new MachineInstr'. 184/// 185MachineInstr * 186MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID, 187 DebugLoc DL, bool NoImp) { 188 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 189 MachineInstr(TID, DL, NoImp); 190} 191 192/// CloneMachineInstr - Create a new MachineInstr which is a copy of the 193/// 'Orig' instruction, identical in all ways except the instruction 194/// has no parent, prev, or next. 195/// 196MachineInstr * 197MachineFunction::CloneMachineInstr(const MachineInstr *Orig) { 198 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 199 MachineInstr(*this, *Orig); 200} 201 202/// DeleteMachineInstr - Delete the given MachineInstr. 203/// 204void 205MachineFunction::DeleteMachineInstr(MachineInstr *MI) { 206 MI->~MachineInstr(); 207 InstructionRecycler.Deallocate(Allocator, MI); 208} 209 210/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this 211/// instead of `new MachineBasicBlock'. 212/// 213MachineBasicBlock * 214MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { 215 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) 216 MachineBasicBlock(*this, bb); 217} 218 219/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock. 220/// 221void 222MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { 223 assert(MBB->getParent() == this && "MBB parent mismatch!"); 224 MBB->~MachineBasicBlock(); 225 BasicBlockRecycler.Deallocate(Allocator, MBB); 226} 227 228MachineMemOperand * 229MachineFunction::getMachineMemOperand(const Value *v, unsigned f, 230 int64_t o, uint64_t s, 231 unsigned base_alignment) { 232 return new (Allocator.Allocate<MachineMemOperand>()) 233 MachineMemOperand(v, f, o, s, base_alignment); 234} 235 236MachineMemOperand * 237MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 238 int64_t Offset, uint64_t Size) { 239 return new (Allocator.Allocate<MachineMemOperand>()) 240 MachineMemOperand(MMO->getValue(), MMO->getFlags(), 241 int64_t(uint64_t(MMO->getOffset()) + 242 uint64_t(Offset)), 243 Size, MMO->getBaseAlignment()); 244} 245 246MachineInstr::mmo_iterator 247MachineFunction::allocateMemRefsArray(unsigned long Num) { 248 return Allocator.Allocate<MachineMemOperand *>(Num); 249} 250 251std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator> 252MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin, 253 MachineInstr::mmo_iterator End) { 254 // Count the number of load mem refs. 255 unsigned Num = 0; 256 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) 257 if ((*I)->isLoad()) 258 ++Num; 259 260 // Allocate a new array and populate it with the load information. 261 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num); 262 unsigned Index = 0; 263 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) { 264 if ((*I)->isLoad()) { 265 if (!(*I)->isStore()) 266 // Reuse the MMO. 267 Result[Index] = *I; 268 else { 269 // Clone the MMO and unset the store flag. 270 MachineMemOperand *JustLoad = 271 getMachineMemOperand((*I)->getValue(), 272 (*I)->getFlags() & ~MachineMemOperand::MOStore, 273 (*I)->getOffset(), (*I)->getSize(), 274 (*I)->getBaseAlignment()); 275 Result[Index] = JustLoad; 276 } 277 ++Index; 278 } 279 } 280 return std::make_pair(Result, Result + Num); 281} 282 283std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator> 284MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin, 285 MachineInstr::mmo_iterator End) { 286 // Count the number of load mem refs. 287 unsigned Num = 0; 288 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) 289 if ((*I)->isStore()) 290 ++Num; 291 292 // Allocate a new array and populate it with the store information. 293 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num); 294 unsigned Index = 0; 295 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) { 296 if ((*I)->isStore()) { 297 if (!(*I)->isLoad()) 298 // Reuse the MMO. 299 Result[Index] = *I; 300 else { 301 // Clone the MMO and unset the load flag. 302 MachineMemOperand *JustStore = 303 getMachineMemOperand((*I)->getValue(), 304 (*I)->getFlags() & ~MachineMemOperand::MOLoad, 305 (*I)->getOffset(), (*I)->getSize(), 306 (*I)->getBaseAlignment()); 307 Result[Index] = JustStore; 308 } 309 ++Index; 310 } 311 } 312 return std::make_pair(Result, Result + Num); 313} 314 315void MachineFunction::dump() const { 316 print(dbgs()); 317} 318 319void MachineFunction::print(raw_ostream &OS) const { 320 OS << "# Machine code for function " << Fn->getName() << ":\n"; 321 322 // Print Frame Information 323 FrameInfo->print(*this, OS); 324 325 // Print JumpTable Information 326 if (JumpTableInfo) 327 JumpTableInfo->print(OS); 328 329 // Print Constant Pool 330 ConstantPool->print(OS); 331 332 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo(); 333 334 if (RegInfo && !RegInfo->livein_empty()) { 335 OS << "Function Live Ins: "; 336 for (MachineRegisterInfo::livein_iterator 337 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { 338 if (TRI) 339 OS << "%" << TRI->getName(I->first); 340 else 341 OS << " %physreg" << I->first; 342 343 if (I->second) 344 OS << " in reg%" << I->second; 345 346 if (llvm::next(I) != E) 347 OS << ", "; 348 } 349 OS << '\n'; 350 } 351 if (RegInfo && !RegInfo->liveout_empty()) { 352 OS << "Function Live Outs: "; 353 for (MachineRegisterInfo::liveout_iterator 354 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I){ 355 if (TRI) 356 OS << '%' << TRI->getName(*I); 357 else 358 OS << "%physreg" << *I; 359 360 if (llvm::next(I) != E) 361 OS << " "; 362 } 363 OS << '\n'; 364 } 365 366 for (const_iterator BB = begin(), E = end(); BB != E; ++BB) { 367 OS << '\n'; 368 BB->print(OS); 369 } 370 371 OS << "\n# End machine code for function " << Fn->getName() << ".\n\n"; 372} 373 374namespace llvm { 375 template<> 376 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { 377 378 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} 379 380 static std::string getGraphName(const MachineFunction *F) { 381 return "CFG for '" + F->getFunction()->getNameStr() + "' function"; 382 } 383 384 std::string getNodeLabel(const MachineBasicBlock *Node, 385 const MachineFunction *Graph) { 386 if (isSimple () && Node->getBasicBlock() && 387 !Node->getBasicBlock()->getName().empty()) 388 return Node->getBasicBlock()->getNameStr() + ":"; 389 390 std::string OutStr; 391 { 392 raw_string_ostream OSS(OutStr); 393 394 if (isSimple()) 395 OSS << Node->getNumber() << ':'; 396 else 397 Node->print(OSS); 398 } 399 400 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 401 402 // Process string output to make it nicer... 403 for (unsigned i = 0; i != OutStr.length(); ++i) 404 if (OutStr[i] == '\n') { // Left justify 405 OutStr[i] = '\\'; 406 OutStr.insert(OutStr.begin()+i+1, 'l'); 407 } 408 return OutStr; 409 } 410 }; 411} 412 413void MachineFunction::viewCFG() const 414{ 415#ifndef NDEBUG 416 ViewGraph(this, "mf" + getFunction()->getNameStr()); 417#else 418 errs() << "SelectionDAG::viewGraph is only available in debug builds on " 419 << "systems with Graphviz or gv!\n"; 420#endif // NDEBUG 421} 422 423void MachineFunction::viewCFGOnly() const 424{ 425#ifndef NDEBUG 426 ViewGraph(this, "mf" + getFunction()->getNameStr(), true); 427#else 428 errs() << "SelectionDAG::viewGraph is only available in debug builds on " 429 << "systems with Graphviz or gv!\n"; 430#endif // NDEBUG 431} 432 433/// addLiveIn - Add the specified physical register as a live-in value and 434/// create a corresponding virtual register for it. 435unsigned MachineFunction::addLiveIn(unsigned PReg, 436 const TargetRegisterClass *RC) { 437 assert(RC->contains(PReg) && "Not the correct regclass!"); 438 unsigned VReg = getRegInfo().createVirtualRegister(RC); 439 getRegInfo().addLiveIn(PReg, VReg); 440 return VReg; 441} 442 443/// getDILocation - Get the DILocation for a given DebugLoc object. 444DILocation MachineFunction::getDILocation(DebugLoc DL) const { 445 unsigned Idx = DL.getIndex(); 446 assert(Idx < DebugLocInfo.DebugLocations.size() && 447 "Invalid index into debug locations!"); 448 return DILocation(DebugLocInfo.DebugLocations[Idx]); 449} 450 451 452/// getJTISymbol - Return the MCSymbol for the specified non-empty jump table. 453/// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a 454/// normal 'L' label is returned. 455MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, 456 bool isLinkerPrivate) const { 457 assert(JumpTableInfo && "No jump tables"); 458 459 assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!"); 460 const MCAsmInfo &MAI = *getTarget().getMCAsmInfo(); 461 462 const char *Prefix = isLinkerPrivate ? MAI.getLinkerPrivateGlobalPrefix() : 463 MAI.getPrivateGlobalPrefix(); 464 SmallString<60> Name; 465 raw_svector_ostream(Name) 466 << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; 467 return Ctx.GetOrCreateSymbol(Name.str()); 468} 469 470 471//===----------------------------------------------------------------------===// 472// MachineFrameInfo implementation 473//===----------------------------------------------------------------------===// 474 475/// CreateFixedObject - Create a new object at a fixed location on the stack. 476/// All fixed objects should be created before other objects are created for 477/// efficiency. By default, fixed objects are immutable. This returns an 478/// index with a negative value. 479/// 480int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset, 481 bool Immutable, bool isSS) { 482 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!"); 483 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable, 484 isSS)); 485 return -++NumFixedObjects; 486} 487 488 489BitVector 490MachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const { 491 assert(MBB && "MBB must be valid"); 492 const MachineFunction *MF = MBB->getParent(); 493 assert(MF && "MBB must be part of a MachineFunction"); 494 const TargetMachine &TM = MF->getTarget(); 495 const TargetRegisterInfo *TRI = TM.getRegisterInfo(); 496 BitVector BV(TRI->getNumRegs()); 497 498 // Before CSI is calculated, no registers are considered pristine. They can be 499 // freely used and PEI will make sure they are saved. 500 if (!isCalleeSavedInfoValid()) 501 return BV; 502 503 for (const unsigned *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR) 504 BV.set(*CSR); 505 506 // The entry MBB always has all CSRs pristine. 507 if (MBB == &MF->front()) 508 return BV; 509 510 // On other MBBs the saved CSRs are not pristine. 511 const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo(); 512 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(), 513 E = CSI.end(); I != E; ++I) 514 BV.reset(I->getReg()); 515 516 return BV; 517} 518 519 520void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{ 521 if (Objects.empty()) return; 522 523 const TargetFrameInfo *FI = MF.getTarget().getFrameInfo(); 524 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0); 525 526 OS << "Frame Objects:\n"; 527 528 for (unsigned i = 0, e = Objects.size(); i != e; ++i) { 529 const StackObject &SO = Objects[i]; 530 OS << " fi#" << (int)(i-NumFixedObjects) << ": "; 531 if (SO.Size == ~0ULL) { 532 OS << "dead\n"; 533 continue; 534 } 535 if (SO.Size == 0) 536 OS << "variable sized"; 537 else 538 OS << "size=" << SO.Size; 539 OS << ", align=" << SO.Alignment; 540 541 if (i < NumFixedObjects) 542 OS << ", fixed"; 543 if (i < NumFixedObjects || SO.SPOffset != -1) { 544 int64_t Off = SO.SPOffset - ValOffset; 545 OS << ", at location [SP"; 546 if (Off > 0) 547 OS << "+" << Off; 548 else if (Off < 0) 549 OS << Off; 550 OS << "]"; 551 } 552 OS << "\n"; 553 } 554} 555 556void MachineFrameInfo::dump(const MachineFunction &MF) const { 557 print(MF, dbgs()); 558} 559 560//===----------------------------------------------------------------------===// 561// MachineJumpTableInfo implementation 562//===----------------------------------------------------------------------===// 563 564/// getEntrySize - Return the size of each entry in the jump table. 565unsigned MachineJumpTableInfo::getEntrySize(const TargetData &TD) const { 566 // The size of a jump table entry is 4 bytes unless the entry is just the 567 // address of a block, in which case it is the pointer size. 568 switch (getEntryKind()) { 569 case MachineJumpTableInfo::EK_BlockAddress: 570 return TD.getPointerSize(); 571 case MachineJumpTableInfo::EK_GPRel32BlockAddress: 572 case MachineJumpTableInfo::EK_LabelDifference32: 573 case MachineJumpTableInfo::EK_Custom32: 574 return 4; 575 } 576 assert(0 && "Unknown jump table encoding!"); 577 return ~0; 578} 579 580/// getEntryAlignment - Return the alignment of each entry in the jump table. 581unsigned MachineJumpTableInfo::getEntryAlignment(const TargetData &TD) const { 582 // The alignment of a jump table entry is the alignment of int32 unless the 583 // entry is just the address of a block, in which case it is the pointer 584 // alignment. 585 switch (getEntryKind()) { 586 case MachineJumpTableInfo::EK_BlockAddress: 587 return TD.getPointerABIAlignment(); 588 case MachineJumpTableInfo::EK_GPRel32BlockAddress: 589 case MachineJumpTableInfo::EK_LabelDifference32: 590 case MachineJumpTableInfo::EK_Custom32: 591 return TD.getABIIntegerTypeAlignment(32); 592 } 593 assert(0 && "Unknown jump table encoding!"); 594 return ~0; 595} 596 597/// getJumpTableIndex - Create a new jump table entry in the jump table info 598/// or return an existing one. 599/// 600unsigned MachineJumpTableInfo::getJumpTableIndex( 601 const std::vector<MachineBasicBlock*> &DestBBs) { 602 assert(!DestBBs.empty() && "Cannot create an empty jump table!"); 603 JumpTables.push_back(MachineJumpTableEntry(DestBBs)); 604 return JumpTables.size()-1; 605} 606 607 608/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update 609/// the jump tables to branch to New instead. 610bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, 611 MachineBasicBlock *New) { 612 assert(Old != New && "Not making a change?"); 613 bool MadeChange = false; 614 for (size_t i = 0, e = JumpTables.size(); i != e; ++i) 615 ReplaceMBBInJumpTable(i, Old, New); 616 return MadeChange; 617} 618 619/// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update 620/// the jump table to branch to New instead. 621bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, 622 MachineBasicBlock *Old, 623 MachineBasicBlock *New) { 624 assert(Old != New && "Not making a change?"); 625 bool MadeChange = false; 626 MachineJumpTableEntry &JTE = JumpTables[Idx]; 627 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j) 628 if (JTE.MBBs[j] == Old) { 629 JTE.MBBs[j] = New; 630 MadeChange = true; 631 } 632 return MadeChange; 633} 634 635void MachineJumpTableInfo::print(raw_ostream &OS) const { 636 if (JumpTables.empty()) return; 637 638 OS << "Jump Tables:\n"; 639 640 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { 641 OS << " jt#" << i << ": "; 642 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j) 643 OS << " BB#" << JumpTables[i].MBBs[j]->getNumber(); 644 } 645 646 OS << '\n'; 647} 648 649void MachineJumpTableInfo::dump() const { print(dbgs()); } 650 651 652//===----------------------------------------------------------------------===// 653// MachineConstantPool implementation 654//===----------------------------------------------------------------------===// 655 656const Type *MachineConstantPoolEntry::getType() const { 657 if (isMachineConstantPoolEntry()) 658 return Val.MachineCPVal->getType(); 659 return Val.ConstVal->getType(); 660} 661 662 663unsigned MachineConstantPoolEntry::getRelocationInfo() const { 664 if (isMachineConstantPoolEntry()) 665 return Val.MachineCPVal->getRelocationInfo(); 666 return Val.ConstVal->getRelocationInfo(); 667} 668 669MachineConstantPool::~MachineConstantPool() { 670 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 671 if (Constants[i].isMachineConstantPoolEntry()) 672 delete Constants[i].Val.MachineCPVal; 673} 674 675/// CanShareConstantPoolEntry - Test whether the given two constants 676/// can be allocated the same constant pool entry. 677static bool CanShareConstantPoolEntry(Constant *A, Constant *B, 678 const TargetData *TD) { 679 // Handle the trivial case quickly. 680 if (A == B) return true; 681 682 // If they have the same type but weren't the same constant, quickly 683 // reject them. 684 if (A->getType() == B->getType()) return false; 685 686 // For now, only support constants with the same size. 687 if (TD->getTypeStoreSize(A->getType()) != TD->getTypeStoreSize(B->getType())) 688 return false; 689 690 // If a floating-point value and an integer value have the same encoding, 691 // they can share a constant-pool entry. 692 if (ConstantFP *AFP = dyn_cast<ConstantFP>(A)) 693 if (ConstantInt *BI = dyn_cast<ConstantInt>(B)) 694 return AFP->getValueAPF().bitcastToAPInt() == BI->getValue(); 695 if (ConstantFP *BFP = dyn_cast<ConstantFP>(B)) 696 if (ConstantInt *AI = dyn_cast<ConstantInt>(A)) 697 return BFP->getValueAPF().bitcastToAPInt() == AI->getValue(); 698 699 // Two vectors can share an entry if each pair of corresponding 700 // elements could. 701 if (ConstantVector *AV = dyn_cast<ConstantVector>(A)) 702 if (ConstantVector *BV = dyn_cast<ConstantVector>(B)) { 703 if (AV->getType()->getNumElements() != BV->getType()->getNumElements()) 704 return false; 705 for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i) 706 if (!CanShareConstantPoolEntry(AV->getOperand(i), 707 BV->getOperand(i), TD)) 708 return false; 709 return true; 710 } 711 712 // TODO: Handle other cases. 713 714 return false; 715} 716 717/// getConstantPoolIndex - Create a new entry in the constant pool or return 718/// an existing one. User must specify the log2 of the minimum required 719/// alignment for the object. 720/// 721unsigned MachineConstantPool::getConstantPoolIndex(Constant *C, 722 unsigned Alignment) { 723 assert(Alignment && "Alignment must be specified!"); 724 if (Alignment > PoolAlignment) PoolAlignment = Alignment; 725 726 // Check to see if we already have this constant. 727 // 728 // FIXME, this could be made much more efficient for large constant pools. 729 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 730 if (!Constants[i].isMachineConstantPoolEntry() && 731 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) { 732 if ((unsigned)Constants[i].getAlignment() < Alignment) 733 Constants[i].Alignment = Alignment; 734 return i; 735 } 736 737 Constants.push_back(MachineConstantPoolEntry(C, Alignment)); 738 return Constants.size()-1; 739} 740 741unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, 742 unsigned Alignment) { 743 assert(Alignment && "Alignment must be specified!"); 744 if (Alignment > PoolAlignment) PoolAlignment = Alignment; 745 746 // Check to see if we already have this constant. 747 // 748 // FIXME, this could be made much more efficient for large constant pools. 749 int Idx = V->getExistingMachineCPValue(this, Alignment); 750 if (Idx != -1) 751 return (unsigned)Idx; 752 753 Constants.push_back(MachineConstantPoolEntry(V, Alignment)); 754 return Constants.size()-1; 755} 756 757void MachineConstantPool::print(raw_ostream &OS) const { 758 if (Constants.empty()) return; 759 760 OS << "Constant Pool:\n"; 761 for (unsigned i = 0, e = Constants.size(); i != e; ++i) { 762 OS << " cp#" << i << ": "; 763 if (Constants[i].isMachineConstantPoolEntry()) 764 Constants[i].Val.MachineCPVal->print(OS); 765 else 766 OS << *(Value*)Constants[i].Val.ConstVal; 767 OS << ", align=" << Constants[i].getAlignment(); 768 OS << "\n"; 769 } 770} 771 772void MachineConstantPool::dump() const { print(dbgs()); } 773