1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-18 18:42:46 +02:00

Begin the painful process of tearing apart the rat'ss nest that is Constants.cpp and ConstantFold.cpp.

This involves temporarily hard wiring some parts to use the global context.  This isn't ideal, but it's
the only way I could figure out to make this process vaguely incremental.

llvm-svn: 75445
This commit is contained in:
Owen Anderson 2009-07-13 04:09:18 +00:00
parent fee90bcb80
commit 393d8b0a0c
43 changed files with 547 additions and 480 deletions

View File

@ -856,7 +856,7 @@ the loop again and exiting the loop. Any future code is emitted in the
NamedValues.erase(VarName);
// for expr always returns 0.0.
return Constant::getNullValue(Type::DoubleTy);
return TheFunction->getContext()->getNullValue(Type::DoubleTy);
}
</pre>
</div>

View File

@ -1570,7 +1570,7 @@ Value *ForExprAST::Codegen() {
// for expr always returns 0.0.
return Constant::getNullValue(Type::DoubleTy);
return TheFunction->getContext()->getNullValue(Type::DoubleTy);
}
Function *PrototypeAST::Codegen() {

View File

@ -1858,7 +1858,7 @@ Value *ForExprAST::Codegen() {
// for expr always returns 0.0.
return Constant::getNullValue(Type::DoubleTy);
return TheFunction->getContext()->getNullValue(Type::DoubleTy);
}
Value *VarExprAST::Codegen() {

View File

@ -145,7 +145,7 @@ void BrainF::header(LLVMContext& C) {
//call i32 @puts(i8 *getelementptr([%d x i8] *@aberrormsg, i32 0, i32 0))
{
Constant *zero_32 = Constant::getNullValue(IntegerType::Int32Ty);
Constant *zero_32 = C.getNullValue(IntegerType::Int32Ty);
Constant *gep_params[] = {
zero_32,

View File

@ -854,7 +854,7 @@ Value *ForExprAST::Codegen() {
// for expr always returns 0.0.
return Constant::getNullValue(Type::DoubleTy);
return TheFunction->getContext()->getNullValue(Type::DoubleTy);
}
Value *VarExprAST::Codegen() {

View File

@ -60,6 +60,7 @@ protected:
const TargetData &TD;
const TargetInstrInfo &TII;
const TargetLowering &TLI;
LLVMContext *Context;
public:
/// startNewBlock - Set the current block to which generated machine

View File

@ -18,6 +18,7 @@
namespace llvm {
template<typename T> class SmallVectorImpl;
class LLVMContext;
/// If object contains references to other objects, then relocations are
/// usually required for emission of such object (especially in PIC mode). One
@ -59,10 +60,6 @@ protected:
void destroyConstantImpl();
public:
/// Static constructor to get a '0' constant of arbitrary type...
///
static Constant *getNullValue(const Type *Ty);
/// Static constructor to get a '-1' constant. This supports integers and
/// vectors.
///
@ -98,7 +95,8 @@ public:
/// type, returns the elements of the vector in the specified smallvector.
/// This handles breaking down a vector undef into undef elements, etc. For
/// constant exprs and other cases we can't handle, we return an empty vector.
void getVectorElements(SmallVectorImpl<Constant*> &Elts) const;
void getVectorElements(LLVMContext &Context,
SmallVectorImpl<Constant*> &Elts) const;
/// destroyConstant - Called if some element of this constant is no longer
/// valid. At this point only other constants may be on the use_list for this

View File

@ -276,9 +276,6 @@ public:
/// considers -0.0 to be null as well as 0.0. :(
virtual bool isNullValue() const;
// Get a negative zero.
static ConstantFP *getNegativeZero(const Type* Ty);
/// isExactlyValue - We don't rely on operator== working on double values, as
/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
/// As such, this method can be used to do an exact bit-for-bit comparison of
@ -383,7 +380,7 @@ public:
/// isString) and it ends in a null byte \0 and does not contains any other
/// @endverbatim
/// null bytes except its terminator.
bool isCString() const;
bool isCString(LLVMContext &Context) const;
/// getAsString - If this array is isString(), then this method converts the
/// array to an std::string and returns it. Otherwise, it asserts out.
@ -694,17 +691,6 @@ public:
return getSelectTy(V1->getType(), C, V1, V2);
}
/// getAlignOf constant expr - computes the alignment of a type in a target
/// independent way (Note: the return type is an i32; Note: assumes that i8
/// is byte aligned).
///
static Constant *getAlignOf(const Type *Ty);
/// getSizeOf constant expr - computes the size of a type in a target
/// independent way (Note: the return type is an i64).
///
static Constant *getSizeOf(const Type *Ty);
/// ConstantExpr::get - Return a binary or shift operator constant expression,
/// folding if possible.
///
@ -716,8 +702,6 @@ public:
/// ConstantExpr::get* - Return some common constants without having to
/// specify the full Instruction::OPCODE identifier.
///
static Constant *getNeg(Constant *C);
static Constant *getFNeg(Constant *C);
static Constant *getNot(Constant *C);
static Constant *getAdd(Constant *C1, Constant *C2);
static Constant *getFAdd(Constant *C1, Constant *C2);
@ -756,11 +740,6 @@ public:
static Constant *getInsertValue(Constant *Agg, Constant *Val,
const unsigned *IdxList, unsigned NumIdx);
/// Floating point negation must be implemented with f(x) = -0.0 - x. This
/// method returns the negative zero constant for floating point or vector
/// floating point types; for all other types, it returns the null value.
static Constant *getZeroValueForNegationExpr(const Type *Ty);
/// isNullValue - Return true if this is the value that would be returned by
/// getNullValue.
virtual bool isNullValue() const { return false; }

View File

@ -202,13 +202,17 @@ public:
/// CreateNeg, CreateNot - Create the NEG and NOT
/// instructions out of SUB and XOR instructions.
///
static BinaryOperator *CreateNeg(Value *Op, const std::string &Name = "",
static BinaryOperator *CreateNeg(LLVMContext &Context,
Value *Op, const std::string &Name = "",
Instruction *InsertBefore = 0);
static BinaryOperator *CreateNeg(Value *Op, const std::string &Name,
static BinaryOperator *CreateNeg(LLVMContext &Context,
Value *Op, const std::string &Name,
BasicBlock *InsertAtEnd);
static BinaryOperator *CreateFNeg(Value *Op, const std::string &Name = "",
static BinaryOperator *CreateFNeg(LLVMContext &Context,
Value *Op, const std::string &Name = "",
Instruction *InsertBefore = 0);
static BinaryOperator *CreateFNeg(Value *Op, const std::string &Name,
static BinaryOperator *CreateFNeg(LLVMContext &Context,
Value *Op, const std::string &Name,
BasicBlock *InsertAtEnd);
static BinaryOperator *CreateNot(Value *Op, const std::string &Name = "",
Instruction *InsertBefore = 0);
@ -218,8 +222,8 @@ public:
/// isNeg, isFNeg, isNot - Check if the given Value is a
/// NEG, FNeg, or NOT instruction.
///
static bool isNeg(const Value *V);
static bool isFNeg(const Value *V);
static bool isNeg(LLVMContext &Context, const Value *V);
static bool isFNeg(LLVMContext &Context, const Value *V);
static bool isNot(const Value *V);
/// getNegArgument, getNotArgument - Helper functions to extract the

View File

@ -120,6 +120,11 @@ public:
bool isSigned);
Constant* getConstantExprFPCast(Constant* C, const Type* Ty);
Constant* getConstantExprSelect(Constant* C, Constant* V1, Constant* V2);
/// getAlignOf constant expr - computes the alignment of a type in a target
/// independent way (Note: the return type is an i32; Note: assumes that i8
/// is byte aligned).
///
Constant* getConstantExprAlignOf(const Type* Ty);
Constant* getConstantExprCompare(unsigned short pred,
Constant* C1, Constant* C2);
@ -162,7 +167,15 @@ public:
Constant* getConstantExprInsertValue(Constant* Agg, Constant* Val,
const unsigned* IdxList,
unsigned NumIdx);
/// getSizeOf constant expr - computes the size of a type in a target
/// independent way (Note: the return type is an i64).
///
Constant* getConstantExprSizeOf(const Type* Ty);
/// Floating point negation must be implemented with f(x) = -0.0 - x. This
/// method returns the negative zero constant for floating point or vector
/// floating point types; for all other types, it returns the null value.
Constant* getZeroValueForNegation(const Type* Ty);
// ConstantFP accessors
@ -200,6 +213,7 @@ public:
StructType* getStructType(bool isPacked=false);
StructType* getStructType(const std::vector<const Type*>& Params,
bool isPacked = false);
StructType* getStructType(const Type* type, ...);
// ArrayType accessors
ArrayType* getArrayType(const Type* ElementType, uint64_t NumElements);

View File

@ -311,12 +311,12 @@ public:
Value *CreateNeg(Value *V, const char *Name = "") {
if (Constant *VC = dyn_cast<Constant>(V))
return Folder.CreateNeg(VC);
return Insert(BinaryOperator::CreateNeg(V), Name);
return Insert(BinaryOperator::CreateNeg(getGlobalContext(), V), Name);
}
Value *CreateFNeg(Value *V, const char *Name = "") {
if (Constant *VC = dyn_cast<Constant>(V))
return Folder.CreateFNeg(VC);
return Insert(BinaryOperator::CreateFNeg(V), Name);
return Insert(BinaryOperator::CreateFNeg(getGlobalContext(), V), Name);
}
Value *CreateNot(Value *V, const char *Name = "") {
if (Constant *VC = dyn_cast<Constant>(V))

View File

@ -463,7 +463,7 @@ DIFactory::DIFactory(Module &m)
/// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
/// This is only valid when the descriptor is non-null.
Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
if (D.isNull()) return Constant::getNullValue(EmptyStructPtr);
if (D.isNull()) return VMContext.getNullValue(EmptyStructPtr);
return VMContext.getConstantExprBitCast(D.getGV(), EmptyStructPtr);
}

View File

@ -693,7 +693,7 @@ void Andersens::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
// If the object in the points-to set is the null object, then the null
// pointer is a must alias.
if (Pointee == &GraphNodes[NullObject])
RetVals.push_back(Constant::getNullValue(P->getType()));
RetVals.push_back(Context->getNullValue(P->getType()));
}
}
AliasAnalysis::getMustAliases(P, RetVals);

View File

@ -2084,7 +2084,8 @@ const SCEV *ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) {
///
const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) {
if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
return getConstant(cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue())));
return getConstant(
cast<ConstantInt>(Context->getConstantExprNeg(VC->getValue())));
const Type *Ty = V->getType();
Ty = getEffectiveSCEVType(Ty);
@ -3284,7 +3285,7 @@ EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C,
/// the addressed element of the initializer or null if the index expression is
/// invalid.
static Constant *
GetAddressedElementFromGlobal(GlobalVariable *GV,
GetAddressedElementFromGlobal(LLVMContext *Context, GlobalVariable *GV,
const std::vector<ConstantInt*> &Indices) {
Constant *Init = GV->getInitializer();
for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
@ -3298,10 +3299,10 @@ GetAddressedElementFromGlobal(GlobalVariable *GV,
} else if (isa<ConstantAggregateZero>(Init)) {
if (const StructType *STy = dyn_cast<StructType>(Init->getType())) {
assert(Idx < STy->getNumElements() && "Bad struct index!");
Init = Constant::getNullValue(STy->getElementType(Idx));
Init = Context->getNullValue(STy->getElementType(Idx));
} else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) {
if (Idx >= ATy->getNumElements()) return 0; // Bogus program
Init = Constant::getNullValue(ATy->getElementType());
Init = Context->getNullValue(ATy->getElementType());
} else {
LLVM_UNREACHABLE("Unknown constant aggregate type!");
}
@ -3372,7 +3373,7 @@ ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount(
// Form the GEP offset.
Indexes[VarIdxNum] = Val;
Constant *Result = GetAddressedElementFromGlobal(GV, Indexes);
Constant *Result = GetAddressedElementFromGlobal(Context, GV, Indexes);
if (Result == 0) break; // Cannot compute!
// Evaluate the condition for this iteration.

View File

@ -315,6 +315,7 @@ static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
IRBuilder<> Builder(CI->getParent(), CI);
LLVMContext *Context = CI->getParent()->getContext();
Function *Callee = CI->getCalledFunction();
assert(Callee && "Cannot lower an indirect call!");
@ -340,7 +341,7 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
}
case Intrinsic::sigsetjmp:
if (CI->getType() != Type::VoidTy)
CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
CI->replaceAllUsesWith(Context->getNullValue(CI->getType()));
break;
case Intrinsic::longjmp: {
@ -387,7 +388,7 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
"save" : "restore") << " intrinsic.\n";
Warned = true;
if (Callee->getIntrinsicID() == Intrinsic::stacksave)
CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
CI->replaceAllUsesWith(Context->getNullValue(CI->getType()));
break;
}
@ -422,7 +423,7 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
case Intrinsic::eh_exception:
case Intrinsic::eh_selector_i32:
case Intrinsic::eh_selector_i64:
CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
CI->replaceAllUsesWith(Context->getNullValue(CI->getType()));
break;
case Intrinsic::eh_typeid_for_i32:

View File

@ -123,7 +123,7 @@ bool MachOWriter::doFinalization(Module &M) {
// getConstSection - Get constant section for Constant 'C'
MachOSection *MachOWriter::getConstSection(Constant *C) {
const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
if (CVA && CVA->isCString())
if (CVA && CVA->isCString(*Context))
return getSection("__TEXT", "__cstring",
MachOSection::S_CSTRING_LITERALS);

View File

@ -92,7 +92,7 @@ unsigned FastISel::getRegForValue(Value *V) {
} else if (isa<ConstantPointerNull>(V)) {
// Translate this as an integer zero so that it can be
// local-CSE'd with actual integer zeros.
Reg = getRegForValue(Constant::getNullValue(TD.getIntPtrType()));
Reg = getRegForValue(Context->getNullValue(TD.getIntPtrType()));
} else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
@ -480,7 +480,7 @@ bool FastISel::SelectCall(User *I) {
UpdateValueMap(I, ResultReg);
} else {
unsigned ResultReg =
getRegForValue(Constant::getNullValue(I->getType()));
getRegForValue(Context->getNullValue(I->getType()));
UpdateValueMap(I, ResultReg);
}
return true;
@ -753,7 +753,8 @@ FastISel::FastISel(MachineFunction &mf,
TM(MF.getTarget()),
TD(*TM.getTargetData()),
TII(*TM.getInstrInfo()),
TLI(*TM.getTargetLowering()) {
TLI(*TM.getTargetLowering()),
Context(mf.getFunction()->getContext()) {
}
FastISel::~FastISel() {}

View File

@ -2139,7 +2139,7 @@ void SelectionDAGLowering::visitFSub(User &I) {
const VectorType *DestTy = cast<VectorType>(I.getType());
const Type *ElTy = DestTy->getElementType();
unsigned VL = DestTy->getNumElements();
std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
std::vector<Constant*> NZ(VL, Context->getConstantFPNegativeZero(ElTy));
Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
if (CV == CNZ) {
SDValue Op2 = getValue(I.getOperand(1));
@ -2150,7 +2150,8 @@ void SelectionDAGLowering::visitFSub(User &I) {
}
}
if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
if (CFP->isExactlyValue(
Context->getConstantFPNegativeZero(Ty)->getValueAPF())) {
SDValue Op2 = getValue(I.getOperand(1));
setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
Op2.getValueType(), Op2));
@ -2398,7 +2399,7 @@ void SelectionDAGLowering::visitShuffleVector(User &I) {
// Convert the ConstantVector mask operand into an array of ints, with -1
// representing undef values.
SmallVector<Constant*, 8> MaskElts;
cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
cast<Constant>(I.getOperand(2))->getVectorElements(*Context, MaskElts);
unsigned MaskNumElts = MaskElts.size();
for (unsigned i = 0; i != MaskNumElts; ++i) {
if (isa<UndefValue>(MaskElts[i]))

View File

@ -15,6 +15,7 @@
#define SELECTIONDAGBUILD_H
#include "llvm/Constants.h"
#include "llvm/CodeGen/SelectionDAG.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/DenseMap.h"
#ifndef NDEBUG
@ -362,11 +363,14 @@ public:
/// GFI - Garbage collection metadata for the function.
GCFunctionInfo *GFI;
LLVMContext *Context;
SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli,
FunctionLoweringInfo &funcinfo,
CodeGenOpt::Level ol)
: CurDebugLoc(DebugLoc::getUnknownLoc()),
TLI(tli), DAG(dag), FuncInfo(funcinfo), OptLevel(ol) {
TLI(tli), DAG(dag), FuncInfo(funcinfo), OptLevel(ol),
Context(dag.getContext()) {
}
void init(GCFunctionInfo *gfi, AliasAnalysis &aa);

View File

@ -293,10 +293,10 @@ bool ShadowStackGC::initializeCustomLowering(Module &M) {
// linkage!
Head = new GlobalVariable(M, StackEntryPtrTy, false,
GlobalValue::LinkOnceAnyLinkage,
Constant::getNullValue(StackEntryPtrTy),
M.getContext().getNullValue(StackEntryPtrTy),
"llvm_gc_root_chain");
} else if (Head->hasExternalLinkage() && Head->isDeclaration()) {
Head->setInitializer(Constant::getNullValue(StackEntryPtrTy));
Head->setInitializer(M.getContext().getNullValue(StackEntryPtrTy));
Head->setLinkage(GlobalValue::LinkOnceAnyLinkage);
}

View File

@ -68,7 +68,7 @@ bool UnreachableBlockElim::runOnFunction(Function &F) {
BasicBlock *BB = I;
DeadBlocks.push_back(BB);
while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
PN->replaceAllUsesWith(Context->getNullValue(PN->getType()));
BB->getInstList().pop_front();
}
for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)

View File

@ -1239,7 +1239,7 @@ void CWriter::printConstant(Constant *CPV, bool Static) {
Out << '{';
if (AT->getNumElements()) {
Out << ' ';
Constant *CZ = Constant::getNullValue(AT->getElementType());
Constant *CZ = Context->getNullValue(AT->getElementType());
printConstant(CZ, Static);
for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) {
Out << ", ";
@ -1264,7 +1264,7 @@ void CWriter::printConstant(Constant *CPV, bool Static) {
assert(isa<ConstantAggregateZero>(CPV) || isa<UndefValue>(CPV));
const VectorType *VT = cast<VectorType>(CPV->getType());
Out << "{ ";
Constant *CZ = Constant::getNullValue(VT->getElementType());
Constant *CZ = Context->getNullValue(VT->getElementType());
printConstant(CZ, Static);
for (unsigned i = 1, e = VT->getNumElements(); i != e; ++i) {
Out << ", ";
@ -1286,10 +1286,10 @@ void CWriter::printConstant(Constant *CPV, bool Static) {
Out << '{';
if (ST->getNumElements()) {
Out << ' ';
printConstant(Constant::getNullValue(ST->getElementType(0)), Static);
printConstant(Context->getNullValue(ST->getElementType(0)), Static);
for (unsigned i = 1, e = ST->getNumElements(); i != e; ++i) {
Out << ", ";
printConstant(Constant::getNullValue(ST->getElementType(i)), Static);
printConstant(Context->getNullValue(ST->getElementType(i)), Static);
}
}
Out << " }";
@ -2621,11 +2621,11 @@ void CWriter::visitBinaryOperator(Instruction &I) {
// If this is a negation operation, print it out as such. For FP, we don't
// want to print "-0.0 - X".
if (BinaryOperator::isNeg(&I)) {
if (BinaryOperator::isNeg(*Context, &I)) {
Out << "-(";
writeOperand(BinaryOperator::getNegArgument(cast<BinaryOperator>(&I)));
Out << ")";
} else if (BinaryOperator::isFNeg(&I)) {
} else if (BinaryOperator::isFNeg(*Context, &I)) {
Out << "-(";
writeOperand(BinaryOperator::getFNegArgument(cast<BinaryOperator>(&I)));
Out << ")";

View File

@ -541,7 +541,7 @@ printModuleLevelGV(const GlobalVariable* GVar) {
// Fall Through
case GlobalValue::PrivateLinkage:
case GlobalValue::InternalLinkage:
if (CVA && CVA->isCString())
if (CVA && CVA->isCString(GVar->getParent()->getContext()))
printSizeAndType = false;
break;
case GlobalValue::GhostLinkage:

View File

@ -227,7 +227,7 @@ bool MipsTargetLowering::IsGlobalInSmallSection(GlobalValue *GV)
if (GVA->hasInitializer() && GV->hasLocalLinkage()) {
Constant *C = GVA->getInitializer();
const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
if (CVA && CVA->isCString())
if (CVA && CVA->isCString(GV->getParent()->getContext()))
return false;
}

View File

@ -170,11 +170,11 @@ static bool isSuitableForBSS(const GlobalVariable *GV) {
return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS);
}
static bool isConstantString(const Constant *C) {
static bool isConstantString(LLVMContext &Context, const Constant *C) {
// First check: is we have constant array of i8 terminated with zero
const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
// Check, if initializer is a null-terminated string
if (CVA && CVA->isCString())
if (CVA && CVA->isCString(Context))
return true;
// Another possibility: [1 x i8] zeroinitializer
@ -229,7 +229,7 @@ TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
}
} else {
// Check, if initializer is a null-terminated string
if (isConstantString(C))
if (isConstantString(GV->getParent()->getContext(), C))
return SectionKind::RODataMergeStr;
else
return SectionKind::RODataMergeConst;

View File

@ -272,7 +272,7 @@ bool X86FastISel::X86FastEmitStore(MVT VT, Value *Val,
const X86AddressMode &AM) {
// Handle 'null' like i32/i64 0.
if (isa<ConstantPointerNull>(Val))
Val = Constant::getNullValue(TD.getIntPtrType());
Val = Context->getNullValue(TD.getIntPtrType());
// If this is a store of a simple constant, fold the constant into the store.
if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
@ -672,7 +672,7 @@ bool X86FastISel::X86FastEmitCompare(Value *Op0, Value *Op1, MVT VT) {
// Handle 'null' like i32/i64 0.
if (isa<ConstantPointerNull>(Op1))
Op1 = Constant::getNullValue(TD.getIntPtrType());
Op1 = Context->getNullValue(TD.getIntPtrType());
// We have two options: compare with register or immediate. If the RHS of
// the compare is an immediate that we can fold into this compare, use

View File

@ -20,6 +20,7 @@
#include "X86TargetMachine.h"
#include "llvm/GlobalVariable.h"
#include "llvm/DerivedTypes.h"
#include "llvm/LLVMContext.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/CodeGen/MachineConstantPool.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
@ -2312,7 +2313,7 @@ MachineInstr* X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
MachineConstantPool &MCP = *MF.getConstantPool();
const VectorType *Ty = VectorType::get(Type::Int32Ty, 4);
Constant *C = LoadMI->getOpcode() == X86::V_SET0 ?
ConstantVector::getNullValue(Ty) :
MF.getFunction()->getContext()->getNullValue(Ty) :
ConstantVector::getAllOnesValue(Ty);
unsigned CPI = MCP.getConstantPoolIndex(C, 16);

View File

@ -408,9 +408,10 @@ X("instcombine", "Combine redundant instructions");
// getComplexity: Assign a complexity or rank value to LLVM Values...
// 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst
static unsigned getComplexity(Value *V) {
static unsigned getComplexity(LLVMContext *Context, Value *V) {
if (isa<Instruction>(V)) {
if (BinaryOperator::isNeg(V) || BinaryOperator::isFNeg(V) ||
if (BinaryOperator::isNeg(*Context, V) ||
BinaryOperator::isFNeg(*Context, V) ||
BinaryOperator::isNot(V))
return 3;
return 4;
@ -521,7 +522,8 @@ static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V,
//
bool InstCombiner::SimplifyCommutative(BinaryOperator &I) {
bool Changed = false;
if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1)))
if (getComplexity(Context, I.getOperand(0)) <
getComplexity(Context, I.getOperand(1)))
Changed = !I.swapOperands();
if (!I.isAssociative()) return Changed;
@ -559,7 +561,8 @@ bool InstCombiner::SimplifyCommutative(BinaryOperator &I) {
/// so that theyare listed from right (least complex) to left (most complex).
/// This puts constants before unary operators before binary operators.
bool InstCombiner::SimplifyCompare(CmpInst &I) {
if (getComplexity(I.getOperand(0)) >= getComplexity(I.getOperand(1)))
if (getComplexity(Context, I.getOperand(0)) >=
getComplexity(Context, I.getOperand(1)))
return false;
I.swapOperands();
// Compare instructions are not associative so there's nothing else we can do.
@ -570,7 +573,7 @@ bool InstCombiner::SimplifyCompare(CmpInst &I) {
// if the LHS is a constant zero (which is the 'negate' form).
//
static inline Value *dyn_castNegVal(Value *V, LLVMContext *Context) {
if (BinaryOperator::isNeg(V))
if (BinaryOperator::isNeg(*Context, V))
return BinaryOperator::getNegArgument(V);
// Constants can be considered to be negated values if they can be folded.
@ -589,7 +592,7 @@ static inline Value *dyn_castNegVal(Value *V, LLVMContext *Context) {
// form).
//
static inline Value *dyn_castFNegVal(Value *V, LLVMContext *Context) {
if (BinaryOperator::isFNeg(V))
if (BinaryOperator::isFNeg(*Context, V))
return BinaryOperator::getFNegArgument(V);
// Constants can be considered to be negated values if they can be folded.
@ -2185,7 +2188,7 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
if (Value *RHSV = dyn_castNegVal(RHS, Context)) {
Instruction *NewAdd = BinaryOperator::CreateAdd(LHSV, RHSV, "sum");
InsertNewInstBefore(NewAdd, I);
return BinaryOperator::CreateNeg(NewAdd);
return BinaryOperator::CreateNeg(*Context, NewAdd);
}
}
@ -2530,9 +2533,11 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
if (Op1I->getOpcode() == Instruction::Add) {
if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y
return BinaryOperator::CreateNeg(Op1I->getOperand(1), I.getName());
return BinaryOperator::CreateNeg(*Context, Op1I->getOperand(1),
I.getName());
else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y
return BinaryOperator::CreateNeg(Op1I->getOperand(0), I.getName());
return BinaryOperator::CreateNeg(*Context, Op1I->getOperand(0),
I.getName());
else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) {
if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1)))
// C1-(X+C2) --> (C1-C2)-X
@ -2593,7 +2598,8 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
return ReplaceInstUsesWith(I, Op0I->getOperand(0));
} else if (Op0I->getOpcode() == Instruction::Sub) {
if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y
return BinaryOperator::CreateNeg(Op0I->getOperand(1), I.getName());
return BinaryOperator::CreateNeg(*Context, Op0I->getOperand(1),
I.getName());
}
}
@ -2619,9 +2625,11 @@ Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) {
if (Op1I->getOpcode() == Instruction::FAdd) {
if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y
return BinaryOperator::CreateFNeg(Op1I->getOperand(1), I.getName());
return BinaryOperator::CreateFNeg(*Context, Op1I->getOperand(1),
I.getName());
else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y
return BinaryOperator::CreateFNeg(Op1I->getOperand(0), I.getName());
return BinaryOperator::CreateFNeg(*Context, Op1I->getOperand(0),
I.getName());
}
}
@ -2683,7 +2691,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
if (CI->equalsInt(1)) // X * 1 == X
return ReplaceInstUsesWith(I, Op0);
if (CI->isAllOnesValue()) // X * -1 == 0 - X
return BinaryOperator::CreateNeg(Op0, I.getName());
return BinaryOperator::CreateNeg(*Context, Op0, I.getName());
const APInt& Val = cast<ConstantInt>(CI)->getValue();
if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C
@ -2695,7 +2703,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) {
if (Op1V->isAllOnesValue()) // X * -1 == 0 - X
return BinaryOperator::CreateNeg(Op0, I.getName());
return BinaryOperator::CreateNeg(*Context, Op0, I.getName());
// As above, vector X*splat(1.0) -> X in all defined cases.
if (Constant *Splat = Op1V->getSplatValue()) {
@ -3108,7 +3116,7 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
// sdiv X, -1 == -X
if (RHS->isAllOnesValue())
return BinaryOperator::CreateNeg(Op0);
return BinaryOperator::CreateNeg(*Context, Op0);
}
// If the sign bits of both operands are zero (i.e. we can prove they are
@ -4042,7 +4050,7 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
ConstantInt *A = dyn_cast<ConstantInt>(Op0LHS);
if (!(A && A->isZero()) && // avoid infinite recursion.
MaskedValueIsZero(Op0LHS, Mask)) {
Instruction *NewNeg = BinaryOperator::CreateNeg(Op0RHS);
Instruction *NewNeg = BinaryOperator::CreateNeg(*Context, Op0RHS);
InsertNewInstBefore(NewNeg, I);
return BinaryOperator::CreateAnd(NewNeg, AndRHS);
}
@ -7094,7 +7102,7 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
else if (Value *NegVal = dyn_castNegVal(BOp0, Context))
return new ICmpInst(*Context, ICI.getPredicate(), NegVal, BOp1);
else if (BO->hasOneUse()) {
Instruction *Neg = BinaryOperator::CreateNeg(BOp1);
Instruction *Neg = BinaryOperator::CreateNeg(*Context, BOp1);
InsertNewInstBefore(Neg, ICI);
Neg->takeName(BO);
return new ICmpInst(*Context, ICI.getPredicate(), BOp0, Neg);
@ -9587,7 +9595,8 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
NegVal = Context->getConstantExprNeg(C);
} else {
NegVal = InsertNewInstBefore(
BinaryOperator::CreateNeg(SubOp->getOperand(1), "tmp"), SI);
BinaryOperator::CreateNeg(*Context, SubOp->getOperand(1),
"tmp"), SI);
}
Value *NewTrueOp = OtherAddOp;

View File

@ -1341,6 +1341,7 @@ namespace {
BasicBlock *TopBB;
Instruction *TopInst;
bool &modified;
LLVMContext *Context;
typedef InequalityGraph::Node Node;
@ -1660,7 +1661,8 @@ namespace {
Top(DTDFS->getNodeForBlock(TopBB)),
TopBB(TopBB),
TopInst(NULL),
modified(modified)
modified(modified),
Context(TopBB->getContext())
{
assert(Top && "VRPSolver created for unreachable basic block.");
}
@ -1760,7 +1762,7 @@ namespace {
} break;
case Instruction::Or: {
// "or i32 %a, %b" EQ 0 then %a EQ 0 and %b EQ 0
Constant *Zero = Constant::getNullValue(Ty);
Constant *Zero = Context->getNullValue(Ty);
if (Canonical == Zero) {
add(Zero, Op0, ICmpInst::ICMP_EQ, NewContext);
add(Zero, Op1, ICmpInst::ICMP_EQ, NewContext);
@ -1783,10 +1785,10 @@ namespace {
}
if (Canonical == LHS) {
if (isa<ConstantInt>(Canonical))
add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_EQ,
add(RHS, Context->getNullValue(Ty), ICmpInst::ICMP_EQ,
NewContext);
} else if (isRelatedBy(LHS, Canonical, ICmpInst::ICMP_NE)) {
add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_NE,
add(RHS, Context->getNullValue(Ty), ICmpInst::ICMP_NE,
NewContext);
}
} break;
@ -1831,10 +1833,10 @@ namespace {
}
// TODO: The GEPI indices are all zero. Copy from definition to operand,
// jumping the type plane as needed.
if (isRelatedBy(GEPI, Constant::getNullValue(GEPI->getType()),
if (isRelatedBy(GEPI, Context->getNullValue(GEPI->getType()),
ICmpInst::ICMP_NE)) {
Value *Ptr = GEPI->getPointerOperand();
add(Ptr, Constant::getNullValue(Ptr->getType()), ICmpInst::ICMP_NE,
add(Ptr, Context->getNullValue(Ptr->getType()), ICmpInst::ICMP_NE,
NewContext);
}
} else if (CastInst *CI = dyn_cast<CastInst>(I)) {
@ -1888,7 +1890,7 @@ namespace {
const Type *Ty = BO->getType();
assert(!Ty->isFPOrFPVector() && "Float in work queue!");
Constant *Zero = Constant::getNullValue(Ty);
Constant *Zero = Context->getNullValue(Ty);
Constant *One = ConstantInt::get(Ty, 1);
ConstantInt *AllOnes = ConstantInt::getAllOnesValue(Ty);
@ -2110,9 +2112,9 @@ namespace {
// TODO: The GEPI indices are all zero. Copy from operand to definition,
// jumping the type plane as needed.
Value *Ptr = GEPI->getPointerOperand();
if (isRelatedBy(Ptr, Constant::getNullValue(Ptr->getType()),
if (isRelatedBy(Ptr, Context->getNullValue(Ptr->getType()),
ICmpInst::ICMP_NE)) {
add(GEPI, Constant::getNullValue(GEPI->getType()), ICmpInst::ICMP_NE,
add(GEPI, Context->getNullValue(GEPI->getType()), ICmpInst::ICMP_NE,
NewContext);
}
}
@ -2496,7 +2498,8 @@ namespace {
void PredicateSimplifier::Forwards::visitAllocaInst(AllocaInst &AI) {
VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &AI);
VRP.add(Constant::getNullValue(AI.getType()), &AI, ICmpInst::ICMP_NE);
VRP.add(AI.getParent()->getContext()->getNullValue(AI.getType()),
&AI, ICmpInst::ICMP_NE);
VRP.solve();
}
@ -2506,7 +2509,8 @@ namespace {
if (isa<Constant>(Ptr)) return;
VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &LI);
VRP.add(Constant::getNullValue(Ptr->getType()), Ptr, ICmpInst::ICMP_NE);
VRP.add(LI.getParent()->getContext()->getNullValue(Ptr->getType()),
Ptr, ICmpInst::ICMP_NE);
VRP.solve();
}
@ -2515,7 +2519,8 @@ namespace {
if (isa<Constant>(Ptr)) return;
VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &SI);
VRP.add(Constant::getNullValue(Ptr->getType()), Ptr, ICmpInst::ICMP_NE);
VRP.add(SI.getParent()->getContext()->getNullValue(Ptr->getType()),
Ptr, ICmpInst::ICMP_NE);
VRP.solve();
}
@ -2550,8 +2555,8 @@ namespace {
case Instruction::SDiv: {
Value *Divisor = BO.getOperand(1);
VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &BO);
VRP.add(Constant::getNullValue(Divisor->getType()), Divisor,
ICmpInst::ICMP_NE);
VRP.add(BO.getParent()->getContext()->getNullValue(Divisor->getType()),
Divisor, ICmpInst::ICMP_NE);
VRP.solve();
break;
}

View File

@ -178,7 +178,7 @@ unsigned Reassociate::getRank(Value *V) {
// If this is a not or neg instruction, do not count it for rank. This
// assures us that X and ~X will have the same rank.
if (!I->getType()->isInteger() ||
(!BinaryOperator::isNot(I) && !BinaryOperator::isNeg(I)))
(!BinaryOperator::isNot(I) && !BinaryOperator::isNeg(*Context, I)))
++Rank;
//DOUT << "Calculated Rank[" << V->getName() << "] = "
@ -264,12 +264,12 @@ void Reassociate::LinearizeExprTree(BinaryOperator *I,
// If this is a multiply expression tree and it contains internal negations,
// transform them into multiplies by -1 so they can be reassociated.
if (I->getOpcode() == Instruction::Mul) {
if (!LHSBO && LHS->hasOneUse() && BinaryOperator::isNeg(LHS)) {
if (!LHSBO && LHS->hasOneUse() && BinaryOperator::isNeg(*Context, LHS)) {
LHS = LowerNegateToMultiply(cast<Instruction>(LHS),
ValueRankMap, Context);
LHSBO = isReassociableOp(LHS, Opcode);
}
if (!RHSBO && RHS->hasOneUse() && BinaryOperator::isNeg(RHS)) {
if (!RHSBO && RHS->hasOneUse() && BinaryOperator::isNeg(*Context, RHS)) {
RHS = LowerNegateToMultiply(cast<Instruction>(RHS),
ValueRankMap, Context);
RHSBO = isReassociableOp(RHS, Opcode);
@ -373,7 +373,7 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
// version of the value is returned, and BI is left pointing at the instruction
// that should be processed next by the reassociation pass.
//
static Value *NegateValue(Value *V, Instruction *BI) {
static Value *NegateValue(LLVMContext *Context, Value *V, Instruction *BI) {
// We are trying to expose opportunity for reassociation. One of the things
// that we want to do to achieve this is to push a negation as deep into an
// expression chain as possible, to expose the add instructions. In practice,
@ -386,8 +386,8 @@ static Value *NegateValue(Value *V, Instruction *BI) {
if (Instruction *I = dyn_cast<Instruction>(V))
if (I->getOpcode() == Instruction::Add && I->hasOneUse()) {
// Push the negates through the add.
I->setOperand(0, NegateValue(I->getOperand(0), BI));
I->setOperand(1, NegateValue(I->getOperand(1), BI));
I->setOperand(0, NegateValue(Context, I->getOperand(0), BI));
I->setOperand(1, NegateValue(Context, I->getOperand(1), BI));
// We must move the add instruction here, because the neg instructions do
// not dominate the old add instruction in general. By moving it, we are
@ -402,14 +402,14 @@ static Value *NegateValue(Value *V, Instruction *BI) {
// Insert a 'neg' instruction that subtracts the value from zero to get the
// negation.
//
return BinaryOperator::CreateNeg(V, V->getName() + ".neg", BI);
return BinaryOperator::CreateNeg(*Context, V, V->getName() + ".neg", BI);
}
/// ShouldBreakUpSubtract - Return true if we should break up this subtract of
/// X-Y into (X + -Y).
static bool ShouldBreakUpSubtract(Instruction *Sub) {
static bool ShouldBreakUpSubtract(LLVMContext *Context, Instruction *Sub) {
// If this is a negation, we can't split it up!
if (BinaryOperator::isNeg(Sub))
if (BinaryOperator::isNeg(*Context, Sub))
return false;
// Don't bother to break this up unless either the LHS is an associable add or
@ -431,7 +431,7 @@ static bool ShouldBreakUpSubtract(Instruction *Sub) {
/// BreakUpSubtract - If we have (X-Y), and if either X is an add, or if this is
/// only used by an add, transform this into (X+(0-Y)) to promote better
/// reassociation.
static Instruction *BreakUpSubtract(Instruction *Sub,
static Instruction *BreakUpSubtract(LLVMContext *Context, Instruction *Sub,
std::map<AssertingVH<>, unsigned> &ValueRankMap) {
// Convert a subtract into an add and a neg instruction... so that sub
// instructions can be commuted with other add instructions...
@ -439,7 +439,7 @@ static Instruction *BreakUpSubtract(Instruction *Sub,
// Calculate the negative value of Operand 1 of the sub instruction...
// and set it as the RHS of the add instruction we just made...
//
Value *NegVal = NegateValue(Sub->getOperand(1), Sub);
Value *NegVal = NegateValue(Context, Sub->getOperand(1), Sub);
Instruction *New =
BinaryOperator::CreateAdd(Sub->getOperand(0), NegVal, "", Sub);
New->takeName(Sub);
@ -663,7 +663,7 @@ Value *Reassociate::OptimizeExpression(BinaryOperator *I,
for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
assert(i < Ops.size());
// Check for X and -X in the operand list.
if (BinaryOperator::isNeg(Ops[i].Op)) {
if (BinaryOperator::isNeg(*Context, Ops[i].Op)) {
Value *X = BinaryOperator::getNegArgument(Ops[i].Op);
unsigned FoundX = FindInOperandList(Ops, i, X);
if (FoundX != i) {
@ -798,10 +798,10 @@ void Reassociate::ReassociateBB(BasicBlock *BB) {
// If this is a subtract instruction which is not already in negate form,
// see if we can convert it to X+-Y.
if (BI->getOpcode() == Instruction::Sub) {
if (ShouldBreakUpSubtract(BI)) {
BI = BreakUpSubtract(BI, ValueRankMap);
if (ShouldBreakUpSubtract(Context, BI)) {
BI = BreakUpSubtract(Context, BI, ValueRankMap);
MadeChange = true;
} else if (BinaryOperator::isNeg(BI)) {
} else if (BinaryOperator::isNeg(*Context, BI)) {
// Otherwise, this is a negation. See if the operand is a multiply tree
// and if this is not an inner node of a multiply tree.
if (isReassociableOp(BI->getOperand(1), Instruction::Mul) &&

View File

@ -251,7 +251,7 @@ bool LowerInvoke::insertCheapEHSupport(Function &F) {
// Insert a return instruction. This really should be a "barrier", as it
// is unreachable.
ReturnInst::Create(F.getReturnType() == Type::VoidTy ? 0 :
Constant::getNullValue(F.getReturnType()), UI);
Context->getNullValue(F.getReturnType()), UI);
// Remove the unwind instruction now.
BB->getInstList().erase(UI);
@ -285,7 +285,7 @@ void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
BasicBlock::iterator NI = II->getNormalDest()->getFirstNonPHI();
// nonvolatile.
new StoreInst(Constant::getNullValue(Type::Int32Ty), InvokeNum, false, NI);
new StoreInst(Context->getNullValue(Type::Int32Ty), InvokeNum, false, NI);
// Add a switch case to our unwind block.
CatchSwitch->addCase(InvokeNoC, II->getUnwindDest());
@ -473,7 +473,7 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
new AllocaInst(JBLinkTy, 0, Align, "jblink", F.begin()->begin());
std::vector<Value*> Idx;
Idx.push_back(Constant::getNullValue(Type::Int32Ty));
Idx.push_back(Context->getNullValue(Type::Int32Ty));
Idx.push_back(ConstantInt::get(Type::Int32Ty, 1));
OldJmpBufPtr = GetElementPtrInst::Create(JmpBuf, Idx.begin(), Idx.end(),
"OldBuf", EntryBB->getTerminator());
@ -525,7 +525,7 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
// Compare the return value to zero.
Value *IsNormal = new ICmpInst(EntryBB->getTerminator(),
ICmpInst::ICMP_EQ, SJRet,
Constant::getNullValue(SJRet->getType()),
Context->getNullValue(SJRet->getType()),
"notunwind");
// Nuke the uncond branch.
EntryBB->getTerminator()->eraseFromParent();
@ -559,14 +559,14 @@ bool LowerInvoke::insertExpensiveEHSupport(Function &F) {
// Load the JBList, if it's null, then there was no catch!
Value *NotNull = new ICmpInst(*UnwindHandler, ICmpInst::ICMP_NE, BufPtr,
Constant::getNullValue(BufPtr->getType()),
Context->getNullValue(BufPtr->getType()),
"notnull");
BranchInst::Create(UnwindBlock, TermBlock, NotNull, UnwindHandler);
// Create the block to do the longjmp.
// Get a pointer to the jmpbuf and longjmp.
std::vector<Value*> Idx;
Idx.push_back(Constant::getNullValue(Type::Int32Ty));
Idx.push_back(Context->getNullValue(Type::Int32Ty));
Idx.push_back(ConstantInt::get(Type::Int32Ty, 0));
Idx[0] = GetElementPtrInst::Create(BufPtr, Idx.begin(), Idx.end(), "JmpBuf",
UnwindBlock);

File diff suppressed because it is too large Load Diff

View File

@ -23,37 +23,47 @@ namespace llvm {
class Value;
class Constant;
class Type;
class LLVMContext;
// Constant fold various types of instruction...
Constant *ConstantFoldCastInstruction(
LLVMContext &Context,
unsigned opcode, ///< The opcode of the cast
const Constant *V, ///< The source constant
const Type *DestTy ///< The destination type
);
Constant *ConstantFoldSelectInstruction(const Constant *Cond,
Constant *ConstantFoldSelectInstruction(LLVMContext &Context,
const Constant *Cond,
const Constant *V1,
const Constant *V2);
Constant *ConstantFoldExtractElementInstruction(const Constant *Val,
Constant *ConstantFoldExtractElementInstruction(LLVMContext &Context,
const Constant *Val,
const Constant *Idx);
Constant *ConstantFoldInsertElementInstruction(const Constant *Val,
Constant *ConstantFoldInsertElementInstruction(LLVMContext &Context,
const Constant *Val,
const Constant *Elt,
const Constant *Idx);
Constant *ConstantFoldShuffleVectorInstruction(const Constant *V1,
Constant *ConstantFoldShuffleVectorInstruction(LLVMContext &Context,
const Constant *V1,
const Constant *V2,
const Constant *Mask);
Constant *ConstantFoldExtractValueInstruction(const Constant *Agg,
Constant *ConstantFoldExtractValueInstruction(LLVMContext &Context,
const Constant *Agg,
const unsigned *Idxs,
unsigned NumIdx);
Constant *ConstantFoldInsertValueInstruction(const Constant *Agg,
Constant *ConstantFoldInsertValueInstruction(LLVMContext &Context,
const Constant *Agg,
const Constant *Val,
const unsigned* Idxs,
unsigned NumIdx);
Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
Constant *ConstantFoldBinaryInstruction(LLVMContext &Context,
unsigned Opcode, const Constant *V1,
const Constant *V2);
Constant *ConstantFoldCompareInstruction(unsigned short predicate,
Constant *ConstantFoldCompareInstruction(LLVMContext &Context,
unsigned short predicate,
const Constant *C1,
const Constant *C2);
Constant *ConstantFoldGetElementPtr(const Constant *C,
Constant *ConstantFoldGetElementPtr(LLVMContext &Context, const Constant *C,
Constant* const *Idxs, unsigned NumIdx);
} // End llvm namespace

View File

@ -128,35 +128,6 @@ bool Constant::ContainsRelocations(unsigned Kind) const {
return false;
}
// Static constructor to create a '0' constant of arbitrary type...
static const uint64_t zero[2] = {0, 0};
Constant *Constant::getNullValue(const Type *Ty) {
switch (Ty->getTypeID()) {
case Type::IntegerTyID:
return ConstantInt::get(Ty, 0);
case Type::FloatTyID:
return ConstantFP::get(APFloat(APInt(32, 0)));
case Type::DoubleTyID:
return ConstantFP::get(APFloat(APInt(64, 0)));
case Type::X86_FP80TyID:
return ConstantFP::get(APFloat(APInt(80, 2, zero)));
case Type::FP128TyID:
return ConstantFP::get(APFloat(APInt(128, 2, zero), true));
case Type::PPC_FP128TyID:
return ConstantFP::get(APFloat(APInt(128, 2, zero)));
case Type::PointerTyID:
return ConstantPointerNull::get(cast<PointerType>(Ty));
case Type::StructTyID:
case Type::ArrayTyID:
case Type::VectorTyID:
return ConstantAggregateZero::get(Ty);
default:
// Function, Label, or Opaque type?
assert(!"Cannot create a null constant of that type!");
return 0;
}
}
Constant *Constant::getAllOnesValue(const Type *Ty) {
if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty))
return ConstantInt::get(APInt::getAllOnesValue(ITy->getBitWidth()));
@ -186,7 +157,8 @@ ConstantVector *ConstantVector::getAllOnesValue(const VectorType *Ty) {
/// type, returns the elements of the vector in the specified smallvector.
/// This handles breaking down a vector undef into undef elements, etc. For
/// constant exprs and other cases we can't handle, we return an empty vector.
void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const {
void Constant::getVectorElements(LLVMContext &Context,
SmallVectorImpl<Constant*> &Elts) const {
assert(isa<VectorType>(getType()) && "Not a vector constant!");
if (const ConstantVector *CV = dyn_cast<ConstantVector>(this)) {
@ -198,12 +170,12 @@ void Constant::getVectorElements(SmallVectorImpl<Constant*> &Elts) const {
const VectorType *VT = cast<VectorType>(getType());
if (isa<ConstantAggregateZero>(this)) {
Elts.assign(VT->getNumElements(),
Constant::getNullValue(VT->getElementType()));
Context.getNullValue(VT->getElementType()));
return;
}
if (isa<UndefValue>(this)) {
Elts.assign(VT->getNumElements(), UndefValue::get(VT->getElementType()));
Elts.assign(VT->getNumElements(), Context.getUndef(VT->getElementType()));
return;
}
@ -361,12 +333,6 @@ bool ConstantFP::isNullValue() const {
return Val.isZero() && !Val.isNegative();
}
ConstantFP *ConstantFP::getNegativeZero(const Type *Ty) {
APFloat apf = cast <ConstantFP>(Constant::getNullValue(Ty))->getValueAPF();
apf.changeSign();
return ConstantFP::get(apf);
}
bool ConstantFP::isExactlyValue(const APFloat& V) const {
return Val.bitwiseIsEqual(V);
}
@ -831,26 +797,6 @@ const SmallVector<unsigned, 4> &ConstantExpr::getIndices() const {
return cast<InsertValueConstantExpr>(this)->Indices;
}
/// ConstantExpr::get* - Return some common constants without having to
/// specify the full Instruction::OPCODE identifier.
///
Constant *ConstantExpr::getNeg(Constant *C) {
// API compatibility: Adjust integer opcodes to floating-point opcodes.
if (C->getType()->isFPOrFPVector())
return getFNeg(C);
assert(C->getType()->isIntOrIntVector() &&
"Cannot NEG a nonintegral value!");
return get(Instruction::Sub,
ConstantExpr::getZeroValueForNegationExpr(C->getType()),
C);
}
Constant *ConstantExpr::getFNeg(Constant *C) {
assert(C->getType()->isFPOrFPVector() &&
"Cannot FNEG a non-floating-point value!");
return get(Instruction::FSub,
ConstantExpr::getZeroValueForNegationExpr(C->getType()),
C);
}
Constant *ConstantExpr::getNot(Constant *C) {
assert(C->getType()->isIntOrIntVector() &&
"Cannot NOT a nonintegral value!");
@ -1501,11 +1447,11 @@ bool ConstantArray::isString() const {
/// isCString - This method returns true if the array is a string (see
/// isString) and it ends in a null byte \\0 and does not contains any other
/// null bytes except its terminator.
bool ConstantArray::isCString() const {
bool ConstantArray::isCString(LLVMContext &Context) const {
// Check the element type for i8...
if (getType()->getElementType() != Type::Int8Ty)
return false;
Constant *Zero = Constant::getNullValue(getOperand(0)->getType());
Constant *Zero = Context.getNullValue(getOperand(0)->getType());
// Last element must be a null.
if (getOperand(getNumOperands()-1) != Zero)
return false;
@ -2011,7 +1957,8 @@ static inline Constant *getFoldedCast(
Instruction::CastOps opc, Constant *C, const Type *Ty) {
assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
// Fold a few common cases
if (Constant *FC = ConstantFoldCastInstruction(opc, C, Ty))
if (Constant *FC =
ConstantFoldCastInstruction(getGlobalContext(), opc, C, Ty))
return FC;
// Look up the constant in the table first to ensure uniqueness
@ -2245,25 +2192,6 @@ Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
return getFoldedCast(Instruction::BitCast, C, DstTy);
}
Constant *ConstantExpr::getAlignOf(const Type *Ty) {
// alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
const Type *AligningTy = StructType::get(Type::Int8Ty, Ty, NULL);
Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
Constant *One = ConstantInt::get(Type::Int32Ty, 1);
Constant *Indices[2] = { Zero, One };
Constant *GEP = getGetElementPtr(NullPtr, Indices, 2);
return getCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
}
Constant *ConstantExpr::getSizeOf(const Type *Ty) {
// sizeof is implemented as: (i64) gep (Ty*)null, 1
Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
Constant *GEP =
getGetElementPtr(getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
}
Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
Constant *C1, Constant *C2) {
// Check the operands for consistency first
@ -2274,7 +2202,8 @@ Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
"Operand types in binary constant expression should match");
if (ReqTy == C1->getType() || ReqTy == Type::Int1Ty)
if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
if (Constant *FC = ConstantFoldBinaryInstruction(
getGlobalContext(), Opcode, C1, C2))
return FC; // Fold a few common cases...
std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
@ -2383,7 +2312,8 @@ Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
assert(!SelectInst::areInvalidOperands(C, V1, V2)&&"Invalid select operands");
if (ReqTy == V1->getType())
if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
if (Constant *SC = ConstantFoldSelectInstruction(
getGlobalContext(), C, V1, V2))
return SC; // Fold common cases
std::vector<Constant*> argVec(3, C);
@ -2403,7 +2333,8 @@ Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
cast<PointerType>(ReqTy)->getElementType() &&
"GEP indices invalid!");
if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
if (Constant *FC = ConstantFoldGetElementPtr(
getGlobalContext(), C, (Constant**)Idxs, NumIdx))
return FC; // Fold a few common cases...
assert(isa<PointerType>(C->getType()) &&
@ -2442,7 +2373,8 @@ ConstantExpr::getICmp(unsigned short pred, Constant* LHS, Constant* RHS) {
assert(pred >= ICmpInst::FIRST_ICMP_PREDICATE &&
pred <= ICmpInst::LAST_ICMP_PREDICATE && "Invalid ICmp Predicate");
if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
if (Constant *FC = ConstantFoldCompareInstruction(
getGlobalContext(),pred, LHS, RHS))
return FC; // Fold a few common cases...
// Look up the constant in the table first to ensure uniqueness
@ -2461,7 +2393,8 @@ ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
assert(LHS->getType() == RHS->getType());
assert(pred <= FCmpInst::LAST_FCMP_PREDICATE && "Invalid FCmp Predicate");
if (Constant *FC = ConstantFoldCompareInstruction(pred, LHS, RHS))
if (Constant *FC = ConstantFoldCompareInstruction(
getGlobalContext(), pred, LHS, RHS))
return FC; // Fold a few common cases...
// Look up the constant in the table first to ensure uniqueness
@ -2477,7 +2410,8 @@ ConstantExpr::getFCmp(unsigned short pred, Constant* LHS, Constant* RHS) {
Constant *ConstantExpr::getExtractElementTy(const Type *ReqTy, Constant *Val,
Constant *Idx) {
if (Constant *FC = ConstantFoldExtractElementInstruction(Val, Idx))
if (Constant *FC = ConstantFoldExtractElementInstruction(
getGlobalContext(), Val, Idx))
return FC; // Fold a few common cases...
// Look up the constant in the table first to ensure uniqueness
std::vector<Constant*> ArgVec(1, Val);
@ -2499,7 +2433,8 @@ Constant *ConstantExpr::getExtractElement(Constant *Val, Constant *Idx) {
Constant *ConstantExpr::getInsertElementTy(const Type *ReqTy, Constant *Val,
Constant *Elt, Constant *Idx) {
if (Constant *FC = ConstantFoldInsertElementInstruction(Val, Elt, Idx))
if (Constant *FC = ConstantFoldInsertElementInstruction(
getGlobalContext(), Val, Elt, Idx))
return FC; // Fold a few common cases...
// Look up the constant in the table first to ensure uniqueness
std::vector<Constant*> ArgVec(1, Val);
@ -2524,7 +2459,8 @@ Constant *ConstantExpr::getInsertElement(Constant *Val, Constant *Elt,
Constant *ConstantExpr::getShuffleVectorTy(const Type *ReqTy, Constant *V1,
Constant *V2, Constant *Mask) {
if (Constant *FC = ConstantFoldShuffleVectorInstruction(V1, V2, Mask))
if (Constant *FC = ConstantFoldShuffleVectorInstruction(
getGlobalContext(), V1, V2, Mask))
return FC; // Fold a few common cases...
// Look up the constant in the table first to ensure uniqueness
std::vector<Constant*> ArgVec(1, V1);
@ -2557,7 +2493,8 @@ Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg,
"insertvalue type invalid!");
assert(Agg->getType()->isFirstClassType() &&
"Non-first-class type for constant InsertValue expression");
Constant *FC = ConstantFoldInsertValueInstruction(Agg, Val, Idxs, NumIdx);
Constant *FC = ConstantFoldInsertValueInstruction(
getGlobalContext(), Agg, Val, Idxs, NumIdx);
assert(FC && "InsertValue constant expr couldn't be folded!");
return FC;
}
@ -2583,7 +2520,8 @@ Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg,
"extractvalue indices invalid!");
assert(Agg->getType()->isFirstClassType() &&
"Non-first-class type for constant extractvalue expression");
Constant *FC = ConstantFoldExtractValueInstruction(Agg, Idxs, NumIdx);
Constant *FC = ConstantFoldExtractValueInstruction(
getGlobalContext(), Agg, Idxs, NumIdx);
assert(FC && "ExtractValue constant expr couldn't be folded!");
return FC;
}
@ -2599,20 +2537,6 @@ Constant *ConstantExpr::getExtractValue(Constant *Agg,
return getExtractValueTy(ReqTy, Agg, IdxList, NumIdx);
}
Constant *ConstantExpr::getZeroValueForNegationExpr(const Type *Ty) {
if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
if (PTy->getElementType()->isFloatingPoint()) {
std::vector<Constant*> zeros(PTy->getNumElements(),
ConstantFP::getNegativeZero(PTy->getElementType()));
return ConstantVector::get(PTy, zeros);
}
if (Ty->isFloatingPoint())
return ConstantFP::getNegativeZero(Ty);
return Constant::getNullValue(Ty);
}
// destroyConstant - Remove the constant from the constant table...
//
void ConstantExpr::destroyConstant() {

View File

@ -1633,33 +1633,37 @@ BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
return Res;
}
BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context,
Value *Op, const std::string &Name,
Instruction *InsertBefore) {
Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
Value *zero = Context.getZeroValueForNegation(Op->getType());
return new BinaryOperator(Instruction::Sub,
zero, Op,
Op->getType(), Name, InsertBefore);
}
BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context,
Value *Op, const std::string &Name,
BasicBlock *InsertAtEnd) {
Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
Value *zero = Context.getZeroValueForNegation(Op->getType());
return new BinaryOperator(Instruction::Sub,
zero, Op,
Op->getType(), Name, InsertAtEnd);
}
BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const std::string &Name,
BinaryOperator *BinaryOperator::CreateFNeg(LLVMContext &Context,
Value *Op, const std::string &Name,
Instruction *InsertBefore) {
Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
Value *zero = Context.getZeroValueForNegation(Op->getType());
return new BinaryOperator(Instruction::FSub,
zero, Op,
Op->getType(), Name, InsertBefore);
}
BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const std::string &Name,
BinaryOperator *BinaryOperator::CreateFNeg(LLVMContext &Context,
Value *Op, const std::string &Name,
BasicBlock *InsertAtEnd) {
Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
Value *zero = Context.getZeroValueForNegation(Op->getType());
return new BinaryOperator(Instruction::FSub,
zero, Op,
Op->getType(), Name, InsertAtEnd);
@ -1705,19 +1709,19 @@ static inline bool isConstantAllOnes(const Value *V) {
return false;
}
bool BinaryOperator::isNeg(const Value *V) {
bool BinaryOperator::isNeg(LLVMContext &Context, const Value *V) {
if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
if (Bop->getOpcode() == Instruction::Sub)
return Bop->getOperand(0) ==
ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Context.getZeroValueForNegation(Bop->getType());
return false;
}
bool BinaryOperator::isFNeg(const Value *V) {
bool BinaryOperator::isFNeg(LLVMContext &Context, const Value *V) {
if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
if (Bop->getOpcode() == Instruction::FSub)
return Bop->getOperand(0) ==
ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Context.getZeroValueForNegation(Bop->getType());
return false;
}
@ -1730,7 +1734,6 @@ bool BinaryOperator::isNot(const Value *V) {
}
Value *BinaryOperator::getNegArgument(Value *BinOp) {
assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
return cast<BinaryOperator>(BinOp)->getOperand(1);
}
@ -1739,7 +1742,6 @@ const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
}
Value *BinaryOperator::getFNegArgument(Value *BinOp) {
assert(isFNeg(BinOp) && "getFNegArgument from non-'fneg' instruction!");
return cast<BinaryOperator>(BinOp)->getOperand(1);
}

View File

@ -15,6 +15,7 @@
#include "llvm/LLVMContext.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Instruction.h"
#include "llvm/MDNode.h"
#include "llvm/Support/ManagedStatic.h"
#include "LLVMContextImpl.h"
@ -31,8 +32,34 @@ LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl()) { }
LLVMContext::~LLVMContext() { delete pImpl; }
// Constant accessors
// Constructor to create a '0' constant of arbitrary type...
static const uint64_t zero[2] = {0, 0};
Constant* LLVMContext::getNullValue(const Type* Ty) {
return Constant::getNullValue(Ty);
switch (Ty->getTypeID()) {
case Type::IntegerTyID:
return getConstantInt(Ty, 0);
case Type::FloatTyID:
return getConstantFP(APFloat(APInt(32, 0)));
case Type::DoubleTyID:
return getConstantFP(APFloat(APInt(64, 0)));
case Type::X86_FP80TyID:
return getConstantFP(APFloat(APInt(80, 2, zero)));
case Type::FP128TyID:
return getConstantFP(APFloat(APInt(128, 2, zero), true));
case Type::PPC_FP128TyID:
return getConstantFP(APFloat(APInt(128, 2, zero)));
case Type::PointerTyID:
return getConstantPointerNull(cast<PointerType>(Ty));
case Type::StructTyID:
case Type::ArrayTyID:
case Type::VectorTyID:
return getConstantAggregateZero(Ty);
default:
// Function, Label, or Opaque type?
assert(!"Cannot create a null constant of that type!");
return 0;
}
}
Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
@ -222,7 +249,14 @@ Constant* LLVMContext::getConstantExprSelect(Constant* C, Constant* V1,
}
Constant* LLVMContext::getConstantExprAlignOf(const Type* Ty) {
return ConstantExpr::getAlignOf(Ty);
// alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
const Type *AligningTy = getStructType(Type::Int8Ty, Ty, NULL);
Constant *NullPtr = getNullValue(AligningTy->getPointerTo());
Constant *Zero = getConstantInt(Type::Int32Ty, 0);
Constant *One = getConstantInt(Type::Int32Ty, 1);
Constant *Indices[2] = { Zero, One };
Constant *GEP = getConstantExprGetElementPtr(NullPtr, Indices, 2);
return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int32Ty);
}
Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
@ -231,11 +265,22 @@ Constant* LLVMContext::getConstantExprCompare(unsigned short pred,
}
Constant* LLVMContext::getConstantExprNeg(Constant* C) {
return ConstantExpr::getNeg(C);
// API compatibility: Adjust integer opcodes to floating-point opcodes.
if (C->getType()->isFPOrFPVector())
return getConstantExprFNeg(C);
assert(C->getType()->isIntOrIntVector() &&
"Cannot NEG a nonintegral value!");
return getConstantExpr(Instruction::Sub,
getZeroValueForNegation(C->getType()),
C);
}
Constant* LLVMContext::getConstantExprFNeg(Constant* C) {
return ConstantExpr::getFNeg(C);
assert(C->getType()->isFPOrFPVector() &&
"Cannot FNEG a non-floating-point value!");
return getConstantExpr(Instruction::FSub,
getZeroValueForNegation(C->getType()),
C);
}
Constant* LLVMContext::getConstantExprNot(Constant* C) {
@ -365,11 +410,25 @@ Constant* LLVMContext::getConstantExprInsertValue(Constant* Agg, Constant* Val,
}
Constant* LLVMContext::getConstantExprSizeOf(const Type* Ty) {
return ConstantExpr::getSizeOf(Ty);
// sizeof is implemented as: (i64) gep (Ty*)null, 1
Constant *GEPIdx = getConstantInt(Type::Int32Ty, 1);
Constant *GEP = getConstantExprGetElementPtr(
getNullValue(getPointerTypeUnqual(Ty)), &GEPIdx, 1);
return getConstantExprCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
}
Constant* LLVMContext::getZeroValueForNegation(const Type* Ty) {
return ConstantExpr::getZeroValueForNegationExpr(Ty);
if (const VectorType *PTy = dyn_cast<VectorType>(Ty))
if (PTy->getElementType()->isFloatingPoint()) {
std::vector<Constant*> zeros(PTy->getNumElements(),
getConstantFPNegativeZero(PTy->getElementType()));
return getConstantVector(PTy, zeros);
}
if (Ty->isFloatingPoint())
return getConstantFPNegativeZero(Ty);
return getNullValue(Ty);
}
@ -383,7 +442,9 @@ Constant* LLVMContext::getConstantFP(const Type* Ty, double V) {
}
ConstantFP* LLVMContext::getConstantFPNegativeZero(const Type* Ty) {
return ConstantFP::getNegativeZero(Ty);
APFloat apf = cast <ConstantFP>(getNullValue(Ty))->getValueAPF();
apf.changeSign();
return getConstantFP(apf);
}
@ -452,6 +513,17 @@ StructType* LLVMContext::getStructType(const std::vector<const Type*>& Params,
return StructType::get(Params, isPacked);
}
StructType *LLVMContext::getStructType(const Type *type, ...) {
va_list ap;
std::vector<const llvm::Type*> StructFields;
va_start(ap, type);
while (type) {
StructFields.push_back(type);
type = va_arg(ap, llvm::Type*);
}
return StructType::get(StructFields);
}
// ArrayType accessors
ArrayType* LLVMContext::getArrayType(const Type* ElementType,
uint64_t NumElements) {

View File

@ -299,7 +299,8 @@ bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) {
if (isa<StructType>(BBTerm->getType()))
BBTerm->replaceAllUsesWith(UndefValue::get(BBTerm->getType()));
else if (BB->getTerminator()->getType() != Type::VoidTy)
BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType()));
BBTerm->replaceAllUsesWith(
BD.getContext().getNullValue(BBTerm->getType()));
// Replace the old terminator instruction.
BB->getInstList().pop_back();

View File

@ -74,7 +74,7 @@ Module *BugDriver::deleteInstructionFromProgram(const Instruction *I,
if (isa<StructType>(TheInst->getType()))
TheInst->replaceAllUsesWith(UndefValue::get(TheInst->getType()));
else if (TheInst->getType() != Type::VoidTy)
TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType()));
TheInst->replaceAllUsesWith(Context.getNullValue(TheInst->getType()));
// Remove the instruction from the program.
TheInst->getParent()->getInstList().erase(TheInst);

View File

@ -711,7 +711,8 @@ static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
// sbyte* so it matches the signature of the resolver function.
// GetElementPtr *funcName, ulong 0, ulong 0
std::vector<Constant*> GEPargs(2,Constant::getNullValue(Type::Int32Ty));
std::vector<Constant*> GEPargs(2,
BD.getContext().getNullValue(Type::Int32Ty));
Value *GEP = ConstantExpr::getGetElementPtr(funcName, &GEPargs[0], 2);
std::vector<Value*> ResolverArgs;
ResolverArgs.push_back(GEP);

View File

@ -60,7 +60,7 @@ namespace {
for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
if (CallInst *CI = dyn_cast<CallInst>(I)) {
if (!CI->use_empty())
CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
CI->replaceAllUsesWith(Context->getNullValue(CI->getType()));
CI->getParent()->getInstList().erase(CI);
break;
}

View File

@ -189,7 +189,7 @@ bool LTOModule::objcClassNameFromExpression(Constant* c, std::string& name)
if (GlobalVariable* gvn = dyn_cast<GlobalVariable>(op)) {
Constant* cn = gvn->getInitializer();
if (ConstantArray* ca = dyn_cast<ConstantArray>(cn)) {
if ( ca->isCString() ) {
if ( ca->isCString(getGlobalContext()) ) {
name = ".objc_class_name_" + ca->getAsString();
return true;
}

View File

@ -73,7 +73,7 @@ TEST(JIT, GlobalInFunction) {
GTy,
false, // Not constant.
GlobalValue::InternalLinkage,
Constant::getNullValue(GTy),
context.getNullValue(GTy),
"myglobal");
// Make a function that points to a global.

View File

@ -284,14 +284,17 @@ TEST_F(ValueHandle, CallbackVH_DeletionCanRAUW) {
public:
int DeletedCalls;
Value *AURWArgument;
LLVMContext *Context;
RecoveringVH() : DeletedCalls(0), AURWArgument(NULL) {}
RecoveringVH() : DeletedCalls(0), AURWArgument(NULL),
Context(&getGlobalContext()) {}
RecoveringVH(Value *V)
: CallbackVH(V), DeletedCalls(0), AURWArgument(NULL) {}
: CallbackVH(V), DeletedCalls(0), AURWArgument(NULL),
Context(&getGlobalContext()) {}
private:
virtual void deleted() {
getValPtr()->replaceAllUsesWith(Constant::getNullValue(Type::Int32Ty));
getValPtr()->replaceAllUsesWith(Context->getNullValue(Type::Int32Ty));
setValPtr(NULL);
}
virtual void allUsesReplacedWith(Value *new_value) {
@ -307,11 +310,13 @@ TEST_F(ValueHandle, CallbackVH_DeletionCanRAUW) {
RecoveringVH RVH;
RVH = BitcastV.get();
std::auto_ptr<BinaryOperator> BitcastUser(
BinaryOperator::CreateAdd(RVH, Constant::getNullValue(Type::Int32Ty)));
BinaryOperator::CreateAdd(RVH,
getGlobalContext().getNullValue(Type::Int32Ty)));
EXPECT_EQ(BitcastV.get(), BitcastUser->getOperand(0));
BitcastV.reset(); // Would crash without the ValueHandler.
EXPECT_EQ(Constant::getNullValue(Type::Int32Ty), RVH.AURWArgument);
EXPECT_EQ(Constant::getNullValue(Type::Int32Ty), BitcastUser->getOperand(0));
EXPECT_EQ(getGlobalContext().getNullValue(Type::Int32Ty), RVH.AURWArgument);
EXPECT_EQ(getGlobalContext().getNullValue(Type::Int32Ty),
BitcastUser->getOperand(0));
}
}