37 #define DEBUG_NAME "compiler2/ConstantPropagationPass"
41 "constantpropagationpass","constantpropagationpass",compiler2_stat)
43 "number of nodes which could be folded",compiler2_constantpropagationpass_stat)
45 "number of PHIInsts which could be replaced",compiler2_constantpropagationpass_stat)
53 template <
typename T, Instruction::InstID ID>
54 struct BinaryOperation :
public std::binary_function<T,T,T> {
55 T operator()(
const T &lhs,
const T &rhs)
const ;
60 struct BinaryOperation<T, Instruction::ADDInstID> :
61 public std::binary_function<T,T,T> {
69 struct BinaryOperation<T, Instruction::SUBInstID> :
70 public std::binary_function<T,T,T> {
78 struct BinaryOperation<T, Instruction::MULInstID> :
79 public std::binary_function<T,T,T> {
87 struct BinaryOperation<T, Instruction::DIVInstID> :
88 public std::binary_function<T,T,T> {
96 struct BinaryOperation<T, Instruction::ANDInstID> :
97 public std::binary_function<T,T,T> {
104 template <
typename T>
116 assert(0 &&
"not implemented");
128 switch (inst->get_type()) {
131 op1->get_Int(), op2->get_Int()), Type::IntType());
134 op1->get_Long(), op2->get_Long()), Type::LongType());
137 op1->get_Float(), op2->get_Float()), Type::FloatType());
140 op1->get_Double(), op2->get_Double()), Type::DoubleType());
148 template <
typename T, Instruction::InstID ID>
150 T operator()(
const T &
op)
const;
154 template <
typename T>
156 public std::unary_function<T,T> {
163 template <
typename T>
169 assert(0 &&
"not implemented");
178 CONSTInst *
op = inst->get_operand(0)->to_Instruction()->to_CONSTInst();
180 assert(inst->get_type() == op->get_type());
182 switch (inst->get_type()) {
185 op->get_Int()), Type::IntType());
188 op->get_Long()), Type::LongType());
191 op->get_Float()), Type::FloatType());
194 op->get_Double()), Type::DoubleType());
201 template <
typename T>
203 switch (inst->get_type()) {
205 return new CONSTInst((int32_t) op, Type::IntType());
207 return new CONSTInst((int64_t) op, Type::LongType());
209 return new CONSTInst((
float) op, Type::FloatType());
211 return new CONSTInst((
double) op, Type::DoubleType());
219 CONSTInst *
op = inst->get_operand(0)->to_Instruction()->to_CONSTInst();
222 switch (op->get_type()) {
241 }
else if (inst->to_UnaryInst()) {
243 }
else if (inst->to_BinaryInst()) {
251 for (Value::UserListTy::const_iterator
i = inst->
user_begin(),
267 LOG(
"replace " << inst <<
" by " << c <<
nl);
273 assert(inst->op_size() > 0);
274 CONSTInst *firstOp = inst->get_operand(0)->to_Instruction()->to_CONSTInst();
276 e = inst->op_end();
i !=
e;
i++) {
277 CONSTInst *
op = (*i)->to_Instruction()->to_CONSTInst();
280 switch (inst->get_type()) {
282 equal = firstOp->get_Int() == op->get_Int();
285 equal = firstOp->get_Long() == op->get_Long();
288 equal = firstOp->get_Float() == op->get_Float();
291 equal = firstOp->get_Double() == op->get_Double();
305 return inst->is_arithmetic()
Template specialization for NEGInstID.
#define STATISTICS(x)
Wrapper for statistics only code.
Value * get_operand(size_t i)
virtual Instruction * to_Instruction()
#define STAT_REGISTER_SUBGROUP(var, name, description, group)
Register a statistics group and add it to a group.
Template specialization for DIVInstID.
virtual bool run(JITData &JD)
Run the Pass.
UserListTy::const_iterator user_begin() const
virtual CONSTInst * to_CONSTInst()
WorkListTy workList
This work list is used by the algorithm to store the instructions which have to be reconsidered...
T operator()(const T &lhs, const T &rhs) const
T operator()(const T &lhs, const T &rhs) const
T operator()(const T &lhs, const T &rhs) const
UserListTy::const_iterator user_end() const
static Option< bool > enabled
InstBoolMapTy inWorkList
will be used to look up whether an instruction is currently contained in the worklist to avoid insert...
InstIntMapTy constantOperands
used to track for each instruction the number of its operands which are already known to be constant ...
OperandListTy::const_iterator const_op_iterator
T operator()(const T &op) const
void add_schedule_before()
Schedule before PassName.
InstID get_opcode() const
return the opcode of the instruction
CONSTInst * foldBinaryInst(BinaryInst *inst)
CONSTInst * foldUnaryInst(UnaryInst *inst)
T operator()(const T &lhs, const T &rhs) const
void replace_value(Value *v)
Replace this Value this the new one in all users.
T unaryOperate(Instruction::InstID ID, const T &op)
function wrapper for UnaryOperation
Template specialization for MULInstID.
Template specialization for SUBInstID.
Template specialization for ADDInstID.
Stores the interdependencies of a pass.
virtual PassUsage & get_PassUsage(PassUsage &PA) const
Set the requirements for the pass.
#define STAT_REGISTER_GROUP_VAR(type, var, init, name, description, group)
Register an statistics variable and add it to a group.
bool has_only_constant_operands(Instruction *inst)
void add_Instruction(Instruction *I)
Add instructions to a Method.
T operator()(const T &lhs, const T &rhs) const
GlobalValueNumberingPass.
InstructionListTy::const_iterator const_iterator
const_iterator end() const
bool can_be_statically_evaluated(Instruction *inst)
CONSTInst * foldInstruction(Instruction *inst)
#define LOG(STMT)
Analogous to DEBUG.
CONSTInst * castOperate(CASTInst *inst, const T &op)
UnaryOperation function object class.
#define STAT_DECLARE_GROUP(var)
Declare an external group (or subgroup).
const_iterator begin() const
T binaryOperate(Instruction::InstID ID, const T &lhs, const T &rhs)
function wrapper for BinaryOperation
CONSTInst * foldCASTInst(CASTInst *inst)
void propagate(Instruction *inst)
static PassRegistry< BasicBlockSchedulingPass > X("BasicBlockSchedulingPass")
void replace_by_constant(Instruction *isnt, CONSTInst *c, Method *M)
void add_requires()
PassName is required.