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 if (AArch64Options::Global.stress_promote_const)
358 AArch64PromoteConstant::PromotionCacheTy &PromotionCache) {
359 auto Converted = PromotionCache.
insert(
360 std::make_pair(&
C, AArch64PromoteConstant::PromotedConstant()));
361 if (Converted.second)
363 return Converted.first->second.ShouldConvert;
371 return PhiInst->getIncomingBlock(OpNo)->getTerminator();
376bool AArch64PromoteConstant::isDominated(Instruction *NewPt, Instruction *User,
378 InsertionPoints &InsertPts) {
379 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(
380 *NewPt->
getParent()->getParent()).getDomTree();
384 for (
auto &IPI : InsertPts) {
385 if (NewPt == IPI.first || DT.
dominates(IPI.first, NewPt) ||
389 (IPI.first->getParent() != NewPt->
getParent() &&
395 IPI.second.emplace_back(User, OpNo);
402bool AArch64PromoteConstant::tryAndMerge(Instruction *NewPt, Instruction *User,
404 InsertionPoints &InsertPts) {
405 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(
406 *NewPt->
getParent()->getParent()).getDomTree();
412 for (InsertionPoints::iterator IPI = InsertPts.begin(),
413 EndIPI = InsertPts.end();
414 IPI != EndIPI; ++IPI) {
416 if (NewBB == CurBB) {
423 appendAndTransferDominatedUses(NewPt, User, OpNo, IPI, InsertPts);
430 if (!CommonDominator)
433 if (CommonDominator != NewBB) {
435 assert(CommonDominator != CurBB &&
436 "Instruction has not been rejected during isDominated check!");
447 appendAndTransferDominatedUses(NewPt, User, OpNo, IPI, InsertPts);
453void AArch64PromoteConstant::computeInsertionPoint(
454 Instruction *User,
unsigned OpNo, InsertionPoints &InsertPts) {
459 Instruction *InsertionPoint = findInsertionPoint(*User, OpNo);
465 if (isDominated(InsertionPoint, User, OpNo, InsertPts))
469 if (tryAndMerge(InsertionPoint, User, OpNo, InsertPts))
475 InsertPts[InsertionPoint].emplace_back(User, OpNo);
479 AArch64PromoteConstant::PromotedConstant &PC) {
480 assert(PC.ShouldConvert &&
481 "Expected that we should convert this to a global");
494void AArch64PromoteConstant::insertDefinitions(
Function &
F,
495 GlobalVariable &PromotedGV,
496 InsertionPoints &InsertPts) {
499 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(
F).getDomTree();
501 assert(!InsertPts.empty() &&
"Empty uses does not need a definition");
503 for (
const auto &IPI : InsertPts) {
506 LoadInst *LoadedCst =
507 Builder.CreateLoad(PromotedGV.
getValueType(), &PromotedGV);
514 for (
auto Use : IPI.second) {
517 findInsertionPoint(*
Use.first,
Use.second)) &&
518 "Inserted definition does not dominate all its uses!");
521 dbgs() <<
"Use to update " <<
Use.second <<
":";
525 Use.first->setOperand(
Use.second, LoadedCst);
531void AArch64PromoteConstant::promoteConstants(
532 Function &
F, SmallVectorImpl<UpdateRecord> &Updates,
533 PromotionCacheTy &PromotionCache) {
535 for (
auto U = Updates.
begin(),
E = Updates.
end(); U !=
E;) {
539 InsertionPoints InsertPts;
541 computeInsertionPoint(
U->User,
U->Op, InsertPts);
542 }
while (++U !=
E &&
U->C ==
C);
544 auto &Promotion = PromotionCache[
C];
546 insertDefinitions(
F, *Promotion.GV, InsertPts);
550bool AArch64PromoteConstant::runOnFunction(
Function &
F,
551 PromotionCacheTy &PromotionCache) {
559 for (Use &U :
I.operands()) {
573 unsigned OpNo = &
U -
I.op_begin();
584 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 ...
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.
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()