diff -wu include/llvm/Analysis/ConstantFolding.h include/llvm/Analysis/ConstantFolding.h --- include/llvm/Analysis/ConstantFolding.h (working copy) +++ include/llvm/Analysis/ConstantFolding.h (working copy) @@ -31,15 +31,14 @@ /// Constant *ConstantFoldInstruction(Instruction *I, const TargetData *TD = 0); -/// ConstantFoldInstruction - Behaves just as ConstantFoldInstruction that -/// returns a Constant* if simplify is false (default). +/// SimplifyInstruction - Behaves just as ConstantFoldInstruction if the +/// Instruction has all Constant operands. /// Otherwise attempt to do simplifications that don't necesarely result in /// a Constant result, but is simpler than the Instruction I /// (such as returning X for add X, 0). /// In case of BinOps, it can also return an identical instruction that is -/// before the current one. -Value *ConstantFoldInstruction(Instruction *I, bool simplify, - const TargetData *TD = 0); +/// before the current one, in the same Basic Block. +Value *SimplifyInstruction(Instruction *I, const TargetData *TD = 0); /// ConstantFoldConstantExpression - Attempt to fold the constant expression /// using the specified TargetData. If successful, the constant result is @@ -57,16 +56,6 @@ Constant*const * Ops, unsigned NumOps, const TargetData *TD = 0); -/// ConstantFoldInstOperands - Behaves just as ConstantFoldInstOperands that -/// take a Constant*const* Ops, if the Ops are constants. -/// Otherwise attempt to do simplifications that don't necesarely result in -/// a Constant result, but is simpler than the instruction would be -/// (such as returning X for add X, 0). -/// This doesn't look for an already existing, identical instruction. -Value *ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy, - Value*const *Ops, unsigned NumOps, - const TargetData *TD = 0); - /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare /// instruction (icmp/fcmp) with the specified operands. If it fails, it /// returns a constant expression of the specified operands. diff -wu include/llvm/Support/IRBuilder.h include/llvm/Support/IRBuilder.h --- include/llvm/Support/IRBuilder.h (working copy) +++ include/llvm/Support/IRBuilder.h (working copy) @@ -21,7 +21,6 @@ #include "llvm/GlobalVariable.h" #include "llvm/Function.h" #include "llvm/Support/ConstantFolder.h" -#include "llvm/Support/NoSimplifier.h" namespace llvm { diff -wu include/llvm/Support/ConstantFolder.h include/llvm/Support/ConstantFolder.h --- include/llvm/Support/ConstantFolder.h (working copy) +++ include/llvm/Support/ConstantFolder.h (working copy) @@ -34,12 +34,14 @@ inline Value *findBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, Value *LRHS, const BasicBlock::iterator *I) const { + unsigned maxUses = 8; // Maximum number of uses to inspect // Loop over all uses of operand to find an identical binop, with // same operands. // There should be less uses than instructions in a BB, so this should be - // faster than iterating over all instructions in BB. + // faster than iterating over all instructions in BB, and we limit the + // number of uses to inspect. for (Value::use_iterator i = LRHS->use_begin(), e = LRHS->use_end(); - i != e; ++i) { + i != e && maxUses; ++i, --maxUses) { Instruction *UI = dyn_cast(i); if (!UI || UI->getOpcode() != static_cast(Opc) || UI->getNumOperands() != 2) @@ -54,10 +56,11 @@ continue; } { - // Only accept instructions before or at the insertion point, - // because we can't move instructions here. + // Only accept instructions before or at the insertion point + // in the same BB, because we can't move instructions here. BasicBlock::iterator P = UI; - if (*I == UI->getParent()->end() || P <= *I) + if (*I == UI->getParent()->end() || + (P <= *I && P > UI->getParent()->begin())) return UI; } } diff -wu lib/Analysis/ConstantFolding.cpp lib/Analysis/ConstantFolding.cpp --- lib/Analysis/ConstantFolding.cpp (working copy) +++ lib/Analysis/ConstantFolding.cpp (working copy) @@ -23,7 +23,7 @@ #include "llvm/Target/TargetData.h" #include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/MathExtras.h" -#include "llvm/Support/TargetFolder.h" +#include "llvm/Support/ConstantFolder.h" #include #include using namespace llvm; @@ -312,15 +312,24 @@ &Ops[0], Ops.size(), TD); } -Value *llvm::ConstantFoldInstruction(Instruction *I, bool simplify, - const TargetData *TD) { - if (!simplify) { - Constant *C = ConstantFoldInstruction(I, TD); - return C; +Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD) { + // Check whether all operands are constant. + Instruction::const_op_iterator i = I->op_begin(), e = I->op_end(); + for (; i != e; ++i) { + if (!isa(i)) + break; } + if (i == e) + return ConstantFoldInstruction(I, TD); + BinaryOperator *BO = dyn_cast(I); - if (!BO) + if (!BO) { + if (const SelectInst *SI = dyn_cast(I)) { + if (const ConstantInt *CB = dyn_cast(SI->getCondition())) + return CB->getZExtValue() ? SI->getTrueValue() : SI->getFalseValue(); + } return 0; + } ConstantFolder CF; BasicBlock::iterator It = BO; @@ -328,30 +337,6 @@ true, &It); } -Value *ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy, - Value*const *Ops, unsigned NumOps, - const TargetData *TD = 0) { - unsigned i; - // Check whether all operands are constant. - for (i=0;i < NumOps; i++) { - if (!isa(Ops[i])) - break; - } - - // If all operands are constant use the specialized constant folder. - if (i == NumOps) - return ConstantFoldInstOperands(Opcode, DestTy, - reinterpret_cast(Ops), - NumOps, TD); - - if (!BinaryOperator::isBinaryOp(Opcode) || NumOps != 2) - return 0; - - ConstantFolder CF; - return CF.CreateBinOp(static_cast(Opcode), Ops[0], - Ops[1], false, 0); -} - /// ConstantFoldConstantExpression - Attempt to fold the constant expression /// using the specified TargetData. If successful, the constant result is /// result is returned, if not, null is returned.