aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/llvm/lib/CodeGen/MachineFunction.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/llvm-project/llvm/lib/CodeGen/MachineFunction.cpp')
-rw-r--r--contrib/llvm-project/llvm/lib/CodeGen/MachineFunction.cpp111
1 files changed, 62 insertions, 49 deletions
diff --git a/contrib/llvm-project/llvm/lib/CodeGen/MachineFunction.cpp b/contrib/llvm-project/llvm/lib/CodeGen/MachineFunction.cpp
index 02f58ca5eef0..f58996ea90c6 100644
--- a/contrib/llvm-project/llvm/lib/CodeGen/MachineFunction.cpp
+++ b/contrib/llvm-project/llvm/lib/CodeGen/MachineFunction.cpp
@@ -44,7 +44,6 @@
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/DataLayout.h"
-#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
@@ -61,7 +60,6 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DOTGraphTraits.h"
-#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/GraphWriter.h"
#include "llvm/Support/raw_ostream.h"
@@ -109,6 +107,27 @@ static const char *getPropertyName(MachineFunctionProperties::Property Prop) {
llvm_unreachable("Invalid machine function property");
}
+void setUnsafeStackSize(const Function &F, MachineFrameInfo &FrameInfo) {
+ if (!F.hasFnAttribute(Attribute::SafeStack))
+ return;
+
+ auto *Existing =
+ dyn_cast_or_null<MDTuple>(F.getMetadata(LLVMContext::MD_annotation));
+
+ if (!Existing || Existing->getNumOperands() != 2)
+ return;
+
+ auto *MetadataName = "unsafe-stack-size";
+ if (auto &N = Existing->getOperand(0)) {
+ if (cast<MDString>(N.get())->getString() == MetadataName) {
+ if (auto &Op = Existing->getOperand(1)) {
+ auto Val = mdconst::extract<ConstantInt>(Op)->getZExtValue();
+ FrameInfo.setUnsafeStackSize(Val);
+ }
+ }
+ }
+}
+
// Pin the vtable to this file.
void MachineFunction::Delegate::anchor() {}
@@ -133,11 +152,11 @@ void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
MBB->getParent()->deleteMachineBasicBlock(MBB);
}
-static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI,
+static inline Align getFnStackAlignment(const TargetSubtargetInfo *STI,
const Function &F) {
if (auto MA = F.getFnStackAlign())
- return MA->value();
- return STI->getFrameLowering()->getStackAlign().value();
+ return *MA;
+ return STI->getFrameLowering()->getStackAlign();
}
MachineFunction::MachineFunction(Function &F, const LLVMTargetMachine &Target,
@@ -177,6 +196,8 @@ void MachineFunction::init() {
/*ForcedRealign=*/CanRealignSP &&
F.hasFnAttribute(Attribute::StackAlignment));
+ setUnsafeStackSize(F, *FrameInfo);
+
if (F.hasFnAttribute(Attribute::StackAlignment))
FrameInfo->ensureMaxAlignment(*F.getFnStackAlign());
@@ -208,9 +229,7 @@ void MachineFunction::init() {
"Can't create a MachineFunction using a Module with a "
"Target-incompatible DataLayout attached\n");
- PSVManager =
- std::make_unique<PseudoSourceValueManager>(*(getSubtarget().
- getInstrInfo()));
+ PSVManager = std::make_unique<PseudoSourceValueManager>(getTarget());
}
MachineFunction::~MachineFunction() {
@@ -837,25 +856,6 @@ void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
LP.TypeIds.push_back(0);
}
-void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad,
- const Function *Filter,
- const BlockAddress *RecoverBA) {
- LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
- SEHHandler Handler;
- Handler.FilterOrFinally = Filter;
- Handler.RecoverBA = RecoverBA;
- LP.SEHHandlers.push_back(Handler);
-}
-
-void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
- const Function *Cleanup) {
- LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
- SEHHandler Handler;
- Handler.FilterOrFinally = Cleanup;
- Handler.RecoverBA = nullptr;
- LP.SEHHandlers.push_back(Handler);
-}
-
void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
ArrayRef<unsigned> Sites) {
LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
@@ -1012,7 +1012,32 @@ void MachineFunction::substituteDebugValuesForInst(const MachineInstr &Old,
}
}
-auto MachineFunction::salvageCopySSA(MachineInstr &MI)
+auto MachineFunction::salvageCopySSA(
+ MachineInstr &MI, DenseMap<Register, DebugInstrOperandPair> &DbgPHICache)
+ -> DebugInstrOperandPair {
+ const TargetInstrInfo &TII = *getSubtarget().getInstrInfo();
+
+ // Check whether this copy-like instruction has already been salvaged into
+ // an operand pair.
+ Register Dest;
+ if (auto CopyDstSrc = TII.isCopyInstr(MI)) {
+ Dest = CopyDstSrc->Destination->getReg();
+ } else {
+ assert(MI.isSubregToReg());
+ Dest = MI.getOperand(0).getReg();
+ }
+
+ auto CacheIt = DbgPHICache.find(Dest);
+ if (CacheIt != DbgPHICache.end())
+ return CacheIt->second;
+
+ // Calculate the instruction number to use, or install a DBG_PHI.
+ auto OperandPair = salvageCopySSAImpl(MI);
+ DbgPHICache.insert({Dest, OperandPair});
+ return OperandPair;
+}
+
+auto MachineFunction::salvageCopySSAImpl(MachineInstr &MI)
-> DebugInstrOperandPair {
MachineRegisterInfo &MRI = getRegInfo();
const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
@@ -1141,26 +1166,13 @@ auto MachineFunction::salvageCopySSA(MachineInstr &MI)
MachineBasicBlock &InsertBB = *CurInst->getParent();
// We reached the start of the block before finding a defining instruction.
- // It could be from a constant register, otherwise it must be an argument.
- if (TRI.isConstantPhysReg(State.first)) {
- // We can produce a DBG_PHI that identifies the constant physreg. Doesn't
- // matter where we put it, as it's constant valued.
- assert(CurInst->isCopy());
- } else if (State.first == TRI.getFrameRegister(*this)) {
- // LLVM IR is allowed to read the framepointer by calling a
- // llvm.frameaddress.* intrinsic. We can support this by emitting a
- // DBG_PHI $fp. This isn't ideal, because it extends the behaviours /
- // position that DBG_PHIs appear at, limiting what can be done later.
- // TODO: see if there's a better way of expressing these variable
- // locations.
- ;
- } else {
- // Assert that this is the entry block, or an EH pad. If it isn't, then
- // there is some code construct we don't recognise that deals with physregs
- // across blocks.
- assert(!State.first.isVirtual());
- assert(&*InsertBB.getParent()->begin() == &InsertBB || InsertBB.isEHPad());
- }
+ // There are numerous scenarios where this can happen:
+ // * Constant physical registers,
+ // * Several intrinsics that allow LLVM-IR to read arbitary registers,
+ // * Arguments in the entry block,
+ // * Exception handling landing pads.
+ // Validating all of them is too difficult, so just insert a DBG_PHI reading
+ // the variable value at this position, rather than checking it makes sense.
// Create DBG_PHI for specified physreg.
auto Builder = BuildMI(InsertBB, InsertBB.getFirstNonPHI(), DebugLoc(),
@@ -1181,6 +1193,7 @@ void MachineFunction::finalizeDebugInstrRefs() {
MI.getOperand(1).ChangeToRegister(0, false);
};
+ DenseMap<Register, DebugInstrOperandPair> ArgDbgPHIs;
for (auto &MBB : *this) {
for (auto &MI : MBB) {
if (!MI.isDebugRef() || !MI.getOperand(0).isReg())
@@ -1203,7 +1216,7 @@ void MachineFunction::finalizeDebugInstrRefs() {
// instruction that defines the source value, see salvageCopySSA docs
// for why this is important.
if (DefMI.isCopyLike() || TII->isCopyInstr(DefMI)) {
- auto Result = salvageCopySSA(DefMI);
+ auto Result = salvageCopySSA(DefMI, ArgDbgPHIs);
MI.getOperand(0).ChangeToImmediate(Result.first);
MI.getOperand(1).setImm(Result.second);
} else {