blob: 80d5992295fa8aaf414c59f82055a9a59dde46b3 [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}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000048unsigned CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000049 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000050}
51void CallSite::setCallingConv(unsigned 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)
Gabor Greiff6caff662008-05-10 08:32:32 +00001275 : Instruction(V1->getType(), ShuffleVector,
1276 OperandTraits<ShuffleVectorInst>::op_begin(this),
1277 OperandTraits<ShuffleVectorInst>::operands(this),
1278 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001279 assert(isValidOperands(V1, V2, Mask) &&
1280 "Invalid shuffle vector instruction operands!");
1281
Gabor Greif2d3024d2008-05-26 21:33:52 +00001282 Op<0>() = V1;
1283 Op<1>() = V2;
1284 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001285 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001286}
1287
Mon P Wang25f01062008-11-10 04:46:22 +00001288bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001289 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001290 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001291 return false;
1292
1293 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1294 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Owen Anderson55f1c092009-08-13 21:58:54 +00001295 MaskTy->getElementType() != Type::getInt32Ty(V1->getContext()))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001296 return false;
1297 return true;
1298}
1299
Chris Lattnerf724e342008-03-02 05:28:33 +00001300/// getMaskValue - Return the index from the shuffle mask for the specified
1301/// output result. This is either -1 if the element is undef or a number less
1302/// than 2*numelements.
1303int ShuffleVectorInst::getMaskValue(unsigned i) const {
1304 const Constant *Mask = cast<Constant>(getOperand(2));
1305 if (isa<UndefValue>(Mask)) return -1;
1306 if (isa<ConstantAggregateZero>(Mask)) return 0;
1307 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1308 assert(i < MaskCV->getNumOperands() && "Index out of range");
1309
1310 if (isa<UndefValue>(MaskCV->getOperand(i)))
1311 return -1;
1312 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1313}
1314
Dan Gohman12fce772008-05-15 19:50:34 +00001315//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001316// InsertValueInst Class
1317//===----------------------------------------------------------------------===//
1318
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001319void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001320 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001321 assert(NumOperands == 2 && "NumOperands not initialized?");
1322 Op<0>() = Agg;
1323 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001324
Dan Gohman1ecaf452008-05-31 00:58:22 +00001325 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001326 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001327}
1328
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001329void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001330 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001331 assert(NumOperands == 2 && "NumOperands not initialized?");
1332 Op<0>() = Agg;
1333 Op<1>() = Val;
1334
1335 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001336 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001337}
1338
1339InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001340 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001341 OperandTraits<InsertValueInst>::op_begin(this), 2),
1342 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001343 Op<0>() = IVI.getOperand(0);
1344 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001345 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001346}
1347
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001348InsertValueInst::InsertValueInst(Value *Agg,
1349 Value *Val,
1350 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001351 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001352 Instruction *InsertBefore)
1353 : Instruction(Agg->getType(), InsertValue,
1354 OperandTraits<InsertValueInst>::op_begin(this),
1355 2, InsertBefore) {
1356 init(Agg, Val, Idx, Name);
1357}
1358
1359InsertValueInst::InsertValueInst(Value *Agg,
1360 Value *Val,
1361 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001362 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001363 BasicBlock *InsertAtEnd)
1364 : Instruction(Agg->getType(), InsertValue,
1365 OperandTraits<InsertValueInst>::op_begin(this),
1366 2, InsertAtEnd) {
1367 init(Agg, Val, Idx, Name);
1368}
1369
Dan Gohman0752bff2008-05-23 00:36:11 +00001370//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001371// ExtractValueInst Class
1372//===----------------------------------------------------------------------===//
1373
Gabor Greifcbcc4952008-06-06 21:06:32 +00001374void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001375 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001376 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001377
Dan Gohman1ecaf452008-05-31 00:58:22 +00001378 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001379 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001380}
1381
Daniel Dunbar4975db62009-07-25 04:41:11 +00001382void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001383 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001384
1385 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001386 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001387}
1388
1389ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001390 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001391 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001392 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001393}
1394
Dan Gohman12fce772008-05-15 19:50:34 +00001395// getIndexedType - Returns the type of the element that would be extracted
1396// with an extractvalue instruction with the specified parameters.
1397//
1398// A null type is returned if the indices are invalid for the specified
1399// pointer type.
1400//
1401const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001402 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001403 unsigned NumIdx) {
1404 unsigned CurIdx = 0;
1405 for (; CurIdx != NumIdx; ++CurIdx) {
1406 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001407 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1408 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001409 if (!CT->indexValid(Index)) return 0;
1410 Agg = CT->getTypeAtIndex(Index);
1411
1412 // If the new type forwards to another type, then it is in the middle
1413 // of being refined to another type (and hence, may have dropped all
1414 // references to what it was using before). So, use the new forwarded
1415 // type.
1416 if (const Type *Ty = Agg->getForwardedType())
1417 Agg = Ty;
1418 }
1419 return CurIdx == NumIdx ? Agg : 0;
1420}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001421
Dan Gohman4a3125b2008-06-17 21:07:55 +00001422const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001423 unsigned Idx) {
1424 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001425}
1426
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001427//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001428// BinaryOperator Class
1429//===----------------------------------------------------------------------===//
1430
Dan Gohmana5b96452009-06-04 22:49:04 +00001431/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1432/// type is floating-point, to help provide compatibility with an older API.
1433///
1434static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1435 const Type *Ty) {
1436 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1437 if (Ty->isFPOrFPVector()) {
1438 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1439 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1440 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1441 }
1442 return iType;
1443}
1444
Chris Lattner2195fc42007-02-24 00:55:48 +00001445BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001446 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001447 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001448 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001449 OperandTraits<BinaryOperator>::op_begin(this),
1450 OperandTraits<BinaryOperator>::operands(this),
1451 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001452 Op<0>() = S1;
1453 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001454 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001455 setName(Name);
1456}
1457
1458BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001459 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001460 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001461 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001462 OperandTraits<BinaryOperator>::op_begin(this),
1463 OperandTraits<BinaryOperator>::operands(this),
1464 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001465 Op<0>() = S1;
1466 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001467 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001468 setName(Name);
1469}
1470
1471
1472void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001473 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001474 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001475 assert(LHS->getType() == RHS->getType() &&
1476 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001477#ifndef NDEBUG
1478 switch (iType) {
1479 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001480 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001481 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001482 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001483 assert(getType()->isIntOrIntVector() &&
1484 "Tried to create an integer operation on a non-integer type!");
1485 break;
1486 case FAdd: case FSub:
1487 case FMul:
1488 assert(getType() == LHS->getType() &&
1489 "Arithmetic operation should return same type as operands!");
1490 assert(getType()->isFPOrFPVector() &&
1491 "Tried to create a floating-point operation on a "
1492 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001493 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001494 case UDiv:
1495 case SDiv:
1496 assert(getType() == LHS->getType() &&
1497 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001498 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1499 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001500 "Incorrect operand type (not integer) for S/UDIV");
1501 break;
1502 case FDiv:
1503 assert(getType() == LHS->getType() &&
1504 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001505 assert(getType()->isFPOrFPVector() &&
1506 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001507 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001508 case URem:
1509 case SRem:
1510 assert(getType() == LHS->getType() &&
1511 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001512 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1513 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001514 "Incorrect operand type (not integer) for S/UREM");
1515 break;
1516 case FRem:
1517 assert(getType() == LHS->getType() &&
1518 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001519 assert(getType()->isFPOrFPVector() &&
1520 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001521 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001522 case Shl:
1523 case LShr:
1524 case AShr:
1525 assert(getType() == LHS->getType() &&
1526 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001527 assert((getType()->isInteger() ||
1528 (isa<VectorType>(getType()) &&
1529 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1530 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001531 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001532 case And: case Or:
1533 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001534 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001535 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001536 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001537 (isa<VectorType>(getType()) &&
1538 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001539 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001540 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001541 default:
1542 break;
1543 }
1544#endif
1545}
1546
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001547BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001548 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001549 Instruction *InsertBefore) {
1550 assert(S1->getType() == S2->getType() &&
1551 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001552 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001553}
1554
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001555BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001556 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001557 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001558 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001559 InsertAtEnd->getInstList().push_back(Res);
1560 return Res;
1561}
1562
Dan Gohman5476cfd2009-08-12 16:23:25 +00001563BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001564 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001565 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001566 return new BinaryOperator(Instruction::Sub,
1567 zero, Op,
1568 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001569}
1570
Dan Gohman5476cfd2009-08-12 16:23:25 +00001571BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001572 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001573 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001574 return new BinaryOperator(Instruction::Sub,
1575 zero, Op,
1576 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001577}
1578
Dan Gohman5476cfd2009-08-12 16:23:25 +00001579BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001580 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001581 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001582 return new BinaryOperator(Instruction::FSub,
1583 zero, Op,
1584 Op->getType(), Name, InsertBefore);
1585}
1586
Dan Gohman5476cfd2009-08-12 16:23:25 +00001587BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001588 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001589 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001590 return new BinaryOperator(Instruction::FSub,
1591 zero, Op,
1592 Op->getType(), Name, InsertAtEnd);
1593}
1594
Dan Gohman5476cfd2009-08-12 16:23:25 +00001595BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001596 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001597 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001598 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001599 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001600 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001601 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001602 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001603 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001604 }
1605
1606 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001607 Op->getType(), Name, InsertBefore);
1608}
1609
Dan Gohman5476cfd2009-08-12 16:23:25 +00001610BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001611 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001612 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001613 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001614 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001615 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001616 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001617 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001618 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001619 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001620 }
1621
1622 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001623 Op->getType(), Name, InsertAtEnd);
1624}
1625
1626
1627// isConstantAllOnes - Helper function for several functions below
1628static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001629 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1630 return CI->isAllOnesValue();
1631 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1632 return CV->isAllOnesValue();
1633 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001634}
1635
Owen Andersonbb2501b2009-07-13 22:18:28 +00001636bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001637 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1638 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001639 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1640 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001641 return false;
1642}
1643
Owen Andersonbb2501b2009-07-13 22:18:28 +00001644bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001645 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1646 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001647 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1648 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001649 return false;
1650}
1651
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001652bool BinaryOperator::isNot(const Value *V) {
1653 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1654 return (Bop->getOpcode() == Instruction::Xor &&
1655 (isConstantAllOnes(Bop->getOperand(1)) ||
1656 isConstantAllOnes(Bop->getOperand(0))));
1657 return false;
1658}
1659
Chris Lattner2c7d1772005-04-24 07:28:37 +00001660Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001661 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001662}
1663
Chris Lattner2c7d1772005-04-24 07:28:37 +00001664const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1665 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001666}
1667
Dan Gohmana5b96452009-06-04 22:49:04 +00001668Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001669 return cast<BinaryOperator>(BinOp)->getOperand(1);
1670}
1671
1672const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1673 return getFNegArgument(const_cast<Value*>(BinOp));
1674}
1675
Chris Lattner2c7d1772005-04-24 07:28:37 +00001676Value *BinaryOperator::getNotArgument(Value *BinOp) {
1677 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1678 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1679 Value *Op0 = BO->getOperand(0);
1680 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001681 if (isConstantAllOnes(Op0)) return Op1;
1682
1683 assert(isConstantAllOnes(Op1));
1684 return Op0;
1685}
1686
Chris Lattner2c7d1772005-04-24 07:28:37 +00001687const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1688 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001689}
1690
1691
1692// swapOperands - Exchange the two operands to this instruction. This
1693// instruction is safe to use on any binary instruction and does not
1694// modify the semantics of the instruction. If the instruction is
1695// order dependent (SetLT f.e.) the opcode is changed.
1696//
1697bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001698 if (!isCommutative())
1699 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001700 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001701 return false;
1702}
1703
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001704//===----------------------------------------------------------------------===//
1705// CastInst Class
1706//===----------------------------------------------------------------------===//
1707
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001708// Just determine if this cast only deals with integral->integral conversion.
1709bool CastInst::isIntegerCast() const {
1710 switch (getOpcode()) {
1711 default: return false;
1712 case Instruction::ZExt:
1713 case Instruction::SExt:
1714 case Instruction::Trunc:
1715 return true;
1716 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001717 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001718 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001719}
1720
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001721bool CastInst::isLosslessCast() const {
1722 // Only BitCast can be lossless, exit fast if we're not BitCast
1723 if (getOpcode() != Instruction::BitCast)
1724 return false;
1725
1726 // Identity cast is always lossless
1727 const Type* SrcTy = getOperand(0)->getType();
1728 const Type* DstTy = getType();
1729 if (SrcTy == DstTy)
1730 return true;
1731
Reid Spencer8d9336d2006-12-31 05:26:44 +00001732 // Pointer to pointer is always lossless.
1733 if (isa<PointerType>(SrcTy))
1734 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001735 return false; // Other types have no identity values
1736}
1737
1738/// This function determines if the CastInst does not require any bits to be
1739/// changed in order to effect the cast. Essentially, it identifies cases where
1740/// no code gen is necessary for the cast, hence the name no-op cast. For
1741/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001742/// # bitcast i32* %x to i8*
1743/// # bitcast <2 x i32> %x to <4 x i16>
1744/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001745/// @brief Determine if a cast is a no-op.
1746bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1747 switch (getOpcode()) {
1748 default:
1749 assert(!"Invalid CastOp");
1750 case Instruction::Trunc:
1751 case Instruction::ZExt:
1752 case Instruction::SExt:
1753 case Instruction::FPTrunc:
1754 case Instruction::FPExt:
1755 case Instruction::UIToFP:
1756 case Instruction::SIToFP:
1757 case Instruction::FPToUI:
1758 case Instruction::FPToSI:
1759 return false; // These always modify bits
1760 case Instruction::BitCast:
1761 return true; // BitCast never modifies bits.
1762 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001763 return IntPtrTy->getScalarSizeInBits() ==
1764 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001765 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001766 return IntPtrTy->getScalarSizeInBits() ==
1767 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001768 }
1769}
1770
1771/// This function determines if a pair of casts can be eliminated and what
1772/// opcode should be used in the elimination. This assumes that there are two
1773/// instructions like this:
1774/// * %F = firstOpcode SrcTy %x to MidTy
1775/// * %S = secondOpcode MidTy %F to DstTy
1776/// The function returns a resultOpcode so these two casts can be replaced with:
1777/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1778/// If no such cast is permited, the function returns 0.
1779unsigned CastInst::isEliminableCastPair(
1780 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1781 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1782{
1783 // Define the 144 possibilities for these two cast instructions. The values
1784 // in this matrix determine what to do in a given situation and select the
1785 // case in the switch below. The rows correspond to firstOp, the columns
1786 // correspond to secondOp. In looking at the table below, keep in mind
1787 // the following cast properties:
1788 //
1789 // Size Compare Source Destination
1790 // Operator Src ? Size Type Sign Type Sign
1791 // -------- ------------ ------------------- ---------------------
1792 // TRUNC > Integer Any Integral Any
1793 // ZEXT < Integral Unsigned Integer Any
1794 // SEXT < Integral Signed Integer Any
1795 // FPTOUI n/a FloatPt n/a Integral Unsigned
1796 // FPTOSI n/a FloatPt n/a Integral Signed
1797 // UITOFP n/a Integral Unsigned FloatPt n/a
1798 // SITOFP n/a Integral Signed FloatPt n/a
1799 // FPTRUNC > FloatPt n/a FloatPt n/a
1800 // FPEXT < FloatPt n/a FloatPt n/a
1801 // PTRTOINT n/a Pointer n/a Integral Unsigned
1802 // INTTOPTR n/a Integral Unsigned Pointer n/a
1803 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001804 //
1805 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001806 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1807 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001808 // of the produced value (we no longer know the top-part is all zeros).
1809 // Further this conversion is often much more expensive for typical hardware,
1810 // and causes issues when building libgcc. We disallow fptosi+sext for the
1811 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001812 const unsigned numCastOps =
1813 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1814 static const uint8_t CastResults[numCastOps][numCastOps] = {
1815 // T F F U S F F P I B -+
1816 // R Z S P P I I T P 2 N T |
1817 // U E E 2 2 2 2 R E I T C +- secondOp
1818 // N X X U S F F N X N 2 V |
1819 // C T T I I P P C T T P T -+
1820 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1821 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1822 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001823 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1824 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001825 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1826 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1827 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1828 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1829 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1830 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1831 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1832 };
1833
1834 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1835 [secondOp-Instruction::CastOpsBegin];
1836 switch (ElimCase) {
1837 case 0:
1838 // categorically disallowed
1839 return 0;
1840 case 1:
1841 // allowed, use first cast's opcode
1842 return firstOp;
1843 case 2:
1844 // allowed, use second cast's opcode
1845 return secondOp;
1846 case 3:
1847 // no-op cast in second op implies firstOp as long as the DestTy
1848 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001849 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001850 return firstOp;
1851 return 0;
1852 case 4:
1853 // no-op cast in second op implies firstOp as long as the DestTy
1854 // is floating point
1855 if (DstTy->isFloatingPoint())
1856 return firstOp;
1857 return 0;
1858 case 5:
1859 // no-op cast in first op implies secondOp as long as the SrcTy
1860 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001861 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001862 return secondOp;
1863 return 0;
1864 case 6:
1865 // no-op cast in first op implies secondOp as long as the SrcTy
1866 // is a floating point
1867 if (SrcTy->isFloatingPoint())
1868 return secondOp;
1869 return 0;
1870 case 7: {
1871 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00001872 if (!IntPtrTy)
1873 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001874 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1875 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001876 if (MidSize >= PtrSize)
1877 return Instruction::BitCast;
1878 return 0;
1879 }
1880 case 8: {
1881 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1882 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1883 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001884 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1885 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001886 if (SrcSize == DstSize)
1887 return Instruction::BitCast;
1888 else if (SrcSize < DstSize)
1889 return firstOp;
1890 return secondOp;
1891 }
1892 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1893 return Instruction::ZExt;
1894 case 10:
1895 // fpext followed by ftrunc is allowed if the bit size returned to is
1896 // the same as the original, in which case its just a bitcast
1897 if (SrcTy == DstTy)
1898 return Instruction::BitCast;
1899 return 0; // If the types are not the same we can't eliminate it.
1900 case 11:
1901 // bitcast followed by ptrtoint is allowed as long as the bitcast
1902 // is a pointer to pointer cast.
1903 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1904 return secondOp;
1905 return 0;
1906 case 12:
1907 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1908 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1909 return firstOp;
1910 return 0;
1911 case 13: {
1912 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00001913 if (!IntPtrTy)
1914 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001915 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1916 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1917 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001918 if (SrcSize <= PtrSize && SrcSize == DstSize)
1919 return Instruction::BitCast;
1920 return 0;
1921 }
1922 case 99:
1923 // cast combination can't happen (error in input). This is for all cases
1924 // where the MidTy is not the same for the two cast instructions.
1925 assert(!"Invalid Cast Combination");
1926 return 0;
1927 default:
1928 assert(!"Error in CastResults table!!!");
1929 return 0;
1930 }
1931 return 0;
1932}
1933
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001934CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001935 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001936 // Construct and return the appropriate CastInst subclass
1937 switch (op) {
1938 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1939 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1940 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1941 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1942 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1943 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1944 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1945 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1946 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1947 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1948 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1949 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1950 default:
1951 assert(!"Invalid opcode provided");
1952 }
1953 return 0;
1954}
1955
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001956CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001957 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001958 // Construct and return the appropriate CastInst subclass
1959 switch (op) {
1960 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1961 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1962 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1963 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1964 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1965 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1966 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1967 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1968 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1969 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1970 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1971 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1972 default:
1973 assert(!"Invalid opcode provided");
1974 }
1975 return 0;
1976}
1977
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001978CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001979 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00001980 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001981 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001982 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1983 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001984}
1985
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001986CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001987 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00001988 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001989 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001990 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1991 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001992}
1993
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001994CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001995 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00001996 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001997 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001998 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1999 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002000}
2001
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002002CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002003 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002004 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002005 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002006 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2007 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002008}
2009
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002010CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002011 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002012 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002013 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002014 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2015 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002016}
2017
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002018CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002019 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002020 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002021 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002022 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2023 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002024}
2025
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002026CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002027 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002028 BasicBlock *InsertAtEnd) {
2029 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002030 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002031 "Invalid cast");
2032
Chris Lattner03c49532007-01-15 02:27:26 +00002033 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002034 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2035 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002036}
2037
2038/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002039CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002040 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002041 Instruction *InsertBefore) {
2042 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002043 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002044 "Invalid cast");
2045
Chris Lattner03c49532007-01-15 02:27:26 +00002046 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002047 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2048 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002049}
2050
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002051CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002052 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002053 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002054 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002055 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2056 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002057 Instruction::CastOps opcode =
2058 (SrcBits == DstBits ? Instruction::BitCast :
2059 (SrcBits > DstBits ? Instruction::Trunc :
2060 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002061 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002062}
2063
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002064CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002065 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002066 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002067 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2068 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002069 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2070 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002071 Instruction::CastOps opcode =
2072 (SrcBits == DstBits ? Instruction::BitCast :
2073 (SrcBits > DstBits ? Instruction::Trunc :
2074 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002075 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002076}
2077
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002078CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002079 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002080 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002081 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002082 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002083 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2084 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002085 Instruction::CastOps opcode =
2086 (SrcBits == DstBits ? Instruction::BitCast :
2087 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002088 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002089}
2090
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002091CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002092 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002093 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002094 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002095 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002096 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2097 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002098 Instruction::CastOps opcode =
2099 (SrcBits == DstBits ? Instruction::BitCast :
2100 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002101 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002102}
2103
Duncan Sands55e50902008-01-06 10:12:28 +00002104// Check whether it is valid to call getCastOpcode for these types.
2105// This routine must be kept in sync with getCastOpcode.
2106bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2107 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2108 return false;
2109
2110 if (SrcTy == DestTy)
2111 return true;
2112
2113 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002114 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2115 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002116
2117 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002118 if (DestTy->isInteger()) { // Casting to integral
2119 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002120 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002121 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002122 return true;
2123 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002124 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002125 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002126 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002127 return isa<PointerType>(SrcTy);
2128 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002129 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2130 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002131 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002132 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002133 return true;
2134 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002135 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002136 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002137 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002138 return false;
2139 }
2140 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002141 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002142 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002143 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002144 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002145 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002146 return DestPTy->getBitWidth() == SrcBits;
2147 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002148 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2149 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002150 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002151 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002152 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002153 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002154 return false;
2155 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002156 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002157 return false;
2158 }
2159}
2160
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002161// Provide a way to get a "cast" where the cast opcode is inferred from the
2162// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002163// logic in the castIsValid function below. This axiom should hold:
2164// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2165// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002166// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002167// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002168Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002169CastInst::getCastOpcode(
2170 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002171 // Get the bit sizes, we'll need these
2172 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002173 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2174 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002175
Duncan Sands55e50902008-01-06 10:12:28 +00002176 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2177 "Only first class types are castable!");
2178
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002179 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002180 if (DestTy->isInteger()) { // Casting to integral
2181 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002182 if (DestBits < SrcBits)
2183 return Trunc; // int -> smaller int
2184 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002185 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002186 return SExt; // signed -> SEXT
2187 else
2188 return ZExt; // unsigned -> ZEXT
2189 } else {
2190 return BitCast; // Same size, No-op cast
2191 }
2192 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002193 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002194 return FPToSI; // FP -> sint
2195 else
2196 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002197 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002198 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002199 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002200 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002201 return BitCast; // Same size, no-op cast
2202 } else {
2203 assert(isa<PointerType>(SrcTy) &&
2204 "Casting from a value that is not first-class type");
2205 return PtrToInt; // ptr -> int
2206 }
2207 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002208 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002209 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002210 return SIToFP; // sint -> FP
2211 else
2212 return UIToFP; // uint -> FP
2213 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2214 if (DestBits < SrcBits) {
2215 return FPTrunc; // FP -> smaller FP
2216 } else if (DestBits > SrcBits) {
2217 return FPExt; // FP -> larger FP
2218 } else {
2219 return BitCast; // same size, no-op cast
2220 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002221 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002222 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002223 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002224 PTy = NULL;
2225 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002226 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002227 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002228 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002229 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2230 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002231 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002232 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002233 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002234 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002235 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002236 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002238 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002239 }
2240 } else if (isa<PointerType>(DestTy)) {
2241 if (isa<PointerType>(SrcTy)) {
2242 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002243 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002244 return IntToPtr; // int -> ptr
2245 } else {
2246 assert(!"Casting pointer to other than pointer or int");
2247 }
2248 } else {
2249 assert(!"Casting to type that is not first-class");
2250 }
2251
2252 // If we fall through to here we probably hit an assertion cast above
2253 // and assertions are not turned on. Anything we return is an error, so
2254 // BitCast is as good a choice as any.
2255 return BitCast;
2256}
2257
2258//===----------------------------------------------------------------------===//
2259// CastInst SubClass Constructors
2260//===----------------------------------------------------------------------===//
2261
2262/// Check that the construction parameters for a CastInst are correct. This
2263/// could be broken out into the separate constructors but it is useful to have
2264/// it in one place and to eliminate the redundant code for getting the sizes
2265/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002266bool
2267CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002268
2269 // Check for type sanity on the arguments
2270 const Type *SrcTy = S->getType();
2271 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2272 return false;
2273
2274 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002275 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2276 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002277
2278 // Switch on the opcode provided
2279 switch (op) {
2280 default: return false; // This is an input error
2281 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002282 return SrcTy->isIntOrIntVector() &&
2283 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002284 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002285 return SrcTy->isIntOrIntVector() &&
2286 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002287 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002288 return SrcTy->isIntOrIntVector() &&
2289 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002290 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002291 return SrcTy->isFPOrFPVector() &&
2292 DstTy->isFPOrFPVector() &&
2293 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002294 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002295 return SrcTy->isFPOrFPVector() &&
2296 DstTy->isFPOrFPVector() &&
2297 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002298 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002300 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2301 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002302 return SVTy->getElementType()->isIntOrIntVector() &&
2303 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002304 SVTy->getNumElements() == DVTy->getNumElements();
2305 }
2306 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002307 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002308 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002309 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002310 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2311 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002312 return SVTy->getElementType()->isFPOrFPVector() &&
2313 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002314 SVTy->getNumElements() == DVTy->getNumElements();
2315 }
2316 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002317 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002318 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002319 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002320 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002321 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002322 case Instruction::BitCast:
2323 // BitCast implies a no-op cast of type only. No bits change.
2324 // However, you can't cast pointers to anything but pointers.
2325 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2326 return false;
2327
Duncan Sands55e50902008-01-06 10:12:28 +00002328 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002329 // these cases, the cast is okay if the source and destination bit widths
2330 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002331 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002332 }
2333}
2334
2335TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002336 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002337) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002338 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002339}
2340
2341TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002342 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002343) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002344 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002345}
2346
2347ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002348 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002349) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002350 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002351}
2352
2353ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002354 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002355) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002356 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357}
2358SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002359 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002360) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002361 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362}
2363
Jeff Cohencc08c832006-12-02 02:22:01 +00002364SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002365 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002366) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002367 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002368}
2369
2370FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002371 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002372) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002373 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002374}
2375
2376FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002377 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002379 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002380}
2381
2382FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002383 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002385 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002386}
2387
2388FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002389 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002390) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002391 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392}
2393
2394UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002395 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002396) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002397 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398}
2399
2400UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002401 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002403 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002404}
2405
2406SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002407 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002409 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002410}
2411
2412SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002413 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002414) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002415 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002416}
2417
2418FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002419 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002421 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422}
2423
2424FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002425 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002426) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002427 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428}
2429
2430FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002431 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002432) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002433 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002434}
2435
2436FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002437 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002438) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002439 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002440}
2441
2442PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002443 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002445 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002446}
2447
2448PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002449 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002451 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002452}
2453
2454IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002455 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002457 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002458}
2459
2460IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002461 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002462) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002463 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464}
2465
2466BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002467 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002468) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002469 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002470}
2471
2472BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002473 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002474) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002475 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002476}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002477
2478//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002479// CmpInst Classes
2480//===----------------------------------------------------------------------===//
2481
Nate Begemand2195702008-05-12 19:01:56 +00002482CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002483 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002484 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002485 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002486 OperandTraits<CmpInst>::op_begin(this),
2487 OperandTraits<CmpInst>::operands(this),
2488 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002489 Op<0>() = LHS;
2490 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002491 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002492 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002493}
Gabor Greiff6caff662008-05-10 08:32:32 +00002494
Nate Begemand2195702008-05-12 19:01:56 +00002495CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002496 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002497 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002498 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002499 OperandTraits<CmpInst>::op_begin(this),
2500 OperandTraits<CmpInst>::operands(this),
2501 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002502 Op<0>() = LHS;
2503 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002504 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002505 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002506}
2507
2508CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002509CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002510 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002511 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002512 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002513 if (InsertBefore)
2514 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2515 S1, S2, Name);
2516 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002517 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002518 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002519 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002520
2521 if (InsertBefore)
2522 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2523 S1, S2, Name);
2524 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002525 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002526 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002527}
2528
2529CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002530CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002531 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002532 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002533 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2534 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002535 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002536 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2537 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002538}
2539
2540void CmpInst::swapOperands() {
2541 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2542 IC->swapOperands();
2543 else
2544 cast<FCmpInst>(this)->swapOperands();
2545}
2546
2547bool CmpInst::isCommutative() {
2548 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2549 return IC->isCommutative();
2550 return cast<FCmpInst>(this)->isCommutative();
2551}
2552
2553bool CmpInst::isEquality() {
2554 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2555 return IC->isEquality();
2556 return cast<FCmpInst>(this)->isEquality();
2557}
2558
2559
Dan Gohman4e724382008-05-31 02:47:54 +00002560CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002561 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002562 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002563 case ICMP_EQ: return ICMP_NE;
2564 case ICMP_NE: return ICMP_EQ;
2565 case ICMP_UGT: return ICMP_ULE;
2566 case ICMP_ULT: return ICMP_UGE;
2567 case ICMP_UGE: return ICMP_ULT;
2568 case ICMP_ULE: return ICMP_UGT;
2569 case ICMP_SGT: return ICMP_SLE;
2570 case ICMP_SLT: return ICMP_SGE;
2571 case ICMP_SGE: return ICMP_SLT;
2572 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002573
Dan Gohman4e724382008-05-31 02:47:54 +00002574 case FCMP_OEQ: return FCMP_UNE;
2575 case FCMP_ONE: return FCMP_UEQ;
2576 case FCMP_OGT: return FCMP_ULE;
2577 case FCMP_OLT: return FCMP_UGE;
2578 case FCMP_OGE: return FCMP_ULT;
2579 case FCMP_OLE: return FCMP_UGT;
2580 case FCMP_UEQ: return FCMP_ONE;
2581 case FCMP_UNE: return FCMP_OEQ;
2582 case FCMP_UGT: return FCMP_OLE;
2583 case FCMP_ULT: return FCMP_OGE;
2584 case FCMP_UGE: return FCMP_OLT;
2585 case FCMP_ULE: return FCMP_OGT;
2586 case FCMP_ORD: return FCMP_UNO;
2587 case FCMP_UNO: return FCMP_ORD;
2588 case FCMP_TRUE: return FCMP_FALSE;
2589 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002590 }
2591}
2592
Reid Spencer266e42b2006-12-23 06:05:41 +00002593ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2594 switch (pred) {
2595 default: assert(! "Unknown icmp predicate!");
2596 case ICMP_EQ: case ICMP_NE:
2597 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2598 return pred;
2599 case ICMP_UGT: return ICMP_SGT;
2600 case ICMP_ULT: return ICMP_SLT;
2601 case ICMP_UGE: return ICMP_SGE;
2602 case ICMP_ULE: return ICMP_SLE;
2603 }
2604}
2605
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002606ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2607 switch (pred) {
2608 default: assert(! "Unknown icmp predicate!");
2609 case ICMP_EQ: case ICMP_NE:
2610 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2611 return pred;
2612 case ICMP_SGT: return ICMP_UGT;
2613 case ICMP_SLT: return ICMP_ULT;
2614 case ICMP_SGE: return ICMP_UGE;
2615 case ICMP_SLE: return ICMP_ULE;
2616 }
2617}
2618
Reid Spencer266e42b2006-12-23 06:05:41 +00002619bool ICmpInst::isSignedPredicate(Predicate pred) {
2620 switch (pred) {
2621 default: assert(! "Unknown icmp predicate!");
2622 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2623 return true;
2624 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2625 case ICMP_UGE: case ICMP_ULE:
2626 return false;
2627 }
2628}
2629
Reid Spencer0286bc12007-02-28 22:00:54 +00002630/// Initialize a set of values that all satisfy the condition with C.
2631///
2632ConstantRange
2633ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2634 APInt Lower(C);
2635 APInt Upper(C);
2636 uint32_t BitWidth = C.getBitWidth();
2637 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002638 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002639 case ICmpInst::ICMP_EQ: Upper++; break;
2640 case ICmpInst::ICMP_NE: Lower++; break;
2641 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2642 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2643 case ICmpInst::ICMP_UGT:
2644 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2645 break;
2646 case ICmpInst::ICMP_SGT:
2647 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2648 break;
2649 case ICmpInst::ICMP_ULE:
2650 Lower = APInt::getMinValue(BitWidth); Upper++;
2651 break;
2652 case ICmpInst::ICMP_SLE:
2653 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2654 break;
2655 case ICmpInst::ICMP_UGE:
2656 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2657 break;
2658 case ICmpInst::ICMP_SGE:
2659 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2660 break;
2661 }
2662 return ConstantRange(Lower, Upper);
2663}
2664
Dan Gohman4e724382008-05-31 02:47:54 +00002665CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002666 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002667 default: assert(!"Unknown cmp predicate!");
2668 case ICMP_EQ: case ICMP_NE:
2669 return pred;
2670 case ICMP_SGT: return ICMP_SLT;
2671 case ICMP_SLT: return ICMP_SGT;
2672 case ICMP_SGE: return ICMP_SLE;
2673 case ICMP_SLE: return ICMP_SGE;
2674 case ICMP_UGT: return ICMP_ULT;
2675 case ICMP_ULT: return ICMP_UGT;
2676 case ICMP_UGE: return ICMP_ULE;
2677 case ICMP_ULE: return ICMP_UGE;
2678
Reid Spencerd9436b62006-11-20 01:22:35 +00002679 case FCMP_FALSE: case FCMP_TRUE:
2680 case FCMP_OEQ: case FCMP_ONE:
2681 case FCMP_UEQ: case FCMP_UNE:
2682 case FCMP_ORD: case FCMP_UNO:
2683 return pred;
2684 case FCMP_OGT: return FCMP_OLT;
2685 case FCMP_OLT: return FCMP_OGT;
2686 case FCMP_OGE: return FCMP_OLE;
2687 case FCMP_OLE: return FCMP_OGE;
2688 case FCMP_UGT: return FCMP_ULT;
2689 case FCMP_ULT: return FCMP_UGT;
2690 case FCMP_UGE: return FCMP_ULE;
2691 case FCMP_ULE: return FCMP_UGE;
2692 }
2693}
2694
Reid Spencer266e42b2006-12-23 06:05:41 +00002695bool CmpInst::isUnsigned(unsigned short predicate) {
2696 switch (predicate) {
2697 default: return false;
2698 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2699 case ICmpInst::ICMP_UGE: return true;
2700 }
2701}
2702
2703bool CmpInst::isSigned(unsigned short predicate){
2704 switch (predicate) {
2705 default: return false;
2706 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2707 case ICmpInst::ICMP_SGE: return true;
2708 }
2709}
2710
2711bool CmpInst::isOrdered(unsigned short predicate) {
2712 switch (predicate) {
2713 default: return false;
2714 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2715 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2716 case FCmpInst::FCMP_ORD: return true;
2717 }
2718}
2719
2720bool CmpInst::isUnordered(unsigned short predicate) {
2721 switch (predicate) {
2722 default: return false;
2723 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2724 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2725 case FCmpInst::FCMP_UNO: return true;
2726 }
2727}
2728
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002729//===----------------------------------------------------------------------===//
2730// SwitchInst Implementation
2731//===----------------------------------------------------------------------===//
2732
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002733void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002734 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002735 ReservedSpace = 2+NumCases*2;
2736 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002737 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002738
Gabor Greif2d3024d2008-05-26 21:33:52 +00002739 OperandList[0] = Value;
2740 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002741}
2742
Chris Lattner2195fc42007-02-24 00:55:48 +00002743/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2744/// switch on and a default destination. The number of additional cases can
2745/// be specified here to make memory allocation more efficient. This
2746/// constructor can also autoinsert before another instruction.
2747SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2748 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002749 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2750 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002751 init(Value, Default, NumCases);
2752}
2753
2754/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2755/// switch on and a default destination. The number of additional cases can
2756/// be specified here to make memory allocation more efficient. This
2757/// constructor also autoinserts at the end of the specified BasicBlock.
2758SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2759 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002760 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2761 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002762 init(Value, Default, NumCases);
2763}
2764
Misha Brukmanb1c93172005-04-21 23:48:37 +00002765SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002766 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002767 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002768 Use *OL = OperandList, *InOL = SI.OperandList;
2769 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002770 OL[i] = InOL[i];
2771 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002772 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002773 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002774}
2775
Gordon Henriksen14a55692007-12-10 02:14:30 +00002776SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002777 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002778}
2779
2780
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002781/// addCase - Add an entry to the switch instruction...
2782///
Chris Lattner47ac1872005-02-24 05:32:09 +00002783void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002784 unsigned OpNo = NumOperands;
2785 if (OpNo+2 > ReservedSpace)
2786 resizeOperands(0); // Get more space!
2787 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002788 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002789 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002790 OperandList[OpNo] = OnVal;
2791 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002792}
2793
2794/// removeCase - This method removes the specified successor from the switch
2795/// instruction. Note that this cannot be used to remove the default
2796/// destination (successor #0).
2797///
2798void SwitchInst::removeCase(unsigned idx) {
2799 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002800 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2801
2802 unsigned NumOps = getNumOperands();
2803 Use *OL = OperandList;
2804
2805 // Move everything after this operand down.
2806 //
2807 // FIXME: we could just swap with the end of the list, then erase. However,
2808 // client might not expect this to happen. The code as it is thrashes the
2809 // use/def lists, which is kinda lame.
2810 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2811 OL[i-2] = OL[i];
2812 OL[i-2+1] = OL[i+1];
2813 }
2814
2815 // Nuke the last value.
2816 OL[NumOps-2].set(0);
2817 OL[NumOps-2+1].set(0);
2818 NumOperands = NumOps-2;
2819}
2820
2821/// resizeOperands - resize operands - This adjusts the length of the operands
2822/// list according to the following behavior:
2823/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002824/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002825/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2826/// 3. If NumOps == NumOperands, trim the reserved space.
2827///
2828void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002829 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002830 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002831 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002832 } else if (NumOps*2 > NumOperands) {
2833 // No resize needed.
2834 if (ReservedSpace >= NumOps) return;
2835 } else if (NumOps == NumOperands) {
2836 if (ReservedSpace == NumOps) return;
2837 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002838 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002839 }
2840
2841 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002842 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002843 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002844 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002845 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002846 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002847 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002848 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002849}
2850
2851
2852BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2853 return getSuccessor(idx);
2854}
2855unsigned SwitchInst::getNumSuccessorsV() const {
2856 return getNumSuccessors();
2857}
2858void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2859 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002860}
Chris Lattnerf22be932004-10-15 23:52:53 +00002861
Chris Lattnerf22be932004-10-15 23:52:53 +00002862// Define these methods here so vtables don't get emitted into every translation
2863// unit that uses these classes.
2864
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002865GetElementPtrInst *GetElementPtrInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002866 GetElementPtrInst *New = new(getNumOperands()) GetElementPtrInst(*this);
2867 New->SubclassOptionalData = SubclassOptionalData;
2868 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00002869}
2870
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002871BinaryOperator *BinaryOperator::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002872 BinaryOperator *New = Create(getOpcode(), Op<0>(), Op<1>());
2873 New->SubclassOptionalData = SubclassOptionalData;
2874 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00002875}
2876
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002877FCmpInst* FCmpInst::clone(LLVMContext &Context) const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002878 FCmpInst *New = new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002879 New->SubclassOptionalData = SubclassOptionalData;
2880 return New;
Chris Lattner0b490b02007-08-24 20:48:18 +00002881}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002882ICmpInst* ICmpInst::clone(LLVMContext &Context) const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002883 ICmpInst *New = new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002884 New->SubclassOptionalData = SubclassOptionalData;
2885 return New;
Reid Spencerd9436b62006-11-20 01:22:35 +00002886}
2887
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002888ExtractValueInst *ExtractValueInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002889 ExtractValueInst *New = new ExtractValueInst(*this);
2890 New->SubclassOptionalData = SubclassOptionalData;
2891 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00002892}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002893InsertValueInst *InsertValueInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002894 InsertValueInst *New = new InsertValueInst(*this);
2895 New->SubclassOptionalData = SubclassOptionalData;
2896 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00002897}
2898
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002899MallocInst *MallocInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002900 MallocInst *New = new MallocInst(getAllocatedType(),
2901 (Value*)getOperand(0),
2902 getAlignment());
2903 New->SubclassOptionalData = SubclassOptionalData;
2904 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002905}
Dan Gohman0752bff2008-05-23 00:36:11 +00002906
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002907AllocaInst *AllocaInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002908 AllocaInst *New = new AllocaInst(getAllocatedType(),
2909 (Value*)getOperand(0),
2910 getAlignment());
2911 New->SubclassOptionalData = SubclassOptionalData;
2912 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002913}
2914
2915FreeInst *FreeInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002916 FreeInst *New = new FreeInst(getOperand(0));
2917 New->SubclassOptionalData = SubclassOptionalData;
2918 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002919}
2920
2921LoadInst *LoadInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002922 LoadInst *New = new LoadInst(getOperand(0),
2923 Twine(), isVolatile(),
2924 getAlignment());
2925 New->SubclassOptionalData = SubclassOptionalData;
2926 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002927}
2928
2929StoreInst *StoreInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002930 StoreInst *New = new StoreInst(getOperand(0), getOperand(1),
2931 isVolatile(), getAlignment());
2932 New->SubclassOptionalData = SubclassOptionalData;
2933 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002934}
2935
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002936TruncInst *TruncInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002937 TruncInst *New = new TruncInst(getOperand(0), getType());
2938 New->SubclassOptionalData = SubclassOptionalData;
2939 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002940}
2941
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002942ZExtInst *ZExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002943 ZExtInst *New = new ZExtInst(getOperand(0), getType());
2944 New->SubclassOptionalData = SubclassOptionalData;
2945 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002946}
2947
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002948SExtInst *SExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002949 SExtInst *New = new SExtInst(getOperand(0), getType());
2950 New->SubclassOptionalData = SubclassOptionalData;
2951 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002952}
2953
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002954FPTruncInst *FPTruncInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002955 FPTruncInst *New = new FPTruncInst(getOperand(0), getType());
2956 New->SubclassOptionalData = SubclassOptionalData;
2957 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002958}
2959
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002960FPExtInst *FPExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002961 FPExtInst *New = new FPExtInst(getOperand(0), getType());
2962 New->SubclassOptionalData = SubclassOptionalData;
2963 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002964}
2965
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002966UIToFPInst *UIToFPInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002967 UIToFPInst *New = new UIToFPInst(getOperand(0), getType());
2968 New->SubclassOptionalData = SubclassOptionalData;
2969 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002970}
2971
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002972SIToFPInst *SIToFPInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002973 SIToFPInst *New = new SIToFPInst(getOperand(0), getType());
2974 New->SubclassOptionalData = SubclassOptionalData;
2975 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002976}
2977
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002978FPToUIInst *FPToUIInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002979 FPToUIInst *New = new FPToUIInst(getOperand(0), getType());
2980 New->SubclassOptionalData = SubclassOptionalData;
2981 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002982}
2983
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002984FPToSIInst *FPToSIInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002985 FPToSIInst *New = new FPToSIInst(getOperand(0), getType());
2986 New->SubclassOptionalData = SubclassOptionalData;
2987 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002988}
2989
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002990PtrToIntInst *PtrToIntInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002991 PtrToIntInst *New = new PtrToIntInst(getOperand(0), getType());
2992 New->SubclassOptionalData = SubclassOptionalData;
2993 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002994}
2995
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002996IntToPtrInst *IntToPtrInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002997 IntToPtrInst *New = new IntToPtrInst(getOperand(0), getType());
2998 New->SubclassOptionalData = SubclassOptionalData;
2999 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003000}
3001
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003002BitCastInst *BitCastInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003003 BitCastInst *New = new BitCastInst(getOperand(0), getType());
3004 New->SubclassOptionalData = SubclassOptionalData;
3005 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003006}
3007
3008CallInst *CallInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003009 CallInst *New = new(getNumOperands()) CallInst(*this);
3010 New->SubclassOptionalData = SubclassOptionalData;
3011 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003012}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003013
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003014SelectInst *SelectInst::clone(LLVMContext&) const {
3015 SelectInst *New = SelectInst::Create(getOperand(0),
3016 getOperand(1),
3017 getOperand(2));
3018 New->SubclassOptionalData = SubclassOptionalData;
3019 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003020}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003021
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003022VAArgInst *VAArgInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003023 VAArgInst *New = new VAArgInst(getOperand(0), getType());
3024 New->SubclassOptionalData = SubclassOptionalData;
3025 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003026}
3027
3028ExtractElementInst *ExtractElementInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003029 ExtractElementInst *New = ExtractElementInst::Create(getOperand(0),
3030 getOperand(1));
3031 New->SubclassOptionalData = SubclassOptionalData;
3032 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003033}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003034
3035InsertElementInst *InsertElementInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003036 InsertElementInst *New = InsertElementInst::Create(getOperand(0),
3037 getOperand(1),
3038 getOperand(2));
3039 New->SubclassOptionalData = SubclassOptionalData;
3040 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003041}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003042
3043ShuffleVectorInst *ShuffleVectorInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003044 ShuffleVectorInst *New = new ShuffleVectorInst(getOperand(0),
3045 getOperand(1),
3046 getOperand(2));
3047 New->SubclassOptionalData = SubclassOptionalData;
3048 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003049}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003050
3051PHINode *PHINode::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003052 PHINode *New = new PHINode(*this);
3053 New->SubclassOptionalData = SubclassOptionalData;
3054 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003055}
3056
3057ReturnInst *ReturnInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003058 ReturnInst *New = new(getNumOperands()) ReturnInst(*this);
3059 New->SubclassOptionalData = SubclassOptionalData;
3060 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003061}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003062
3063BranchInst *BranchInst::clone(LLVMContext&) const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003064 unsigned Ops(getNumOperands());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003065 BranchInst *New = new(Ops, Ops == 1) BranchInst(*this);
3066 New->SubclassOptionalData = SubclassOptionalData;
3067 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003068}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003069
3070SwitchInst *SwitchInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003071 SwitchInst *New = new SwitchInst(*this);
3072 New->SubclassOptionalData = SubclassOptionalData;
3073 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003074}
3075
3076InvokeInst *InvokeInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003077 InvokeInst *New = new(getNumOperands()) InvokeInst(*this);
3078 New->SubclassOptionalData = SubclassOptionalData;
3079 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003080}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003081
Owen Anderson55f1c092009-08-13 21:58:54 +00003082UnwindInst *UnwindInst::clone(LLVMContext &C) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003083 UnwindInst *New = new UnwindInst(C);
3084 New->SubclassOptionalData = SubclassOptionalData;
3085 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003086}
3087
Owen Anderson55f1c092009-08-13 21:58:54 +00003088UnreachableInst *UnreachableInst::clone(LLVMContext &C) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003089 UnreachableInst *New = new UnreachableInst(C);
3090 New->SubclassOptionalData = SubclassOptionalData;
3091 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003092}