//
//===----------------------------------------------------------------------===//
+#include "LLVMContextImpl.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
#include "llvm/Support/CallSite.h"
#include "llvm/Support/ConstantRange.h"
#include "llvm/Support/MathExtras.h"
+
using namespace llvm;
//===----------------------------------------------------------------------===//
/// otherwise use DT to test for dominance.
///
Value *PHINode::hasConstantValue(DominatorTree *DT) const {
- // If the PHI node only has one incoming value, eliminate the PHI node...
+ // If the PHI node only has one incoming value, eliminate the PHI node.
if (getNumIncomingValues() == 1) {
if (getIncomingValue(0) != this) // not X = phi X
return getIncomingValue(0);
- else
- return UndefValue::get(getType()); // Self cycle is dead.
+ return UndefValue::get(getType()); // Self cycle is dead.
}
// Otherwise if all of the incoming values are the same for the PHI, replace
} else if (getIncomingValue(i) != this) { // Not the PHI node itself...
if (InVal && getIncomingValue(i) != InVal)
return 0; // Not the same, bail out.
- else
- InVal = getIncomingValue(i);
+ InVal = getIncomingValue(i);
}
// The only case that could cause InVal to be null is if we have a PHI node
// instruction, we cannot always return X as the result of the PHI node. Only
// do this if X is not an instruction (thus it must dominate the PHI block),
// or if the client is prepared to deal with this possibility.
- if (HasUndefInput)
- if (Instruction *IV = dyn_cast<Instruction>(InVal)) {
- if (DT) {
- // We have a DominatorTree. Do a precise test.
- if (!DT->dominates(IV, this))
- return 0;
- } else {
- // If it's in the entry block, it dominates everything.
- if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
- isa<InvokeInst>(IV))
- return 0; // Cannot guarantee that InVal dominates this PHINode.
- }
- }
+ if (!HasUndefInput || !isa<Instruction>(InVal))
+ return InVal;
+
+ Instruction *IV = cast<Instruction>(InVal);
+ if (DT) {
+ // We have a DominatorTree. Do a precise test.
+ if (!DT->dominates(IV, this))
+ return 0;
+ } else {
+ // If it is in the entry block, it obviously dominates everything.
+ if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
+ isa<InvokeInst>(IV))
+ return 0; // Cannot guarantee that InVal dominates this PHINode.
+ }
// All of the incoming values are the same, return the value now.
return InVal;
}
static Value *createMalloc(Instruction *InsertBefore, BasicBlock *InsertAtEnd,
- const Type *AllocTy, const Type *IntPtrTy,
+ const Type *IntPtrTy, const Type *AllocTy,
Value *ArraySize, const Twine &NameStr) {
assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
- "createMalloc needs only InsertBefore or InsertAtEnd");
- const PointerType *AllocPtrType = dyn_cast<PointerType>(AllocTy);
- assert(AllocPtrType && "CreateMalloc passed a non-pointer allocation type");
-
+ "createMalloc needs either InsertBefore or InsertAtEnd");
+
+ // malloc(type) becomes:
+ // bitcast (i8* malloc(typeSize)) to type*
+ // malloc(type, arraySize) becomes:
+ // bitcast (i8 *malloc(typeSize*arraySize)) to type*
+ Value *AllocSize = ConstantExpr::getSizeOf(AllocTy);
+ AllocSize = ConstantExpr::getTruncOrBitCast(cast<Constant>(AllocSize),
+ IntPtrTy);
ArraySize = checkArraySize(ArraySize, IntPtrTy);
- // malloc(type) becomes i8 *malloc(size)
- Value *AllocSize = ConstantExpr::getSizeOf(AllocPtrType->getElementType());
- AllocSize = ConstantExpr::getTruncOrBitCast(cast<Constant>(AllocSize),
- IntPtrTy);
if (!IsConstantOne(ArraySize)) {
if (IsConstantOne(AllocSize)) {
AllocSize = ArraySize; // Operand * 1 = Operand
// Malloc arg is constant product of type size and array size
AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
} else {
- Value *Scale = ArraySize;
- if (Scale->getType() != IntPtrTy) {
- if (InsertBefore)
- Scale = CastInst::CreateIntegerCast(Scale, IntPtrTy, false /*ZExt*/,
- "", InsertBefore);
- else
- Scale = CastInst::CreateIntegerCast(Scale, IntPtrTy, false /*ZExt*/,
- "", InsertAtEnd);
- }
// Multiply type size by the array size...
if (InsertBefore)
- AllocSize = BinaryOperator::CreateMul(Scale, AllocSize,
- "", InsertBefore);
+ AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
+ "mallocsize", InsertBefore);
else
- AllocSize = BinaryOperator::CreateMul(Scale, AllocSize,
- "", InsertAtEnd);
+ AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
+ "mallocsize", InsertAtEnd);
}
}
+ assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
// Create the call to Malloc.
BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
Module* M = BB->getParent()->getParent();
const Type *BPTy = PointerType::getUnqual(Type::getInt8Ty(BB->getContext()));
// prototype malloc as "void *malloc(size_t)"
- Constant *MallocFunc = M->getOrInsertFunction("malloc", BPTy,
- IntPtrTy, NULL);
+ Constant *MallocF = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
+ if (!cast<Function>(MallocF)->doesNotAlias(0))
+ cast<Function>(MallocF)->setDoesNotAlias(0);
+ const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
CallInst *MCall = NULL;
- if (InsertBefore)
- MCall = CallInst::Create(MallocFunc, AllocSize, NameStr, InsertBefore);
- else
- MCall = CallInst::Create(MallocFunc, AllocSize, NameStr, InsertAtEnd);
- MCall->setTailCall();
-
- // Create a cast instruction to convert to the right type...
- const Type* VoidT = Type::getVoidTy(BB->getContext());
- assert(MCall->getType() != VoidT && "Malloc has void return type");
- Value *MCast;
- if (InsertBefore)
+ Value *MCast = NULL;
+ if (InsertBefore) {
+ MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertBefore);
+ // Create a cast instruction to convert to the right type...
MCast = new BitCastInst(MCall, AllocPtrType, NameStr, InsertBefore);
- else
+ } else {
+ MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertAtEnd);
+ // Create a cast instruction to convert to the right type...
MCast = new BitCastInst(MCall, AllocPtrType, NameStr);
+ }
+ MCall->setTailCall();
+ assert(MCall->getType() != Type::getVoidTy(BB->getContext()) &&
+ "Malloc has void return type");
+
return MCast;
}
/// constant 1.
/// 2. Call malloc with that argument.
/// 3. Bitcast the result of the malloc call to the specified type.
-Value *CallInst::CreateMalloc(Instruction *InsertBefore,
- const Type *AllocTy, const Type *IntPtrTy,
- Value *ArraySize, const Twine &NameStr) {
- return createMalloc(InsertBefore, NULL, AllocTy,
- IntPtrTy, ArraySize, NameStr);
+Value *CallInst::CreateMalloc(Instruction *InsertBefore, const Type *IntPtrTy,
+ const Type *AllocTy, Value *ArraySize,
+ const Twine &Name) {
+ return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, ArraySize, Name);
}
/// CreateMalloc - Generate the IR for a call to malloc:
/// 3. Bitcast the result of the malloc call to the specified type.
/// Note: This function does not add the bitcast to the basic block, that is the
/// responsibility of the caller.
-Value *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
- const Type *AllocTy, const Type *IntPtrTy,
- Value *ArraySize, const Twine &NameStr) {
- return createMalloc(NULL, InsertAtEnd, AllocTy,
- IntPtrTy, ArraySize, NameStr);
+Value *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, const Type *IntPtrTy,
+ const Type *AllocTy, Value *ArraySize,
+ const Twine &Name) {
+ return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, ArraySize, Name);
}
//===----------------------------------------------------------------------===//
GetElementPtrInst *GetElementPtrInst::clone(LLVMContext&) const {
GetElementPtrInst *New = new(getNumOperands()) GetElementPtrInst(*this);
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
BinaryOperator *BinaryOperator::clone(LLVMContext&) const {
BinaryOperator *New = Create(getOpcode(), Op<0>(), Op<1>());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
FCmpInst* FCmpInst::clone(LLVMContext &Context) const {
FCmpInst *New = new FCmpInst(getPredicate(), Op<0>(), Op<1>());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
ICmpInst* ICmpInst::clone(LLVMContext &Context) const {
ICmpInst *New = new ICmpInst(getPredicate(), Op<0>(), Op<1>());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
ExtractValueInst *ExtractValueInst::clone(LLVMContext&) const {
ExtractValueInst *New = new ExtractValueInst(*this);
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
InsertValueInst *InsertValueInst::clone(LLVMContext&) const {
InsertValueInst *New = new InsertValueInst(*this);
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
(Value*)getOperand(0),
getAlignment());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
(Value*)getOperand(0),
getAlignment());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
FreeInst *FreeInst::clone(LLVMContext&) const {
FreeInst *New = new FreeInst(getOperand(0));
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
Twine(), isVolatile(),
getAlignment());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
StoreInst *New = new StoreInst(getOperand(0), getOperand(1),
isVolatile(), getAlignment());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
TruncInst *TruncInst::clone(LLVMContext&) const {
TruncInst *New = new TruncInst(getOperand(0), getType());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
ZExtInst *ZExtInst::clone(LLVMContext&) const {
ZExtInst *New = new ZExtInst(getOperand(0), getType());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
SExtInst *SExtInst::clone(LLVMContext&) const {
SExtInst *New = new SExtInst(getOperand(0), getType());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
FPTruncInst *FPTruncInst::clone(LLVMContext&) const {
FPTruncInst *New = new FPTruncInst(getOperand(0), getType());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
FPExtInst *FPExtInst::clone(LLVMContext&) const {
FPExtInst *New = new FPExtInst(getOperand(0), getType());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
UIToFPInst *UIToFPInst::clone(LLVMContext&) const {
UIToFPInst *New = new UIToFPInst(getOperand(0), getType());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
SIToFPInst *SIToFPInst::clone(LLVMContext&) const {
SIToFPInst *New = new SIToFPInst(getOperand(0), getType());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
FPToUIInst *FPToUIInst::clone(LLVMContext&) const {
FPToUIInst *New = new FPToUIInst(getOperand(0), getType());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
FPToSIInst *FPToSIInst::clone(LLVMContext&) const {
FPToSIInst *New = new FPToSIInst(getOperand(0), getType());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
PtrToIntInst *PtrToIntInst::clone(LLVMContext&) const {
PtrToIntInst *New = new PtrToIntInst(getOperand(0), getType());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
IntToPtrInst *IntToPtrInst::clone(LLVMContext&) const {
IntToPtrInst *New = new IntToPtrInst(getOperand(0), getType());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
BitCastInst *BitCastInst::clone(LLVMContext&) const {
BitCastInst *New = new BitCastInst(getOperand(0), getType());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
CallInst *CallInst::clone(LLVMContext&) const {
CallInst *New = new(getNumOperands()) CallInst(*this);
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
getOperand(1),
getOperand(2));
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
VAArgInst *VAArgInst::clone(LLVMContext&) const {
VAArgInst *New = new VAArgInst(getOperand(0), getType());
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
ExtractElementInst *New = ExtractElementInst::Create(getOperand(0),
getOperand(1));
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
getOperand(1),
getOperand(2));
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
getOperand(1),
getOperand(2));
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
PHINode *PHINode::clone(LLVMContext&) const {
PHINode *New = new PHINode(*this);
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
ReturnInst *ReturnInst::clone(LLVMContext&) const {
ReturnInst *New = new(getNumOperands()) ReturnInst(*this);
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
unsigned Ops(getNumOperands());
BranchInst *New = new(Ops, Ops == 1) BranchInst(*this);
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
SwitchInst *SwitchInst::clone(LLVMContext&) const {
SwitchInst *New = new SwitchInst(*this);
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
InvokeInst *InvokeInst::clone(LLVMContext&) const {
InvokeInst *New = new(getNumOperands()) InvokeInst(*this);
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
UnwindInst *UnwindInst::clone(LLVMContext &C) const {
UnwindInst *New = new UnwindInst(C);
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}
UnreachableInst *UnreachableInst::clone(LLVMContext &C) const {
UnreachableInst *New = new UnreachableInst(C);
New->SubclassOptionalData = SubclassOptionalData;
+ if (hasMetadata()) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsCloned(this, New);
+ }
return New;
}