2001-06-06 20:29:01 +00:00
|
|
|
//===- ConstantProp.cpp - Code to perform Constant Propogation ------------===//
|
|
|
|
//
|
|
|
|
// This file implements constant propogation and merging:
|
|
|
|
//
|
|
|
|
// Specifically, this:
|
|
|
|
// * Folds multiple identical constants in the constant pool together
|
|
|
|
// Note that if one is named and the other is not, that the result gets the
|
|
|
|
// original name.
|
|
|
|
// * Converts instructions like "add int %1, %2" into a direct def of %3 in
|
|
|
|
// the constant pool
|
|
|
|
// * Converts conditional branches on a constant boolean value into direct
|
|
|
|
// branches.
|
|
|
|
// * Converts phi nodes with one incoming def to the incoming def directly
|
|
|
|
// . Converts switch statements with one entry into a test & conditional
|
|
|
|
// branch
|
|
|
|
// . Converts switches on constant values into an unconditional branch.
|
|
|
|
//
|
|
|
|
// Notice that:
|
|
|
|
// * This pass has a habit of making definitions be dead. It is a good idea
|
|
|
|
// to to run a DCE pass sometime after running this pass.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-01-21 23:17:48 +00:00
|
|
|
#include "llvm/Transforms/Scalar/ConstantProp.h"
|
|
|
|
#include "llvm/Transforms/Scalar/ConstantHandling.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/Module.h"
|
2002-04-07 20:49:59 +00:00
|
|
|
#include "llvm/Function.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/iTerminators.h"
|
2001-12-03 18:02:31 +00:00
|
|
|
#include "llvm/iPHINode.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/iOther.h"
|
2002-02-26 21:46:54 +00:00
|
|
|
#include "llvm/Pass.h"
|
2001-12-03 22:26:30 +00:00
|
|
|
#include "llvm/ConstantVals.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
inline static bool
|
2001-11-26 18:57:12 +00:00
|
|
|
ConstantFoldUnaryInst(BasicBlock *BB, BasicBlock::iterator &II,
|
2001-12-03 22:26:30 +00:00
|
|
|
UnaryOperator *Op, Constant *D) {
|
2002-01-21 23:17:48 +00:00
|
|
|
Constant *ReplaceWith = ConstantFoldUnaryInstruction(Op->getOpcode(), D);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
if (!ReplaceWith) return false; // Nothing new to change...
|
|
|
|
|
|
|
|
// Replaces all of the uses of a variable with uses of the constant.
|
|
|
|
Op->replaceAllUsesWith(ReplaceWith);
|
|
|
|
|
|
|
|
// Remove the operator from the list of definitions...
|
2001-11-26 18:57:12 +00:00
|
|
|
Op->getParent()->getInstList().remove(II);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// The new constant inherits the old name of the operator...
|
2001-09-07 16:41:30 +00:00
|
|
|
if (Op->hasName())
|
2001-11-26 18:57:12 +00:00
|
|
|
ReplaceWith->setName(Op->getName(), BB->getParent()->getSymbolTableSure());
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Delete the operator now...
|
|
|
|
delete Op;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-10-31 05:07:57 +00:00
|
|
|
inline static bool
|
2001-11-26 18:57:12 +00:00
|
|
|
ConstantFoldCast(BasicBlock *BB, BasicBlock::iterator &II,
|
2001-12-03 22:26:30 +00:00
|
|
|
CastInst *CI, Constant *D) {
|
2002-01-21 23:17:48 +00:00
|
|
|
Constant *ReplaceWith = ConstantFoldCastInstruction(D, CI->getType());
|
2001-10-31 05:07:57 +00:00
|
|
|
|
|
|
|
if (!ReplaceWith) return false; // Nothing new to change...
|
|
|
|
|
|
|
|
// Replaces all of the uses of a variable with uses of the constant.
|
|
|
|
CI->replaceAllUsesWith(ReplaceWith);
|
|
|
|
|
|
|
|
// Remove the cast from the list of definitions...
|
2001-11-26 18:57:12 +00:00
|
|
|
CI->getParent()->getInstList().remove(II);
|
2001-10-31 05:07:57 +00:00
|
|
|
|
|
|
|
// The new constant inherits the old name of the cast...
|
|
|
|
if (CI->hasName())
|
2001-11-26 18:57:12 +00:00
|
|
|
ReplaceWith->setName(CI->getName(), BB->getParent()->getSymbolTableSure());
|
2001-10-31 05:07:57 +00:00
|
|
|
|
|
|
|
// Delete the cast now...
|
|
|
|
delete CI;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
inline static bool
|
2001-11-26 18:57:12 +00:00
|
|
|
ConstantFoldBinaryInst(BasicBlock *BB, BasicBlock::iterator &II,
|
2001-06-06 20:29:01 +00:00
|
|
|
BinaryOperator *Op,
|
2001-12-03 22:26:30 +00:00
|
|
|
Constant *D1, Constant *D2) {
|
2002-01-21 23:17:48 +00:00
|
|
|
Constant *ReplaceWith = ConstantFoldBinaryInstruction(Op->getOpcode(), D1,D2);
|
2001-06-06 20:29:01 +00:00
|
|
|
if (!ReplaceWith) return false; // Nothing new to change...
|
|
|
|
|
|
|
|
// Replaces all of the uses of a variable with uses of the constant.
|
|
|
|
Op->replaceAllUsesWith(ReplaceWith);
|
|
|
|
|
|
|
|
// Remove the operator from the list of definitions...
|
2001-11-26 18:57:12 +00:00
|
|
|
Op->getParent()->getInstList().remove(II);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// The new constant inherits the old name of the operator...
|
2001-09-07 16:41:30 +00:00
|
|
|
if (Op->hasName())
|
2001-11-26 18:57:12 +00:00
|
|
|
ReplaceWith->setName(Op->getName(), BB->getParent()->getSymbolTableSure());
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Delete the operator now...
|
|
|
|
delete Op;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-06-29 23:56:58 +00:00
|
|
|
// ConstantFoldTerminator - If a terminator instruction is predicated on a
|
|
|
|
// constant value, convert it into an unconditional branch to the constant
|
|
|
|
// destination.
|
|
|
|
//
|
2002-03-11 22:11:07 +00:00
|
|
|
bool ConstantFoldTerminator(BasicBlock *BB, BasicBlock::iterator &II,
|
|
|
|
TerminatorInst *T) {
|
2001-06-06 20:29:01 +00:00
|
|
|
// Branch - See if we are conditional jumping on constant
|
2001-10-02 03:41:24 +00:00
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
|
2001-06-29 23:56:58 +00:00
|
|
|
if (BI->isUnconditional()) return false; // Can't optimize uncond branch
|
2001-10-01 16:18:37 +00:00
|
|
|
BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
|
|
|
|
BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
|
2001-06-29 23:56:58 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
if (ConstantBool *Cond = dyn_cast<ConstantBool>(BI->getCondition())) {
|
2001-10-01 20:11:19 +00:00
|
|
|
// Are we branching on constant?
|
2001-06-06 20:29:01 +00:00
|
|
|
// YES. Change to unconditional branch...
|
2001-06-29 23:56:58 +00:00
|
|
|
BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
|
|
|
|
BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1;
|
2001-06-29 05:23:10 +00:00
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
//cerr << "Function: " << T->getParent()->getParent()
|
2001-06-29 23:56:58 +00:00
|
|
|
// << "\nRemoving branch from " << T->getParent()
|
|
|
|
// << "\n\nTo: " << OldDest << endl;
|
2001-06-29 05:23:10 +00:00
|
|
|
|
|
|
|
// Let the basic block know that we are letting go of it. Based on this,
|
|
|
|
// it will adjust it's PHI nodes.
|
2001-06-29 23:56:58 +00:00
|
|
|
assert(BI->getParent() && "Terminator not inserted in block!");
|
|
|
|
OldDest->removePredecessor(BI->getParent());
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-07-07 08:36:50 +00:00
|
|
|
// Set the unconditional destination, and change the insn to be an
|
|
|
|
// unconditional branch.
|
|
|
|
BI->setUnconditionalDest(Destination);
|
2002-03-11 22:11:07 +00:00
|
|
|
II = BB->end()-1; // Update instruction iterator!
|
2001-06-29 23:56:58 +00:00
|
|
|
return true;
|
2001-09-07 16:41:30 +00:00
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
// FIXME: TODO: This doesn't work if the destination has PHI nodes with
|
|
|
|
// different incoming values on each branch!
|
|
|
|
//
|
|
|
|
else if (Dest2 == Dest1) { // Conditional branch to same location?
|
2001-06-29 23:56:58 +00:00
|
|
|
// This branch matches something like this:
|
|
|
|
// br bool %cond, label %Dest, label %Dest
|
|
|
|
// and changes it into: br label %Dest
|
|
|
|
|
|
|
|
// Let the basic block know that we are letting go of one copy of it.
|
|
|
|
assert(BI->getParent() && "Terminator not inserted in block!");
|
|
|
|
Dest1->removePredecessor(BI->getParent());
|
|
|
|
|
2001-07-07 08:36:50 +00:00
|
|
|
// Change a conditional branch to unconditional.
|
|
|
|
BI->setUnconditionalDest(Dest1);
|
2001-06-06 20:29:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
2001-09-07 16:41:30 +00:00
|
|
|
#endif
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConstantFoldInstruction - If an instruction references constants, try to fold
|
|
|
|
// them together...
|
|
|
|
//
|
2002-02-26 21:46:54 +00:00
|
|
|
bool doConstantPropogation(BasicBlock *BB, BasicBlock::iterator &II) {
|
2001-06-06 20:29:01 +00:00
|
|
|
Instruction *Inst = *II;
|
2002-01-20 22:54:45 +00:00
|
|
|
if (isa<BinaryOperator>(Inst)) {
|
2001-12-03 22:26:30 +00:00
|
|
|
Constant *D1 = dyn_cast<Constant>(Inst->getOperand(0));
|
|
|
|
Constant *D2 = dyn_cast<Constant>(Inst->getOperand(1));
|
2001-06-27 23:35:26 +00:00
|
|
|
|
|
|
|
if (D1 && D2)
|
2001-11-26 18:57:12 +00:00
|
|
|
return ConstantFoldBinaryInst(BB, II, cast<BinaryOperator>(Inst), D1, D2);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-10-31 05:07:57 +00:00
|
|
|
} else if (CastInst *CI = dyn_cast<CastInst>(Inst)) {
|
2001-12-03 22:26:30 +00:00
|
|
|
Constant *D = dyn_cast<Constant>(CI->getOperand(0));
|
2001-11-26 18:57:12 +00:00
|
|
|
if (D) return ConstantFoldCast(BB, II, CI, D);
|
2001-10-31 05:07:57 +00:00
|
|
|
|
2001-10-02 03:41:24 +00:00
|
|
|
} else if (UnaryOperator *UInst = dyn_cast<UnaryOperator>(Inst)) {
|
2001-12-03 22:26:30 +00:00
|
|
|
Constant *D = dyn_cast<Constant>(UInst->getOperand(0));
|
2001-11-26 18:57:12 +00:00
|
|
|
if (D) return ConstantFoldUnaryInst(BB, II, UInst, D);
|
2001-10-02 03:41:24 +00:00
|
|
|
} else if (TerminatorInst *TInst = dyn_cast<TerminatorInst>(Inst)) {
|
2002-03-11 22:11:07 +00:00
|
|
|
return ConstantFoldTerminator(BB, II, TInst);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-10-02 03:41:24 +00:00
|
|
|
} else if (PHINode *PN = dyn_cast<PHINode>(Inst)) {
|
|
|
|
// If it's a PHI node and only has one operand
|
|
|
|
// Then replace it directly with that operand.
|
2001-12-13 00:45:40 +00:00
|
|
|
assert(PN->getNumOperands() && "PHI Node must have at least one operand!");
|
2001-07-07 08:36:50 +00:00
|
|
|
if (PN->getNumOperands() == 1) { // If the PHI Node has exactly 1 operand
|
2001-06-06 20:29:01 +00:00
|
|
|
Value *V = PN->getOperand(0);
|
|
|
|
PN->replaceAllUsesWith(V); // Replace all uses of this PHI
|
|
|
|
// Unlink from basic block
|
2001-11-26 18:57:12 +00:00
|
|
|
PN->getParent()->getInstList().remove(II);
|
2001-09-07 16:41:30 +00:00
|
|
|
if (PN->hasName()) // Inherit PHINode name
|
2001-11-26 18:57:12 +00:00
|
|
|
V->setName(PN->getName(), BB->getParent()->getSymbolTableSure());
|
2001-06-06 20:29:01 +00:00
|
|
|
delete PN; // Finally, delete the node...
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// DoConstPropPass - Propogate constants and do constant folding on instructions
|
|
|
|
// this returns true if something was changed, false if nothing was changed.
|
|
|
|
//
|
2002-04-07 20:49:59 +00:00
|
|
|
static bool DoConstPropPass(Function *F) {
|
2001-06-06 20:29:01 +00:00
|
|
|
bool SomethingChanged = false;
|
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
for (Method::iterator BBI = F->begin(); BBI != F->end(); ++BBI) {
|
2001-11-26 18:57:12 +00:00
|
|
|
BasicBlock *BB = *BBI;
|
|
|
|
for (BasicBlock::iterator I = BB->begin(); I != BB->end(); )
|
2002-02-26 21:46:54 +00:00
|
|
|
if (doConstantPropogation(BB, I))
|
2001-11-26 18:57:12 +00:00
|
|
|
SomethingChanged = true;
|
|
|
|
else
|
|
|
|
++I;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
return SomethingChanged;
|
|
|
|
}
|
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
namespace {
|
|
|
|
struct ConstantPropogation : public MethodPass {
|
2002-04-07 20:49:59 +00:00
|
|
|
inline bool runOnMethod(Function *F) {
|
2002-02-26 21:46:54 +00:00
|
|
|
bool Modified = false;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
// Fold constants until we make no progress...
|
2002-04-07 20:49:59 +00:00
|
|
|
while (DoConstPropPass(F)) Modified = true;
|
2002-02-26 21:46:54 +00:00
|
|
|
|
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
Pass *createConstantPropogationPass() {
|
|
|
|
return new ConstantPropogation();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
|