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