Skip to content

Commit

Permalink
Revert "[Remarks] Add analysis remarks for memset/memcpy/memmove leng…
Browse files Browse the repository at this point in the history
…ths"

This reverts commit 4bf69fb.

This broke spec2k6/403.gcc under -global-isel. Details to follow once I've
reduced the problem.
  • Loading branch information
jroelofs committed May 20, 2021
1 parent e1ffcfc commit 0af3105
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 905 deletions.
3 changes: 0 additions & 3 deletions llvm/include/llvm/Analysis/OptimizationRemarkEmitter.h
Expand Up @@ -76,9 +76,6 @@ class OptimizationRemarkEmitter {
if (F->getContext().getLLVMRemarkStreamer() ||
F->getContext().getDiagHandlerPtr()->isAnyRemarkEnabled()) {
auto R = RemarkBuilder();
static_assert(
std::is_base_of<DiagnosticInfoOptimizationBase, decltype(R)>::value,
"the lambda passed to emit() must return a remark");
emit((DiagnosticInfoOptimizationBase &)R);
}
}
Expand Down
@@ -1,4 +1,4 @@
//===- MemoryOpRemark.h - Memory operation remark analysis -*- C++ ------*-===//
//===- AutoInitRemark.h - Auto-init remark analysis -*- C++ -------------*-===//
//
// The LLVM Compiler Infrastructure
//
Expand All @@ -7,13 +7,13 @@
//
//===----------------------------------------------------------------------===//
//
// Provide more information about instructions that copy, move, or initialize
// memory, including those with a "auto-init" !annotation metadata.
// Provide more information about instructions with a "auto-init"
// !annotation metadata.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_TRANSFORMS_UTILS_MEMORYOPREMARK_H
#define LLVM_TRANSFORMS_UTILS_MEMORYOPREMARK_H
#ifndef LLVM_TRANSFORMS_UTILS_AUTOINITREMARK_H
#define LLVM_TRANSFORMS_UTILS_AUTOINITREMARK_H

