2004-07-29 12:33:25 +00:00
|
|
|
//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-07-29 12:33:25 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-07-29 12:33:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2005-01-29 00:35:16 +00:00
|
|
|
// This file implements all of the non-inline methods for the LLVM instruction
|
|
|
|
// classes.
|
2004-07-29 12:33:25 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Support/CallSite.h"
|
2007-02-28 22:00:54 +00:00
|
|
|
#include "llvm/Support/ConstantRange.h"
|
2007-04-22 19:24:39 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2004-07-29 12:33:25 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-01-02 23:42:30 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CallSite Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-18 17:32:13 +00:00
|
|
|
CallSite::CallSite(Instruction *C) {
|
|
|
|
assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
|
|
|
|
I = C;
|
|
|
|
}
|
2005-05-06 20:26:43 +00:00
|
|
|
unsigned CallSite::getCallingConv() const {
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I))
|
|
|
|
return CI->getCallingConv();
|
|
|
|
else
|
|
|
|
return cast<InvokeInst>(I)->getCallingConv();
|
|
|
|
}
|
|
|
|
void CallSite::setCallingConv(unsigned CC) {
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I))
|
|
|
|
CI->setCallingConv(CC);
|
|
|
|
else
|
|
|
|
cast<InvokeInst>(I)->setCallingConv(CC);
|
|
|
|
}
|
2008-03-12 17:45:29 +00:00
|
|
|
const PAListPtr &CallSite::getParamAttrs() const {
|
2007-11-27 13:23:08 +00:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I))
|
|
|
|
return CI->getParamAttrs();
|
|
|
|
else
|
|
|
|
return cast<InvokeInst>(I)->getParamAttrs();
|
|
|
|
}
|
2008-03-12 17:45:29 +00:00
|
|
|
void CallSite::setParamAttrs(const PAListPtr &PAL) {
|
2007-11-27 13:23:08 +00:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I))
|
|
|
|
CI->setParamAttrs(PAL);
|
|
|
|
else
|
|
|
|
cast<InvokeInst>(I)->setParamAttrs(PAL);
|
|
|
|
}
|
2008-02-19 21:38:47 +00:00
|
|
|
bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
|
2007-11-28 17:07:01 +00:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I))
|
2008-02-19 21:38:47 +00:00
|
|
|
return CI->paramHasAttr(i, attr);
|
2007-11-28 17:07:01 +00:00
|
|
|
else
|
2008-02-19 21:38:47 +00:00
|
|
|
return cast<InvokeInst>(I)->paramHasAttr(i, attr);
|
2007-11-28 17:07:01 +00:00
|
|
|
}
|
2008-02-22 17:49:45 +00:00
|
|
|
uint16_t CallSite::getParamAlignment(uint16_t i) const {
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I))
|
|
|
|
return CI->getParamAlignment(i);
|
|
|
|
else
|
|
|
|
return cast<InvokeInst>(I)->getParamAlignment(i);
|
|
|
|
}
|
|
|
|
|
2007-12-03 20:06:50 +00:00
|
|
|
bool CallSite::doesNotAccessMemory() const {
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I))
|
|
|
|
return CI->doesNotAccessMemory();
|
|
|
|
else
|
|
|
|
return cast<InvokeInst>(I)->doesNotAccessMemory();
|
|
|
|
}
|
|
|
|
bool CallSite::onlyReadsMemory() const {
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I))
|
|
|
|
return CI->onlyReadsMemory();
|
|
|
|
else
|
|
|
|
return cast<InvokeInst>(I)->onlyReadsMemory();
|
|
|
|
}
|
2007-12-18 09:59:50 +00:00
|
|
|
bool CallSite::doesNotThrow() const {
|
2007-12-16 15:51:49 +00:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I))
|
2007-12-18 09:59:50 +00:00
|
|
|
return CI->doesNotThrow();
|
2007-12-16 15:51:49 +00:00
|
|
|
else
|
2007-12-18 09:59:50 +00:00
|
|
|
return cast<InvokeInst>(I)->doesNotThrow();
|
2007-12-16 15:51:49 +00:00
|
|
|
}
|
2007-12-19 21:13:37 +00:00
|
|
|
void CallSite::setDoesNotThrow(bool doesNotThrow) {
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(I))
|
|
|
|
CI->setDoesNotThrow(doesNotThrow);
|
|
|
|
else
|
|
|
|
cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
|
|
|
|
}
|
2005-05-06 20:26:43 +00:00
|
|
|
|
2007-12-10 02:14:30 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TerminatorInst Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Out of line virtual method, so the vtable, etc has a home.
|
|
|
|
TerminatorInst::~TerminatorInst() {
|
|
|
|
}
|
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// UnaryInstruction Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-12-10 02:14:30 +00:00
|
|
|
// Out of line virtual method, so the vtable, etc has a home.
|
|
|
|
UnaryInstruction::~UnaryInstruction() {
|
|
|
|
}
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PHINode Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
PHINode::PHINode(const PHINode &PN)
|
|
|
|
: Instruction(PN.getType(), Instruction::PHI,
|
2008-05-10 08:32:32 +00:00
|
|
|
allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
|
2005-01-29 00:35:16 +00:00
|
|
|
ReservedSpace(PN.getNumOperands()) {
|
|
|
|
Use *OL = OperandList;
|
|
|
|
for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[i] = PN.getOperand(i);
|
|
|
|
OL[i+1] = PN.getOperand(i+1);
|
2005-01-29 00:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-10 02:14:30 +00:00
|
|
|
PHINode::~PHINode() {
|
2008-05-10 08:32:32 +00:00
|
|
|
dropHungoffUses(OperandList);
|
2005-01-29 00:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// removeIncomingValue - Remove an incoming value. This is useful if a
|
|
|
|
// predecessor basic block is deleted.
|
|
|
|
Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
|
|
|
|
unsigned NumOps = getNumOperands();
|
|
|
|
Use *OL = OperandList;
|
|
|
|
assert(Idx*2 < NumOps && "BB not in PHI node!");
|
|
|
|
Value *Removed = OL[Idx*2];
|
|
|
|
|
|
|
|
// Move everything after this operand down.
|
|
|
|
//
|
|
|
|
// FIXME: we could just swap with the end of the list, then erase. However,
|
|
|
|
// client might not expect this to happen. The code as it is thrashes the
|
|
|
|
// use/def lists, which is kinda lame.
|
|
|
|
for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
|
|
|
|
OL[i-2] = OL[i];
|
|
|
|
OL[i-2+1] = OL[i+1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nuke the last value.
|
|
|
|
OL[NumOps-2].set(0);
|
|
|
|
OL[NumOps-2+1].set(0);
|
|
|
|
NumOperands = NumOps-2;
|
|
|
|
|
|
|
|
// If the PHI node is dead, because it has zero entries, nuke it now.
|
|
|
|
if (NumOps == 2 && DeletePHIIfEmpty) {
|
|
|
|
// If anyone is using this PHI, make them use a dummy value instead...
|
|
|
|
replaceAllUsesWith(UndefValue::get(getType()));
|
|
|
|
eraseFromParent();
|
|
|
|
}
|
|
|
|
return Removed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// resizeOperands - resize operands - This adjusts the length of the operands
|
|
|
|
/// list according to the following behavior:
|
|
|
|
/// 1. If NumOps == 0, grow the operand list in response to a push_back style
|
|
|
|
/// of operation. This grows the number of ops by 1.5 times.
|
|
|
|
/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
|
|
|
|
/// 3. If NumOps == NumOperands, trim the reserved space.
|
|
|
|
///
|
|
|
|
void PHINode::resizeOperands(unsigned NumOps) {
|
2008-05-10 08:32:32 +00:00
|
|
|
unsigned e = getNumOperands();
|
2005-01-29 00:35:16 +00:00
|
|
|
if (NumOps == 0) {
|
2008-05-10 08:32:32 +00:00
|
|
|
NumOps = e*3/2;
|
2005-01-29 00:35:16 +00:00
|
|
|
if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
|
|
|
|
} else if (NumOps*2 > NumOperands) {
|
|
|
|
// No resize needed.
|
|
|
|
if (ReservedSpace >= NumOps) return;
|
|
|
|
} else if (NumOps == NumOperands) {
|
|
|
|
if (ReservedSpace == NumOps) return;
|
|
|
|
} else {
|
2005-04-21 23:48:37 +00:00
|
|
|
return;
|
2005-01-29 00:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ReservedSpace = NumOps;
|
|
|
|
Use *OldOps = OperandList;
|
2008-05-10 08:32:32 +00:00
|
|
|
Use *NewOps = allocHungoffUses(NumOps);
|
|
|
|
for (unsigned i = 0; i != e; ++i) {
|
2008-05-26 21:33:52 +00:00
|
|
|
NewOps[i] = OldOps[i];
|
2005-01-29 00:35:16 +00:00
|
|
|
}
|
|
|
|
OperandList = NewOps;
|
2008-05-10 08:32:32 +00:00
|
|
|
if (OldOps) Use::zap(OldOps, OldOps + e, true);
|
2005-01-29 00:35:16 +00:00
|
|
|
}
|
|
|
|
|
2005-08-04 23:24:19 +00:00
|
|
|
/// hasConstantValue - If the specified PHI node always merges together the same
|
|
|
|
/// value, return the value, otherwise return null.
|
|
|
|
///
|
2005-08-05 00:49:06 +00:00
|
|
|
Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
|
2005-08-04 23:24:19 +00:00
|
|
|
// If the PHI node only has one incoming value, eliminate the PHI node...
|
2008-02-20 11:08:44 +00:00
|
|
|
if (getNumIncomingValues() == 1) {
|
2005-08-05 15:37:31 +00:00
|
|
|
if (getIncomingValue(0) != this) // not X = phi X
|
|
|
|
return getIncomingValue(0);
|
|
|
|
else
|
|
|
|
return UndefValue::get(getType()); // Self cycle is dead.
|
2008-02-20 11:08:44 +00:00
|
|
|
}
|
2005-08-05 15:37:31 +00:00
|
|
|
|
2005-08-04 23:24:19 +00:00
|
|
|
// Otherwise if all of the incoming values are the same for the PHI, replace
|
|
|
|
// the PHI node with the incoming value.
|
|
|
|
//
|
|
|
|
Value *InVal = 0;
|
2005-08-05 01:00:58 +00:00
|
|
|
bool HasUndefInput = false;
|
2005-08-04 23:24:19 +00:00
|
|
|
for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
|
2008-02-20 11:08:44 +00:00
|
|
|
if (isa<UndefValue>(getIncomingValue(i))) {
|
2005-08-05 01:00:58 +00:00
|
|
|
HasUndefInput = true;
|
2008-02-20 11:08:44 +00:00
|
|
|
} else if (getIncomingValue(i) != this) { // Not the PHI node itself...
|
2005-08-04 23:24:19 +00:00
|
|
|
if (InVal && getIncomingValue(i) != InVal)
|
|
|
|
return 0; // Not the same, bail out.
|
|
|
|
else
|
|
|
|
InVal = getIncomingValue(i);
|
2008-02-20 11:08:44 +00:00
|
|
|
}
|
2005-08-04 23:24:19 +00:00
|
|
|
|
|
|
|
// The only case that could cause InVal to be null is if we have a PHI node
|
|
|
|
// that only has entries for itself. In this case, there is no entry into the
|
|
|
|
// loop, so kill the PHI.
|
|
|
|
//
|
|
|
|
if (InVal == 0) InVal = UndefValue::get(getType());
|
|
|
|
|
2005-08-05 01:00:58 +00:00
|
|
|
// If we have a PHI node like phi(X, undef, X), where X is defined by some
|
|
|
|
// 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 && !AllowNonDominatingInstruction)
|
|
|
|
if (Instruction *IV = dyn_cast<Instruction>(InVal))
|
|
|
|
// If it's in the entry block, it dominates everything.
|
2007-03-22 16:38:57 +00:00
|
|
|
if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
|
2005-08-05 01:03:27 +00:00
|
|
|
isa<InvokeInst>(IV))
|
2005-08-05 01:00:58 +00:00
|
|
|
return 0; // Cannot guarantee that InVal dominates this PHINode.
|
|
|
|
|
2005-08-04 23:24:19 +00:00
|
|
|
// All of the incoming values are the same, return the value now.
|
|
|
|
return InVal;
|
|
|
|
}
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
|
2004-07-29 12:33:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CallInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-12-10 02:14:30 +00:00
|
|
|
CallInst::~CallInst() {
|
2005-01-29 00:35:16 +00:00
|
|
|
}
|
|
|
|
|
2007-02-13 00:58:44 +00:00
|
|
|
void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
|
2008-05-10 08:32:32 +00:00
|
|
|
assert(NumOperands == NumParams+1 && "NumOperands not set up?");
|
|
|
|
Use *OL = OperandList;
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[0] = Func;
|
2004-07-29 12:33:25 +00:00
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
const FunctionType *FTy =
|
2004-07-29 12:33:25 +00:00
|
|
|
cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
|
2007-02-01 04:59:37 +00:00
|
|
|
FTy = FTy; // silence warning.
|
2004-07-29 12:33:25 +00:00
|
|
|
|
2007-02-13 00:58:44 +00:00
|
|
|
assert((NumParams == FTy->getNumParams() ||
|
|
|
|
(FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
|
2006-05-03 00:48:22 +00:00
|
|
|
"Calling a function with bad signature!");
|
2007-02-13 00:58:44 +00:00
|
|
|
for (unsigned i = 0; i != NumParams; ++i) {
|
2006-05-03 00:48:22 +00:00
|
|
|
assert((i >= FTy->getNumParams() ||
|
|
|
|
FTy->getParamType(i) == Params[i]->getType()) &&
|
|
|
|
"Calling a function with a bad signature!");
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[i+1] = Params[i];
|
2006-05-03 00:48:22 +00:00
|
|
|
}
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
|
2008-05-10 08:32:32 +00:00
|
|
|
assert(NumOperands == 3 && "NumOperands not set up?");
|
|
|
|
Use *OL = OperandList;
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[0] = Func;
|
|
|
|
OL[1] = Actual1;
|
|
|
|
OL[2] = Actual2;
|
2005-04-21 23:48:37 +00:00
|
|
|
|
|
|
|
const FunctionType *FTy =
|
2004-07-29 12:33:25 +00:00
|
|
|
cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
|
2007-02-01 04:59:37 +00:00
|
|
|
FTy = FTy; // silence warning.
|
2004-07-29 12:33:25 +00:00
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
assert((FTy->getNumParams() == 2 ||
|
2006-05-03 00:48:22 +00:00
|
|
|
(FTy->isVarArg() && FTy->getNumParams() < 2)) &&
|
2004-07-29 12:33:25 +00:00
|
|
|
"Calling a function with bad signature");
|
2006-05-03 00:48:22 +00:00
|
|
|
assert((0 >= FTy->getNumParams() ||
|
|
|
|
FTy->getParamType(0) == Actual1->getType()) &&
|
|
|
|
"Calling a function with a bad signature!");
|
|
|
|
assert((1 >= FTy->getNumParams() ||
|
|
|
|
FTy->getParamType(1) == Actual2->getType()) &&
|
|
|
|
"Calling a function with a bad signature!");
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
void CallInst::init(Value *Func, Value *Actual) {
|
2008-05-10 08:32:32 +00:00
|
|
|
assert(NumOperands == 2 && "NumOperands not set up?");
|
|
|
|
Use *OL = OperandList;
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[0] = Func;
|
|
|
|
OL[1] = Actual;
|
2005-04-21 23:48:37 +00:00
|
|
|
|
|
|
|
const FunctionType *FTy =
|
2004-07-29 12:33:25 +00:00
|
|
|
cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
|
2007-02-01 04:59:37 +00:00
|
|
|
FTy = FTy; // silence warning.
|
2004-07-29 12:33:25 +00:00
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
assert((FTy->getNumParams() == 1 ||
|
|
|
|
(FTy->isVarArg() && FTy->getNumParams() == 0)) &&
|
2004-07-29 12:33:25 +00:00
|
|
|
"Calling a function with bad signature");
|
2006-05-03 00:48:22 +00:00
|
|
|
assert((0 == FTy->getNumParams() ||
|
|
|
|
FTy->getParamType(0) == Actual->getType()) &&
|
|
|
|
"Calling a function with a bad signature!");
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
void CallInst::init(Value *Func) {
|
2008-05-10 08:32:32 +00:00
|
|
|
assert(NumOperands == 1 && "NumOperands not set up?");
|
|
|
|
Use *OL = OperandList;
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[0] = Func;
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2007-02-01 04:59:37 +00:00
|
|
|
const FunctionType *FTy =
|
2004-07-29 12:33:25 +00:00
|
|
|
cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
|
2007-02-01 04:59:37 +00:00
|
|
|
FTy = FTy; // silence warning.
|
2004-07-29 12:33:25 +00:00
|
|
|
|
2007-02-01 04:59:37 +00:00
|
|
|
assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
|
2007-02-24 00:55:48 +00:00
|
|
|
Instruction *InsertBefore)
|
2004-07-29 12:33:25 +00:00
|
|
|
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
|
|
|
|
->getElementType())->getReturnType(),
|
2008-05-10 08:32:32 +00:00
|
|
|
Instruction::Call,
|
|
|
|
OperandTraits<CallInst>::op_end(this) - 2,
|
|
|
|
2, InsertBefore) {
|
2004-07-29 12:33:25 +00:00
|
|
|
init(Func, Actual);
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
|
|
|
|
BasicBlock *InsertAtEnd)
|
|
|
|
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
|
|
|
|
->getElementType())->getReturnType(),
|
2008-05-10 08:32:32 +00:00
|
|
|
Instruction::Call,
|
|
|
|
OperandTraits<CallInst>::op_end(this) - 2,
|
|
|
|
2, InsertAtEnd) {
|
2004-07-29 12:33:25 +00:00
|
|
|
init(Func, Actual);
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
CallInst::CallInst(Value *Func, const std::string &Name,
|
|
|
|
Instruction *InsertBefore)
|
|
|
|
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
|
|
|
|
->getElementType())->getReturnType(),
|
2008-05-10 08:32:32 +00:00
|
|
|
Instruction::Call,
|
|
|
|
OperandTraits<CallInst>::op_end(this) - 1,
|
|
|
|
1, InsertBefore) {
|
2004-07-29 12:33:25 +00:00
|
|
|
init(Func);
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CallInst::CallInst(Value *Func, const std::string &Name,
|
|
|
|
BasicBlock *InsertAtEnd)
|
|
|
|
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
|
|
|
|
->getElementType())->getReturnType(),
|
2008-05-10 08:32:32 +00:00
|
|
|
Instruction::Call,
|
|
|
|
OperandTraits<CallInst>::op_end(this) - 1,
|
|
|
|
1, InsertAtEnd) {
|
2004-07-29 12:33:25 +00:00
|
|
|
init(Func);
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
CallInst::CallInst(const CallInst &CI)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(CI.getType(), Instruction::Call,
|
|
|
|
OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
|
2008-03-12 17:45:29 +00:00
|
|
|
CI.getNumOperands()) {
|
2007-11-27 13:23:08 +00:00
|
|
|
setParamAttrs(CI.getParamAttrs());
|
2005-05-06 20:26:43 +00:00
|
|
|
SubclassData = CI.SubclassData;
|
2005-01-29 00:35:16 +00:00
|
|
|
Use *OL = OperandList;
|
|
|
|
Use *InOL = CI.OperandList;
|
|
|
|
for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[i] = InOL[i];
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 20:39:43 +00:00
|
|
|
void CallInst::addParamAttr(unsigned i, ParameterAttributes attr) {
|
|
|
|
PAListPtr PAL = getParamAttrs();
|
|
|
|
PAL = PAL.addAttr(i, attr);
|
|
|
|
setParamAttrs(PAL);
|
|
|
|
}
|
|
|
|
|
2008-03-13 05:00:21 +00:00
|
|
|
bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
|
2008-03-12 17:45:29 +00:00
|
|
|
if (ParamAttrs.paramHasAttr(i, attr))
|
2007-11-28 17:07:01 +00:00
|
|
|
return true;
|
2007-11-29 08:30:15 +00:00
|
|
|
if (const Function *F = getCalledFunction())
|
2008-02-19 21:38:47 +00:00
|
|
|
return F->paramHasAttr(i, attr);
|
2007-11-29 08:30:15 +00:00
|
|
|
return false;
|
2007-11-27 13:23:08 +00:00
|
|
|
}
|
|
|
|
|
2007-12-19 21:13:37 +00:00
|
|
|
void CallInst::setDoesNotThrow(bool doesNotThrow) {
|
2008-03-12 17:45:29 +00:00
|
|
|
PAListPtr PAL = getParamAttrs();
|
2007-12-19 21:13:37 +00:00
|
|
|
if (doesNotThrow)
|
2008-03-12 17:45:29 +00:00
|
|
|
PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
|
2007-12-19 21:13:37 +00:00
|
|
|
else
|
2008-03-12 17:45:29 +00:00
|
|
|
PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
|
2007-12-19 21:13:37 +00:00
|
|
|
setParamAttrs(PAL);
|
|
|
|
}
|
|
|
|
|
2007-11-28 17:07:01 +00:00
|
|
|
|
2004-07-29 12:33:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// InvokeInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
|
2007-02-13 01:04:01 +00:00
|
|
|
Value* const *Args, unsigned NumArgs) {
|
2008-05-10 08:32:32 +00:00
|
|
|
assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
|
|
|
|
Use *OL = OperandList;
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[0] = Fn;
|
|
|
|
OL[1] = IfNormal;
|
|
|
|
OL[2] = IfException;
|
2005-04-21 23:48:37 +00:00
|
|
|
const FunctionType *FTy =
|
2004-07-29 12:33:25 +00:00
|
|
|
cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
|
2007-02-01 04:59:37 +00:00
|
|
|
FTy = FTy; // silence warning.
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2008-02-20 11:08:44 +00:00
|
|
|
assert(((NumArgs == FTy->getNumParams()) ||
|
|
|
|
(FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
|
2004-07-29 12:33:25 +00:00
|
|
|
"Calling a function with bad signature");
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2007-02-13 01:04:01 +00:00
|
|
|
for (unsigned i = 0, e = NumArgs; i != e; i++) {
|
2006-05-03 00:48:22 +00:00
|
|
|
assert((i >= FTy->getNumParams() ||
|
2007-02-13 01:04:01 +00:00
|
|
|
FTy->getParamType(i) == Args[i]->getType()) &&
|
2006-05-03 00:48:22 +00:00
|
|
|
"Invoking a function with a bad signature!");
|
|
|
|
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[i+3] = Args[i];
|
2006-05-03 00:48:22 +00:00
|
|
|
}
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
InvokeInst::InvokeInst(const InvokeInst &II)
|
2005-01-29 00:35:16 +00:00
|
|
|
: TerminatorInst(II.getType(), Instruction::Invoke,
|
2008-05-15 10:04:30 +00:00
|
|
|
OperandTraits<InvokeInst>::op_end(this)
|
|
|
|
- II.getNumOperands(),
|
2008-05-10 08:32:32 +00:00
|
|
|
II.getNumOperands()) {
|
2007-11-27 13:23:08 +00:00
|
|
|
setParamAttrs(II.getParamAttrs());
|
2005-05-06 20:26:43 +00:00
|
|
|
SubclassData = II.SubclassData;
|
2005-01-29 00:35:16 +00:00
|
|
|
Use *OL = OperandList, *InOL = II.OperandList;
|
|
|
|
for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[i] = InOL[i];
|
2005-01-29 00:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
|
|
|
|
return getSuccessor(idx);
|
|
|
|
}
|
|
|
|
unsigned InvokeInst::getNumSuccessorsV() const {
|
|
|
|
return getNumSuccessors();
|
|
|
|
}
|
|
|
|
void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
|
|
|
|
return setSuccessor(idx, B);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2008-03-13 05:00:21 +00:00
|
|
|
bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
|
2008-03-12 17:45:29 +00:00
|
|
|
if (ParamAttrs.paramHasAttr(i, attr))
|
2007-11-28 17:07:01 +00:00
|
|
|
return true;
|
2007-11-29 08:30:15 +00:00
|
|
|
if (const Function *F = getCalledFunction())
|
2008-02-19 21:38:47 +00:00
|
|
|
return F->paramHasAttr(i, attr);
|
2007-11-29 08:30:15 +00:00
|
|
|
return false;
|
2007-11-27 13:23:08 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 20:39:43 +00:00
|
|
|
void InvokeInst::addParamAttr(unsigned i, ParameterAttributes attr) {
|
|
|
|
PAListPtr PAL = getParamAttrs();
|
|
|
|
PAL = PAL.addAttr(i, attr);
|
|
|
|
setParamAttrs(PAL);
|
|
|
|
}
|
|
|
|
|
2007-12-19 21:13:37 +00:00
|
|
|
void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
|
2008-03-12 17:45:29 +00:00
|
|
|
PAListPtr PAL = getParamAttrs();
|
2007-12-19 21:13:37 +00:00
|
|
|
if (doesNotThrow)
|
2008-03-12 17:45:29 +00:00
|
|
|
PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
|
2007-12-19 21:13:37 +00:00
|
|
|
else
|
2008-03-12 17:45:29 +00:00
|
|
|
PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
|
2007-12-19 21:13:37 +00:00
|
|
|
setParamAttrs(PAL);
|
|
|
|
}
|
|
|
|
|
2007-11-28 17:07:01 +00:00
|
|
|
|
2004-07-29 12:33:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ReturnInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-02-24 00:55:48 +00:00
|
|
|
ReturnInst::ReturnInst(const ReturnInst &RI)
|
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Ret,
|
2008-05-15 10:04:30 +00:00
|
|
|
OperandTraits<ReturnInst>::op_end(this)
|
|
|
|
- RI.getNumOperands(),
|
2008-05-10 08:32:32 +00:00
|
|
|
RI.getNumOperands()) {
|
2008-02-23 00:35:18 +00:00
|
|
|
unsigned N = RI.getNumOperands();
|
2008-05-10 08:32:32 +00:00
|
|
|
if (N == 1)
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = RI.Op<0>();
|
2008-02-26 17:56:20 +00:00
|
|
|
else if (N) {
|
2008-05-10 08:32:32 +00:00
|
|
|
Use *OL = OperandList;
|
2008-02-26 17:56:20 +00:00
|
|
|
for (unsigned i = 0; i < N; ++i)
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[i] = RI.getOperand(i);
|
2008-02-26 17:56:20 +00:00
|
|
|
}
|
2007-02-24 00:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
|
2008-05-10 08:32:32 +00:00
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Ret,
|
|
|
|
OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
|
|
|
|
retVal != 0, InsertBefore) {
|
2008-02-26 18:49:29 +00:00
|
|
|
if (retVal)
|
|
|
|
init(&retVal, 1);
|
2007-02-24 00:55:48 +00:00
|
|
|
}
|
|
|
|
ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
|
2008-05-10 08:32:32 +00:00
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Ret,
|
|
|
|
OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
|
|
|
|
retVal != 0, InsertAtEnd) {
|
2008-02-26 18:49:29 +00:00
|
|
|
if (retVal)
|
|
|
|
init(&retVal, 1);
|
2007-02-24 00:55:48 +00:00
|
|
|
}
|
|
|
|
ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
|
2008-05-10 08:32:32 +00:00
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Ret,
|
|
|
|
OperandTraits<ReturnInst>::op_end(this),
|
|
|
|
0, InsertAtEnd) {
|
2007-02-24 00:55:48 +00:00
|
|
|
}
|
|
|
|
|
2008-02-26 19:38:17 +00:00
|
|
|
ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
|
|
|
|
Instruction *InsertBefore)
|
2008-05-10 08:32:32 +00:00
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Ret,
|
|
|
|
OperandTraits<ReturnInst>::op_end(this) - N,
|
|
|
|
N, InsertBefore) {
|
2008-02-26 19:38:17 +00:00
|
|
|
if (N != 0)
|
|
|
|
init(retVals, N);
|
|
|
|
}
|
|
|
|
ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
|
|
|
|
BasicBlock *InsertAtEnd)
|
2008-05-10 08:32:32 +00:00
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Ret,
|
|
|
|
OperandTraits<ReturnInst>::op_end(this) - N,
|
|
|
|
N, InsertAtEnd) {
|
2008-02-26 19:38:17 +00:00
|
|
|
if (N != 0)
|
|
|
|
init(retVals, N);
|
|
|
|
}
|
|
|
|
|
2008-02-26 19:15:26 +00:00
|
|
|
void ReturnInst::init(Value * const* retVals, unsigned N) {
|
2008-02-26 18:49:29 +00:00
|
|
|
assert (N > 0 && "Invalid operands numbers in ReturnInst init");
|
2008-02-23 00:35:18 +00:00
|
|
|
|
2008-02-26 18:49:29 +00:00
|
|
|
NumOperands = N;
|
2008-02-23 00:35:18 +00:00
|
|
|
if (NumOperands == 1) {
|
2008-02-26 19:15:26 +00:00
|
|
|
Value *V = *retVals;
|
2008-02-23 00:35:18 +00:00
|
|
|
if (V->getType() == Type::VoidTy)
|
|
|
|
return;
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = V;
|
2008-02-26 17:56:20 +00:00
|
|
|
return;
|
2008-02-23 00:35:18 +00:00
|
|
|
}
|
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
Use *OL = OperandList;
|
2008-02-23 00:35:18 +00:00
|
|
|
for (unsigned i = 0; i < NumOperands; ++i) {
|
2008-02-26 19:15:26 +00:00
|
|
|
Value *V = *retVals++;
|
2008-02-23 00:35:18 +00:00
|
|
|
assert(!isa<BasicBlock>(V) &&
|
|
|
|
"Cannot return basic block. Probably using the incorrect ctor");
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[i] = V;
|
2004-11-17 21:02:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
unsigned ReturnInst::getNumSuccessorsV() const {
|
|
|
|
return getNumSuccessors();
|
|
|
|
}
|
|
|
|
|
2008-02-26 17:56:20 +00:00
|
|
|
/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
|
|
|
|
/// emit the vtable for the class in this translation unit.
|
2005-01-29 00:35:16 +00:00
|
|
|
void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
|
2004-07-29 12:33:25 +00:00
|
|
|
assert(0 && "ReturnInst has no successors!");
|
|
|
|
}
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
|
|
|
|
assert(0 && "ReturnInst has no successors!");
|
|
|
|
abort();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-23 00:35:18 +00:00
|
|
|
ReturnInst::~ReturnInst() {
|
|
|
|
}
|
2005-01-29 00:35:16 +00:00
|
|
|
|
2004-07-29 12:33:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// UnwindInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-02-24 00:55:48 +00:00
|
|
|
UnwindInst::UnwindInst(Instruction *InsertBefore)
|
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
|
|
|
|
}
|
|
|
|
UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
|
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
unsigned UnwindInst::getNumSuccessorsV() const {
|
|
|
|
return getNumSuccessors();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
|
2004-07-29 12:33:25 +00:00
|
|
|
assert(0 && "UnwindInst has no successors!");
|
|
|
|
}
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
|
|
|
|
assert(0 && "UnwindInst has no successors!");
|
|
|
|
abort();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-16 18:08:06 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// UnreachableInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-02-24 00:55:48 +00:00
|
|
|
UnreachableInst::UnreachableInst(Instruction *InsertBefore)
|
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
|
|
|
|
}
|
|
|
|
UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
|
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
|
|
|
|
}
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
unsigned UnreachableInst::getNumSuccessorsV() const {
|
|
|
|
return getNumSuccessors();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
|
|
|
|
assert(0 && "UnwindInst has no successors!");
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
|
|
|
|
assert(0 && "UnwindInst has no successors!");
|
|
|
|
abort();
|
|
|
|
return 0;
|
2004-10-16 18:08:06 +00:00
|
|
|
}
|
|
|
|
|
2004-07-29 12:33:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BranchInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
void BranchInst::AssertOK() {
|
|
|
|
if (isConditional())
|
2007-01-11 18:21:29 +00:00
|
|
|
assert(getCondition()->getType() == Type::Int1Ty &&
|
2005-01-29 00:35:16 +00:00
|
|
|
"May only branch on boolean predicates!");
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2007-02-24 00:55:48 +00:00
|
|
|
BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
|
2008-05-10 08:32:32 +00:00
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Br,
|
|
|
|
OperandTraits<BranchInst>::op_end(this) - 1,
|
|
|
|
1, InsertBefore) {
|
2007-02-24 00:55:48 +00:00
|
|
|
assert(IfTrue != 0 && "Branch destination may not be null!");
|
2008-05-27 10:48:39 +00:00
|
|
|
Op<0>() = IfTrue;
|
2007-02-24 00:55:48 +00:00
|
|
|
}
|
|
|
|
BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
|
|
|
|
Instruction *InsertBefore)
|
2008-05-10 08:32:32 +00:00
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Br,
|
|
|
|
OperandTraits<BranchInst>::op_end(this) - 3,
|
|
|
|
3, InsertBefore) {
|
2008-05-27 10:48:39 +00:00
|
|
|
Op<0>() = IfTrue;
|
|
|
|
Op<1>() = IfFalse;
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<2>() = Cond;
|
2007-02-24 00:55:48 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
AssertOK();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
|
2008-05-10 08:32:32 +00:00
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Br,
|
|
|
|
OperandTraits<BranchInst>::op_end(this) - 1,
|
|
|
|
1, InsertAtEnd) {
|
2007-02-24 00:55:48 +00:00
|
|
|
assert(IfTrue != 0 && "Branch destination may not be null!");
|
2008-05-27 10:48:39 +00:00
|
|
|
Op<0>() = IfTrue;
|
2007-02-24 00:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
|
|
|
|
BasicBlock *InsertAtEnd)
|
2008-05-10 08:32:32 +00:00
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Br,
|
|
|
|
OperandTraits<BranchInst>::op_end(this) - 3,
|
|
|
|
3, InsertAtEnd) {
|
2008-05-27 10:48:39 +00:00
|
|
|
Op<0>() = IfTrue;
|
|
|
|
Op<1>() = IfFalse;
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<2>() = Cond;
|
2007-02-24 00:55:48 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
AssertOK();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
BranchInst::BranchInst(const BranchInst &BI) :
|
2008-05-10 08:32:32 +00:00
|
|
|
TerminatorInst(Type::VoidTy, Instruction::Br,
|
|
|
|
OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
|
|
|
|
BI.getNumOperands()) {
|
2008-05-26 21:33:52 +00:00
|
|
|
OperandList[0] = BI.getOperand(0);
|
2005-01-29 00:35:16 +00:00
|
|
|
if (BI.getNumOperands() != 1) {
|
|
|
|
assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
|
2008-05-26 21:33:52 +00:00
|
|
|
OperandList[1] = BI.getOperand(1);
|
|
|
|
OperandList[2] = BI.getOperand(2);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
|
|
|
|
return getSuccessor(idx);
|
|
|
|
}
|
|
|
|
unsigned BranchInst::getNumSuccessorsV() const {
|
|
|
|
return getNumSuccessors();
|
|
|
|
}
|
|
|
|
void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
|
|
|
|
setSuccessor(idx, B);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-29 12:33:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AllocationInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
static Value *getAISize(Value *Amt) {
|
|
|
|
if (!Amt)
|
2006-12-31 05:26:44 +00:00
|
|
|
Amt = ConstantInt::get(Type::Int32Ty, 1);
|
2006-05-10 04:32:43 +00:00
|
|
|
else {
|
|
|
|
assert(!isa<BasicBlock>(Amt) &&
|
2007-10-18 16:10:48 +00:00
|
|
|
"Passed basic block into allocation size parameter! Use other ctor");
|
2006-12-31 05:26:44 +00:00
|
|
|
assert(Amt->getType() == Type::Int32Ty &&
|
2007-01-26 06:30:34 +00:00
|
|
|
"Malloc/Allocation array size is not a 32-bit integer!");
|
2006-05-10 04:32:43 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
return Amt;
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
|
2005-11-05 09:21:28 +00:00
|
|
|
unsigned Align, const std::string &Name,
|
2004-07-29 12:33:25 +00:00
|
|
|
Instruction *InsertBefore)
|
2007-12-17 01:12:55 +00:00
|
|
|
: UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
|
2008-03-24 16:55:58 +00:00
|
|
|
InsertBefore) {
|
|
|
|
setAlignment(Align);
|
2005-01-29 00:35:16 +00:00
|
|
|
assert(Ty != Type::VoidTy && "Cannot allocate void!");
|
2007-02-13 07:54:42 +00:00
|
|
|
setName(Name);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
|
2005-11-05 09:21:28 +00:00
|
|
|
unsigned Align, const std::string &Name,
|
2004-07-29 12:33:25 +00:00
|
|
|
BasicBlock *InsertAtEnd)
|
2007-12-17 01:12:55 +00:00
|
|
|
: UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
|
2008-03-24 16:55:58 +00:00
|
|
|
InsertAtEnd) {
|
|
|
|
setAlignment(Align);
|
2005-01-29 00:35:16 +00:00
|
|
|
assert(Ty != Type::VoidTy && "Cannot allocate void!");
|
2007-02-13 07:54:42 +00:00
|
|
|
setName(Name);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2007-12-10 02:14:30 +00:00
|
|
|
// Out of line virtual method, so the vtable, etc has a home.
|
|
|
|
AllocationInst::~AllocationInst() {
|
|
|
|
}
|
|
|
|
|
2008-03-24 16:55:58 +00:00
|
|
|
void AllocationInst::setAlignment(unsigned Align) {
|
|
|
|
assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
|
|
|
|
SubclassData = Log2_32(Align) + 1;
|
|
|
|
assert(getAlignment() == Align && "Alignment representation error!");
|
|
|
|
}
|
|
|
|
|
2004-07-29 12:33:25 +00:00
|
|
|
bool AllocationInst::isArrayAllocation() const {
|
2007-03-01 20:27:41 +00:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
|
|
|
|
return CI->getZExtValue() != 1;
|
2005-01-29 00:35:16 +00:00
|
|
|
return true;
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Type *AllocationInst::getAllocatedType() const {
|
|
|
|
return getType()->getElementType();
|
|
|
|
}
|
|
|
|
|
|
|
|
AllocaInst::AllocaInst(const AllocaInst &AI)
|
|
|
|
: AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
|
2005-11-05 09:21:28 +00:00
|
|
|
Instruction::Alloca, AI.getAlignment()) {
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MallocInst::MallocInst(const MallocInst &MI)
|
|
|
|
: AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
|
2005-11-05 09:21:28 +00:00
|
|
|
Instruction::Malloc, MI.getAlignment()) {
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FreeInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
void FreeInst::AssertOK() {
|
|
|
|
assert(isa<PointerType>(getOperand(0)->getType()) &&
|
|
|
|
"Can not free something of nonpointer type!");
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
|
2007-02-24 00:55:48 +00:00
|
|
|
: UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
|
2005-01-29 00:35:16 +00:00
|
|
|
AssertOK();
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
|
2007-02-24 00:55:48 +00:00
|
|
|
: UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
|
2005-01-29 00:35:16 +00:00
|
|
|
AssertOK();
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LoadInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
void LoadInst::AssertOK() {
|
2005-04-21 23:48:37 +00:00
|
|
|
assert(isa<PointerType>(getOperand(0)->getType()) &&
|
2004-07-29 12:33:25 +00:00
|
|
|
"Ptr must have pointer type.");
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
|
2005-01-29 00:35:16 +00:00
|
|
|
: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
|
2007-02-24 00:55:48 +00:00
|
|
|
Load, Ptr, InsertBef) {
|
2005-02-05 01:38:38 +00:00
|
|
|
setVolatile(false);
|
2007-04-22 19:24:39 +00:00
|
|
|
setAlignment(0);
|
2005-01-29 00:35:16 +00:00
|
|
|
AssertOK();
|
2007-02-13 07:54:42 +00:00
|
|
|
setName(Name);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
|
2005-01-29 00:35:16 +00:00
|
|
|
: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
|
2007-02-24 00:55:48 +00:00
|
|
|
Load, Ptr, InsertAE) {
|
2005-02-05 01:38:38 +00:00
|
|
|
setVolatile(false);
|
2007-04-22 19:24:39 +00:00
|
|
|
setAlignment(0);
|
2005-01-29 00:35:16 +00:00
|
|
|
AssertOK();
|
2007-02-13 07:54:42 +00:00
|
|
|
setName(Name);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
|
|
|
|
Instruction *InsertBef)
|
2005-01-29 00:35:16 +00:00
|
|
|
: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
|
2007-02-24 00:55:48 +00:00
|
|
|
Load, Ptr, InsertBef) {
|
2005-02-05 01:38:38 +00:00
|
|
|
setVolatile(isVolatile);
|
2007-04-22 19:24:39 +00:00
|
|
|
setAlignment(0);
|
|
|
|
AssertOK();
|
|
|
|
setName(Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
|
|
|
|
unsigned Align, Instruction *InsertBef)
|
|
|
|
: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
|
|
|
|
Load, Ptr, InsertBef) {
|
|
|
|
setVolatile(isVolatile);
|
|
|
|
setAlignment(Align);
|
2005-01-29 00:35:16 +00:00
|
|
|
AssertOK();
|
2007-02-13 07:54:42 +00:00
|
|
|
setName(Name);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2007-07-18 20:51:11 +00:00
|
|
|
LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
|
|
|
|
unsigned Align, BasicBlock *InsertAE)
|
|
|
|
: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
|
|
|
|
Load, Ptr, InsertAE) {
|
|
|
|
setVolatile(isVolatile);
|
|
|
|
setAlignment(Align);
|
|
|
|
AssertOK();
|
|
|
|
setName(Name);
|
|
|
|
}
|
|
|
|
|
2004-07-29 12:33:25 +00:00
|
|
|
LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
|
|
|
|
BasicBlock *InsertAE)
|
2005-01-29 00:35:16 +00:00
|
|
|
: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
|
2007-02-24 00:55:48 +00:00
|
|
|
Load, Ptr, InsertAE) {
|
2007-02-13 07:54:42 +00:00
|
|
|
setVolatile(isVolatile);
|
2007-04-22 19:24:39 +00:00
|
|
|
setAlignment(0);
|
2007-02-13 07:54:42 +00:00
|
|
|
AssertOK();
|
|
|
|
setName(Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
|
2007-02-24 00:55:48 +00:00
|
|
|
: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
|
|
|
|
Load, Ptr, InsertBef) {
|
2007-02-13 07:54:42 +00:00
|
|
|
setVolatile(false);
|
2007-04-22 19:24:39 +00:00
|
|
|
setAlignment(0);
|
2007-02-13 07:54:42 +00:00
|
|
|
AssertOK();
|
2007-02-24 00:55:48 +00:00
|
|
|
if (Name && Name[0]) setName(Name);
|
2007-02-13 07:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
|
2007-02-24 00:55:48 +00:00
|
|
|
: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
|
|
|
|
Load, Ptr, InsertAE) {
|
2007-02-13 07:54:42 +00:00
|
|
|
setVolatile(false);
|
2007-04-22 19:24:39 +00:00
|
|
|
setAlignment(0);
|
2007-02-13 07:54:42 +00:00
|
|
|
AssertOK();
|
2007-02-24 00:55:48 +00:00
|
|
|
if (Name && Name[0]) setName(Name);
|
2007-02-13 07:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
|
|
|
|
Instruction *InsertBef)
|
|
|
|
: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
|
2007-02-24 00:55:48 +00:00
|
|
|
Load, Ptr, InsertBef) {
|
2007-02-13 07:54:42 +00:00
|
|
|
setVolatile(isVolatile);
|
2007-04-22 19:24:39 +00:00
|
|
|
setAlignment(0);
|
2007-02-13 07:54:42 +00:00
|
|
|
AssertOK();
|
2007-02-24 00:55:48 +00:00
|
|
|
if (Name && Name[0]) setName(Name);
|
2007-02-13 07:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
|
|
|
|
BasicBlock *InsertAE)
|
2007-02-24 00:55:48 +00:00
|
|
|
: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
|
|
|
|
Load, Ptr, InsertAE) {
|
2005-02-05 01:38:38 +00:00
|
|
|
setVolatile(isVolatile);
|
2007-04-22 19:24:39 +00:00
|
|
|
setAlignment(0);
|
2005-01-29 00:35:16 +00:00
|
|
|
AssertOK();
|
2007-02-24 00:55:48 +00:00
|
|
|
if (Name && Name[0]) setName(Name);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2007-04-22 19:24:39 +00:00
|
|
|
void LoadInst::setAlignment(unsigned Align) {
|
|
|
|
assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
|
|
|
|
SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
|
|
|
|
}
|
2004-07-29 12:33:25 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// StoreInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
void StoreInst::AssertOK() {
|
|
|
|
assert(isa<PointerType>(getOperand(1)->getType()) &&
|
|
|
|
"Ptr must have pointer type!");
|
|
|
|
assert(getOperand(0)->getType() ==
|
|
|
|
cast<PointerType>(getOperand(1)->getType())->getElementType()
|
|
|
|
&& "Ptr must be a pointer to Val type!");
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(Type::VoidTy, Store,
|
|
|
|
OperandTraits<StoreInst>::op_begin(this),
|
|
|
|
OperandTraits<StoreInst>::operands(this),
|
|
|
|
InsertBefore) {
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = val;
|
|
|
|
Op<1>() = addr;
|
2005-02-05 01:38:38 +00:00
|
|
|
setVolatile(false);
|
2007-04-22 19:24:39 +00:00
|
|
|
setAlignment(0);
|
2005-01-29 00:35:16 +00:00
|
|
|
AssertOK();
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(Type::VoidTy, Store,
|
|
|
|
OperandTraits<StoreInst>::op_begin(this),
|
|
|
|
OperandTraits<StoreInst>::operands(this),
|
|
|
|
InsertAtEnd) {
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = val;
|
|
|
|
Op<1>() = addr;
|
2005-02-05 01:38:38 +00:00
|
|
|
setVolatile(false);
|
2007-04-22 19:24:39 +00:00
|
|
|
setAlignment(0);
|
2005-01-29 00:35:16 +00:00
|
|
|
AssertOK();
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
|
2005-01-29 00:35:16 +00:00
|
|
|
Instruction *InsertBefore)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(Type::VoidTy, Store,
|
|
|
|
OperandTraits<StoreInst>::op_begin(this),
|
|
|
|
OperandTraits<StoreInst>::operands(this),
|
|
|
|
InsertBefore) {
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = val;
|
|
|
|
Op<1>() = addr;
|
2005-02-05 01:38:38 +00:00
|
|
|
setVolatile(isVolatile);
|
2007-04-22 19:24:39 +00:00
|
|
|
setAlignment(0);
|
|
|
|
AssertOK();
|
|
|
|
}
|
|
|
|
|
|
|
|
StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
|
|
|
|
unsigned Align, Instruction *InsertBefore)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(Type::VoidTy, Store,
|
|
|
|
OperandTraits<StoreInst>::op_begin(this),
|
|
|
|
OperandTraits<StoreInst>::operands(this),
|
|
|
|
InsertBefore) {
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = val;
|
|
|
|
Op<1>() = addr;
|
2007-04-22 19:24:39 +00:00
|
|
|
setVolatile(isVolatile);
|
|
|
|
setAlignment(Align);
|
2005-01-29 00:35:16 +00:00
|
|
|
AssertOK();
|
|
|
|
}
|
2004-07-29 12:33:25 +00:00
|
|
|
|
2007-07-18 20:51:11 +00:00
|
|
|
StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
|
|
|
|
unsigned Align, BasicBlock *InsertAtEnd)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(Type::VoidTy, Store,
|
|
|
|
OperandTraits<StoreInst>::op_begin(this),
|
|
|
|
OperandTraits<StoreInst>::operands(this),
|
|
|
|
InsertAtEnd) {
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = val;
|
|
|
|
Op<1>() = addr;
|
2007-07-18 20:51:11 +00:00
|
|
|
setVolatile(isVolatile);
|
|
|
|
setAlignment(Align);
|
|
|
|
AssertOK();
|
|
|
|
}
|
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
|
2005-01-29 00:35:16 +00:00
|
|
|
BasicBlock *InsertAtEnd)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(Type::VoidTy, Store,
|
|
|
|
OperandTraits<StoreInst>::op_begin(this),
|
|
|
|
OperandTraits<StoreInst>::operands(this),
|
|
|
|
InsertAtEnd) {
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = val;
|
|
|
|
Op<1>() = addr;
|
2005-02-05 01:38:38 +00:00
|
|
|
setVolatile(isVolatile);
|
2007-04-22 19:24:39 +00:00
|
|
|
setAlignment(0);
|
2005-01-29 00:35:16 +00:00
|
|
|
AssertOK();
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2007-04-22 19:24:39 +00:00
|
|
|
void StoreInst::setAlignment(unsigned Align) {
|
|
|
|
assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
|
|
|
|
SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
|
|
|
|
}
|
|
|
|
|
2004-07-29 12:33:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// GetElementPtrInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-12-17 01:12:55 +00:00
|
|
|
static unsigned retrieveAddrSpace(const Value *Val) {
|
|
|
|
return cast<PointerType>(Val->getType())->getAddressSpace();
|
|
|
|
}
|
|
|
|
|
2007-01-31 19:47:18 +00:00
|
|
|
void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
|
2008-05-10 08:32:32 +00:00
|
|
|
assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
|
|
|
|
Use *OL = OperandList;
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[0] = Ptr;
|
2004-07-29 12:33:25 +00:00
|
|
|
|
2007-01-31 19:47:18 +00:00
|
|
|
for (unsigned i = 0; i != NumIdx; ++i)
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[i+1] = Idx[i];
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2005-05-03 05:43:30 +00:00
|
|
|
void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
|
2008-05-10 08:32:32 +00:00
|
|
|
assert(NumOperands == 2 && "NumOperands not initialized?");
|
|
|
|
Use *OL = OperandList;
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[0] = Ptr;
|
|
|
|
OL[1] = Idx;
|
2005-05-03 05:43:30 +00:00
|
|
|
}
|
|
|
|
|
2008-05-10 08:32:32 +00:00
|
|
|
GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
|
2008-05-27 11:03:29 +00:00
|
|
|
: Instruction(GEPI.getType(), GetElementPtr,
|
2008-05-15 10:04:30 +00:00
|
|
|
OperandTraits<GetElementPtrInst>::op_end(this)
|
|
|
|
- GEPI.getNumOperands(),
|
2008-05-10 08:32:32 +00:00
|
|
|
GEPI.getNumOperands()) {
|
|
|
|
Use *OL = OperandList;
|
|
|
|
Use *GEPIOL = GEPI.OperandList;
|
|
|
|
for (unsigned i = 0, E = NumOperands; i != E; ++i)
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[i] = GEPIOL[i];
|
2008-05-10 08:32:32 +00:00
|
|
|
}
|
|
|
|
|
2005-05-03 05:43:30 +00:00
|
|
|
GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
|
|
|
|
const std::string &Name, Instruction *InBe)
|
2007-12-11 08:59:05 +00:00
|
|
|
: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
|
2007-12-17 01:12:55 +00:00
|
|
|
retrieveAddrSpace(Ptr)),
|
2008-05-10 08:32:32 +00:00
|
|
|
GetElementPtr,
|
|
|
|
OperandTraits<GetElementPtrInst>::op_end(this) - 2,
|
|
|
|
2, InBe) {
|
2005-05-03 05:43:30 +00:00
|
|
|
init(Ptr, Idx);
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2005-05-03 05:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
|
|
|
|
const std::string &Name, BasicBlock *IAE)
|
2007-12-11 08:59:05 +00:00
|
|
|
: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
|
2007-12-17 01:12:55 +00:00
|
|
|
retrieveAddrSpace(Ptr)),
|
2008-05-10 08:32:32 +00:00
|
|
|
GetElementPtr,
|
|
|
|
OperandTraits<GetElementPtrInst>::op_end(this) - 2,
|
|
|
|
2, IAE) {
|
2005-05-03 05:43:30 +00:00
|
|
|
init(Ptr, Idx);
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2005-05-03 05:43:30 +00:00
|
|
|
}
|
|
|
|
|
2004-07-29 12:33:25 +00:00
|
|
|
// getIndexedType - Returns the type of the element that would be loaded with
|
|
|
|
// a load instruction with the specified parameters.
|
|
|
|
//
|
2005-04-21 23:48:37 +00:00
|
|
|
// A null type is returned if the indices are invalid for the specified
|
2004-07-29 12:33:25 +00:00
|
|
|
// pointer type.
|
|
|
|
//
|
2005-04-21 23:48:37 +00:00
|
|
|
const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
|
2007-01-31 04:40:28 +00:00
|
|
|
Value* const *Idxs,
|
2008-05-15 19:50:34 +00:00
|
|
|
unsigned NumIdx) {
|
|
|
|
const PointerType *PTy = dyn_cast<PointerType>(Ptr);
|
|
|
|
if (!PTy) return 0; // Type isn't a pointer type!
|
|
|
|
const Type *Agg = PTy->getElementType();
|
2004-07-29 12:33:25 +00:00
|
|
|
|
|
|
|
// Handle the special case of the empty set index set...
|
2008-05-15 19:50:34 +00:00
|
|
|
if (NumIdx == 0)
|
|
|
|
return Agg;
|
2004-07-29 12:33:25 +00:00
|
|
|
|
2008-05-15 19:50:34 +00:00
|
|
|
return ExtractValueInst::getIndexedType(Agg, Idxs+1, Idxs+NumIdx);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2005-05-03 05:43:30 +00:00
|
|
|
const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
|
|
|
|
const PointerType *PTy = dyn_cast<PointerType>(Ptr);
|
|
|
|
if (!PTy) return 0; // Type isn't a pointer type!
|
|
|
|
|
|
|
|
// Check the pointer index.
|
|
|
|
if (!PTy->indexValid(Idx)) return 0;
|
|
|
|
|
2005-05-03 16:44:45 +00:00
|
|
|
return PTy->getElementType();
|
2005-05-03 05:43:30 +00:00
|
|
|
}
|
|
|
|
|
2007-04-14 00:12:57 +00:00
|
|
|
|
|
|
|
/// hasAllZeroIndices - Return true if all of the indices of this GEP are
|
|
|
|
/// zeros. If so, the result pointer and the first operand have the same
|
|
|
|
/// value, just potentially different types.
|
|
|
|
bool GetElementPtrInst::hasAllZeroIndices() const {
|
|
|
|
for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
|
|
|
|
if (!CI->isZero()) return false;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-04-27 20:35:56 +00:00
|
|
|
/// hasAllConstantIndices - Return true if all of the indices of this GEP are
|
|
|
|
/// constant integers. If so, the result pointer and the first operand have
|
|
|
|
/// a constant offset between them.
|
|
|
|
bool GetElementPtrInst::hasAllConstantIndices() const {
|
|
|
|
for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
|
|
|
|
if (!isa<ConstantInt>(getOperand(i)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-04-14 00:12:57 +00:00
|
|
|
|
2006-01-10 19:05:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ExtractElementInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
|
2006-04-08 01:18:18 +00:00
|
|
|
const std::string &Name,
|
|
|
|
Instruction *InsertBef)
|
2007-02-15 02:26:10 +00:00
|
|
|
: Instruction(cast<VectorType>(Val->getType())->getElementType(),
|
2008-05-10 08:32:32 +00:00
|
|
|
ExtractElement,
|
|
|
|
OperandTraits<ExtractElementInst>::op_begin(this),
|
|
|
|
2, InsertBef) {
|
2006-04-08 04:05:48 +00:00
|
|
|
assert(isValidOperands(Val, Index) &&
|
|
|
|
"Invalid extractelement instruction operands!");
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = Val;
|
|
|
|
Op<1>() = Index;
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2006-01-10 19:05:34 +00:00
|
|
|
}
|
|
|
|
|
2006-10-05 06:24:58 +00:00
|
|
|
ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
|
|
|
|
const std::string &Name,
|
|
|
|
Instruction *InsertBef)
|
2007-02-15 02:26:10 +00:00
|
|
|
: Instruction(cast<VectorType>(Val->getType())->getElementType(),
|
2008-05-10 08:32:32 +00:00
|
|
|
ExtractElement,
|
|
|
|
OperandTraits<ExtractElementInst>::op_begin(this),
|
|
|
|
2, InsertBef) {
|
2006-12-31 05:26:44 +00:00
|
|
|
Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
|
2006-10-05 06:24:58 +00:00
|
|
|
assert(isValidOperands(Val, Index) &&
|
|
|
|
"Invalid extractelement instruction operands!");
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = Val;
|
|
|
|
Op<1>() = Index;
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2006-10-05 06:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-10 19:05:34 +00:00
|
|
|
ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
|
2006-04-08 01:18:18 +00:00
|
|
|
const std::string &Name,
|
|
|
|
BasicBlock *InsertAE)
|
2007-02-15 02:26:10 +00:00
|
|
|
: Instruction(cast<VectorType>(Val->getType())->getElementType(),
|
2008-05-10 08:32:32 +00:00
|
|
|
ExtractElement,
|
|
|
|
OperandTraits<ExtractElementInst>::op_begin(this),
|
|
|
|
2, InsertAE) {
|
2006-04-08 04:05:48 +00:00
|
|
|
assert(isValidOperands(Val, Index) &&
|
|
|
|
"Invalid extractelement instruction operands!");
|
|
|
|
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = Val;
|
|
|
|
Op<1>() = Index;
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2006-01-10 19:05:34 +00:00
|
|
|
}
|
|
|
|
|
2006-10-05 06:24:58 +00:00
|
|
|
ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
|
|
|
|
const std::string &Name,
|
|
|
|
BasicBlock *InsertAE)
|
2007-02-15 02:26:10 +00:00
|
|
|
: Instruction(cast<VectorType>(Val->getType())->getElementType(),
|
2008-05-10 08:32:32 +00:00
|
|
|
ExtractElement,
|
|
|
|
OperandTraits<ExtractElementInst>::op_begin(this),
|
|
|
|
2, InsertAE) {
|
2006-12-31 05:26:44 +00:00
|
|
|
Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
|
2006-10-05 06:24:58 +00:00
|
|
|
assert(isValidOperands(Val, Index) &&
|
|
|
|
"Invalid extractelement instruction operands!");
|
|
|
|
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = Val;
|
|
|
|
Op<1>() = Index;
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2006-10-05 06:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-08 04:05:48 +00:00
|
|
|
bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
|
2007-02-15 02:26:10 +00:00
|
|
|
if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
|
2006-04-08 04:05:48 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-17 20:07:22 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// InsertElementInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-04-14 22:20:32 +00:00
|
|
|
InsertElementInst::InsertElementInst(const InsertElementInst &IE)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(IE.getType(), InsertElement,
|
|
|
|
OperandTraits<InsertElementInst>::op_begin(this), 3) {
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = IE.Op<0>();
|
|
|
|
Op<1>() = IE.Op<1>();
|
|
|
|
Op<2>() = IE.Op<2>();
|
2006-04-14 22:20:32 +00:00
|
|
|
}
|
2006-04-08 04:05:48 +00:00
|
|
|
InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
|
2006-04-08 01:18:18 +00:00
|
|
|
const std::string &Name,
|
|
|
|
Instruction *InsertBef)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(Vec->getType(), InsertElement,
|
|
|
|
OperandTraits<InsertElementInst>::op_begin(this),
|
|
|
|
3, InsertBef) {
|
2006-04-08 04:05:48 +00:00
|
|
|
assert(isValidOperands(Vec, Elt, Index) &&
|
|
|
|
"Invalid insertelement instruction operands!");
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = Vec;
|
|
|
|
Op<1>() = Elt;
|
|
|
|
Op<2>() = Index;
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2006-01-17 20:07:22 +00:00
|
|
|
}
|
|
|
|
|
2006-10-05 06:24:58 +00:00
|
|
|
InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
|
|
|
|
const std::string &Name,
|
|
|
|
Instruction *InsertBef)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(Vec->getType(), InsertElement,
|
|
|
|
OperandTraits<InsertElementInst>::op_begin(this),
|
|
|
|
3, InsertBef) {
|
2006-12-31 05:26:44 +00:00
|
|
|
Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
|
2006-10-05 06:24:58 +00:00
|
|
|
assert(isValidOperands(Vec, Elt, Index) &&
|
|
|
|
"Invalid insertelement instruction operands!");
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = Vec;
|
|
|
|
Op<1>() = Elt;
|
|
|
|
Op<2>() = Index;
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2006-10-05 06:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-08 04:05:48 +00:00
|
|
|
InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
|
2006-04-08 01:18:18 +00:00
|
|
|
const std::string &Name,
|
|
|
|
BasicBlock *InsertAE)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(Vec->getType(), InsertElement,
|
|
|
|
OperandTraits<InsertElementInst>::op_begin(this),
|
|
|
|
3, InsertAE) {
|
2006-04-08 04:05:48 +00:00
|
|
|
assert(isValidOperands(Vec, Elt, Index) &&
|
|
|
|
"Invalid insertelement instruction operands!");
|
|
|
|
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = Vec;
|
|
|
|
Op<1>() = Elt;
|
|
|
|
Op<2>() = Index;
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2006-01-17 20:07:22 +00:00
|
|
|
}
|
|
|
|
|
2006-10-05 06:24:58 +00:00
|
|
|
InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
|
|
|
|
const std::string &Name,
|
|
|
|
BasicBlock *InsertAE)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(Vec->getType(), InsertElement,
|
|
|
|
OperandTraits<InsertElementInst>::op_begin(this),
|
|
|
|
3, InsertAE) {
|
2006-12-31 05:26:44 +00:00
|
|
|
Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
|
2006-10-05 06:24:58 +00:00
|
|
|
assert(isValidOperands(Vec, Elt, Index) &&
|
|
|
|
"Invalid insertelement instruction operands!");
|
|
|
|
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = Vec;
|
|
|
|
Op<1>() = Elt;
|
|
|
|
Op<2>() = Index;
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2006-10-05 06:24:58 +00:00
|
|
|
}
|
|
|
|
|
2006-04-08 04:05:48 +00:00
|
|
|
bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
|
|
|
|
const Value *Index) {
|
2007-02-15 02:26:10 +00:00
|
|
|
if (!isa<VectorType>(Vec->getType()))
|
2007-02-15 03:39:18 +00:00
|
|
|
return false; // First operand of insertelement must be vector type.
|
2006-04-08 04:05:48 +00:00
|
|
|
|
2007-02-15 02:26:10 +00:00
|
|
|
if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
|
2007-05-11 21:43:24 +00:00
|
|
|
return false;// Second operand of insertelement must be vector element type.
|
2006-04-08 04:05:48 +00:00
|
|
|
|
2006-12-31 05:26:44 +00:00
|
|
|
if (Index->getType() != Type::Int32Ty)
|
2006-04-08 04:05:48 +00:00
|
|
|
return false; // Third operand of insertelement must be uint.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-08 01:18:18 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ShuffleVectorInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-04-14 22:20:32 +00:00
|
|
|
ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(SV.getType(), ShuffleVector,
|
|
|
|
OperandTraits<ShuffleVectorInst>::op_begin(this),
|
|
|
|
OperandTraits<ShuffleVectorInst>::operands(this)) {
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = SV.Op<0>();
|
|
|
|
Op<1>() = SV.Op<1>();
|
|
|
|
Op<2>() = SV.Op<2>();
|
2006-04-14 22:20:32 +00:00
|
|
|
}
|
|
|
|
|
2006-04-08 01:18:18 +00:00
|
|
|
ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
|
|
|
|
const std::string &Name,
|
|
|
|
Instruction *InsertBefore)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(V1->getType(), ShuffleVector,
|
|
|
|
OperandTraits<ShuffleVectorInst>::op_begin(this),
|
|
|
|
OperandTraits<ShuffleVectorInst>::operands(this),
|
|
|
|
InsertBefore) {
|
2006-04-08 01:18:18 +00:00
|
|
|
assert(isValidOperands(V1, V2, Mask) &&
|
|
|
|
"Invalid shuffle vector instruction operands!");
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = V1;
|
|
|
|
Op<1>() = V2;
|
|
|
|
Op<2>() = Mask;
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2006-04-08 01:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
|
|
|
|
const std::string &Name,
|
|
|
|
BasicBlock *InsertAtEnd)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(V1->getType(), ShuffleVector,
|
|
|
|
OperandTraits<ShuffleVectorInst>::op_begin(this),
|
|
|
|
OperandTraits<ShuffleVectorInst>::operands(this),
|
|
|
|
InsertAtEnd) {
|
2006-04-08 01:18:18 +00:00
|
|
|
assert(isValidOperands(V1, V2, Mask) &&
|
|
|
|
"Invalid shuffle vector instruction operands!");
|
|
|
|
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = V1;
|
|
|
|
Op<1>() = V2;
|
|
|
|
Op<2>() = Mask;
|
2007-02-24 00:55:48 +00:00
|
|
|
setName(Name);
|
2006-04-08 01:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
|
|
|
|
const Value *Mask) {
|
2008-03-02 05:28:33 +00:00
|
|
|
if (!isa<VectorType>(V1->getType()) ||
|
|
|
|
V1->getType() != V2->getType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
|
|
|
|
if (!isa<Constant>(Mask) || MaskTy == 0 ||
|
|
|
|
MaskTy->getElementType() != Type::Int32Ty ||
|
|
|
|
MaskTy->getNumElements() !=
|
|
|
|
cast<VectorType>(V1->getType())->getNumElements())
|
2006-04-08 01:18:18 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-03-02 05:28:33 +00:00
|
|
|
/// getMaskValue - Return the index from the shuffle mask for the specified
|
|
|
|
/// output result. This is either -1 if the element is undef or a number less
|
|
|
|
/// than 2*numelements.
|
|
|
|
int ShuffleVectorInst::getMaskValue(unsigned i) const {
|
|
|
|
const Constant *Mask = cast<Constant>(getOperand(2));
|
|
|
|
if (isa<UndefValue>(Mask)) return -1;
|
|
|
|
if (isa<ConstantAggregateZero>(Mask)) return 0;
|
|
|
|
const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
|
|
|
|
assert(i < MaskCV->getNumOperands() && "Index out of range");
|
|
|
|
|
|
|
|
if (isa<UndefValue>(MaskCV->getOperand(i)))
|
|
|
|
return -1;
|
|
|
|
return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
|
|
|
|
}
|
|
|
|
|
2008-05-23 00:36:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// InsertValueInst Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void InsertValueInst::init(Value *Agg, Value *Val, Value* const *Idx, unsigned NumIdx) {
|
|
|
|
assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
|
|
|
|
Use *OL = OperandList;
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[0] = Agg;
|
|
|
|
OL[1] = Val;
|
2008-05-23 00:36:11 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i != NumIdx; ++i)
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[i+2] = Idx[i];
|
2008-05-23 00:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InsertValueInst::init(Value *Agg, Value *Val, Value *Idx) {
|
|
|
|
assert(NumOperands == 3 && "NumOperands not initialized?");
|
|
|
|
Use *OL = OperandList;
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[0] = Agg;
|
|
|
|
OL[1] = Val;
|
|
|
|
OL[2] = Idx;
|
2008-05-23 00:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
|
2008-05-27 11:03:29 +00:00
|
|
|
: Instruction(IVI.getType(), InsertValue,
|
2008-05-23 00:36:11 +00:00
|
|
|
OperandTraits<InsertValueInst>::op_end(this)
|
|
|
|
- IVI.getNumOperands(),
|
|
|
|
IVI.getNumOperands()) {
|
|
|
|
Use *OL = OperandList;
|
|
|
|
Use *IVIOL = IVI.OperandList;
|
|
|
|
for (unsigned i = 0, E = NumOperands; i != E; ++i)
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[i] = IVIOL[i];
|
2008-05-23 00:36:11 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 19:50:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ExtractValueInst Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-23 00:36:11 +00:00
|
|
|
void ExtractValueInst::init(Value *Agg, Value* const *Idx, unsigned NumIdx) {
|
|
|
|
assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
|
|
|
|
Use *OL = OperandList;
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[0] = Agg;
|
2008-05-23 00:36:11 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i != NumIdx; ++i)
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[i+1] = Idx[i];
|
2008-05-23 00:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExtractValueInst::init(Value *Agg, Value *Idx) {
|
|
|
|
assert(NumOperands == 2 && "NumOperands not initialized?");
|
|
|
|
Use *OL = OperandList;
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[0] = Agg;
|
|
|
|
OL[1] = Idx;
|
2008-05-23 00:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
|
|
|
|
: Instruction(reinterpret_cast<const Type*>(EVI.getType()), ExtractValue,
|
|
|
|
OperandTraits<ExtractValueInst>::op_end(this)
|
|
|
|
- EVI.getNumOperands(),
|
|
|
|
EVI.getNumOperands()) {
|
|
|
|
Use *OL = OperandList;
|
|
|
|
Use *EVIOL = EVI.OperandList;
|
|
|
|
for (unsigned i = 0, E = NumOperands; i != E; ++i)
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[i] = EVIOL[i];
|
2008-05-23 00:36:11 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 19:50:34 +00:00
|
|
|
// getIndexedType - Returns the type of the element that would be extracted
|
|
|
|
// with an extractvalue instruction with the specified parameters.
|
|
|
|
//
|
|
|
|
// A null type is returned if the indices are invalid for the specified
|
|
|
|
// pointer type.
|
|
|
|
//
|
|
|
|
const Type* ExtractValueInst::getIndexedType(const Type *Agg,
|
|
|
|
Value* const *Idxs,
|
|
|
|
unsigned NumIdx) {
|
|
|
|
unsigned CurIdx = 0;
|
|
|
|
for (; CurIdx != NumIdx; ++CurIdx) {
|
|
|
|
const CompositeType *CT = dyn_cast<CompositeType>(Agg);
|
2008-05-16 00:16:32 +00:00
|
|
|
if (!CT || isa<PointerType>(CT)) return 0;
|
2008-05-15 19:50:34 +00:00
|
|
|
Value *Index = Idxs[CurIdx];
|
|
|
|
if (!CT->indexValid(Index)) return 0;
|
|
|
|
Agg = CT->getTypeAtIndex(Index);
|
|
|
|
|
|
|
|
// If the new type forwards to another type, then it is in the middle
|
|
|
|
// of being refined to another type (and hence, may have dropped all
|
|
|
|
// references to what it was using before). So, use the new forwarded
|
|
|
|
// type.
|
|
|
|
if (const Type *Ty = Agg->getForwardedType())
|
|
|
|
Agg = Ty;
|
|
|
|
}
|
|
|
|
return CurIdx == NumIdx ? Agg : 0;
|
|
|
|
}
|
2006-04-08 01:18:18 +00:00
|
|
|
|
2004-07-29 12:33:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BinaryOperator Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-02-24 00:55:48 +00:00
|
|
|
BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
|
|
|
|
const Type *Ty, const std::string &Name,
|
|
|
|
Instruction *InsertBefore)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(Ty, iType,
|
|
|
|
OperandTraits<BinaryOperator>::op_begin(this),
|
|
|
|
OperandTraits<BinaryOperator>::operands(this),
|
|
|
|
InsertBefore) {
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = S1;
|
|
|
|
Op<1>() = S2;
|
2007-02-24 00:55:48 +00:00
|
|
|
init(iType);
|
|
|
|
setName(Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
|
|
|
|
const Type *Ty, const std::string &Name,
|
|
|
|
BasicBlock *InsertAtEnd)
|
2008-05-10 08:32:32 +00:00
|
|
|
: Instruction(Ty, iType,
|
|
|
|
OperandTraits<BinaryOperator>::op_begin(this),
|
|
|
|
OperandTraits<BinaryOperator>::operands(this),
|
|
|
|
InsertAtEnd) {
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = S1;
|
|
|
|
Op<1>() = S2;
|
2007-02-24 00:55:48 +00:00
|
|
|
init(iType);
|
|
|
|
setName(Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BinaryOperator::init(BinaryOps iType) {
|
2005-01-29 00:35:16 +00:00
|
|
|
Value *LHS = getOperand(0), *RHS = getOperand(1);
|
2007-02-01 04:59:37 +00:00
|
|
|
LHS = LHS; RHS = RHS; // Silence warnings.
|
2005-01-29 00:35:16 +00:00
|
|
|
assert(LHS->getType() == RHS->getType() &&
|
|
|
|
"Binary operator operand types must match!");
|
2004-07-29 12:33:25 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
switch (iType) {
|
|
|
|
case Add: case Sub:
|
2006-10-26 06:15:43 +00:00
|
|
|
case Mul:
|
2005-01-29 00:35:16 +00:00
|
|
|
assert(getType() == LHS->getType() &&
|
2004-07-29 12:33:25 +00:00
|
|
|
"Arithmetic operation should return same type as operands!");
|
2007-01-15 02:27:26 +00:00
|
|
|
assert((getType()->isInteger() || getType()->isFloatingPoint() ||
|
2007-02-15 02:26:10 +00:00
|
|
|
isa<VectorType>(getType())) &&
|
2004-08-20 06:00:58 +00:00
|
|
|
"Tried to create an arithmetic operation on a non-arithmetic type!");
|
2004-07-29 12:33:25 +00:00
|
|
|
break;
|
2006-10-26 06:15:43 +00:00
|
|
|
case UDiv:
|
|
|
|
case SDiv:
|
|
|
|
assert(getType() == LHS->getType() &&
|
|
|
|
"Arithmetic operation should return same type as operands!");
|
2007-02-15 02:26:10 +00:00
|
|
|
assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
|
|
|
|
cast<VectorType>(getType())->getElementType()->isInteger())) &&
|
2006-10-26 06:15:43 +00:00
|
|
|
"Incorrect operand type (not integer) for S/UDIV");
|
|
|
|
break;
|
|
|
|
case FDiv:
|
|
|
|
assert(getType() == LHS->getType() &&
|
|
|
|
"Arithmetic operation should return same type as operands!");
|
2007-02-15 02:26:10 +00:00
|
|
|
assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
|
|
|
|
cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
|
2006-10-26 06:15:43 +00:00
|
|
|
&& "Incorrect operand type (not floating point) for FDIV");
|
|
|
|
break;
|
2006-11-02 01:53:59 +00:00
|
|
|
case URem:
|
|
|
|
case SRem:
|
|
|
|
assert(getType() == LHS->getType() &&
|
|
|
|
"Arithmetic operation should return same type as operands!");
|
2007-02-15 02:26:10 +00:00
|
|
|
assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
|
|
|
|
cast<VectorType>(getType())->getElementType()->isInteger())) &&
|
2006-11-02 01:53:59 +00:00
|
|
|
"Incorrect operand type (not integer) for S/UREM");
|
|
|
|
break;
|
|
|
|
case FRem:
|
|
|
|
assert(getType() == LHS->getType() &&
|
|
|
|
"Arithmetic operation should return same type as operands!");
|
2007-02-15 02:26:10 +00:00
|
|
|
assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
|
|
|
|
cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
|
2006-11-02 01:53:59 +00:00
|
|
|
&& "Incorrect operand type (not floating point) for FREM");
|
|
|
|
break;
|
2007-02-02 02:16:23 +00:00
|
|
|
case Shl:
|
|
|
|
case LShr:
|
|
|
|
case AShr:
|
|
|
|
assert(getType() == LHS->getType() &&
|
|
|
|
"Shift operation should return same type as operands!");
|
|
|
|
assert(getType()->isInteger() &&
|
|
|
|
"Shift operation requires integer operands");
|
|
|
|
break;
|
2004-07-29 12:33:25 +00:00
|
|
|
case And: case Or:
|
|
|
|
case Xor:
|
2005-01-29 00:35:16 +00:00
|
|
|
assert(getType() == LHS->getType() &&
|
2004-07-29 12:33:25 +00:00
|
|
|
"Logical operation should return same type as operands!");
|
2007-01-15 02:27:26 +00:00
|
|
|
assert((getType()->isInteger() ||
|
2007-02-15 02:26:10 +00:00
|
|
|
(isa<VectorType>(getType()) &&
|
|
|
|
cast<VectorType>(getType())->getElementType()->isInteger())) &&
|
2005-01-27 06:46:38 +00:00
|
|
|
"Tried to create a logical operation on a non-integral type!");
|
2004-07-29 12:33:25 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
|
2005-03-16 05:42:00 +00:00
|
|
|
const std::string &Name,
|
2004-07-29 12:33:25 +00:00
|
|
|
Instruction *InsertBefore) {
|
|
|
|
assert(S1->getType() == S2->getType() &&
|
|
|
|
"Cannot create binary operator with two operands of differing type!");
|
2006-12-23 06:05:41 +00:00
|
|
|
return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
|
2005-03-16 05:42:00 +00:00
|
|
|
const std::string &Name,
|
2004-07-29 12:33:25 +00:00
|
|
|
BasicBlock *InsertAtEnd) {
|
2008-05-16 19:29:10 +00:00
|
|
|
BinaryOperator *Res = Create(Op, S1, S2, Name);
|
2004-07-29 12:33:25 +00:00
|
|
|
InsertAtEnd->getInstList().push_back(Res);
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
|
2004-07-29 12:33:25 +00:00
|
|
|
Instruction *InsertBefore) {
|
2007-01-21 00:29:26 +00:00
|
|
|
Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
|
|
|
|
return new BinaryOperator(Instruction::Sub,
|
|
|
|
zero, Op,
|
|
|
|
Op->getType(), Name, InsertBefore);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
|
2004-07-29 12:33:25 +00:00
|
|
|
BasicBlock *InsertAtEnd) {
|
2007-01-21 00:29:26 +00:00
|
|
|
Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
|
|
|
|
return new BinaryOperator(Instruction::Sub,
|
|
|
|
zero, Op,
|
|
|
|
Op->getType(), Name, InsertAtEnd);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
|
2004-07-29 12:33:25 +00:00
|
|
|
Instruction *InsertBefore) {
|
2006-03-25 21:54:21 +00:00
|
|
|
Constant *C;
|
2007-02-15 02:26:10 +00:00
|
|
|
if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
|
2007-01-11 12:24:14 +00:00
|
|
|
C = ConstantInt::getAllOnesValue(PTy->getElementType());
|
2007-02-15 02:26:10 +00:00
|
|
|
C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
|
2006-03-25 21:54:21 +00:00
|
|
|
} else {
|
2007-01-11 12:24:14 +00:00
|
|
|
C = ConstantInt::getAllOnesValue(Op->getType());
|
2006-03-25 21:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new BinaryOperator(Instruction::Xor, Op, C,
|
2004-07-29 12:33:25 +00:00
|
|
|
Op->getType(), Name, InsertBefore);
|
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
|
2004-07-29 12:33:25 +00:00
|
|
|
BasicBlock *InsertAtEnd) {
|
2005-12-21 18:22:19 +00:00
|
|
|
Constant *AllOnes;
|
2007-02-15 02:26:10 +00:00
|
|
|
if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
|
2005-12-21 18:22:19 +00:00
|
|
|
// Create a vector of all ones values.
|
2007-01-11 12:24:14 +00:00
|
|
|
Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
|
2005-12-21 18:22:19 +00:00
|
|
|
AllOnes =
|
2007-02-15 02:26:10 +00:00
|
|
|
ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
|
2005-12-21 18:22:19 +00:00
|
|
|
} else {
|
2007-01-11 12:24:14 +00:00
|
|
|
AllOnes = ConstantInt::getAllOnesValue(Op->getType());
|
2005-12-21 18:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new BinaryOperator(Instruction::Xor, Op, AllOnes,
|
2004-07-29 12:33:25 +00:00
|
|
|
Op->getType(), Name, InsertAtEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// isConstantAllOnes - Helper function for several functions below
|
|
|
|
static inline bool isConstantAllOnes(const Value *V) {
|
2007-06-15 06:04:24 +00:00
|
|
|
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
|
|
|
|
return CI->isAllOnesValue();
|
|
|
|
if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
|
|
|
|
return CV->isAllOnesValue();
|
|
|
|
return false;
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BinaryOperator::isNeg(const Value *V) {
|
|
|
|
if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
|
|
|
|
if (Bop->getOpcode() == Instruction::Sub)
|
2007-01-21 00:29:26 +00:00
|
|
|
return Bop->getOperand(0) ==
|
|
|
|
ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
|
2004-07-29 12:33:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BinaryOperator::isNot(const Value *V) {
|
|
|
|
if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
|
|
|
|
return (Bop->getOpcode() == Instruction::Xor &&
|
|
|
|
(isConstantAllOnes(Bop->getOperand(1)) ||
|
|
|
|
isConstantAllOnes(Bop->getOperand(0))));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-04-24 07:28:37 +00:00
|
|
|
Value *BinaryOperator::getNegArgument(Value *BinOp) {
|
|
|
|
assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
|
|
|
|
return cast<BinaryOperator>(BinOp)->getOperand(1);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2005-04-24 07:28:37 +00:00
|
|
|
const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
|
|
|
|
return getNegArgument(const_cast<Value*>(BinOp));
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2005-04-24 07:28:37 +00:00
|
|
|
Value *BinaryOperator::getNotArgument(Value *BinOp) {
|
|
|
|
assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
|
|
|
|
BinaryOperator *BO = cast<BinaryOperator>(BinOp);
|
|
|
|
Value *Op0 = BO->getOperand(0);
|
|
|
|
Value *Op1 = BO->getOperand(1);
|
2004-07-29 12:33:25 +00:00
|
|
|
if (isConstantAllOnes(Op0)) return Op1;
|
|
|
|
|
|
|
|
assert(isConstantAllOnes(Op1));
|
|
|
|
return Op0;
|
|
|
|
}
|
|
|
|
|
2005-04-24 07:28:37 +00:00
|
|
|
const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
|
|
|
|
return getNotArgument(const_cast<Value*>(BinOp));
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// swapOperands - Exchange the two operands to this instruction. This
|
|
|
|
// instruction is safe to use on any binary instruction and does not
|
|
|
|
// modify the semantics of the instruction. If the instruction is
|
|
|
|
// order dependent (SetLT f.e.) the opcode is changed.
|
|
|
|
//
|
|
|
|
bool BinaryOperator::swapOperands() {
|
2006-12-23 06:05:41 +00:00
|
|
|
if (!isCommutative())
|
|
|
|
return true; // Can't commute operands
|
2008-05-13 22:51:52 +00:00
|
|
|
Op<0>().swap(Op<1>());
|
2004-07-29 12:33:25 +00:00
|
|
|
return false;
|
2006-09-17 19:29:56 +00:00
|
|
|
}
|
|
|
|
|
2006-09-18 04:54:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CastInst Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
// Just determine if this cast only deals with integral->integral conversion.
|
|
|
|
bool CastInst::isIntegerCast() const {
|
|
|
|
switch (getOpcode()) {
|
|
|
|
default: return false;
|
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::SExt:
|
|
|
|
case Instruction::Trunc:
|
|
|
|
return true;
|
|
|
|
case Instruction::BitCast:
|
2007-01-15 02:27:26 +00:00
|
|
|
return getOperand(0)->getType()->isInteger() && getType()->isInteger();
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CastInst::isLosslessCast() const {
|
|
|
|
// Only BitCast can be lossless, exit fast if we're not BitCast
|
|
|
|
if (getOpcode() != Instruction::BitCast)
|
2006-09-18 04:54:57 +00:00
|
|
|
return false;
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
// Identity cast is always lossless
|
|
|
|
const Type* SrcTy = getOperand(0)->getType();
|
|
|
|
const Type* DstTy = getType();
|
|
|
|
if (SrcTy == DstTy)
|
|
|
|
return true;
|
|
|
|
|
2006-12-31 05:26:44 +00:00
|
|
|
// Pointer to pointer is always lossless.
|
|
|
|
if (isa<PointerType>(SrcTy))
|
|
|
|
return isa<PointerType>(DstTy);
|
2006-11-27 01:05:10 +00:00
|
|
|
return false; // Other types have no identity values
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function determines if the CastInst does not require any bits to be
|
|
|
|
/// changed in order to effect the cast. Essentially, it identifies cases where
|
|
|
|
/// no code gen is necessary for the cast, hence the name no-op cast. For
|
|
|
|
/// example, the following are all no-op casts:
|
2008-05-12 16:34:30 +00:00
|
|
|
/// # bitcast i32* %x to i8*
|
|
|
|
/// # bitcast <2 x i32> %x to <4 x i16>
|
|
|
|
/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
|
2006-11-27 01:05:10 +00:00
|
|
|
/// @brief Determine if a cast is a no-op.
|
|
|
|
bool CastInst::isNoopCast(const Type *IntPtrTy) const {
|
|
|
|
switch (getOpcode()) {
|
|
|
|
default:
|
|
|
|
assert(!"Invalid CastOp");
|
|
|
|
case Instruction::Trunc:
|
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::SExt:
|
|
|
|
case Instruction::FPTrunc:
|
|
|
|
case Instruction::FPExt:
|
|
|
|
case Instruction::UIToFP:
|
|
|
|
case Instruction::SIToFP:
|
|
|
|
case Instruction::FPToUI:
|
|
|
|
case Instruction::FPToSI:
|
|
|
|
return false; // These always modify bits
|
|
|
|
case Instruction::BitCast:
|
|
|
|
return true; // BitCast never modifies bits.
|
|
|
|
case Instruction::PtrToInt:
|
|
|
|
return IntPtrTy->getPrimitiveSizeInBits() ==
|
|
|
|
getType()->getPrimitiveSizeInBits();
|
|
|
|
case Instruction::IntToPtr:
|
|
|
|
return IntPtrTy->getPrimitiveSizeInBits() ==
|
|
|
|
getOperand(0)->getType()->getPrimitiveSizeInBits();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function determines if a pair of casts can be eliminated and what
|
|
|
|
/// opcode should be used in the elimination. This assumes that there are two
|
|
|
|
/// instructions like this:
|
|
|
|
/// * %F = firstOpcode SrcTy %x to MidTy
|
|
|
|
/// * %S = secondOpcode MidTy %F to DstTy
|
|
|
|
/// The function returns a resultOpcode so these two casts can be replaced with:
|
|
|
|
/// * %Replacement = resultOpcode %SrcTy %x to DstTy
|
|
|
|
/// If no such cast is permited, the function returns 0.
|
|
|
|
unsigned CastInst::isEliminableCastPair(
|
|
|
|
Instruction::CastOps firstOp, Instruction::CastOps secondOp,
|
|
|
|
const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
|
|
|
|
{
|
|
|
|
// Define the 144 possibilities for these two cast instructions. The values
|
|
|
|
// in this matrix determine what to do in a given situation and select the
|
|
|
|
// case in the switch below. The rows correspond to firstOp, the columns
|
|
|
|
// correspond to secondOp. In looking at the table below, keep in mind
|
|
|
|
// the following cast properties:
|
|
|
|
//
|
|
|
|
// Size Compare Source Destination
|
|
|
|
// Operator Src ? Size Type Sign Type Sign
|
|
|
|
// -------- ------------ ------------------- ---------------------
|
|
|
|
// TRUNC > Integer Any Integral Any
|
|
|
|
// ZEXT < Integral Unsigned Integer Any
|
|
|
|
// SEXT < Integral Signed Integer Any
|
|
|
|
// FPTOUI n/a FloatPt n/a Integral Unsigned
|
|
|
|
// FPTOSI n/a FloatPt n/a Integral Signed
|
|
|
|
// UITOFP n/a Integral Unsigned FloatPt n/a
|
|
|
|
// SITOFP n/a Integral Signed FloatPt n/a
|
|
|
|
// FPTRUNC > FloatPt n/a FloatPt n/a
|
|
|
|
// FPEXT < FloatPt n/a FloatPt n/a
|
|
|
|
// PTRTOINT n/a Pointer n/a Integral Unsigned
|
|
|
|
// INTTOPTR n/a Integral Unsigned Pointer n/a
|
|
|
|
// BITCONVERT = FirstClass n/a FirstClass n/a
|
2006-12-05 23:43:59 +00:00
|
|
|
//
|
|
|
|
// NOTE: some transforms are safe, but we consider them to be non-profitable.
|
|
|
|
// For example, we could merge "fptoui double to uint" + "zext uint to ulong",
|
|
|
|
// into "fptoui double to ulong", but this loses information about the range
|
|
|
|
// of the produced value (we no longer know the top-part is all zeros).
|
|
|
|
// Further this conversion is often much more expensive for typical hardware,
|
|
|
|
// and causes issues when building libgcc. We disallow fptosi+sext for the
|
|
|
|
// same reason.
|
2006-11-27 01:05:10 +00:00
|
|
|
const unsigned numCastOps =
|
|
|
|
Instruction::CastOpsEnd - Instruction::CastOpsBegin;
|
|
|
|
static const uint8_t CastResults[numCastOps][numCastOps] = {
|
|
|
|
// T F F U S F F P I B -+
|
|
|
|
// R Z S P P I I T P 2 N T |
|
|
|
|
// U E E 2 2 2 2 R E I T C +- secondOp
|
|
|
|
// N X X U S F F N X N 2 V |
|
|
|
|
// C T T I I P P C T T P T -+
|
|
|
|
{ 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
|
|
|
|
{ 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
|
|
|
|
{ 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
|
2006-12-05 23:43:59 +00:00
|
|
|
{ 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
|
|
|
|
{ 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
|
2006-11-27 01:05:10 +00:00
|
|
|
{ 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
|
|
|
|
{ 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
|
|
|
|
{ 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
|
|
|
|
{ 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
|
|
|
|
{ 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
|
|
|
|
{ 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
|
|
|
|
{ 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
|
|
|
|
};
|
|
|
|
|
|
|
|
int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
|
|
|
|
[secondOp-Instruction::CastOpsBegin];
|
|
|
|
switch (ElimCase) {
|
|
|
|
case 0:
|
|
|
|
// categorically disallowed
|
|
|
|
return 0;
|
|
|
|
case 1:
|
|
|
|
// allowed, use first cast's opcode
|
|
|
|
return firstOp;
|
|
|
|
case 2:
|
|
|
|
// allowed, use second cast's opcode
|
|
|
|
return secondOp;
|
|
|
|
case 3:
|
|
|
|
// no-op cast in second op implies firstOp as long as the DestTy
|
|
|
|
// is integer
|
2007-01-15 02:27:26 +00:00
|
|
|
if (DstTy->isInteger())
|
2006-11-27 01:05:10 +00:00
|
|
|
return firstOp;
|
|
|
|
return 0;
|
|
|
|
case 4:
|
|
|
|
// no-op cast in second op implies firstOp as long as the DestTy
|
|
|
|
// is floating point
|
|
|
|
if (DstTy->isFloatingPoint())
|
|
|
|
return firstOp;
|
|
|
|
return 0;
|
|
|
|
case 5:
|
|
|
|
// no-op cast in first op implies secondOp as long as the SrcTy
|
|
|
|
// is an integer
|
2007-01-15 02:27:26 +00:00
|
|
|
if (SrcTy->isInteger())
|
2006-11-27 01:05:10 +00:00
|
|
|
return secondOp;
|
|
|
|
return 0;
|
|
|
|
case 6:
|
|
|
|
// no-op cast in first op implies secondOp as long as the SrcTy
|
|
|
|
// is a floating point
|
|
|
|
if (SrcTy->isFloatingPoint())
|
|
|
|
return secondOp;
|
|
|
|
return 0;
|
|
|
|
case 7: {
|
|
|
|
// ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
|
|
|
|
unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
|
|
|
|
unsigned MidSize = MidTy->getPrimitiveSizeInBits();
|
|
|
|
if (MidSize >= PtrSize)
|
|
|
|
return Instruction::BitCast;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case 8: {
|
|
|
|
// ext, trunc -> bitcast, if the SrcTy and DstTy are same size
|
|
|
|
// ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
|
|
|
|
// ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
|
|
|
|
unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
|
|
|
|
unsigned DstSize = DstTy->getPrimitiveSizeInBits();
|
|
|
|
if (SrcSize == DstSize)
|
|
|
|
return Instruction::BitCast;
|
|
|
|
else if (SrcSize < DstSize)
|
|
|
|
return firstOp;
|
|
|
|
return secondOp;
|
|
|
|
}
|
|
|
|
case 9: // zext, sext -> zext, because sext can't sign extend after zext
|
|
|
|
return Instruction::ZExt;
|
|
|
|
case 10:
|
|
|
|
// fpext followed by ftrunc is allowed if the bit size returned to is
|
|
|
|
// the same as the original, in which case its just a bitcast
|
|
|
|
if (SrcTy == DstTy)
|
|
|
|
return Instruction::BitCast;
|
|
|
|
return 0; // If the types are not the same we can't eliminate it.
|
|
|
|
case 11:
|
|
|
|
// bitcast followed by ptrtoint is allowed as long as the bitcast
|
|
|
|
// is a pointer to pointer cast.
|
|
|
|
if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
|
|
|
|
return secondOp;
|
|
|
|
return 0;
|
|
|
|
case 12:
|
|
|
|
// inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
|
|
|
|
if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
|
|
|
|
return firstOp;
|
|
|
|
return 0;
|
|
|
|
case 13: {
|
|
|
|
// inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
|
|
|
|
unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
|
|
|
|
unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
|
|
|
|
unsigned DstSize = DstTy->getPrimitiveSizeInBits();
|
|
|
|
if (SrcSize <= PtrSize && SrcSize == DstSize)
|
|
|
|
return Instruction::BitCast;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case 99:
|
|
|
|
// cast combination can't happen (error in input). This is for all cases
|
|
|
|
// where the MidTy is not the same for the two cast instructions.
|
|
|
|
assert(!"Invalid Cast Combination");
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
assert(!"Error in CastResults table!!!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
|
2006-11-27 01:05:10 +00:00
|
|
|
const std::string &Name, Instruction *InsertBefore) {
|
|
|
|
// Construct and return the appropriate CastInst subclass
|
|
|
|
switch (op) {
|
|
|
|
case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
|
|
|
|
case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
|
|
|
|
case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
|
|
|
|
case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
|
|
|
|
case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
|
|
|
|
case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
|
|
|
|
case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
|
|
|
|
case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
|
|
|
|
case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
|
|
|
|
case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
|
|
|
|
case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
|
|
|
|
case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
|
|
|
|
default:
|
|
|
|
assert(!"Invalid opcode provided");
|
|
|
|
}
|
|
|
|
return 0;
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
|
2006-11-27 01:05:10 +00:00
|
|
|
const std::string &Name, BasicBlock *InsertAtEnd) {
|
|
|
|
// Construct and return the appropriate CastInst subclass
|
|
|
|
switch (op) {
|
|
|
|
case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
|
|
|
|
case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
|
|
|
|
case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
|
|
|
|
case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
|
|
|
|
case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
|
|
|
|
case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
|
|
|
|
case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
|
|
|
|
case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
|
|
|
|
case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
|
|
|
|
case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
|
|
|
|
case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
|
|
|
|
case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
|
|
|
|
default:
|
|
|
|
assert(!"Invalid opcode provided");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
|
2006-12-04 20:17:56 +00:00
|
|
|
const std::string &Name,
|
|
|
|
Instruction *InsertBefore) {
|
|
|
|
if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
|
2008-05-16 19:29:10 +00:00
|
|
|
return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
|
|
|
|
return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
|
2006-12-04 20:17:56 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
|
2006-12-04 20:17:56 +00:00
|
|
|
const std::string &Name,
|
|
|
|
BasicBlock *InsertAtEnd) {
|
|
|
|
if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
|
2008-05-16 19:29:10 +00:00
|
|
|
return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
|
|
|
|
return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
|
2006-12-04 20:17:56 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
|
2006-12-04 20:17:56 +00:00
|
|
|
const std::string &Name,
|
|
|
|
Instruction *InsertBefore) {
|
|
|
|
if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
|
2008-05-16 19:29:10 +00:00
|
|
|
return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
|
|
|
|
return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
|
2006-12-04 20:17:56 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
|
2006-12-04 20:17:56 +00:00
|
|
|
const std::string &Name,
|
|
|
|
BasicBlock *InsertAtEnd) {
|
|
|
|
if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
|
2008-05-16 19:29:10 +00:00
|
|
|
return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
|
|
|
|
return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
|
2006-12-04 20:17:56 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
|
2006-12-04 20:17:56 +00:00
|
|
|
const std::string &Name,
|
|
|
|
Instruction *InsertBefore) {
|
|
|
|
if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
|
2008-05-16 19:29:10 +00:00
|
|
|
return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
|
|
|
|
return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
|
2006-12-04 20:17:56 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
|
2006-12-04 20:17:56 +00:00
|
|
|
const std::string &Name,
|
|
|
|
BasicBlock *InsertAtEnd) {
|
|
|
|
if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
|
2008-05-16 19:29:10 +00:00
|
|
|
return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
|
|
|
|
return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
|
2006-12-04 20:17:56 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
|
2006-12-05 03:28:26 +00:00
|
|
|
const std::string &Name,
|
|
|
|
BasicBlock *InsertAtEnd) {
|
|
|
|
assert(isa<PointerType>(S->getType()) && "Invalid cast");
|
2007-01-15 02:27:26 +00:00
|
|
|
assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
|
2006-12-05 03:28:26 +00:00
|
|
|
"Invalid cast");
|
|
|
|
|
2007-01-15 02:27:26 +00:00
|
|
|
if (Ty->isInteger())
|
2008-05-16 19:29:10 +00:00
|
|
|
return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
|
|
|
|
return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
|
2006-12-05 03:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Create a BitCast or a PtrToInt cast instruction
|
2008-05-16 19:29:10 +00:00
|
|
|
CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
|
2006-12-05 03:28:26 +00:00
|
|
|
const std::string &Name,
|
|
|
|
Instruction *InsertBefore) {
|
|
|
|
assert(isa<PointerType>(S->getType()) && "Invalid cast");
|
2007-01-15 02:27:26 +00:00
|
|
|
assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
|
2006-12-05 03:28:26 +00:00
|
|
|
"Invalid cast");
|
|
|
|
|
2007-01-15 02:27:26 +00:00
|
|
|
if (Ty->isInteger())
|
2008-05-16 19:29:10 +00:00
|
|
|
return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
|
|
|
|
return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
|
2006-12-05 03:28:26 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
|
2006-12-12 00:49:44 +00:00
|
|
|
bool isSigned, const std::string &Name,
|
|
|
|
Instruction *InsertBefore) {
|
2007-01-15 02:27:26 +00:00
|
|
|
assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
|
2006-12-12 00:49:44 +00:00
|
|
|
unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
|
|
|
|
unsigned DstBits = Ty->getPrimitiveSizeInBits();
|
|
|
|
Instruction::CastOps opcode =
|
|
|
|
(SrcBits == DstBits ? Instruction::BitCast :
|
|
|
|
(SrcBits > DstBits ? Instruction::Trunc :
|
|
|
|
(isSigned ? Instruction::SExt : Instruction::ZExt)));
|
2008-05-16 19:29:10 +00:00
|
|
|
return Create(opcode, C, Ty, Name, InsertBefore);
|
2006-12-12 00:49:44 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
|
2006-12-12 00:49:44 +00:00
|
|
|
bool isSigned, const std::string &Name,
|
|
|
|
BasicBlock *InsertAtEnd) {
|
2007-01-15 02:27:26 +00:00
|
|
|
assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
|
2006-12-12 00:49:44 +00:00
|
|
|
unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
|
|
|
|
unsigned DstBits = Ty->getPrimitiveSizeInBits();
|
|
|
|
Instruction::CastOps opcode =
|
|
|
|
(SrcBits == DstBits ? Instruction::BitCast :
|
|
|
|
(SrcBits > DstBits ? Instruction::Trunc :
|
|
|
|
(isSigned ? Instruction::SExt : Instruction::ZExt)));
|
2008-05-16 19:29:10 +00:00
|
|
|
return Create(opcode, C, Ty, Name, InsertAtEnd);
|
2006-12-12 00:49:44 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
|
2006-12-12 00:49:44 +00:00
|
|
|
const std::string &Name,
|
|
|
|
Instruction *InsertBefore) {
|
|
|
|
assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
|
|
|
|
"Invalid cast");
|
|
|
|
unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
|
|
|
|
unsigned DstBits = Ty->getPrimitiveSizeInBits();
|
|
|
|
Instruction::CastOps opcode =
|
|
|
|
(SrcBits == DstBits ? Instruction::BitCast :
|
|
|
|
(SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
|
2008-05-16 19:29:10 +00:00
|
|
|
return Create(opcode, C, Ty, Name, InsertBefore);
|
2006-12-12 00:49:44 +00:00
|
|
|
}
|
|
|
|
|
2008-05-16 19:29:10 +00:00
|
|
|
CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
|
2006-12-12 00:49:44 +00:00
|
|
|
const std::string &Name,
|
|
|
|
BasicBlock *InsertAtEnd) {
|
|
|
|
assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
|
|
|
|
"Invalid cast");
|
|
|
|
unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
|
|
|
|
unsigned DstBits = Ty->getPrimitiveSizeInBits();
|
|
|
|
Instruction::CastOps opcode =
|
|
|
|
(SrcBits == DstBits ? Instruction::BitCast :
|
|
|
|
(SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
|
2008-05-16 19:29:10 +00:00
|
|
|
return Create(opcode, C, Ty, Name, InsertAtEnd);
|
2006-12-12 00:49:44 +00:00
|
|
|
}
|
|
|
|
|
2008-01-06 10:12:28 +00:00
|
|
|
// Check whether it is valid to call getCastOpcode for these types.
|
|
|
|
// This routine must be kept in sync with getCastOpcode.
|
|
|
|
bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
|
|
|
|
if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (SrcTy == DestTy)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Get the bit sizes, we'll need these
|
|
|
|
unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
|
|
|
|
unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
|
|
|
|
|
|
|
|
// Run through the possibilities ...
|
2008-05-15 10:04:30 +00:00
|
|
|
if (DestTy->isInteger()) { // Casting to integral
|
|
|
|
if (SrcTy->isInteger()) { // Casting from integral
|
2008-01-06 10:12:28 +00:00
|
|
|
return true;
|
2008-05-15 10:04:30 +00:00
|
|
|
} else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
|
2008-01-06 10:12:28 +00:00
|
|
|
return true;
|
|
|
|
} else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
|
2008-05-15 10:04:30 +00:00
|
|
|
// Casting from vector
|
2008-01-06 10:12:28 +00:00
|
|
|
return DestBits == PTy->getBitWidth();
|
2008-05-15 10:04:30 +00:00
|
|
|
} else { // Casting from something else
|
2008-01-06 10:12:28 +00:00
|
|
|
return isa<PointerType>(SrcTy);
|
|
|
|
}
|
2008-05-15 10:04:30 +00:00
|
|
|
} else if (DestTy->isFloatingPoint()) { // Casting to floating pt
|
|
|
|
if (SrcTy->isInteger()) { // Casting from integral
|
2008-01-06 10:12:28 +00:00
|
|
|
return true;
|
2008-05-15 10:04:30 +00:00
|
|
|
} else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
|
2008-01-06 10:12:28 +00:00
|
|
|
return true;
|
|
|
|
} else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
|
2008-05-15 10:04:30 +00:00
|
|
|
// Casting from vector
|
2008-01-06 10:12:28 +00:00
|
|
|
return DestBits == PTy->getBitWidth();
|
2008-05-15 10:04:30 +00:00
|
|
|
} else { // Casting from something else
|
2008-01-06 10:12:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
|
2008-05-15 10:04:30 +00:00
|
|
|
// Casting to vector
|
2008-01-06 10:12:28 +00:00
|
|
|
if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
|
2008-05-15 10:04:30 +00:00
|
|
|
// Casting from vector
|
2008-01-06 10:12:28 +00:00
|
|
|
return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
|
2008-05-15 10:04:30 +00:00
|
|
|
} else { // Casting from something else
|
2008-01-06 10:12:28 +00:00
|
|
|
return DestPTy->getBitWidth() == SrcBits;
|
|
|
|
}
|
2008-05-15 10:04:30 +00:00
|
|
|
} else if (isa<PointerType>(DestTy)) { // Casting to pointer
|
|
|
|
if (isa<PointerType>(SrcTy)) { // Casting from pointer
|
2008-01-06 10:12:28 +00:00
|
|
|
return true;
|
2008-05-15 10:04:30 +00:00
|
|
|
} else if (SrcTy->isInteger()) { // Casting from integral
|
2008-01-06 10:12:28 +00:00
|
|
|
return true;
|
2008-05-15 10:04:30 +00:00
|
|
|
} else { // Casting from something else
|
2008-01-06 10:12:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-05-15 10:04:30 +00:00
|
|
|
} else { // Casting to something else
|
2008-01-06 10:12:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
// Provide a way to get a "cast" where the cast opcode is inferred from the
|
|
|
|
// types and size of the operand. This, basically, is a parallel of the
|
2007-01-17 02:46:11 +00:00
|
|
|
// logic in the castIsValid function below. This axiom should hold:
|
|
|
|
// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
|
|
|
|
// should not assert in castIsValid. In other words, this produces a "correct"
|
2006-11-27 01:05:10 +00:00
|
|
|
// casting opcode for the arguments passed to it.
|
2008-01-06 10:12:28 +00:00
|
|
|
// This routine must be kept in sync with isCastable.
|
2006-11-27 01:05:10 +00:00
|
|
|
Instruction::CastOps
|
2006-12-04 02:43:42 +00:00
|
|
|
CastInst::getCastOpcode(
|
|
|
|
const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
|
2006-11-27 01:05:10 +00:00
|
|
|
// Get the bit sizes, we'll need these
|
|
|
|
const Type *SrcTy = Src->getType();
|
2007-05-11 21:43:24 +00:00
|
|
|
unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
|
|
|
|
unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
|
2006-11-27 01:05:10 +00:00
|
|
|
|
2008-01-06 10:12:28 +00:00
|
|
|
assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
|
|
|
|
"Only first class types are castable!");
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
// Run through the possibilities ...
|
2007-01-15 02:27:26 +00:00
|
|
|
if (DestTy->isInteger()) { // Casting to integral
|
|
|
|
if (SrcTy->isInteger()) { // Casting from integral
|
2006-11-27 01:05:10 +00:00
|
|
|
if (DestBits < SrcBits)
|
|
|
|
return Trunc; // int -> smaller int
|
|
|
|
else if (DestBits > SrcBits) { // its an extension
|
2006-12-04 02:43:42 +00:00
|
|
|
if (SrcIsSigned)
|
2006-11-27 01:05:10 +00:00
|
|
|
return SExt; // signed -> SEXT
|
|
|
|
else
|
|
|
|
return ZExt; // unsigned -> ZEXT
|
|
|
|
} else {
|
|
|
|
return BitCast; // Same size, No-op cast
|
|
|
|
}
|
|
|
|
} else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
|
2006-12-04 02:43:42 +00:00
|
|
|
if (DestIsSigned)
|
2006-11-27 01:05:10 +00:00
|
|
|
return FPToSI; // FP -> sint
|
|
|
|
else
|
|
|
|
return FPToUI; // FP -> uint
|
2007-02-15 02:26:10 +00:00
|
|
|
} else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
|
2006-11-27 01:05:10 +00:00
|
|
|
assert(DestBits == PTy->getBitWidth() &&
|
2007-05-11 21:43:24 +00:00
|
|
|
"Casting vector to integer of different width");
|
2006-11-27 01:05:10 +00:00
|
|
|
return BitCast; // Same size, no-op cast
|
|
|
|
} else {
|
|
|
|
assert(isa<PointerType>(SrcTy) &&
|
|
|
|
"Casting from a value that is not first-class type");
|
|
|
|
return PtrToInt; // ptr -> int
|
|
|
|
}
|
|
|
|
} else if (DestTy->isFloatingPoint()) { // Casting to floating pt
|
2007-01-15 02:27:26 +00:00
|
|
|
if (SrcTy->isInteger()) { // Casting from integral
|
2006-12-04 02:43:42 +00:00
|
|
|
if (SrcIsSigned)
|
2006-11-27 01:05:10 +00:00
|
|
|
return SIToFP; // sint -> FP
|
|
|
|
else
|
|
|
|
return UIToFP; // uint -> FP
|
|
|
|
} else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
|
|
|
|
if (DestBits < SrcBits) {
|
|
|
|
return FPTrunc; // FP -> smaller FP
|
|
|
|
} else if (DestBits > SrcBits) {
|
|
|
|
return FPExt; // FP -> larger FP
|
|
|
|
} else {
|
|
|
|
return BitCast; // same size, no-op cast
|
|
|
|
}
|
2007-02-15 02:26:10 +00:00
|
|
|
} else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
|
2006-11-27 01:05:10 +00:00
|
|
|
assert(DestBits == PTy->getBitWidth() &&
|
2007-05-11 21:43:24 +00:00
|
|
|
"Casting vector to floating point of different width");
|
2006-11-27 01:05:10 +00:00
|
|
|
return BitCast; // same size, no-op cast
|
|
|
|
} else {
|
|
|
|
assert(0 && "Casting pointer or non-first class to float");
|
|
|
|
}
|
2007-02-15 02:26:10 +00:00
|
|
|
} else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
|
|
|
|
if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
|
2006-11-27 01:05:10 +00:00
|
|
|
assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
|
2007-05-11 21:43:24 +00:00
|
|
|
"Casting vector to vector of different widths");
|
|
|
|
return BitCast; // vector -> vector
|
2006-11-27 01:05:10 +00:00
|
|
|
} else if (DestPTy->getBitWidth() == SrcBits) {
|
2007-05-11 21:43:24 +00:00
|
|
|
return BitCast; // float/int -> vector
|
2006-11-27 01:05:10 +00:00
|
|
|
} else {
|
2007-05-11 21:43:24 +00:00
|
|
|
assert(!"Illegal cast to vector (wrong type or size)");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
} else if (isa<PointerType>(DestTy)) {
|
|
|
|
if (isa<PointerType>(SrcTy)) {
|
|
|
|
return BitCast; // ptr -> ptr
|
2007-01-15 02:27:26 +00:00
|
|
|
} else if (SrcTy->isInteger()) {
|
2006-11-27 01:05:10 +00:00
|
|
|
return IntToPtr; // int -> ptr
|
|
|
|
} else {
|
|
|
|
assert(!"Casting pointer to other than pointer or int");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(!"Casting to type that is not first-class");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we fall through to here we probably hit an assertion cast above
|
|
|
|
// and assertions are not turned on. Anything we return is an error, so
|
|
|
|
// BitCast is as good a choice as any.
|
|
|
|
return BitCast;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CastInst SubClass Constructors
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Check that the construction parameters for a CastInst are correct. This
|
|
|
|
/// could be broken out into the separate constructors but it is useful to have
|
|
|
|
/// it in one place and to eliminate the redundant code for getting the sizes
|
|
|
|
/// of the types involved.
|
2007-01-17 02:46:11 +00:00
|
|
|
bool
|
|
|
|
CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
|
2006-11-27 01:05:10 +00:00
|
|
|
|
|
|
|
// Check for type sanity on the arguments
|
|
|
|
const Type *SrcTy = S->getType();
|
|
|
|
if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Get the size of the types in bits, we'll need this later
|
|
|
|
unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
|
|
|
|
unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
|
|
|
|
|
|
|
|
// Switch on the opcode provided
|
|
|
|
switch (op) {
|
|
|
|
default: return false; // This is an input error
|
|
|
|
case Instruction::Trunc:
|
2007-01-15 02:27:26 +00:00
|
|
|
return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::ZExt:
|
2007-01-15 02:27:26 +00:00
|
|
|
return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::SExt:
|
2007-01-15 02:27:26 +00:00
|
|
|
return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::FPTrunc:
|
|
|
|
return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
|
|
|
|
SrcBitSize > DstBitSize;
|
|
|
|
case Instruction::FPExt:
|
|
|
|
return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
|
|
|
|
SrcBitSize < DstBitSize;
|
|
|
|
case Instruction::UIToFP:
|
|
|
|
case Instruction::SIToFP:
|
2007-11-17 03:58:34 +00:00
|
|
|
if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
|
|
|
|
if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
|
|
|
|
return SVTy->getElementType()->isInteger() &&
|
|
|
|
DVTy->getElementType()->isFloatingPoint() &&
|
|
|
|
SVTy->getNumElements() == DVTy->getNumElements();
|
|
|
|
}
|
|
|
|
}
|
2007-01-15 02:27:26 +00:00
|
|
|
return SrcTy->isInteger() && DstTy->isFloatingPoint();
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::FPToUI:
|
|
|
|
case Instruction::FPToSI:
|
2007-11-17 03:58:34 +00:00
|
|
|
if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
|
|
|
|
if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
|
|
|
|
return SVTy->getElementType()->isFloatingPoint() &&
|
|
|
|
DVTy->getElementType()->isInteger() &&
|
|
|
|
SVTy->getNumElements() == DVTy->getNumElements();
|
|
|
|
}
|
|
|
|
}
|
2007-01-15 02:27:26 +00:00
|
|
|
return SrcTy->isFloatingPoint() && DstTy->isInteger();
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::PtrToInt:
|
2007-01-15 02:27:26 +00:00
|
|
|
return isa<PointerType>(SrcTy) && DstTy->isInteger();
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::IntToPtr:
|
2007-01-15 02:27:26 +00:00
|
|
|
return SrcTy->isInteger() && isa<PointerType>(DstTy);
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::BitCast:
|
|
|
|
// BitCast implies a no-op cast of type only. No bits change.
|
|
|
|
// However, you can't cast pointers to anything but pointers.
|
|
|
|
if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
|
|
|
|
return false;
|
|
|
|
|
2008-01-06 10:12:28 +00:00
|
|
|
// Now we know we're not dealing with a pointer/non-pointer mismatch. In all
|
2006-11-27 01:05:10 +00:00
|
|
|
// these cases, the cast is okay if the source and destination bit widths
|
|
|
|
// are identical.
|
|
|
|
return SrcBitSize == DstBitSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TruncInst::TruncInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
|
|
|
|
) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TruncInst::TruncInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
|
|
|
|
) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ZExtInst::ZExtInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
|
|
|
|
) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ZExtInst::ZExtInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
|
|
|
|
) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
SExtInst::SExtInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
|
|
|
|
) : CastInst(Ty, SExt, S, Name, InsertBefore) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
2006-12-02 02:22:01 +00:00
|
|
|
SExtInst::SExtInst(
|
2006-11-27 01:05:10 +00:00
|
|
|
Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
|
|
|
|
) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FPTruncInst::FPTruncInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
|
|
|
|
) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FPTruncInst::FPTruncInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
|
|
|
|
) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FPExtInst::FPExtInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
|
|
|
|
) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FPExtInst::FPExtInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
|
|
|
|
) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UIToFPInst::UIToFPInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
|
|
|
|
) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UIToFPInst::UIToFPInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
|
|
|
|
) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SIToFPInst::SIToFPInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
|
|
|
|
) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SIToFPInst::SIToFPInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
|
|
|
|
) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FPToUIInst::FPToUIInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
|
|
|
|
) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FPToUIInst::FPToUIInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
|
|
|
|
) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FPToSIInst::FPToSIInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
|
|
|
|
) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FPToSIInst::FPToSIInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
|
|
|
|
) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PtrToIntInst::PtrToIntInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
|
|
|
|
) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PtrToIntInst::PtrToIntInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
|
|
|
|
) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IntToPtrInst::IntToPtrInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
|
|
|
|
) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IntToPtrInst::IntToPtrInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
|
|
|
|
) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BitCastInst::BitCastInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
|
|
|
|
) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BitCastInst::BitCastInst(
|
|
|
|
Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
|
|
|
|
) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
|
2007-01-17 02:46:11 +00:00
|
|
|
assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
2004-07-29 12:33:25 +00:00
|
|
|
|
2006-11-20 01:22:35 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CmpInst Classes
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-12 19:01:56 +00:00
|
|
|
CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
|
|
|
|
Value *LHS, Value *RHS, const std::string &Name,
|
|
|
|
Instruction *InsertBefore)
|
2008-05-12 20:11:05 +00:00
|
|
|
: Instruction(ty, op,
|
2008-05-10 08:32:32 +00:00
|
|
|
OperandTraits<CmpInst>::op_begin(this),
|
|
|
|
OperandTraits<CmpInst>::operands(this),
|
|
|
|
InsertBefore) {
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = LHS;
|
|
|
|
Op<1>() = RHS;
|
2006-11-20 01:22:35 +00:00
|
|
|
SubclassData = predicate;
|
2007-04-11 13:04:48 +00:00
|
|
|
setName(Name);
|
2008-05-12 19:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
|
|
|
|
Value *LHS, Value *RHS, const std::string &Name,
|
|
|
|
BasicBlock *InsertAtEnd)
|
2008-05-12 20:11:05 +00:00
|
|
|
: Instruction(ty, op,
|
2008-05-10 08:32:32 +00:00
|
|
|
OperandTraits<CmpInst>::op_begin(this),
|
|
|
|
OperandTraits<CmpInst>::operands(this),
|
|
|
|
InsertAtEnd) {
|
2008-05-26 21:33:52 +00:00
|
|
|
Op<0>() = LHS;
|
|
|
|
Op<1>() = RHS;
|
2006-11-20 01:22:35 +00:00
|
|
|
SubclassData = predicate;
|
2007-04-11 13:04:48 +00:00
|
|
|
setName(Name);
|
2006-11-20 01:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CmpInst *
|
2008-05-16 19:29:10 +00:00
|
|
|
CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
|
2006-11-20 01:22:35 +00:00
|
|
|
const std::string &Name, Instruction *InsertBefore) {
|
|
|
|
if (Op == Instruction::ICmp) {
|
2008-05-12 19:01:56 +00:00
|
|
|
return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
|
|
|
|
InsertBefore);
|
|
|
|
}
|
|
|
|
if (Op == Instruction::FCmp) {
|
|
|
|
return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
|
2006-11-20 01:22:35 +00:00
|
|
|
InsertBefore);
|
|
|
|
}
|
2008-05-12 19:01:56 +00:00
|
|
|
if (Op == Instruction::VICmp) {
|
|
|
|
return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
|
|
|
|
InsertBefore);
|
|
|
|
}
|
|
|
|
return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
|
|
|
|
InsertBefore);
|
2006-11-20 01:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CmpInst *
|
2008-05-16 19:29:10 +00:00
|
|
|
CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
|
2006-11-20 01:22:35 +00:00
|
|
|
const std::string &Name, BasicBlock *InsertAtEnd) {
|
|
|
|
if (Op == Instruction::ICmp) {
|
2008-05-12 19:01:56 +00:00
|
|
|
return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
|
|
|
|
InsertAtEnd);
|
|
|
|
}
|
|
|
|
if (Op == Instruction::FCmp) {
|
|
|
|
return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
|
2006-11-20 01:22:35 +00:00
|
|
|
InsertAtEnd);
|
|
|
|
}
|
2008-05-12 19:01:56 +00:00
|
|
|
if (Op == Instruction::VICmp) {
|
|
|
|
return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
|
|
|
|
InsertAtEnd);
|
|
|
|
}
|
|
|
|
return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
|
|
|
|
InsertAtEnd);
|
2006-11-20 01:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CmpInst::swapOperands() {
|
|
|
|
if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
|
|
|
|
IC->swapOperands();
|
|
|
|
else
|
|
|
|
cast<FCmpInst>(this)->swapOperands();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CmpInst::isCommutative() {
|
|
|
|
if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
|
|
|
|
return IC->isCommutative();
|
|
|
|
return cast<FCmpInst>(this)->isCommutative();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CmpInst::isEquality() {
|
|
|
|
if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
|
|
|
|
return IC->isEquality();
|
|
|
|
return cast<FCmpInst>(this)->isEquality();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
|
|
|
|
switch (pred) {
|
|
|
|
default:
|
|
|
|
assert(!"Unknown icmp predicate!");
|
|
|
|
case ICMP_EQ: return ICMP_NE;
|
|
|
|
case ICMP_NE: return ICMP_EQ;
|
|
|
|
case ICMP_UGT: return ICMP_ULE;
|
|
|
|
case ICMP_ULT: return ICMP_UGE;
|
|
|
|
case ICMP_UGE: return ICMP_ULT;
|
|
|
|
case ICMP_ULE: return ICMP_UGT;
|
|
|
|
case ICMP_SGT: return ICMP_SLE;
|
|
|
|
case ICMP_SLT: return ICMP_SGE;
|
|
|
|
case ICMP_SGE: return ICMP_SLT;
|
|
|
|
case ICMP_SLE: return ICMP_SGT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
|
|
|
|
switch (pred) {
|
2006-12-23 06:05:41 +00:00
|
|
|
default: assert(! "Unknown icmp predicate!");
|
2006-11-20 01:22:35 +00:00
|
|
|
case ICMP_EQ: case ICMP_NE:
|
|
|
|
return pred;
|
|
|
|
case ICMP_SGT: return ICMP_SLT;
|
|
|
|
case ICMP_SLT: return ICMP_SGT;
|
|
|
|
case ICMP_SGE: return ICMP_SLE;
|
|
|
|
case ICMP_SLE: return ICMP_SGE;
|
|
|
|
case ICMP_UGT: return ICMP_ULT;
|
|
|
|
case ICMP_ULT: return ICMP_UGT;
|
|
|
|
case ICMP_UGE: return ICMP_ULE;
|
|
|
|
case ICMP_ULE: return ICMP_UGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
|
|
|
|
switch (pred) {
|
|
|
|
default: assert(! "Unknown icmp predicate!");
|
|
|
|
case ICMP_EQ: case ICMP_NE:
|
|
|
|
case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
|
|
|
|
return pred;
|
|
|
|
case ICMP_UGT: return ICMP_SGT;
|
|
|
|
case ICMP_ULT: return ICMP_SLT;
|
|
|
|
case ICMP_UGE: return ICMP_SGE;
|
|
|
|
case ICMP_ULE: return ICMP_SLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-28 03:48:02 +00:00
|
|
|
ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
|
|
|
|
switch (pred) {
|
|
|
|
default: assert(! "Unknown icmp predicate!");
|
|
|
|
case ICMP_EQ: case ICMP_NE:
|
|
|
|
case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
|
|
|
|
return pred;
|
|
|
|
case ICMP_SGT: return ICMP_UGT;
|
|
|
|
case ICMP_SLT: return ICMP_ULT;
|
|
|
|
case ICMP_SGE: return ICMP_UGE;
|
|
|
|
case ICMP_SLE: return ICMP_ULE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
bool ICmpInst::isSignedPredicate(Predicate pred) {
|
|
|
|
switch (pred) {
|
|
|
|
default: assert(! "Unknown icmp predicate!");
|
|
|
|
case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
|
|
|
|
return true;
|
|
|
|
case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
|
|
|
|
case ICMP_UGE: case ICMP_ULE:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-28 22:00:54 +00:00
|
|
|
/// Initialize a set of values that all satisfy the condition with C.
|
|
|
|
///
|
|
|
|
ConstantRange
|
|
|
|
ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
|
|
|
|
APInt Lower(C);
|
|
|
|
APInt Upper(C);
|
|
|
|
uint32_t BitWidth = C.getBitWidth();
|
|
|
|
switch (pred) {
|
|
|
|
default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
|
|
|
|
case ICmpInst::ICMP_EQ: Upper++; break;
|
|
|
|
case ICmpInst::ICMP_NE: Lower++; break;
|
|
|
|
case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
|
|
|
|
case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
|
|
|
|
case ICmpInst::ICMP_UGT:
|
|
|
|
Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_SGT:
|
|
|
|
Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_ULE:
|
|
|
|
Lower = APInt::getMinValue(BitWidth); Upper++;
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_SLE:
|
|
|
|
Lower = APInt::getSignedMinValue(BitWidth); Upper++;
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_UGE:
|
|
|
|
Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
|
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_SGE:
|
|
|
|
Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ConstantRange(Lower, Upper);
|
|
|
|
}
|
|
|
|
|
2006-11-20 01:22:35 +00:00
|
|
|
FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
|
|
|
|
switch (pred) {
|
|
|
|
default:
|
|
|
|
assert(!"Unknown icmp predicate!");
|
|
|
|
case FCMP_OEQ: return FCMP_UNE;
|
|
|
|
case FCMP_ONE: return FCMP_UEQ;
|
|
|
|
case FCMP_OGT: return FCMP_ULE;
|
|
|
|
case FCMP_OLT: return FCMP_UGE;
|
|
|
|
case FCMP_OGE: return FCMP_ULT;
|
|
|
|
case FCMP_OLE: return FCMP_UGT;
|
|
|
|
case FCMP_UEQ: return FCMP_ONE;
|
|
|
|
case FCMP_UNE: return FCMP_OEQ;
|
|
|
|
case FCMP_UGT: return FCMP_OLE;
|
|
|
|
case FCMP_ULT: return FCMP_OGE;
|
|
|
|
case FCMP_UGE: return FCMP_OLT;
|
|
|
|
case FCMP_ULE: return FCMP_OGT;
|
|
|
|
case FCMP_ORD: return FCMP_UNO;
|
|
|
|
case FCMP_UNO: return FCMP_ORD;
|
|
|
|
case FCMP_TRUE: return FCMP_FALSE;
|
|
|
|
case FCMP_FALSE: return FCMP_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
|
|
|
|
switch (pred) {
|
2006-12-23 06:05:41 +00:00
|
|
|
default: assert(!"Unknown fcmp predicate!");
|
2006-11-20 01:22:35 +00:00
|
|
|
case FCMP_FALSE: case FCMP_TRUE:
|
|
|
|
case FCMP_OEQ: case FCMP_ONE:
|
|
|
|
case FCMP_UEQ: case FCMP_UNE:
|
|
|
|
case FCMP_ORD: case FCMP_UNO:
|
|
|
|
return pred;
|
|
|
|
case FCMP_OGT: return FCMP_OLT;
|
|
|
|
case FCMP_OLT: return FCMP_OGT;
|
|
|
|
case FCMP_OGE: return FCMP_OLE;
|
|
|
|
case FCMP_OLE: return FCMP_OGE;
|
|
|
|
case FCMP_UGT: return FCMP_ULT;
|
|
|
|
case FCMP_ULT: return FCMP_UGT;
|
|
|
|
case FCMP_UGE: return FCMP_ULE;
|
|
|
|
case FCMP_ULE: return FCMP_UGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
bool CmpInst::isUnsigned(unsigned short predicate) {
|
|
|
|
switch (predicate) {
|
|
|
|
default: return false;
|
|
|
|
case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
|
|
|
|
case ICmpInst::ICMP_UGE: return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CmpInst::isSigned(unsigned short predicate){
|
|
|
|
switch (predicate) {
|
|
|
|
default: return false;
|
|
|
|
case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
|
|
|
|
case ICmpInst::ICMP_SGE: return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CmpInst::isOrdered(unsigned short predicate) {
|
|
|
|
switch (predicate) {
|
|
|
|
default: return false;
|
|
|
|
case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
|
|
|
|
case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
|
|
|
|
case FCmpInst::FCMP_ORD: return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CmpInst::isUnordered(unsigned short predicate) {
|
|
|
|
switch (predicate) {
|
|
|
|
default: return false;
|
|
|
|
case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
|
|
|
|
case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
|
|
|
|
case FCmpInst::FCMP_UNO: return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-29 12:33:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SwitchInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-01-29 00:35:16 +00:00
|
|
|
void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
|
2004-07-29 12:33:25 +00:00
|
|
|
assert(Value && Default);
|
2005-01-29 00:35:16 +00:00
|
|
|
ReservedSpace = 2+NumCases*2;
|
|
|
|
NumOperands = 2;
|
2008-05-10 08:32:32 +00:00
|
|
|
OperandList = allocHungoffUses(ReservedSpace);
|
2005-01-29 00:35:16 +00:00
|
|
|
|
2008-05-26 21:33:52 +00:00
|
|
|
OperandList[0] = Value;
|
|
|
|
OperandList[1] = Default;
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
2007-02-24 00:55:48 +00:00
|
|
|
/// SwitchInst ctor - Create a new switch instruction, specifying a value to
|
|
|
|
/// switch on and a default destination. The number of additional cases can
|
|
|
|
/// be specified here to make memory allocation more efficient. This
|
|
|
|
/// constructor can also autoinsert before another instruction.
|
|
|
|
SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
|
|
|
|
Instruction *InsertBefore)
|
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
|
|
|
|
init(Value, Default, NumCases);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SwitchInst ctor - Create a new switch instruction, specifying a value to
|
|
|
|
/// switch on and a default destination. The number of additional cases can
|
|
|
|
/// be specified here to make memory allocation more efficient. This
|
|
|
|
/// constructor also autoinserts at the end of the specified BasicBlock.
|
|
|
|
SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
|
|
|
|
BasicBlock *InsertAtEnd)
|
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
|
|
|
|
init(Value, Default, NumCases);
|
|
|
|
}
|
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
SwitchInst::SwitchInst(const SwitchInst &SI)
|
2007-02-24 00:55:48 +00:00
|
|
|
: TerminatorInst(Type::VoidTy, Instruction::Switch,
|
2008-05-10 08:32:32 +00:00
|
|
|
allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
|
2005-01-29 00:35:16 +00:00
|
|
|
Use *OL = OperandList, *InOL = SI.OperandList;
|
|
|
|
for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
|
2008-05-26 21:33:52 +00:00
|
|
|
OL[i] = InOL[i];
|
|
|
|
OL[i+1] = InOL[i+1];
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-10 02:14:30 +00:00
|
|
|
SwitchInst::~SwitchInst() {
|
2008-05-10 08:32:32 +00:00
|
|
|
dropHungoffUses(OperandList);
|
2005-01-29 00:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-29 12:33:25 +00:00
|
|
|
/// addCase - Add an entry to the switch instruction...
|
|
|
|
///
|
2005-02-24 05:32:09 +00:00
|
|
|
void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
|
2005-01-29 00:35:16 +00:00
|
|
|
unsigned OpNo = NumOperands;
|
|
|
|
if (OpNo+2 > ReservedSpace)
|
|
|
|
resizeOperands(0); // Get more space!
|
|
|
|
// Initialize some new operands.
|
2005-01-29 01:05:12 +00:00
|
|
|
assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
|
2005-01-29 00:35:16 +00:00
|
|
|
NumOperands = OpNo+2;
|
2008-05-26 21:33:52 +00:00
|
|
|
OperandList[OpNo] = OnVal;
|
|
|
|
OperandList[OpNo+1] = Dest;
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// removeCase - This method removes the specified successor from the switch
|
|
|
|
/// instruction. Note that this cannot be used to remove the default
|
|
|
|
/// destination (successor #0).
|
|
|
|
///
|
|
|
|
void SwitchInst::removeCase(unsigned idx) {
|
|
|
|
assert(idx != 0 && "Cannot remove the default case!");
|
2005-01-29 00:35:16 +00:00
|
|
|
assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
|
|
|
|
|
|
|
|
unsigned NumOps = getNumOperands();
|
|
|
|
Use *OL = OperandList;
|
|
|
|
|
|
|
|
// Move everything after this operand down.
|
|
|
|
//
|
|
|
|
// FIXME: we could just swap with the end of the list, then erase. However,
|
|
|
|
// client might not expect this to happen. The code as it is thrashes the
|
|
|
|
// use/def lists, which is kinda lame.
|
|
|
|
for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
|
|
|
|
OL[i-2] = OL[i];
|
|
|
|
OL[i-2+1] = OL[i+1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nuke the last value.
|
|
|
|
OL[NumOps-2].set(0);
|
|
|
|
OL[NumOps-2+1].set(0);
|
|
|
|
NumOperands = NumOps-2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// resizeOperands - resize operands - This adjusts the length of the operands
|
|
|
|
/// list according to the following behavior:
|
|
|
|
/// 1. If NumOps == 0, grow the operand list in response to a push_back style
|
2008-05-10 08:32:32 +00:00
|
|
|
/// of operation. This grows the number of ops by 3 times.
|
2005-01-29 00:35:16 +00:00
|
|
|
/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
|
|
|
|
/// 3. If NumOps == NumOperands, trim the reserved space.
|
|
|
|
///
|
|
|
|
void SwitchInst::resizeOperands(unsigned NumOps) {
|
2008-05-10 08:32:32 +00:00
|
|
|
unsigned e = getNumOperands();
|
2005-01-29 00:35:16 +00:00
|
|
|
if (NumOps == 0) {
|
2008-05-10 08:32:32 +00:00
|
|
|
NumOps = e*3;
|
2005-01-29 00:35:16 +00:00
|
|
|
} else if (NumOps*2 > NumOperands) {
|
|
|
|
// No resize needed.
|
|
|
|
if (ReservedSpace >= NumOps) return;
|
|
|
|
} else if (NumOps == NumOperands) {
|
|
|
|
if (ReservedSpace == NumOps) return;
|
|
|
|
} else {
|
2005-01-29 01:05:12 +00:00
|
|
|
return;
|
2005-01-29 00:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ReservedSpace = NumOps;
|
2008-05-10 08:32:32 +00:00
|
|
|
Use *NewOps = allocHungoffUses(NumOps);
|
2005-01-29 00:35:16 +00:00
|
|
|
Use *OldOps = OperandList;
|
2008-05-10 08:32:32 +00:00
|
|
|
for (unsigned i = 0; i != e; ++i) {
|
2008-05-26 21:33:52 +00:00
|
|
|
NewOps[i] = OldOps[i];
|
2005-01-29 00:35:16 +00:00
|
|
|
}
|
|
|
|
OperandList = NewOps;
|
2008-05-10 08:32:32 +00:00
|
|
|
if (OldOps) Use::zap(OldOps, OldOps + e, true);
|
2005-01-29 00:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
|
|
|
|
return getSuccessor(idx);
|
|
|
|
}
|
|
|
|
unsigned SwitchInst::getNumSuccessorsV() const {
|
|
|
|
return getNumSuccessors();
|
|
|
|
}
|
|
|
|
void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
|
|
|
|
setSuccessor(idx, B);
|
2004-07-29 12:33:25 +00:00
|
|
|
}
|
2004-10-15 23:52:53 +00:00
|
|
|
|
2008-02-19 22:15:16 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// GetResultInst Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-20 19:10:47 +00:00
|
|
|
GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
|
2008-02-19 22:15:16 +00:00
|
|
|
const std::string &Name,
|
|
|
|
Instruction *InsertBef)
|
2008-05-13 07:09:08 +00:00
|
|
|
: UnaryInstruction(cast<StructType>(Aggregate->getType())
|
|
|
|
->getElementType(Index),
|
|
|
|
GetResult, Aggregate, InsertBef),
|
|
|
|
Idx(Index) {
|
|
|
|
assert(isValidOperands(Aggregate, Index)
|
|
|
|
&& "Invalid GetResultInst operands!");
|
2008-02-19 22:15:16 +00:00
|
|
|
setName(Name);
|
|
|
|
}
|
|
|
|
|
2008-02-20 19:10:47 +00:00
|
|
|
bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
|
|
|
|
if (!Aggregate)
|
2008-02-19 22:15:16 +00:00
|
|
|
return false;
|
2008-02-20 19:10:47 +00:00
|
|
|
|
2008-02-20 19:39:41 +00:00
|
|
|
if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
|
|
|
|
unsigned NumElements = STy->getNumElements();
|
2008-04-23 05:36:34 +00:00
|
|
|
if (Index >= NumElements || NumElements == 0)
|
2008-02-20 19:39:41 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// getresult aggregate value's element types are restricted to
|
|
|
|
// avoid nested aggregates.
|
|
|
|
for (unsigned i = 0; i < NumElements; ++i)
|
|
|
|
if (!STy->getElementType(i)->isFirstClassType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise, Aggregate is valid.
|
|
|
|
return true;
|
|
|
|
}
|
2008-02-20 19:10:47 +00:00
|
|
|
return false;
|
2008-02-19 22:15:16 +00:00
|
|
|
}
|
|
|
|
|
2004-10-15 23:52:53 +00:00
|
|
|
// Define these methods here so vtables don't get emitted into every translation
|
|
|
|
// unit that uses these classes.
|
|
|
|
|
|
|
|
GetElementPtrInst *GetElementPtrInst::clone() const {
|
2008-04-06 20:25:17 +00:00
|
|
|
return new(getNumOperands()) GetElementPtrInst(*this);
|
2004-10-15 23:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BinaryOperator *BinaryOperator::clone() const {
|
2008-05-16 19:29:10 +00:00
|
|
|
return Create(getOpcode(), Op<0>(), Op<1>());
|
2004-10-15 23:52:53 +00:00
|
|
|
}
|
|
|
|
|
2007-08-24 20:48:18 +00:00
|
|
|
FCmpInst* FCmpInst::clone() const {
|
2008-05-10 08:32:32 +00:00
|
|
|
return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
|
2007-08-24 20:48:18 +00:00
|
|
|
}
|
|
|
|
ICmpInst* ICmpInst::clone() const {
|
2008-05-10 08:32:32 +00:00
|
|
|
return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
|
2006-11-20 01:22:35 +00:00
|
|
|
}
|
|
|
|
|
2008-05-12 19:01:56 +00:00
|
|
|
VFCmpInst* VFCmpInst::clone() const {
|
|
|
|
return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
|
|
|
|
}
|
|
|
|
VICmpInst* VICmpInst::clone() const {
|
|
|
|
return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
|
|
|
|
}
|
|
|
|
|
2008-05-23 00:36:11 +00:00
|
|
|
ExtractValueInst *ExtractValueInst::clone() const {
|
|
|
|
return new(getNumOperands()) ExtractValueInst(*this);
|
|
|
|
}
|
|
|
|
InsertValueInst *InsertValueInst::clone() const {
|
|
|
|
return new(getNumOperands()) InsertValueInst(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
|
|
|
|
AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
|
|
|
|
FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
|
|
|
|
LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
|
|
|
|
StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
|
|
|
|
CastInst *TruncInst::clone() const { return new TruncInst(*this); }
|
|
|
|
CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
|
|
|
|
CastInst *SExtInst::clone() const { return new SExtInst(*this); }
|
|
|
|
CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
|
|
|
|
CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
|
|
|
|
CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
|
|
|
|
CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
|
|
|
|
CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
|
|
|
|
CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
|
|
|
|
CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
|
|
|
|
CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
|
|
|
|
CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
|
2008-05-15 10:04:30 +00:00
|
|
|
CallInst *CallInst::clone() const {
|
|
|
|
return new(getNumOperands()) CallInst(*this);
|
|
|
|
}
|
|
|
|
SelectInst *SelectInst::clone() const {
|
|
|
|
return new(getNumOperands()) SelectInst(*this);
|
|
|
|
}
|
2006-11-27 01:05:10 +00:00
|
|
|
VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
|
|
|
|
|
2006-04-08 01:18:18 +00:00
|
|
|
ExtractElementInst *ExtractElementInst::clone() const {
|
|
|
|
return new ExtractElementInst(*this);
|
|
|
|
}
|
|
|
|
InsertElementInst *InsertElementInst::clone() const {
|
2008-04-06 20:25:17 +00:00
|
|
|
return InsertElementInst::Create(*this);
|
2006-04-08 01:18:18 +00:00
|
|
|
}
|
|
|
|
ShuffleVectorInst *ShuffleVectorInst::clone() const {
|
|
|
|
return new ShuffleVectorInst(*this);
|
|
|
|
}
|
2004-10-15 23:52:53 +00:00
|
|
|
PHINode *PHINode::clone() const { return new PHINode(*this); }
|
2008-05-15 10:04:30 +00:00
|
|
|
ReturnInst *ReturnInst::clone() const {
|
|
|
|
return new(getNumOperands()) ReturnInst(*this);
|
|
|
|
}
|
|
|
|
BranchInst *BranchInst::clone() const {
|
|
|
|
return new(getNumOperands()) BranchInst(*this);
|
|
|
|
}
|
2008-05-10 08:32:32 +00:00
|
|
|
SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
|
2008-05-15 10:04:30 +00:00
|
|
|
InvokeInst *InvokeInst::clone() const {
|
|
|
|
return new(getNumOperands()) InvokeInst(*this);
|
|
|
|
}
|
2004-10-15 23:52:53 +00:00
|
|
|
UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
|
2004-10-16 18:08:06 +00:00
|
|
|
UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
|
2008-02-19 22:15:16 +00:00
|
|
|
GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }
|