29 #define DEBUG_TYPE "ipconstprop" 31 STATISTIC(NumArgumentsProped,
"Number of args turned into constants");
32 STATISTIC(NumReturnValProped,
"Number of return values turned into constants");
43 bool runOnModule(
Module &M)
override;
59 unsigned NumNonconstant = 0;
61 User *UR = U.getUser();
63 if (isa<BlockAddress>(UR))
continue;
73 if (F.
isVarArg() ? ArgumentConstants.
size() > NumActualArgs
74 : ArgumentConstants.
size() != NumActualArgs)
80 for (
unsigned i = 0,
e = ArgumentConstants.
size(); i !=
e; ++i, ++
Arg) {
83 if (ArgumentConstants[i].
second)
87 Constant *
C = dyn_cast_or_null<Constant>(V);
102 if (++NumNonconstant == ArgumentConstants.
size())
105 ArgumentConstants[i].second =
true;
109 if (C && ArgumentConstants[i].
first ==
nullptr) {
110 ArgumentConstants[i].first =
C;
111 }
else if (C && ArgumentConstants[i].
first == C) {
113 }
else if (V == &*Arg) {
118 if (++NumNonconstant == ArgumentConstants.
size())
120 ArgumentConstants[i].second =
true;
126 assert(NumNonconstant != ArgumentConstants.
size());
127 bool MadeChange =
false;
129 for (
unsigned i = 0,
e = ArgumentConstants.
size(); i !=
e; ++i, ++AI) {
135 Value *V = ArgumentConstants[i].first;
138 ++NumArgumentsProped;
178 unsigned NumNonConstant = 0;
180 if (
ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator())) {
181 for (
unsigned i = 0, e = RetVals.
size(); i !=
e; ++i) {
183 Value *RV = RetVals[i];
190 V = RI->getOperand(0);
196 if (isa<UndefValue>(V))
200 if (isa<Constant>(V) || isa<Argument>(V)) {
201 if (isa<UndefValue>(RV)) {
213 RetVals[i] =
nullptr;
215 if (++NumNonConstant == RetVals.
size())
223 bool MadeChange =
false;
224 for (
Use &U : F.uses()) {
230 if (!Call || !CS.isCallee(&U))
234 if (Call->use_empty())
240 Value* New = RetVals[0];
241 if (
Argument *A = dyn_cast<Argument>(New))
244 New = CS.getArgument(A->getArgNo());
249 for (
auto I = Call->user_begin(),
E = Call->user_end();
I !=
E;) {
258 if (EV->getNumIndices() == 1)
259 index = *EV->idx_begin();
264 Value *New = RetVals[index];
266 if (
Argument *A = dyn_cast<Argument>(New))
269 New = CS.getArgument(A->getArgNo());
277 if (MadeChange) ++NumReturnValProped;
283 "Interprocedural constant propagation",
false,
false)
287 bool IPCP::runOnModule(
Module &M) {
291 bool Changed =
false;
292 bool LocalChange =
true;
296 while (LocalChange) {
299 if (!
F.isDeclaration()) {
301 F.removeDeadConstantUsers();
302 if (
F.hasLocalLinkage())
306 Changed |= LocalChange;
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
bool onlyReadsMemory() const
Determine if the function does not access or only reads memory.
Return a value (possibly void), from a function.
void initializeIPCPPass(PassRegistry &)
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
ModulePass * createIPConstantPropagationPass()
createIPConstantPropagationPass - This pass propagates constants from call sites into the bodies of f...
iterator_range< use_iterator > uses()
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents an incoming formal argument to a Function.
static bool PropagateConstantsIntoArguments(Function &F)
PropagateConstantsIntoArguments - Look at all uses of the specified function.
This class represents lattice values for constants.
Type * getElementType(unsigned N) const
A Module instance is used to store all the information related to an LLVM module. ...
unsigned getNumElements() const
Random access to the elements.
void push_back(const T &Elt)
Value * getCallArgOperand(Argument &Arg) const
Return the operand of the underlying instruction associated with Arg.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
STATISTIC(NumFunctions, "Total number of functions")
bool hasByValAttr() const
Return true if this argument has the byval attribute.
Class to represent struct types.
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.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
bool isVoidTy() const
Return true if this is 'void'.
bool isThreadDependent() const
Return true if the value can vary between threads.
Type * getReturnType() const
Returns the type of the ret val.
LLVM Basic Block Representation.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
INITIALIZE_PASS(IPCP, "ipconstprop", "Interprocedural constant propagation", false, false) ModulePass *llvm
This file contains the declarations for the subclasses of Constant, which represent the different fla...
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
bool hasInAllocaAttr() const
Return true if this argument has the inalloca attribute.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
unsigned getNumArgOperands() const
Return the number of parameters of the callee.
Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, Instruction *InsertBefore=nullptr)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
bool isDefinitionExact() const
Return true if the currently visible definition of this global (if any) is exactly the definition we ...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
bool isCallbackCall() const
Return true if this ACS represents a callback call.
static bool PropagateConstantReturn(Function &F)
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.