aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/CodeGen/MachineFunction.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2023-07-26 19:03:47 +0000
committerDimitry Andric <dim@FreeBSD.org>2023-07-26 19:04:23 +0000
commit7fa27ce4a07f19b07799a767fc29416f3b625afb (patch)
tree27825c83636c4de341eb09a74f49f5d38a15d165 /llvm/lib/CodeGen/MachineFunction.cpp
parente3b557809604d036af6e00c60f012c2025b59a5e (diff)
Diffstat (limited to 'llvm/lib/CodeGen/MachineFunction.cpp')
-rw-r--r--llvm/lib/CodeGen/MachineFunction.cpp54
1 files changed, 40 insertions, 14 deletions
diff --git a/llvm/lib/CodeGen/MachineFunction.cpp b/llvm/lib/CodeGen/MachineFunction.cpp
index 59e6647fa643..88939e96e07f 100644
--- a/llvm/lib/CodeGen/MachineFunction.cpp
+++ b/llvm/lib/CodeGen/MachineFunction.cpp
@@ -22,7 +22,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Analysis/ConstantFolding.h"
-#include "llvm/Analysis/EHPersonalities.h"
+#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineConstantPool.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
@@ -45,6 +45,7 @@
#include "llvm/IR/Constant.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/EHPersonalities.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/Instruction.h"
@@ -119,7 +120,7 @@ void setUnsafeStackSize(const Function &F, MachineFrameInfo &FrameInfo) {
auto *MetadataName = "unsafe-stack-size";
if (auto &N = Existing->getOperand(0)) {
- if (cast<MDString>(N.get())->getString() == MetadataName) {
+ if (N.equalsStr(MetadataName)) {
if (auto &Op = Existing->getOperand(1)) {
auto Val = mdconst::extract<ConstantInt>(Op)->getZExtValue();
FrameInfo.setUnsafeStackSize(Val);
@@ -211,6 +212,14 @@ void MachineFunction::init() {
Alignment = std::max(Alignment,
STI->getTargetLowering()->getPrefFunctionAlignment());
+ // -fsanitize=function and -fsanitize=kcfi instrument indirect function calls
+ // to load a type hash before the function label. Ensure functions are aligned
+ // by a least 4 to avoid unaligned access, which is especially important for
+ // -mno-unaligned-access.
+ if (F.hasMetadata(LLVMContext::MD_func_sanitize) ||
+ F.getMetadata(LLVMContext::MD_kcfi_type))
+ Alignment = std::max(Alignment, Align(4));
+
if (AlignAllFunctions)
Alignment = Align(1ULL << AlignAllFunctions);
@@ -427,8 +436,7 @@ void MachineFunction::deleteMachineInstr(MachineInstr *MI) {
// be triggered during the implementation of support for the
// call site info of a new architecture. If the assertion is triggered,
// back trace will tell where to insert a call to updateCallSiteInfo().
- assert((!MI->isCandidateForCallSiteEntry() ||
- CallSitesInfo.find(MI) == CallSitesInfo.end()) &&
+ assert((!MI->isCandidateForCallSiteEntry() || !CallSitesInfo.contains(MI)) &&
"Call site info was not updated!");
// Strip it for parts. The operand array and the MI object itself are
// independently recyclable.
@@ -1083,11 +1091,10 @@ auto MachineFunction::salvageCopySSAImpl(MachineInstr &MI)
if (State.first.isVirtual()) {
// Virtual register def -- we can just look up where this happens.
MachineInstr *Inst = MRI.def_begin(State.first)->getParent();
- for (auto &MO : Inst->operands()) {
- if (!MO.isReg() || !MO.isDef() || MO.getReg() != State.first)
+ for (auto &MO : Inst->all_defs()) {
+ if (MO.getReg() != State.first)
continue;
- return ApplySubregisters(
- {Inst->getDebugInstrNum(), Inst->getOperandNo(&MO)});
+ return ApplySubregisters({Inst->getDebugInstrNum(), MO.getOperandNo()});
}
llvm_unreachable("Vreg def with no corresponding operand?");
@@ -1102,14 +1109,13 @@ auto MachineFunction::salvageCopySSAImpl(MachineInstr &MI)
auto RMII = CurInst->getReverseIterator();
auto PrevInstrs = make_range(RMII, CurInst->getParent()->instr_rend());
for (auto &ToExamine : PrevInstrs) {
- for (auto &MO : ToExamine.operands()) {
+ for (auto &MO : ToExamine.all_defs()) {
// Test for operand that defines something aliasing RegToSeek.
- if (!MO.isReg() || !MO.isDef() ||
- !TRI.regsOverlap(RegToSeek, MO.getReg()))
+ if (!TRI.regsOverlap(RegToSeek, MO.getReg()))
continue;
return ApplySubregisters(
- {ToExamine.getDebugInstrNum(), ToExamine.getOperandNo(&MO)});
+ {ToExamine.getDebugInstrNum(), MO.getOperandNo()});
}
}
@@ -1395,7 +1401,7 @@ MachineConstantPool::~MachineConstantPool() {
}
/// Test whether the given two constants can be allocated the same constant pool
-/// entry.
+/// entry referenced by \param A.
static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
const DataLayout &DL) {
// Handle the trivial case quickly.
@@ -1415,6 +1421,8 @@ static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128)
return false;
+ bool ContainsUndefOrPoisonA = A->containsUndefOrPoisonElement();
+
Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
// Try constant folding a bitcast of both instructions to an integer. If we
@@ -1434,7 +1442,14 @@ static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B),
IntTy, DL);
- return A == B;
+ if (A != B)
+ return false;
+
+ // Constants only safely match if A doesn't contain undef/poison.
+ // As we'll be reusing A, it doesn't matter if B contain undef/poison.
+ // TODO: Handle cases where A and B have the same undef/poison elements.
+ // TODO: Merge A and B with mismatching undef/poison elements.
+ return !ContainsUndefOrPoisonA;
}
/// Create a new entry in the constant pool or return an existing one.
@@ -1490,6 +1505,17 @@ void MachineConstantPool::print(raw_ostream &OS) const {
}
}
+//===----------------------------------------------------------------------===//
+// Template specialization for MachineFunction implementation of
+// ProfileSummaryInfo::getEntryCount().
+//===----------------------------------------------------------------------===//
+template <>
+std::optional<Function::ProfileCount>
+ProfileSummaryInfo::getEntryCount<llvm::MachineFunction>(
+ const llvm::MachineFunction *F) const {
+ return F->getFunction().getEntryCount();
+}
+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); }
#endif