40#define DEBUG_TYPE "tlshoist"
44 cl::desc(
"hoist the TLS loads in PIC model to eliminate redundant "
45 "TLS address calculation."));
74char TLSVariableHoistLegacyPass::ID = 0;
77 "TLS Variable Hoist",
false,
false)
84 return new TLSVariableHoistLegacyPass();
88bool TLSVariableHoistLegacyPass::runOnFunction(
Function &Fn) {
92 LLVM_DEBUG(
dbgs() <<
"********** Begin TLS Variable Hoist **********\n");
96 Impl.runImpl(Fn, getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
97 getAnalysis<LoopInfoWrapperPass>().getLoopInfo());
100 LLVM_DEBUG(
dbgs() <<
"********** Function after TLS Variable Hoist: "
104 LLVM_DEBUG(
dbgs() <<
"********** End TLS Variable Hoist **********\n");
109void TLSVariableHoistPass::collectTLSCandidate(
Instruction *Inst) {
117 if (!GV || !GV->isThreadLocal())
121 TLSCandMap[GV].addUser(Inst,
Idx);
125void TLSVariableHoistPass::collectTLSCandidates(
Function &Fn) {
145 collectTLSCandidate(&Inst);
150 if (Cand.
Users.size() != 1)
162 assert(L &&
"Unexcepted Loop status!");
165 while (
Loop *Parent =
L->getParentLoop())
179 assert(Dom &&
"Not find dominator BB!");
204 Pos = getNearestLoopDomInst(BB, L);
205 assert(Pos &&
"Not find insert position out of loop!");
207 Pos = getDomInst(LastPos, Pos);
211 assert(LastPos &&
"Unexpected insert position!");
228bool TLSVariableHoistPass::tryReplaceTLSCandidate(
Function &Fn,
238 auto *
CastInst = genBitCastInst(Fn, GV);
247bool TLSVariableHoistPass::tryReplaceTLSCandidates(
Function &Fn) {
248 if (TLSCandMap.
empty())
251 bool Replaced =
false;
252 for (
auto &GV2Cand : TLSCandMap) {
254 Replaced |= tryReplaceTLSCandidate(Fn, GV);
271 assert(this->LI && this->DT &&
"Unexcepted requirement!");
274 collectTLSCandidates(Fn);
276 bool MadeChange = tryReplaceTLSCandidates(Fn);
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
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)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
TLS Variable Hoist
When an instruction is found to use only loop invariant operands that are safe to hoist,...
static bool oneUseOutsideLoop(tlshoist::TLSCandidate &Cand, LoopInfo *LI)
static cl::opt< bool > TLSLoadHoist("tls-load-hoist", cl::init(false), cl::Hidden, cl::desc("hoist the TLS loads in PIC model to eliminate redundant " "TLS address calculation."))
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
bool hasFnAttr(Attribute::AttrKind Kind) const
Return true if the attribute exists for the function.
LLVM Basic Block Representation.
InstListType::iterator iterator
Instruction iterators...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
This class represents a no-op cast from one type to another.
Represents analyses that only rely on functions' control flow.
This is the base class for all instructions that perform data casts.
Analysis pass which computes a DominatorTree.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
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...
FunctionPass class - This class is used to implement most global optimizations.
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
const BasicBlock & getEntryBlock() const
AttributeList getAttributes() const
Return the attribute list for this Function.
bool hasOptNone() const
Do not optimize this function (-O0).
Module * getParent()
Get the module that this global value is contained inside of...
PointerType * getType() const
Global values are always pointers.
InstListType::iterator insertInto(BasicBlock *ParentBB, InstListType::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
Analysis pass that exposes the LoopInfo for a function.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
The legacy pass manager's analysis pass to compute loop information.
Represents a single loop in the control flow graph.
A Module instance is used to store all the information related to an LLVM module.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void preserveSet()
Mark an analysis set as preserved.
StringRef - Represent a constant reference to a string, i.e.
bool runImpl(Function &F, DominatorTree &DT, LoopInfo &LI)
Optimize expensive TLS variables in the given function.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
The instances of the Type class are immutable: once they are created, they are never changed.
void setOperand(unsigned i, Value *Val)
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
StringRef getName() const
Return a constant reference to the value's name.
const ParentTy * getParent() const
self_iterator getIterator()
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createTLSVariableHoistPass()
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
auto predecessors(const MachineBasicBlock *BB)
void initializeTLSVariableHoistLegacyPassPass(PassRegistry &)
Keeps track of a TLS variable candidate and its users.
SmallVector< TLSUser, 8 > Users