51#define DEBUG_TYPE "aarch64-promote-const"
53STATISTIC(NumPromoted,
"Number of promoted constants");
54STATISTIC(NumPromotedUses,
"Number of promoted constants uses");
88class AArch64PromoteConstant :
public ModulePass {
90 struct PromotedConstant {
91 bool ShouldConvert =
false;
94 using PromotionCacheTy = SmallDenseMap<Constant *, PromotedConstant, 16>;
101 UpdateRecord(Constant *C, Instruction *User,
unsigned Op)
102 : C(C), User(User), Op(Op) {}
107 AArch64PromoteConstant() : ModulePass(ID) {}
109 StringRef getPassName()
const override {
return "AArch64 Promote Constant"; }
113 bool runOnModule(
Module &M)
override {
118 PromotionCacheTy PromotionCache;
132 void getAnalysisUsage(AnalysisUsage &AU)
const override {
140 using InsertionPoints = DenseMap<Instruction *, Uses>;
143 Instruction *findInsertionPoint(Instruction &User,
unsigned OpNo);
156 bool isDominated(Instruction *NewPt, Instruction *User,
unsigned OpNo,
157 InsertionPoints &InsertPts);
172 bool tryAndMerge(Instruction *NewPt, Instruction *User,
unsigned OpNo,
173 InsertionPoints &InsertPts);
182 void computeInsertionPoint(Instruction *User,
unsigned OpNo,
183 InsertionPoints &InsertPts);
188 void insertDefinitions(
Function &
F, GlobalVariable &GV,
189 InsertionPoints &InsertPts);
193 void promoteConstants(
Function &
F, SmallVectorImpl<UpdateRecord> &Updates,
194 PromotionCacheTy &PromotionCache);
198 static void appendAndTransferDominatedUses(Instruction *NewPt,
199 Instruction *User,
unsigned OpNo,
201 InsertionPoints &InsertPts) {
203 IPI->second.emplace_back(User, OpNo);
209 Uses OldUses = std::move(IPI->second);
210 InsertPts[NewPt] = std::move(OldUses);
212 InsertPts.erase(OldInstr);
218char AArch64PromoteConstant::ID = 0;
221 "AArch64 Promote Constant Pass",
false,
false)
227 return new AArch64PromoteConstant();
236 EltIdx < EndEltIdx; ++EltIdx)
253 return all_of(
C->operands(), [](
const Use &U) {
254 return containsOnlyConstantData(cast<Constant>(&U));
347 const AArch64Options &CLOpts =
349 if (CLOpts.stress_promote_const)
360 AArch64PromoteConstant::PromotionCacheTy &PromotionCache) {
361 auto Converted = PromotionCache.
insert(
362 std::make_pair(&
C, AArch64PromoteConstant::PromotedConstant()));
363 if (Converted.second)
365 return Converted.first->second.ShouldConvert;
373 return PhiInst->getIncomingBlock(OpNo)->getTerminator();
378bool AArch64PromoteConstant::isDominated(Instruction *NewPt, Instruction *User,
380 InsertionPoints &InsertPts) {
381 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(
382 *NewPt->
getParent()->getParent()).getDomTree();
386 for (
auto &IPI : InsertPts) {
387 if (NewPt == IPI.first || DT.
dominates(IPI.first, NewPt) ||
391 (IPI.first->getParent() != NewPt->
getParent() &&
397 IPI.second.emplace_back(User, OpNo);
404bool AArch64PromoteConstant::tryAndMerge(Instruction *NewPt, Instruction *User,
406 InsertionPoints &InsertPts) {
407 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(
408 *NewPt->
getParent()->getParent()).getDomTree();
414 for (InsertionPoints::iterator IPI = InsertPts.begin(),
415 EndIPI = InsertPts.end();
416 IPI != EndIPI; ++IPI) {
418 if (NewBB == CurBB) {
425 appendAndTransferDominatedUses(NewPt, User, OpNo, IPI, InsertPts);
432 if (!CommonDominator)
435 if (CommonDominator != NewBB) {
437 assert(CommonDominator != CurBB &&
438 "Instruction has not been rejected during isDominated check!");
449 appendAndTransferDominatedUses(NewPt, User, OpNo, IPI, InsertPts);
455void AArch64PromoteConstant::computeInsertionPoint(
456 Instruction *User,
unsigned OpNo, InsertionPoints &InsertPts) {
461 Instruction *InsertionPoint = findInsertionPoint(*User, OpNo);
467 if (isDominated(InsertionPoint, User, OpNo, InsertPts))
471 if (tryAndMerge(InsertionPoint, User, OpNo, InsertPts))
477 InsertPts[InsertionPoint].emplace_back(User, OpNo);
481 AArch64PromoteConstant::PromotedConstant &PC) {
482 assert(PC.ShouldConvert &&
483 "Expected that we should convert this to a global");
496void AArch64PromoteConstant::insertDefinitions(
Function &
F,
497 GlobalVariable &PromotedGV,
498 InsertionPoints &InsertPts) {
501 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(
F).getDomTree();
503 assert(!InsertPts.empty() &&
"Empty uses does not need a definition");
505 for (
const auto &IPI : InsertPts) {
508 LoadInst *LoadedCst =
509 Builder.CreateLoad(PromotedGV.
getValueType(), &PromotedGV);
516 for (
auto Use : IPI.second) {
519 findInsertionPoint(*
Use.first,
Use.second)) &&
520 "Inserted definition does not dominate all its uses!");
523 dbgs() <<
"Use to update " <<
Use.second <<
":";
527 Use.first->setOperand(
Use.second, LoadedCst);
533void AArch64PromoteConstant::promoteConstants(
534 Function &
F, SmallVectorImpl<UpdateRecord> &Updates,
535 PromotionCacheTy &PromotionCache) {
537 for (
auto U = Updates.
begin(),
E = Updates.
end(); U !=
E;) {
541 InsertionPoints InsertPts;
543 computeInsertionPoint(
U->User,
U->Op, InsertPts);
544 }
while (++U !=
E &&
U->C ==
C);
546 auto &Promotion = PromotionCache[
C];
548 insertDefinitions(
F, *Promotion.GV, InsertPts);
552bool AArch64PromoteConstant::runOnFunction(
Function &
F,
553 PromotionCacheTy &PromotionCache) {
561 for (Use &U :
I.operands()) {
575 unsigned OpNo = &
U -
I.op_begin();
586 promoteConstants(
F, Updates, PromotionCache);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Expand Atomic instructions
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
static bool runOnFunction(Function &F, bool PostInlining)
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
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)
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
bool isInlineAsm() const
Check if this call is an inline asm statement.
This class represents a function call, abstracting a target machine's calling convention.
This is an important base class in LLVM.
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Legacy analysis pass which computes a DominatorTree.
LLVM_ABI Instruction * findNearestCommonDominator(Instruction *I1, Instruction *I2) const
Find the nearest instruction I that dominates both I1 and I2, in the sense that a result produced bef...
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
@ InternalLinkage
Rename collisions when linking (static functions).
Type * getValueType() const
LLVM_ABI void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
LLVM_ABI cl::LibraryOptionsContext & getLibraryOptions() const
The library options this context reads in place of their process-wide values.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
reference emplace_back(ArgTypes &&... Args)
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM_ABI Type * getStructElementType(unsigned N) const
bool isVectorTy() const
True if this is an instance of VectorType.
bool isArrayTy() const
True if this is an instance of ArrayType.
Type * getArrayElementType() const
LLVM_ABI unsigned getStructNumElements() const
bool isStructTy() const
True if this is an instance of StructType.
LLVM_ABI bool isScalableTy() const
Return true if this is a type whose size is a known multiple of vscale.
A Use represents the edge between a Value definition and its users.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
LLVMContext & getContext() const
All values hold a context through their type.
const ParentTy * getParent() const
@ BasicBlock
Various leaf nodes.
@ User
could "use" a pointer
NodeAddr< UseNode * > Use
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
IRBuilder(LLVMContext &, FolderTy, InserterTy) -> IRBuilder< FolderTy, InserterTy >
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
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...
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
ModulePass * createAArch64PromoteConstantPass()