[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp updated: 1.20 - 1.21 --- Log message: For PR1297: http://llvm.org/PR1297 : Support overloaded intrinsics bswap, ctpop, cttz, ctlz. --- Diffs of the changes: (+17 -23) ConstantFolding.cpp | 40 +--- 1 files changed, 17 insertions(+), 23 deletions(-) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -u llvm/lib/Analysis/ConstantFolding.cpp:1.20 llvm/lib/Analysis/ConstantFolding.cpp:1.21 --- llvm/lib/Analysis/ConstantFolding.cpp:1.20 Sun Mar 4 18:00:41 2007 +++ llvm/lib/Analysis/ConstantFolding.cpp Sun Apr 1 02:35:23 2007 @@ -317,24 +317,12 @@ switch (F-getIntrinsicID()) { case Intrinsic::sqrt_f32: case Intrinsic::sqrt_f64: - case Intrinsic::bswap_i16: - case Intrinsic::bswap_i32: - case Intrinsic::bswap_i64: case Intrinsic::powi_f32: case Intrinsic::powi_f64: - // FIXME: these should be constant folded as well - //case Intrinsic::ctpop_i8: - //case Intrinsic::ctpop_i16: - //case Intrinsic::ctpop_i32: - //case Intrinsic::ctpop_i64: - //case Intrinsic::ctlz_i8: - //case Intrinsic::ctlz_i16: - //case Intrinsic::ctlz_i32: - //case Intrinsic::ctlz_i64: - //case Intrinsic::cttz_i8: - //case Intrinsic::cttz_i16: - //case Intrinsic::cttz_i32: - //case Intrinsic::cttz_i64: + case Intrinsic::bswap: + case Intrinsic::ctpop: + case Intrinsic::ctlz: + case Intrinsic::cttz: return true; default: break; } @@ -445,13 +433,19 @@ break; } } else if (ConstantInt *Op = dyn_castConstantInt(Operands[0])) { - uint64_t V = Op-getZExtValue(); - if (Name == llvm.bswap.i16) -return ConstantInt::get(Ty, ByteSwap_16(V)); - else if (Name == llvm.bswap.i32) -return ConstantInt::get(Ty, ByteSwap_32(V)); - else if (Name == llvm.bswap.i64) -return ConstantInt::get(Ty, ByteSwap_64(V)); + const IntegerType *OpTy = castIntegerType(Op-getType()); + if (Name.size() 11 !memcmp(Name[0], llvm.bswap, 10)) { +return ConstantInt::get(Op-getValue().byteSwap()); + } else if (Name.size() 11 !memcmp(Name[0],llvm.ctpop,10)) { +uint64_t ctpop = Op-getValue().countPopulation(); +return ConstantInt::get(OpTy, ctpop); + } else if (Name.size() 10 !memcmp(Name[0], llvm.cttz, 9)) { +uint64_t cttz = Op-getValue().countTrailingZeros(); +return ConstantInt::get(OpTy, cttz); + } else if (Name.size() 10 !memcmp(Name[0], llvm.ctlz, 9)) { +uint64_t ctlz = Op-getValue().countLeadingZeros(); +return ConstantInt::get(OpTy, ctlz); + } } } else if (NumOperands == 2) { if (ConstantFP *Op1 = dyn_castConstantFP(Operands[0])) { ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp updated: 1.21 - 1.22 --- Log message: The bit counting intrinsics return i32 not the operand type. This fixes last night's regression in SingleSource/UnitTests/2005-05-11-Popcount-ffs-fls --- Diffs of the changes: (+3 -4) ConstantFolding.cpp |7 +++ 1 files changed, 3 insertions(+), 4 deletions(-) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -u llvm/lib/Analysis/ConstantFolding.cpp:1.21 llvm/lib/Analysis/ConstantFolding.cpp:1.22 --- llvm/lib/Analysis/ConstantFolding.cpp:1.21 Sun Apr 1 02:35:23 2007 +++ llvm/lib/Analysis/ConstantFolding.cpp Sun Apr 1 13:42:20 2007 @@ -433,18 +433,17 @@ break; } } else if (ConstantInt *Op = dyn_castConstantInt(Operands[0])) { - const IntegerType *OpTy = castIntegerType(Op-getType()); if (Name.size() 11 !memcmp(Name[0], llvm.bswap, 10)) { return ConstantInt::get(Op-getValue().byteSwap()); } else if (Name.size() 11 !memcmp(Name[0],llvm.ctpop,10)) { uint64_t ctpop = Op-getValue().countPopulation(); -return ConstantInt::get(OpTy, ctpop); +return ConstantInt::get(Type::Int32Ty, ctpop); } else if (Name.size() 10 !memcmp(Name[0], llvm.cttz, 9)) { uint64_t cttz = Op-getValue().countTrailingZeros(); -return ConstantInt::get(OpTy, cttz); +return ConstantInt::get(Type::Int32Ty, cttz); } else if (Name.size() 10 !memcmp(Name[0], llvm.ctlz, 9)) { uint64_t ctlz = Op-getValue().countLeadingZeros(); -return ConstantInt::get(OpTy, ctlz); +return ConstantInt::get(Type::Int32Ty, ctlz); } } } else if (NumOperands == 2) { ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp updated: 1.19 - 1.20 --- Log message: Unbreak VC++ build. --- Diffs of the changes: (+2 -2) ConstantFolding.cpp |4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -u llvm/lib/Analysis/ConstantFolding.cpp:1.19 llvm/lib/Analysis/ConstantFolding.cpp:1.20 --- llvm/lib/Analysis/ConstantFolding.cpp:1.19 Wed Feb 14 20:26:09 2007 +++ llvm/lib/Analysis/ConstantFolding.cpp Sun Mar 4 18:00:41 2007 @@ -72,8 +72,8 @@ // N = N + Offset Offset += TD.getStructLayout(ST)-getElementOffset(CI-getZExtValue()); } else { -const SequentialType *ST = castSequentialType(*GTI); -Offset += TD.getTypeSize(ST-getElementType())*CI-getSExtValue(); +const SequentialType *SQT = castSequentialType(*GTI); +Offset += TD.getTypeSize(SQT-getElementType())*CI-getSExtValue(); } } return true; ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp updated: 1.16 - 1.17 --- Log message: Privatize StructLayout::MemberOffsets, adding an accessor --- Diffs of the changes: (+1 -1) ConstantFolding.cpp |2 +- 1 files changed, 1 insertion(+), 1 deletion(-) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -u llvm/lib/Analysis/ConstantFolding.cpp:1.16 llvm/lib/Analysis/ConstantFolding.cpp:1.17 --- llvm/lib/Analysis/ConstantFolding.cpp:1.16 Thu Feb 1 20:16:21 2007 +++ llvm/lib/Analysis/ConstantFolding.cpp Sat Feb 10 13:55:17 2007 @@ -70,7 +70,7 @@ if (const StructType *ST = dyn_castStructType(*GTI)) { // N = N + Offset -Offset += TD.getStructLayout(ST)-MemberOffsets[CI-getZExtValue()]; +Offset += TD.getStructLayout(ST)-getElementOffset(CI-getZExtValue()); } else { const SequentialType *ST = castSequentialType(*GTI); Offset += TD.getTypeSize(ST-getElementType())*CI-getSExtValue(); ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp ScalarEvolution.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp updated: 1.15 - 1.16 ScalarEvolution.cpp updated: 1.92 - 1.93 --- Log message: Changes to support making the shift instructions be true BinaryOperators. This feature is needed in order to support shifts of more than 255 bits on large integer types. This changes the syntax for llvm assembly to make shl, ashr and lshr instructions look like a binary operator: shl i32 %X, 1 instead of shl i32 %X, i8 1 Additionally, this should help a few passes perform additional optimizations. --- Diffs of the changes: (+1 -5) ConstantFolding.cpp |4 ScalarEvolution.cpp |2 +- 2 files changed, 1 insertion(+), 5 deletions(-) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -u llvm/lib/Analysis/ConstantFolding.cpp:1.15 llvm/lib/Analysis/ConstantFolding.cpp:1.16 --- llvm/lib/Analysis/ConstantFolding.cpp:1.15 Wed Jan 31 12:04:55 2007 +++ llvm/lib/Analysis/ConstantFolding.cpp Thu Feb 1 20:16:21 2007 @@ -216,10 +216,6 @@ case Instruction::FCmp: return ConstantExpr::getCompare(castCmpInst(I)-getPredicate(), Ops[0], Ops[1]); - case Instruction::Shl: - case Instruction::LShr: - case Instruction::AShr: -return ConstantExpr::get(Opc, Ops[0], Ops[1]); case Instruction::Trunc: case Instruction::ZExt: case Instruction::SExt: Index: llvm/lib/Analysis/ScalarEvolution.cpp diff -u llvm/lib/Analysis/ScalarEvolution.cpp:1.92 llvm/lib/Analysis/ScalarEvolution.cpp:1.93 --- llvm/lib/Analysis/ScalarEvolution.cpp:1.92 Tue Jan 30 17:52:44 2007 +++ llvm/lib/Analysis/ScalarEvolution.cpp Thu Feb 1 20:16:21 2007 @@ -1736,7 +1736,7 @@ /// CanConstantFold - Return true if we can constant fold an instruction of the /// specified type, assuming that all operands were constants. static bool CanConstantFold(const Instruction *I) { - if (isaBinaryOperator(I) || isaShiftInst(I) || isaCmpInst(I) || + if (isaBinaryOperator(I) || isaCmpInst(I) || isaSelectInst(I) || isaCastInst(I) || isaGetElementPtrInst(I)) return true; ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp updated: 1.14 - 1.15 --- Log message: Fix a minor bug in my patch yesterday that broken ConstProp/bswap.ll --- Diffs of the changes: (+1 -1) ConstantFolding.cpp |2 +- 1 files changed, 1 insertion(+), 1 deletion(-) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -u llvm/lib/Analysis/ConstantFolding.cpp:1.14 llvm/lib/Analysis/ConstantFolding.cpp:1.15 --- llvm/lib/Analysis/ConstantFolding.cpp:1.14 Tue Jan 30 22:42:05 2007 +++ llvm/lib/Analysis/ConstantFolding.cpp Wed Jan 31 12:04:55 2007 @@ -210,7 +210,7 @@ case Instruction::Call: if (Function *F = dyn_castFunction(Ops[0])) if (canConstantFoldCallTo(F)) -return ConstantFoldCall(F, Ops+1, NumOps); +return ConstantFoldCall(F, Ops+1, NumOps-1); return 0; case Instruction::ICmp: case Instruction::FCmp: ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp ScalarEvolution.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp updated: 1.10 - 1.11 ScalarEvolution.cpp updated: 1.90 - 1.91 --- Log message: adjust to constant folding api changes. --- Diffs of the changes: (+8 -7) ConstantFolding.cpp | 11 ++- ScalarEvolution.cpp |4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -u llvm/lib/Analysis/ConstantFolding.cpp:1.10 llvm/lib/Analysis/ConstantFolding.cpp:1.11 --- llvm/lib/Analysis/ConstantFolding.cpp:1.10 Mon Jan 15 00:27:37 2007 +++ llvm/lib/Analysis/ConstantFolding.cpp Tue Jan 30 17:15:43 2007 @@ -85,23 +85,24 @@ } } -Constant * -llvm::ConstantFoldFP(double (*NativeFP)(double), double V, const Type *Ty) { +static Constant *ConstantFoldFP(double (*NativeFP)(double), double V, +const Type *Ty) { errno = 0; V = NativeFP(V); if (errno == 0) return ConstantFP::get(Ty, V); + errno = 0; return 0; } /// ConstantFoldCall - Attempt to constant fold a call to the specified function /// with the specified arguments, returning null if unsuccessful. Constant * -llvm::ConstantFoldCall(Function *F, const std::vectorConstant* Operands) { +llvm::ConstantFoldCall(Function *F, Constant** Operands, unsigned NumOperands) { const std::string Name = F-getName(); const Type *Ty = F-getReturnType(); - if (Operands.size() == 1) { + if (NumOperands == 1) { if (ConstantFP *Op = dyn_castConstantFP(Operands[0])) { double V = Op-getValue(); switch (Name[0]) @@ -172,7 +173,7 @@ else if (Name == llvm.bswap.i64) return ConstantInt::get(Ty, ByteSwap_64(V)); } - } else if (Operands.size() == 2) { + } else if (NumOperands == 2) { if (ConstantFP *Op1 = dyn_castConstantFP(Operands[0])) { double Op1V = Op1-getValue(); if (ConstantFP *Op2 = dyn_castConstantFP(Operands[1])) { Index: llvm/lib/Analysis/ScalarEvolution.cpp diff -u llvm/lib/Analysis/ScalarEvolution.cpp:1.90 llvm/lib/Analysis/ScalarEvolution.cpp:1.91 --- llvm/lib/Analysis/ScalarEvolution.cpp:1.90 Fri Jan 19 15:13:56 2007 +++ llvm/lib/Analysis/ScalarEvolution.cpp Tue Jan 30 17:15:43 2007 @@ -1761,8 +1761,8 @@ return ConstantExpr::getSelect(Operands[0], Operands[1], Operands[2]); case Instruction::Call: if (Function *GV = dyn_castFunction(Operands[0])) { - Operands.erase(Operands.begin()); - return ConstantFoldCall(castFunction(GV), Operands); + return ConstantFoldCall(castFunction(GV), Operands[1], + Operands.size()-1); } return 0; case Instruction::GetElementPtr: { ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp updated: 1.11 - 1.12 --- Log message: move a bunch of constant folding code f rom Transforms/Utils/Local.cpp into libanalysis/ConstantFolding.cpp. --- Diffs of the changes: (+155 -2) ConstantFolding.cpp | 157 +++- 1 files changed, 155 insertions(+), 2 deletions(-) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -u llvm/lib/Analysis/ConstantFolding.cpp:1.11 llvm/lib/Analysis/ConstantFolding.cpp:1.12 --- llvm/lib/Analysis/ConstantFolding.cpp:1.11 Tue Jan 30 17:15:43 2007 +++ llvm/lib/Analysis/ConstantFolding.cpp Tue Jan 30 17:45:45 2007 @@ -15,19 +15,172 @@ #include llvm/Analysis/ConstantFolding.h #include llvm/Constants.h #include llvm/DerivedTypes.h +#include llvm/Function.h #include llvm/Instructions.h #include llvm/Intrinsics.h +#include llvm/ADT/SmallVector.h #include llvm/Support/GetElementPtrTypeIterator.h #include llvm/Support/MathExtras.h #include cerrno #include cmath using namespace llvm; +/// ConstantFoldInstruction - Attempt to constant fold the specified +/// instruction. If successful, the constant result is returned, if not, null +/// is returned. Note that this function can only fail when attempting to fold +/// instructions like loads and stores, which have no constant expression form. +/// +Constant *llvm::ConstantFoldInstruction(Instruction *I, const TargetData *TD) { + if (PHINode *PN = dyn_castPHINode(I)) { +if (PN-getNumIncomingValues() == 0) + return Constant::getNullValue(PN-getType()); + +Constant *Result = dyn_castConstant(PN-getIncomingValue(0)); +if (Result == 0) return 0; + +// Handle PHI nodes specially here... +for (unsigned i = 1, e = PN-getNumIncomingValues(); i != e; ++i) + if (PN-getIncomingValue(i) != Result PN-getIncomingValue(i) != PN) +return 0; // Not all the same incoming constants... + +// If we reach here, all incoming values are the same constant. +return Result; + } + + // Scan the operand list, checking to see if they are all constants, if so, + // hand off to ConstantFoldInstOperands. + SmallVectorConstant*, 8 Ops; + for (unsigned i = 0, e = I-getNumOperands(); i != e; ++i) +if (Constant *Op = dyn_castConstant(I-getOperand(i))) + Ops.push_back(Op); +else + return 0; // All operands not constant! + + return ConstantFoldInstOperands(I, Ops[0], Ops.size()); +} + +/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the +/// specified opcode and operands. If successful, the constant result is +/// returned, if not, null is returned. Note that this function can fail when +/// attempting to fold instructions like loads and stores, which have no +/// constant expression form. +/// +Constant *llvm::ConstantFoldInstOperands(const Instruction* I, + Constant** Ops, unsigned NumOps, + const TargetData *TD) { + unsigned Opc = I-getOpcode(); + const Type *DestTy = I-getType(); + + // Handle easy binops first + if (isaBinaryOperator(I)) +return ConstantExpr::get(Opc, Ops[0], Ops[1]); + + switch (Opc) { + default: return 0; + case Instruction::Call: +if (Function *F = dyn_castFunction(Ops[0])) + if (canConstantFoldCallTo(F)) +return ConstantFoldCall(F, Ops+1, NumOps); +return 0; + case Instruction::ICmp: + case Instruction::FCmp: +return ConstantExpr::getCompare(castCmpInst(I)-getPredicate(), Ops[0], +Ops[1]); + case Instruction::Shl: + case Instruction::LShr: + case Instruction::AShr: +return ConstantExpr::get(Opc, Ops[0], Ops[1]); + case Instruction::Trunc: + case Instruction::ZExt: + case Instruction::SExt: + case Instruction::FPTrunc: + case Instruction::FPExt: + case Instruction::UIToFP: + case Instruction::SIToFP: + case Instruction::FPToUI: + case Instruction::FPToSI: + case Instruction::PtrToInt: + case Instruction::IntToPtr: + case Instruction::BitCast: +return ConstantExpr::getCast(Opc, Ops[0], DestTy); + case Instruction::Select: +return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); + case Instruction::ExtractElement: +return ConstantExpr::getExtractElement(Ops[0], Ops[1]); + case Instruction::InsertElement: +return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); + case Instruction::ShuffleVector: +return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); + case Instruction::GetElementPtr: +return ConstantExpr::getGetElementPtr(Ops[0], + std::vectorConstant*(Ops+1, + Ops+NumOps)); + } +} + +/// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a +/// getelementptr constantexpr, return the constant value being addressed by the +/// constant expression, or null if something is funny and we
[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp updated: 1.12 - 1.13 --- Log message: Move some symbolic constant folding code out of instcombine into a place it can be used by multiple clients. This specifically allows the inliner to constant fold symbolically. --- Diffs of the changes: (+136 -3) ConstantFolding.cpp | 139 ++-- 1 files changed, 136 insertions(+), 3 deletions(-) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -u llvm/lib/Analysis/ConstantFolding.cpp:1.12 llvm/lib/Analysis/ConstantFolding.cpp:1.13 --- llvm/lib/Analysis/ConstantFolding.cpp:1.12 Tue Jan 30 17:45:45 2007 +++ llvm/lib/Analysis/ConstantFolding.cpp Tue Jan 30 18:51:48 2007 @@ -19,12 +19,136 @@ #include llvm/Instructions.h #include llvm/Intrinsics.h #include llvm/ADT/SmallVector.h +#include llvm/Target/TargetData.h #include llvm/Support/GetElementPtrTypeIterator.h #include llvm/Support/MathExtras.h #include cerrno #include cmath using namespace llvm; +//===--===// +// Constant Folding internal helper functions +//===--===// + +/// IsConstantOffsetFromGlobal - If this constant is actually a constant offset +/// from a global, return the global and the constant. Because of +/// constantexprs, this function is recursive. +static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *GV, + int64_t Offset, const TargetData TD) { + // Trivial case, constant is the global. + if ((GV = dyn_castGlobalValue(C))) { +Offset = 0; +return true; + } + + // Otherwise, if this isn't a constant expr, bail out. + ConstantExpr *CE = dyn_castConstantExpr(C); + if (!CE) return false; + + // Look through ptr-int and ptr-ptr casts. + if (CE-getOpcode() == Instruction::PtrToInt || + CE-getOpcode() == Instruction::BitCast) +return IsConstantOffsetFromGlobal(CE-getOperand(0), GV, Offset, TD); + + // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5) + if (CE-getOpcode() == Instruction::GetElementPtr) { +// Cannot compute this if the element type of the pointer is missing size +// info. +if (!castPointerType(CE-getOperand(0)-getType())-getElementType()-isSized()) + return false; + +// If the base isn't a global+constant, we aren't either. +if (!IsConstantOffsetFromGlobal(CE-getOperand(0), GV, Offset, TD)) + return false; + +// Otherwise, add any offset that our operands provide. +gep_type_iterator GTI = gep_type_begin(CE); +for (unsigned i = 1, e = CE-getNumOperands(); i != e; ++i, ++GTI) { + ConstantInt *CI = dyn_castConstantInt(CE-getOperand(i)); + if (!CI) return false; // Index isn't a simple constant? + if (CI-getZExtValue() == 0) continue; // Not adding anything. + + if (const StructType *ST = dyn_castStructType(*GTI)) { +// N = N + Offset +Offset += TD.getStructLayout(ST)-MemberOffsets[CI-getZExtValue()]; + } else { +const SequentialType *ST = castSequentialType(*GTI); +Offset += TD.getTypeSize(ST-getElementType())*CI-getSExtValue(); + } +} +return true; + } + + return false; +} + + +/// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression. +/// Attempt to symbolically evaluate the result of a binary operator merging +/// these together. If target data info is available, it is provided as TD, +/// otherwise TD is null. +static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, + Constant *Op1, const TargetData *TD){ + // SROA + + // Fold (and 0x, (shl x, 32)) - shl. + // Fold (lshr (or X, Y), 32) - (lshr [X/Y], 32) if one doesn't contribute + // bits. + + + // If the constant expr is something like A[123] - A[4].f, fold this into a + // constant. This happens frequently when iterating over a global array. + if (Opc == Instruction::Sub TD) { +GlobalValue *GV1, *GV2; +int64_t Offs1, Offs2; + +if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *TD)) + if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *TD) + GV1 == GV2) { +// (GV+C1) - (GV+C2) - C1-C2, pointer arithmetic cannot overflow. +return ConstantInt::get(Op0-getType(), Offs1-Offs2); + } + } + + // TODO: Fold icmp setne/seteq as well. + return 0; +} + +/// SymbolicallyEvaluateGEP - If we can symbolically evaluate the specified GEP +/// constant expression, do so. +static Constant *SymbolicallyEvaluateGEP(Constant** Ops, unsigned NumOps, + const Type *ResultTy, + const TargetData *TD) { + Constant *Ptr = Ops[0]; + if (!castPointerType(Ptr-getType())-getElementType()-isSized()) +return 0; + + if (TD Ptr-isNullValue()) { +
[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp updated: 1.13 - 1.14 --- Log message: elimiante a temporary vector --- Diffs of the changes: (+1 -3) ConstantFolding.cpp |4 +--- 1 files changed, 1 insertion(+), 3 deletions(-) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -u llvm/lib/Analysis/ConstantFolding.cpp:1.13 llvm/lib/Analysis/ConstantFolding.cpp:1.14 --- llvm/lib/Analysis/ConstantFolding.cpp:1.13 Tue Jan 30 18:51:48 2007 +++ llvm/lib/Analysis/ConstantFolding.cpp Tue Jan 30 22:42:05 2007 @@ -245,9 +245,7 @@ if (Constant *C = SymbolicallyEvaluateGEP(Ops, NumOps, I-getType(), TD)) return C; -return ConstantExpr::getGetElementPtr(Ops[0], - std::vectorConstant*(Ops+1, - Ops+NumOps)); +return ConstantExpr::getGetElementPtr(Ops[0], Ops+1, NumOps-1); } } ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp updated: 1.8 - 1.9 --- Log message: remove llvm.isunordered --- Diffs of the changes: (+0 -5) ConstantFolding.cpp |5 - 1 files changed, 5 deletions(-) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -u llvm/lib/Analysis/ConstantFolding.cpp:1.8 llvm/lib/Analysis/ConstantFolding.cpp:1.9 --- llvm/lib/Analysis/ConstantFolding.cpp:1.8 Sat Dec 30 23:48:39 2006 +++ llvm/lib/Analysis/ConstantFolding.cpp Sun Jan 7 02:19:47 2007 @@ -35,8 +35,6 @@ const std::string Name = F-getName(); switch (F-getIntrinsicID()) { - case Intrinsic::isunordered_f32: - case Intrinsic::isunordered_f64: case Intrinsic::sqrt_f32: case Intrinsic::sqrt_f64: case Intrinsic::bswap_i16: @@ -178,9 +176,6 @@ if (ConstantFP *Op2 = dyn_castConstantFP(Operands[1])) { double Op2V = Op2-getValue(); -if (Name == llvm.isunordered.f32 || Name == llvm.isunordered.f64) - return ConstantBool::get(IsNAN(Op1V) || IsNAN(Op2V)); -else if (Name == pow) { errno = 0; double V = pow(Op1V, Op2V); ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp ScalarEvolution.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp updated: 1.5 - 1.6 ScalarEvolution.cpp updated: 1.57 - 1.58 --- Log message: Remove redundant cmath. --- Diffs of the changes: (+0 -2) ConstantFolding.cpp |1 - ScalarEvolution.cpp |1 - 2 files changed, 2 deletions(-) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -u llvm/lib/Analysis/ConstantFolding.cpp:1.5 llvm/lib/Analysis/ConstantFolding.cpp:1.6 --- llvm/lib/Analysis/ConstantFolding.cpp:1.5 Fri Oct 20 02:07:24 2006 +++ llvm/lib/Analysis/ConstantFolding.cpp Wed Nov 8 13:16:43 2006 @@ -20,7 +20,6 @@ #include llvm/Support/GetElementPtrTypeIterator.h #include llvm/Support/MathExtras.h #include cerrno -#include cmath using namespace llvm; //===--===// Index: llvm/lib/Analysis/ScalarEvolution.cpp diff -u llvm/lib/Analysis/ScalarEvolution.cpp:1.57 llvm/lib/Analysis/ScalarEvolution.cpp:1.58 --- llvm/lib/Analysis/ScalarEvolution.cpp:1.57 Wed Nov 1 19:53:58 2006 +++ llvm/lib/Analysis/ScalarEvolution.cpp Wed Nov 8 13:16:43 2006 @@ -75,7 +75,6 @@ #include llvm/Support/InstIterator.h #include llvm/Support/ManagedStatic.h #include llvm/ADT/Statistic.h -#include cmath #include iostream #include algorithm using namespace llvm; ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp updated: 1.3 - 1.4 --- Log message: Constant fold sqrtf --- Diffs of the changes: (+4 -1) ConstantFolding.cpp |5 - 1 files changed, 4 insertions(+), 1 deletion(-) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -u llvm/lib/Analysis/ConstantFolding.cpp:1.3 llvm/lib/Analysis/ConstantFolding.cpp:1.4 --- llvm/lib/Analysis/ConstantFolding.cpp:1.3 Mon Jan 16 15:12:35 2006 +++ llvm/lib/Analysis/ConstantFolding.cpp Sat Jun 17 13:17:52 2006 @@ -76,7 +76,8 @@ case 'p': return Name == pow; case 's': - return Name == sin || Name == sinh || Name == sqrt; + return Name == sin || Name == sinh || + Name == sqrt || Name == sqrtf; case 't': return Name == tan || Name == tanh; default: @@ -150,6 +151,8 @@ return ConstantFP::get(Ty, sinh(V)); else if (Name == sqrt V = 0) return ConstantFP::get(Ty, sqrt(V)); + else if (Name == sqrtf V = 0) +return ConstantFP::get(Ty, sqrt((float)V)); break; case 't': if (Name == tan) ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp updated: 1.2 - 1.3 --- Log message: For PR411: http://llvm.cs.uiuc.edu/PR411 : This patch is an incremental step towards supporting a flat symbol table. It de-overloads the intrinsic functions by providing type-specific intrinsics and arranging for automatically upgrading from the old overloaded name to the new non-overloaded name. Specifically: llvm.isunordered - llvm.isunordered.f32, llvm.isunordered.f64 llvm.sqrt - llvm.sqrt.f32, llvm.sqrt.f64 llvm.ctpop - llvm.ctpop.i8, llvm.ctpop.i16, llvm.ctpop.i32, llvm.ctpop.i64 llvm.ctlz - llvm.ctlz.i8, llvm.ctlz.i16, llvm.ctlz.i32, llvm.ctlz.i64 llvm.cttz - llvm.cttz.i8, llvm.cttz.i16, llvm.cttz.i32, llvm.cttz.i64 New code should not use the overloaded intrinsic names. Warnings will be emitted if they are used. --- Diffs of the changes: (+18 -7) ConstantFolding.cpp | 25 ++--- 1 files changed, 18 insertions(+), 7 deletions(-) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -u llvm/lib/Analysis/ConstantFolding.cpp:1.2 llvm/lib/Analysis/ConstantFolding.cpp:1.3 --- llvm/lib/Analysis/ConstantFolding.cpp:1.2 Fri Jan 13 19:25:24 2006 +++ llvm/lib/Analysis/ConstantFolding.cpp Mon Jan 16 15:12:35 2006 @@ -35,15 +35,26 @@ const std::string Name = F-getName(); switch (F-getIntrinsicID()) { - case Intrinsic::isunordered: - case Intrinsic::sqrt: + case Intrinsic::isunordered_f32: + case Intrinsic::isunordered_f64: + case Intrinsic::sqrt_f32: + case Intrinsic::sqrt_f64: case Intrinsic::bswap_i16: case Intrinsic::bswap_i32: case Intrinsic::bswap_i64: // FIXME: these should be constant folded as well - //case Intrinsic::ctpop: - //case Intrinsic::ctlz: - //case Intrinsic::cttz: + //case Intrinsic::ctpop_i8: + //case Intrinsic::ctpop_i16: + //case Intrinsic::ctpop_i32: + //case Intrinsic::ctpop_i64: + //case Intrinsic::ctlz_i8: + //case Intrinsic::ctlz_i16: + //case Intrinsic::ctlz_i32: + //case Intrinsic::ctlz_i64: + //case Intrinsic::cttz_i8: + //case Intrinsic::cttz_i16: + //case Intrinsic::cttz_i32: + //case Intrinsic::cttz_i64: return true; default: break; } @@ -125,7 +136,7 @@ return ConstantFP::get(Ty, log(V)); else if (Name == log10 V 0) return ConstantFoldFP(log10, V, Ty); - else if (Name == llvm.sqrt) { + else if (Name == llvm.sqrt.f32 || Name == llvm.sqrt.f64) { if (V = -0.0) return ConstantFP::get(Ty, sqrt(V)); else // Undefined @@ -164,7 +175,7 @@ if (ConstantFP *Op2 = dyn_castConstantFP(Operands[1])) { double Op2V = Op2-getValue(); -if (Name == llvm.isunordered) +if (Name == llvm.isunordered.f32 || Name == llvm.isunordered.f64) return ConstantBool::get(IsNAN(Op1V) || IsNAN(Op2V)); else if (Name == pow) { ___ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
[llvm-commits] CVS: llvm/lib/Analysis/ConstantFolding.cpp
Changes in directory llvm/lib/Analysis: ConstantFolding.cpp added (r1.1) --- Log message: Move some constant folding functions into LLVMAnalysis since they are used by Analysis and Transformation passes. --- Diffs of the changes: (+172 -0) ConstantFolding.cpp | 172 1 files changed, 172 insertions(+) Index: llvm/lib/Analysis/ConstantFolding.cpp diff -c /dev/null llvm/lib/Analysis/ConstantFolding.cpp:1.1 *** /dev/null Thu Oct 27 11:00:22 2005 --- llvm/lib/Analysis/ConstantFolding.cpp Thu Oct 27 11:00:11 2005 *** *** 0 --- 1,172 + //===-- ConstantFolding.cpp - Analyze constant folding possibilities --===// + // + // The LLVM Compiler Infrastructure + // + // This file was developed by the LLVM research group and is distributed under + // the University of Illinois Open Source License. See LICENSE.TXT for details. + // + //===--===// + // + // This family of functions determines the possibility of performing constant + // folding. + // + //===--===// + + #include llvm/Analysis/ConstantFolding.h + #include llvm/Constants.h + #include llvm/DerivedTypes.h + #include llvm/Instructions.h + #include llvm/Intrinsics.h + #include llvm/Support/GetElementPtrTypeIterator.h + #include llvm/Support/MathExtras.h + #include cerrno + #include cmath + using namespace llvm; + + //===--===// + // Constant Folding ... + // + + + /// canConstantFoldCallTo - Return true if its even possible to fold a call to + /// the specified function. + bool + llvm::canConstantFoldCallTo(Function *F) { + const std::string Name = F-getName(); + + switch (F-getIntrinsicID()) { + case Intrinsic::isunordered: + case Intrinsic::sqrt: + return true; + default: break; + } + + switch (Name[0]) + { + case 'a': + return Name == acos || Name == asin || Name == atan || + Name == atan2; + case 'c': + return Name == ceil || Name == cos || Name == cosf || + Name == cosh; + case 'e': + return Name == exp; + case 'f': + return Name == fabs || Name == fmod || Name == floor; + case 'l': + return Name == log || Name == log10; + case 'p': + return Name == pow; + case 's': + return Name == sin || Name == sinh || Name == sqrt; + case 't': + return Name == tan || Name == tanh; + default: + return false; + } + } + + Constant * + llvm::ConstantFoldFP(double (*NativeFP)(double), double V, const Type *Ty) { + errno = 0; + V = NativeFP(V); + if (errno == 0) + return ConstantFP::get(Ty, V); + return 0; + } + + /// ConstantFoldCall - Attempt to constant fold a call to the specified function + /// with the specified arguments, returning null if unsuccessful. + Constant * + llvm::ConstantFoldCall(Function *F, const std::vectorConstant* Operands) { + const std::string Name = F-getName(); + const Type *Ty = F-getReturnType(); + + if (Operands.size() == 1) { + if (ConstantFP *Op = dyn_castConstantFP(Operands[0])) { + double V = Op-getValue(); + switch (Name[0]) + { + case 'a': + if (Name == acos) + return ConstantFoldFP(acos, V, Ty); + else if (Name == asin) + return ConstantFoldFP(asin, V, Ty); + else if (Name == atan) + return ConstantFP::get(Ty, atan(V)); + break; + case 'c': + if (Name == ceil) + return ConstantFoldFP(ceil, V, Ty); + else if (Name == cos) + return ConstantFP::get(Ty, cos(V)); + else if (Name == cosh) + return ConstantFP::get(Ty, cosh(V)); + break; + case 'e': + if (Name == exp) + return ConstantFP::get(Ty, exp(V)); + break; + case 'f': + if (Name == fabs) + return ConstantFP::get(Ty, fabs(V)); + else if (Name == floor) + return ConstantFoldFP(floor, V, Ty); + break; + case 'l': + if (Name == log V 0) + return ConstantFP::get(Ty, log(V)); + else if (Name == log10 V 0) + return ConstantFoldFP(log10, V, Ty); + else if (Name == llvm.sqrt) { + if (V = -0.0) + return ConstantFP::get(Ty, sqrt(V)); + else // Undefined + return ConstantFP::get(Ty, 0.0); + } + break; + case 's': + if (Name == sin) + return ConstantFP::get(Ty, sin(V)); + else if (Name == sinh) + return ConstantFP::get(Ty, sinh(V)); + else if (Name == sqrt V = 0) + return ConstantFP::get(Ty, sqrt(V)); + break;