remove a bunch of extraneous LLVMContext arguments

from various APIs, addressing PR5325.

llvm-svn: 86231
This commit is contained in:
Chris Lattner 2009-11-06 04:27:31 +00:00
parent 421a5c9651
commit 46b5c642b9
20 changed files with 220 additions and 289 deletions

View File

@ -26,20 +26,18 @@ namespace llvm {
class TargetData; class TargetData;
class Function; class Function;
class Type; class Type;
class LLVMContext;
/// ConstantFoldInstruction - Attempt to constant fold the specified /// ConstantFoldInstruction - Attempt to constant fold the specified
/// instruction. If successful, the constant result is returned, if not, null /// instruction. If successful, the constant result is returned, if not, null
/// is returned. Note that this function can only fail when attempting to fold /// is returned. Note that this function can only fail when attempting to fold
/// instructions like loads and stores, which have no constant expression form. /// instructions like loads and stores, which have no constant expression form.
/// ///
Constant *ConstantFoldInstruction(Instruction *I, LLVMContext &Context, Constant *ConstantFoldInstruction(Instruction *I, const TargetData *TD = 0);
const TargetData *TD = 0);
/// ConstantFoldConstantExpression - Attempt to fold the constant expression /// ConstantFoldConstantExpression - Attempt to fold the constant expression
/// using the specified TargetData. If successful, the constant result is /// using the specified TargetData. If successful, the constant result is
/// result is returned, if not, null is returned. /// result is returned, if not, null is returned.
Constant *ConstantFoldConstantExpression(ConstantExpr *CE, LLVMContext &Context, Constant *ConstantFoldConstantExpression(ConstantExpr *CE,
const TargetData *TD = 0); const TargetData *TD = 0);
/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
@ -50,7 +48,6 @@ Constant *ConstantFoldConstantExpression(ConstantExpr *CE, LLVMContext &Context,
/// ///
Constant *ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy, Constant *ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
Constant *const *Ops, unsigned NumOps, Constant *const *Ops, unsigned NumOps,
LLVMContext &Context,
const TargetData *TD = 0); const TargetData *TD = 0);
/// ConstantFoldCompareInstOperands - Attempt to constant fold a compare /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
@ -59,7 +56,6 @@ Constant *ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
/// ///
Constant *ConstantFoldCompareInstOperands(unsigned Predicate, Constant *ConstantFoldCompareInstOperands(unsigned Predicate,
Constant *const *Ops, unsigned NumOps, Constant *const *Ops, unsigned NumOps,
LLVMContext &Context,
const TargetData *TD = 0); const TargetData *TD = 0);
/// ConstantFoldLoadFromConstPtr - Return the value that a load from C would /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would

View File

@ -17,7 +17,6 @@
namespace llvm { namespace llvm {
class CallInst; class CallInst;
class LLVMContext;
class PointerType; class PointerType;
class TargetData; class TargetData;
class Type; class Type;
@ -45,8 +44,8 @@ CallInst* extractMallocCallFromBitCast(Value* I);
/// isArrayMalloc - Returns the corresponding CallInst if the instruction /// isArrayMalloc - Returns the corresponding CallInst if the instruction
/// is a call to malloc whose array size can be determined and the array size /// is a call to malloc whose array size can be determined and the array size
/// is not constant 1. Otherwise, return NULL. /// is not constant 1. Otherwise, return NULL.
CallInst* isArrayMalloc(Value* I, LLVMContext &Context, const TargetData* TD); CallInst* isArrayMalloc(Value *I, const TargetData *TD);
const CallInst* isArrayMalloc(const Value* I, LLVMContext &Context, const CallInst* isArrayMalloc(const Value *I,
const TargetData *TD); const TargetData *TD);
/// getMallocType - Returns the PointerType resulting from the malloc call. /// getMallocType - Returns the PointerType resulting from the malloc call.
@ -64,8 +63,7 @@ const Type* getMallocAllocatedType(const CallInst* CI);
/// then return that multiple. For non-array mallocs, the multiple is /// then return that multiple. For non-array mallocs, the multiple is
/// constant 1. Otherwise, return NULL for mallocs whose array size cannot be /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
/// determined. /// determined.
Value* getMallocArraySize(CallInst* CI, LLVMContext &Context, Value* getMallocArraySize(CallInst *CI, const TargetData *TD);
const TargetData* TD);
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// free Call Utility Functions. // free Call Utility Functions.

View File

@ -28,7 +28,6 @@ namespace llvm {
class Module; class Module;
class Constant; class Constant;
class LLVMContext;
template<typename ValueSubClass, typename ItemParentClass> template<typename ValueSubClass, typename ItemParentClass>
class SymbolTableListTraits; class SymbolTableListTraits;
@ -50,8 +49,7 @@ public:
} }
/// GlobalVariable ctor - If a parent module is specified, the global is /// GlobalVariable ctor - If a parent module is specified, the global is
/// automatically inserted into the end of the specified modules global list. /// automatically inserted into the end of the specified modules global list.
GlobalVariable(LLVMContext &Context, const Type *Ty, bool isConstant, GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
LinkageTypes Linkage,
Constant *Initializer = 0, const Twine &Name = "", Constant *Initializer = 0, const Twine &Name = "",
bool ThreadLocal = false, unsigned AddressSpace = 0); bool ThreadLocal = false, unsigned AddressSpace = 0);
/// GlobalVariable ctor - This creates a global and inserts it before the /// GlobalVariable ctor - This creates a global and inserts it before the

View File

