blob: a12075fe0d9ff5c752bef9770a7bbc92efc46405 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000015#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Function.h"
18#include "llvm/Instructions.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000019#include "llvm/Operator.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000020#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000023#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024using namespace llvm;
25
Chris Lattner3e13b8c2008-01-02 23:42:30 +000026//===----------------------------------------------------------------------===//
27// CallSite Class
28//===----------------------------------------------------------------------===//
29
Gabor Greif1b9921a2009-01-11 22:33:22 +000030#define CALLSITE_DELEGATE_GETTER(METHOD) \
31 Instruction *II(getInstruction()); \
32 return isCall() \
33 ? cast<CallInst>(II)->METHOD \
34 : cast<InvokeInst>(II)->METHOD
35
36#define CALLSITE_DELEGATE_SETTER(METHOD) \
37 Instruction *II(getInstruction()); \
38 if (isCall()) \
39 cast<CallInst>(II)->METHOD; \
40 else \
41 cast<InvokeInst>(II)->METHOD
42
Duncan Sands85fab3a2008-02-18 17:32:13 +000043CallSite::CallSite(Instruction *C) {
44 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
Gabor Greif1b9921a2009-01-11 22:33:22 +000045 I.setPointer(C);
46 I.setInt(isa<CallInst>(C));
Duncan Sands85fab3a2008-02-18 17:32:13 +000047}
Sandeep Patel68c5f472009-09-02 08:44:58 +000048CallingConv::ID CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000049 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000050}
Sandeep Patel68c5f472009-09-02 08:44:58 +000051void CallSite::setCallingConv(CallingConv::ID CC) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000052 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
Chris Lattnerf7b6d312005-05-06 20:26:43 +000053}
Devang Patel4c758ea2008-09-25 21:00:45 +000054const AttrListPtr &CallSite::getAttributes() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000055 CALLSITE_DELEGATE_GETTER(getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000056}
Devang Patel4c758ea2008-09-25 21:00:45 +000057void CallSite::setAttributes(const AttrListPtr &PAL) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000058 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000059}
Devang Patelba3fa6c2008-09-23 23:03:40 +000060bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000061 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
Duncan Sands5208d1a2007-11-28 17:07:01 +000062}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000063uint16_t CallSite::getParamAlignment(uint16_t i) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000064 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
Dale Johanneseneabc5f32008-02-22 17:49:45 +000065}
Duncan Sands38ef3a82007-12-03 20:06:50 +000066bool CallSite::doesNotAccessMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000067 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000068}
Duncan Sands78c88722008-07-08 08:38:44 +000069void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000070 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000071}
Duncan Sands38ef3a82007-12-03 20:06:50 +000072bool CallSite::onlyReadsMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000073 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000074}
Duncan Sands78c88722008-07-08 08:38:44 +000075void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000076 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000077}
78bool CallSite::doesNotReturn() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000079 CALLSITE_DELEGATE_GETTER(doesNotReturn());
Duncan Sands78c88722008-07-08 08:38:44 +000080}
81void CallSite::setDoesNotReturn(bool doesNotReturn) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000082 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
Duncan Sands78c88722008-07-08 08:38:44 +000083}
Duncan Sands3353ed02007-12-18 09:59:50 +000084bool CallSite::doesNotThrow() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000085 CALLSITE_DELEGATE_GETTER(doesNotThrow());
Duncan Sands8e4847e2007-12-16 15:51:49 +000086}
Duncan Sandsaa31b922007-12-19 21:13:37 +000087void CallSite::setDoesNotThrow(bool doesNotThrow) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000088 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
Duncan Sandsaa31b922007-12-19 21:13:37 +000089}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000090
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000091bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000092 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
93 if (AI->get() == Arg)
94 return true;
95 return false;
96}
97
Gabor Greif1b9921a2009-01-11 22:33:22 +000098#undef CALLSITE_DELEGATE_GETTER
99#undef CALLSITE_DELEGATE_SETTER
100
Gordon Henriksen14a55692007-12-10 02:14:30 +0000101//===----------------------------------------------------------------------===//
102// TerminatorInst Class
103//===----------------------------------------------------------------------===//
104
105// Out of line virtual method, so the vtable, etc has a home.
106TerminatorInst::~TerminatorInst() {
107}
108
Gabor Greiff6caff662008-05-10 08:32:32 +0000109//===----------------------------------------------------------------------===//
110// UnaryInstruction Class
111//===----------------------------------------------------------------------===//
112
Gordon Henriksen14a55692007-12-10 02:14:30 +0000113// Out of line virtual method, so the vtable, etc has a home.
114UnaryInstruction::~UnaryInstruction() {
115}
116
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000118// SelectInst Class
119//===----------------------------------------------------------------------===//
120
121/// areInvalidOperands - Return a string if the specified operands are invalid
122/// for a select operation, otherwise return null.
123const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
124 if (Op1->getType() != Op2->getType())
125 return "both values to select must have same type";
126
127 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
128 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000129 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000130 return "vector select condition element type must be i1";
131 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
132 if (ET == 0)
133 return "selected values for vector select must be vectors";
134 if (ET->getNumElements() != VT->getNumElements())
135 return "vector select requires selected vectors to have "
136 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000137 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000138 return "select condition must be i1 or <n x i1>";
139 }
140 return 0;
141}
142
143
144//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000145// PHINode Class
146//===----------------------------------------------------------------------===//
147
148PHINode::PHINode(const PHINode &PN)
149 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000150 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000151 ReservedSpace(PN.getNumOperands()) {
152 Use *OL = OperandList;
153 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000154 OL[i] = PN.getOperand(i);
155 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000157 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000158}
159
Gordon Henriksen14a55692007-12-10 02:14:30 +0000160PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000161 if (OperandList)
162 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000163}
164
165// removeIncomingValue - Remove an incoming value. This is useful if a
166// predecessor basic block is deleted.
167Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
168 unsigned NumOps = getNumOperands();
169 Use *OL = OperandList;
170 assert(Idx*2 < NumOps && "BB not in PHI node!");
171 Value *Removed = OL[Idx*2];
172
173 // Move everything after this operand down.
174 //
175 // FIXME: we could just swap with the end of the list, then erase. However,
176 // client might not expect this to happen. The code as it is thrashes the
177 // use/def lists, which is kinda lame.
178 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
179 OL[i-2] = OL[i];
180 OL[i-2+1] = OL[i+1];
181 }
182
183 // Nuke the last value.
184 OL[NumOps-2].set(0);
185 OL[NumOps-2+1].set(0);
186 NumOperands = NumOps-2;
187
188 // If the PHI node is dead, because it has zero entries, nuke it now.
189 if (NumOps == 2 && DeletePHIIfEmpty) {
190 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000191 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000192 eraseFromParent();
193 }
194 return Removed;
195}
196
197/// resizeOperands - resize operands - This adjusts the length of the operands
198/// list according to the following behavior:
199/// 1. If NumOps == 0, grow the operand list in response to a push_back style
200/// of operation. This grows the number of ops by 1.5 times.
201/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
202/// 3. If NumOps == NumOperands, trim the reserved space.
203///
204void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000205 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000206 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000207 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000208 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
209 } else if (NumOps*2 > NumOperands) {
210 // No resize needed.
211 if (ReservedSpace >= NumOps) return;
212 } else if (NumOps == NumOperands) {
213 if (ReservedSpace == NumOps) return;
214 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000215 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000216 }
217
218 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000219 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000220 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000221 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000222 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000223 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000224}
225
Nate Begemanb3923212005-08-04 23:24:19 +0000226/// hasConstantValue - If the specified PHI node always merges together the same
227/// value, return the value, otherwise return null.
228///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000229Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000230 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000231 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000232 if (getIncomingValue(0) != this) // not X = phi X
233 return getIncomingValue(0);
234 else
Owen Andersonb292b8c2009-07-30 23:03:37 +0000235 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000236 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000237
Nate Begemanb3923212005-08-04 23:24:19 +0000238 // Otherwise if all of the incoming values are the same for the PHI, replace
239 // the PHI node with the incoming value.
240 //
241 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000242 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000243 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000244 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000245 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000246 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000247 if (InVal && getIncomingValue(i) != InVal)
248 return 0; // Not the same, bail out.
249 else
250 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000251 }
Nate Begemanb3923212005-08-04 23:24:19 +0000252
253 // The only case that could cause InVal to be null is if we have a PHI node
254 // that only has entries for itself. In this case, there is no entry into the
255 // loop, so kill the PHI.
256 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000257 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000258
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000259 // If we have a PHI node like phi(X, undef, X), where X is defined by some
260 // instruction, we cannot always return X as the result of the PHI node. Only
261 // do this if X is not an instruction (thus it must dominate the PHI block),
262 // or if the client is prepared to deal with this possibility.
263 if (HasUndefInput && !AllowNonDominatingInstruction)
264 if (Instruction *IV = dyn_cast<Instruction>(InVal))
265 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000266 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000267 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000268 return 0; // Cannot guarantee that InVal dominates this PHINode.
269
Nate Begemanb3923212005-08-04 23:24:19 +0000270 // All of the incoming values are the same, return the value now.
271 return InVal;
272}
273
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000274
275//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000276// CallInst Implementation
277//===----------------------------------------------------------------------===//
278
Gordon Henriksen14a55692007-12-10 02:14:30 +0000279CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000280}
281
Chris Lattner054ba2c2007-02-13 00:58:44 +0000282void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000283 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
284 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000285 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286
Misha Brukmanb1c93172005-04-21 23:48:37 +0000287 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000288 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000289 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290
Chris Lattner054ba2c2007-02-13 00:58:44 +0000291 assert((NumParams == FTy->getNumParams() ||
292 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000293 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000294 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000295 assert((i >= FTy->getNumParams() ||
296 FTy->getParamType(i) == Params[i]->getType()) &&
297 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000298 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000299 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300}
301
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000302void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000303 assert(NumOperands == 3 && "NumOperands not set up?");
304 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000305 OL[0] = Func;
306 OL[1] = Actual1;
307 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000308
309 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000310 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000311 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000313 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000314 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000316 assert((0 >= FTy->getNumParams() ||
317 FTy->getParamType(0) == Actual1->getType()) &&
318 "Calling a function with a bad signature!");
319 assert((1 >= FTy->getNumParams() ||
320 FTy->getParamType(1) == Actual2->getType()) &&
321 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322}
323
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000324void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000325 assert(NumOperands == 2 && "NumOperands not set up?");
326 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000327 OL[0] = Func;
328 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000329
330 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000332 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000333
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000334 assert((FTy->getNumParams() == 1 ||
335 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000337 assert((0 == FTy->getNumParams() ||
338 FTy->getParamType(0) == Actual->getType()) &&
339 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000340}
341
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000342void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000343 assert(NumOperands == 1 && "NumOperands not set up?");
344 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000345 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000346
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000347 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000349 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000351 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000352}
353
Daniel Dunbar4975db62009-07-25 04:41:11 +0000354CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000355 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000356 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
357 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000358 Instruction::Call,
359 OperandTraits<CallInst>::op_end(this) - 2,
360 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000361 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000362 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000363}
364
Daniel Dunbar4975db62009-07-25 04:41:11 +0000365CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000366 BasicBlock *InsertAtEnd)
367 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
368 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000369 Instruction::Call,
370 OperandTraits<CallInst>::op_end(this) - 2,
371 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000372 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000373 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000374}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000375CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000376 Instruction *InsertBefore)
377 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
378 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000379 Instruction::Call,
380 OperandTraits<CallInst>::op_end(this) - 1,
381 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000382 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000383 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000384}
385
Daniel Dunbar4975db62009-07-25 04:41:11 +0000386CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000387 BasicBlock *InsertAtEnd)
388 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
389 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000390 Instruction::Call,
391 OperandTraits<CallInst>::op_end(this) - 1,
392 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000393 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000394 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000395}
396
Misha Brukmanb1c93172005-04-21 23:48:37 +0000397CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000398 : Instruction(CI.getType(), Instruction::Call,
399 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000400 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000401 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000402 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000403 Use *OL = OperandList;
404 Use *InOL = CI.OperandList;
405 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000406 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000407 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000408}
409
Devang Patel4c758ea2008-09-25 21:00:45 +0000410void CallInst::addAttribute(unsigned i, Attributes attr) {
411 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000412 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000413 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000414}
415
Devang Patel4c758ea2008-09-25 21:00:45 +0000416void CallInst::removeAttribute(unsigned i, Attributes attr) {
417 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000418 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000419 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000420}
421
Devang Patelba3fa6c2008-09-23 23:03:40 +0000422bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000423 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000424 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000425 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000426 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000427 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000428}
429
Duncan Sands5208d1a2007-11-28 17:07:01 +0000430
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000431//===----------------------------------------------------------------------===//
432// InvokeInst Implementation
433//===----------------------------------------------------------------------===//
434
435void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000436 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000437 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Bill Wendling4bb96e92009-03-13 21:15:59 +0000438 Use *OL = OperandList;
439 OL[0] = Fn;
440 OL[1] = IfNormal;
441 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000442 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000443 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000444 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000445
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000446 assert(((NumArgs == FTy->getNumParams()) ||
447 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000448 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000449
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000450 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000451 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000452 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000453 "Invoking a function with a bad signature!");
454
Bill Wendling4bb96e92009-03-13 21:15:59 +0000455 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000456 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000457}
458
Misha Brukmanb1c93172005-04-21 23:48:37 +0000459InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000460 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000461 OperandTraits<InvokeInst>::op_end(this)
462 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000463 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000464 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000465 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000466 Use *OL = OperandList, *InOL = II.OperandList;
467 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000468 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000469 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000470}
471
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000472BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
473 return getSuccessor(idx);
474}
475unsigned InvokeInst::getNumSuccessorsV() const {
476 return getNumSuccessors();
477}
478void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
479 return setSuccessor(idx, B);
480}
481
Devang Patelba3fa6c2008-09-23 23:03:40 +0000482bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000483 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000484 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000485 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000486 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000487 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000488}
489
Devang Patel4c758ea2008-09-25 21:00:45 +0000490void InvokeInst::addAttribute(unsigned i, Attributes attr) {
491 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000492 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000493 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000494}
495
Devang Patel4c758ea2008-09-25 21:00:45 +0000496void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
497 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000498 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000499 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000500}
501
Duncan Sands5208d1a2007-11-28 17:07:01 +0000502
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000503//===----------------------------------------------------------------------===//
504// ReturnInst Implementation
505//===----------------------------------------------------------------------===//
506
Chris Lattner2195fc42007-02-24 00:55:48 +0000507ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000508 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000509 OperandTraits<ReturnInst>::op_end(this) -
510 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000511 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000512 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000513 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000514 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000515}
516
Owen Anderson55f1c092009-08-13 21:58:54 +0000517ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
518 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000519 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
520 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000521 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000522 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000523}
Owen Anderson55f1c092009-08-13 21:58:54 +0000524ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
525 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000526 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
527 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000528 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000529 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000530}
Owen Anderson55f1c092009-08-13 21:58:54 +0000531ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
532 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000533 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000534}
535
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000536unsigned ReturnInst::getNumSuccessorsV() const {
537 return getNumSuccessors();
538}
539
Devang Patelae682fb2008-02-26 17:56:20 +0000540/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
541/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000542void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000543 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000544}
545
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000546BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000547 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000548 return 0;
549}
550
Devang Patel59643e52008-02-23 00:35:18 +0000551ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000552}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000553
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000554//===----------------------------------------------------------------------===//
555// UnwindInst Implementation
556//===----------------------------------------------------------------------===//
557
Owen Anderson55f1c092009-08-13 21:58:54 +0000558UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
559 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
560 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000561}
Owen Anderson55f1c092009-08-13 21:58:54 +0000562UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
563 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
564 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000565}
566
567
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000568unsigned UnwindInst::getNumSuccessorsV() const {
569 return getNumSuccessors();
570}
571
572void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000573 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000574}
575
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000576BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000577 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000578 return 0;
579}
580
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000581//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000582// UnreachableInst Implementation
583//===----------------------------------------------------------------------===//
584
Owen Anderson55f1c092009-08-13 21:58:54 +0000585UnreachableInst::UnreachableInst(LLVMContext &Context,
586 Instruction *InsertBefore)
587 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
588 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000589}
Owen Anderson55f1c092009-08-13 21:58:54 +0000590UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
591 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
592 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000593}
594
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000595unsigned UnreachableInst::getNumSuccessorsV() const {
596 return getNumSuccessors();
597}
598
599void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000600 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000601}
602
603BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000604 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000605 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000606}
607
608//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000609// BranchInst Implementation
610//===----------------------------------------------------------------------===//
611
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000612void BranchInst::AssertOK() {
613 if (isConditional())
Owen Anderson55f1c092009-08-13 21:58:54 +0000614 assert(getCondition()->getType() == Type::getInt1Ty(getContext()) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000615 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000616}
617
Chris Lattner2195fc42007-02-24 00:55:48 +0000618BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000619 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000620 OperandTraits<BranchInst>::op_end(this) - 1,
621 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000622 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000623 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000624}
625BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
626 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000627 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000628 OperandTraits<BranchInst>::op_end(this) - 3,
629 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000630 Op<-1>() = IfTrue;
631 Op<-2>() = IfFalse;
632 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000633#ifndef NDEBUG
634 AssertOK();
635#endif
636}
637
638BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000639 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000640 OperandTraits<BranchInst>::op_end(this) - 1,
641 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000642 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000643 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000644}
645
646BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
647 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000648 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000649 OperandTraits<BranchInst>::op_end(this) - 3,
650 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000651 Op<-1>() = IfTrue;
652 Op<-2>() = IfFalse;
653 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000654#ifndef NDEBUG
655 AssertOK();
656#endif
657}
658
659
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000660BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000661 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000662 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
663 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000664 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000665 if (BI.getNumOperands() != 1) {
666 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000667 Op<-3>() = BI.Op<-3>();
668 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000669 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000670 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000671}
672
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000673
674Use* Use::getPrefix() {
675 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
676 if (PotentialPrefix.getOpaqueValue())
677 return 0;
678
679 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
680}
681
682BranchInst::~BranchInst() {
683 if (NumOperands == 1) {
684 if (Use *Prefix = OperandList->getPrefix()) {
685 Op<-1>() = 0;
686 //
687 // mark OperandList to have a special value for scrutiny
688 // by baseclass destructors and operator delete
689 OperandList = Prefix;
690 } else {
691 NumOperands = 3;
692 OperandList = op_begin();
693 }
694 }
695}
696
697
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000698BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
699 return getSuccessor(idx);
700}
701unsigned BranchInst::getNumSuccessorsV() const {
702 return getNumSuccessors();
703}
704void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
705 setSuccessor(idx, B);
706}
707
708
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000709//===----------------------------------------------------------------------===//
710// AllocationInst Implementation
711//===----------------------------------------------------------------------===//
712
Owen Andersonb6b25302009-07-14 23:09:55 +0000713static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000714 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000715 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000716 else {
717 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000718 "Passed basic block into allocation size parameter! Use other ctor");
Owen Anderson55f1c092009-08-13 21:58:54 +0000719 assert(Amt->getType() == Type::getInt32Ty(Context) &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000720 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000721 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000722 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000723}
724
Owen Anderson4fdeba92009-07-15 23:53:25 +0000725AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000726 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000727 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +0000728 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000729 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000730 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000731 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000732 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733}
734
Owen Anderson4fdeba92009-07-15 23:53:25 +0000735AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000736 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000737 BasicBlock *InsertAtEnd)
Owen Anderson4056ca92009-07-29 22:17:13 +0000738 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000739 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000740 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000741 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000742 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000743}
744
Gordon Henriksen14a55692007-12-10 02:14:30 +0000745// Out of line virtual method, so the vtable, etc has a home.
746AllocationInst::~AllocationInst() {
747}
748
Dan Gohmanaa583d72008-03-24 16:55:58 +0000749void AllocationInst::setAlignment(unsigned Align) {
750 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
751 SubclassData = Log2_32(Align) + 1;
752 assert(getAlignment() == Align && "Alignment representation error!");
753}
754
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000755bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000756 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
757 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000758 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000759}
760
761const Type *AllocationInst::getAllocatedType() const {
762 return getType()->getElementType();
763}
764
Chris Lattner8b291e62008-11-26 02:54:17 +0000765/// isStaticAlloca - Return true if this alloca is in the entry block of the
766/// function and is a constant size. If so, the code generator will fold it
767/// into the prolog/epilog code, so it is basically free.
768bool AllocaInst::isStaticAlloca() const {
769 // Must be constant size.
770 if (!isa<ConstantInt>(getArraySize())) return false;
771
772 // Must be in the entry block.
773 const BasicBlock *Parent = getParent();
774 return Parent == &Parent->getParent()->front();
775}
776
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000777//===----------------------------------------------------------------------===//
778// FreeInst Implementation
779//===----------------------------------------------------------------------===//
780
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000781void FreeInst::AssertOK() {
782 assert(isa<PointerType>(getOperand(0)->getType()) &&
783 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000784}
785
786FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000787 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
788 Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000789 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000790}
791
792FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000793 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
794 Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000795 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000796}
797
798
799//===----------------------------------------------------------------------===//
800// LoadInst Implementation
801//===----------------------------------------------------------------------===//
802
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000803void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000804 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000805 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000806}
807
Daniel Dunbar4975db62009-07-25 04:41:11 +0000808LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000809 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000810 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000811 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000812 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000813 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000814 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000815}
816
Daniel Dunbar4975db62009-07-25 04:41:11 +0000817LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000818 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000819 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000820 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000821 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000822 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000823 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000824}
825
Daniel Dunbar4975db62009-07-25 04:41:11 +0000826LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000827 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000828 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000829 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000830 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000831 setAlignment(0);
832 AssertOK();
833 setName(Name);
834}
835
Daniel Dunbar4975db62009-07-25 04:41:11 +0000836LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000837 unsigned Align, Instruction *InsertBef)
838 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
839 Load, Ptr, InsertBef) {
840 setVolatile(isVolatile);
841 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000842 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000843 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000844}
845
Daniel Dunbar4975db62009-07-25 04:41:11 +0000846LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000847 unsigned Align, BasicBlock *InsertAE)
848 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
849 Load, Ptr, InsertAE) {
850 setVolatile(isVolatile);
851 setAlignment(Align);
852 AssertOK();
853 setName(Name);
854}
855
Daniel Dunbar4975db62009-07-25 04:41:11 +0000856LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000857 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000858 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000859 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000860 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000861 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000862 AssertOK();
863 setName(Name);
864}
865
Daniel Dunbar27096822009-08-11 18:11:15 +0000866
867
868LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
869 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
870 Load, Ptr, InsertBef) {
871 setVolatile(false);
872 setAlignment(0);
873 AssertOK();
874 if (Name && Name[0]) setName(Name);
875}
876
877LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
878 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
879 Load, Ptr, InsertAE) {
880 setVolatile(false);
881 setAlignment(0);
882 AssertOK();
883 if (Name && Name[0]) setName(Name);
884}
885
886LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
887 Instruction *InsertBef)
888: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
889 Load, Ptr, InsertBef) {
890 setVolatile(isVolatile);
891 setAlignment(0);
892 AssertOK();
893 if (Name && Name[0]) setName(Name);
894}
895
896LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
897 BasicBlock *InsertAE)
898 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
899 Load, Ptr, InsertAE) {
900 setVolatile(isVolatile);
901 setAlignment(0);
902 AssertOK();
903 if (Name && Name[0]) setName(Name);
904}
905
Christopher Lamb84485702007-04-22 19:24:39 +0000906void LoadInst::setAlignment(unsigned Align) {
907 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
908 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
909}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000910
911//===----------------------------------------------------------------------===//
912// StoreInst Implementation
913//===----------------------------------------------------------------------===//
914
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000915void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000916 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000917 assert(isa<PointerType>(getOperand(1)->getType()) &&
918 "Ptr must have pointer type!");
919 assert(getOperand(0)->getType() ==
920 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000921 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000922}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000923
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000924
925StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000926 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000927 OperandTraits<StoreInst>::op_begin(this),
928 OperandTraits<StoreInst>::operands(this),
929 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000930 Op<0>() = val;
931 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000932 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000933 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000934 AssertOK();
935}
936
937StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000938 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000939 OperandTraits<StoreInst>::op_begin(this),
940 OperandTraits<StoreInst>::operands(this),
941 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000942 Op<0>() = val;
943 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000944 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000945 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000946 AssertOK();
947}
948
Misha Brukmanb1c93172005-04-21 23:48:37 +0000949StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000950 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000951 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000952 OperandTraits<StoreInst>::op_begin(this),
953 OperandTraits<StoreInst>::operands(this),
954 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000955 Op<0>() = val;
956 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000957 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000958 setAlignment(0);
959 AssertOK();
960}
961
962StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
963 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000964 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000965 OperandTraits<StoreInst>::op_begin(this),
966 OperandTraits<StoreInst>::operands(this),
967 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000968 Op<0>() = val;
969 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000970 setVolatile(isVolatile);
971 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000972 AssertOK();
973}
974
Misha Brukmanb1c93172005-04-21 23:48:37 +0000975StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000976 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000977 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000978 OperandTraits<StoreInst>::op_begin(this),
979 OperandTraits<StoreInst>::operands(this),
980 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000981 Op<0>() = val;
982 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000983 setVolatile(isVolatile);
984 setAlignment(Align);
985 AssertOK();
986}
987
988StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000989 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000990 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000991 OperandTraits<StoreInst>::op_begin(this),
992 OperandTraits<StoreInst>::operands(this),
993 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000994 Op<0>() = val;
995 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000996 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000997 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000998 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000999}
1000
Christopher Lamb84485702007-04-22 19:24:39 +00001001void StoreInst::setAlignment(unsigned Align) {
1002 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1003 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1004}
1005
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001006//===----------------------------------------------------------------------===//
1007// GetElementPtrInst Implementation
1008//===----------------------------------------------------------------------===//
1009
Christopher Lambedf07882007-12-17 01:12:55 +00001010static unsigned retrieveAddrSpace(const Value *Val) {
1011 return cast<PointerType>(Val->getType())->getAddressSpace();
1012}
1013
Gabor Greif21ba1842008-06-06 20:28:12 +00001014void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001015 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001016 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1017 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001018 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001019
Chris Lattner79807c3d2007-01-31 19:47:18 +00001020 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001021 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001022
1023 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001024}
1025
Daniel Dunbar4975db62009-07-25 04:41:11 +00001026void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001027 assert(NumOperands == 2 && "NumOperands not initialized?");
1028 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001029 OL[0] = Ptr;
1030 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001031
1032 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001033}
1034
Gabor Greiff6caff662008-05-10 08:32:32 +00001035GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001036 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001037 OperandTraits<GetElementPtrInst>::op_end(this)
1038 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001039 GEPI.getNumOperands()) {
1040 Use *OL = OperandList;
1041 Use *GEPIOL = GEPI.OperandList;
1042 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001043 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001044 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001045}
1046
Chris Lattner82981202005-05-03 05:43:30 +00001047GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001048 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001049 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001050 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001051 GetElementPtr,
1052 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1053 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001054 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001055}
1056
1057GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001058 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001059 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001060 checkType(getIndexedType(Ptr->getType(),Idx)),
1061 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001062 GetElementPtr,
1063 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1064 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001065 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001066}
1067
Chris Lattner6090a422009-03-09 04:46:40 +00001068/// getIndexedType - Returns the type of the element that would be accessed with
1069/// a gep instruction with the specified parameters.
1070///
1071/// The Idxs pointer should point to a continuous piece of memory containing the
1072/// indices, either as Value* or uint64_t.
1073///
1074/// A null type is returned if the indices are invalid for the specified
1075/// pointer type.
1076///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001077template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001078static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1079 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001080 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1081 if (!PTy) return 0; // Type isn't a pointer type!
1082 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001083
Chris Lattner6090a422009-03-09 04:46:40 +00001084 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001085 if (NumIdx == 0)
1086 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001087
1088 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001089 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1090 // that contain opaque types) under the assumption that it will be resolved to
1091 // a sane type later.
1092 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001093 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001094
Dan Gohman1ecaf452008-05-31 00:58:22 +00001095 unsigned CurIdx = 1;
1096 for (; CurIdx != NumIdx; ++CurIdx) {
1097 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1098 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001099 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001100 if (!CT->indexValid(Index)) return 0;
1101 Agg = CT->getTypeAtIndex(Index);
1102
1103 // If the new type forwards to another type, then it is in the middle
1104 // of being refined to another type (and hence, may have dropped all
1105 // references to what it was using before). So, use the new forwarded
1106 // type.
1107 if (const Type *Ty = Agg->getForwardedType())
1108 Agg = Ty;
1109 }
1110 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001111}
1112
Matthijs Kooijman04468622008-07-29 08:46:11 +00001113const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1114 Value* const *Idxs,
1115 unsigned NumIdx) {
1116 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1117}
1118
1119const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1120 uint64_t const *Idxs,
1121 unsigned NumIdx) {
1122 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1123}
1124
Chris Lattner82981202005-05-03 05:43:30 +00001125const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1126 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1127 if (!PTy) return 0; // Type isn't a pointer type!
1128
1129 // Check the pointer index.
1130 if (!PTy->indexValid(Idx)) return 0;
1131
Chris Lattnerc2233332005-05-03 16:44:45 +00001132 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001133}
1134
Chris Lattner45f15572007-04-14 00:12:57 +00001135
1136/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1137/// zeros. If so, the result pointer and the first operand have the same
1138/// value, just potentially different types.
1139bool GetElementPtrInst::hasAllZeroIndices() const {
1140 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1141 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1142 if (!CI->isZero()) return false;
1143 } else {
1144 return false;
1145 }
1146 }
1147 return true;
1148}
1149
Chris Lattner27058292007-04-27 20:35:56 +00001150/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1151/// constant integers. If so, the result pointer and the first operand have
1152/// a constant offset between them.
1153bool GetElementPtrInst::hasAllConstantIndices() const {
1154 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1155 if (!isa<ConstantInt>(getOperand(i)))
1156 return false;
1157 }
1158 return true;
1159}
1160
Chris Lattner45f15572007-04-14 00:12:57 +00001161
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001162//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001163// ExtractElementInst Implementation
1164//===----------------------------------------------------------------------===//
1165
1166ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001167 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001168 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001169 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001170 ExtractElement,
1171 OperandTraits<ExtractElementInst>::op_begin(this),
1172 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001173 assert(isValidOperands(Val, Index) &&
1174 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001175 Op<0>() = Val;
1176 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001177 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001178}
1179
1180ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001181 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001182 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001183 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001184 ExtractElement,
1185 OperandTraits<ExtractElementInst>::op_begin(this),
1186 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001187 assert(isValidOperands(Val, Index) &&
1188 "Invalid extractelement instruction operands!");
1189
Gabor Greif2d3024d2008-05-26 21:33:52 +00001190 Op<0>() = Val;
1191 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001192 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001193}
1194
Chris Lattner65511ff2006-10-05 06:24:58 +00001195
Chris Lattner54865b32006-04-08 04:05:48 +00001196bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Owen Anderson55f1c092009-08-13 21:58:54 +00001197 if (!isa<VectorType>(Val->getType()) ||
1198 Index->getType() != Type::getInt32Ty(Val->getContext()))
Chris Lattner54865b32006-04-08 04:05:48 +00001199 return false;
1200 return true;
1201}
1202
1203
Robert Bocchino23004482006-01-10 19:05:34 +00001204//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001205// InsertElementInst Implementation
1206//===----------------------------------------------------------------------===//
1207
Chris Lattner54865b32006-04-08 04:05:48 +00001208InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001209 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001210 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001211 : Instruction(Vec->getType(), InsertElement,
1212 OperandTraits<InsertElementInst>::op_begin(this),
1213 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001214 assert(isValidOperands(Vec, Elt, Index) &&
1215 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001216 Op<0>() = Vec;
1217 Op<1>() = Elt;
1218 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001219 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001220}
1221
Chris Lattner54865b32006-04-08 04:05:48 +00001222InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001223 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001224 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001225 : Instruction(Vec->getType(), InsertElement,
1226 OperandTraits<InsertElementInst>::op_begin(this),
1227 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001228 assert(isValidOperands(Vec, Elt, Index) &&
1229 "Invalid insertelement instruction operands!");
1230
Gabor Greif2d3024d2008-05-26 21:33:52 +00001231 Op<0>() = Vec;
1232 Op<1>() = Elt;
1233 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001234 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001235}
1236
Chris Lattner54865b32006-04-08 04:05:48 +00001237bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1238 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001239 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001240 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001241
Reid Spencerd84d35b2007-02-15 02:26:10 +00001242 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001243 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001244
Owen Anderson55f1c092009-08-13 21:58:54 +00001245 if (Index->getType() != Type::getInt32Ty(Vec->getContext()))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001246 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001247 return true;
1248}
1249
1250
Robert Bocchinoca27f032006-01-17 20:07:22 +00001251//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001252// ShuffleVectorInst Implementation
1253//===----------------------------------------------------------------------===//
1254
1255ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001256 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001257 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001258: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001259 cast<VectorType>(Mask->getType())->getNumElements()),
1260 ShuffleVector,
1261 OperandTraits<ShuffleVectorInst>::op_begin(this),
1262 OperandTraits<ShuffleVectorInst>::operands(this),
1263 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001264 assert(isValidOperands(V1, V2, Mask) &&
1265 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001266 Op<0>() = V1;
1267 Op<1>() = V2;
1268 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001269 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001270}
1271
1272ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001273 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001274 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001275: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1276 cast<VectorType>(Mask->getType())->getNumElements()),
1277 ShuffleVector,
1278 OperandTraits<ShuffleVectorInst>::op_begin(this),
1279 OperandTraits<ShuffleVectorInst>::operands(this),
1280 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001281 assert(isValidOperands(V1, V2, Mask) &&
1282 "Invalid shuffle vector instruction operands!");
1283
Gabor Greif2d3024d2008-05-26 21:33:52 +00001284 Op<0>() = V1;
1285 Op<1>() = V2;
1286 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001287 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001288}
1289
Mon P Wang25f01062008-11-10 04:46:22 +00001290bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001291 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001292 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001293 return false;
1294
1295 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1296 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Owen Anderson55f1c092009-08-13 21:58:54 +00001297 MaskTy->getElementType() != Type::getInt32Ty(V1->getContext()))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001298 return false;
1299 return true;
1300}
1301
Chris Lattnerf724e342008-03-02 05:28:33 +00001302/// getMaskValue - Return the index from the shuffle mask for the specified
1303/// output result. This is either -1 if the element is undef or a number less
1304/// than 2*numelements.
1305int ShuffleVectorInst::getMaskValue(unsigned i) const {
1306 const Constant *Mask = cast<Constant>(getOperand(2));
1307 if (isa<UndefValue>(Mask)) return -1;
1308 if (isa<ConstantAggregateZero>(Mask)) return 0;
1309 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1310 assert(i < MaskCV->getNumOperands() && "Index out of range");
1311
1312 if (isa<UndefValue>(MaskCV->getOperand(i)))
1313 return -1;
1314 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1315}
1316
Dan Gohman12fce772008-05-15 19:50:34 +00001317//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001318// InsertValueInst Class
1319//===----------------------------------------------------------------------===//
1320
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001321void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001322 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001323 assert(NumOperands == 2 && "NumOperands not initialized?");
1324 Op<0>() = Agg;
1325 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001326
Dan Gohman1ecaf452008-05-31 00:58:22 +00001327 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001328 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001329}
1330
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001331void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001332 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001333 assert(NumOperands == 2 && "NumOperands not initialized?");
1334 Op<0>() = Agg;
1335 Op<1>() = Val;
1336
1337 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001338 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001339}
1340
1341InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001342 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001343 OperandTraits<InsertValueInst>::op_begin(this), 2),
1344 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001345 Op<0>() = IVI.getOperand(0);
1346 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001347 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001348}
1349
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001350InsertValueInst::InsertValueInst(Value *Agg,
1351 Value *Val,
1352 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001353 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001354 Instruction *InsertBefore)
1355 : Instruction(Agg->getType(), InsertValue,
1356 OperandTraits<InsertValueInst>::op_begin(this),
1357 2, InsertBefore) {
1358 init(Agg, Val, Idx, Name);
1359}
1360
1361InsertValueInst::InsertValueInst(Value *Agg,
1362 Value *Val,
1363 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001364 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001365 BasicBlock *InsertAtEnd)
1366 : Instruction(Agg->getType(), InsertValue,
1367 OperandTraits<InsertValueInst>::op_begin(this),
1368 2, InsertAtEnd) {
1369 init(Agg, Val, Idx, Name);
1370}
1371
Dan Gohman0752bff2008-05-23 00:36:11 +00001372//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001373// ExtractValueInst Class
1374//===----------------------------------------------------------------------===//
1375
Gabor Greifcbcc4952008-06-06 21:06:32 +00001376void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001377 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001378 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001379
Dan Gohman1ecaf452008-05-31 00:58:22 +00001380 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001381 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001382}
1383
Daniel Dunbar4975db62009-07-25 04:41:11 +00001384void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001385 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001386
1387 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001388 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001389}
1390
1391ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001392 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001393 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001394 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001395}
1396
Dan Gohman12fce772008-05-15 19:50:34 +00001397// getIndexedType - Returns the type of the element that would be extracted
1398// with an extractvalue instruction with the specified parameters.
1399//
1400// A null type is returned if the indices are invalid for the specified
1401// pointer type.
1402//
1403const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001404 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001405 unsigned NumIdx) {
1406 unsigned CurIdx = 0;
1407 for (; CurIdx != NumIdx; ++CurIdx) {
1408 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001409 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1410 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001411 if (!CT->indexValid(Index)) return 0;
1412 Agg = CT->getTypeAtIndex(Index);
1413
1414 // If the new type forwards to another type, then it is in the middle
1415 // of being refined to another type (and hence, may have dropped all
1416 // references to what it was using before). So, use the new forwarded
1417 // type.
1418 if (const Type *Ty = Agg->getForwardedType())
1419 Agg = Ty;
1420 }
1421 return CurIdx == NumIdx ? Agg : 0;
1422}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001423
Dan Gohman4a3125b2008-06-17 21:07:55 +00001424const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001425 unsigned Idx) {
1426 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001427}
1428
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001429//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001430// BinaryOperator Class
1431//===----------------------------------------------------------------------===//
1432
Dan Gohmana5b96452009-06-04 22:49:04 +00001433/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1434/// type is floating-point, to help provide compatibility with an older API.
1435///
1436static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1437 const Type *Ty) {
1438 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1439 if (Ty->isFPOrFPVector()) {
1440 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1441 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1442 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1443 }
1444 return iType;
1445}
1446
Chris Lattner2195fc42007-02-24 00:55:48 +00001447BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001448 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001449 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001450 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001451 OperandTraits<BinaryOperator>::op_begin(this),
1452 OperandTraits<BinaryOperator>::operands(this),
1453 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001454 Op<0>() = S1;
1455 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001456 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001457 setName(Name);
1458}
1459
1460BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001461 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001462 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001463 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001464 OperandTraits<BinaryOperator>::op_begin(this),
1465 OperandTraits<BinaryOperator>::operands(this),
1466 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001467 Op<0>() = S1;
1468 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001469 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001470 setName(Name);
1471}
1472
1473
1474void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001475 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001476 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001477 assert(LHS->getType() == RHS->getType() &&
1478 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001479#ifndef NDEBUG
1480 switch (iType) {
1481 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001482 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001483 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001484 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001485 assert(getType()->isIntOrIntVector() &&
1486 "Tried to create an integer operation on a non-integer type!");
1487 break;
1488 case FAdd: case FSub:
1489 case FMul:
1490 assert(getType() == LHS->getType() &&
1491 "Arithmetic operation should return same type as operands!");
1492 assert(getType()->isFPOrFPVector() &&
1493 "Tried to create a floating-point operation on a "
1494 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001495 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001496 case UDiv:
1497 case SDiv:
1498 assert(getType() == LHS->getType() &&
1499 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001500 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1501 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001502 "Incorrect operand type (not integer) for S/UDIV");
1503 break;
1504 case FDiv:
1505 assert(getType() == LHS->getType() &&
1506 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001507 assert(getType()->isFPOrFPVector() &&
1508 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001509 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001510 case URem:
1511 case SRem:
1512 assert(getType() == LHS->getType() &&
1513 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001514 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1515 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001516 "Incorrect operand type (not integer) for S/UREM");
1517 break;
1518 case FRem:
1519 assert(getType() == LHS->getType() &&
1520 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001521 assert(getType()->isFPOrFPVector() &&
1522 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001523 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001524 case Shl:
1525 case LShr:
1526 case AShr:
1527 assert(getType() == LHS->getType() &&
1528 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001529 assert((getType()->isInteger() ||
1530 (isa<VectorType>(getType()) &&
1531 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1532 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001533 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001534 case And: case Or:
1535 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001536 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001537 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001538 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001539 (isa<VectorType>(getType()) &&
1540 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001541 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001542 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001543 default:
1544 break;
1545 }
1546#endif
1547}
1548
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001549BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001550 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001551 Instruction *InsertBefore) {
1552 assert(S1->getType() == S2->getType() &&
1553 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001554 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001555}
1556
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001557BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001558 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001559 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001560 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001561 InsertAtEnd->getInstList().push_back(Res);
1562 return Res;
1563}
1564
Dan Gohman5476cfd2009-08-12 16:23:25 +00001565BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001566 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001567 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001568 return new BinaryOperator(Instruction::Sub,
1569 zero, Op,
1570 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001571}
1572
Dan Gohman5476cfd2009-08-12 16:23:25 +00001573BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001574 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001575 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001576 return new BinaryOperator(Instruction::Sub,
1577 zero, Op,
1578 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001579}
1580
Dan Gohman5476cfd2009-08-12 16:23:25 +00001581BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001582 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001583 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001584 return new BinaryOperator(Instruction::FSub,
1585 zero, Op,
1586 Op->getType(), Name, InsertBefore);
1587}
1588
Dan Gohman5476cfd2009-08-12 16:23:25 +00001589BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001590 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001591 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001592 return new BinaryOperator(Instruction::FSub,
1593 zero, Op,
1594 Op->getType(), Name, InsertAtEnd);
1595}
1596
Dan Gohman5476cfd2009-08-12 16:23:25 +00001597BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001598 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001599 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001600 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001601 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001602 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001603 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001604 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001605 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001606 }
1607
1608 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001609 Op->getType(), Name, InsertBefore);
1610}
1611
Dan Gohman5476cfd2009-08-12 16:23:25 +00001612BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001613 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001614 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001615 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001616 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001617 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001618 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001619 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001620 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001621 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001622 }
1623
1624 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001625 Op->getType(), Name, InsertAtEnd);
1626}
1627
1628
1629// isConstantAllOnes - Helper function for several functions below
1630static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001631 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1632 return CI->isAllOnesValue();
1633 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1634 return CV->isAllOnesValue();
1635 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001636}
1637
Owen Andersonbb2501b2009-07-13 22:18:28 +00001638bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001639 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1640 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001641 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1642 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001643 return false;
1644}
1645
Owen Andersonbb2501b2009-07-13 22:18:28 +00001646bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001647 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1648 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001649 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1650 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001651 return false;
1652}
1653
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001654bool BinaryOperator::isNot(const Value *V) {
1655 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1656 return (Bop->getOpcode() == Instruction::Xor &&
1657 (isConstantAllOnes(Bop->getOperand(1)) ||
1658 isConstantAllOnes(Bop->getOperand(0))));
1659 return false;
1660}
1661
Chris Lattner2c7d1772005-04-24 07:28:37 +00001662Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001663 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001664}
1665
Chris Lattner2c7d1772005-04-24 07:28:37 +00001666const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1667 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001668}
1669
Dan Gohmana5b96452009-06-04 22:49:04 +00001670Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001671 return cast<BinaryOperator>(BinOp)->getOperand(1);
1672}
1673
1674const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1675 return getFNegArgument(const_cast<Value*>(BinOp));
1676}
1677
Chris Lattner2c7d1772005-04-24 07:28:37 +00001678Value *BinaryOperator::getNotArgument(Value *BinOp) {
1679 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1680 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1681 Value *Op0 = BO->getOperand(0);
1682 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001683 if (isConstantAllOnes(Op0)) return Op1;
1684
1685 assert(isConstantAllOnes(Op1));
1686 return Op0;
1687}
1688
Chris Lattner2c7d1772005-04-24 07:28:37 +00001689const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1690 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001691}
1692
1693
1694// swapOperands - Exchange the two operands to this instruction. This
1695// instruction is safe to use on any binary instruction and does not
1696// modify the semantics of the instruction. If the instruction is
1697// order dependent (SetLT f.e.) the opcode is changed.
1698//
1699bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001700 if (!isCommutative())
1701 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001702 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001703 return false;
1704}
1705
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001706//===----------------------------------------------------------------------===//
1707// CastInst Class
1708//===----------------------------------------------------------------------===//
1709
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001710// Just determine if this cast only deals with integral->integral conversion.
1711bool CastInst::isIntegerCast() const {
1712 switch (getOpcode()) {
1713 default: return false;
1714 case Instruction::ZExt:
1715 case Instruction::SExt:
1716 case Instruction::Trunc:
1717 return true;
1718 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001719 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001720 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001721}
1722
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001723bool CastInst::isLosslessCast() const {
1724 // Only BitCast can be lossless, exit fast if we're not BitCast
1725 if (getOpcode() != Instruction::BitCast)
1726 return false;
1727
1728 // Identity cast is always lossless
1729 const Type* SrcTy = getOperand(0)->getType();
1730 const Type* DstTy = getType();
1731 if (SrcTy == DstTy)
1732 return true;
1733
Reid Spencer8d9336d2006-12-31 05:26:44 +00001734 // Pointer to pointer is always lossless.
1735 if (isa<PointerType>(SrcTy))
1736 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001737 return false; // Other types have no identity values
1738}
1739
1740/// This function determines if the CastInst does not require any bits to be
1741/// changed in order to effect the cast. Essentially, it identifies cases where
1742/// no code gen is necessary for the cast, hence the name no-op cast. For
1743/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001744/// # bitcast i32* %x to i8*
1745/// # bitcast <2 x i32> %x to <4 x i16>
1746/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001747/// @brief Determine if a cast is a no-op.
1748bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1749 switch (getOpcode()) {
1750 default:
1751 assert(!"Invalid CastOp");
1752 case Instruction::Trunc:
1753 case Instruction::ZExt:
1754 case Instruction::SExt:
1755 case Instruction::FPTrunc:
1756 case Instruction::FPExt:
1757 case Instruction::UIToFP:
1758 case Instruction::SIToFP:
1759 case Instruction::FPToUI:
1760 case Instruction::FPToSI:
1761 return false; // These always modify bits
1762 case Instruction::BitCast:
1763 return true; // BitCast never modifies bits.
1764 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001765 return IntPtrTy->getScalarSizeInBits() ==
1766 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001767 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001768 return IntPtrTy->getScalarSizeInBits() ==
1769 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001770 }
1771}
1772
1773/// This function determines if a pair of casts can be eliminated and what
1774/// opcode should be used in the elimination. This assumes that there are two
1775/// instructions like this:
1776/// * %F = firstOpcode SrcTy %x to MidTy
1777/// * %S = secondOpcode MidTy %F to DstTy
1778/// The function returns a resultOpcode so these two casts can be replaced with:
1779/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1780/// If no such cast is permited, the function returns 0.
1781unsigned CastInst::isEliminableCastPair(
1782 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1783 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1784{
1785 // Define the 144 possibilities for these two cast instructions. The values
1786 // in this matrix determine what to do in a given situation and select the
1787 // case in the switch below. The rows correspond to firstOp, the columns
1788 // correspond to secondOp. In looking at the table below, keep in mind
1789 // the following cast properties:
1790 //
1791 // Size Compare Source Destination
1792 // Operator Src ? Size Type Sign Type Sign
1793 // -------- ------------ ------------------- ---------------------
1794 // TRUNC > Integer Any Integral Any
1795 // ZEXT < Integral Unsigned Integer Any
1796 // SEXT < Integral Signed Integer Any
1797 // FPTOUI n/a FloatPt n/a Integral Unsigned
1798 // FPTOSI n/a FloatPt n/a Integral Signed
1799 // UITOFP n/a Integral Unsigned FloatPt n/a
1800 // SITOFP n/a Integral Signed FloatPt n/a
1801 // FPTRUNC > FloatPt n/a FloatPt n/a
1802 // FPEXT < FloatPt n/a FloatPt n/a
1803 // PTRTOINT n/a Pointer n/a Integral Unsigned
1804 // INTTOPTR n/a Integral Unsigned Pointer n/a
1805 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001806 //
1807 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001808 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1809 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001810 // of the produced value (we no longer know the top-part is all zeros).
1811 // Further this conversion is often much more expensive for typical hardware,
1812 // and causes issues when building libgcc. We disallow fptosi+sext for the
1813 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001814 const unsigned numCastOps =
1815 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1816 static const uint8_t CastResults[numCastOps][numCastOps] = {
1817 // T F F U S F F P I B -+
1818 // R Z S P P I I T P 2 N T |
1819 // U E E 2 2 2 2 R E I T C +- secondOp
1820 // N X X U S F F N X N 2 V |
1821 // C T T I I P P C T T P T -+
1822 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1823 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1824 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001825 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1826 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001827 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1828 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1829 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1830 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1831 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1832 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1833 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1834 };
1835
1836 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1837 [secondOp-Instruction::CastOpsBegin];
1838 switch (ElimCase) {
1839 case 0:
1840 // categorically disallowed
1841 return 0;
1842 case 1:
1843 // allowed, use first cast's opcode
1844 return firstOp;
1845 case 2:
1846 // allowed, use second cast's opcode
1847 return secondOp;
1848 case 3:
1849 // no-op cast in second op implies firstOp as long as the DestTy
1850 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001851 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001852 return firstOp;
1853 return 0;
1854 case 4:
1855 // no-op cast in second op implies firstOp as long as the DestTy
1856 // is floating point
1857 if (DstTy->isFloatingPoint())
1858 return firstOp;
1859 return 0;
1860 case 5:
1861 // no-op cast in first op implies secondOp as long as the SrcTy
1862 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001863 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001864 return secondOp;
1865 return 0;
1866 case 6:
1867 // no-op cast in first op implies secondOp as long as the SrcTy
1868 // is a floating point
1869 if (SrcTy->isFloatingPoint())
1870 return secondOp;
1871 return 0;
1872 case 7: {
1873 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00001874 if (!IntPtrTy)
1875 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001876 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1877 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001878 if (MidSize >= PtrSize)
1879 return Instruction::BitCast;
1880 return 0;
1881 }
1882 case 8: {
1883 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1884 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1885 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001886 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1887 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001888 if (SrcSize == DstSize)
1889 return Instruction::BitCast;
1890 else if (SrcSize < DstSize)
1891 return firstOp;
1892 return secondOp;
1893 }
1894 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1895 return Instruction::ZExt;
1896 case 10:
1897 // fpext followed by ftrunc is allowed if the bit size returned to is
1898 // the same as the original, in which case its just a bitcast
1899 if (SrcTy == DstTy)
1900 return Instruction::BitCast;
1901 return 0; // If the types are not the same we can't eliminate it.
1902 case 11:
1903 // bitcast followed by ptrtoint is allowed as long as the bitcast
1904 // is a pointer to pointer cast.
1905 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1906 return secondOp;
1907 return 0;
1908 case 12:
1909 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1910 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1911 return firstOp;
1912 return 0;
1913 case 13: {
1914 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00001915 if (!IntPtrTy)
1916 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001917 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1918 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1919 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001920 if (SrcSize <= PtrSize && SrcSize == DstSize)
1921 return Instruction::BitCast;
1922 return 0;
1923 }
1924 case 99:
1925 // cast combination can't happen (error in input). This is for all cases
1926 // where the MidTy is not the same for the two cast instructions.
1927 assert(!"Invalid Cast Combination");
1928 return 0;
1929 default:
1930 assert(!"Error in CastResults table!!!");
1931 return 0;
1932 }
1933 return 0;
1934}
1935
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001936CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001937 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001938 // Construct and return the appropriate CastInst subclass
1939 switch (op) {
1940 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1941 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1942 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1943 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1944 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1945 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1946 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1947 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1948 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1949 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1950 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1951 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1952 default:
1953 assert(!"Invalid opcode provided");
1954 }
1955 return 0;
1956}
1957
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001958CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001959 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001960 // Construct and return the appropriate CastInst subclass
1961 switch (op) {
1962 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1963 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1964 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1965 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1966 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1967 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1968 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1969 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1970 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1971 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1972 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1973 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1974 default:
1975 assert(!"Invalid opcode provided");
1976 }
1977 return 0;
1978}
1979
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001980CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001981 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00001982 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001983 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001984 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1985 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001986}
1987
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001988CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001989 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00001990 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001991 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001992 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1993 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001994}
1995
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001996CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001997 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00001998 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001999 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002000 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2001 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002002}
2003
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002004CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002005 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002006 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002007 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002008 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2009 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002010}
2011
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002012CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002013 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002014 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002015 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002016 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2017 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002018}
2019
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002020CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002021 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002022 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002023 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002024 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2025 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002026}
2027
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002028CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002029 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002030 BasicBlock *InsertAtEnd) {
2031 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002032 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002033 "Invalid cast");
2034
Chris Lattner03c49532007-01-15 02:27:26 +00002035 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002036 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2037 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002038}
2039
2040/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002041CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002042 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002043 Instruction *InsertBefore) {
2044 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002045 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002046 "Invalid cast");
2047
Chris Lattner03c49532007-01-15 02:27:26 +00002048 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002049 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2050 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002051}
2052
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002053CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002054 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002055 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002056 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002057 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2058 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002059 Instruction::CastOps opcode =
2060 (SrcBits == DstBits ? Instruction::BitCast :
2061 (SrcBits > DstBits ? Instruction::Trunc :
2062 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002063 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002064}
2065
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002066CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002067 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002068 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002069 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2070 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002071 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2072 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002073 Instruction::CastOps opcode =
2074 (SrcBits == DstBits ? Instruction::BitCast :
2075 (SrcBits > DstBits ? Instruction::Trunc :
2076 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002077 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002078}
2079
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002080CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002081 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002082 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002083 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002084 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002085 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2086 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002087 Instruction::CastOps opcode =
2088 (SrcBits == DstBits ? Instruction::BitCast :
2089 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002090 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002091}
2092
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002093CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002094 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002095 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002096 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002097 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002098 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2099 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002100 Instruction::CastOps opcode =
2101 (SrcBits == DstBits ? Instruction::BitCast :
2102 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002103 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002104}
2105
Duncan Sands55e50902008-01-06 10:12:28 +00002106// Check whether it is valid to call getCastOpcode for these types.
2107// This routine must be kept in sync with getCastOpcode.
2108bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2109 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2110 return false;
2111
2112 if (SrcTy == DestTy)
2113 return true;
2114
2115 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002116 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2117 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002118
2119 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002120 if (DestTy->isInteger()) { // Casting to integral
2121 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002122 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002123 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002124 return true;
2125 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002126 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002127 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002128 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002129 return isa<PointerType>(SrcTy);
2130 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002131 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2132 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002133 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002134 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002135 return true;
2136 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002137 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002138 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002139 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002140 return false;
2141 }
2142 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002143 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002144 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002145 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002146 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002147 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002148 return DestPTy->getBitWidth() == SrcBits;
2149 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002150 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2151 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002152 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002153 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002154 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002155 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002156 return false;
2157 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002158 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002159 return false;
2160 }
2161}
2162
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002163// Provide a way to get a "cast" where the cast opcode is inferred from the
2164// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002165// logic in the castIsValid function below. This axiom should hold:
2166// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2167// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002168// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002169// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002170Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002171CastInst::getCastOpcode(
2172 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002173 // Get the bit sizes, we'll need these
2174 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002175 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2176 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002177
Duncan Sands55e50902008-01-06 10:12:28 +00002178 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2179 "Only first class types are castable!");
2180
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002181 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002182 if (DestTy->isInteger()) { // Casting to integral
2183 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002184 if (DestBits < SrcBits)
2185 return Trunc; // int -> smaller int
2186 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002187 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002188 return SExt; // signed -> SEXT
2189 else
2190 return ZExt; // unsigned -> ZEXT
2191 } else {
2192 return BitCast; // Same size, No-op cast
2193 }
2194 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002195 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002196 return FPToSI; // FP -> sint
2197 else
2198 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002199 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002200 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002201 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002202 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002203 return BitCast; // Same size, no-op cast
2204 } else {
2205 assert(isa<PointerType>(SrcTy) &&
2206 "Casting from a value that is not first-class type");
2207 return PtrToInt; // ptr -> int
2208 }
2209 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002210 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002211 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002212 return SIToFP; // sint -> FP
2213 else
2214 return UIToFP; // uint -> FP
2215 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2216 if (DestBits < SrcBits) {
2217 return FPTrunc; // FP -> smaller FP
2218 } else if (DestBits > SrcBits) {
2219 return FPExt; // FP -> larger FP
2220 } else {
2221 return BitCast; // same size, no-op cast
2222 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002223 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002224 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002225 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002226 PTy = NULL;
2227 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002228 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002229 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002230 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002231 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2232 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002233 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002234 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002235 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002236 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002238 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002239 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002240 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002241 }
2242 } else if (isa<PointerType>(DestTy)) {
2243 if (isa<PointerType>(SrcTy)) {
2244 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002245 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002246 return IntToPtr; // int -> ptr
2247 } else {
2248 assert(!"Casting pointer to other than pointer or int");
2249 }
2250 } else {
2251 assert(!"Casting to type that is not first-class");
2252 }
2253
2254 // If we fall through to here we probably hit an assertion cast above
2255 // and assertions are not turned on. Anything we return is an error, so
2256 // BitCast is as good a choice as any.
2257 return BitCast;
2258}
2259
2260//===----------------------------------------------------------------------===//
2261// CastInst SubClass Constructors
2262//===----------------------------------------------------------------------===//
2263
2264/// Check that the construction parameters for a CastInst are correct. This
2265/// could be broken out into the separate constructors but it is useful to have
2266/// it in one place and to eliminate the redundant code for getting the sizes
2267/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002268bool
2269CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002270
2271 // Check for type sanity on the arguments
2272 const Type *SrcTy = S->getType();
2273 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2274 return false;
2275
2276 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002277 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2278 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002279
2280 // Switch on the opcode provided
2281 switch (op) {
2282 default: return false; // This is an input error
2283 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002284 return SrcTy->isIntOrIntVector() &&
2285 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002286 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002287 return SrcTy->isIntOrIntVector() &&
2288 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002289 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002290 return SrcTy->isIntOrIntVector() &&
2291 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002292 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002293 return SrcTy->isFPOrFPVector() &&
2294 DstTy->isFPOrFPVector() &&
2295 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002296 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002297 return SrcTy->isFPOrFPVector() &&
2298 DstTy->isFPOrFPVector() &&
2299 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002300 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002302 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2303 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002304 return SVTy->getElementType()->isIntOrIntVector() &&
2305 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002306 SVTy->getNumElements() == DVTy->getNumElements();
2307 }
2308 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002309 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002310 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002311 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002312 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2313 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002314 return SVTy->getElementType()->isFPOrFPVector() &&
2315 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002316 SVTy->getNumElements() == DVTy->getNumElements();
2317 }
2318 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002319 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002320 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002321 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002322 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002323 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324 case Instruction::BitCast:
2325 // BitCast implies a no-op cast of type only. No bits change.
2326 // However, you can't cast pointers to anything but pointers.
2327 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2328 return false;
2329
Duncan Sands55e50902008-01-06 10:12:28 +00002330 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002331 // these cases, the cast is okay if the source and destination bit widths
2332 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002333 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002334 }
2335}
2336
2337TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002338 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002339) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002340 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002341}
2342
2343TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002344 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002345) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002346 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002347}
2348
2349ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002350 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002351) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002352 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002353}
2354
2355ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002356 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002358 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359}
2360SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002361 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002363 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002364}
2365
Jeff Cohencc08c832006-12-02 02:22:01 +00002366SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002367 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002368) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002369 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002370}
2371
2372FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002373 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002374) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002375 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002376}
2377
2378FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002379 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002380) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002381 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002382}
2383
2384FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002385 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002386) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002387 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002388}
2389
2390FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002391 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002393 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002394}
2395
2396UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002397 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002399 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400}
2401
2402UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002403 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002404) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002405 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002406}
2407
2408SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002409 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002410) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002411 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002412}
2413
2414SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002415 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002416) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002417 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002418}
2419
2420FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002421 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002423 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002424}
2425
2426FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002427 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002429 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002430}
2431
2432FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002433 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002434) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002435 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002436}
2437
2438FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002439 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002440) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002441 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002442}
2443
2444PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002445 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002446) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002447 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002448}
2449
2450PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002451 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002452) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002453 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002454}
2455
2456IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002457 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002458) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002459 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002460}
2461
2462IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002463 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002465 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002466}
2467
2468BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002469 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002470) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002471 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472}
2473
2474BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002475 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002476) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002477 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002479
2480//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002481// CmpInst Classes
2482//===----------------------------------------------------------------------===//
2483
Nate Begemand2195702008-05-12 19:01:56 +00002484CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002485 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002486 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002487 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002488 OperandTraits<CmpInst>::op_begin(this),
2489 OperandTraits<CmpInst>::operands(this),
2490 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002491 Op<0>() = LHS;
2492 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002493 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002494 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002495}
Gabor Greiff6caff662008-05-10 08:32:32 +00002496
Nate Begemand2195702008-05-12 19:01:56 +00002497CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002498 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002499 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002500 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002501 OperandTraits<CmpInst>::op_begin(this),
2502 OperandTraits<CmpInst>::operands(this),
2503 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002504 Op<0>() = LHS;
2505 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002506 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002507 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002508}
2509
2510CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002511CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002512 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002513 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002514 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002515 if (InsertBefore)
2516 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2517 S1, S2, Name);
2518 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002519 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002520 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002521 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002522
2523 if (InsertBefore)
2524 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2525 S1, S2, Name);
2526 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002527 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002528 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002529}
2530
2531CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002532CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002533 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002534 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002535 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2536 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002537 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002538 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2539 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002540}
2541
2542void CmpInst::swapOperands() {
2543 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2544 IC->swapOperands();
2545 else
2546 cast<FCmpInst>(this)->swapOperands();
2547}
2548
2549bool CmpInst::isCommutative() {
2550 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2551 return IC->isCommutative();
2552 return cast<FCmpInst>(this)->isCommutative();
2553}
2554
2555bool CmpInst::isEquality() {
2556 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2557 return IC->isEquality();
2558 return cast<FCmpInst>(this)->isEquality();
2559}
2560
2561
Dan Gohman4e724382008-05-31 02:47:54 +00002562CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002563 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002564 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002565 case ICMP_EQ: return ICMP_NE;
2566 case ICMP_NE: return ICMP_EQ;
2567 case ICMP_UGT: return ICMP_ULE;
2568 case ICMP_ULT: return ICMP_UGE;
2569 case ICMP_UGE: return ICMP_ULT;
2570 case ICMP_ULE: return ICMP_UGT;
2571 case ICMP_SGT: return ICMP_SLE;
2572 case ICMP_SLT: return ICMP_SGE;
2573 case ICMP_SGE: return ICMP_SLT;
2574 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002575
Dan Gohman4e724382008-05-31 02:47:54 +00002576 case FCMP_OEQ: return FCMP_UNE;
2577 case FCMP_ONE: return FCMP_UEQ;
2578 case FCMP_OGT: return FCMP_ULE;
2579 case FCMP_OLT: return FCMP_UGE;
2580 case FCMP_OGE: return FCMP_ULT;
2581 case FCMP_OLE: return FCMP_UGT;
2582 case FCMP_UEQ: return FCMP_ONE;
2583 case FCMP_UNE: return FCMP_OEQ;
2584 case FCMP_UGT: return FCMP_OLE;
2585 case FCMP_ULT: return FCMP_OGE;
2586 case FCMP_UGE: return FCMP_OLT;
2587 case FCMP_ULE: return FCMP_OGT;
2588 case FCMP_ORD: return FCMP_UNO;
2589 case FCMP_UNO: return FCMP_ORD;
2590 case FCMP_TRUE: return FCMP_FALSE;
2591 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002592 }
2593}
2594
Reid Spencer266e42b2006-12-23 06:05:41 +00002595ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2596 switch (pred) {
2597 default: assert(! "Unknown icmp predicate!");
2598 case ICMP_EQ: case ICMP_NE:
2599 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2600 return pred;
2601 case ICMP_UGT: return ICMP_SGT;
2602 case ICMP_ULT: return ICMP_SLT;
2603 case ICMP_UGE: return ICMP_SGE;
2604 case ICMP_ULE: return ICMP_SLE;
2605 }
2606}
2607
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002608ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2609 switch (pred) {
2610 default: assert(! "Unknown icmp predicate!");
2611 case ICMP_EQ: case ICMP_NE:
2612 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2613 return pred;
2614 case ICMP_SGT: return ICMP_UGT;
2615 case ICMP_SLT: return ICMP_ULT;
2616 case ICMP_SGE: return ICMP_UGE;
2617 case ICMP_SLE: return ICMP_ULE;
2618 }
2619}
2620
Reid Spencer266e42b2006-12-23 06:05:41 +00002621bool ICmpInst::isSignedPredicate(Predicate pred) {
2622 switch (pred) {
2623 default: assert(! "Unknown icmp predicate!");
2624 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2625 return true;
2626 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2627 case ICMP_UGE: case ICMP_ULE:
2628 return false;
2629 }
2630}
2631
Reid Spencer0286bc12007-02-28 22:00:54 +00002632/// Initialize a set of values that all satisfy the condition with C.
2633///
2634ConstantRange
2635ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2636 APInt Lower(C);
2637 APInt Upper(C);
2638 uint32_t BitWidth = C.getBitWidth();
2639 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002640 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002641 case ICmpInst::ICMP_EQ: Upper++; break;
2642 case ICmpInst::ICMP_NE: Lower++; break;
2643 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2644 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2645 case ICmpInst::ICMP_UGT:
2646 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2647 break;
2648 case ICmpInst::ICMP_SGT:
2649 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2650 break;
2651 case ICmpInst::ICMP_ULE:
2652 Lower = APInt::getMinValue(BitWidth); Upper++;
2653 break;
2654 case ICmpInst::ICMP_SLE:
2655 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2656 break;
2657 case ICmpInst::ICMP_UGE:
2658 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2659 break;
2660 case ICmpInst::ICMP_SGE:
2661 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2662 break;
2663 }
2664 return ConstantRange(Lower, Upper);
2665}
2666
Dan Gohman4e724382008-05-31 02:47:54 +00002667CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002668 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002669 default: assert(!"Unknown cmp predicate!");
2670 case ICMP_EQ: case ICMP_NE:
2671 return pred;
2672 case ICMP_SGT: return ICMP_SLT;
2673 case ICMP_SLT: return ICMP_SGT;
2674 case ICMP_SGE: return ICMP_SLE;
2675 case ICMP_SLE: return ICMP_SGE;
2676 case ICMP_UGT: return ICMP_ULT;
2677 case ICMP_ULT: return ICMP_UGT;
2678 case ICMP_UGE: return ICMP_ULE;
2679 case ICMP_ULE: return ICMP_UGE;
2680
Reid Spencerd9436b62006-11-20 01:22:35 +00002681 case FCMP_FALSE: case FCMP_TRUE:
2682 case FCMP_OEQ: case FCMP_ONE:
2683 case FCMP_UEQ: case FCMP_UNE:
2684 case FCMP_ORD: case FCMP_UNO:
2685 return pred;
2686 case FCMP_OGT: return FCMP_OLT;
2687 case FCMP_OLT: return FCMP_OGT;
2688 case FCMP_OGE: return FCMP_OLE;
2689 case FCMP_OLE: return FCMP_OGE;
2690 case FCMP_UGT: return FCMP_ULT;
2691 case FCMP_ULT: return FCMP_UGT;
2692 case FCMP_UGE: return FCMP_ULE;
2693 case FCMP_ULE: return FCMP_UGE;
2694 }
2695}
2696
Reid Spencer266e42b2006-12-23 06:05:41 +00002697bool CmpInst::isUnsigned(unsigned short predicate) {
2698 switch (predicate) {
2699 default: return false;
2700 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2701 case ICmpInst::ICMP_UGE: return true;
2702 }
2703}
2704
2705bool CmpInst::isSigned(unsigned short predicate){
2706 switch (predicate) {
2707 default: return false;
2708 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2709 case ICmpInst::ICMP_SGE: return true;
2710 }
2711}
2712
2713bool CmpInst::isOrdered(unsigned short predicate) {
2714 switch (predicate) {
2715 default: return false;
2716 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2717 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2718 case FCmpInst::FCMP_ORD: return true;
2719 }
2720}
2721
2722bool CmpInst::isUnordered(unsigned short predicate) {
2723 switch (predicate) {
2724 default: return false;
2725 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2726 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2727 case FCmpInst::FCMP_UNO: return true;
2728 }
2729}
2730
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002731//===----------------------------------------------------------------------===//
2732// SwitchInst Implementation
2733//===----------------------------------------------------------------------===//
2734
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002735void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002736 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002737 ReservedSpace = 2+NumCases*2;
2738 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002739 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002740
Gabor Greif2d3024d2008-05-26 21:33:52 +00002741 OperandList[0] = Value;
2742 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002743}
2744
Chris Lattner2195fc42007-02-24 00:55:48 +00002745/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2746/// switch on and a default destination. The number of additional cases can
2747/// be specified here to make memory allocation more efficient. This
2748/// constructor can also autoinsert before another instruction.
2749SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2750 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002751 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2752 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002753 init(Value, Default, NumCases);
2754}
2755
2756/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2757/// switch on and a default destination. The number of additional cases can
2758/// be specified here to make memory allocation more efficient. This
2759/// constructor also autoinserts at the end of the specified BasicBlock.
2760SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2761 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002762 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2763 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002764 init(Value, Default, NumCases);
2765}
2766
Misha Brukmanb1c93172005-04-21 23:48:37 +00002767SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002768 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002769 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002770 Use *OL = OperandList, *InOL = SI.OperandList;
2771 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002772 OL[i] = InOL[i];
2773 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002774 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002775 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002776}
2777
Gordon Henriksen14a55692007-12-10 02:14:30 +00002778SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002779 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002780}
2781
2782
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002783/// addCase - Add an entry to the switch instruction...
2784///
Chris Lattner47ac1872005-02-24 05:32:09 +00002785void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002786 unsigned OpNo = NumOperands;
2787 if (OpNo+2 > ReservedSpace)
2788 resizeOperands(0); // Get more space!
2789 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002790 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002791 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002792 OperandList[OpNo] = OnVal;
2793 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002794}
2795
2796/// removeCase - This method removes the specified successor from the switch
2797/// instruction. Note that this cannot be used to remove the default
2798/// destination (successor #0).
2799///
2800void SwitchInst::removeCase(unsigned idx) {
2801 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002802 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2803
2804 unsigned NumOps = getNumOperands();
2805 Use *OL = OperandList;
2806
2807 // Move everything after this operand down.
2808 //
2809 // FIXME: we could just swap with the end of the list, then erase. However,
2810 // client might not expect this to happen. The code as it is thrashes the
2811 // use/def lists, which is kinda lame.
2812 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2813 OL[i-2] = OL[i];
2814 OL[i-2+1] = OL[i+1];
2815 }
2816
2817 // Nuke the last value.
2818 OL[NumOps-2].set(0);
2819 OL[NumOps-2+1].set(0);
2820 NumOperands = NumOps-2;
2821}
2822
2823/// resizeOperands - resize operands - This adjusts the length of the operands
2824/// list according to the following behavior:
2825/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002826/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002827/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2828/// 3. If NumOps == NumOperands, trim the reserved space.
2829///
2830void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002831 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002832 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002833 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002834 } else if (NumOps*2 > NumOperands) {
2835 // No resize needed.
2836 if (ReservedSpace >= NumOps) return;
2837 } else if (NumOps == NumOperands) {
2838 if (ReservedSpace == NumOps) return;
2839 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002840 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002841 }
2842
2843 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002844 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002845 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002846 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002847 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002848 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002849 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002850 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002851}
2852
2853
2854BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2855 return getSuccessor(idx);
2856}
2857unsigned SwitchInst::getNumSuccessorsV() const {
2858 return getNumSuccessors();
2859}
2860void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2861 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002862}
Chris Lattnerf22be932004-10-15 23:52:53 +00002863
Chris Lattnerf22be932004-10-15 23:52:53 +00002864// Define these methods here so vtables don't get emitted into every translation
2865// unit that uses these classes.
2866
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002867GetElementPtrInst *GetElementPtrInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002868 GetElementPtrInst *New = new(getNumOperands()) GetElementPtrInst(*this);
2869 New->SubclassOptionalData = SubclassOptionalData;
2870 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00002871}
2872
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002873BinaryOperator *BinaryOperator::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002874 BinaryOperator *New = Create(getOpcode(), Op<0>(), Op<1>());
2875 New->SubclassOptionalData = SubclassOptionalData;
2876 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00002877}
2878
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002879FCmpInst* FCmpInst::clone(LLVMContext &Context) const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002880 FCmpInst *New = new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002881 New->SubclassOptionalData = SubclassOptionalData;
2882 return New;
Chris Lattner0b490b02007-08-24 20:48:18 +00002883}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002884ICmpInst* ICmpInst::clone(LLVMContext &Context) const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002885 ICmpInst *New = new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002886 New->SubclassOptionalData = SubclassOptionalData;
2887 return New;
Reid Spencerd9436b62006-11-20 01:22:35 +00002888}
2889
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002890ExtractValueInst *ExtractValueInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002891 ExtractValueInst *New = new ExtractValueInst(*this);
2892 New->SubclassOptionalData = SubclassOptionalData;
2893 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00002894}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002895InsertValueInst *InsertValueInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002896 InsertValueInst *New = new InsertValueInst(*this);
2897 New->SubclassOptionalData = SubclassOptionalData;
2898 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00002899}
2900
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002901MallocInst *MallocInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002902 MallocInst *New = new MallocInst(getAllocatedType(),
2903 (Value*)getOperand(0),
2904 getAlignment());
2905 New->SubclassOptionalData = SubclassOptionalData;
2906 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002907}
Dan Gohman0752bff2008-05-23 00:36:11 +00002908
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002909AllocaInst *AllocaInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002910 AllocaInst *New = new AllocaInst(getAllocatedType(),
2911 (Value*)getOperand(0),
2912 getAlignment());
2913 New->SubclassOptionalData = SubclassOptionalData;
2914 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002915}
2916
2917FreeInst *FreeInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002918 FreeInst *New = new FreeInst(getOperand(0));
2919 New->SubclassOptionalData = SubclassOptionalData;
2920 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002921}
2922
2923LoadInst *LoadInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002924 LoadInst *New = new LoadInst(getOperand(0),
2925 Twine(), isVolatile(),
2926 getAlignment());
2927 New->SubclassOptionalData = SubclassOptionalData;
2928 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002929}
2930
2931StoreInst *StoreInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002932 StoreInst *New = new StoreInst(getOperand(0), getOperand(1),
2933 isVolatile(), getAlignment());
2934 New->SubclassOptionalData = SubclassOptionalData;
2935 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002936}
2937
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002938TruncInst *TruncInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002939 TruncInst *New = new TruncInst(getOperand(0), getType());
2940 New->SubclassOptionalData = SubclassOptionalData;
2941 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002942}
2943
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002944ZExtInst *ZExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002945 ZExtInst *New = new ZExtInst(getOperand(0), getType());
2946 New->SubclassOptionalData = SubclassOptionalData;
2947 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002948}
2949
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002950SExtInst *SExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002951 SExtInst *New = new SExtInst(getOperand(0), getType());
2952 New->SubclassOptionalData = SubclassOptionalData;
2953 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002954}
2955
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002956FPTruncInst *FPTruncInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002957 FPTruncInst *New = new FPTruncInst(getOperand(0), getType());
2958 New->SubclassOptionalData = SubclassOptionalData;
2959 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002960}
2961
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002962FPExtInst *FPExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002963 FPExtInst *New = new FPExtInst(getOperand(0), getType());
2964 New->SubclassOptionalData = SubclassOptionalData;
2965 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002966}
2967
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002968UIToFPInst *UIToFPInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002969 UIToFPInst *New = new UIToFPInst(getOperand(0), getType());
2970 New->SubclassOptionalData = SubclassOptionalData;
2971 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002972}
2973
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002974SIToFPInst *SIToFPInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002975 SIToFPInst *New = new SIToFPInst(getOperand(0), getType());
2976 New->SubclassOptionalData = SubclassOptionalData;
2977 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002978}
2979
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002980FPToUIInst *FPToUIInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002981 FPToUIInst *New = new FPToUIInst(getOperand(0), getType());
2982 New->SubclassOptionalData = SubclassOptionalData;
2983 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002984}
2985
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002986FPToSIInst *FPToSIInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002987 FPToSIInst *New = new FPToSIInst(getOperand(0), getType());
2988 New->SubclassOptionalData = SubclassOptionalData;
2989 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002990}
2991
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002992PtrToIntInst *PtrToIntInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002993 PtrToIntInst *New = new PtrToIntInst(getOperand(0), getType());
2994 New->SubclassOptionalData = SubclassOptionalData;
2995 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002996}
2997
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002998IntToPtrInst *IntToPtrInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002999 IntToPtrInst *New = new IntToPtrInst(getOperand(0), getType());
3000 New->SubclassOptionalData = SubclassOptionalData;
3001 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003002}
3003
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003004BitCastInst *BitCastInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003005 BitCastInst *New = new BitCastInst(getOperand(0), getType());
3006 New->SubclassOptionalData = SubclassOptionalData;
3007 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003008}
3009
3010CallInst *CallInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003011 CallInst *New = new(getNumOperands()) CallInst(*this);
3012 New->SubclassOptionalData = SubclassOptionalData;
3013 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003014}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003015
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003016SelectInst *SelectInst::clone(LLVMContext&) const {
3017 SelectInst *New = SelectInst::Create(getOperand(0),
3018 getOperand(1),
3019 getOperand(2));
3020 New->SubclassOptionalData = SubclassOptionalData;
3021 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003022}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003023
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003024VAArgInst *VAArgInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003025 VAArgInst *New = new VAArgInst(getOperand(0), getType());
3026 New->SubclassOptionalData = SubclassOptionalData;
3027 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003028}
3029
3030ExtractElementInst *ExtractElementInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003031 ExtractElementInst *New = ExtractElementInst::Create(getOperand(0),
3032 getOperand(1));
3033 New->SubclassOptionalData = SubclassOptionalData;
3034 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003035}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003036
3037InsertElementInst *InsertElementInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003038 InsertElementInst *New = InsertElementInst::Create(getOperand(0),
3039 getOperand(1),
3040 getOperand(2));
3041 New->SubclassOptionalData = SubclassOptionalData;
3042 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003043}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003044
3045ShuffleVectorInst *ShuffleVectorInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003046 ShuffleVectorInst *New = new ShuffleVectorInst(getOperand(0),
3047 getOperand(1),
3048 getOperand(2));
3049 New->SubclassOptionalData = SubclassOptionalData;
3050 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003051}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003052
3053PHINode *PHINode::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003054 PHINode *New = new PHINode(*this);
3055 New->SubclassOptionalData = SubclassOptionalData;
3056 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003057}
3058
3059ReturnInst *ReturnInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003060 ReturnInst *New = new(getNumOperands()) ReturnInst(*this);
3061 New->SubclassOptionalData = SubclassOptionalData;
3062 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003063}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003064
3065BranchInst *BranchInst::clone(LLVMContext&) const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003066 unsigned Ops(getNumOperands());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003067 BranchInst *New = new(Ops, Ops == 1) BranchInst(*this);
3068 New->SubclassOptionalData = SubclassOptionalData;
3069 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003070}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003071
3072SwitchInst *SwitchInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003073 SwitchInst *New = new SwitchInst(*this);
3074 New->SubclassOptionalData = SubclassOptionalData;
3075 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003076}
3077
3078InvokeInst *InvokeInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003079 InvokeInst *New = new(getNumOperands()) InvokeInst(*this);
3080 New->SubclassOptionalData = SubclassOptionalData;
3081 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003082}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003083
Owen Anderson55f1c092009-08-13 21:58:54 +00003084UnwindInst *UnwindInst::clone(LLVMContext &C) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003085 UnwindInst *New = new UnwindInst(C);
3086 New->SubclassOptionalData = SubclassOptionalData;
3087 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003088}
3089
Owen Anderson55f1c092009-08-13 21:58:54 +00003090UnreachableInst *UnreachableInst::clone(LLVMContext &C) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003091 UnreachableInst *New = new UnreachableInst(C);
3092 New->SubclassOptionalData = SubclassOptionalData;
3093 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003094}