2003-05-20 23:01:22 +02:00
|
|
|
//===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
2003-05-20 23:01:22 +02:00
|
|
|
// This file implements constant propagation and merging:
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
// Specifically, this:
|
2002-05-06 20:21:31 +02:00
|
|
|
// * Converts instructions like "add int 1, 2" into 3
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
// Notice that:
|
|
|
|
// * This pass has a habit of making definitions be dead. It is a good idea
|
2008-08-01 14:23:49 +02:00
|
|
|
// to run a DIE pass sometime after running this pass.
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 22:40:18 +01:00
|
|
|
#define DEBUG_TYPE "constprop"
|
2002-05-07 22:03:00 +02:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-01-31 00:46:24 +01:00
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Constant.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-03-04 11:30:26 +01:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Instruction.h"
|
2002-02-26 22:46:54 +01:00
|
|
|
#include "llvm/Pass.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Target/TargetLibraryInfo.h"
|
2002-05-06 20:21:31 +02:00
|
|
|
#include <set>
|
2004-01-09 07:02:20 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2006-12-19 22:40:18 +01:00
|
|
|
STATISTIC(NumInstKilled, "Number of instructions killed");
|
2002-10-02 00:38:41 +02:00
|
|
|
|
2006-12-19 22:40:18 +01:00
|
|
|
namespace {
|
2009-09-02 08:11:42 +02:00
|
|
|
struct ConstantPropagation : public FunctionPass {
|
2007-05-06 15:37:16 +02:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-19 19:21:58 +02:00
|
|
|
ConstantPropagation() : FunctionPass(ID) {
|
|
|
|
initializeConstantPropagationPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-05-01 23:15:47 +02:00
|
|
|
|
2014-03-05 10:10:37 +01:00
|
|
|
bool runOnFunction(Function &F) override;
|
2002-04-28 23:27:06 +02:00
|
|
|
|
2014-03-05 10:10:37 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2002-10-21 22:00:28 +02:00
|
|
|
AU.setPreservesCFG();
|
2011-12-01 22:29:16 +01:00
|
|
|
AU.addRequired<TargetLibraryInfo>();
|
2002-04-28 23:27:06 +02:00
|
|
|
}
|
2002-02-26 22:46:54 +01:00
|
|
|
};
|
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
char ConstantPropagation::ID = 0;
|
2011-12-01 22:29:16 +01:00
|
|
|
INITIALIZE_PASS_BEGIN(ConstantPropagation, "constprop",
|
|
|
|
"Simple constant propagation", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
|
|
|
|
INITIALIZE_PASS_END(ConstantPropagation, "constprop",
|
2010-10-08 00:25:06 +02:00
|
|
|
"Simple constant propagation", false, false)
|
2008-05-13 02:00:25 +02:00
|
|
|
|
2004-09-20 06:43:15 +02:00
|
|
|
FunctionPass *llvm::createConstantPropagationPass() {
|
2003-05-20 23:01:22 +02:00
|
|
|
return new ConstantPropagation();
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
2002-02-26 22:46:54 +01:00
|
|
|
|
2003-05-20 23:01:22 +02:00
|
|
|
bool ConstantPropagation::runOnFunction(Function &F) {
|
2002-05-06 20:21:31 +02:00
|
|
|
// Initialize the worklist to all of the instructions ready to process...
|
2004-04-27 17:13:33 +02:00
|
|
|
std::set<Instruction*> WorkList;
|
|
|
|
for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
|
|
|
|
WorkList.insert(&*i);
|
|
|
|
}
|
2002-05-06 20:21:31 +02:00
|
|
|
bool Changed = false;
|
2014-02-25 18:30:31 +01:00
|
|
|
DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
|
|
|
|
const DataLayout *DL = DLP ? &DLP->getDataLayout() : 0;
|
2011-12-01 22:29:16 +01:00
|
|
|
TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
|
2002-05-06 20:21:31 +02:00
|
|
|
|
|
|
|
while (!WorkList.empty()) {
|
|
|
|
Instruction *I = *WorkList.begin();
|
|
|
|
WorkList.erase(WorkList.begin()); // Get an element from the worklist...
|
|
|
|
|
|
|
|
if (!I->use_empty()) // Don't muck with dead instructions...
|
2014-02-21 02:53:35 +01:00
|
|
|
if (Constant *C = ConstantFoldInstruction(I, DL, TLI)) {
|
2002-05-06 20:21:31 +02:00
|
|
|
// Add all of the users of this instruction to the worklist, they might
|
2003-05-20 23:01:22 +02:00
|
|
|
// be constant propagatable now...
|
2002-05-06 20:21:31 +02:00
|
|
|
for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
|
|
|
|
UI != UE; ++UI)
|
|
|
|
WorkList.insert(cast<Instruction>(*UI));
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2002-05-06 20:21:31 +02:00
|
|
|
// Replace all of the uses of a variable with uses of the constant.
|
|
|
|
I->replaceAllUsesWith(C);
|
2002-05-10 17:38:35 +02:00
|
|
|
|
2004-04-13 21:28:20 +02:00
|
|
|
// Remove the dead instruction.
|
|
|
|
WorkList.erase(I);
|
2008-06-22 00:08:46 +02:00
|
|
|
I->eraseFromParent();
|
2004-04-13 21:28:20 +02:00
|
|
|
|
2002-05-06 20:21:31 +02:00
|
|
|
// We made a change to the function...
|
|
|
|
Changed = true;
|
2002-05-10 17:38:35 +02:00
|
|
|
++NumInstKilled;
|
2002-05-06 20:21:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|