2004-02-25 21:34:36 +00:00
|
|
|
//===- GlobalConstifier.cpp - Mark read-only globals constant -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass loops over the non-constant internal global variables in the
|
|
|
|
// program. If it can prove that they are never written to, it marks them
|
|
|
|
// constant.
|
|
|
|
//
|
|
|
|
// NOTE: this should eventually use the alias analysis interfaces to do the
|
|
|
|
// transformation, but for now we just stick with a simple solution. DSA in
|
|
|
|
// particular could give a much more accurate answer to the mod/ref query, but
|
|
|
|
// it's not quite ready for this.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Constants.h"
|
2004-02-27 18:09:25 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2004-02-25 21:34:36 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2004-08-14 20:57:17 +00:00
|
|
|
#include <set>
|
2004-02-25 21:34:36 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
Statistic<> NumMarked("constify", "Number of globals marked constant");
|
|
|
|
|
|
|
|
struct Constifier : public Pass {
|
|
|
|
bool run(Module &M);
|
|
|
|
};
|
|
|
|
|
|
|
|
RegisterOpt<Constifier> X("constify", "Global Constifier");
|
|
|
|
}
|
|
|
|
|
|
|
|
Pass *llvm::createGlobalConstifierPass() { return new Constifier(); }
|
|
|
|
|
2004-07-20 03:58:07 +00:00
|
|
|
/// A lot of global constants are stored only in trivially dead setter
|
|
|
|
/// functions. Because we don't want to cycle between globaldce and this pass,
|
|
|
|
/// just do a simple check to catch the common case.
|
|
|
|
static bool ContainingFunctionIsTriviallyDead(Instruction *I) {
|
|
|
|
Function *F = I->getParent()->getParent();
|
|
|
|
if (!F->hasInternalLinkage()) return false;
|
|
|
|
F->removeDeadConstantUsers();
|
|
|
|
return F->use_empty();
|
|
|
|
}
|
|
|
|
|
2004-02-25 21:34:36 +00:00
|
|
|
/// isStoredThrough - Return false if the specified pointer is provably never
|
|
|
|
/// stored through. If we can't tell, we must conservatively assume it might.
|
|
|
|
///
|
2004-08-14 20:57:17 +00:00
|
|
|
static bool isStoredThrough(Value *V, std::set<PHINode*> &PHIUsers) {
|
2004-02-25 21:34:36 +00:00
|
|
|
for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
|
2004-07-18 19:56:20 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) {
|
2004-08-14 20:57:17 +00:00
|
|
|
if (isStoredThrough(CE, PHIUsers))
|
2004-02-25 21:34:36 +00:00
|
|
|
return true;
|
|
|
|
} else if (Instruction *I = dyn_cast<Instruction>(*UI)) {
|
2004-07-20 03:58:07 +00:00
|
|
|
if (!ContainingFunctionIsTriviallyDead(I)) {
|
2004-08-14 20:57:17 +00:00
|
|
|
if (I->getOpcode() == Instruction::GetElementPtr ||
|
|
|
|
I->getOpcode() == Instruction::Select) {
|
|
|
|
if (isStoredThrough(I, PHIUsers)) return true;
|
|
|
|
} else if (PHINode *PN = dyn_cast<PHINode>(I)) {
|
|
|
|
// PHI nodes we can check just like select or GEP instructions, but we
|
|
|
|
// have to be careful about infinite recursion.
|
|
|
|
if (PHIUsers.insert(PN).second) // Not already visited.
|
|
|
|
if (isStoredThrough(I, PHIUsers)) return true;
|
|
|
|
|
|
|
|
} else if (!isa<LoadInst>(I) && !isa<SetCondInst>(I)) {
|
2004-07-20 03:58:07 +00:00
|
|
|
return true; // Any other non-load instruction might store!
|
|
|
|
}
|
|
|
|
}
|
2004-02-25 21:34:36 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise must be a global or some other user.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Constifier::run(Module &M) {
|
|
|
|
bool Changed = false;
|
2004-08-14 20:57:17 +00:00
|
|
|
std::set<PHINode*> PHIUsers;
|
2004-02-25 21:34:36 +00:00
|
|
|
for (Module::giterator GV = M.gbegin(), E = M.gend(); GV != E; ++GV)
|
|
|
|
if (!GV->isConstant() && GV->hasInternalLinkage() && GV->hasInitializer()) {
|
2004-08-14 20:57:17 +00:00
|
|
|
if (!isStoredThrough(GV, PHIUsers)) {
|
2004-02-25 21:34:36 +00:00
|
|
|
DEBUG(std::cerr << "MARKING CONSTANT: " << *GV << "\n");
|
|
|
|
GV->setConstant(true);
|
|
|
|
++NumMarked;
|
|
|
|
Changed = true;
|
|
|
|
}
|
2004-08-14 20:57:17 +00:00
|
|
|
PHIUsers.clear();
|
2004-02-25 21:34:36 +00:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|