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;
67 if (!isa<CallInst>(UR) && !isa<InvokeInst>(UR))
78 for (
unsigned i = 0, e = ArgumentConstants.
size(); i != e;
82 if (ArgumentConstants[i].
second)
86 if (C && ArgumentConstants[i].
first ==
nullptr) {
87 ArgumentConstants[i].first =
C;
88 }
else if (C && ArgumentConstants[i].
first == C) {
90 }
else if (*AI == &*Arg) {
95 if (++NumNonconstant == ArgumentConstants.
size())
97 ArgumentConstants[i].second =
true;
103 assert(NumNonconstant != ArgumentConstants.
size());
104 bool MadeChange =
false;
106 for (
unsigned i = 0, e = ArgumentConstants.
size(); i != e; ++i, ++AI) {
112 Value *V = ArgumentConstants[i].first;
115 ++NumArgumentsProped;
155 unsigned NumNonConstant = 0;
157 if (
ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator())) {
158 for (
unsigned i = 0, e = RetVals.
size(); i != e; ++i) {
160 Value *RV = RetVals[i];
167 V = RI->getOperand(0);
173 if (isa<UndefValue>(V))
177 if (isa<Constant>(V) || isa<Argument>(V)) {
178 if (isa<UndefValue>(RV)) {
190 RetVals[i] =
nullptr;
192 if (++NumNonConstant == RetVals.
size())
200 bool MadeChange =
false;
201 for (
Use &U : F.uses()) {
207 if (!Call || !CS.isCallee(&U))
211 if (Call->use_empty())
217 Value* New = RetVals[0];
218 if (
Argument *
A = dyn_cast<Argument>(New))
221 New = CS.getArgument(
A->getArgNo());
222 Call->replaceAllUsesWith(New);
226 for (
auto I = Call->user_begin(),
E = Call->user_end();
I !=
E;) {
235 if (EV->hasIndices())
236 index = *EV->idx_begin();
241 Value *New = RetVals[index];
243 if (
Argument *
A = dyn_cast<Argument>(New))
246 New = CS.getArgument(
A->getArgNo());
254 if (MadeChange) ++NumReturnValProped;
260 "Interprocedural constant propagation",
false,
false)
264 bool IPCP::runOnModule(
Module &M) {
268 bool Changed =
false;
269 bool LocalChange =
true;
273 while (LocalChange) {
276 if (!
F.isDeclaration()) {
278 F.removeDeadConstantUsers();
279 if (
F.hasLocalLinkage())
283 Changed |= LocalChange;
Return a value (possibly void), from a function.
void initializeIPCPPass(PassRegistry &)
User::op_iterator arg_iterator
The type of iterator to use when looping over actual arguments at this call site. ...
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void push_back(const T &Elt)
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.
STATISTIC(NumFunctions,"Total number of functions")
bool onlyReadsMemory() const
Determine if the function does not access or only reads memory.
INITIALIZE_PASS(IPCP,"ipconstprop","Interprocedural constant propagation", false, false) ModulePass *llvm
A Module instance is used to store all the information related to an LLVM module. ...
Class to represent struct types.
A Use represents the edge between a Value definition and its users.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Type * getReturnType() const
Returns the type of the ret val.
LLVM Basic Block Representation.
Type * getElementType(unsigned N) const
This is an important base class in LLVM.
bool hasInAllocaAttr() const
Return true if this argument has the inalloca attribute.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isDefinitionExact() const
Return true if the currently visible definition of this global (if any) is exactly the definition we ...
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, Instruction *InsertBefore=nullptr)
Given an aggregrate and an sequence of indices, see if the scalar value indexed is already around as ...
bool hasByValAttr() const
Return true if this argument has the byval attribute.
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.
Type * getType() const
All values are typed, get the type of this value.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
static bool PropagateConstantReturn(Function &F)
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
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.
unsigned getNumElements() const
Random access to the elements.
bool isCallee(Value::const_user_iterator UI) const
Determine whether the passed iterator points to the callee operand's Use.
bool isVoidTy() const
Return true if this is 'void'.