#include "llvm/ADT/StringRef.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
Expand All @@ -22,7 +22,6 @@ namespace llvm {

class CallInst;
class DataLayout;
class DiagnosticInfoIROptimization;
class Instruction;
class IntrinsicInst;
class Value;
Expand All @@ -32,49 +31,36 @@ class StoreInst;

// FIXME: Once we get to more remarks like this one, we need to re-evaluate how
// much of this logic should actually go into the remark emitter.
struct MemoryOpRemark {
struct AutoInitRemark {
OptimizationRemarkEmitter &ORE;
StringRef RemarkPass;
const DataLayout &DL;
const TargetLibraryInfo &TLI;

MemoryOpRemark(OptimizationRemarkEmitter &ORE, StringRef RemarkPass,
AutoInitRemark(OptimizationRemarkEmitter &ORE, StringRef RemarkPass,
const DataLayout &DL, const TargetLibraryInfo &TLI)
: ORE(ORE), RemarkPass(RemarkPass), DL(DL), TLI(TLI) {}

virtual ~MemoryOpRemark();

/// \return true iff the instruction is understood by MemoryOpRemark.
static bool canHandle(const Instruction *I, const TargetLibraryInfo &TLI);

void visit(const Instruction *I);

protected:
virtual std::string explainSource(StringRef Type);

enum RemarkKind { RK_Store, RK_Unknown, RK_IntrinsicCall, RK_Call };
virtual StringRef remarkName(RemarkKind RK);

private:
/// Emit a remark using information from the store's destination, size, etc.
void visitStore(const StoreInst &SI);
void inspectStore(StoreInst &SI);
/// Emit a generic auto-init remark.
void visitUnknown(const Instruction &I);
void inspectUnknown(Instruction &I);
/// Emit a remark using information from known intrinsic calls.
void visitIntrinsicCall(const IntrinsicInst &II);
void inspectIntrinsicCall(IntrinsicInst &II);
/// Emit a remark using information from known function calls.
void visitCall(const CallInst &CI);
void inspectCall(CallInst &CI);

private:
/// Add callee information to a remark: whether it's known, the function name,
/// etc.
template <typename FTy>
void visitCallee(FTy F, bool KnownLibCall, OptimizationRemarkMissed &R);
void inspectCallee(FTy F, bool KnownLibCall, OptimizationRemarkMissed &R);
/// Add operand information to a remark based on knowledge we have for known
/// libcalls.
void visitKnownLibCall(const CallInst &CI, LibFunc LF,
OptimizationRemarkMissed &R);
void inspectKnownLibCall(CallInst &CI, LibFunc LF,
OptimizationRemarkMissed &R);
/// Add the memory operation size to a remark.
void visitSizeOperand(Value *V, OptimizationRemarkMissed &R);
void inspectSizeOperand(Value *V, OptimizationRemarkMissed &R);

struct VariableInfo {
Optional<StringRef> Name;
Expand All @@ -84,22 +70,8 @@ struct MemoryOpRemark {
/// Gather more information about \p V as a variable. This can be debug info,
/// information from the alloca, etc. Since \p V can represent more than a
/// single variable, they will all be added to the remark.
void visitPtr(Value *V, bool IsSrc, OptimizationRemarkMissed &R);
void visitVariable(const Value *V, SmallVectorImpl<VariableInfo> &Result);
};

/// Special case for -ftrivial-auto-var-init remarks.
struct AutoInitRemark : public MemoryOpRemark {
AutoInitRemark(OptimizationRemarkEmitter &ORE, StringRef RemarkPass,
const DataLayout &DL, const TargetLibraryInfo &TLI)
: MemoryOpRemark(ORE, RemarkPass, DL, TLI) {}

/// \return true iff the instruction is understood by AutoInitRemark.
static bool canHandle(const Instruction *I);

protected:
virtual std::string explainSource(StringRef Type) override;
virtual StringRef remarkName(RemarkKind RK) override;
void inspectDst(Value *Dst, OptimizationRemarkMissed &R);
void inspectVariable(const Value *V, SmallVectorImpl<VariableInfo> &Result);
};

} // namespace llvm
Expand Down
21 changes: 0 additions & 21 deletions llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
Expand Up @@ -72,7 +72,6 @@
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetIntrinsicInfo.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/Utils/MemoryOpRemark.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
Expand All @@ -98,7 +97,6 @@ INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
INITIALIZE_PASS_DEPENDENCY(GISelCSEAnalysisWrapperPass)
INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(StackProtector)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
INITIALIZE_PASS_END(IRTranslator, DEBUG_TYPE, "IRTranslator LLVM IR -> MI",
false, false)

Expand Down Expand Up @@ -166,8 +164,6 @@ void IRTranslator::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<GISelCSEAnalysisWrapperPass>();
if (OptLevel != CodeGenOpt::None)
AU.addRequired<BranchProbabilityInfoWrapperPass>();
AU.addRequired<TargetLibraryInfoWrapperPass>();
AU.addPreserved<TargetLibraryInfoWrapperPass>();
getSelectionDAGFallbackAnalysisUsage(AU);
MachineFunctionPass::getAnalysisUsage(AU);
}
Expand Down Expand Up @@ -1819,14 +1815,6 @@ bool IRTranslator::translateConstrainedFPIntrinsic(

bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
MachineIRBuilder &MIRBuilder) {
if (auto *MI = dyn_cast<AnyMemIntrinsic>(&CI)) {
const Function &F = *MI->getParent()->getParent();
auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
if (MemoryOpRemark::canHandle(MI, TLI)) {
MemoryOpRemark R(*ORE, "memsize", *DL, TLI);
R.visit(MI);
}
}

// If this is a simple intrinsic (that is, we just need to add a def of
// a vreg, and uses for each arg operand, then translate it.
Expand Down Expand Up @@ -2256,15 +2244,6 @@ bool IRTranslator::translateCallBase(const CallBase &CB,
Args.push_back(getOrCreateVRegs(*Arg));
}

if (auto *CI = dyn_cast<CallInst>(&CB)) {
const Function &F = *CI->getParent()->getParent();
auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
if (MemoryOpRemark::canHandle(CI, TLI)) {
MemoryOpRemark R(*ORE, "memsize", *DL, TLI);
R.visit(CI);
}
}

// We don't set HasCalls on MFI here yet because call lowering may decide to
// optimize into tail calls. Instead, we defer that to selection where a final
// scan is done to check if any instructions are calls.
Expand Down
46 changes: 39 additions & 7 deletions llvm/lib/Transforms/Scalar/AnnotationRemarks.cpp
Expand Up @@ -22,7 +22,7 @@
#include "llvm/Pass.h"
#include "llvm/Support/Debug.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Utils/MemoryOpRemark.h"
#include "llvm/Transforms/Utils/AutoInitRemark.h"

using namespace llvm;
using namespace llvm::ore;
Expand All @@ -35,13 +35,45 @@ static void tryEmitAutoInitRemark(ArrayRef<Instruction *> Instructions,
const TargetLibraryInfo &TLI) {
// For every auto-init annotation generate a separate remark.
for (Instruction *I : Instructions) {
if (!AutoInitRemark::canHandle(I))
if (!I->hasMetadata(LLVMContext::MD_annotation))
continue;

Function &F = *I->getParent()->getParent();
const DataLayout &DL = F.getParent()->getDataLayout();
AutoInitRemark Remark(ORE, REMARK_PASS, DL, TLI);
Remark.visit(I);
for (const MDOperand &Op :
I->getMetadata(LLVMContext::MD_annotation)->operands()) {
if (cast<MDString>(Op.get())->getString() != "auto-init")
continue;

Function &F = *I->getParent()->getParent();
const DataLayout &DL = F.getParent()->getDataLayout();
AutoInitRemark Remark(ORE, REMARK_PASS, DL, TLI);
// For some of them, we can provide more information:

// For stores:
// * size
// * volatile / atomic
if (auto *SI = dyn_cast<StoreInst>(I)) {
Remark.inspectStore(*SI);
continue;
}

// For intrinsics:
// * user-friendly name
// * size
if (auto *II = dyn_cast<IntrinsicInst>(I)) {
Remark.inspectIntrinsicCall(*II);
continue;
}

// For calls:
// * known/unknown function (e.g. the compiler knows bzero, but it doesn't
// know my_bzero)
// * memory operation size
if (auto *CI = dyn_cast<CallInst>(I)) {
Remark.inspectCall(*CI);
continue;
}

Remark.inspectUnknown(*I);
}
}
}

Expand Down

0 comments on commit 0af3105

Please sign in to comment.