38#define DEBUG_TYPE "riscv-promote-constant"
39#define RISCV_PROMOTE_CONSTANT_NAME "RISC-V Promote Constants"
41STATISTIC(NumPromoted,
"Number of constant literals promoted to globals");
42STATISTIC(NumPromotedUses,
"Number of uses of promoted literal constants");
46class RISCVPromoteConstantLegacy :
public ModulePass {
49 RISCVPromoteConstantLegacy() :
ModulePass(ID) {}
53 void getAnalysisUsage(AnalysisUsage &AU)
const override {
58 bool runOnModule(
Module &M)
override;
62char RISCVPromoteConstantLegacy::ID = 0;
68 return new RISCVPromoteConstantLegacy();
73 if (
F.hasOptNone() ||
F.hasOptSize())
87 for (
Use &U :
I.operands()) {
89 if (!
C || !
C->getType()->isDoubleTy())
99 unsigned OperandIdx = U.getOperandNo();
100 if (IntrinsicFunc && IntrinsicFunc->
getAttributes().hasParamAttr(
101 OperandIdx, Attribute::ImmArg)) {
103 <<
" because operand " << OperandIdx
104 <<
" must be an immediate.\n");
111 ConstUsesMap[
C].push_back(&U);
115 int PromotableConstants = ConstUsesMap.
size();
117 <<
" promotable constants in " <<
F.getName() <<
"\n");
119 if (PromotableConstants < 2) {
120 LLVM_DEBUG(
dbgs() <<
"Performing no promotions as insufficient promotable "
121 "constants found\n");
125 NumPromoted += PromotableConstants;
132 for (
auto const &Pair : ConstUsesMap)
142 ".promoted_doubles." +
F.getName());
149 for (
auto const &Pair : ConstUsesMap) {
161 InsertionBB = PN->getIncomingBlock(*U);
166 LLVM_DEBUG(
dbgs() <<
"Bailing out: catchswitch means thre is no valid "
167 "insertion point.\n");
171 auto CacheKey = std::make_pair(Const, InsertionBB);
172 Value *LoadedVal =
nullptr;
175 if (LocalLoads.
count(CacheKey)) {
176 LoadedVal = LocalLoads.
at(CacheKey);
182 Value *ElementPtr = Builder.CreateConstInBoundsGEP2_64(
183 GlobalArray->getValueType(), GlobalArray, 0, Idx,
"double.addr");
184 LoadedVal = Builder.CreateLoad(DoubleTy, ElementPtr,
"double.val");
187 LocalLoads[CacheKey] = LoadedVal;
204 if (
F.isDeclaration())
213bool RISCVPromoteConstantLegacy::runOnModule(
Module &M) {
217 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
218 const TargetMachine &TM = TPC.
getTM<TargetMachine>();
Expand Atomic instructions
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
uint64_t IntrinsicInst * II
ModuleAnalysisManager MAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Remove Loads Into Fake Uses
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
This file describes how to lower LLVM code to machine code.
Target-Independent Code Generator Pass Configuration Options pass.
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
LLVM Basic Block Representation.
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Represents analyses that only rely on functions' control flow.
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
ConstantFP - Floating Point Values [float, double].
Constant Vector Declarations.
This is an important base class in LLVM.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
ValueT & at(const_arg_type_t< KeyT > Val)
Return the entry for the specified key, or abort if no such entry exists.
AttributeList getAttributes() const
Return the attribute list for this Function.
@ InternalLinkage
Rename collisions when linking (static functions).
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
This class implements a map that also provides access to all stored values in a deterministic order.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
A Module instance is used to store all the information related to an LLVM module.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
bool isFPImmLegal(const APFloat &Imm, EVT VT, bool ForCodeSize=false) const
Returns true if the specified FP immediate can be materialized without a load from the constant pool.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
Primary interface to the complete machine description for the target machine.
const STC & getSubtarget(const Function &F) const
This method returns a pointer to the specified type of TargetSubtargetInfo.
TMC & getTM() const
Get the right type of TargetMachine for this target.
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
A Use represents the edge between a Value definition and its users.
LLVM Value Representation.
const ParentTy * getParent() const
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
ModulePass * createRISCVPromoteConstantLegacyPass()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.