2002-10-28 09:16:38 +08:00
|
|
|
//===-- MachineFunction.cpp -----------------------------------------------===//
|
2004-09-06 02:41:35 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-09-06 02:41:35 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-09-06 02:41:35 +08:00
|
|
|
//
|
2002-10-28 09:16:38 +08:00
|
|
|
// Collect native machine code information for a function. This allows
|
|
|
|
// target-specific information about the generated code to be stored with each
|
|
|
|
// function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-02-03 15:54:50 +08:00
|
|
|
|
2012-01-27 09:47:28 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
2007-12-31 12:13:23 +08:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2002-12-29 05:08:26 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2007-12-31 12:13:23 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2006-04-23 02:53:45 +08:00
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.h"
|
2010-04-05 13:49:50 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2007-12-31 12:13:23 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2003-12-20 18:20:58 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-03-06 08:46:21 +08:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2010-01-26 13:58:28 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2010-01-05 07:39:17 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2006-10-04 04:19:23 +08:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2008-08-24 06:23:09 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetFrameLowering.h"
|
|
|
|
#include "llvm/Target/TargetLowering.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2014-08-05 05:25:23 +08:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2003-12-20 17:17:07 +08:00
|
|
|
using namespace llvm;
|
2002-02-03 15:54:50 +08:00
|
|
|
|
2014-04-22 06:55:11 +08:00
|
|
|
#define DEBUG_TYPE "codegen"
|
|
|
|
|
2010-01-26 12:35:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-28 09:12:41 +08:00
|
|
|
// MachineFunction implementation
|
2010-01-26 12:35:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-01-30 02:41:25 +08:00
|
|
|
|
2009-09-16 06:44:26 +08:00
|
|
|
// Out of line virtual method.
|
|
|
|
MachineFunctionInfo::~MachineFunctionInfo() {}
|
|
|
|
|
2008-07-29 05:51:04 +08:00
|
|
|
void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
|
2008-07-08 07:14:23 +08:00
|
|
|
MBB->getParent()->DeleteMachineBasicBlock(MBB);
|
2004-05-24 14:11:51 +08:00
|
|
|
}
|
2002-10-28 09:12:41 +08:00
|
|
|
|
2010-04-15 12:33:49 +08:00
|
|
|
MachineFunction::MachineFunction(const Function *F, const TargetMachine &TM,
|
2014-10-15 02:53:16 +08:00
|
|
|
unsigned FunctionNum, MachineModuleInfo &mmi)
|
2015-03-21 11:13:10 +08:00
|
|
|
: Fn(F), Target(TM), STI(TM.getSubtargetImpl(*F)), Ctx(mmi.getContext()),
|
2014-10-15 02:53:16 +08:00
|
|
|
MMI(mmi) {
|
2014-10-08 15:51:41 +08:00
|
|
|
if (STI->getRegisterInfo())
|
2014-08-12 16:00:56 +08:00
|
|
|
RegInfo = new (Allocator) MachineRegisterInfo(this);
|
2008-10-13 20:37:16 +08:00
|
|
|
else
|
2014-04-14 08:51:57 +08:00
|
|
|
RegInfo = nullptr;
|
2013-06-18 04:41:25 +08:00
|
|
|
|
2014-04-14 08:51:57 +08:00
|
|
|
MFInfo = nullptr;
|
2014-10-08 16:46:34 +08:00
|
|
|
FrameInfo = new (Allocator)
|
|
|
|
MachineFrameInfo(STI->getFrameLowering()->getStackAlignment(),
|
|
|
|
STI->getFrameLowering()->isStackRealignable(),
|
|
|
|
!F->hasFnAttribute("no-realign-stack"));
|
2013-06-18 04:41:25 +08:00
|
|
|
|
2015-02-14 09:44:41 +08:00
|
|
|
if (Fn->hasFnAttribute(Attribute::StackAlignment))
|
|
|
|
FrameInfo->ensureMaxAlignment(Fn->getFnStackAlignment());
|
2013-06-18 04:41:25 +08:00
|
|
|
|
|
|
|
ConstantPool = new (Allocator) MachineConstantPool(TM);
|
2014-10-08 15:51:41 +08:00
|
|
|
Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
|
2013-06-18 04:41:25 +08:00
|
|
|
|
2011-05-07 04:34:06 +08:00
|
|
|
// FIXME: Shouldn't use pref alignment if explicit alignment is set on Fn.
|
2015-02-14 09:44:41 +08:00
|
|
|
if (!Fn->hasFnAttribute(Attribute::OptimizeForSize))
|
2014-10-08 15:51:41 +08:00
|
|
|
Alignment = std::max(Alignment,
|
|
|
|
STI->getTargetLowering()->getPrefFunctionAlignment());
|
2013-06-18 04:41:25 +08:00
|
|
|
|
2010-01-26 12:35:26 +08:00
|
|
|
FunctionNumber = FunctionNum;
|
2014-04-14 08:51:57 +08:00
|
|
|
JumpTableInfo = nullptr;
|
2002-12-25 13:03:22 +08:00
|
|
|
}
|
|
|
|
|
2004-09-06 02:41:35 +08:00
|
|
|
MachineFunction::~MachineFunction() {
|
2013-01-05 13:05:51 +08:00
|
|
|
// Don't call destructors on MachineInstr and MachineOperand. All of their
|
|
|
|
// memory comes from the BumpPtrAllocator which is about to be purged.
|
|
|
|
//
|
|
|
|
// Do call MachineBasicBlock destructors, it contains std::vectors.
|
|
|
|
for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I))
|
|
|
|
I->Insts.clearAndLeakNodesUnsafely();
|
|
|
|
|
2008-07-08 07:14:23 +08:00
|
|
|
InstructionRecycler.clear(Allocator);
|
2013-01-05 13:00:09 +08:00
|
|
|
OperandRecycler.clear(Allocator);
|
2008-07-08 07:14:23 +08:00
|
|
|
BasicBlockRecycler.clear(Allocator);
|
2009-06-25 08:32:48 +08:00
|
|
|
if (RegInfo) {
|
|
|
|
RegInfo->~MachineRegisterInfo();
|
|
|
|
Allocator.Deallocate(RegInfo);
|
|
|
|
}
|
2008-07-08 07:14:23 +08:00
|
|
|
if (MFInfo) {
|
2009-06-25 08:32:48 +08:00
|
|
|
MFInfo->~MachineFunctionInfo();
|
|
|
|
Allocator.Deallocate(MFInfo);
|
2008-07-08 07:14:23 +08:00
|
|
|
}
|
2012-06-20 07:37:57 +08:00
|
|
|
|
|
|
|
FrameInfo->~MachineFrameInfo();
|
|
|
|
Allocator.Deallocate(FrameInfo);
|
|
|
|
|
|
|
|
ConstantPool->~MachineConstantPool();
|
|
|
|
Allocator.Deallocate(ConstantPool);
|
|
|
|
|
2010-01-26 07:26:13 +08:00
|
|
|
if (JumpTableInfo) {
|
|
|
|
JumpTableInfo->~MachineJumpTableInfo();
|
|
|
|
Allocator.Deallocate(JumpTableInfo);
|
|
|
|
}
|
2002-10-30 08:48:05 +08:00
|
|
|
}
|
|
|
|
|
2010-01-26 07:26:13 +08:00
|
|
|
/// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
|
|
|
|
/// does already exist, allocate one.
|
|
|
|
MachineJumpTableInfo *MachineFunction::
|
|
|
|
getOrCreateJumpTableInfo(unsigned EntryKind) {
|
|
|
|
if (JumpTableInfo) return JumpTableInfo;
|
2012-06-20 07:37:57 +08:00
|
|
|
|
2010-03-19 02:49:47 +08:00
|
|
|
JumpTableInfo = new (Allocator)
|
2010-01-26 07:26:13 +08:00
|
|
|
MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
|
|
|
|
return JumpTableInfo;
|
|
|
|
}
|
2006-10-04 03:18:57 +08:00
|
|
|
|
2014-04-11 06:58:43 +08:00
|
|
|
/// Should we be emitting segmented stack stuff for the function
|
|
|
|
bool MachineFunction::shouldSplitStack() {
|
|
|
|
return getFunction()->hasFnAttribute("split-stack");
|
|
|
|
}
|
|
|
|
|
2006-10-04 03:18:57 +08:00
|
|
|
/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
|
|
|
|
/// recomputes them. This guarantees that the MBB numbers are sequential,
|
|
|
|
/// dense, and match the ordering of the blocks within the function. If a
|
|
|
|
/// specific MachineBasicBlock is specified, only that block and those after
|
|
|
|
/// it are renumbered.
|
|
|
|
void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
|
|
|
|
if (empty()) { MBBNumbering.clear(); return; }
|
|
|
|
MachineFunction::iterator MBBI, E = end();
|
2014-04-14 08:51:57 +08:00
|
|
|
if (MBB == nullptr)
|
2006-10-04 03:18:57 +08:00
|
|
|
MBBI = begin();
|
|
|
|
else
|
|
|
|
MBBI = MBB;
|
2012-06-20 07:37:57 +08:00
|
|
|
|
2006-10-04 03:18:57 +08:00
|
|
|
// Figure out the block number this should have.
|
|
|
|
unsigned BlockNo = 0;
|
2006-10-04 04:19:23 +08:00
|
|
|
if (MBBI != begin())
|
2014-03-02 20:27:27 +08:00
|
|
|
BlockNo = std::prev(MBBI)->getNumber() + 1;
|
2012-06-20 07:37:57 +08:00
|
|
|
|
2006-10-04 03:18:57 +08:00
|
|
|
for (; MBBI != E; ++MBBI, ++BlockNo) {
|
|
|
|
if (MBBI->getNumber() != (int)BlockNo) {
|
|
|
|
// Remove use of the old number.
|
|
|
|
if (MBBI->getNumber() != -1) {
|
|
|
|
assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
|
|
|
|
"MBB number mismatch!");
|
2014-04-14 08:51:57 +08:00
|
|
|
MBBNumbering[MBBI->getNumber()] = nullptr;
|
2006-10-04 03:18:57 +08:00
|
|
|
}
|
2012-06-20 07:37:57 +08:00
|
|
|
|
2006-10-04 03:18:57 +08:00
|
|
|
// If BlockNo is already taken, set that block's number to -1.
|
|
|
|
if (MBBNumbering[BlockNo])
|
|
|
|
MBBNumbering[BlockNo]->setNumber(-1);
|
|
|
|
|
|
|
|
MBBNumbering[BlockNo] = MBBI;
|
|
|
|
MBBI->setNumber(BlockNo);
|
|
|
|
}
|
2012-06-20 07:37:57 +08:00
|
|
|
}
|
2006-10-04 03:18:57 +08:00
|
|
|
|
|
|
|
// Okay, all the blocks are renumbered. If we have compactified the block
|
|
|
|
// numbering, shrink MBBNumbering now.
|
|
|
|
assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
|
|
|
|
MBBNumbering.resize(BlockNo);
|
|
|
|
}
|
|
|
|
|
2008-07-08 07:14:23 +08:00
|
|
|
/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
|
|
|
|
/// of `new MachineInstr'.
|
|
|
|
///
|
|
|
|
MachineInstr *
|
2011-06-29 03:10:37 +08:00
|
|
|
MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
|
2009-02-03 08:55:04 +08:00
|
|
|
DebugLoc DL, bool NoImp) {
|
2008-07-08 07:14:23 +08:00
|
|
|
return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
|
2012-12-21 06:53:58 +08:00
|
|
|
MachineInstr(*this, MCID, DL, NoImp);
|
2008-07-08 07:14:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
|
2010-02-11 00:03:48 +08:00
|
|
|
/// 'Orig' instruction, identical in all ways except the instruction
|
2008-07-08 07:14:23 +08:00
|
|
|
/// has no parent, prev, or next.
|
|
|
|
///
|
|
|
|
MachineInstr *
|
|
|
|
MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
|
|
|
|
return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
|
|
|
|
MachineInstr(*this, *Orig);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// DeleteMachineInstr - Delete the given MachineInstr.
|
|
|
|
///
|
2013-01-05 13:05:51 +08:00
|
|
|
/// This function also serves as the MachineInstr destructor - the real
|
|
|
|
/// ~MachineInstr() destructor must be empty.
|
2008-07-08 07:14:23 +08:00
|
|
|
void
|
|
|
|
MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
|
2013-01-05 13:00:09 +08:00
|
|
|
// Strip it for parts. The operand array and the MI object itself are
|
|
|
|
// independently recyclable.
|
|
|
|
if (MI->Operands)
|
|
|
|
deallocateOperandArray(MI->CapOperands, MI->Operands);
|
2013-01-05 13:05:51 +08:00
|
|
|
// Don't call ~MachineInstr() which must be trivial anyway because
|
|
|
|
// ~MachineFunction drops whole lists of MachineInstrs wihout calling their
|
|
|
|
// destructors.
|
2008-07-08 07:14:23 +08:00
|
|
|
InstructionRecycler.Deallocate(Allocator, MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
|
|
|
|
/// instead of `new MachineBasicBlock'.
|
|
|
|
///
|
|
|
|
MachineBasicBlock *
|
|
|
|
MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
|
|
|
|
return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
|
|
|
|
MachineBasicBlock(*this, bb);
|
|
|
|
}
|
2006-10-04 03:18:57 +08:00
|
|
|
|
2008-07-08 07:14:23 +08:00
|
|
|
/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
|
|
|
|
///
|
|
|
|
void
|
|
|
|
MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
|
|
|
|
assert(MBB->getParent() == this && "MBB parent mismatch!");
|
|
|
|
MBB->~MachineBasicBlock();
|
|
|
|
BasicBlockRecycler.Deallocate(Allocator, MBB);
|
|
|
|
}
|
|
|
|
|
2010-09-21 12:32:08 +08:00
|
|
|
MachineMemOperand *
|
|
|
|
MachineFunction::getMachineMemOperand(MachinePointerInfo PtrInfo, unsigned f,
|
2010-10-20 08:31:05 +08:00
|
|
|
uint64_t s, unsigned base_alignment,
|
2014-07-24 20:16:19 +08:00
|
|
|
const AAMDNodes &AAInfo,
|
2012-04-01 02:14:00 +08:00
|
|
|
const MDNode *Ranges) {
|
2010-10-20 08:31:05 +08:00
|
|
|
return new (Allocator) MachineMemOperand(PtrInfo, f, s, base_alignment,
|
2014-07-24 20:16:19 +08:00
|
|
|
AAInfo, Ranges);
|
2009-09-26 04:36:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MachineMemOperand *
|
|
|
|
MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
|
|
|
|
int64_t Offset, uint64_t Size) {
|
2014-04-15 15:22:52 +08:00
|
|
|
if (MMO->getValue())
|
|
|
|
return new (Allocator)
|
|
|
|
MachineMemOperand(MachinePointerInfo(MMO->getValue(),
|
|
|
|
MMO->getOffset()+Offset),
|
|
|
|
MMO->getFlags(), Size,
|
2014-10-05 06:44:29 +08:00
|
|
|
MMO->getBaseAlignment());
|
2010-03-19 02:49:47 +08:00
|
|
|
return new (Allocator)
|
2014-04-15 15:22:52 +08:00
|
|
|
MachineMemOperand(MachinePointerInfo(MMO->getPseudoValue(),
|
2010-09-21 12:32:08 +08:00
|
|
|
MMO->getOffset()+Offset),
|
2010-10-20 08:31:05 +08:00
|
|
|
MMO->getFlags(), Size,
|
2014-10-05 06:44:29 +08:00
|
|
|
MMO->getBaseAlignment());
|
2009-09-26 04:36:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr::mmo_iterator
|
|
|
|
MachineFunction::allocateMemRefsArray(unsigned long Num) {
|
|
|
|
return Allocator.Allocate<MachineMemOperand *>(Num);
|
|
|
|
}
|
|
|
|
|
2009-10-10 02:10:05 +08:00
|
|
|
std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
|
|
|
|
MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
|
|
|
|
MachineInstr::mmo_iterator End) {
|
|
|
|
// Count the number of load mem refs.
|
|
|
|
unsigned Num = 0;
|
|
|
|
for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
|
|
|
|
if ((*I)->isLoad())
|
|
|
|
++Num;
|
|
|
|
|
|
|
|
// Allocate a new array and populate it with the load information.
|
|
|
|
MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
|
|
|
|
unsigned Index = 0;
|
|
|
|
for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
|
|
|
|
if ((*I)->isLoad()) {
|
|
|
|
if (!(*I)->isStore())
|
|
|
|
// Reuse the MMO.
|
|
|
|
Result[Index] = *I;
|
|
|
|
else {
|
|
|
|
// Clone the MMO and unset the store flag.
|
|
|
|
MachineMemOperand *JustLoad =
|
2010-09-21 12:46:39 +08:00
|
|
|
getMachineMemOperand((*I)->getPointerInfo(),
|
2009-10-10 02:10:05 +08:00
|
|
|
(*I)->getFlags() & ~MachineMemOperand::MOStore,
|
2010-10-20 08:31:05 +08:00
|
|
|
(*I)->getSize(), (*I)->getBaseAlignment(),
|
2014-07-24 20:16:19 +08:00
|
|
|
(*I)->getAAInfo());
|
2009-10-10 02:10:05 +08:00
|
|
|
Result[Index] = JustLoad;
|
|
|
|
}
|
|
|
|
++Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::make_pair(Result, Result + Num);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
|
|
|
|
MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
|
|
|
|
MachineInstr::mmo_iterator End) {
|
|
|
|
// Count the number of load mem refs.
|
|
|
|
unsigned Num = 0;
|
|
|
|
for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
|
|
|
|
if ((*I)->isStore())
|
|
|
|
++Num;
|
|
|
|
|
|
|
|
// Allocate a new array and populate it with the store information.
|
|
|
|
MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
|
|
|
|
unsigned Index = 0;
|
|
|
|
for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
|
|
|
|
if ((*I)->isStore()) {
|
|
|
|
if (!(*I)->isLoad())
|
|
|
|
// Reuse the MMO.
|
|
|
|
Result[Index] = *I;
|
|
|
|
else {
|
|
|
|
// Clone the MMO and unset the load flag.
|
|
|
|
MachineMemOperand *JustStore =
|
2010-09-21 12:46:39 +08:00
|
|
|
getMachineMemOperand((*I)->getPointerInfo(),
|
2009-10-10 02:10:05 +08:00
|
|
|
(*I)->getFlags() & ~MachineMemOperand::MOLoad,
|
2010-10-20 08:31:05 +08:00
|
|
|
(*I)->getSize(), (*I)->getBaseAlignment(),
|
2014-07-24 20:16:19 +08:00
|
|
|
(*I)->getAAInfo());
|
2009-10-10 02:10:05 +08:00
|
|
|
Result[Index] = JustStore;
|
|
|
|
}
|
|
|
|
++Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::make_pair(Result, Result + Num);
|
|
|
|
}
|
|
|
|
|
2012-09-12 06:23:19 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2008-07-08 07:14:23 +08:00
|
|
|
void MachineFunction::dump() const {
|
2010-01-05 07:39:17 +08:00
|
|
|
print(dbgs());
|
2008-07-08 07:14:23 +08:00
|
|
|
}
|
2012-09-07 03:06:06 +08:00
|
|
|
#endif
|
2002-10-30 08:48:05 +08:00
|
|
|
|
2012-08-22 14:07:19 +08:00
|
|
|
StringRef MachineFunction::getName() const {
|
|
|
|
assert(getFunction() && "No function!");
|
|
|
|
return getFunction()->getName();
|
|
|
|
}
|
|
|
|
|
2010-10-27 04:21:46 +08:00
|
|
|
void MachineFunction::print(raw_ostream &OS, SlotIndexes *Indexes) const {
|
2012-08-23 01:18:53 +08:00
|
|
|
OS << "# Machine code for function " << getName() << ": ";
|
2012-03-28 01:17:16 +08:00
|
|
|
if (RegInfo) {
|
|
|
|
OS << (RegInfo->isSSA() ? "SSA" : "Post SSA");
|
|
|
|
if (!RegInfo->tracksLiveness())
|
|
|
|
OS << ", not tracking liveness";
|
|
|
|
}
|
|
|
|
OS << '\n';
|
2002-12-29 04:37:16 +08:00
|
|
|
|
|
|
|
// Print Frame Information
|
2008-07-08 07:14:23 +08:00
|
|
|
FrameInfo->print(*this, OS);
|
2012-06-20 07:37:57 +08:00
|
|
|
|
2006-04-23 02:53:45 +08:00
|
|
|
// Print JumpTable Information
|
2010-01-26 07:26:13 +08:00
|
|
|
if (JumpTableInfo)
|
|
|
|
JumpTableInfo->print(OS);
|
2003-01-13 08:23:03 +08:00
|
|
|
|
|
|
|
// Print Constant Pool
|
2009-08-23 09:12:47 +08:00
|
|
|
ConstantPool->print(OS);
|
2012-06-20 07:37:57 +08:00
|
|
|
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
|
2012-06-20 07:37:57 +08:00
|
|
|
|
2008-10-13 20:37:16 +08:00
|
|
|
if (RegInfo && !RegInfo->livein_empty()) {
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << "Function Live Ins: ";
|
2007-12-31 12:13:23 +08:00
|
|
|
for (MachineRegisterInfo::livein_iterator
|
|
|
|
I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
|
2011-05-03 04:06:30 +08:00
|
|
|
OS << PrintReg(I->first, TRI);
|
2006-05-16 13:55:30 +08:00
|
|
|
if (I->second)
|
2011-05-03 04:06:30 +08:00
|
|
|
OS << " in " << PrintReg(I->second, TRI);
|
2014-03-02 20:27:27 +08:00
|
|
|
if (std::next(I) != E)
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << ", ";
|
2005-09-01 06:34:59 +08:00
|
|
|
}
|
2009-08-23 09:12:47 +08:00
|
|
|
OS << '\n';
|
2005-09-01 06:34:59 +08:00
|
|
|
}
|
2012-06-20 07:37:57 +08:00
|
|
|
|
2014-05-01 02:29:51 +08:00
|
|
|
for (const auto &BB : *this) {
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << '\n';
|
2014-05-01 02:29:51 +08:00
|
|
|
BB.print(OS, Indexes);
|
2009-11-01 04:19:03 +08:00
|
|
|
}
|
2004-03-30 05:58:31 +08:00
|
|
|
|
2012-08-23 01:18:53 +08:00
|
|
|
OS << "\n# End machine code for function " << getName() << ".\n\n";
|
2002-10-30 08:48:05 +08:00
|
|
|
}
|
|
|
|
|
2004-07-08 08:47:58 +08:00
|
|
|
namespace llvm {
|
2004-09-06 02:41:35 +08:00
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
|
2009-11-30 20:38:13 +08:00
|
|
|
|
|
|
|
DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
|
|
|
|
|
2004-09-06 02:41:35 +08:00
|
|
|
static std::string getGraphName(const MachineFunction *F) {
|
2015-03-28 01:51:30 +08:00
|
|
|
return ("CFG for '" + F->getName() + "' function").str();
|
2004-09-06 02:41:35 +08:00
|
|
|
}
|
2004-07-08 08:47:58 +08:00
|
|
|
|
2009-11-30 20:38:47 +08:00
|
|
|
std::string getNodeLabel(const MachineBasicBlock *Node,
|
|
|
|
const MachineFunction *Graph) {
|
2009-08-23 11:13:20 +08:00
|
|
|
std::string OutStr;
|
|
|
|
{
|
|
|
|
raw_string_ostream OSS(OutStr);
|
2010-10-30 09:26:19 +08:00
|
|
|
|
|
|
|
if (isSimple()) {
|
|
|
|
OSS << "BB#" << Node->getNumber();
|
|
|
|
if (const BasicBlock *BB = Node->getBasicBlock())
|
|
|
|
OSS << ": " << BB->getName();
|
|
|
|
} else
|
2009-08-23 11:13:20 +08:00
|
|
|
Node->print(OSS);
|
2004-09-06 02:41:35 +08:00
|
|
|
}
|
2004-07-08 08:47:58 +08:00
|
|
|
|
2004-09-06 02:41:35 +08:00
|
|
|
if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
|
2004-07-08 08:47:58 +08:00
|
|
|
|
2004-09-06 02:41:35 +08:00
|
|
|
// Process string output to make it nicer...
|
|
|
|
for (unsigned i = 0; i != OutStr.length(); ++i)
|
|
|
|
if (OutStr[i] == '\n') { // Left justify
|
|
|
|
OutStr[i] = '\\';
|
|
|
|
OutStr.insert(OutStr.begin()+i+1, 'l');
|
|
|
|
}
|
|
|
|
return OutStr;
|
|
|
|
}
|
|
|
|
};
|
2004-07-08 08:47:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachineFunction::viewCFG() const
|
|
|
|
{
|
2005-10-12 20:09:05 +08:00
|
|
|
#ifndef NDEBUG
|
2012-08-23 01:18:53 +08:00
|
|
|
ViewGraph(this, "mf" + getName());
|
2006-06-28 00:49:46 +08:00
|
|
|
#else
|
2010-07-08 01:28:45 +08:00
|
|
|
errs() << "MachineFunction::viewCFG is only available in debug builds on "
|
2009-08-23 16:50:52 +08:00
|
|
|
<< "systems with Graphviz or gv!\n";
|
2006-06-28 00:49:46 +08:00
|
|
|
#endif // NDEBUG
|
2004-07-08 08:47:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachineFunction::viewCFGOnly() const
|
|
|
|
{
|
2009-06-25 01:37:09 +08:00
|
|
|
#ifndef NDEBUG
|
2012-08-23 01:18:53 +08:00
|
|
|
ViewGraph(this, "mf" + getName(), true);
|
2009-06-25 01:37:09 +08:00
|
|
|
#else
|
2010-07-08 01:28:45 +08:00
|
|
|
errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
|
2009-08-23 16:50:52 +08:00
|
|
|
<< "systems with Graphviz or gv!\n";
|
2009-06-25 01:37:09 +08:00
|
|
|
#endif // NDEBUG
|
2004-07-08 08:47:58 +08:00
|
|
|
}
|
|
|
|
|
2009-04-21 02:36:57 +08:00
|
|
|
/// addLiveIn - Add the specified physical register as a live-in value and
|
|
|
|
/// create a corresponding virtual register for it.
|
|
|
|
unsigned MachineFunction::addLiveIn(unsigned PReg,
|
2011-02-22 07:21:26 +08:00
|
|
|
const TargetRegisterClass *RC) {
|
2010-05-25 05:33:37 +08:00
|
|
|
MachineRegisterInfo &MRI = getRegInfo();
|
|
|
|
unsigned VReg = MRI.getLiveInVirtReg(PReg);
|
|
|
|
if (VReg) {
|
2013-12-12 08:15:47 +08:00
|
|
|
const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
|
|
|
|
(void)VRegRC;
|
|
|
|
// A physical register can be added several times.
|
|
|
|
// Between two calls, the register class of the related virtual register
|
|
|
|
// may have been constrained to match some operation constraints.
|
|
|
|
// In that case, check that the current register class includes the
|
|
|
|
// physical register and is a sub class of the specified RC.
|
|
|
|
assert((VRegRC == RC || (VRegRC->contains(PReg) &&
|
|
|
|
RC->hasSubClassEq(VRegRC))) &&
|
|
|
|
"Register class mismatch!");
|
2010-05-25 05:33:37 +08:00
|
|
|
return VReg;
|
|
|
|
}
|
|
|
|
VReg = MRI.createVirtualRegister(RC);
|
|
|
|
MRI.addLiveIn(PReg, VReg);
|
2009-04-21 02:36:57 +08:00
|
|
|
return VReg;
|
|
|
|
}
|
|
|
|
|
2010-01-26 14:28:43 +08:00
|
|
|
/// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
|
2010-06-30 06:34:52 +08:00
|
|
|
/// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
|
|
|
|
/// normal 'L' label is returned.
|
2014-06-25 20:40:56 +08:00
|
|
|
MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
|
2010-06-30 06:34:52 +08:00
|
|
|
bool isLinkerPrivate) const {
|
2015-01-27 03:03:15 +08:00
|
|
|
const DataLayout *DL = getTarget().getDataLayout();
|
2010-01-26 14:28:43 +08:00
|
|
|
assert(JumpTableInfo && "No jump tables");
|
2010-01-27 18:27:10 +08:00
|
|
|
assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
|
2012-06-20 07:37:57 +08:00
|
|
|
|
2014-01-04 03:21:54 +08:00
|
|
|
const char *Prefix = isLinkerPrivate ? DL->getLinkerPrivateGlobalPrefix() :
|
|
|
|
DL->getPrivateGlobalPrefix();
|
2014-06-27 06:52:05 +08:00
|
|
|
SmallString<60> Name;
|
|
|
|
raw_svector_ostream(Name)
|
|
|
|
<< Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
|
2015-05-19 02:43:14 +08:00
|
|
|
return Ctx.getOrCreateSymbol(Name);
|
2010-01-26 14:28:43 +08:00
|
|
|
}
|
|
|
|
|
2010-11-15 06:48:15 +08:00
|
|
|
/// getPICBaseSymbol - Return a function-local symbol to represent the PIC
|
|
|
|
/// base.
|
|
|
|
MCSymbol *MachineFunction::getPICBaseSymbol() const {
|
2015-01-27 03:03:15 +08:00
|
|
|
const DataLayout *DL = getTarget().getDataLayout();
|
2015-05-19 02:43:14 +08:00
|
|
|
return Ctx.getOrCreateSymbol(Twine(DL->getPrivateGlobalPrefix())+
|
2010-11-15 06:48:15 +08:00
|
|
|
Twine(getFunctionNumber())+"$pb");
|
|
|
|
}
|
2010-01-26 14:28:43 +08:00
|
|
|
|
2002-12-29 04:37:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-29 05:08:26 +08:00
|
|
|
// MachineFrameInfo implementation
|
2002-12-29 04:37:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 08:26:44 +08:00
|
|
|
/// ensureMaxAlignment - Make sure the function is at least Align bytes
|
|
|
|
/// aligned.
|
|
|
|
void MachineFrameInfo::ensureMaxAlignment(unsigned Align) {
|
2014-10-08 16:46:34 +08:00
|
|
|
if (!StackRealignable || !RealignOption)
|
|
|
|
assert(Align <= StackAlignment &&
|
2012-12-04 08:52:33 +08:00
|
|
|
"For targets without stack realignment, Align is out of limit!");
|
2012-12-04 08:26:44 +08:00
|
|
|
if (MaxAlignment < Align) MaxAlignment = Align;
|
|
|
|
}
|
|
|
|
|
2012-12-04 08:52:33 +08:00
|
|
|
/// clampStackAlignment - Clamp the alignment if requested and emit a warning.
|
2013-02-09 04:35:15 +08:00
|
|
|
static inline unsigned clampStackAlignment(bool ShouldClamp, unsigned Align,
|
|
|
|
unsigned StackAlign) {
|
|
|
|
if (!ShouldClamp || Align <= StackAlign)
|
|
|
|
return Align;
|
|
|
|
DEBUG(dbgs() << "Warning: requested alignment " << Align
|
|
|
|
<< " exceeds the stack alignment " << StackAlign
|
|
|
|
<< " when stack realignment is off" << '\n');
|
2012-12-04 08:52:33 +08:00
|
|
|
return StackAlign;
|
|
|
|
}
|
|
|
|
|
2013-02-09 04:35:15 +08:00
|
|
|
/// CreateStackObject - Create a new statically sized stack object, returning
|
|
|
|
/// a nonnegative identifier to represent it.
|
2012-12-04 08:26:44 +08:00
|
|
|
///
|
2013-02-09 04:35:15 +08:00
|
|
|
int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment,
|
2013-12-19 11:17:11 +08:00
|
|
|
bool isSS, const AllocaInst *Alloca) {
|
2012-12-04 08:26:44 +08:00
|
|
|
assert(Size != 0 && "Cannot allocate zero size stack objects!");
|
2014-10-08 16:46:34 +08:00
|
|
|
Alignment = clampStackAlignment(!StackRealignable || !RealignOption,
|
|
|
|
Alignment, StackAlignment);
|
2014-08-16 08:17:02 +08:00
|
|
|
Objects.push_back(StackObject(Size, Alignment, 0, false, isSS, Alloca,
|
|
|
|
!isSS));
|
2012-12-04 08:26:44 +08:00
|
|
|
int Index = (int)Objects.size() - NumFixedObjects - 1;
|
|
|
|
assert(Index >= 0 && "Bad frame index!");
|
|
|
|
ensureMaxAlignment(Alignment);
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CreateSpillStackObject - Create a new statically sized stack object that
|
|
|
|
/// represents a spill slot, returning a nonnegative identifier to represent
|
|
|
|
/// it.
|
|
|
|
///
|
|
|
|
int MachineFrameInfo::CreateSpillStackObject(uint64_t Size,
|
|
|
|
unsigned Alignment) {
|
2014-10-08 16:46:34 +08:00
|
|
|
Alignment = clampStackAlignment(!StackRealignable || !RealignOption,
|
|
|
|
Alignment, StackAlignment);
|
2013-12-19 11:17:11 +08:00
|
|
|
CreateStackObject(Size, Alignment, true);
|
2012-12-04 08:26:44 +08:00
|
|
|
int Index = (int)Objects.size() - NumFixedObjects - 1;
|
|
|
|
ensureMaxAlignment(Alignment);
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CreateVariableSizedObject - Notify the MachineFrameInfo object that a
|
|
|
|
/// variable sized object has been created. This must be created whenever a
|
|
|
|
/// variable sized object is created, whether or not the index returned is
|
|
|
|
/// actually used.
|
|
|
|
///
|
2013-12-19 11:17:11 +08:00
|
|
|
int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment,
|
|
|
|
const AllocaInst *Alloca) {
|
2012-12-04 08:26:44 +08:00
|
|
|
HasVarSizedObjects = true;
|
2014-10-08 16:46:34 +08:00
|
|
|
Alignment = clampStackAlignment(!StackRealignable || !RealignOption,
|
|
|
|
Alignment, StackAlignment);
|
2014-08-16 08:17:02 +08:00
|
|
|
Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true));
|
2012-12-04 08:26:44 +08:00
|
|
|
ensureMaxAlignment(Alignment);
|
|
|
|
return (int)Objects.size()-NumFixedObjects-1;
|
|
|
|
}
|
|
|
|
|
2008-01-25 15:19:06 +08:00
|
|
|
/// CreateFixedObject - Create a new object at a fixed location on the stack.
|
|
|
|
/// All fixed objects should be created before other objects are created for
|
|
|
|
/// efficiency. By default, fixed objects are immutable. This returns an
|
|
|
|
/// index with a negative value.
|
|
|
|
///
|
|
|
|
int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
|
2014-08-16 08:17:02 +08:00
|
|
|
bool Immutable, bool isAliased) {
|
2008-01-25 15:19:06 +08:00
|
|
|
assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
|
2010-07-05 02:52:05 +08:00
|
|
|
// The alignment of the frame index can be determined from its offset from
|
|
|
|
// the incoming frame position. If the frame object is at offset 32 and
|
|
|
|
// the stack is guaranteed to be 16-byte aligned, then we know that the
|
|
|
|
// object is 16-byte aligned.
|
2014-10-08 16:46:34 +08:00
|
|
|
unsigned Align = MinAlign(SPOffset, StackAlignment);
|
|
|
|
Align = clampStackAlignment(!StackRealignable || !RealignOption, Align,
|
|
|
|
StackAlignment);
|
2010-07-05 02:52:05 +08:00
|
|
|
Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable,
|
2012-09-06 17:17:37 +08:00
|
|
|
/*isSS*/ false,
|
2014-08-16 08:17:02 +08:00
|
|
|
/*Alloca*/ nullptr, isAliased));
|
2008-01-25 15:19:06 +08:00
|
|
|
return -++NumFixedObjects;
|
|
|
|
}
|
|
|
|
|
2014-06-25 20:41:52 +08:00
|
|
|
/// CreateFixedSpillStackObject - Create a spill slot at a fixed location
|
|
|
|
/// on the stack. Returns an index with a negative value.
|
|
|
|
int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size,
|
|
|
|
int64_t SPOffset) {
|
2014-10-08 16:46:34 +08:00
|
|
|
unsigned Align = MinAlign(SPOffset, StackAlignment);
|
|
|
|
Align = clampStackAlignment(!StackRealignable || !RealignOption, Align,
|
|
|
|
StackAlignment);
|
2014-06-25 20:41:52 +08:00
|
|
|
Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset,
|
|
|
|
/*Immutable*/ true,
|
|
|
|
/*isSS*/ true,
|
2014-08-16 08:17:02 +08:00
|
|
|
/*Alloca*/ nullptr,
|
|
|
|
/*isAliased*/ false));
|
2014-06-25 20:41:52 +08:00
|
|
|
return -++NumFixedObjects;
|
|
|
|
}
|
2008-01-25 15:19:06 +08:00
|
|
|
|
2009-08-14 00:19:33 +08:00
|
|
|
BitVector
|
|
|
|
MachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const {
|
|
|
|
assert(MBB && "MBB must be valid");
|
|
|
|
const MachineFunction *MF = MBB->getParent();
|
|
|
|
assert(MF && "MBB must be part of a MachineFunction");
|
2015-01-27 16:48:42 +08:00
|
|
|
const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
|
2009-08-14 00:19:33 +08:00
|
|
|
BitVector BV(TRI->getNumRegs());
|
|
|
|
|
|
|
|
// Before CSI is calculated, no registers are considered pristine. They can be
|
|
|
|
// freely used and PEI will make sure they are saved.
|
|
|
|
if (!isCalleeSavedInfoValid())
|
|
|
|
return BV;
|
|
|
|
|
2014-04-04 13:16:06 +08:00
|
|
|
for (const MCPhysReg *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR)
|
2009-08-14 00:19:33 +08:00
|
|
|
BV.set(*CSR);
|
|
|
|
|
[ShrinkWrap] Add (a simplified version) of shrink-wrapping.
This patch introduces a new pass that computes the safe point to insert the
prologue and epilogue of the function.
The interest is to find safe points that are cheaper than the entry and exits
blocks.
As an example and to avoid regressions to be introduce, this patch also
implements the required bits to enable the shrink-wrapping pass for AArch64.
** Context **
Currently we insert the prologue and epilogue of the method/function in the
entry and exits blocks. Although this is correct, we can do a better job when
those are not immediately required and insert them at less frequently executed
places.
The job of the shrink-wrapping pass is to identify such places.
** Motivating example **
Let us consider the following function that perform a call only in one branch of
a if:
define i32 @f(i32 %a, i32 %b) {
%tmp = alloca i32, align 4
%tmp2 = icmp slt i32 %a, %b
br i1 %tmp2, label %true, label %false
true:
store i32 %a, i32* %tmp, align 4
%tmp4 = call i32 @doSomething(i32 0, i32* %tmp)
br label %false
false:
%tmp.0 = phi i32 [ %tmp4, %true ], [ %a, %0 ]
ret i32 %tmp.0
}
On AArch64 this code generates (removing the cfi directives to ease
readabilities):
_f: ; @f
; BB#0:
stp x29, x30, [sp, #-16]!
mov x29, sp
sub sp, sp, #16 ; =16
cmp w0, w1
b.ge LBB0_2
; BB#1: ; %true
stur w0, [x29, #-4]
sub x1, x29, #4 ; =4
mov w0, wzr
bl _doSomething
LBB0_2: ; %false
mov sp, x29
ldp x29, x30, [sp], #16
ret
With shrink-wrapping we could generate:
_f: ; @f
; BB#0:
cmp w0, w1
b.ge LBB0_2
; BB#1: ; %true
stp x29, x30, [sp, #-16]!
mov x29, sp
sub sp, sp, #16 ; =16
stur w0, [x29, #-4]
sub x1, x29, #4 ; =4
mov w0, wzr
bl _doSomething
add sp, x29, #16 ; =16
ldp x29, x30, [sp], #16
LBB0_2: ; %false
ret
Therefore, we would pay the overhead of setting up/destroying the frame only if
we actually do the call.
** Proposed Solution **
This patch introduces a new machine pass that perform the shrink-wrapping
analysis (See the comments at the beginning of ShrinkWrap.cpp for more details).
It then stores the safe save and restore point into the MachineFrameInfo
attached to the MachineFunction.
This information is then used by the PrologEpilogInserter (PEI) to place the
related code at the right place. This pass runs right before the PEI.
Unlike the original paper of Chow from PLDI’88, this implementation of
shrink-wrapping does not use expensive data-flow analysis and does not need hack
to properly avoid frequently executed point. Instead, it relies on dominance and
loop properties.
The pass is off by default and each target can opt-in by setting the
EnableShrinkWrap boolean to true in their derived class of TargetPassConfig.
This setting can also be overwritten on the command line by using
-enable-shrink-wrap.
Before you try out the pass for your target, make sure you properly fix your
emitProlog/emitEpilog/adjustForXXX method to cope with basic blocks that are not
necessarily the entry block.
** Design Decisions **
1. ShrinkWrap is its own pass right now. It could frankly be merged into PEI but
for debugging and clarity I thought it was best to have its own file.
2. Right now, we only support one save point and one restore point. At some
point we can expand this to several save point and restore point, the impacted
component would then be:
- The pass itself: New algorithm needed.
- MachineFrameInfo: Hold a list or set of Save/Restore point instead of one
pointer.
- PEI: Should loop over the save point and restore point.
Anyhow, at least for this first iteration, I do not believe this is interesting
to support the complex cases. We should revisit that when we motivating
examples.
Differential Revision: http://reviews.llvm.org/D9210
<rdar://problem/3201744>
llvm-svn: 236507
2015-05-06 01:38:16 +08:00
|
|
|
// Each MBB before the save point has all CSRs pristine.
|
|
|
|
if (isBeforeSavePoint(*MF, *MBB))
|
2009-08-14 00:19:33 +08:00
|
|
|
return BV;
|
|
|
|
|
|
|
|
// On other MBBs the saved CSRs are not pristine.
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo();
|
|
|
|
for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
|
|
|
|
E = CSI.end(); I != E; ++I)
|
|
|
|
BV.reset(I->getReg());
|
|
|
|
|
|
|
|
return BV;
|
|
|
|
}
|
|
|
|
|
[ShrinkWrap] Add (a simplified version) of shrink-wrapping.
This patch introduces a new pass that computes the safe point to insert the
prologue and epilogue of the function.
The interest is to find safe points that are cheaper than the entry and exits
blocks.
As an example and to avoid regressions to be introduce, this patch also
implements the required bits to enable the shrink-wrapping pass for AArch64.
** Context **
Currently we insert the prologue and epilogue of the method/function in the
entry and exits blocks. Although this is correct, we can do a better job when
those are not immediately required and insert them at less frequently executed
places.
The job of the shrink-wrapping pass is to identify such places.
** Motivating example **
Let us consider the following function that perform a call only in one branch of
a if:
define i32 @f(i32 %a, i32 %b) {
%tmp = alloca i32, align 4
%tmp2 = icmp slt i32 %a, %b
br i1 %tmp2, label %true, label %false
true:
store i32 %a, i32* %tmp, align 4
%tmp4 = call i32 @doSomething(i32 0, i32* %tmp)
br label %false
false:
%tmp.0 = phi i32 [ %tmp4, %true ], [ %a, %0 ]
ret i32 %tmp.0
}
On AArch64 this code generates (removing the cfi directives to ease
readabilities):
_f: ; @f
; BB#0:
stp x29, x30, [sp, #-16]!
mov x29, sp
sub sp, sp, #16 ; =16
cmp w0, w1
b.ge LBB0_2
; BB#1: ; %true
stur w0, [x29, #-4]
sub x1, x29, #4 ; =4
mov w0, wzr
bl _doSomething
LBB0_2: ; %false
mov sp, x29
ldp x29, x30, [sp], #16
ret
With shrink-wrapping we could generate:
_f: ; @f
; BB#0:
cmp w0, w1
b.ge LBB0_2
; BB#1: ; %true
stp x29, x30, [sp, #-16]!
mov x29, sp
sub sp, sp, #16 ; =16
stur w0, [x29, #-4]
sub x1, x29, #4 ; =4
mov w0, wzr
bl _doSomething
add sp, x29, #16 ; =16
ldp x29, x30, [sp], #16
LBB0_2: ; %false
ret
Therefore, we would pay the overhead of setting up/destroying the frame only if
we actually do the call.
** Proposed Solution **
This patch introduces a new machine pass that perform the shrink-wrapping
analysis (See the comments at the beginning of ShrinkWrap.cpp for more details).
It then stores the safe save and restore point into the MachineFrameInfo
attached to the MachineFunction.
This information is then used by the PrologEpilogInserter (PEI) to place the
related code at the right place. This pass runs right before the PEI.
Unlike the original paper of Chow from PLDI’88, this implementation of
shrink-wrapping does not use expensive data-flow analysis and does not need hack
to properly avoid frequently executed point. Instead, it relies on dominance and
loop properties.
The pass is off by default and each target can opt-in by setting the
EnableShrinkWrap boolean to true in their derived class of TargetPassConfig.
This setting can also be overwritten on the command line by using
-enable-shrink-wrap.
Before you try out the pass for your target, make sure you properly fix your
emitProlog/emitEpilog/adjustForXXX method to cope with basic blocks that are not
necessarily the entry block.
** Design Decisions **
1. ShrinkWrap is its own pass right now. It could frankly be merged into PEI but
for debugging and clarity I thought it was best to have its own file.
2. Right now, we only support one save point and one restore point. At some
point we can expand this to several save point and restore point, the impacted
component would then be:
- The pass itself: New algorithm needed.
- MachineFrameInfo: Hold a list or set of Save/Restore point instead of one
pointer.
- PEI: Should loop over the save point and restore point.
Anyhow, at least for this first iteration, I do not believe this is interesting
to support the complex cases. We should revisit that when we motivating
examples.
Differential Revision: http://reviews.llvm.org/D9210
<rdar://problem/3201744>
llvm-svn: 236507
2015-05-06 01:38:16 +08:00
|
|
|
// Note: We could use some sort of caching mecanism, but we lack the ability
|
|
|
|
// to know when the cache is invalid, i.e., the CFG changed.
|
|
|
|
// Assuming we have that, we can simply compute all the set of MBBs
|
|
|
|
// that are before the save point.
|
|
|
|
bool MachineFrameInfo::isBeforeSavePoint(const MachineFunction &MF,
|
|
|
|
const MachineBasicBlock &MBB) const {
|
|
|
|
// Early exit if shrink-wrapping did not kick.
|
|
|
|
if (!Save)
|
|
|
|
return &MBB == &MF.front();
|
|
|
|
|
|
|
|
// Starting from MBB, check if there is a path leading to Save that do
|
|
|
|
// not cross Restore.
|
|
|
|
SmallPtrSet<const MachineBasicBlock *, 8> Visited;
|
|
|
|
SmallVector<const MachineBasicBlock *, 8> WorkList;
|
|
|
|
WorkList.push_back(&MBB);
|
|
|
|
Visited.insert(&MBB);
|
|
|
|
do {
|
|
|
|
const MachineBasicBlock *CurBB = WorkList.pop_back_val();
|
|
|
|
// By construction, the region that is after the save point is
|
|
|
|
// dominated by the Save and post-dominated by the Restore.
|
|
|
|
// If we do not reach Restore and still reach Save, this
|
|
|
|
// means MBB is before Save.
|
|
|
|
if (CurBB == Save)
|
|
|
|
return true;
|
|
|
|
if (CurBB == Restore)
|
|
|
|
continue;
|
|
|
|
// Enqueue all the successors not already visited.
|
|
|
|
for (MachineBasicBlock *SuccBB : CurBB->successors())
|
|
|
|
if (Visited.insert(SuccBB).second)
|
|
|
|
WorkList.push_back(SuccBB);
|
|
|
|
} while (!WorkList.empty());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-15 05:15:20 +08:00
|
|
|
unsigned MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
|
|
|
|
const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
|
2013-03-15 05:15:20 +08:00
|
|
|
unsigned MaxAlign = getMaxAlignment();
|
|
|
|
int Offset = 0;
|
|
|
|
|
|
|
|
// This code is very, very similar to PEI::calculateFrameObjectOffsets().
|
|
|
|
// It really should be refactored to share code. Until then, changes
|
|
|
|
// should keep in mind that there's tight coupling between the two.
|
|
|
|
|
|
|
|
for (int i = getObjectIndexBegin(); i != 0; ++i) {
|
|
|
|
int FixedOff = -getObjectOffset(i);
|
|
|
|
if (FixedOff > Offset) Offset = FixedOff;
|
|
|
|
}
|
|
|
|
for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) {
|
|
|
|
if (isDeadObjectIndex(i))
|
|
|
|
continue;
|
|
|
|
Offset += getObjectSize(i);
|
|
|
|
unsigned Align = getObjectAlignment(i);
|
|
|
|
// Adjust to alignment boundary
|
|
|
|
Offset = (Offset+Align-1)/Align*Align;
|
|
|
|
|
|
|
|
MaxAlign = std::max(Align, MaxAlign);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (adjustsStack() && TFI->hasReservedCallFrame(MF))
|
|
|
|
Offset += getMaxCallFrameSize();
|
|
|
|
|
|
|
|
// Round up the size to a multiple of the alignment. If the function has
|
|
|
|
// any calls or alloca's, align to the target's StackAlignment value to
|
|
|
|
// ensure that the callee's frame or the alloca data is suitably aligned;
|
|
|
|
// otherwise, for leaf functions, align to the TransientStackAlignment
|
|
|
|
// value.
|
|
|
|
unsigned StackAlign;
|
|
|
|
if (adjustsStack() || hasVarSizedObjects() ||
|
|
|
|
(RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0))
|
|
|
|
StackAlign = TFI->getStackAlignment();
|
|
|
|
else
|
|
|
|
StackAlign = TFI->getTransientStackAlignment();
|
|
|
|
|
|
|
|
// If the frame pointer is eliminated, all frame offsets will be relative to
|
|
|
|
// SP not FP. Align to MaxAlign so this works.
|
|
|
|
StackAlign = std::max(StackAlign, MaxAlign);
|
|
|
|
unsigned AlignMask = StackAlign - 1;
|
|
|
|
Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
|
|
|
|
|
|
|
|
return (unsigned)Offset;
|
|
|
|
}
|
2009-08-14 00:19:33 +08:00
|
|
|
|
2009-08-23 09:12:47 +08:00
|
|
|
void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
|
2009-11-01 04:19:03 +08:00
|
|
|
if (Objects.empty()) return;
|
|
|
|
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
|
2008-11-03 19:16:43 +08:00
|
|
|
int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
|
2003-01-17 02:35:57 +08:00
|
|
|
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << "Frame Objects:\n";
|
|
|
|
|
2002-12-29 04:37:16 +08:00
|
|
|
for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
|
|
|
|
const StackObject &SO = Objects[i];
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
|
2008-02-27 11:04:06 +08:00
|
|
|
if (SO.Size == ~0ULL) {
|
|
|
|
OS << "dead\n";
|
|
|
|
continue;
|
|
|
|
}
|
2002-12-29 04:37:16 +08:00
|
|
|
if (SO.Size == 0)
|
|
|
|
OS << "variable sized";
|
|
|
|
else
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << "size=" << SO.Size;
|
|
|
|
OS << ", align=" << SO.Alignment;
|
2004-09-06 02:41:35 +08:00
|
|
|
|
2002-12-29 04:37:16 +08:00
|
|
|
if (i < NumFixedObjects)
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << ", fixed";
|
2002-12-29 04:37:16 +08:00
|
|
|
if (i < NumFixedObjects || SO.SPOffset != -1) {
|
2007-04-25 12:20:54 +08:00
|
|
|
int64_t Off = SO.SPOffset - ValOffset;
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << ", at location [SP";
|
2003-01-17 02:35:57 +08:00
|
|
|
if (Off > 0)
|
2004-09-06 02:41:35 +08:00
|
|
|
OS << "+" << Off;
|
2003-01-17 02:35:57 +08:00
|
|
|
else if (Off < 0)
|
2004-09-06 02:41:35 +08:00
|
|
|
OS << Off;
|
2002-12-29 04:37:16 +08:00
|
|
|
OS << "]";
|
|
|
|
}
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-12 06:23:19 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2003-01-17 02:35:57 +08:00
|
|
|
void MachineFrameInfo::dump(const MachineFunction &MF) const {
|
2010-01-05 07:39:17 +08:00
|
|
|
print(MF, dbgs());
|
2003-01-17 02:35:57 +08:00
|
|
|
}
|
2012-09-07 03:06:06 +08:00
|
|
|
#endif
|
2002-12-29 04:37:16 +08:00
|
|
|
|
2006-04-23 02:53:45 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MachineJumpTableInfo implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-01-26 07:26:13 +08:00
|
|
|
/// getEntrySize - Return the size of each entry in the jump table.
|
2012-10-09 00:38:25 +08:00
|
|
|
unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
|
2010-01-26 07:26:13 +08:00
|
|
|
// The size of a jump table entry is 4 bytes unless the entry is just the
|
|
|
|
// address of a block, in which case it is the pointer size.
|
|
|
|
switch (getEntryKind()) {
|
|
|
|
case MachineJumpTableInfo::EK_BlockAddress:
|
Revert the majority of the next patch in the address space series:
r165941: Resubmit the changes to llvm core to update the functions to
support different pointer sizes on a per address space basis.
Despite this commit log, this change primarily changed stuff outside of
VMCore, and those changes do not carry any tests for correctness (or
even plausibility), and we have consistently found questionable or flat
out incorrect cases in these changes. Most of them are probably correct,
but we need to devise a system that makes it more clear when we have
handled the address space concerns correctly, and ideally each pass that
gets updated would receive an accompanying test case that exercises that
pass specificaly w.r.t. alternate address spaces.
However, from this commit, I have retained the new C API entry points.
Those were an orthogonal change that probably should have been split
apart, but they seem entirely good.
In several places the changes were very obvious cleanups with no actual
multiple address space code added; these I have not reverted when
I spotted them.
In a few other places there were merge conflicts due to a cleaner
solution being implemented later, often not using address spaces at all.
In those cases, I've preserved the new code which isn't address space
dependent.
This is part of my ongoing effort to clean out the partial address space
code which carries high risk and low test coverage, and not likely to be
finished before the 3.2 release looms closer. Duncan and I would both
like to see the above issues addressed before we return to these
changes.
llvm-svn: 167222
2012-11-01 17:14:31 +08:00
|
|
|
return TD.getPointerSize();
|
2012-02-03 12:33:00 +08:00
|
|
|
case MachineJumpTableInfo::EK_GPRel64BlockAddress:
|
|
|
|
return 8;
|
2010-01-26 07:26:13 +08:00
|
|
|
case MachineJumpTableInfo::EK_GPRel32BlockAddress:
|
|
|
|
case MachineJumpTableInfo::EK_LabelDifference32:
|
2010-01-26 12:05:28 +08:00
|
|
|
case MachineJumpTableInfo::EK_Custom32:
|
2010-01-26 07:26:13 +08:00
|
|
|
return 4;
|
2010-03-11 22:58:16 +08:00
|
|
|
case MachineJumpTableInfo::EK_Inline:
|
|
|
|
return 0;
|
2010-01-26 07:26:13 +08:00
|
|
|
}
|
2012-02-06 16:17:43 +08:00
|
|
|
llvm_unreachable("Unknown jump table encoding!");
|
2010-01-26 07:26:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getEntryAlignment - Return the alignment of each entry in the jump table.
|
2012-10-09 00:38:25 +08:00
|
|
|
unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
|
2010-01-26 07:26:13 +08:00
|
|
|
// The alignment of a jump table entry is the alignment of int32 unless the
|
|
|
|
// entry is just the address of a block, in which case it is the pointer
|
|
|
|
// alignment.
|
|
|
|
switch (getEntryKind()) {
|
|
|
|
case MachineJumpTableInfo::EK_BlockAddress:
|
Revert the majority of the next patch in the address space series:
r165941: Resubmit the changes to llvm core to update the functions to
support different pointer sizes on a per address space basis.
Despite this commit log, this change primarily changed stuff outside of
VMCore, and those changes do not carry any tests for correctness (or
even plausibility), and we have consistently found questionable or flat
out incorrect cases in these changes. Most of them are probably correct,
but we need to devise a system that makes it more clear when we have
handled the address space concerns correctly, and ideally each pass that
gets updated would receive an accompanying test case that exercises that
pass specificaly w.r.t. alternate address spaces.
However, from this commit, I have retained the new C API entry points.
Those were an orthogonal change that probably should have been split
apart, but they seem entirely good.
In several places the changes were very obvious cleanups with no actual
multiple address space code added; these I have not reverted when
I spotted them.
In a few other places there were merge conflicts due to a cleaner
solution being implemented later, often not using address spaces at all.
In those cases, I've preserved the new code which isn't address space
dependent.
This is part of my ongoing effort to clean out the partial address space
code which carries high risk and low test coverage, and not likely to be
finished before the 3.2 release looms closer. Duncan and I would both
like to see the above issues addressed before we return to these
changes.
llvm-svn: 167222
2012-11-01 17:14:31 +08:00
|
|
|
return TD.getPointerABIAlignment();
|
2012-02-03 12:33:00 +08:00
|
|
|
case MachineJumpTableInfo::EK_GPRel64BlockAddress:
|
|
|
|
return TD.getABIIntegerTypeAlignment(64);
|
2010-01-26 07:26:13 +08:00
|
|
|
case MachineJumpTableInfo::EK_GPRel32BlockAddress:
|
|
|
|
case MachineJumpTableInfo::EK_LabelDifference32:
|
2010-01-26 12:05:28 +08:00
|
|
|
case MachineJumpTableInfo::EK_Custom32:
|
2010-01-26 07:26:13 +08:00
|
|
|
return TD.getABIIntegerTypeAlignment(32);
|
2010-03-11 22:58:16 +08:00
|
|
|
case MachineJumpTableInfo::EK_Inline:
|
|
|
|
return 1;
|
2010-01-26 07:26:13 +08:00
|
|
|
}
|
2012-02-06 16:17:43 +08:00
|
|
|
llvm_unreachable("Unknown jump table encoding!");
|
2010-01-26 07:26:13 +08:00
|
|
|
}
|
|
|
|
|
Fix pr6543: svn r88806 changed MachineJumpTableInfo::getJumpTableIndex() to
always create a new jump table. The intention was to avoid merging jump
tables in SelectionDAGBuilder, and to wait for the branch folding pass to
merge tables. Unfortunately, the same getJumpTableIndex() method is also
used to merge tables in branch folding, so as a result of this change
branch tables are never merged. Worse, the branch folding code is expecting
getJumpTableIndex to always return the index of an existing table, but with
this change, it never does so. In at least some cases, e.g., pr6543, this
creates references to non-existent tables.
I've fixed the problem by adding a new createJumpTableIndex function, which
will always create a new table, and I've changed getJumpTableIndex to only
look at existing tables.
llvm-svn: 98845
2010-03-19 02:42:41 +08:00
|
|
|
/// createJumpTableIndex - Create a new jump table entry in the jump table info.
|
2006-04-23 02:53:45 +08:00
|
|
|
///
|
Fix pr6543: svn r88806 changed MachineJumpTableInfo::getJumpTableIndex() to
always create a new jump table. The intention was to avoid merging jump
tables in SelectionDAGBuilder, and to wait for the branch folding pass to
merge tables. Unfortunately, the same getJumpTableIndex() method is also
used to merge tables in branch folding, so as a result of this change
branch tables are never merged. Worse, the branch folding code is expecting
getJumpTableIndex to always return the index of an existing table, but with
this change, it never does so. In at least some cases, e.g., pr6543, this
creates references to non-existent tables.
I've fixed the problem by adding a new createJumpTableIndex function, which
will always create a new table, and I've changed getJumpTableIndex to only
look at existing tables.
llvm-svn: 98845
2010-03-19 02:42:41 +08:00
|
|
|
unsigned MachineJumpTableInfo::createJumpTableIndex(
|
2006-10-29 02:17:09 +08:00
|
|
|
const std::vector<MachineBasicBlock*> &DestBBs) {
|
2006-10-29 02:11:20 +08:00
|
|
|
assert(!DestBBs.empty() && "Cannot create an empty jump table!");
|
2006-04-23 02:53:45 +08:00
|
|
|
JumpTables.push_back(MachineJumpTableEntry(DestBBs));
|
|
|
|
return JumpTables.size()-1;
|
|
|
|
}
|
|
|
|
|
2009-04-15 09:18:49 +08:00
|
|
|
/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
|
|
|
|
/// the jump tables to branch to New instead.
|
2010-01-26 13:58:28 +08:00
|
|
|
bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
|
|
|
|
MachineBasicBlock *New) {
|
2009-04-15 09:18:49 +08:00
|
|
|
assert(Old != New && "Not making a change?");
|
|
|
|
bool MadeChange = false;
|
2009-11-15 04:09:13 +08:00
|
|
|
for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
|
|
|
|
ReplaceMBBInJumpTable(i, Old, New);
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
|
|
|
|
/// the jump table to branch to New instead.
|
2010-01-26 13:58:28 +08:00
|
|
|
bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
|
|
|
|
MachineBasicBlock *Old,
|
|
|
|
MachineBasicBlock *New) {
|
2009-11-15 04:09:13 +08:00
|
|
|
assert(Old != New && "Not making a change?");
|
|
|
|
bool MadeChange = false;
|
|
|
|
MachineJumpTableEntry &JTE = JumpTables[Idx];
|
|
|
|
for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
|
|
|
|
if (JTE.MBBs[j] == Old) {
|
|
|
|
JTE.MBBs[j] = New;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
2009-04-15 09:18:49 +08:00
|
|
|
return MadeChange;
|
|
|
|
}
|
2006-04-23 02:53:45 +08:00
|
|
|
|
2009-08-23 09:12:47 +08:00
|
|
|
void MachineJumpTableInfo::print(raw_ostream &OS) const {
|
2009-11-01 04:19:03 +08:00
|
|
|
if (JumpTables.empty()) return;
|
|
|
|
|
|
|
|
OS << "Jump Tables:\n";
|
|
|
|
|
2006-04-23 02:53:45 +08:00
|
|
|
for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << " jt#" << i << ": ";
|
|
|
|
for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
|
|
|
|
OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
|
2006-04-23 02:53:45 +08:00
|
|
|
}
|
2009-11-01 04:19:03 +08:00
|
|
|
|
|
|
|
OS << '\n';
|
2006-04-23 02:53:45 +08:00
|
|
|
}
|
|
|
|
|
2012-09-12 06:23:19 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2010-01-05 07:39:17 +08:00
|
|
|
void MachineJumpTableInfo::dump() const { print(dbgs()); }
|
2012-09-07 03:06:06 +08:00
|
|
|
#endif
|
2006-04-23 02:53:45 +08:00
|
|
|
|
|
|
|
|
2003-01-13 08:23:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MachineConstantPool implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-12-20 10:50:00 +08:00
|
|
|
void MachineConstantPoolValue::anchor() { }
|
|
|
|
|
2013-06-18 04:41:25 +08:00
|
|
|
const DataLayout *MachineConstantPool::getDataLayout() const {
|
2015-01-27 03:03:15 +08:00
|
|
|
return TM.getDataLayout();
|
2013-06-18 04:41:25 +08:00
|
|
|
}
|
|
|
|
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *MachineConstantPoolEntry::getType() const {
|
2006-09-14 13:50:57 +08:00
|
|
|
if (isMachineConstantPoolEntry())
|
2009-07-22 07:34:23 +08:00
|
|
|
return Val.MachineCPVal->getType();
|
2006-09-14 13:50:57 +08:00
|
|
|
return Val.ConstVal->getType();
|
|
|
|
}
|
|
|
|
|
2009-07-22 07:34:23 +08:00
|
|
|
|
2009-07-22 07:36:01 +08:00
|
|
|
unsigned MachineConstantPoolEntry::getRelocationInfo() const {
|
2009-07-22 07:34:23 +08:00
|
|
|
if (isMachineConstantPoolEntry())
|
2009-07-22 07:36:01 +08:00
|
|
|
return Val.MachineCPVal->getRelocationInfo();
|
2009-07-22 08:05:44 +08:00
|
|
|
return Val.ConstVal->getRelocationInfo();
|
2009-07-22 07:34:23 +08:00
|
|
|
}
|
|
|
|
|
2014-07-15 06:06:29 +08:00
|
|
|
SectionKind
|
|
|
|
MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
|
|
|
|
SectionKind Kind;
|
|
|
|
switch (getRelocationInfo()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown section kind");
|
2015-01-29 21:25:44 +08:00
|
|
|
case Constant::GlobalRelocations:
|
2014-07-15 06:06:29 +08:00
|
|
|
Kind = SectionKind::getReadOnlyWithRel();
|
|
|
|
break;
|
2015-01-29 21:25:44 +08:00
|
|
|
case Constant::LocalRelocation:
|
2014-07-15 06:06:29 +08:00
|
|
|
Kind = SectionKind::getReadOnlyWithRelLocal();
|
|
|
|
break;
|
2015-01-29 21:25:44 +08:00
|
|
|
case Constant::NoRelocation:
|
2014-07-15 06:06:29 +08:00
|
|
|
switch (DL->getTypeAllocSize(getType())) {
|
|
|
|
case 4:
|
|
|
|
Kind = SectionKind::getMergeableConst4();
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
Kind = SectionKind::getMergeableConst8();
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
Kind = SectionKind::getMergeableConst16();
|
|
|
|
break;
|
|
|
|
default:
|
2015-01-29 22:12:41 +08:00
|
|
|
Kind = SectionKind::getReadOnly();
|
2014-07-15 06:06:29 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Kind;
|
|
|
|
}
|
|
|
|
|
2006-09-13 05:00:35 +08:00
|
|
|
MachineConstantPool::~MachineConstantPool() {
|
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i)
|
|
|
|
if (Constants[i].isMachineConstantPoolEntry())
|
|
|
|
delete Constants[i].Val.MachineCPVal;
|
2011-02-22 16:54:30 +08:00
|
|
|
for (DenseSet<MachineConstantPoolValue*>::iterator I =
|
|
|
|
MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
|
|
|
|
I != E; ++I)
|
|
|
|
delete *I;
|
2006-09-13 05:00:35 +08:00
|
|
|
}
|
|
|
|
|
2009-10-28 09:12:16 +08:00
|
|
|
/// CanShareConstantPoolEntry - Test whether the given two constants
|
|
|
|
/// can be allocated the same constant pool entry.
|
2010-04-15 09:51:59 +08:00
|
|
|
static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
|
2012-10-09 00:38:25 +08:00
|
|
|
const DataLayout *TD) {
|
2009-10-28 09:12:16 +08:00
|
|
|
// Handle the trivial case quickly.
|
|
|
|
if (A == B) return true;
|
|
|
|
|
|
|
|
// If they have the same type but weren't the same constant, quickly
|
|
|
|
// reject them.
|
|
|
|
if (A->getType() == B->getType()) return false;
|
|
|
|
|
2012-01-27 09:46:00 +08:00
|
|
|
// We can't handle structs or arrays.
|
|
|
|
if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
|
|
|
|
isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
|
|
|
|
return false;
|
2014-06-25 20:40:56 +08:00
|
|
|
|
2009-10-28 09:12:16 +08:00
|
|
|
// For now, only support constants with the same size.
|
2012-01-27 05:37:55 +08:00
|
|
|
uint64_t StoreSize = TD->getTypeStoreSize(A->getType());
|
2014-06-25 20:40:56 +08:00
|
|
|
if (StoreSize != TD->getTypeStoreSize(B->getType()) || StoreSize > 128)
|
2009-10-28 09:12:16 +08:00
|
|
|
return false;
|
|
|
|
|
2012-01-27 09:46:00 +08:00
|
|
|
Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
|
|
|
|
|
|
|
|
// Try constant folding a bitcast of both instructions to an integer. If we
|
|
|
|
// get two identical ConstantInt's, then we are good to share them. We use
|
|
|
|
// the constant folding APIs to do this so that we get the benefit of
|
2012-10-09 00:38:25 +08:00
|
|
|
// DataLayout.
|
2012-01-27 09:46:00 +08:00
|
|
|
if (isa<PointerType>(A->getType()))
|
|
|
|
A = ConstantFoldInstOperands(Instruction::PtrToInt, IntTy,
|
2015-03-10 10:37:25 +08:00
|
|
|
const_cast<Constant *>(A), *TD);
|
2012-01-27 09:46:00 +08:00
|
|
|
else if (A->getType() != IntTy)
|
|
|
|
A = ConstantFoldInstOperands(Instruction::BitCast, IntTy,
|
2015-03-10 10:37:25 +08:00
|
|
|
const_cast<Constant *>(A), *TD);
|
2012-01-27 09:46:00 +08:00
|
|
|
if (isa<PointerType>(B->getType()))
|
|
|
|
B = ConstantFoldInstOperands(Instruction::PtrToInt, IntTy,
|
2015-03-10 10:37:25 +08:00
|
|
|
const_cast<Constant *>(B), *TD);
|
2012-01-27 09:46:00 +08:00
|
|
|
else if (B->getType() != IntTy)
|
|
|
|
B = ConstantFoldInstOperands(Instruction::BitCast, IntTy,
|
2015-03-10 10:37:25 +08:00
|
|
|
const_cast<Constant *>(B), *TD);
|
2012-06-20 07:37:57 +08:00
|
|
|
|
2012-01-27 09:46:00 +08:00
|
|
|
return A == B;
|
2009-10-28 09:12:16 +08:00
|
|
|
}
|
|
|
|
|
2006-02-09 12:46:04 +08:00
|
|
|
/// getConstantPoolIndex - Create a new entry in the constant pool or return
|
2008-09-17 04:45:53 +08:00
|
|
|
/// an existing one. User must specify the log2 of the minimum required
|
|
|
|
/// alignment for the object.
|
2006-02-09 12:46:04 +08:00
|
|
|
///
|
2014-06-25 20:40:56 +08:00
|
|
|
unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
|
2006-02-09 12:46:04 +08:00
|
|
|
unsigned Alignment) {
|
|
|
|
assert(Alignment && "Alignment must be specified!");
|
|
|
|
if (Alignment > PoolAlignment) PoolAlignment = Alignment;
|
2009-10-28 09:12:16 +08:00
|
|
|
|
2006-02-09 12:46:04 +08:00
|
|
|
// Check to see if we already have this constant.
|
|
|
|
//
|
|
|
|
// FIXME, this could be made much more efficient for large constant pools.
|
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i)
|
2009-10-28 09:12:16 +08:00
|
|
|
if (!Constants[i].isMachineConstantPoolEntry() &&
|
2013-06-18 04:41:25 +08:00
|
|
|
CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C,
|
|
|
|
getDataLayout())) {
|
2009-10-28 09:12:16 +08:00
|
|
|
if ((unsigned)Constants[i].getAlignment() < Alignment)
|
|
|
|
Constants[i].Alignment = Alignment;
|
2006-02-09 12:46:04 +08:00
|
|
|
return i;
|
2009-10-28 09:12:16 +08:00
|
|
|
}
|
2012-06-20 07:37:57 +08:00
|
|
|
|
2009-03-13 15:51:59 +08:00
|
|
|
Constants.push_back(MachineConstantPoolEntry(C, Alignment));
|
2006-02-09 12:46:04 +08:00
|
|
|
return Constants.size()-1;
|
|
|
|
}
|
|
|
|
|
2006-09-13 05:00:35 +08:00
|
|
|
unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
|
|
|
|
unsigned Alignment) {
|
|
|
|
assert(Alignment && "Alignment must be specified!");
|
|
|
|
if (Alignment > PoolAlignment) PoolAlignment = Alignment;
|
2012-06-20 07:37:57 +08:00
|
|
|
|
2006-09-13 05:00:35 +08:00
|
|
|
// Check to see if we already have this constant.
|
|
|
|
//
|
|
|
|
// FIXME, this could be made much more efficient for large constant pools.
|
|
|
|
int Idx = V->getExistingMachineCPValue(this, Alignment);
|
2011-02-22 16:54:30 +08:00
|
|
|
if (Idx != -1) {
|
|
|
|
MachineCPVsSharingEntries.insert(V);
|
2006-09-13 05:00:35 +08:00
|
|
|
return (unsigned)Idx;
|
2011-02-22 16:54:30 +08:00
|
|
|
}
|
2009-03-13 15:51:59 +08:00
|
|
|
|
|
|
|
Constants.push_back(MachineConstantPoolEntry(V, Alignment));
|
2006-09-13 05:00:35 +08:00
|
|
|
return Constants.size()-1;
|
|
|
|
}
|
|
|
|
|
2008-08-24 06:53:13 +08:00
|
|
|
void MachineConstantPool::print(raw_ostream &OS) const {
|
2009-11-01 04:19:03 +08:00
|
|
|
if (Constants.empty()) return;
|
|
|
|
|
|
|
|
OS << "Constant Pool:\n";
|
2006-02-01 06:23:14 +08:00
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << " cp#" << i << ": ";
|
2006-09-13 05:00:35 +08:00
|
|
|
if (Constants[i].isMachineConstantPoolEntry())
|
|
|
|
Constants[i].Val.MachineCPVal->print(OS);
|
|
|
|
else
|
2014-01-09 10:29:41 +08:00
|
|
|
Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << ", align=" << Constants[i].getAlignment();
|
2006-02-01 06:23:14 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
2003-01-13 08:23:03 +08:00
|
|
|
}
|
|
|
|
|
2012-09-12 06:23:19 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2010-01-05 07:39:17 +08:00
|
|
|
void MachineConstantPool::dump() const { print(dbgs()); }
|
2012-09-07 03:06:06 +08:00
|
|
|
#endif
|