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,13 +31,11 @@ /// Constant *ConstantFoldInstruction(Instruction *I, const TargetData *TD = 0); -/// 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 +/// SimplifyInstruction - Attempt to do simplifications that don't necessarily +/// results 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, in the same Basic Block. +/// It first tries to constant fold using ConstantFoldInstruction, and only if +/// that failed attempts to apply the simplifications. Value *SimplifyInstruction(Instruction *I, const TargetData *TD = 0); /// ConstantFoldConstantExpression - Attempt to fold the constant expression 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) @@ -29,6 +29,56 @@ // Binary Operators //===--------------------------------------------------------------------===// + Constant *CreateAdd(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getAdd(LHS, RHS); + } + Constant *CreateSub(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getSub(LHS, RHS); + } + Constant *CreateMul(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getMul(LHS, RHS); + } + Constant *CreateUDiv(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getUDiv(LHS, RHS); + } + Constant *CreateSDiv(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getSDiv(LHS, RHS); + } + Constant *CreateFDiv(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getFDiv(LHS, RHS); + } + Constant *CreateURem(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getURem(LHS, RHS); + } + Constant *CreateSRem(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getSRem(LHS, RHS); + } + Constant *CreateFRem(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getFRem(LHS, RHS); + } + Constant *CreateShl(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getShl(LHS, RHS); + } + Constant *CreateLShr(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getLShr(LHS, RHS); + } + Constant *CreateAShr(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getAShr(LHS, RHS); + } + Constant *CreateAnd(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getAnd(LHS, RHS); + } + Constant *CreateOr(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getOr(LHS, RHS); + } + Constant *CreateXor(Constant *LHS, Constant *RHS) const { + return ConstantExpr::getXor(LHS, RHS); + } + + Constant *CreateBinOp(Instruction::BinaryOps Opc, + Constant *LHS, Constant *RHS) const { + return ConstantExpr::get(Opc, LHS, RHS); + } /// Try to find an instruction identical to this one in the current BB, /// and return it. Value *findBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, @@ -37,9 +87,6 @@ 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, and we limit the - // number of uses to inspect. for (Value::use_iterator i = LRHS->use_begin(), e = LRHS->use_end(); i != e && maxUses; ++i, --maxUses) { Instruction *UI = dyn_cast(i); @@ -194,6 +241,12 @@ // Unary Operators //===--------------------------------------------------------------------===// + Constant *CreateNeg(Constant *C) const { + return ConstantExpr::getNeg(C); + } + Constant *CreateNot(Constant *C) const { + return ConstantExpr::getNot(C); + } Value *CreateNeg(Value *V, const BasicBlock *BB) const { if (Constant *C = dyn_cast(V)) return ConstantExpr::getNeg(C); 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) @@ -38,9 +38,9 @@ /// final instruction output. This defaults to on. The second template argument /// specifies a class to use for creating constants. This defaults to creating /// minimally folded constants. -/// If you specify reuseOps, it will try to reuse binops already inserted. Only +/// If you specify reuseOps, it will try to reuse Binops already inserted. Only /// use this if you don't mutate/RAUW instructions after creating them. -/// If you move the insertion point backwards reuseOps will temporarely be +/// If you move the insertion point backwards reuseOps will temporarily be /// disabled until you move it to the end of the BB again. template class IRBuilder { @@ -48,7 +48,7 @@ BasicBlock::iterator InsertPt; T Folder; - // Returns the BB to search for an instruction to reuse, + // Returns the BB in which to search for an instruction to reuse, // or NULL if this is not desired. const BasicBlock *getReuse() const { diff -wu include/llvm/Support/TargetFolder.h include/llvm/Support/TargetFolder.h --- include/llvm/Support/TargetFolder.h (working copy) +++ include/llvm/Support/TargetFolder.h (working copy) @@ -44,7 +44,57 @@ // Binary Operators //===--------------------------------------------------------------------===// - inline Value *CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, + Constant *CreateAdd(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getAdd(LHS, RHS)); + } + Constant *CreateSub(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getSub(LHS, RHS)); + } + Constant *CreateMul(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getMul(LHS, RHS)); + } + Constant *CreateUDiv(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getUDiv(LHS, RHS)); + } + Constant *CreateSDiv(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getSDiv(LHS, RHS)); + } + Constant *CreateFDiv(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getFDiv(LHS, RHS)); + } + Constant *CreateURem(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getURem(LHS, RHS)); + } + Constant *CreateSRem(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getSRem(LHS, RHS)); + } + Constant *CreateFRem(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getFRem(LHS, RHS)); + } + Constant *CreateShl(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getShl(LHS, RHS)); + } + Constant *CreateLShr(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getLShr(LHS, RHS)); + } + Constant *CreateAShr(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getAShr(LHS, RHS)); + } + Constant *CreateAnd(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getAnd(LHS, RHS)); + } + Constant *CreateOr(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getOr(LHS, RHS)); + } + Constant *CreateXor(Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::getXor(LHS, RHS)); + } + + Constant *CreateBinOp(Instruction::BinaryOps Opc, + Constant *LHS, Constant *RHS) const { + return Fold(ConstantExpr::get(Opc, LHS, RHS)); + } + Value *CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const BasicBlock *BB) const { Constant *LC = dyn_cast(LHS); Constant *RC = dyn_cast(RHS); @@ -58,7 +108,14 @@ // Unary Operators //===--------------------------------------------------------------------===// - inline Value *CreateNeg(Value *V, const BasicBlock *BB) const { + Constant *CreateNeg(Constant *C) const { + return Fold(ConstantExpr::getNeg(C)); + } + Constant *CreateNot(Constant *C) const { + return Fold(ConstantExpr::getNot(C)); + } + + Value *CreateNeg(Value *V, const BasicBlock *BB) const { if (Constant *C = dyn_cast(V)) return Fold(ConstantExpr::getNeg(C)); if (BB) { @@ -69,7 +126,7 @@ return 0; } - inline Value *CreateNot(Value *V, const BasicBlock *BB) const { + Value *CreateNot(Value *V, const BasicBlock *BB) const { if (Constant *C = dyn_cast(V)) return Fold(ConstantExpr::getNot(C)); if (BB) {