@ -26,24 +26,22 @@
namespace llvm { namespace llvm {
class TargetData; class TargetData;
class LLVMContext;
/// TargetFolder - Create constants with target dependent folding. /// TargetFolder - Create constants with target dependent folding.
class TargetFolder { class TargetFolder {
const TargetData *TD; const TargetData *TD;
LLVMContext &Context;
/// Fold - Fold the constant using target specific information. /// Fold - Fold the constant using target specific information.
Constant *Fold(Constant *C) const { Constant *Fold(Constant *C) const {
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
if (Constant *CF = ConstantFoldConstantExpression(CE, Context, TD)) if (Constant *CF = ConstantFoldConstantExpression(CE, TD))
return CF; return CF;
return C; return C;
} }
public: public:
explicit TargetFolder(const TargetData *TheTD, LLVMContext &C) : explicit TargetFolder(const TargetData *TheTD, LLVMContext &C) :
TD(TheTD), Context(C) {} TD(TheTD) {}
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
// Binary Operators // Binary Operators

View File

@ -23,7 +23,6 @@
#include "llvm/GlobalVariable.h" #include "llvm/GlobalVariable.h"
#include "llvm/Instructions.h" #include "llvm/Instructions.h"
#include "llvm/IntrinsicInst.h" #include "llvm/IntrinsicInst.h"
#include "llvm/LLVMContext.h"
#include "llvm/Operator.h" #include "llvm/Operator.h"
#include "llvm/Pass.h" #include "llvm/Pass.h"
#include "llvm/Target/TargetData.h" #include "llvm/Target/TargetData.h"
@ -99,7 +98,7 @@ static bool isNonEscapingLocalObject(const Value *V) {
/// isObjectSmallerThan - Return true if we can prove that the object specified /// isObjectSmallerThan - Return true if we can prove that the object specified
/// by V is smaller than Size. /// by V is smaller than Size.
static bool isObjectSmallerThan(const Value *V, unsigned Size, static bool isObjectSmallerThan(const Value *V, unsigned Size,
LLVMContext &Context, const TargetData &TD) { const TargetData &TD) {
const Type *AccessTy; const Type *AccessTy;
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
AccessTy = GV->getType()->getElementType(); AccessTy = GV->getType()->getElementType();
@ -109,7 +108,7 @@ static bool isObjectSmallerThan(const Value *V, unsigned Size,
else else
return false; return false;
} else if (const CallInst* CI = extractMallocCall(V)) { } else if (const CallInst* CI = extractMallocCall(V)) {
if (!isArrayMalloc(V, Context, &TD)) if (!isArrayMalloc(V, &TD))
// The size is the argument to the malloc call. // The size is the argument to the malloc call.
if (const ConstantInt* C = dyn_cast<ConstantInt>(CI->getOperand(1))) if (const ConstantInt* C = dyn_cast<ConstantInt>(CI->getOperand(1)))
return (C->getZExtValue() < Size); return (C->getZExtValue() < Size);
@ -665,10 +664,9 @@ BasicAliasAnalysis::aliasCheck(const Value *V1, unsigned V1Size,
// If the size of one access is larger than the entire object on the other // If the size of one access is larger than the entire object on the other
// side, then we know such behavior is undefined and can assume no alias. // side, then we know such behavior is undefined and can assume no alias.
LLVMContext &Context = V1->getContext();
if (TD) if (TD)
if ((V1Size != ~0U && isObjectSmallerThan(O2, V1Size, Context, *TD)) || if ((V1Size != ~0U && isObjectSmallerThan(O2, V1Size, *TD)) ||
(V2Size != ~0U && isObjectSmallerThan(O1, V2Size, Context, *TD))) (V2Size != ~0U && isObjectSmallerThan(O1, V2Size, *TD)))
return NoAlias; return NoAlias;
// If one pointer is the result of a call/invoke and the other is a // If one pointer is the result of a call/invoke and the other is a
@ -707,16 +705,16 @@ BasicAliasAnalysis::aliasCheck(const Value *V1, unsigned V1Size,
// This function is used to determine if the indices of two GEP instructions are // This function is used to determine if the indices of two GEP instructions are
// equal. V1 and V2 are the indices. // equal. V1 and V2 are the indices.
static bool IndexOperandsEqual(Value *V1, Value *V2, LLVMContext &Context) { static bool IndexOperandsEqual(Value *V1, Value *V2) {
if (V1->getType() == V2->getType()) if (V1->getType() == V2->getType())
return V1 == V2; return V1 == V2;
if (Constant *C1 = dyn_cast<Constant>(V1)) if (Constant *C1 = dyn_cast<Constant>(V1))
if (Constant *C2 = dyn_cast<Constant>(V2)) { if (Constant *C2 = dyn_cast<Constant>(V2)) {
// Sign extend the constants to long types, if necessary // Sign extend the constants to long types, if necessary
if (C1->getType() != Type::getInt64Ty(Context)) if (C1->getType() != Type::getInt64Ty(C1->getContext()))
C1 = ConstantExpr::getSExt(C1, Type::getInt64Ty(Context)); C1 = ConstantExpr::getSExt(C1, Type::getInt64Ty(C1->getContext()));
if (C2->getType() != Type::getInt64Ty(Context)) if (C2->getType() != Type::getInt64Ty(C1->getContext()))
C2 = ConstantExpr::getSExt(C2, Type::getInt64Ty(Context)); C2 = ConstantExpr::getSExt(C2, Type::getInt64Ty(C1->getContext()));
return C1 == C2; return C1 == C2;
} }
return false; return false;
@ -737,8 +735,6 @@ BasicAliasAnalysis::CheckGEPInstructions(
const PointerType *GEPPointerTy = cast<PointerType>(BasePtr1Ty); const PointerType *GEPPointerTy = cast<PointerType>(BasePtr1Ty);
LLVMContext &Context = GEPPointerTy->getContext();
// Find the (possibly empty) initial sequence of equal values... which are not // Find the (possibly empty) initial sequence of equal values... which are not
// necessarily constants. // necessarily constants.
unsigned NumGEP1Operands = NumGEP1Ops, NumGEP2Operands = NumGEP2Ops; unsigned NumGEP1Operands = NumGEP1Ops, NumGEP2Operands = NumGEP2Ops;
@ -746,8 +742,7 @@ BasicAliasAnalysis::CheckGEPInstructions(
unsigned MaxOperands = std::max(NumGEP1Operands, NumGEP2Operands); unsigned MaxOperands = std::max(NumGEP1Operands, NumGEP2Operands);
unsigned UnequalOper = 0; unsigned UnequalOper = 0;
while (UnequalOper != MinOperands && while (UnequalOper != MinOperands &&
IndexOperandsEqual(GEP1Ops[UnequalOper], GEP2Ops[UnequalOper], IndexOperandsEqual(GEP1Ops[UnequalOper], GEP2Ops[UnequalOper])) {
Context)) {
// Advance through the type as we go... // Advance through the type as we go...
++UnequalOper; ++UnequalOper;
if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr1Ty)) if (const CompositeType *CT = dyn_cast<CompositeType>(BasePtr1Ty))
@ -811,10 +806,11 @@ BasicAliasAnalysis::CheckGEPInstructions(
if (Constant *G2OC = dyn_cast<ConstantInt>(const_cast<Value*>(G2Oper))){ if (Constant *G2OC = dyn_cast<ConstantInt>(const_cast<Value*>(G2Oper))){
if (G1OC->getType() != G2OC->getType()) { if (G1OC->getType() != G2OC->getType()) {
// Sign extend both operands to long. // Sign extend both operands to long.
if (G1OC->getType() != Type::getInt64Ty(Context)) const Type *Int64Ty = Type::getInt64Ty(G1OC->getContext());
G1OC = ConstantExpr::getSExt(G1OC, Type::getInt64Ty(Context)); if (G1OC->getType() != Int64Ty)
if (G2OC->getType() != Type::getInt64Ty(Context)) G1OC = ConstantExpr::getSExt(G1OC, Int64Ty);
G2OC = ConstantExpr::getSExt(G2OC, Type::getInt64Ty(Context)); if (G2OC->getType() != Int64Ty)
G2OC = ConstantExpr::getSExt(G2OC, Int64Ty);
GEP1Ops[FirstConstantOper] = G1OC; GEP1Ops[FirstConstantOper] = G1OC;
GEP2Ops[FirstConstantOper] = G2OC; GEP2Ops[FirstConstantOper] = G2OC;
} }
@ -950,7 +946,7 @@ BasicAliasAnalysis::CheckGEPInstructions(
for (unsigned i = 0; i != FirstConstantOper; ++i) { for (unsigned i = 0; i != FirstConstantOper; ++i) {
if (!isa<StructType>(ZeroIdxTy)) if (!isa<StructType>(ZeroIdxTy))
GEP1Ops[i] = GEP2Ops[i] = GEP1Ops[i] = GEP2Ops[i] =
Constant::getNullValue(Type::getInt32Ty(Context)); Constant::getNullValue(Type::getInt32Ty(ZeroIdxTy->getContext()));
if (const CompositeType *CT = dyn_cast<CompositeType>(ZeroIdxTy)) if (const CompositeType *CT = dyn_cast<CompositeType>(ZeroIdxTy))
ZeroIdxTy = CT->getTypeAtIndex(GEP1Ops[i]); ZeroIdxTy = CT->getTypeAtIndex(GEP1Ops[i]);
@ -992,11 +988,11 @@ BasicAliasAnalysis::CheckGEPInstructions(
// //
if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty)) if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty))
GEP1Ops[i] = GEP1Ops[i] =
ConstantInt::get(Type::getInt64Ty(Context), ConstantInt::get(Type::getInt64Ty(AT->getContext()),
AT->getNumElements()-1); AT->getNumElements()-1);
else if (const VectorType *VT = dyn_cast<VectorType>(BasePtr1Ty)) else if (const VectorType *VT = dyn_cast<VectorType>(BasePtr1Ty))
GEP1Ops[i] = GEP1Ops[i] =
ConstantInt::get(Type::getInt64Ty(Context), ConstantInt::get(Type::getInt64Ty(VT->getContext()),
VT->getNumElements()-1); VT->getNumElements()-1);
} }
} }

View File

@ -23,7 +23,6 @@
#include "llvm/GlobalVariable.h" #include "llvm/GlobalVariable.h"
#include "llvm/Instructions.h" #include "llvm/Instructions.h"
#include "llvm/Intrinsics.h" #include "llvm/Intrinsics.h"
#include "llvm/LLVMContext.h"
#include "llvm/Analysis/ValueTracking.h" #include "llvm/Analysis/ValueTracking.h"
#include "llvm/Target/TargetData.h" #include "llvm/Target/TargetData.h"
#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallVector.h"
@ -493,8 +492,7 @@ static Constant *ConstantFoldLoadInst(const LoadInst *LI, const TargetData *TD){
/// these together. If target data info is available, it is provided as TD, /// these together. If target data info is available, it is provided as TD,
/// otherwise TD is null. /// otherwise TD is null.
static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0,
Constant *Op1, const TargetData *TD, Constant *Op1, const TargetData *TD){
LLVMContext &Context){
// SROA // SROA
// Fold (and 0xffffffff00000000, (shl x, 32)) -> shl. // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
@ -523,13 +521,13 @@ static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0,
/// constant expression, do so. /// constant expression, do so.
static Constant *SymbolicallyEvaluateGEP(Constant *const *Ops, unsigned NumOps, static Constant *SymbolicallyEvaluateGEP(Constant *const *Ops, unsigned NumOps,
const Type *ResultTy, const Type *ResultTy,
LLVMContext &Context,
const TargetData *TD) { const TargetData *TD) {
Constant *Ptr = Ops[0]; Constant *Ptr = Ops[0];
if (!TD || !cast<PointerType>(Ptr->getType())->getElementType()->isSized()) if (!TD || !cast<PointerType>(Ptr->getType())->getElementType()->isSized())
return 0; return 0;
unsigned BitWidth = TD->getTypeSizeInBits(TD->getIntPtrType(Context)); unsigned BitWidth =
TD->getTypeSizeInBits(TD->getIntPtrType(Ptr->getContext()));
APInt BasePtr(BitWidth, 0); APInt BasePtr(BitWidth, 0);
bool BaseIsInt = true; bool BaseIsInt = true;
if (!Ptr->isNullValue()) { if (!Ptr->isNullValue()) {
@ -558,7 +556,7 @@ static Constant *SymbolicallyEvaluateGEP(Constant* const* Ops, unsigned NumOps,
// If the base value for this address is a literal integer value, fold the // If the base value for this address is a literal integer value, fold the
// getelementptr to the resulting integer value casted to the pointer type. // getelementptr to the resulting integer value casted to the pointer type.
if (BaseIsInt) { if (BaseIsInt) {
Constant *C = ConstantInt::get(Context, Offset+BasePtr); Constant *C = ConstantInt::get(Ptr->getContext(), Offset+BasePtr);
return ConstantExpr::getIntToPtr(C, ResultTy); return ConstantExpr::getIntToPtr(C, ResultTy);
} }
@ -579,7 +577,8 @@ static Constant *SymbolicallyEvaluateGEP(Constant* const* Ops, unsigned NumOps,
return 0; return 0;
APInt NewIdx = Offset.udiv(ElemSize); APInt NewIdx = Offset.udiv(ElemSize);
Offset -= NewIdx * ElemSize; Offset -= NewIdx * ElemSize;
NewIdxs.push_back(ConstantInt::get(TD->getIntPtrType(Context), NewIdx)); NewIdxs.push_back(ConstantInt::get(TD->getIntPtrType(Ty->getContext()),
NewIdx));
Ty = ATy->getElementType(); Ty = ATy->getElementType();
} else if (const StructType *STy = dyn_cast<StructType>(Ty)) { } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
// Determine which field of the struct the offset points into. The // Determine which field of the struct the offset points into. The
@ -587,7 +586,8 @@ static Constant *SymbolicallyEvaluateGEP(Constant* const* Ops, unsigned NumOps,
// know the offset is within the struct at this point. // know the offset is within the struct at this point.
const StructLayout &SL = *TD->getStructLayout(STy); const StructLayout &SL = *TD->getStructLayout(STy);
unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue()); unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue());
NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Context), ElIdx)); NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
ElIdx));
Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx)); Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx));
Ty = STy->getTypeAtIndex(ElIdx); Ty = STy->getTypeAtIndex(ElIdx);
} else { } else {
@ -628,8 +628,7 @@ static Constant *SymbolicallyEvaluateGEP(Constant* const* Ops, unsigned NumOps,
/// is returned. Note that this function can only fail when attempting to fold /// is returned. Note that this function can only fail when attempting to fold
/// instructions like loads and stores, which have no constant expression form. /// instructions like loads and stores, which have no constant expression form.
/// ///
Constant *llvm::ConstantFoldInstruction(Instruction *I, LLVMContext &Context, Constant *llvm::ConstantFoldInstruction(Instruction *I, const TargetData *TD) {
const TargetData *TD) {
if (PHINode *PN = dyn_cast<PHINode>(I)) { if (PHINode *PN = dyn_cast<PHINode>(I)) {
if (PN->getNumIncomingValues() == 0) if (PN->getNumIncomingValues() == 0)
return UndefValue::get(PN->getType()); return UndefValue::get(PN->getType());
@ -657,21 +656,19 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I, LLVMContext &Context,
if (const CmpInst *CI = dyn_cast<CmpInst>(I)) if (const CmpInst *CI = dyn_cast<CmpInst>(I))
return ConstantFoldCompareInstOperands(CI->getPredicate(), return ConstantFoldCompareInstOperands(CI->getPredicate(),
Ops.data(), Ops.size(), Ops.data(), Ops.size(), TD);
Context, TD);
if (const LoadInst *LI = dyn_cast<LoadInst>(I)) if (const LoadInst *LI = dyn_cast<LoadInst>(I))
return ConstantFoldLoadInst(LI, TD); return ConstantFoldLoadInst(LI, TD);
return ConstantFoldInstOperands(I->getOpcode(), I->getType(), return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
Ops.data(), Ops.size(), Context, TD); Ops.data(), Ops.size(), TD);
} }
/// ConstantFoldConstantExpression - Attempt to fold the constant expression /// ConstantFoldConstantExpression - Attempt to fold the constant expression
/// using the specified TargetData. If successful, the constant result is /// using the specified TargetData. If successful, the constant result is
/// result is returned, if not, null is returned. /// result is returned, if not, null is returned.
Constant *llvm::ConstantFoldConstantExpression(ConstantExpr *CE, Constant *llvm::ConstantFoldConstantExpression(ConstantExpr *CE,
LLVMContext &Context,
const TargetData *TD) { const TargetData *TD) {
SmallVector<Constant*, 8> Ops; SmallVector<Constant*, 8> Ops;
for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i) for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i)
@ -679,10 +676,9 @@ Constant *llvm::ConstantFoldConstantExpression(ConstantExpr *CE,
if (CE->isCompare()) if (CE->isCompare())
return ConstantFoldCompareInstOperands(CE->getPredicate(), return ConstantFoldCompareInstOperands(CE->getPredicate(),
Ops.data(), Ops.size(), Ops.data(), Ops.size(), TD);
Context, TD);
return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(), return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(),
Ops.data(), Ops.size(), Context, TD); Ops.data(), Ops.size(), TD);
} }
/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
@ -693,13 +689,11 @@ Constant *llvm::ConstantFoldConstantExpression(ConstantExpr *CE,
/// ///
Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy, Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
Constant* const* Ops, unsigned NumOps, Constant* const* Ops, unsigned NumOps,
LLVMContext &Context,
const TargetData *TD) { const TargetData *TD) {
// Handle easy binops first. // Handle easy binops first.
if (Instruction::isBinaryOp(Opcode)) { if (Instruction::isBinaryOp(Opcode)) {
if (isa<ConstantExpr>(Ops[0]) || isa<ConstantExpr>(Ops[1])) if (isa<ConstantExpr>(Ops[0]) || isa<ConstantExpr>(Ops[1]))
if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], TD, if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], TD))
Context))
return C; return C;
return ConstantExpr::get(Opcode, Ops[0], Ops[1]); return ConstantExpr::get(Opcode, Ops[0], Ops[1]);
@ -724,7 +718,7 @@ Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
unsigned InWidth = Input->getType()->getScalarSizeInBits(); unsigned InWidth = Input->getType()->getScalarSizeInBits();
if (TD->getPointerSizeInBits() < InWidth) { if (TD->getPointerSizeInBits() < InWidth) {
Constant *Mask = Constant *Mask =
ConstantInt::get(Context, APInt::getLowBitsSet(InWidth, ConstantInt::get(CE->getContext(), APInt::getLowBitsSet(InWidth,
TD->getPointerSizeInBits())); TD->getPointerSizeInBits()));
Input = ConstantExpr::getAnd(Input, Mask); Input = ConstantExpr::getAnd(Input, Mask);
} }
@ -766,7 +760,7 @@ Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
AT->getNumElements()))) { AT->getNumElements()))) {
Constant *Index[] = { Constant *Index[] = {
Constant::getNullValue(CE->getType()), Constant::getNullValue(CE->getType()),
ConstantInt::get(Context, ElemIdx) ConstantInt::get(ElTy->getContext(), ElemIdx)
}; };
return return
ConstantExpr::getGetElementPtr(GV, &Index[0], 2); ConstantExpr::getGetElementPtr(GV, &Index[0], 2);
@ -800,7 +794,7 @@ Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
case Instruction::ShuffleVector: case Instruction::ShuffleVector:
return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
case Instruction::GetElementPtr: case Instruction::GetElementPtr:
if (Constant *C = SymbolicallyEvaluateGEP(Ops, NumOps, DestTy, Context, TD)) if (Constant *C = SymbolicallyEvaluateGEP(Ops, NumOps, DestTy, TD))
return C; return C;
return ConstantExpr::getGetElementPtr(Ops[0], Ops+1, NumOps-1); return ConstantExpr::getGetElementPtr(Ops[0], Ops+1, NumOps-1);
@ -814,7 +808,6 @@ Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate, Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
Constant *const *Ops, Constant *const *Ops,
unsigned NumOps, unsigned NumOps,
LLVMContext &Context,
const TargetData *TD) { const TargetData *TD) {
// fold: icmp (inttoptr x), null -> icmp x, 0 // fold: icmp (inttoptr x), null -> icmp x, 0
// fold: icmp (ptrtoint x), 0 -> icmp x, null // fold: icmp (ptrtoint x), 0 -> icmp x, null
@ -825,15 +818,14 @@ Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
// around to know if bit truncation is happening. // around to know if bit truncation is happening.
if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops[0])) { if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops[0])) {
if (TD && Ops[1]->isNullValue()) { if (TD && Ops[1]->isNullValue()) {
const Type *IntPtrTy = TD->getIntPtrType(Context); const Type *IntPtrTy = TD->getIntPtrType(CE0->getContext());
if (CE0->getOpcode() == Instruction::IntToPtr) { if (CE0->getOpcode() == Instruction::IntToPtr) {
// Convert the integer value to the right size to ensure we get the // Convert the integer value to the right size to ensure we get the
// proper extension or truncation. // proper extension or truncation.
Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0), Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
IntPtrTy, false); IntPtrTy, false);
Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) }; Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) };
return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, TD);
Context, TD);
} }
// Only do this transformation if the int is intptrty in size, otherwise // Only do this transformation if the int is intptrty in size, otherwise
@ -843,14 +835,13 @@ Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
Constant *C = CE0->getOperand(0); Constant *C = CE0->getOperand(0);
Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) }; Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) };
// FIXME! // FIXME!
return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, TD);
Context, TD);
} }
} }
if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops[1])) { if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops[1])) {
if (TD && CE0->getOpcode() == CE1->getOpcode()) { if (TD && CE0->getOpcode() == CE1->getOpcode()) {
const Type *IntPtrTy = TD->getIntPtrType(Context); const Type *IntPtrTy = TD->getIntPtrType(CE0->getContext());
if (CE0->getOpcode() == Instruction::IntToPtr) { if (CE0->getOpcode() == Instruction::IntToPtr) {
// Convert the integer value to the right size to ensure we get the // Convert the integer value to the right size to ensure we get the
@ -860,8 +851,7 @@ Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0), Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
IntPtrTy, false); IntPtrTy, false);
Constant *NewOps[] = { C0, C1 }; Constant *NewOps[] = { C0, C1 };
return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, TD);
Context, TD);
} }
// Only do this transformation if the int is intptrty in size, otherwise // Only do this transformation if the int is intptrty in size, otherwise
@ -872,8 +862,7 @@ Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
Constant *NewOps[] = { Constant *NewOps[] = {
CE0->getOperand(0), CE1->getOperand(0) CE0->getOperand(0), CE1->getOperand(0)
}; };
return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, TD);
Context, TD);
} }
} }
} }
@ -996,7 +985,7 @@ llvm::canConstantFoldCallTo(const Function *F) {
} }
static Constant *ConstantFoldFP(double (*NativeFP)(double), double V, static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
const Type *Ty, LLVMContext &Context) { const Type *Ty) {
errno = 0; errno = 0;
V = NativeFP(V); V = NativeFP(V);
if (errno != 0) { if (errno != 0) {
@ -1005,17 +994,15 @@ static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
} }
if (Ty->isFloatTy()) if (Ty->isFloatTy())
return ConstantFP::get(Context, APFloat((float)V)); return ConstantFP::get(Ty->getContext(), APFloat((float)V));
if (Ty->isDoubleTy()) if (Ty->isDoubleTy())
return ConstantFP::get(Context, APFloat(V)); return ConstantFP::get(Ty->getContext(), APFloat(V));
llvm_unreachable("Can only constant fold float/double"); llvm_unreachable("Can only constant fold float/double");
return 0; // dummy return to suppress warning return 0; // dummy return to suppress warning
} }
static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
double V, double W, double V, double W, const Type *Ty) {
const Type *Ty,
LLVMContext &Context) {
errno = 0; errno = 0;
V = NativeFP(V, W); V = NativeFP(V, W);
if (errno != 0) { if (errno != 0) {
@ -1024,9 +1011,9 @@ static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
} }
if (Ty->isFloatTy()) if (Ty->isFloatTy())
return ConstantFP::get(Context, APFloat((float)V)); return ConstantFP::get(Ty->getContext(), APFloat((float)V));
if (Ty->isDoubleTy()) if (Ty->isDoubleTy())
return ConstantFP::get(Context, APFloat(V)); return ConstantFP::get(Ty->getContext(), APFloat(V));
llvm_unreachable("Can only constant fold float/double"); llvm_unreachable("Can only constant fold float/double");
return 0; // dummy return to suppress warning return 0; // dummy return to suppress warning
} }
@ -1037,7 +1024,6 @@ Constant *
llvm::ConstantFoldCall(Function *F, llvm::ConstantFoldCall(Function *F,
Constant *const *Operands, unsigned NumOperands) { Constant *const *Operands, unsigned NumOperands) {
if (!F->hasName()) return 0; if (!F->hasName()) return 0;
LLVMContext &Context = F->getContext();
StringRef Name = F->getName(); StringRef Name = F->getName();
const Type *Ty = F->getReturnType(); const Type *Ty = F->getReturnType();
@ -1054,62 +1040,62 @@ llvm::ConstantFoldCall(Function *F,
switch (Name[0]) { switch (Name[0]) {
case 'a': case 'a':
if (Name == "acos") if (Name == "acos")
return ConstantFoldFP(acos, V, Ty, Context); return ConstantFoldFP(acos, V, Ty);
else if (Name == "asin") else if (Name == "asin")
return ConstantFoldFP(asin, V, Ty, Context); return ConstantFoldFP(asin, V, Ty);
else if (Name == "atan") else if (Name == "atan")
return ConstantFoldFP(atan, V, Ty, Context); return ConstantFoldFP(atan, V, Ty);
break; break;
case 'c': case 'c':
if (Name == "ceil") if (Name == "ceil")
return ConstantFoldFP(ceil, V, Ty, Context); return ConstantFoldFP(ceil, V, Ty);
else if (Name == "cos") else if (Name == "cos")
return ConstantFoldFP(cos, V, Ty, Context); return ConstantFoldFP(cos, V, Ty);
else if (Name == "cosh") else if (Name == "cosh")
return ConstantFoldFP(cosh, V, Ty, Context); return ConstantFoldFP(cosh, V, Ty);
else if (Name == "cosf") else if (Name == "cosf")
return ConstantFoldFP(cos, V, Ty, Context); return ConstantFoldFP(cos, V, Ty);
break; break;
case 'e': case 'e':
if (Name == "exp") if (Name == "exp")
return ConstantFoldFP(exp, V, Ty, Context); return ConstantFoldFP(exp, V, Ty);
break; break;
case 'f': case 'f':
if (Name == "fabs") if (Name == "fabs")
return ConstantFoldFP(fabs, V, Ty, Context); return ConstantFoldFP(fabs, V, Ty);
else if (Name == "floor") else if (Name == "floor")
return ConstantFoldFP(floor, V, Ty, Context); return ConstantFoldFP(floor, V, Ty);
break; break;
case 'l': case 'l':
if (Name == "log" && V > 0) if (Name == "log" && V > 0)
return ConstantFoldFP(log, V, Ty, Context); return ConstantFoldFP(log, V, Ty);
else if (Name == "log10" && V > 0) else if (Name == "log10" && V > 0)
return ConstantFoldFP(log10, V, Ty, Context); return ConstantFoldFP(log10, V, Ty);
else if (Name == "llvm.sqrt.f32" || else if (Name == "llvm.sqrt.f32" ||
Name == "llvm.sqrt.f64") { Name == "llvm.sqrt.f64") {
if (V >= -0.0) if (V >= -0.0)
return ConstantFoldFP(sqrt, V, Ty, Context); return ConstantFoldFP(sqrt, V, Ty);
else // Undefined else // Undefined
return Constant::getNullValue(Ty); return Constant::getNullValue(Ty);
} }
break; break;
case 's': case 's':
if (Name == "sin") if (Name == "sin")
return ConstantFoldFP(sin, V, Ty, Context); return ConstantFoldFP(sin, V, Ty);
else if (Name == "sinh") else if (Name == "sinh")
return ConstantFoldFP(sinh, V, Ty, Context); return ConstantFoldFP(sinh, V, Ty);
else if (Name == "sqrt" && V >= 0) else if (Name == "sqrt" && V >= 0)
return ConstantFoldFP(sqrt, V, Ty, Context); return ConstantFoldFP(sqrt, V, Ty);
else if (Name == "sqrtf" && V >= 0) else if (Name == "sqrtf" && V >= 0)
return ConstantFoldFP(sqrt, V, Ty, Context); return ConstantFoldFP(sqrt, V, Ty);
else if (Name == "sinf") else if (Name == "sinf")
return ConstantFoldFP(sin, V, Ty, Context); return ConstantFoldFP(sin, V, Ty);
break; break;
case 't': case 't':
if (Name == "tan") if (Name == "tan")
return ConstantFoldFP(tan, V, Ty, Context); return ConstantFoldFP(tan, V, Ty);
else if (Name == "tanh") else if (Name == "tanh")
return ConstantFoldFP(tanh, V, Ty, Context); return ConstantFoldFP(tanh, V, Ty);
break; break;
default: default:
break; break;
@ -1120,7 +1106,7 @@ llvm::ConstantFoldCall(Function *F,
if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) { if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) {
if (Name.startswith("llvm.bswap")) if (Name.startswith("llvm.bswap"))
return ConstantInt::get(Context, Op->getValue().byteSwap()); return ConstantInt::get(F->getContext(), Op->getValue().byteSwap());
else if (Name.startswith("llvm.ctpop")) else if (Name.startswith("llvm.ctpop"))
return ConstantInt::get(Ty, Op->getValue().countPopulation()); return ConstantInt::get(Ty, Op->getValue().countPopulation());
else if (Name.startswith("llvm.cttz")) else if (Name.startswith("llvm.cttz"))
@ -1149,17 +1135,19 @@ llvm::ConstantFoldCall(Function *F,
Op2->getValueAPF().convertToDouble(); Op2->getValueAPF().convertToDouble();
if (Name == "pow") if (Name == "pow")
return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty, Context); return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
if (Name == "fmod") if (Name == "fmod")
return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty, Context); return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty);
if (Name == "atan2") if (Name == "atan2")
return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty, Context); return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
} else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) { } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
if (Name == "llvm.powi.f32") if (Name == "llvm.powi.f32")
return ConstantFP::get(Context, APFloat((float)std::pow((float)Op1V, return ConstantFP::get(F->getContext(),
APFloat((float)std::pow((float)Op1V,
(int)Op2C->getZExtValue()))); (int)Op2C->getZExtValue())));
if (Name == "llvm.powi.f64") if (Name == "llvm.powi.f64")
return ConstantFP::get(Context, APFloat((double)std::pow((double)Op1V, return ConstantFP::get(F->getContext(),
APFloat((double)std::pow((double)Op1V,
(int)Op2C->getZExtValue()))); (int)Op2C->getZExtValue())));
} }
return 0; return 0;

View File

@ -91,8 +91,7 @@ static bool isConstantOne(Value *val) {
return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne(); return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
} }
static Value *isArrayMallocHelper(const CallInst *CI, LLVMContext &Context, static Value *isArrayMallocHelper(const CallInst *CI, const TargetData *TD) {
const TargetData *TD) {
if (!CI) if (!CI)
return NULL; return NULL;
@ -109,7 +108,7 @@ static Value *isArrayMallocHelper(const CallInst *CI, LLVMContext &Context,
ElementSize = ConstantExpr::getTruncOrBitCast(ElementSize, ElementSize = ConstantExpr::getTruncOrBitCast(ElementSize,
MallocArg->getType()); MallocArg->getType());
Constant *FoldedElementSize = Constant *FoldedElementSize =
ConstantFoldConstantExpression(cast<ConstantExpr>(ElementSize), Context, TD); ConstantFoldConstantExpression(cast<ConstantExpr>(ElementSize), TD);
// First, check if CI is a non-array malloc. // First, check if CI is a non-array malloc.
if (CO && ((CO == ElementSize) || if (CO && ((CO == ElementSize) ||
@ -159,7 +158,7 @@ static Value *isArrayMallocHelper(const CallInst *CI, LLVMContext &Context,
APInt Op1Int = Op1CI->getValue(); APInt Op1Int = Op1CI->getValue();
uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1); uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
Value *Op1Pow = ConstantInt::get(Context, Value *Op1Pow = ConstantInt::get(Op1CI->getContext(),
APInt(Op1Int.getBitWidth(), 0).set(BitToSet)); APInt(Op1Int.getBitWidth(), 0).set(BitToSet));
if (Op0 == ElementSize || (FoldedElementSize && Op0 == FoldedElementSize)) if (Op0 == ElementSize || (FoldedElementSize && Op0 == FoldedElementSize))
// ArraySize << log2(ElementSize) // ArraySize << log2(ElementSize)
@ -178,10 +177,9 @@ static Value *isArrayMallocHelper(const CallInst *CI, LLVMContext &Context,
/// isArrayMalloc - Returns the corresponding CallInst if the instruction /// isArrayMalloc - Returns the corresponding CallInst if the instruction
/// is a call to malloc whose array size can be determined and the array size /// is a call to malloc whose array size can be determined and the array size
/// is not constant 1. Otherwise, return NULL. /// is not constant 1. Otherwise, return NULL.
CallInst *llvm::isArrayMalloc(Value *I, LLVMContext &Context, CallInst *llvm::isArrayMalloc(Value *I, const TargetData *TD) {
const TargetData *TD) {
CallInst *CI = extractMallocCall(I); CallInst *CI = extractMallocCall(I);
Value *ArraySize = isArrayMallocHelper(CI, Context, TD); Value *ArraySize = isArrayMallocHelper(CI, TD);
if (ArraySize && if (ArraySize &&
ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1)) ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1))
@ -191,10 +189,9 @@ CallInst *llvm::isArrayMalloc(Value *I, LLVMContext &Context,
return NULL; return NULL;
} }
const CallInst *llvm::isArrayMalloc(const Value *I, LLVMContext &Context, const CallInst *llvm::isArrayMalloc(const Value *I, const TargetData *TD) {
const TargetData *TD) {
const CallInst *CI = extractMallocCall(I); const CallInst *CI = extractMallocCall(I);
Value *ArraySize = isArrayMallocHelper(CI, Context, TD); Value *ArraySize = isArrayMallocHelper(CI, TD);
if (ArraySize && if (ArraySize &&
ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1)) ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1))
@ -244,9 +241,8 @@ const Type *llvm::getMallocAllocatedType(const CallInst *CI) {
/// then return that multiple. For non-array mallocs, the multiple is /// then return that multiple. For non-array mallocs, the multiple is
/// constant 1. Otherwise, return NULL for mallocs whose array size cannot be /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
/// determined. /// determined.
Value *llvm::getMallocArraySize(CallInst *CI, LLVMContext &Context, Value *llvm::getMallocArraySize(CallInst *CI, const TargetData *TD) {
const TargetData *TD) { return isArrayMallocHelper(CI, TD);
return isArrayMallocHelper(CI, Context, TD);
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//

View File

@ -10,6 +10,7 @@
// This file implements tracking of pointer bounds. // This file implements tracking of pointer bounds.
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/Dominators.h" #include "llvm/Analysis/Dominators.h"
#include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/LoopInfo.h"
@ -101,7 +102,7 @@ const SCEV *PointerTracking::computeAllocationCount(Value *P,
} }
if (CallInst *CI = extractMallocCall(V)) { if (CallInst *CI = extractMallocCall(V)) {
Value *arraySize = getMallocArraySize(CI, P->getContext(), TD); Value *arraySize = getMallocArraySize(CI, TD);
const Type* AllocTy = getMallocAllocatedType(CI); const Type* AllocTy = getMallocAllocatedType(CI);
if (!AllocTy || !arraySize) return SE->getCouldNotCompute(); if (!AllocTy || !arraySize) return SE->getCouldNotCompute();
Ty = AllocTy; Ty = AllocTy;

View File

@ -3816,7 +3816,6 @@ static Constant *EvaluateExpression(Value *V, Constant *PHIVal) {
if (Constant *C = dyn_cast<Constant>(V)) return C; if (Constant *C = dyn_cast<Constant>(V)) return C;
if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV; if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
Instruction *I = cast<Instruction>(V); Instruction *I = cast<Instruction>(V);
LLVMContext &Context = I->getParent()->getContext();
std::vector<Constant*> Operands; std::vector<Constant*> Operands;
Operands.resize(I->getNumOperands()); Operands.resize(I->getNumOperands());
@ -3828,12 +3827,10 @@ static Constant *EvaluateExpression(Value *V, Constant *PHIVal) {
if (const CmpInst *CI = dyn_cast<CmpInst>(I)) if (const CmpInst *CI = dyn_cast<CmpInst>(I))
return ConstantFoldCompareInstOperands(CI->getPredicate(), return ConstantFoldCompareInstOperands(CI->getPredicate(),
&Operands[0], Operands.size(), &Operands[0], Operands.size());
Context);
else else
return ConstantFoldInstOperands(I->getOpcode(), I->getType(), return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
&Operands[0], Operands.size(), &Operands[0], Operands.size());
Context);
} }
/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
@ -4040,12 +4037,10 @@ const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
Constant *C; Constant *C;
if (const CmpInst *CI = dyn_cast<CmpInst>(I)) if (const CmpInst *CI = dyn_cast<CmpInst>(I))
C = ConstantFoldCompareInstOperands(CI->getPredicate(), C = ConstantFoldCompareInstOperands(CI->getPredicate(),
&Operands[0], Operands.size(), &Operands[0], Operands.size());
getContext());
else else
C = ConstantFoldInstOperands(I->getOpcode(), I->getType(), C = ConstantFoldInstOperands(I->getOpcode(), I->getType(),
&Operands[0], Operands.size(), &Operands[0], Operands.size());
getContext());
return getSCEV(C); return getSCEV(C);
} }
} }

View File

@ -20,7 +20,6 @@
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h" #include "llvm/Instructions.h"
#include "llvm/IntrinsicInst.h" #include "llvm/IntrinsicInst.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/Pass.h" #include "llvm/Pass.h"
#include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/ConstantFolding.h"
@ -245,8 +244,7 @@ static bool AnalyzeGlobal(Value *V, GlobalStatus &GS,
return false; return false;
} }
static Constant *getAggregateConstantElement(Constant *Agg, Constant *Idx, static Constant *getAggregateConstantElement(Constant *Agg, Constant *Idx) {
LLVMContext &Context) {
ConstantInt *CI = dyn_cast<ConstantInt>(Idx); ConstantInt *CI = dyn_cast<ConstantInt>(Idx);
if (!CI) return 0; if (!CI) return 0;
unsigned IdxV = CI->getZExtValue(); unsigned IdxV = CI->getZExtValue();
@ -282,8 +280,7 @@ static Constant *getAggregateConstantElement(Constant *Agg, Constant *Idx,
/// users of the global, cleaning up the obvious ones. This is largely just a /// users of the global, cleaning up the obvious ones. This is largely just a
/// quick scan over the use list to clean up the easy and obvious cruft. This /// quick scan over the use list to clean up the easy and obvious cruft. This
/// returns true if it made a change. /// returns true if it made a change.
static bool CleanupConstantGlobalUsers(Value *V, Constant *Init, static bool CleanupConstantGlobalUsers(Value *V, Constant *Init) {
LLVMContext &Context) {
bool Changed = false; bool Changed = false;
for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;) { for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;) {
User *U = *UI++; User *U = *UI++;
@ -304,11 +301,11 @@ static bool CleanupConstantGlobalUsers(Value *V, Constant *Init,
Constant *SubInit = 0; Constant *SubInit = 0;
if (Init) if (Init)
SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE); SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
Changed |= CleanupConstantGlobalUsers(CE, SubInit, Context); Changed |= CleanupConstantGlobalUsers(CE, SubInit);
} else if (CE->getOpcode() == Instruction::BitCast && } else if (CE->getOpcode() == Instruction::BitCast &&
isa<PointerType>(CE->getType())) { isa<PointerType>(CE->getType())) {
// Pointer cast, delete any stores and memsets to the global. // Pointer cast, delete any stores and memsets to the global.
Changed |= CleanupConstantGlobalUsers(CE, 0, Context); Changed |= CleanupConstantGlobalUsers(CE, 0);
} }
if (CE->use_empty()) { if (CE->use_empty()) {
@ -322,11 +319,11 @@ static bool CleanupConstantGlobalUsers(Value *V, Constant *Init,
Constant *SubInit = 0; Constant *SubInit = 0;
if (!isa<ConstantExpr>(GEP->getOperand(0))) { if (!isa<ConstantExpr>(GEP->getOperand(0))) {
ConstantExpr *CE = ConstantExpr *CE =
dyn_cast_or_null<ConstantExpr>(ConstantFoldInstruction(GEP, Context)); dyn_cast_or_null<ConstantExpr>(ConstantFoldInstruction(GEP));
if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr) if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr)
SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE); SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
} }
Changed |= CleanupConstantGlobalUsers(GEP, SubInit, Context); Changed |= CleanupConstantGlobalUsers(GEP, SubInit);
if (GEP->use_empty()) { if (GEP->use_empty()) {
GEP->eraseFromParent(); GEP->eraseFromParent();
@ -344,7 +341,7 @@ static bool CleanupConstantGlobalUsers(Value *V, Constant *Init,
if (SafeToDestroyConstant(C)) { if (SafeToDestroyConstant(C)) {
C->destroyConstant(); C->destroyConstant();
// This could have invalidated UI, start over from scratch. // This could have invalidated UI, start over from scratch.
CleanupConstantGlobalUsers(V, Init, Context); CleanupConstantGlobalUsers(V, Init);
return true; return true;
} }
} }
@ -469,8 +466,7 @@ static bool GlobalUsersSafeToSRA(GlobalValue *GV) {
/// behavior of the program in a more fine-grained way. We have determined that /// behavior of the program in a more fine-grained way. We have determined that
/// this transformation is safe already. We return the first global variable we /// this transformation is safe already. We return the first global variable we
/// insert so that the caller can reprocess it. /// insert so that the caller can reprocess it.
static GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD, static GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD) {
LLVMContext &Context) {
// Make sure this global only has simple uses that we can SRA. // Make sure this global only has simple uses that we can SRA.
if (!GlobalUsersSafeToSRA(GV)) if (!GlobalUsersSafeToSRA(GV))
return 0; return 0;
@ -492,11 +488,9 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD,
const StructLayout &Layout = *TD.getStructLayout(STy); const StructLayout &Layout = *TD.getStructLayout(STy);
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
Constant *In = getAggregateConstantElement(Init, Constant *In = getAggregateConstantElement(Init,
ConstantInt::get(Type::getInt32Ty(Context), i), ConstantInt::get(Type::getInt32Ty(STy->getContext()), i));
Context);
assert(In && "Couldn't get element of initializer?"); assert(In && "Couldn't get element of initializer?");
GlobalVariable *NGV = new GlobalVariable(Context, GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false,
STy->getElementType(i), false,
GlobalVariable::InternalLinkage, GlobalVariable::InternalLinkage,
In, GV->getName()+"."+Twine(i), In, GV->getName()+"."+Twine(i),
GV->isThreadLocal(), GV->isThreadLocal(),
@ -527,12 +521,10 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD,
unsigned EltAlign = TD.getABITypeAlignment(STy->getElementType()); unsigned EltAlign = TD.getABITypeAlignment(STy->getElementType());
for (unsigned i = 0, e = NumElements; i != e; ++i) { for (unsigned i = 0, e = NumElements; i != e; ++i) {
Constant *In = getAggregateConstantElement(Init, Constant *In = getAggregateConstantElement(Init,
ConstantInt::get(Type::getInt32Ty(Context), i), ConstantInt::get(Type::getInt32Ty(Init->getContext()), i));
Context);
assert(In && "Couldn't get element of initializer?"); assert(In && "Couldn't get element of initializer?");
GlobalVariable *NGV = new GlobalVariable(Context, GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false,
STy->getElementType(), false,
GlobalVariable::InternalLinkage, GlobalVariable::InternalLinkage,
In, GV->getName()+"."+Twine(i), In, GV->getName()+"."+Twine(i),
GV->isThreadLocal(), GV->isThreadLocal(),
@ -554,7 +546,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD,
DEBUG(errs() << "PERFORMING GLOBAL SRA ON: " << *GV); DEBUG(errs() << "PERFORMING GLOBAL SRA ON: " << *GV);
Constant *NullInt = Constant::getNullValue(Type::getInt32Ty(Context)); Constant *NullInt =Constant::getNullValue(Type::getInt32Ty(GV->getContext()));
// Loop over all of the uses of the global, replacing the constantexpr geps, // Loop over all of the uses of the global, replacing the constantexpr geps,
// with smaller constantexpr geps or direct references. // with smaller constantexpr geps or direct references.
@ -678,8 +670,7 @@ static bool AllUsesOfLoadedValueWillTrapIfNull(GlobalVariable *GV) {
return true; return true;
} }
static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV, static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) {
LLVMContext &Context) {
bool Changed = false; bool Changed = false;
for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) { for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) {
Instruction *I = cast<Instruction>(*UI++); Instruction *I = cast<Instruction>(*UI++);
@ -712,7 +703,7 @@ static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV,
} else if (CastInst *CI = dyn_cast<CastInst>(I)) { } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
Changed |= OptimizeAwayTrappingUsesOfValue(CI, Changed |= OptimizeAwayTrappingUsesOfValue(CI,
ConstantExpr::getCast(CI->getOpcode(), ConstantExpr::getCast(CI->getOpcode(),
NewV, CI->getType()), Context); NewV, CI->getType()));
if (CI->use_empty()) { if (CI->use_empty()) {
Changed = true; Changed = true;
CI->eraseFromParent(); CI->eraseFromParent();
@ -730,7 +721,7 @@ static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV,
if (Idxs.size() == GEPI->getNumOperands()-1) if (Idxs.size() == GEPI->getNumOperands()-1)
Changed |= OptimizeAwayTrappingUsesOfValue(GEPI, Changed |= OptimizeAwayTrappingUsesOfValue(GEPI,
ConstantExpr::getGetElementPtr(NewV, &Idxs[0], ConstantExpr::getGetElementPtr(NewV, &Idxs[0],
Idxs.size()), Context); Idxs.size()));
if (GEPI->use_empty()) { if (GEPI->use_empty()) {
Changed = true; Changed = true;
GEPI->eraseFromParent(); GEPI->eraseFromParent();
@ -746,8 +737,7 @@ static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV,
/// value stored into it. If there are uses of the loaded value that would trap /// value stored into it. If there are uses of the loaded value that would trap
/// if the loaded value is dynamically null, then we know that they cannot be /// if the loaded value is dynamically null, then we know that they cannot be
/// reachable with a null optimize away the load. /// reachable with a null optimize away the load.
static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV, static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV) {
LLVMContext &Context) {
bool Changed = false; bool Changed = false;
// Keep track of whether we are able to remove all the uses of the global // Keep track of whether we are able to remove all the uses of the global
@ -758,7 +748,7 @@ static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV,
for (Value::use_iterator GUI = GV->use_begin(), E = GV->use_end(); GUI != E;){ for (Value::use_iterator GUI = GV->use_begin(), E = GV->use_end(); GUI != E;){
User *GlobalUser = *GUI++; User *GlobalUser = *GUI++;
if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) { if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) {
Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV, Context); Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV);
// If we were able to delete all uses of the loads // If we were able to delete all uses of the loads
if (LI->use_empty()) { if (LI->use_empty()) {
LI->eraseFromParent(); LI->eraseFromParent();
@ -789,7 +779,7 @@ static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV,
// nor is the global. // nor is the global.
if (AllNonStoreUsesGone) { if (AllNonStoreUsesGone) {
DEBUG(errs() << " *** GLOBAL NOW DEAD!\n"); DEBUG(errs() << " *** GLOBAL NOW DEAD!\n");
CleanupConstantGlobalUsers(GV, 0, Context); CleanupConstantGlobalUsers(GV, 0);
if (GV->use_empty()) { if (GV->use_empty()) {
GV->eraseFromParent(); GV->eraseFromParent();
++NumDeleted; ++NumDeleted;
@ -801,10 +791,10 @@ static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV,
/// ConstantPropUsersOf - Walk the use list of V, constant folding all of the /// ConstantPropUsersOf - Walk the use list of V, constant folding all of the
/// instructions that are foldable. /// instructions that are foldable.
static void ConstantPropUsersOf(Value *V, LLVMContext &Context) { static void ConstantPropUsersOf(Value *V) {
for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; )
if (Instruction *I = dyn_cast<Instruction>(*UI++)) if (Instruction *I = dyn_cast<Instruction>(*UI++))
if (Constant *NewC = ConstantFoldInstruction(I, Context)) { if (Constant *NewC = ConstantFoldInstruction(I)) {
I->replaceAllUsesWith(NewC); I->replaceAllUsesWith(NewC);
// Advance UI to the next non-I use to avoid invalidating it! // Advance UI to the next non-I use to avoid invalidating it!
@ -824,12 +814,11 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
CallInst *CI, CallInst *CI,
BitCastInst *BCI, BitCastInst *BCI,
Value* NElems, Value* NElems,
LLVMContext &Context,
TargetData* TD) { TargetData* TD) {
DEBUG(errs() << "PROMOTING MALLOC GLOBAL: " << *GV DEBUG(errs() << "PROMOTING MALLOC GLOBAL: " << *GV
<< " CALL = " << *CI << " BCI = " << *BCI << '\n'); << " CALL = " << *CI << " BCI = " << *BCI << '\n');
const Type *IntPtrTy = TD->getIntPtrType(Context); const Type *IntPtrTy = TD->getIntPtrType(GV->getContext());
ConstantInt *NElements = cast<ConstantInt>(NElems); ConstantInt *NElements = cast<ConstantInt>(NElems);
if (NElements->getZExtValue() != 1) { if (NElements->getZExtValue() != 1) {
@ -872,10 +861,10 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
// If there is a comparison against null, we will insert a global bool to // If there is a comparison against null, we will insert a global bool to
// keep track of whether the global was initialized yet or not. // keep track of whether the global was initialized yet or not.
GlobalVariable *InitBool = GlobalVariable *InitBool =
new GlobalVariable(Context, Type::getInt1Ty(Context), false, new GlobalVariable(Type::getInt1Ty(GV->getContext()), false,
GlobalValue::InternalLinkage, GlobalValue::InternalLinkage,
ConstantInt::getFalse(Context), GV->getName()+".init", ConstantInt::getFalse(GV->getContext()),
GV->isThreadLocal()); GV->getName()+".init", GV->isThreadLocal());
bool InitBoolUsed = false; bool InitBoolUsed = false;
// Loop over all uses of GV, processing them in turn. // Loop over all uses of GV, processing them in turn.
@ -894,8 +883,8 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
switch (ICI->getPredicate()) { switch (ICI->getPredicate()) {
default: llvm_unreachable("Unknown ICmp Predicate!"); default: llvm_unreachable("Unknown ICmp Predicate!");
case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULT:
case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLT: // X < null -> always false
LV = ConstantInt::getFalse(Context); // X < null -> always false LV = ConstantInt::getFalse(GV->getContext());
break; break;
case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_ULE:
case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SLE:
@ -917,7 +906,7 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
} else { } else {
StoreInst *SI = cast<StoreInst>(GV->use_back()); StoreInst *SI = cast<StoreInst>(GV->use_back());
// The global is initialized when the store to it occurs. // The global is initialized when the store to it occurs.
new StoreInst(ConstantInt::getTrue(Context), InitBool, SI); new StoreInst(ConstantInt::getTrue(GV->getContext()), InitBool, SI);
SI->eraseFromParent(); SI->eraseFromParent();
} }
@ -938,9 +927,9 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
// To further other optimizations, loop over all users of NewGV and try to // To further other optimizations, loop over all users of NewGV and try to
// constant prop them. This will promote GEP instructions with constant // constant prop them. This will promote GEP instructions with constant
// indices into GEP constant-exprs, which will allow global-opt to hack on it. // indices into GEP constant-exprs, which will allow global-opt to hack on it.
ConstantPropUsersOf(NewGV, Context); ConstantPropUsersOf(NewGV);
if (RepValue != NewGV) if (RepValue != NewGV)
ConstantPropUsersOf(RepValue, Context); ConstantPropUsersOf(RepValue);
return NewGV; return NewGV;
} }
@ -1142,8 +1131,7 @@ static bool AllGlobalLoadUsesSimpleEnoughForHeapSRA(GlobalVariable *GV,
static Value *GetHeapSROAValue(Value *V, unsigned FieldNo, static Value *GetHeapSROAValue(Value *V, unsigned FieldNo,
DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues, DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite, std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
LLVMContext &Context) {
std::vector<Value*> &FieldVals = InsertedScalarizedValues[V]; std::vector<Value*> &FieldVals = InsertedScalarizedValues[V];
if (FieldNo >= FieldVals.size()) if (FieldNo >= FieldVals.size())
@ -1161,7 +1149,7 @@ static Value *GetHeapSROAValue(Value *V, unsigned FieldNo,
// a new Load of the scalarized global. // a new Load of the scalarized global.
Result = new LoadInst(GetHeapSROAValue(LI->getOperand(0), FieldNo, Result = new LoadInst(GetHeapSROAValue(LI->getOperand(0), FieldNo,
InsertedScalarizedValues, InsertedScalarizedValues,
PHIsToRewrite, Context), PHIsToRewrite),
LI->getName()+".f"+Twine(FieldNo), LI); LI->getName()+".f"+Twine(FieldNo), LI);
} else if (PHINode *PN = dyn_cast<PHINode>(V)) { } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
// PN's type is pointer to struct. Make a new PHI of pointer to struct // PN's type is pointer to struct. Make a new PHI of pointer to struct
@ -1185,16 +1173,14 @@ static Value *GetHeapSROAValue(Value *V, unsigned FieldNo,
/// the load, rewrite the derived value to use the HeapSRoA'd load. /// the load, rewrite the derived value to use the HeapSRoA'd load.
static void RewriteHeapSROALoadUser(Instruction *LoadUser, static void RewriteHeapSROALoadUser(Instruction *LoadUser,
DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues, DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite, std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
LLVMContext &Context) {
// If this is a comparison against null, handle it. // If this is a comparison against null, handle it.
if (ICmpInst *SCI = dyn_cast<ICmpInst>(LoadUser)) { if (ICmpInst *SCI = dyn_cast<ICmpInst>(LoadUser)) {
assert(isa<ConstantPointerNull>(SCI->getOperand(1))); assert(isa<ConstantPointerNull>(SCI->getOperand(1)));
// If we have a setcc of the loaded pointer, we can use a setcc of any // If we have a setcc of the loaded pointer, we can use a setcc of any
// field. // field.
Value *NPtr = GetHeapSROAValue(SCI->getOperand(0), 0, Value *NPtr = GetHeapSROAValue(SCI->getOperand(0), 0,
InsertedScalarizedValues, PHIsToRewrite, InsertedScalarizedValues, PHIsToRewrite);
Context);
Value *New = new ICmpInst(SCI, SCI->getPredicate(), NPtr, Value *New = new ICmpInst(SCI, SCI->getPredicate(), NPtr,
Constant::getNullValue(NPtr->getType()), Constant::getNullValue(NPtr->getType()),
@ -1212,8 +1198,7 @@ static void RewriteHeapSROALoadUser(Instruction *LoadUser,
// Load the pointer for this field. // Load the pointer for this field.
unsigned FieldNo = cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue(); unsigned FieldNo = cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue();
Value *NewPtr = GetHeapSROAValue(GEPI->getOperand(0), FieldNo, Value *NewPtr = GetHeapSROAValue(GEPI->getOperand(0), FieldNo,
InsertedScalarizedValues, PHIsToRewrite, InsertedScalarizedValues, PHIsToRewrite);
Context);
// Create the new GEP idx vector. // Create the new GEP idx vector.
SmallVector<Value*, 8> GEPIdx; SmallVector<Value*, 8> GEPIdx;
@ -1245,8 +1230,7 @@ static void RewriteHeapSROALoadUser(Instruction *LoadUser,
// users. // users.
for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); UI != E; ) { for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); UI != E; ) {
Instruction *User = cast<Instruction>(*UI++); Instruction *User = cast<Instruction>(*UI++);
RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite, RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
Context);
} }
} }
@ -1256,13 +1240,11 @@ static void RewriteHeapSROALoadUser(Instruction *LoadUser,
/// AllGlobalLoadUsesSimpleEnoughForHeapSRA. /// AllGlobalLoadUsesSimpleEnoughForHeapSRA.
static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load, static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load,
DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues, DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite, std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
LLVMContext &Context) {
for (Value::use_iterator UI = Load->use_begin(), E = Load->use_end(); for (Value::use_iterator UI = Load->use_begin(), E = Load->use_end();
UI != E; ) { UI != E; ) {
Instruction *User = cast<Instruction>(*UI++); Instruction *User = cast<Instruction>(*UI++);
RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite, RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
Context);
} }
if (Load->use_empty()) { if (Load->use_empty()) {
@ -1276,7 +1258,6 @@ static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load,
static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV,
CallInst *CI, BitCastInst* BCI, CallInst *CI, BitCastInst* BCI,
Value* NElems, Value* NElems,
LLVMContext &Context,
TargetData *TD) { TargetData *TD) {
DEBUG(errs() << "SROA HEAP ALLOC: " << *GV << " MALLOC CALL = " << *CI DEBUG(errs() << "SROA HEAP ALLOC: " << *GV << " MALLOC CALL = " << *CI
<< " BITCAST = " << *BCI << '\n'); << " BITCAST = " << *BCI << '\n');
@ -1306,7 +1287,7 @@ static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV,
GV->isThreadLocal()); GV->isThreadLocal());
FieldGlobals.push_back(NGV); FieldGlobals.push_back(NGV);
Value *NMI = CallInst::CreateMalloc(CI, TD->getIntPtrType(Context), Value *NMI = CallInst::CreateMalloc(CI, TD->getIntPtrType(CI->getContext()),
FieldTy, NElems, FieldTy, NElems,
BCI->getName() + ".f" + Twine(FieldNo)); BCI->getName() + ".f" + Twine(FieldNo));
FieldMallocs.push_back(NMI); FieldMallocs.push_back(NMI);
@ -1342,7 +1323,8 @@ static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV,
// Create the block to check the first condition. Put all these blocks at the // Create the block to check the first condition. Put all these blocks at the
// end of the function as they are unlikely to be executed. // end of the function as they are unlikely to be executed.
BasicBlock *NullPtrBlock = BasicBlock::Create(Context, "malloc_ret_null", BasicBlock *NullPtrBlock = BasicBlock::Create(OrigBB->getContext(),
"malloc_ret_null",
OrigBB->getParent()); OrigBB->getParent());
// Remove the uncond branch from OrigBB to ContBB, turning it into a cond // Remove the uncond branch from OrigBB to ContBB, turning it into a cond
@ -1357,9 +1339,9 @@ static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV,
Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal, Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal,
Constant::getNullValue(GVVal->getType()), Constant::getNullValue(GVVal->getType()),
"tmp"); "tmp");
BasicBlock *FreeBlock = BasicBlock::Create(Context, "free_it", BasicBlock *FreeBlock = BasicBlock::Create(Cmp->getContext(), "free_it",
OrigBB->getParent()); OrigBB->getParent());
BasicBlock *NextBlock = BasicBlock::Create(Context, "next", BasicBlock *NextBlock = BasicBlock::Create(Cmp->getContext(), "next",
OrigBB->getParent()); OrigBB->getParent());
Instruction *BI = BranchInst::Create(FreeBlock, NextBlock, Instruction *BI = BranchInst::Create(FreeBlock, NextBlock,
Cmp, NullPtrBlock); Cmp, NullPtrBlock);
@ -1394,8 +1376,7 @@ static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV,
Instruction *User = cast<Instruction>(*UI++); Instruction *User = cast<Instruction>(*UI++);
if (LoadInst *LI = dyn_cast<LoadInst>(User)) { if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite, RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite);
Context);
continue; continue;
} }
@ -1426,7 +1407,7 @@ static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV,
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
Value *InVal = PN->getIncomingValue(i); Value *InVal = PN->getIncomingValue(i);
InVal = GetHeapSROAValue(InVal, FieldNo, InsertedScalarizedValues, InVal = GetHeapSROAValue(InVal, FieldNo, InsertedScalarizedValues,
PHIsToRewrite, Context); PHIsToRewrite);
FieldPN->addIncoming(InVal, PN->getIncomingBlock(i)); FieldPN->addIncoming(InVal, PN->getIncomingBlock(i));
} }
} }
@ -1465,8 +1446,7 @@ static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV,
CallInst *CI, CallInst *CI,
BitCastInst *BCI, BitCastInst *BCI,
Module::global_iterator &GVI, Module::global_iterator &GVI,
TargetData *TD, TargetData *TD) {
LLVMContext &Context) {
// If we can't figure out the type being malloced, then we can't optimize. // If we can't figure out the type being malloced, then we can't optimize.
const Type *AllocTy = getMallocAllocatedType(CI); const Type *AllocTy = getMallocAllocatedType(CI);
assert(AllocTy); assert(AllocTy);
@ -1499,7 +1479,7 @@ static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV,
// transform the program to use global memory instead of malloc'd memory. // transform the program to use global memory instead of malloc'd memory.
// This eliminates dynamic allocation, avoids an indirection accessing the // This eliminates dynamic allocation, avoids an indirection accessing the
// data, and exposes the resultant global to further GlobalOpt. // data, and exposes the resultant global to further GlobalOpt.
Value *NElems = getMallocArraySize(CI, Context, TD); Value *NElems = getMallocArraySize(CI, TD);
// We cannot optimize the malloc if we cannot determine malloc array size. // We cannot optimize the malloc if we cannot determine malloc array size.
if (NElems) { if (NElems) {
if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems)) if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems))
@ -1508,7 +1488,7 @@ static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV,
// something. // something.
if (TD && if (TD &&
NElements->getZExtValue() * TD->getTypeAllocSize(AllocTy) < 2048) { NElements->getZExtValue() * TD->getTypeAllocSize(AllocTy) < 2048) {
GVI = OptimizeGlobalAddressOfMalloc(GV, CI, BCI, NElems, Context, TD); GVI = OptimizeGlobalAddressOfMalloc(GV, CI, BCI, NElems, TD);
return true; return true;
} }
@ -1532,9 +1512,11 @@ static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV,
// structs. malloc [100 x struct],1 -> malloc struct, 100 // structs. malloc [100 x struct],1 -> malloc struct, 100
if (const ArrayType *AT = if (const ArrayType *AT =
dyn_cast<ArrayType>(getMallocAllocatedType(CI))) { dyn_cast<ArrayType>(getMallocAllocatedType(CI))) {
Value* NumElements = ConstantInt::get(Type::getInt32Ty(Context), Value *NumElements =
ConstantInt::get(Type::getInt32Ty(CI->getContext()),
AT->getNumElements()); AT->getNumElements());
Value* NewMI = CallInst::CreateMalloc(CI, TD->getIntPtrType(Context), Value *NewMI = CallInst::CreateMalloc(CI,
TD->getIntPtrType(CI->getContext()),
AllocSTy, NumElements, AllocSTy, NumElements,
BCI->getName()); BCI->getName());
Value *Cast = new BitCastInst(NewMI, getMallocType(CI), "tmp", CI); Value *Cast = new BitCastInst(NewMI, getMallocType(CI), "tmp", CI);
@ -1545,7 +1527,7 @@ static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV,
CI = extractMallocCallFromBitCast(NewMI); CI = extractMallocCallFromBitCast(NewMI);
} }
GVI = PerformHeapAllocSRoA(GV, CI, BCI, NElems, Context, TD); GVI = PerformHeapAllocSRoA(GV, CI, BCI, NElems, TD);
return true; return true;
} }
} }
@ -1558,7 +1540,7 @@ static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV,
// that only one value (besides its initializer) is ever stored to the global. // that only one value (besides its initializer) is ever stored to the global.
static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal, static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
Module::global_iterator &GVI, Module::global_iterator &GVI,
TargetData *TD, LLVMContext &Context) { TargetData *TD) {
// Ignore no-op GEPs and bitcasts. // Ignore no-op GEPs and bitcasts.
StoredOnceVal = StoredOnceVal->stripPointerCasts(); StoredOnceVal = StoredOnceVal->stripPointerCasts();
@ -1574,7 +1556,7 @@ static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType()); ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType());
// Optimize away any trapping uses of the loaded value. // Optimize away any trapping uses of the loaded value.
if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, Context)) if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC))
return true; return true;
} else if (CallInst *CI = extractMallocCall(StoredOnceVal)) { } else if (CallInst *CI = extractMallocCall(StoredOnceVal)) {
if (getMallocAllocatedType(CI)) { if (getMallocAllocatedType(CI)) {
@ -1582,8 +1564,7 @@ static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
UI != E; ) UI != E; )
BCI = dyn_cast<BitCastInst>(cast<Instruction>(*UI++)); BCI = dyn_cast<BitCastInst>(cast<Instruction>(*UI++));
if (BCI && if (BCI && TryToOptimizeStoreOfMallocToGlobal(GV, CI, BCI, GVI, TD))
TryToOptimizeStoreOfMallocToGlobal(GV, CI, BCI, GVI, TD, Context))
return true; return true;
} }
} }
@ -1596,8 +1577,7 @@ static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
/// two values ever stored into GV are its initializer and OtherVal. See if we /// two values ever stored into GV are its initializer and OtherVal. See if we
/// can shrink the global into a boolean and select between the two values /// can shrink the global into a boolean and select between the two values
/// whenever it is used. This exposes the values to other scalar optimizations. /// whenever it is used. This exposes the values to other scalar optimizations.
static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal, static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
LLVMContext &Context) {
const Type *GVElType = GV->getType()->getElementType(); const Type *GVElType = GV->getType()->getElementType();
// If GVElType is already i1, it is already shrunk. If the type of the GV is // If GVElType is already i1, it is already shrunk. If the type of the GV is
@ -1605,7 +1585,8 @@ static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal,
// between them is very expensive and unlikely to lead to later // between them is very expensive and unlikely to lead to later
// simplification. In these cases, we typically end up with "cond ? v1 : v2" // simplification. In these cases, we typically end up with "cond ? v1 : v2"
// where v1 and v2 both require constant pool loads, a big loss. // where v1 and v2 both require constant pool loads, a big loss.
if (GVElType == Type::getInt1Ty(Context) || GVElType->isFloatingPoint() || if (GVElType == Type::getInt1Ty(GV->getContext()) ||
GVElType->isFloatingPoint() ||
isa<PointerType>(GVElType) || isa<VectorType>(GVElType)) isa<PointerType>(GVElType) || isa<VectorType>(GVElType))
return false; return false;
@ -1618,15 +1599,16 @@ static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal,
DEBUG(errs() << " *** SHRINKING TO BOOL: " << *GV); DEBUG(errs() << " *** SHRINKING TO BOOL: " << *GV);
// Create the new global, initializing it to false. // Create the new global, initializing it to false.
GlobalVariable *NewGV = new GlobalVariable(Context, GlobalVariable *NewGV = new GlobalVariable(Type::getInt1Ty(GV->getContext()),
Type::getInt1Ty(Context), false, false,
GlobalValue::InternalLinkage, ConstantInt::getFalse(Context), GlobalValue::InternalLinkage,
ConstantInt::getFalse(GV->getContext()),
GV->getName()+".b", GV->getName()+".b",
GV->isThreadLocal()); GV->isThreadLocal());
GV->getParent()->getGlobalList().insert(GV, NewGV); GV->getParent()->getGlobalList().insert(GV, NewGV);
Constant *InitVal = GV->getInitializer(); Constant *InitVal = GV->getInitializer();
assert(InitVal->getType() != Type::getInt1Ty(Context) && assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) &&
"No reason to shrink to bool!"); "No reason to shrink to bool!");
// If initialized to zero and storing one into the global, we can use a cast // If initialized to zero and storing one into the global, we can use a cast
@ -1643,7 +1625,8 @@ static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal,
// Only do this if we weren't storing a loaded value. // Only do this if we weren't storing a loaded value.
Value *StoreVal; Value *StoreVal;
if (StoringOther || SI->getOperand(0) == InitVal) if (StoringOther || SI->getOperand(0) == InitVal)
StoreVal = ConstantInt::get(Type::getInt1Ty(Context), StoringOther); StoreVal = ConstantInt::get(Type::getInt1Ty(GV->getContext()),
StoringOther);
else { else {
// Otherwise, we are storing a previously loaded copy. To do this, // Otherwise, we are storing a previously loaded copy. To do this,
// change the copy from copying the original value to just copying the // change the copy from copying the original value to just copying the
@ -1758,8 +1741,7 @@ bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,
// Delete any stores we can find to the global. We may not be able to // Delete any stores we can find to the global. We may not be able to
// make it completely dead though. // make it completely dead though.
bool Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer(), bool Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer());
GV->getContext());
// If the global is dead now, delete it. // If the global is dead now, delete it.
if (GV->use_empty()) { if (GV->use_empty()) {
@ -1774,7 +1756,7 @@ bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,
GV->setConstant(true); GV->setConstant(true);
// Clean up any obviously simplifiable users now. // Clean up any obviously simplifiable users now.
CleanupConstantGlobalUsers(GV, GV->getInitializer(), GV->getContext()); CleanupConstantGlobalUsers(GV, GV->getInitializer());
// If the global is dead now, just nuke it. // If the global is dead now, just nuke it.
if (GV->use_empty()) { if (GV->use_empty()) {
@ -1788,8 +1770,7 @@ bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,
return true; return true;
} else if (!GV->getInitializer()->getType()->isSingleValueType()) { } else if (!GV->getInitializer()->getType()->isSingleValueType()) {
if (TargetData *TD = getAnalysisIfAvailable<TargetData>()) if (TargetData *TD = getAnalysisIfAvailable<TargetData>())
if (GlobalVariable *FirstNewGV = SRAGlobal(GV, *TD, if (GlobalVariable *FirstNewGV = SRAGlobal(GV, *TD)) {
GV->getContext())) {
GVI = FirstNewGV; // Don't skip the newly produced globals! GVI = FirstNewGV; // Don't skip the newly produced globals!
return true; return true;
} }
@ -1804,8 +1785,7 @@ bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,
GV->setInitializer(SOVConstant); GV->setInitializer(SOVConstant);
// Clean up any obviously simplifiable users now. // Clean up any obviously simplifiable users now.
CleanupConstantGlobalUsers(GV, GV->getInitializer(), CleanupConstantGlobalUsers(GV, GV->getInitializer());
GV->getContext());
if (GV->use_empty()) { if (GV->use_empty()) {
DEBUG(errs() << " *** Substituting initializer allowed us to " DEBUG(errs() << " *** Substituting initializer allowed us to "
@ -1822,14 +1802,13 @@ bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,
// Try to optimize globals based on the knowledge that only one value // Try to optimize globals based on the knowledge that only one value
// (besides its initializer) is ever stored to the global. // (besides its initializer) is ever stored to the global.
if (OptimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GVI, if (OptimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GVI,
getAnalysisIfAvailable<TargetData>(), getAnalysisIfAvailable<TargetData>()))
GV->getContext()))
return true; return true;
// Otherwise, if the global was not a boolean, we can shrink it to be a // Otherwise, if the global was not a boolean, we can shrink it to be a
// boolean. // boolean.
if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
if (TryToShrinkGlobalToBoolean(GV, SOVConstant, GV->getContext())) { if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) {
++NumShrunkToBool; ++NumShrunkToBool;
return true; return true;
} }
@ -1981,11 +1960,10 @@ static std::vector<Function*> ParseGlobalCtors(GlobalVariable *GV) {
/// InstallGlobalCtors - Given a specified llvm.global_ctors list, install the /// InstallGlobalCtors - Given a specified llvm.global_ctors list, install the
/// specified array, returning the new global to use. /// specified array, returning the new global to use.
static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL, static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL,
const std::vector<Function*> &Ctors, const std::vector<Function*> &Ctors) {
LLVMContext &Context) {
// If we made a change, reassemble the initializer list. // If we made a change, reassemble the initializer list.
std::vector<Constant*> CSVals; std::vector<Constant*> CSVals;
CSVals.push_back(ConstantInt::get(Type::getInt32Ty(Context), 65535)); CSVals.push_back(ConstantInt::get(Type::getInt32Ty(GCL->getContext()),65535));
CSVals.push_back(0); CSVals.push_back(0);
// Create the new init list. // Create the new init list.
@ -1994,12 +1972,14 @@ static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL,
if (Ctors[i]) { if (Ctors[i]) {
CSVals[1] = Ctors[i]; CSVals[1] = Ctors[i];
} else { } else {
const Type *FTy = FunctionType::get(Type::getVoidTy(Context), false); const Type *FTy = FunctionType::get(Type::getVoidTy(GCL->getContext()),
false);
const PointerType *PFTy = PointerType::getUnqual(FTy); const PointerType *PFTy = PointerType::getUnqual(FTy);
CSVals[1] = Constant::getNullValue(PFTy); CSVals[1] = Constant::getNullValue(PFTy);
CSVals[0] = ConstantInt::get(Type::getInt32Ty(Context), 2147483647); CSVals[0] = ConstantInt::get(Type::getInt32Ty(GCL->getContext()),
2147483647);
} }
CAList.push_back(ConstantStruct::get(Context, CSVals, false)); CAList.push_back(ConstantStruct::get(GCL->getContext(), CSVals, false));
} }
// Create the array initializer. // Create the array initializer.
@ -2015,8 +1995,7 @@ static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL,
} }
// Create the new global and insert it next to the existing list. // Create the new global and insert it next to the existing list.
GlobalVariable *NGV = new GlobalVariable(Context, CA->getType(), GlobalVariable *NGV = new GlobalVariable(CA->getType(), GCL->isConstant(),
GCL->isConstant(),
GCL->getLinkage(), CA, "", GCL->getLinkage(), CA, "",
GCL->isThreadLocal()); GCL->isThreadLocal());
GCL->getParent()->getGlobalList().insert(GCL, NGV); GCL->getParent()->getGlobalList().insert(GCL, NGV);
@ -2050,7 +2029,7 @@ static Constant *getVal(DenseMap<Value*, Constant*> &ComputedValues,
/// enough for us to understand. In particular, if it is a cast of something, /// enough for us to understand. In particular, if it is a cast of something,
/// we punt. We basically just support direct accesses to globals and GEP's of /// we punt. We basically just support direct accesses to globals and GEP's of
/// globals. This should be kept up to date with CommitValueTo. /// globals. This should be kept up to date with CommitValueTo.
static bool isSimpleEnoughPointerToCommit(Constant *C, LLVMContext &Context) { static bool isSimpleEnoughPointerToCommit(Constant *C) {
// Conservatively, avoid aggregate types. This is because we don't // Conservatively, avoid aggregate types. This is because we don't
// want to worry about them partially overlapping other stores. // want to worry about them partially overlapping other stores.
if (!cast<PointerType>(C->getType())->getElementType()->isSingleValueType()) if (!cast<PointerType>(C->getType())->getElementType()->isSingleValueType())
@ -2090,8 +2069,7 @@ static bool isSimpleEnoughPointerToCommit(Constant *C, LLVMContext &Context) {
/// initializer. This returns 'Init' modified to reflect 'Val' stored into it. /// initializer. This returns 'Init' modified to reflect 'Val' stored into it.
/// At this point, the GEP operands of Addr [0, OpNo) have been stepped into. /// At this point, the GEP operands of Addr [0, OpNo) have been stepped into.
static Constant *EvaluateStoreInto(Constant *Init, Constant *Val, static Constant *EvaluateStoreInto(Constant *Init, Constant *Val,
ConstantExpr *Addr, unsigned OpNo, ConstantExpr *Addr, unsigned OpNo) {
LLVMContext &Context) {
// Base case of the recursion. // Base case of the recursion.
if (OpNo == Addr->getNumOperands()) { if (OpNo == Addr->getNumOperands()) {
assert(Val->getType() == Init->getType() && "Type mismatch!"); assert(Val->getType() == Init->getType() && "Type mismatch!");
@ -2120,10 +2098,11 @@ static Constant *EvaluateStoreInto(Constant *Init, Constant *Val,
ConstantInt *CU = cast<ConstantInt>(Addr->getOperand(OpNo)); ConstantInt *CU = cast<ConstantInt>(Addr->getOperand(OpNo));
unsigned Idx = CU->getZExtValue(); unsigned Idx = CU->getZExtValue();
assert(Idx < STy->getNumElements() && "Struct index out of range!"); assert(Idx < STy->getNumElements() && "Struct index out of range!");
Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1, Context); Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1);
// Return the modified struct. // Return the modified struct.
return ConstantStruct::get(Context, &Elts[0], Elts.size(), STy->isPacked()); return ConstantStruct::get(Init->getContext(), &Elts[0], Elts.size(),
STy->isPacked());
} else { } else {
ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo)); ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo));
const ArrayType *ATy = cast<ArrayType>(Init->getType()); const ArrayType *ATy = cast<ArrayType>(Init->getType());
@ -2146,15 +2125,14 @@ static Constant *EvaluateStoreInto(Constant *Init, Constant *Val,
assert(CI->getZExtValue() < ATy->getNumElements()); assert(CI->getZExtValue() < ATy->getNumElements());
Elts[CI->getZExtValue()] = Elts[CI->getZExtValue()] =
EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1, Context); EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1);
return ConstantArray::get(ATy, Elts); return ConstantArray::get(ATy, Elts);
} }
} }
/// CommitValueTo - We have decided that Addr (which satisfies the predicate /// CommitValueTo - We have decided that Addr (which satisfies the predicate
/// isSimpleEnoughPointerToCommit) should get Val as its value. Make it happen. /// isSimpleEnoughPointerToCommit) should get Val as its value. Make it happen.
static void CommitValueTo(Constant *Val, Constant *Addr, static void CommitValueTo(Constant *Val, Constant *Addr) {
LLVMContext &Context) {
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) { if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
assert(GV->hasInitializer()); assert(GV->hasInitializer());
GV->setInitializer(Val); GV->setInitializer(Val);
@ -2165,7 +2143,7 @@ static void CommitValueTo(Constant *Val, Constant *Addr,
GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0)); GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
Constant *Init = GV->getInitializer(); Constant *Init = GV->getInitializer();
Init = EvaluateStoreInto(Init, Val, CE, 2, Context); Init = EvaluateStoreInto(Init, Val, CE, 2);
GV->setInitializer(Init); GV->setInitializer(Init);
} }
@ -2173,8 +2151,7 @@ static void CommitValueTo(Constant *Val, Constant *Addr,
/// P after the stores reflected by 'memory' have been performed. If we can't /// P after the stores reflected by 'memory' have been performed. If we can't
/// decide, return null. /// decide, return null.
static Constant *ComputeLoadResult(Constant *P, static Constant *ComputeLoadResult(Constant *P,
const DenseMap<Constant*, Constant*> &Memory, const DenseMap<Constant*, Constant*> &Memory) {
LLVMContext &Context) {
// If this memory location has been recently stored, use the stored value: it // If this memory location has been recently stored, use the stored value: it
// is the most up-to-date. // is the most up-to-date.
DenseMap<Constant*, Constant*>::const_iterator I = Memory.find(P); DenseMap<Constant*, Constant*>::const_iterator I = Memory.find(P);
@ -2212,8 +2189,6 @@ static bool EvaluateFunction(Function *F, Constant *&RetVal,
if (std::find(CallStack.begin(), CallStack.end(), F) != CallStack.end()) if (std::find(CallStack.begin(), CallStack.end(), F) != CallStack.end())
return false; return false;
LLVMContext &Context = F->getContext();
CallStack.push_back(F); CallStack.push_back(F);
/// Values - As we compute SSA register values, we store their contents here. /// Values - As we compute SSA register values, we store their contents here.
@ -2240,7 +2215,7 @@ static bool EvaluateFunction(Function *F, Constant *&RetVal,
if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) { if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) {
if (SI->isVolatile()) return false; // no volatile accesses. if (SI->isVolatile()) return false; // no volatile accesses.
Constant *Ptr = getVal(Values, SI->getOperand(1)); Constant *Ptr = getVal(Values, SI->getOperand(1));
if (!isSimpleEnoughPointerToCommit(Ptr, Context)) if (!isSimpleEnoughPointerToCommit(Ptr))
// If this is too complex for us to commit, reject it. // If this is too complex for us to commit, reject it.
return false; return false;
Constant *Val = getVal(Values, SI->getOperand(0)); Constant *Val = getVal(Values, SI->getOperand(0));
@ -2274,12 +2249,12 @@ static bool EvaluateFunction(Function *F, Constant *&RetVal,
} else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) { } else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) {
if (LI->isVolatile()) return false; // no volatile accesses. if (LI->isVolatile()) return false; // no volatile accesses.
InstResult = ComputeLoadResult(getVal(Values, LI->getOperand(0)), InstResult = ComputeLoadResult(getVal(Values, LI->getOperand(0)),
MutatedMemory, Context); MutatedMemory);
if (InstResult == 0) return false; // Could not evaluate load. if (InstResult == 0) return false; // Could not evaluate load.
} else if (AllocaInst *AI = dyn_cast<AllocaInst>(CurInst)) { } else if (AllocaInst *AI = dyn_cast<AllocaInst>(CurInst)) {
if (AI->isArrayAllocation()) return false; // Cannot handle array allocs. if (AI->isArrayAllocation()) return false; // Cannot handle array allocs.
const Type *Ty = AI->getType()->getElementType(); const Type *Ty = AI->getType()->getElementType();
AllocaTmps.push_back(new GlobalVariable(Context, Ty, false, AllocaTmps.push_back(new GlobalVariable(Ty, false,
GlobalValue::InternalLinkage, GlobalValue::InternalLinkage,
UndefValue::get(Ty), UndefValue::get(Ty),
AI->getName())); AI->getName()));
@ -2417,7 +2392,7 @@ static bool EvaluateStaticConstructor(Function *F) {
<< " stores.\n"); << " stores.\n");
for (DenseMap<Constant*, Constant*>::iterator I = MutatedMemory.begin(), for (DenseMap<Constant*, Constant*>::iterator I = MutatedMemory.begin(),
E = MutatedMemory.end(); I != E; ++I) E = MutatedMemory.end(); I != E; ++I)
CommitValueTo(I->second, I->first, F->getContext()); CommitValueTo(I->second, I->first);
} }
// At this point, we are done interpreting. If we created any 'alloca' // At this point, we are done interpreting. If we created any 'alloca'
@ -2474,7 +2449,7 @@ bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) {
if (!MadeChange) return false; if (!MadeChange) return false;
GCL = InstallGlobalCtors(GCL, Ctors, GCL->getContext()); GCL = InstallGlobalCtors(GCL, Ctors);
return true; return true;
} }

View File

@ -66,7 +66,7 @@ bool ConstantPropagation::runOnFunction(Function &F) {
WorkList.erase(WorkList.begin()); // Get an element from the worklist... WorkList.erase(WorkList.begin()); // Get an element from the worklist...
if (!I->use_empty()) // Don't muck with dead instructions... if (!I->use_empty()) // Don't muck with dead instructions...
if (Constant *C = ConstantFoldInstruction(I, F.getContext())) { if (Constant *C = ConstantFoldInstruction(I)) {
// Add all of the users of this instruction to the worklist, they might // Add all of the users of this instruction to the worklist, they might
// be constant propagatable now... // be constant propagatable now...
for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();

View File

@ -12824,7 +12824,7 @@ static bool AddReachableCodeToWorklist(BasicBlock *BB,
// ConstantProp instruction if trivially constant. // ConstantProp instruction if trivially constant.
if (!Inst->use_empty() && isa<Constant>(Inst->getOperand(0))) if (!Inst->use_empty() && isa<Constant>(Inst->getOperand(0)))
if (Constant *C = ConstantFoldInstruction(Inst, BB->getContext(), TD)) { if (Constant *C = ConstantFoldInstruction(Inst, TD)) {
DEBUG(errs() << "IC: ConstFold to: " << *C << " from: " DEBUG(errs() << "IC: ConstFold to: " << *C << " from: "
<< *Inst << '\n'); << *Inst << '\n');
Inst->replaceAllUsesWith(C); Inst->replaceAllUsesWith(C);
@ -12846,8 +12846,7 @@ static bool AddReachableCodeToWorklist(BasicBlock *BB,
if (!FoldedConstants.insert(CE)) if (!FoldedConstants.insert(CE))
continue; continue;
Constant *NewC = Constant *NewC = ConstantFoldConstantExpression(CE, TD);
ConstantFoldConstantExpression(CE, BB->getContext(), TD);
if (NewC && NewC != CE) { if (NewC && NewC != CE) {
*i = NewC; *i = NewC;
MadeIRChange = true; MadeIRChange = true;
@ -12954,7 +12953,7 @@ bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {
// Instruction isn't dead, see if we can constant propagate it. // Instruction isn't dead, see if we can constant propagate it.
if (!I->use_empty() && isa<Constant>(I->getOperand(0))) if (!I->use_empty() && isa<Constant>(I->getOperand(0)))
if (Constant *C = ConstantFoldInstruction(I, F.getContext(), TD)) { if (Constant *C = ConstantFoldInstruction(I, TD)) {
DEBUG(errs() << "IC: ConstFold to: " << *C << " from: " << *I << '\n'); DEBUG(errs() << "IC: ConstFold to: " << *C << " from: " << *I << '\n');
// Add operands to the worklist. // Add operands to the worklist.

View File

@ -1044,7 +1044,7 @@ bool JumpThreading::ThreadEdge(BasicBlock *BB, BasicBlock *PredBB,
BI = NewBB->begin(); BI = NewBB->begin();
for (BasicBlock::iterator E = NewBB->end(); BI != E; ) { for (BasicBlock::iterator E = NewBB->end(); BI != E; ) {
Instruction *Inst = BI++; Instruction *Inst = BI++;
if (Constant *C = ConstantFoldInstruction(Inst, BB->getContext(), TD)) { if (Constant *C = ConstantFoldInstruction(Inst, TD)) {
Inst->replaceAllUsesWith(C); Inst->replaceAllUsesWith(C);
Inst->eraseFromParent(); Inst->eraseFromParent();
continue; continue;

View File

@ -32,7 +32,6 @@
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/Function.h" #include "llvm/Function.h"
#include "llvm/Instructions.h" #include "llvm/Instructions.h"
#include "llvm/LLVMContext.h"
#include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/InlineCost.h" #include "llvm/Analysis/InlineCost.h"
#include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/LoopInfo.h"
@ -961,7 +960,7 @@ void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L) {
Worklist.pop_back(); Worklist.pop_back();
// Simple constant folding. // Simple constant folding.
if (Constant *C = ConstantFoldInstruction(I, I->getContext())) { if (Constant *C = ConstantFoldInstruction(I)) {
ReplaceUsesOfWith(I, C, Worklist, L, LPM); ReplaceUsesOfWith(I, C, Worklist, L, LPM);
continue; continue;
} }

View File

@ -359,8 +359,7 @@ void TailDup::eliminateUnconditionalBranch(BranchInst *Branch) {
Instruction *Inst = BI++; Instruction *Inst = BI++;
if (isInstructionTriviallyDead(Inst)) if (isInstructionTriviallyDead(Inst))
Inst->eraseFromParent(); Inst->eraseFromParent();
else if (Constant *C = ConstantFoldInstruction(Inst, else if (Constant *C = ConstantFoldInstruction(Inst)) {
Inst->getContext())) {
Inst->replaceAllUsesWith(C); Inst->replaceAllUsesWith(C);
Inst->eraseFromParent(); Inst->eraseFromParent();
} }

View File

@ -322,8 +322,6 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
/// mapping its operands through ValueMap if they are available. /// mapping its operands through ValueMap if they are available.
Constant *PruningFunctionCloner:: Constant *PruningFunctionCloner::
ConstantFoldMappedInstruction(const Instruction *I) { ConstantFoldMappedInstruction(const Instruction *I) {
LLVMContext &Context = I->getContext();
SmallVector<Constant*, 8> Ops; SmallVector<Constant*, 8> Ops;
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
if (Constant *Op = dyn_cast_or_null<Constant>(MapValue(I->getOperand(i), if (Constant *Op = dyn_cast_or_null<Constant>(MapValue(I->getOperand(i),
@ -334,8 +332,7 @@ ConstantFoldMappedInstruction(const Instruction *I) {
if (const CmpInst *CI = dyn_cast<CmpInst>(I)) if (const CmpInst *CI = dyn_cast<CmpInst>(I))
return ConstantFoldCompareInstOperands(CI->getPredicate(), return ConstantFoldCompareInstOperands(CI->getPredicate(),
&Ops[0], Ops.size(), &Ops[0], Ops.size(), TD);
Context, TD);
if (const LoadInst *LI = dyn_cast<LoadInst>(I)) if (const LoadInst *LI = dyn_cast<LoadInst>(I))
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0]))
@ -346,7 +343,7 @@ ConstantFoldMappedInstruction(const Instruction *I) {
CE); CE);
return ConstantFoldInstOperands(I->getOpcode(), I->getType(), &Ops[0], return ConstantFoldInstOperands(I->getOpcode(), I->getType(), &Ops[0],
Ops.size(), Context, TD); Ops.size(), TD);
} }
/// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto, /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto,

View File

@ -362,8 +362,7 @@ bool llvm::UnrollLoop(Loop *L, unsigned Count, LoopInfo* LI, LPPassManager* LPM)
if (isInstructionTriviallyDead(Inst)) if (isInstructionTriviallyDead(Inst))
(*BB)->getInstList().erase(Inst); (*BB)->getInstList().erase(Inst);
else if (Constant *C = ConstantFoldInstruction(Inst, else if (Constant *C = ConstantFoldInstruction(Inst)) {
Header->getContext())) {
Inst->replaceAllUsesWith(C); Inst->replaceAllUsesWith(C);
(*BB)->getInstList().erase(Inst); (*BB)->getInstList().erase(Inst);
} }

View File

@ -1217,7 +1217,7 @@ static bool FoldCondBranchOnPHI(BranchInst *BI) {
} }
// Check for trivial simplification. // Check for trivial simplification.
if (Constant *C = ConstantFoldInstruction(N, BB->getContext())) { if (Constant *C = ConstantFoldInstruction(N)) {
TranslateMap[BBI] = C; TranslateMap[BBI] = C;
delete N; // Constant folded away, don't need actual inst delete N; // Constant folded away, don't need actual inst
} else { } else {

View File

@ -16,7 +16,6 @@
#include "llvm/GlobalVariable.h" #include "llvm/GlobalVariable.h"
#include "llvm/GlobalAlias.h" #include "llvm/GlobalAlias.h"
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorHandling.h"
@ -95,8 +94,7 @@ void GlobalValue::copyAttributesFrom(const GlobalValue *Src) {
// GlobalVariable Implementation // GlobalVariable Implementation
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
GlobalVariable::GlobalVariable(LLVMContext &Context, const Type *Ty, GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
bool constant, LinkageTypes Link,
Constant *InitVal, const Twine &Name, Constant *InitVal, const Twine &Name,
bool ThreadLocal, unsigned AddressSpace) bool ThreadLocal, unsigned AddressSpace)
: GlobalValue(PointerType::get(Ty, AddressSpace), : GlobalValue(PointerType::get(Ty, AddressSpace),

View File

@ -31,8 +31,7 @@ using namespace llvm;
// //
GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() { GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() {
GlobalVariable *Ret = new GlobalVariable(getGlobalContext(), GlobalVariable *Ret = new GlobalVariable(Type::getInt32Ty(getGlobalContext()),
Type::getInt32Ty(getGlobalContext()),
false, GlobalValue::ExternalLinkage); false, GlobalValue::ExternalLinkage);
// This should not be garbage monitored. // This should not be garbage monitored.
LeakDetector::removeGarbageObject(Ret); LeakDetector::removeGarbageObject(Ret);