blob: 90af9f1ea74536f1d5e77dda08c6795471c7fbdf [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"
19#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000020#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000021#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000022using namespace llvm;
23
Chris Lattner3e13b8c2008-01-02 23:42:30 +000024//===----------------------------------------------------------------------===//
25// CallSite Class
26//===----------------------------------------------------------------------===//
27
Gabor Greif1b9921a2009-01-11 22:33:22 +000028#define CALLSITE_DELEGATE_GETTER(METHOD) \
29 Instruction *II(getInstruction()); \
30 return isCall() \
31 ? cast<CallInst>(II)->METHOD \
32 : cast<InvokeInst>(II)->METHOD
33
34#define CALLSITE_DELEGATE_SETTER(METHOD) \
35 Instruction *II(getInstruction()); \
36 if (isCall()) \
37 cast<CallInst>(II)->METHOD; \
38 else \
39 cast<InvokeInst>(II)->METHOD
40
Duncan Sands85fab3a2008-02-18 17:32:13 +000041CallSite::CallSite(Instruction *C) {
42 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
Gabor Greif1b9921a2009-01-11 22:33:22 +000043 I.setPointer(C);
44 I.setInt(isa<CallInst>(C));
Duncan Sands85fab3a2008-02-18 17:32:13 +000045}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000046unsigned CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000047 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000048}
49void CallSite::setCallingConv(unsigned CC) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000050 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
Chris Lattnerf7b6d312005-05-06 20:26:43 +000051}
Devang Patel4c758ea2008-09-25 21:00:45 +000052const AttrListPtr &CallSite::getAttributes() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000053 CALLSITE_DELEGATE_GETTER(getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000054}
Devang Patel4c758ea2008-09-25 21:00:45 +000055void CallSite::setAttributes(const AttrListPtr &PAL) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000056 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000057}
Devang Patelba3fa6c2008-09-23 23:03:40 +000058bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000059 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
Duncan Sands5208d1a2007-11-28 17:07:01 +000060}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000061uint16_t CallSite::getParamAlignment(uint16_t i) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000062 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
Dale Johanneseneabc5f32008-02-22 17:49:45 +000063}
Duncan Sands38ef3a82007-12-03 20:06:50 +000064bool CallSite::doesNotAccessMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000065 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000066}
Duncan Sands78c88722008-07-08 08:38:44 +000067void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000068 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000069}
Duncan Sands38ef3a82007-12-03 20:06:50 +000070bool CallSite::onlyReadsMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000071 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000072}
Duncan Sands78c88722008-07-08 08:38:44 +000073void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000074 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000075}
76bool CallSite::doesNotReturn() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000077 CALLSITE_DELEGATE_GETTER(doesNotReturn());
Duncan Sands78c88722008-07-08 08:38:44 +000078}
79void CallSite::setDoesNotReturn(bool doesNotReturn) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000080 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
Duncan Sands78c88722008-07-08 08:38:44 +000081}
Duncan Sands3353ed02007-12-18 09:59:50 +000082bool CallSite::doesNotThrow() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000083 CALLSITE_DELEGATE_GETTER(doesNotThrow());
Duncan Sands8e4847e2007-12-16 15:51:49 +000084}
Duncan Sandsaa31b922007-12-19 21:13:37 +000085void CallSite::setDoesNotThrow(bool doesNotThrow) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000086 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
Duncan Sandsaa31b922007-12-19 21:13:37 +000087}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000088
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000089bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000090 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
91 if (AI->get() == Arg)
92 return true;
93 return false;
94}
95
Gabor Greif1b9921a2009-01-11 22:33:22 +000096#undef CALLSITE_DELEGATE_GETTER
97#undef CALLSITE_DELEGATE_SETTER
98
Gordon Henriksen14a55692007-12-10 02:14:30 +000099//===----------------------------------------------------------------------===//
100// TerminatorInst Class
101//===----------------------------------------------------------------------===//
102
103// Out of line virtual method, so the vtable, etc has a home.
104TerminatorInst::~TerminatorInst() {
105}
106
Gabor Greiff6caff662008-05-10 08:32:32 +0000107//===----------------------------------------------------------------------===//
108// UnaryInstruction Class
109//===----------------------------------------------------------------------===//
110
Gordon Henriksen14a55692007-12-10 02:14:30 +0000111// Out of line virtual method, so the vtable, etc has a home.
112UnaryInstruction::~UnaryInstruction() {
113}
114
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000115//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000116// SelectInst Class
117//===----------------------------------------------------------------------===//
118
119/// areInvalidOperands - Return a string if the specified operands are invalid
120/// for a select operation, otherwise return null.
121const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
122 if (Op1->getType() != Op2->getType())
123 return "both values to select must have same type";
124
125 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
126 // Vector select.
127 if (VT->getElementType() != Type::Int1Ty)
128 return "vector select condition element type must be i1";
129 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
130 if (ET == 0)
131 return "selected values for vector select must be vectors";
132 if (ET->getNumElements() != VT->getNumElements())
133 return "vector select requires selected vectors to have "
134 "the same vector length as select condition";
135 } else if (Op0->getType() != Type::Int1Ty) {
136 return "select condition must be i1 or <n x i1>";
137 }
138 return 0;
139}
140
141
142//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000143// PHINode Class
144//===----------------------------------------------------------------------===//
145
146PHINode::PHINode(const PHINode &PN)
147 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000148 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149 ReservedSpace(PN.getNumOperands()) {
150 Use *OL = OperandList;
151 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000152 OL[i] = PN.getOperand(i);
153 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000154 }
155}
156
Gordon Henriksen14a55692007-12-10 02:14:30 +0000157PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000158 if (OperandList)
159 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000160}
161
162// removeIncomingValue - Remove an incoming value. This is useful if a
163// predecessor basic block is deleted.
164Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
165 unsigned NumOps = getNumOperands();
166 Use *OL = OperandList;
167 assert(Idx*2 < NumOps && "BB not in PHI node!");
168 Value *Removed = OL[Idx*2];
169
170 // Move everything after this operand down.
171 //
172 // FIXME: we could just swap with the end of the list, then erase. However,
173 // client might not expect this to happen. The code as it is thrashes the
174 // use/def lists, which is kinda lame.
175 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
176 OL[i-2] = OL[i];
177 OL[i-2+1] = OL[i+1];
178 }
179
180 // Nuke the last value.
181 OL[NumOps-2].set(0);
182 OL[NumOps-2+1].set(0);
183 NumOperands = NumOps-2;
184
185 // If the PHI node is dead, because it has zero entries, nuke it now.
186 if (NumOps == 2 && DeletePHIIfEmpty) {
187 // If anyone is using this PHI, make them use a dummy value instead...
188 replaceAllUsesWith(UndefValue::get(getType()));
189 eraseFromParent();
190 }
191 return Removed;
192}
193
194/// resizeOperands - resize operands - This adjusts the length of the operands
195/// list according to the following behavior:
196/// 1. If NumOps == 0, grow the operand list in response to a push_back style
197/// of operation. This grows the number of ops by 1.5 times.
198/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
199/// 3. If NumOps == NumOperands, trim the reserved space.
200///
201void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000202 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000203 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000204 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000205 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
206 } else if (NumOps*2 > NumOperands) {
207 // No resize needed.
208 if (ReservedSpace >= NumOps) return;
209 } else if (NumOps == NumOperands) {
210 if (ReservedSpace == NumOps) return;
211 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000212 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000213 }
214
215 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000216 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000217 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000218 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000219 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000220 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000221}
222
Nate Begemanb3923212005-08-04 23:24:19 +0000223/// hasConstantValue - If the specified PHI node always merges together the same
224/// value, return the value, otherwise return null.
225///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000226Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000227 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000228 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000229 if (getIncomingValue(0) != this) // not X = phi X
230 return getIncomingValue(0);
231 else
232 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000233 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000234
Nate Begemanb3923212005-08-04 23:24:19 +0000235 // Otherwise if all of the incoming values are the same for the PHI, replace
236 // the PHI node with the incoming value.
237 //
238 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000239 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000240 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000241 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000242 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000243 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000244 if (InVal && getIncomingValue(i) != InVal)
245 return 0; // Not the same, bail out.
246 else
247 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000248 }
Nate Begemanb3923212005-08-04 23:24:19 +0000249
250 // The only case that could cause InVal to be null is if we have a PHI node
251 // that only has entries for itself. In this case, there is no entry into the
252 // loop, so kill the PHI.
253 //
254 if (InVal == 0) InVal = UndefValue::get(getType());
255
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000256 // If we have a PHI node like phi(X, undef, X), where X is defined by some
257 // instruction, we cannot always return X as the result of the PHI node. Only
258 // do this if X is not an instruction (thus it must dominate the PHI block),
259 // or if the client is prepared to deal with this possibility.
260 if (HasUndefInput && !AllowNonDominatingInstruction)
261 if (Instruction *IV = dyn_cast<Instruction>(InVal))
262 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000263 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000264 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000265 return 0; // Cannot guarantee that InVal dominates this PHINode.
266
Nate Begemanb3923212005-08-04 23:24:19 +0000267 // All of the incoming values are the same, return the value now.
268 return InVal;
269}
270
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000271
272//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273// CallInst Implementation
274//===----------------------------------------------------------------------===//
275
Gordon Henriksen14a55692007-12-10 02:14:30 +0000276CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000277}
278
Chris Lattner054ba2c2007-02-13 00:58:44 +0000279void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000280 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
281 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000282 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000283
Misha Brukmanb1c93172005-04-21 23:48:37 +0000284 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000286 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287
Chris Lattner054ba2c2007-02-13 00:58:44 +0000288 assert((NumParams == FTy->getNumParams() ||
289 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000290 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000291 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000292 assert((i >= FTy->getNumParams() ||
293 FTy->getParamType(i) == Params[i]->getType()) &&
294 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000295 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000296 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000297}
298
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000299void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000300 assert(NumOperands == 3 && "NumOperands not set up?");
301 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000302 OL[0] = Func;
303 OL[1] = Actual1;
304 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000305
306 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000308 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000310 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000311 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000313 assert((0 >= FTy->getNumParams() ||
314 FTy->getParamType(0) == Actual1->getType()) &&
315 "Calling a function with a bad signature!");
316 assert((1 >= FTy->getNumParams() ||
317 FTy->getParamType(1) == Actual2->getType()) &&
318 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000319}
320
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000321void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000322 assert(NumOperands == 2 && "NumOperands not set up?");
323 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000324 OL[0] = Func;
325 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000326
327 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000328 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000329 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000331 assert((FTy->getNumParams() == 1 ||
332 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000333 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000334 assert((0 == FTy->getNumParams() ||
335 FTy->getParamType(0) == Actual->getType()) &&
336 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000337}
338
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000339void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000340 assert(NumOperands == 1 && "NumOperands not set up?");
341 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000342 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000343
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000344 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000345 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000346 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000347
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000348 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000349}
350
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000351CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000352 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000353 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
354 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000355 Instruction::Call,
356 OperandTraits<CallInst>::op_end(this) - 2,
357 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000358 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000359 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000360}
361
362CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
363 BasicBlock *InsertAtEnd)
364 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
365 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000366 Instruction::Call,
367 OperandTraits<CallInst>::op_end(this) - 2,
368 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000369 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000370 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000372CallInst::CallInst(Value *Func, const std::string &Name,
373 Instruction *InsertBefore)
374 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
375 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000376 Instruction::Call,
377 OperandTraits<CallInst>::op_end(this) - 1,
378 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000379 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000380 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000381}
382
383CallInst::CallInst(Value *Func, const std::string &Name,
384 BasicBlock *InsertAtEnd)
385 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
386 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000387 Instruction::Call,
388 OperandTraits<CallInst>::op_end(this) - 1,
389 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000390 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000391 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000392}
393
Misha Brukmanb1c93172005-04-21 23:48:37 +0000394CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000395 : Instruction(CI.getType(), Instruction::Call,
396 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000397 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000398 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000399 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000400 Use *OL = OperandList;
401 Use *InOL = CI.OperandList;
402 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000403 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000404}
405
Devang Patel4c758ea2008-09-25 21:00:45 +0000406void CallInst::addAttribute(unsigned i, Attributes attr) {
407 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000408 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000409 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000410}
411
Devang Patel4c758ea2008-09-25 21:00:45 +0000412void CallInst::removeAttribute(unsigned i, Attributes attr) {
413 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000414 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000415 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000416}
417
Devang Patelba3fa6c2008-09-23 23:03:40 +0000418bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000419 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000420 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000421 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000422 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000423 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000424}
425
Duncan Sands5208d1a2007-11-28 17:07:01 +0000426
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000427//===----------------------------------------------------------------------===//
428// InvokeInst Implementation
429//===----------------------------------------------------------------------===//
430
431void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000432 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000433 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
434 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000435 OL[0] = Fn;
436 OL[1] = IfNormal;
437 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000438 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000439 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000440 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000441
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000442 assert(((NumArgs == FTy->getNumParams()) ||
443 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000444 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000445
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000446 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000447 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000448 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000449 "Invoking a function with a bad signature!");
450
Gabor Greif2d3024d2008-05-26 21:33:52 +0000451 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000452 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000453}
454
Misha Brukmanb1c93172005-04-21 23:48:37 +0000455InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000456 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000457 OperandTraits<InvokeInst>::op_end(this)
458 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000459 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000460 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000461 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000462 Use *OL = OperandList, *InOL = II.OperandList;
463 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000464 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000465}
466
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000467BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
468 return getSuccessor(idx);
469}
470unsigned InvokeInst::getNumSuccessorsV() const {
471 return getNumSuccessors();
472}
473void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
474 return setSuccessor(idx, B);
475}
476
Devang Patelba3fa6c2008-09-23 23:03:40 +0000477bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000478 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000479 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000480 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000481 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000482 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000483}
484
Devang Patel4c758ea2008-09-25 21:00:45 +0000485void InvokeInst::addAttribute(unsigned i, Attributes attr) {
486 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000487 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000488 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000489}
490
Devang Patel4c758ea2008-09-25 21:00:45 +0000491void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
492 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000493 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000494 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000495}
496
Duncan Sands5208d1a2007-11-28 17:07:01 +0000497
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000498//===----------------------------------------------------------------------===//
499// ReturnInst Implementation
500//===----------------------------------------------------------------------===//
501
Chris Lattner2195fc42007-02-24 00:55:48 +0000502ReturnInst::ReturnInst(const ReturnInst &RI)
503 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000504 OperandTraits<ReturnInst>::op_end(this) -
505 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000506 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000507 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000508 Op<0>() = RI.Op<0>();
Chris Lattner2195fc42007-02-24 00:55:48 +0000509}
510
511ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000512 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000513 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
514 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000515 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000516 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000517}
518ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000519 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000520 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
521 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000522 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000523 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000524}
525ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000526 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000527 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000528}
529
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000530unsigned ReturnInst::getNumSuccessorsV() const {
531 return getNumSuccessors();
532}
533
Devang Patelae682fb2008-02-26 17:56:20 +0000534/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
535/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000536void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000537 assert(0 && "ReturnInst has no successors!");
538}
539
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000540BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
541 assert(0 && "ReturnInst has no successors!");
542 abort();
543 return 0;
544}
545
Devang Patel59643e52008-02-23 00:35:18 +0000546ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000547}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000548
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000549//===----------------------------------------------------------------------===//
550// UnwindInst Implementation
551//===----------------------------------------------------------------------===//
552
Chris Lattner2195fc42007-02-24 00:55:48 +0000553UnwindInst::UnwindInst(Instruction *InsertBefore)
554 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
555}
556UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
557 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
558}
559
560
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000561unsigned UnwindInst::getNumSuccessorsV() const {
562 return getNumSuccessors();
563}
564
565void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000566 assert(0 && "UnwindInst has no successors!");
567}
568
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000569BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
570 assert(0 && "UnwindInst has no successors!");
571 abort();
572 return 0;
573}
574
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000575//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000576// UnreachableInst Implementation
577//===----------------------------------------------------------------------===//
578
Chris Lattner2195fc42007-02-24 00:55:48 +0000579UnreachableInst::UnreachableInst(Instruction *InsertBefore)
580 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
581}
582UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
583 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
584}
585
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000586unsigned UnreachableInst::getNumSuccessorsV() const {
587 return getNumSuccessors();
588}
589
590void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
591 assert(0 && "UnwindInst has no successors!");
592}
593
594BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
595 assert(0 && "UnwindInst has no successors!");
596 abort();
597 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000598}
599
600//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000601// BranchInst Implementation
602//===----------------------------------------------------------------------===//
603
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000604void BranchInst::AssertOK() {
605 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000606 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000607 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000608}
609
Chris Lattner2195fc42007-02-24 00:55:48 +0000610BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000611 : TerminatorInst(Type::VoidTy, Instruction::Br,
612 OperandTraits<BranchInst>::op_end(this) - 1,
613 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000614 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000615 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000616}
617BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
618 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000619 : TerminatorInst(Type::VoidTy, Instruction::Br,
620 OperandTraits<BranchInst>::op_end(this) - 3,
621 3, InsertBefore) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000622 Op<0>() = IfTrue;
623 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000624 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000625#ifndef NDEBUG
626 AssertOK();
627#endif
628}
629
630BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000631 : TerminatorInst(Type::VoidTy, Instruction::Br,
632 OperandTraits<BranchInst>::op_end(this) - 1,
633 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000634 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000635 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000636}
637
638BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
639 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000640 : TerminatorInst(Type::VoidTy, Instruction::Br,
641 OperandTraits<BranchInst>::op_end(this) - 3,
642 3, InsertAtEnd) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000643 Op<0>() = IfTrue;
644 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000645 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000646#ifndef NDEBUG
647 AssertOK();
648#endif
649}
650
651
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000652BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000653 TerminatorInst(Type::VoidTy, Instruction::Br,
654 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
655 BI.getNumOperands()) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000656 OperandList[0] = BI.getOperand(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000657 if (BI.getNumOperands() != 1) {
658 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000659 OperandList[1] = BI.getOperand(1);
660 OperandList[2] = BI.getOperand(2);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000661 }
662}
663
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000664BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
665 return getSuccessor(idx);
666}
667unsigned BranchInst::getNumSuccessorsV() const {
668 return getNumSuccessors();
669}
670void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
671 setSuccessor(idx, B);
672}
673
674
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000675//===----------------------------------------------------------------------===//
676// AllocationInst Implementation
677//===----------------------------------------------------------------------===//
678
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000679static Value *getAISize(Value *Amt) {
680 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000681 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000682 else {
683 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000684 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000685 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000686 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000687 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000688 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000689}
690
Misha Brukmanb1c93172005-04-21 23:48:37 +0000691AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000692 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000693 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000694 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000695 InsertBefore) {
696 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000697 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000698 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000699}
700
Misha Brukmanb1c93172005-04-21 23:48:37 +0000701AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000702 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000703 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000704 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000705 InsertAtEnd) {
706 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000707 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000708 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000709}
710
Gordon Henriksen14a55692007-12-10 02:14:30 +0000711// Out of line virtual method, so the vtable, etc has a home.
712AllocationInst::~AllocationInst() {
713}
714
Dan Gohmanaa583d72008-03-24 16:55:58 +0000715void AllocationInst::setAlignment(unsigned Align) {
716 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
717 SubclassData = Log2_32(Align) + 1;
718 assert(getAlignment() == Align && "Alignment representation error!");
719}
720
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000721bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000722 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
723 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000724 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725}
726
727const Type *AllocationInst::getAllocatedType() const {
728 return getType()->getElementType();
729}
730
731AllocaInst::AllocaInst(const AllocaInst &AI)
732 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000733 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000734}
735
Chris Lattner8b291e62008-11-26 02:54:17 +0000736/// isStaticAlloca - Return true if this alloca is in the entry block of the
737/// function and is a constant size. If so, the code generator will fold it
738/// into the prolog/epilog code, so it is basically free.
739bool AllocaInst::isStaticAlloca() const {
740 // Must be constant size.
741 if (!isa<ConstantInt>(getArraySize())) return false;
742
743 // Must be in the entry block.
744 const BasicBlock *Parent = getParent();
745 return Parent == &Parent->getParent()->front();
746}
747
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000748MallocInst::MallocInst(const MallocInst &MI)
749 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000750 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000751}
752
753//===----------------------------------------------------------------------===//
754// FreeInst Implementation
755//===----------------------------------------------------------------------===//
756
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000757void FreeInst::AssertOK() {
758 assert(isa<PointerType>(getOperand(0)->getType()) &&
759 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000760}
761
762FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000763 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000764 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000765}
766
767FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000768 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000769 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000770}
771
772
773//===----------------------------------------------------------------------===//
774// LoadInst Implementation
775//===----------------------------------------------------------------------===//
776
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000777void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000778 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000779 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000780}
781
782LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000783 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000784 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000785 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000786 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000787 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000788 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000789}
790
791LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000792 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000793 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000794 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000795 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000796 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000797 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000798}
799
800LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
801 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000802 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000803 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000804 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000805 setAlignment(0);
806 AssertOK();
807 setName(Name);
808}
809
810LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
811 unsigned Align, Instruction *InsertBef)
812 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
813 Load, Ptr, InsertBef) {
814 setVolatile(isVolatile);
815 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000816 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000817 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000818}
819
Dan Gohman68659282007-07-18 20:51:11 +0000820LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
821 unsigned Align, BasicBlock *InsertAE)
822 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
823 Load, Ptr, InsertAE) {
824 setVolatile(isVolatile);
825 setAlignment(Align);
826 AssertOK();
827 setName(Name);
828}
829
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000830LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
831 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000832 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000833 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000834 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000835 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000836 AssertOK();
837 setName(Name);
838}
839
840
841
842LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000843 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
844 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000845 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000846 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000847 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000848 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000849}
850
851LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000852 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
853 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000854 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000855 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000856 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000857 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000858}
859
860LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
861 Instruction *InsertBef)
862: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000863 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000864 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000865 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000866 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000867 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000868}
869
870LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
871 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000872 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
873 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000874 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000875 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000876 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000877 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000878}
879
Christopher Lamb84485702007-04-22 19:24:39 +0000880void LoadInst::setAlignment(unsigned Align) {
881 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
882 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
883}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000884
885//===----------------------------------------------------------------------===//
886// StoreInst Implementation
887//===----------------------------------------------------------------------===//
888
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000889void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000890 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000891 assert(isa<PointerType>(getOperand(1)->getType()) &&
892 "Ptr must have pointer type!");
893 assert(getOperand(0)->getType() ==
894 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000895 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000896}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000897
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000898
899StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000900 : Instruction(Type::VoidTy, Store,
901 OperandTraits<StoreInst>::op_begin(this),
902 OperandTraits<StoreInst>::operands(this),
903 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000904 Op<0>() = val;
905 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000906 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000907 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000908 AssertOK();
909}
910
911StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000912 : Instruction(Type::VoidTy, Store,
913 OperandTraits<StoreInst>::op_begin(this),
914 OperandTraits<StoreInst>::operands(this),
915 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000916 Op<0>() = val;
917 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000918 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000919 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000920 AssertOK();
921}
922
Misha Brukmanb1c93172005-04-21 23:48:37 +0000923StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000924 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000925 : Instruction(Type::VoidTy, Store,
926 OperandTraits<StoreInst>::op_begin(this),
927 OperandTraits<StoreInst>::operands(this),
928 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000929 Op<0>() = val;
930 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000931 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000932 setAlignment(0);
933 AssertOK();
934}
935
936StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
937 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000938 : Instruction(Type::VoidTy, Store,
939 OperandTraits<StoreInst>::op_begin(this),
940 OperandTraits<StoreInst>::operands(this),
941 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000942 Op<0>() = val;
943 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000944 setVolatile(isVolatile);
945 setAlignment(Align);
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,
Dan Gohman68659282007-07-18 20:51:11 +0000950 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000951 : Instruction(Type::VoidTy, Store,
952 OperandTraits<StoreInst>::op_begin(this),
953 OperandTraits<StoreInst>::operands(this),
954 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000955 Op<0>() = val;
956 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000957 setVolatile(isVolatile);
958 setAlignment(Align);
959 AssertOK();
960}
961
962StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000963 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000964 : Instruction(Type::VoidTy, Store,
965 OperandTraits<StoreInst>::op_begin(this),
966 OperandTraits<StoreInst>::operands(this),
967 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000968 Op<0>() = val;
969 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000970 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000971 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000972 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000973}
974
Christopher Lamb84485702007-04-22 19:24:39 +0000975void StoreInst::setAlignment(unsigned Align) {
976 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
977 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
978}
979
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000980//===----------------------------------------------------------------------===//
981// GetElementPtrInst Implementation
982//===----------------------------------------------------------------------===//
983
Christopher Lambedf07882007-12-17 01:12:55 +0000984static unsigned retrieveAddrSpace(const Value *Val) {
985 return cast<PointerType>(Val->getType())->getAddressSpace();
986}
987
Gabor Greif21ba1842008-06-06 20:28:12 +0000988void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Nate Begeman8a4afd92008-07-25 17:24:13 +0000989 const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000990 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
991 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000992 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000993
Chris Lattner79807c3d2007-01-31 19:47:18 +0000994 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000995 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +0000996
997 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000998}
999
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001000void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001001 assert(NumOperands == 2 && "NumOperands not initialized?");
1002 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001003 OL[0] = Ptr;
1004 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001005
1006 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001007}
1008
Gabor Greiff6caff662008-05-10 08:32:32 +00001009GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001010 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001011 OperandTraits<GetElementPtrInst>::op_end(this)
1012 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001013 GEPI.getNumOperands()) {
1014 Use *OL = OperandList;
1015 Use *GEPIOL = GEPI.OperandList;
1016 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001017 OL[i] = GEPIOL[i];
Gabor Greiff6caff662008-05-10 08:32:32 +00001018}
1019
Chris Lattner82981202005-05-03 05:43:30 +00001020GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1021 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001022 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001023 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001024 GetElementPtr,
1025 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1026 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001027 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001028}
1029
1030GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1031 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001032 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001033 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001034 GetElementPtr,
1035 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1036 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001037 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001038}
1039
Chris Lattner6090a422009-03-09 04:46:40 +00001040/// getIndexedType - Returns the type of the element that would be accessed with
1041/// a gep instruction with the specified parameters.
1042///
1043/// The Idxs pointer should point to a continuous piece of memory containing the
1044/// indices, either as Value* or uint64_t.
1045///
1046/// A null type is returned if the indices are invalid for the specified
1047/// pointer type.
1048///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001049template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001050static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1051 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001052 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1053 if (!PTy) return 0; // Type isn't a pointer type!
1054 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001055
Chris Lattner6090a422009-03-09 04:46:40 +00001056 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001057 if (NumIdx == 0)
1058 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001059
1060 // If there is at least one index, the top level type must be sized, otherwise
1061 // it cannot be 'stepped over'.
1062 if (!Agg->isSized())
1063 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001064
Dan Gohman1ecaf452008-05-31 00:58:22 +00001065 unsigned CurIdx = 1;
1066 for (; CurIdx != NumIdx; ++CurIdx) {
1067 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1068 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001069 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001070 if (!CT->indexValid(Index)) return 0;
1071 Agg = CT->getTypeAtIndex(Index);
1072
1073 // If the new type forwards to another type, then it is in the middle
1074 // of being refined to another type (and hence, may have dropped all
1075 // references to what it was using before). So, use the new forwarded
1076 // type.
1077 if (const Type *Ty = Agg->getForwardedType())
1078 Agg = Ty;
1079 }
1080 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001081}
1082
Matthijs Kooijman04468622008-07-29 08:46:11 +00001083const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1084 Value* const *Idxs,
1085 unsigned NumIdx) {
1086 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1087}
1088
1089const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1090 uint64_t const *Idxs,
1091 unsigned NumIdx) {
1092 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1093}
1094
Chris Lattner82981202005-05-03 05:43:30 +00001095const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1096 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1097 if (!PTy) return 0; // Type isn't a pointer type!
1098
1099 // Check the pointer index.
1100 if (!PTy->indexValid(Idx)) return 0;
1101
Chris Lattnerc2233332005-05-03 16:44:45 +00001102 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001103}
1104
Chris Lattner45f15572007-04-14 00:12:57 +00001105
1106/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1107/// zeros. If so, the result pointer and the first operand have the same
1108/// value, just potentially different types.
1109bool GetElementPtrInst::hasAllZeroIndices() const {
1110 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1111 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1112 if (!CI->isZero()) return false;
1113 } else {
1114 return false;
1115 }
1116 }
1117 return true;
1118}
1119
Chris Lattner27058292007-04-27 20:35:56 +00001120/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1121/// constant integers. If so, the result pointer and the first operand have
1122/// a constant offset between them.
1123bool GetElementPtrInst::hasAllConstantIndices() const {
1124 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1125 if (!isa<ConstantInt>(getOperand(i)))
1126 return false;
1127 }
1128 return true;
1129}
1130
Chris Lattner45f15572007-04-14 00:12:57 +00001131
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001132//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001133// ExtractElementInst Implementation
1134//===----------------------------------------------------------------------===//
1135
1136ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001137 const std::string &Name,
1138 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001139 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001140 ExtractElement,
1141 OperandTraits<ExtractElementInst>::op_begin(this),
1142 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001143 assert(isValidOperands(Val, Index) &&
1144 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001145 Op<0>() = Val;
1146 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001147 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001148}
1149
Chris Lattner65511ff2006-10-05 06:24:58 +00001150ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1151 const std::string &Name,
1152 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001153 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001154 ExtractElement,
1155 OperandTraits<ExtractElementInst>::op_begin(this),
1156 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001157 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001158 assert(isValidOperands(Val, Index) &&
1159 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001160 Op<0>() = Val;
1161 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001162 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001163}
1164
1165
Robert Bocchino23004482006-01-10 19:05:34 +00001166ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001167 const std::string &Name,
1168 BasicBlock *InsertAE)
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, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001173 assert(isValidOperands(Val, Index) &&
1174 "Invalid extractelement instruction operands!");
1175
Gabor Greif2d3024d2008-05-26 21:33:52 +00001176 Op<0>() = Val;
1177 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001178 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001179}
1180
Chris Lattner65511ff2006-10-05 06:24:58 +00001181ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1182 const std::string &Name,
1183 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001184 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001185 ExtractElement,
1186 OperandTraits<ExtractElementInst>::op_begin(this),
1187 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001188 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001189 assert(isValidOperands(Val, Index) &&
1190 "Invalid extractelement instruction operands!");
1191
Gabor Greif2d3024d2008-05-26 21:33:52 +00001192 Op<0>() = Val;
1193 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001194 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001195}
1196
1197
Chris Lattner54865b32006-04-08 04:05:48 +00001198bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001199 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001200 return false;
1201 return true;
1202}
1203
1204
Robert Bocchino23004482006-01-10 19:05:34 +00001205//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001206// InsertElementInst Implementation
1207//===----------------------------------------------------------------------===//
1208
Chris Lattner0875d942006-04-14 22:20:32 +00001209InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001210 : Instruction(IE.getType(), InsertElement,
1211 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001212 Op<0>() = IE.Op<0>();
1213 Op<1>() = IE.Op<1>();
1214 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001215}
Chris Lattner54865b32006-04-08 04:05:48 +00001216InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001217 const std::string &Name,
1218 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001219 : Instruction(Vec->getType(), InsertElement,
1220 OperandTraits<InsertElementInst>::op_begin(this),
1221 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001222 assert(isValidOperands(Vec, Elt, Index) &&
1223 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001224 Op<0>() = Vec;
1225 Op<1>() = Elt;
1226 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001227 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001228}
1229
Chris Lattner65511ff2006-10-05 06:24:58 +00001230InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1231 const std::string &Name,
1232 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001233 : Instruction(Vec->getType(), InsertElement,
1234 OperandTraits<InsertElementInst>::op_begin(this),
1235 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001236 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001237 assert(isValidOperands(Vec, Elt, Index) &&
1238 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001239 Op<0>() = Vec;
1240 Op<1>() = Elt;
1241 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001242 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001243}
1244
1245
Chris Lattner54865b32006-04-08 04:05:48 +00001246InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001247 const std::string &Name,
1248 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001249 : Instruction(Vec->getType(), InsertElement,
1250 OperandTraits<InsertElementInst>::op_begin(this),
1251 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001252 assert(isValidOperands(Vec, Elt, Index) &&
1253 "Invalid insertelement instruction operands!");
1254
Gabor Greif2d3024d2008-05-26 21:33:52 +00001255 Op<0>() = Vec;
1256 Op<1>() = Elt;
1257 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001258 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001259}
1260
Chris Lattner65511ff2006-10-05 06:24:58 +00001261InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1262 const std::string &Name,
1263 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001264: Instruction(Vec->getType(), InsertElement,
1265 OperandTraits<InsertElementInst>::op_begin(this),
1266 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001267 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001268 assert(isValidOperands(Vec, Elt, Index) &&
1269 "Invalid insertelement instruction operands!");
1270
Gabor Greif2d3024d2008-05-26 21:33:52 +00001271 Op<0>() = Vec;
1272 Op<1>() = Elt;
1273 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001274 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001275}
1276
Chris Lattner54865b32006-04-08 04:05:48 +00001277bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1278 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001279 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001280 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001281
Reid Spencerd84d35b2007-02-15 02:26:10 +00001282 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001283 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001284
Reid Spencer8d9336d2006-12-31 05:26:44 +00001285 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001286 return false; // Third operand of insertelement must be uint.
1287 return true;
1288}
1289
1290
Robert Bocchinoca27f032006-01-17 20:07:22 +00001291//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001292// ShuffleVectorInst Implementation
1293//===----------------------------------------------------------------------===//
1294
Chris Lattner0875d942006-04-14 22:20:32 +00001295ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001296 : Instruction(SV.getType(), ShuffleVector,
1297 OperandTraits<ShuffleVectorInst>::op_begin(this),
1298 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001299 Op<0>() = SV.Op<0>();
1300 Op<1>() = SV.Op<1>();
1301 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001302}
1303
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001304ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1305 const std::string &Name,
1306 Instruction *InsertBefore)
Mon P Wang25f01062008-11-10 04:46:22 +00001307: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1308 cast<VectorType>(Mask->getType())->getNumElements()),
1309 ShuffleVector,
1310 OperandTraits<ShuffleVectorInst>::op_begin(this),
1311 OperandTraits<ShuffleVectorInst>::operands(this),
1312 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001313 assert(isValidOperands(V1, V2, Mask) &&
1314 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001315 Op<0>() = V1;
1316 Op<1>() = V2;
1317 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001318 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001319}
1320
1321ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Mon P Wang25f01062008-11-10 04:46:22 +00001322 const std::string &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001323 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001324 : Instruction(V1->getType(), ShuffleVector,
1325 OperandTraits<ShuffleVectorInst>::op_begin(this),
1326 OperandTraits<ShuffleVectorInst>::operands(this),
1327 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001328 assert(isValidOperands(V1, V2, Mask) &&
1329 "Invalid shuffle vector instruction operands!");
1330
Gabor Greif2d3024d2008-05-26 21:33:52 +00001331 Op<0>() = V1;
1332 Op<1>() = V2;
1333 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001334 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001335}
1336
Mon P Wang25f01062008-11-10 04:46:22 +00001337bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001338 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001339 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001340 return false;
1341
1342 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1343 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Mon P Wang25f01062008-11-10 04:46:22 +00001344 MaskTy->getElementType() != Type::Int32Ty)
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001345 return false;
1346 return true;
1347}
1348
Chris Lattnerf724e342008-03-02 05:28:33 +00001349/// getMaskValue - Return the index from the shuffle mask for the specified
1350/// output result. This is either -1 if the element is undef or a number less
1351/// than 2*numelements.
1352int ShuffleVectorInst::getMaskValue(unsigned i) const {
1353 const Constant *Mask = cast<Constant>(getOperand(2));
1354 if (isa<UndefValue>(Mask)) return -1;
1355 if (isa<ConstantAggregateZero>(Mask)) return 0;
1356 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1357 assert(i < MaskCV->getNumOperands() && "Index out of range");
1358
1359 if (isa<UndefValue>(MaskCV->getOperand(i)))
1360 return -1;
1361 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1362}
1363
Dan Gohman12fce772008-05-15 19:50:34 +00001364//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001365// InsertValueInst Class
1366//===----------------------------------------------------------------------===//
1367
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001368void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1369 unsigned NumIdx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001370 assert(NumOperands == 2 && "NumOperands not initialized?");
1371 Op<0>() = Agg;
1372 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001373
Dan Gohman1ecaf452008-05-31 00:58:22 +00001374 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001375 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001376}
1377
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001378void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1379 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001380 assert(NumOperands == 2 && "NumOperands not initialized?");
1381 Op<0>() = Agg;
1382 Op<1>() = Val;
1383
1384 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001385 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001386}
1387
1388InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001389 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001390 OperandTraits<InsertValueInst>::op_begin(this), 2),
1391 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001392 Op<0>() = IVI.getOperand(0);
1393 Op<1>() = IVI.getOperand(1);
Dan Gohman0752bff2008-05-23 00:36:11 +00001394}
1395
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001396InsertValueInst::InsertValueInst(Value *Agg,
1397 Value *Val,
1398 unsigned Idx,
1399 const std::string &Name,
1400 Instruction *InsertBefore)
1401 : Instruction(Agg->getType(), InsertValue,
1402 OperandTraits<InsertValueInst>::op_begin(this),
1403 2, InsertBefore) {
1404 init(Agg, Val, Idx, Name);
1405}
1406
1407InsertValueInst::InsertValueInst(Value *Agg,
1408 Value *Val,
1409 unsigned Idx,
1410 const std::string &Name,
1411 BasicBlock *InsertAtEnd)
1412 : Instruction(Agg->getType(), InsertValue,
1413 OperandTraits<InsertValueInst>::op_begin(this),
1414 2, InsertAtEnd) {
1415 init(Agg, Val, Idx, Name);
1416}
1417
Dan Gohman0752bff2008-05-23 00:36:11 +00001418//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001419// ExtractValueInst Class
1420//===----------------------------------------------------------------------===//
1421
Gabor Greifcbcc4952008-06-06 21:06:32 +00001422void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Gabor Greif1b9921a2009-01-11 22:33:22 +00001423 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001424 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001425
Dan Gohman1ecaf452008-05-31 00:58:22 +00001426 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001427 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001428}
1429
Gabor Greifcbcc4952008-06-06 21:06:32 +00001430void ExtractValueInst::init(unsigned Idx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001431 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001432
1433 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001434 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001435}
1436
1437ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001438 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001439 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001440}
1441
Dan Gohman12fce772008-05-15 19:50:34 +00001442// getIndexedType - Returns the type of the element that would be extracted
1443// with an extractvalue instruction with the specified parameters.
1444//
1445// A null type is returned if the indices are invalid for the specified
1446// pointer type.
1447//
1448const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001449 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001450 unsigned NumIdx) {
1451 unsigned CurIdx = 0;
1452 for (; CurIdx != NumIdx; ++CurIdx) {
1453 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001454 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1455 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001456 if (!CT->indexValid(Index)) return 0;
1457 Agg = CT->getTypeAtIndex(Index);
1458
1459 // If the new type forwards to another type, then it is in the middle
1460 // of being refined to another type (and hence, may have dropped all
1461 // references to what it was using before). So, use the new forwarded
1462 // type.
1463 if (const Type *Ty = Agg->getForwardedType())
1464 Agg = Ty;
1465 }
1466 return CurIdx == NumIdx ? Agg : 0;
1467}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001468
Dan Gohman4a3125b2008-06-17 21:07:55 +00001469const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001470 unsigned Idx) {
1471 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001472}
1473
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001474//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001475// BinaryOperator Class
1476//===----------------------------------------------------------------------===//
1477
Chris Lattner2195fc42007-02-24 00:55:48 +00001478BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1479 const Type *Ty, const std::string &Name,
1480 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001481 : Instruction(Ty, iType,
1482 OperandTraits<BinaryOperator>::op_begin(this),
1483 OperandTraits<BinaryOperator>::operands(this),
1484 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001485 Op<0>() = S1;
1486 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001487 init(iType);
1488 setName(Name);
1489}
1490
1491BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1492 const Type *Ty, const std::string &Name,
1493 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001494 : Instruction(Ty, iType,
1495 OperandTraits<BinaryOperator>::op_begin(this),
1496 OperandTraits<BinaryOperator>::operands(this),
1497 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001498 Op<0>() = S1;
1499 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001500 init(iType);
1501 setName(Name);
1502}
1503
1504
1505void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001506 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001507 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001508 assert(LHS->getType() == RHS->getType() &&
1509 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001510#ifndef NDEBUG
1511 switch (iType) {
1512 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001513 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001514 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001515 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001516 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001517 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001518 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001519 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001520 case UDiv:
1521 case SDiv:
1522 assert(getType() == LHS->getType() &&
1523 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001524 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1525 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001526 "Incorrect operand type (not integer) for S/UDIV");
1527 break;
1528 case FDiv:
1529 assert(getType() == LHS->getType() &&
1530 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001531 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1532 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001533 && "Incorrect operand type (not floating point) for FDIV");
1534 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001535 case URem:
1536 case SRem:
1537 assert(getType() == LHS->getType() &&
1538 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001539 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1540 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001541 "Incorrect operand type (not integer) for S/UREM");
1542 break;
1543 case FRem:
1544 assert(getType() == LHS->getType() &&
1545 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001546 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1547 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001548 && "Incorrect operand type (not floating point) for FREM");
1549 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001550 case Shl:
1551 case LShr:
1552 case AShr:
1553 assert(getType() == LHS->getType() &&
1554 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001555 assert((getType()->isInteger() ||
1556 (isa<VectorType>(getType()) &&
1557 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1558 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001559 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001560 case And: case Or:
1561 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001562 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001563 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001564 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001565 (isa<VectorType>(getType()) &&
1566 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001567 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001568 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001569 default:
1570 break;
1571 }
1572#endif
1573}
1574
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001575BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001576 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001577 Instruction *InsertBefore) {
1578 assert(S1->getType() == S2->getType() &&
1579 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001580 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001581}
1582
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001583BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001584 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001585 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001586 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001587 InsertAtEnd->getInstList().push_back(Res);
1588 return Res;
1589}
1590
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001591BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001592 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001593 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1594 return new BinaryOperator(Instruction::Sub,
1595 zero, Op,
1596 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001597}
1598
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001599BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001600 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001601 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1602 return new BinaryOperator(Instruction::Sub,
1603 zero, Op,
1604 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001605}
1606
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001607BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001608 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001609 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001610 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001611 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001612 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001613 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001614 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001615 }
1616
1617 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001618 Op->getType(), Name, InsertBefore);
1619}
1620
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001621BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001622 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001623 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001624 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001625 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001626 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001627 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001628 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001629 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001630 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001631 }
1632
1633 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001634 Op->getType(), Name, InsertAtEnd);
1635}
1636
1637
1638// isConstantAllOnes - Helper function for several functions below
1639static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001640 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1641 return CI->isAllOnesValue();
1642 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1643 return CV->isAllOnesValue();
1644 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001645}
1646
1647bool BinaryOperator::isNeg(const Value *V) {
1648 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1649 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001650 return Bop->getOperand(0) ==
1651 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001652 return false;
1653}
1654
1655bool BinaryOperator::isNot(const Value *V) {
1656 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1657 return (Bop->getOpcode() == Instruction::Xor &&
1658 (isConstantAllOnes(Bop->getOperand(1)) ||
1659 isConstantAllOnes(Bop->getOperand(0))));
1660 return false;
1661}
1662
Chris Lattner2c7d1772005-04-24 07:28:37 +00001663Value *BinaryOperator::getNegArgument(Value *BinOp) {
1664 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1665 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001666}
1667
Chris Lattner2c7d1772005-04-24 07:28:37 +00001668const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1669 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001670}
1671
Chris Lattner2c7d1772005-04-24 07:28:37 +00001672Value *BinaryOperator::getNotArgument(Value *BinOp) {
1673 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1674 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1675 Value *Op0 = BO->getOperand(0);
1676 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001677 if (isConstantAllOnes(Op0)) return Op1;
1678
1679 assert(isConstantAllOnes(Op1));
1680 return Op0;
1681}
1682
Chris Lattner2c7d1772005-04-24 07:28:37 +00001683const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1684 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001685}
1686
1687
1688// swapOperands - Exchange the two operands to this instruction. This
1689// instruction is safe to use on any binary instruction and does not
1690// modify the semantics of the instruction. If the instruction is
1691// order dependent (SetLT f.e.) the opcode is changed.
1692//
1693bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001694 if (!isCommutative())
1695 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001696 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001697 return false;
1698}
1699
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001700//===----------------------------------------------------------------------===//
1701// CastInst Class
1702//===----------------------------------------------------------------------===//
1703
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001704// Just determine if this cast only deals with integral->integral conversion.
1705bool CastInst::isIntegerCast() const {
1706 switch (getOpcode()) {
1707 default: return false;
1708 case Instruction::ZExt:
1709 case Instruction::SExt:
1710 case Instruction::Trunc:
1711 return true;
1712 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001713 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001714 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001715}
1716
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001717bool CastInst::isLosslessCast() const {
1718 // Only BitCast can be lossless, exit fast if we're not BitCast
1719 if (getOpcode() != Instruction::BitCast)
1720 return false;
1721
1722 // Identity cast is always lossless
1723 const Type* SrcTy = getOperand(0)->getType();
1724 const Type* DstTy = getType();
1725 if (SrcTy == DstTy)
1726 return true;
1727
Reid Spencer8d9336d2006-12-31 05:26:44 +00001728 // Pointer to pointer is always lossless.
1729 if (isa<PointerType>(SrcTy))
1730 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001731 return false; // Other types have no identity values
1732}
1733
1734/// This function determines if the CastInst does not require any bits to be
1735/// changed in order to effect the cast. Essentially, it identifies cases where
1736/// no code gen is necessary for the cast, hence the name no-op cast. For
1737/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001738/// # bitcast i32* %x to i8*
1739/// # bitcast <2 x i32> %x to <4 x i16>
1740/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001741/// @brief Determine if a cast is a no-op.
1742bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1743 switch (getOpcode()) {
1744 default:
1745 assert(!"Invalid CastOp");
1746 case Instruction::Trunc:
1747 case Instruction::ZExt:
1748 case Instruction::SExt:
1749 case Instruction::FPTrunc:
1750 case Instruction::FPExt:
1751 case Instruction::UIToFP:
1752 case Instruction::SIToFP:
1753 case Instruction::FPToUI:
1754 case Instruction::FPToSI:
1755 return false; // These always modify bits
1756 case Instruction::BitCast:
1757 return true; // BitCast never modifies bits.
1758 case Instruction::PtrToInt:
1759 return IntPtrTy->getPrimitiveSizeInBits() ==
1760 getType()->getPrimitiveSizeInBits();
1761 case Instruction::IntToPtr:
1762 return IntPtrTy->getPrimitiveSizeInBits() ==
1763 getOperand(0)->getType()->getPrimitiveSizeInBits();
1764 }
1765}
1766
1767/// This function determines if a pair of casts can be eliminated and what
1768/// opcode should be used in the elimination. This assumes that there are two
1769/// instructions like this:
1770/// * %F = firstOpcode SrcTy %x to MidTy
1771/// * %S = secondOpcode MidTy %F to DstTy
1772/// The function returns a resultOpcode so these two casts can be replaced with:
1773/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1774/// If no such cast is permited, the function returns 0.
1775unsigned CastInst::isEliminableCastPair(
1776 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1777 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1778{
1779 // Define the 144 possibilities for these two cast instructions. The values
1780 // in this matrix determine what to do in a given situation and select the
1781 // case in the switch below. The rows correspond to firstOp, the columns
1782 // correspond to secondOp. In looking at the table below, keep in mind
1783 // the following cast properties:
1784 //
1785 // Size Compare Source Destination
1786 // Operator Src ? Size Type Sign Type Sign
1787 // -------- ------------ ------------------- ---------------------
1788 // TRUNC > Integer Any Integral Any
1789 // ZEXT < Integral Unsigned Integer Any
1790 // SEXT < Integral Signed Integer Any
1791 // FPTOUI n/a FloatPt n/a Integral Unsigned
1792 // FPTOSI n/a FloatPt n/a Integral Signed
1793 // UITOFP n/a Integral Unsigned FloatPt n/a
1794 // SITOFP n/a Integral Signed FloatPt n/a
1795 // FPTRUNC > FloatPt n/a FloatPt n/a
1796 // FPEXT < FloatPt n/a FloatPt n/a
1797 // PTRTOINT n/a Pointer n/a Integral Unsigned
1798 // INTTOPTR n/a Integral Unsigned Pointer n/a
1799 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001800 //
1801 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1802 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1803 // into "fptoui double to ulong", but this loses information about the range
1804 // of the produced value (we no longer know the top-part is all zeros).
1805 // Further this conversion is often much more expensive for typical hardware,
1806 // and causes issues when building libgcc. We disallow fptosi+sext for the
1807 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001808 const unsigned numCastOps =
1809 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1810 static const uint8_t CastResults[numCastOps][numCastOps] = {
1811 // T F F U S F F P I B -+
1812 // R Z S P P I I T P 2 N T |
1813 // U E E 2 2 2 2 R E I T C +- secondOp
1814 // N X X U S F F N X N 2 V |
1815 // C T T I I P P C T T P T -+
1816 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1817 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1818 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001819 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1820 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001821 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1822 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1823 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1824 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1825 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1826 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1827 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1828 };
1829
1830 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1831 [secondOp-Instruction::CastOpsBegin];
1832 switch (ElimCase) {
1833 case 0:
1834 // categorically disallowed
1835 return 0;
1836 case 1:
1837 // allowed, use first cast's opcode
1838 return firstOp;
1839 case 2:
1840 // allowed, use second cast's opcode
1841 return secondOp;
1842 case 3:
1843 // no-op cast in second op implies firstOp as long as the DestTy
1844 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001845 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001846 return firstOp;
1847 return 0;
1848 case 4:
1849 // no-op cast in second op implies firstOp as long as the DestTy
1850 // is floating point
1851 if (DstTy->isFloatingPoint())
1852 return firstOp;
1853 return 0;
1854 case 5:
1855 // no-op cast in first op implies secondOp as long as the SrcTy
1856 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001857 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001858 return secondOp;
1859 return 0;
1860 case 6:
1861 // no-op cast in first op implies secondOp as long as the SrcTy
1862 // is a floating point
1863 if (SrcTy->isFloatingPoint())
1864 return secondOp;
1865 return 0;
1866 case 7: {
1867 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1868 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1869 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1870 if (MidSize >= PtrSize)
1871 return Instruction::BitCast;
1872 return 0;
1873 }
1874 case 8: {
1875 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1876 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1877 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1878 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1879 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1880 if (SrcSize == DstSize)
1881 return Instruction::BitCast;
1882 else if (SrcSize < DstSize)
1883 return firstOp;
1884 return secondOp;
1885 }
1886 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1887 return Instruction::ZExt;
1888 case 10:
1889 // fpext followed by ftrunc is allowed if the bit size returned to is
1890 // the same as the original, in which case its just a bitcast
1891 if (SrcTy == DstTy)
1892 return Instruction::BitCast;
1893 return 0; // If the types are not the same we can't eliminate it.
1894 case 11:
1895 // bitcast followed by ptrtoint is allowed as long as the bitcast
1896 // is a pointer to pointer cast.
1897 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1898 return secondOp;
1899 return 0;
1900 case 12:
1901 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1902 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1903 return firstOp;
1904 return 0;
1905 case 13: {
1906 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1907 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1908 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1909 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1910 if (SrcSize <= PtrSize && SrcSize == DstSize)
1911 return Instruction::BitCast;
1912 return 0;
1913 }
1914 case 99:
1915 // cast combination can't happen (error in input). This is for all cases
1916 // where the MidTy is not the same for the two cast instructions.
1917 assert(!"Invalid Cast Combination");
1918 return 0;
1919 default:
1920 assert(!"Error in CastResults table!!!");
1921 return 0;
1922 }
1923 return 0;
1924}
1925
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001926CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001927 const std::string &Name, Instruction *InsertBefore) {
1928 // Construct and return the appropriate CastInst subclass
1929 switch (op) {
1930 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1931 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1932 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1933 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1934 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1935 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1936 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1937 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1938 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1939 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1940 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1941 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1942 default:
1943 assert(!"Invalid opcode provided");
1944 }
1945 return 0;
1946}
1947
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001948CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001949 const std::string &Name, BasicBlock *InsertAtEnd) {
1950 // Construct and return the appropriate CastInst subclass
1951 switch (op) {
1952 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1953 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1954 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1955 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1956 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1957 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1958 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1959 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1960 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1961 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1962 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1963 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1964 default:
1965 assert(!"Invalid opcode provided");
1966 }
1967 return 0;
1968}
1969
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001970CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001971 const std::string &Name,
1972 Instruction *InsertBefore) {
1973 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001974 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1975 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001976}
1977
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001978CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001979 const std::string &Name,
1980 BasicBlock *InsertAtEnd) {
1981 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001982 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1983 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001984}
1985
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001986CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001987 const std::string &Name,
1988 Instruction *InsertBefore) {
1989 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001990 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1991 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001992}
1993
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001994CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001995 const std::string &Name,
1996 BasicBlock *InsertAtEnd) {
1997 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001998 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1999 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002000}
2001
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002002CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002003 const std::string &Name,
2004 Instruction *InsertBefore) {
2005 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002006 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2007 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002008}
2009
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002010CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002011 const std::string &Name,
2012 BasicBlock *InsertAtEnd) {
2013 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002014 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2015 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002016}
2017
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002018CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002019 const std::string &Name,
2020 BasicBlock *InsertAtEnd) {
2021 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002022 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002023 "Invalid cast");
2024
Chris Lattner03c49532007-01-15 02:27:26 +00002025 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002026 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2027 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002028}
2029
2030/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002031CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002032 const std::string &Name,
2033 Instruction *InsertBefore) {
2034 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002035 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002036 "Invalid cast");
2037
Chris Lattner03c49532007-01-15 02:27:26 +00002038 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002039 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2040 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002041}
2042
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002043CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002044 bool isSigned, const std::string &Name,
2045 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002046 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002047 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2048 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2049 Instruction::CastOps opcode =
2050 (SrcBits == DstBits ? Instruction::BitCast :
2051 (SrcBits > DstBits ? Instruction::Trunc :
2052 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002053 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002054}
2055
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002056CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002057 bool isSigned, const std::string &Name,
2058 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00002059 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002060 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2061 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2062 Instruction::CastOps opcode =
2063 (SrcBits == DstBits ? Instruction::BitCast :
2064 (SrcBits > DstBits ? Instruction::Trunc :
2065 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002066 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002067}
2068
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002069CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002070 const std::string &Name,
2071 Instruction *InsertBefore) {
2072 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2073 "Invalid cast");
2074 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2075 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2076 Instruction::CastOps opcode =
2077 (SrcBits == DstBits ? Instruction::BitCast :
2078 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002079 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002080}
2081
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002082CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002083 const std::string &Name,
2084 BasicBlock *InsertAtEnd) {
2085 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2086 "Invalid cast");
2087 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2088 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2089 Instruction::CastOps opcode =
2090 (SrcBits == DstBits ? Instruction::BitCast :
2091 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002092 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002093}
2094
Duncan Sands55e50902008-01-06 10:12:28 +00002095// Check whether it is valid to call getCastOpcode for these types.
2096// This routine must be kept in sync with getCastOpcode.
2097bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2098 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2099 return false;
2100
2101 if (SrcTy == DestTy)
2102 return true;
2103
2104 // Get the bit sizes, we'll need these
2105 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2106 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2107
2108 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002109 if (DestTy->isInteger()) { // Casting to integral
2110 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002111 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002112 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002113 return true;
2114 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002115 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002116 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002117 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002118 return isa<PointerType>(SrcTy);
2119 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002120 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2121 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002122 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002123 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002124 return true;
2125 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002126 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002127 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002128 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002129 return false;
2130 }
2131 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002132 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002133 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002134 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002135 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002136 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002137 return DestPTy->getBitWidth() == SrcBits;
2138 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002139 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2140 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002141 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002142 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002143 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002144 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002145 return false;
2146 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002147 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002148 return false;
2149 }
2150}
2151
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002152// Provide a way to get a "cast" where the cast opcode is inferred from the
2153// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002154// logic in the castIsValid function below. This axiom should hold:
2155// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2156// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002157// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002158// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002159Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002160CastInst::getCastOpcode(
2161 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002162 // Get the bit sizes, we'll need these
2163 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002164 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2165 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002166
Duncan Sands55e50902008-01-06 10:12:28 +00002167 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2168 "Only first class types are castable!");
2169
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002170 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002171 if (DestTy->isInteger()) { // Casting to integral
2172 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002173 if (DestBits < SrcBits)
2174 return Trunc; // int -> smaller int
2175 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002176 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002177 return SExt; // signed -> SEXT
2178 else
2179 return ZExt; // unsigned -> ZEXT
2180 } else {
2181 return BitCast; // Same size, No-op cast
2182 }
2183 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002184 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002185 return FPToSI; // FP -> sint
2186 else
2187 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002188 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002189 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002190 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002191 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002192 return BitCast; // Same size, no-op cast
2193 } else {
2194 assert(isa<PointerType>(SrcTy) &&
2195 "Casting from a value that is not first-class type");
2196 return PtrToInt; // ptr -> int
2197 }
2198 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002199 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002200 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002201 return SIToFP; // sint -> FP
2202 else
2203 return UIToFP; // uint -> FP
2204 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2205 if (DestBits < SrcBits) {
2206 return FPTrunc; // FP -> smaller FP
2207 } else if (DestBits > SrcBits) {
2208 return FPExt; // FP -> larger FP
2209 } else {
2210 return BitCast; // same size, no-op cast
2211 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002212 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002213 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002214 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002215 PTy = NULL;
2216 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002217 } else {
2218 assert(0 && "Casting pointer or non-first class to float");
2219 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002220 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2221 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002222 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002223 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002224 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002225 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002226 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002227 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002228 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002229 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002230 }
2231 } else if (isa<PointerType>(DestTy)) {
2232 if (isa<PointerType>(SrcTy)) {
2233 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002234 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002235 return IntToPtr; // int -> ptr
2236 } else {
2237 assert(!"Casting pointer to other than pointer or int");
2238 }
2239 } else {
2240 assert(!"Casting to type that is not first-class");
2241 }
2242
2243 // If we fall through to here we probably hit an assertion cast above
2244 // and assertions are not turned on. Anything we return is an error, so
2245 // BitCast is as good a choice as any.
2246 return BitCast;
2247}
2248
2249//===----------------------------------------------------------------------===//
2250// CastInst SubClass Constructors
2251//===----------------------------------------------------------------------===//
2252
2253/// Check that the construction parameters for a CastInst are correct. This
2254/// could be broken out into the separate constructors but it is useful to have
2255/// it in one place and to eliminate the redundant code for getting the sizes
2256/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002257bool
2258CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002259
2260 // Check for type sanity on the arguments
2261 const Type *SrcTy = S->getType();
2262 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2263 return false;
2264
2265 // Get the size of the types in bits, we'll need this later
2266 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2267 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2268
2269 // Switch on the opcode provided
2270 switch (op) {
2271 default: return false; // This is an input error
2272 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002273 return SrcTy->isIntOrIntVector() &&
2274 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002275 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002276 return SrcTy->isIntOrIntVector() &&
2277 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002278 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002279 return SrcTy->isIntOrIntVector() &&
2280 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002281 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002282 return SrcTy->isFPOrFPVector() &&
2283 DstTy->isFPOrFPVector() &&
2284 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002285 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002286 return SrcTy->isFPOrFPVector() &&
2287 DstTy->isFPOrFPVector() &&
2288 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002289 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002290 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002291 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2292 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002293 return SVTy->getElementType()->isIntOrIntVector() &&
2294 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002295 SVTy->getNumElements() == DVTy->getNumElements();
2296 }
2297 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002298 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002300 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002301 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2302 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002303 return SVTy->getElementType()->isFPOrFPVector() &&
2304 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002305 SVTy->getNumElements() == DVTy->getNumElements();
2306 }
2307 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002308 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002309 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002310 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002311 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002312 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002313 case Instruction::BitCast:
2314 // BitCast implies a no-op cast of type only. No bits change.
2315 // However, you can't cast pointers to anything but pointers.
2316 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2317 return false;
2318
Duncan Sands55e50902008-01-06 10:12:28 +00002319 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002320 // these cases, the cast is okay if the source and destination bit widths
2321 // are identical.
2322 return SrcBitSize == DstBitSize;
2323 }
2324}
2325
2326TruncInst::TruncInst(
2327 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2328) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002329 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002330}
2331
2332TruncInst::TruncInst(
2333 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2334) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002335 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002336}
2337
2338ZExtInst::ZExtInst(
2339 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2340) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002341 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002342}
2343
2344ZExtInst::ZExtInst(
2345 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2346) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002347 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002348}
2349SExtInst::SExtInst(
2350 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2351) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002352 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002353}
2354
Jeff Cohencc08c832006-12-02 02:22:01 +00002355SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002356 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2357) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002358 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359}
2360
2361FPTruncInst::FPTruncInst(
2362 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2363) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002364 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002365}
2366
2367FPTruncInst::FPTruncInst(
2368 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2369) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002370 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002371}
2372
2373FPExtInst::FPExtInst(
2374 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2375) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002376 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002377}
2378
2379FPExtInst::FPExtInst(
2380 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2381) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002382 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002383}
2384
2385UIToFPInst::UIToFPInst(
2386 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2387) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002388 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002389}
2390
2391UIToFPInst::UIToFPInst(
2392 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2393) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002394 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002395}
2396
2397SIToFPInst::SIToFPInst(
2398 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2399) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002400 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002401}
2402
2403SIToFPInst::SIToFPInst(
2404 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2405) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002406 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002407}
2408
2409FPToUIInst::FPToUIInst(
2410 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2411) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002412 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002413}
2414
2415FPToUIInst::FPToUIInst(
2416 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2417) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002418 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002419}
2420
2421FPToSIInst::FPToSIInst(
2422 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2423) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002424 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425}
2426
2427FPToSIInst::FPToSIInst(
2428 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2429) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002430 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002431}
2432
2433PtrToIntInst::PtrToIntInst(
2434 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2435) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002436 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437}
2438
2439PtrToIntInst::PtrToIntInst(
2440 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2441) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002442 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002443}
2444
2445IntToPtrInst::IntToPtrInst(
2446 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2447) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002448 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002449}
2450
2451IntToPtrInst::IntToPtrInst(
2452 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2453) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002454 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002455}
2456
2457BitCastInst::BitCastInst(
2458 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2459) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002460 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002461}
2462
2463BitCastInst::BitCastInst(
2464 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2465) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002466 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002467}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002468
2469//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002470// CmpInst Classes
2471//===----------------------------------------------------------------------===//
2472
Nate Begemand2195702008-05-12 19:01:56 +00002473CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2474 Value *LHS, Value *RHS, const std::string &Name,
2475 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002476 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002477 OperandTraits<CmpInst>::op_begin(this),
2478 OperandTraits<CmpInst>::operands(this),
2479 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002480 Op<0>() = LHS;
2481 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002482 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002483 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002484}
Gabor Greiff6caff662008-05-10 08:32:32 +00002485
Nate Begemand2195702008-05-12 19:01:56 +00002486CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2487 Value *LHS, Value *RHS, const std::string &Name,
2488 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002489 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002490 OperandTraits<CmpInst>::op_begin(this),
2491 OperandTraits<CmpInst>::operands(this),
2492 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002493 Op<0>() = LHS;
2494 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002495 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002496 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002497}
2498
2499CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002500CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002501 const std::string &Name, Instruction *InsertBefore) {
2502 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002503 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002504 InsertBefore);
2505 }
Nate Begemand2195702008-05-12 19:01:56 +00002506 if (Op == Instruction::FCmp) {
2507 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2508 InsertBefore);
2509 }
2510 if (Op == Instruction::VICmp) {
2511 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2512 InsertBefore);
2513 }
2514 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2515 InsertBefore);
Reid Spencerd9436b62006-11-20 01:22:35 +00002516}
2517
2518CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002519CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002520 const std::string &Name, BasicBlock *InsertAtEnd) {
2521 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002522 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002523 InsertAtEnd);
2524 }
Nate Begemand2195702008-05-12 19:01:56 +00002525 if (Op == Instruction::FCmp) {
2526 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2527 InsertAtEnd);
2528 }
2529 if (Op == Instruction::VICmp) {
2530 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2531 InsertAtEnd);
2532 }
2533 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2534 InsertAtEnd);
Reid Spencerd9436b62006-11-20 01:22:35 +00002535}
2536
2537void CmpInst::swapOperands() {
2538 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2539 IC->swapOperands();
2540 else
2541 cast<FCmpInst>(this)->swapOperands();
2542}
2543
2544bool CmpInst::isCommutative() {
2545 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2546 return IC->isCommutative();
2547 return cast<FCmpInst>(this)->isCommutative();
2548}
2549
2550bool CmpInst::isEquality() {
2551 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2552 return IC->isEquality();
2553 return cast<FCmpInst>(this)->isEquality();
2554}
2555
2556
Dan Gohman4e724382008-05-31 02:47:54 +00002557CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002558 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002559 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002560 case ICMP_EQ: return ICMP_NE;
2561 case ICMP_NE: return ICMP_EQ;
2562 case ICMP_UGT: return ICMP_ULE;
2563 case ICMP_ULT: return ICMP_UGE;
2564 case ICMP_UGE: return ICMP_ULT;
2565 case ICMP_ULE: return ICMP_UGT;
2566 case ICMP_SGT: return ICMP_SLE;
2567 case ICMP_SLT: return ICMP_SGE;
2568 case ICMP_SGE: return ICMP_SLT;
2569 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002570
Dan Gohman4e724382008-05-31 02:47:54 +00002571 case FCMP_OEQ: return FCMP_UNE;
2572 case FCMP_ONE: return FCMP_UEQ;
2573 case FCMP_OGT: return FCMP_ULE;
2574 case FCMP_OLT: return FCMP_UGE;
2575 case FCMP_OGE: return FCMP_ULT;
2576 case FCMP_OLE: return FCMP_UGT;
2577 case FCMP_UEQ: return FCMP_ONE;
2578 case FCMP_UNE: return FCMP_OEQ;
2579 case FCMP_UGT: return FCMP_OLE;
2580 case FCMP_ULT: return FCMP_OGE;
2581 case FCMP_UGE: return FCMP_OLT;
2582 case FCMP_ULE: return FCMP_OGT;
2583 case FCMP_ORD: return FCMP_UNO;
2584 case FCMP_UNO: return FCMP_ORD;
2585 case FCMP_TRUE: return FCMP_FALSE;
2586 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002587 }
2588}
2589
Reid Spencer266e42b2006-12-23 06:05:41 +00002590ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2591 switch (pred) {
2592 default: assert(! "Unknown icmp predicate!");
2593 case ICMP_EQ: case ICMP_NE:
2594 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2595 return pred;
2596 case ICMP_UGT: return ICMP_SGT;
2597 case ICMP_ULT: return ICMP_SLT;
2598 case ICMP_UGE: return ICMP_SGE;
2599 case ICMP_ULE: return ICMP_SLE;
2600 }
2601}
2602
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002603ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2604 switch (pred) {
2605 default: assert(! "Unknown icmp predicate!");
2606 case ICMP_EQ: case ICMP_NE:
2607 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2608 return pred;
2609 case ICMP_SGT: return ICMP_UGT;
2610 case ICMP_SLT: return ICMP_ULT;
2611 case ICMP_SGE: return ICMP_UGE;
2612 case ICMP_SLE: return ICMP_ULE;
2613 }
2614}
2615
Reid Spencer266e42b2006-12-23 06:05:41 +00002616bool ICmpInst::isSignedPredicate(Predicate pred) {
2617 switch (pred) {
2618 default: assert(! "Unknown icmp predicate!");
2619 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2620 return true;
2621 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2622 case ICMP_UGE: case ICMP_ULE:
2623 return false;
2624 }
2625}
2626
Reid Spencer0286bc12007-02-28 22:00:54 +00002627/// Initialize a set of values that all satisfy the condition with C.
2628///
2629ConstantRange
2630ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2631 APInt Lower(C);
2632 APInt Upper(C);
2633 uint32_t BitWidth = C.getBitWidth();
2634 switch (pred) {
2635 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2636 case ICmpInst::ICMP_EQ: Upper++; break;
2637 case ICmpInst::ICMP_NE: Lower++; break;
2638 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2639 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2640 case ICmpInst::ICMP_UGT:
2641 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2642 break;
2643 case ICmpInst::ICMP_SGT:
2644 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2645 break;
2646 case ICmpInst::ICMP_ULE:
2647 Lower = APInt::getMinValue(BitWidth); Upper++;
2648 break;
2649 case ICmpInst::ICMP_SLE:
2650 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2651 break;
2652 case ICmpInst::ICMP_UGE:
2653 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2654 break;
2655 case ICmpInst::ICMP_SGE:
2656 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2657 break;
2658 }
2659 return ConstantRange(Lower, Upper);
2660}
2661
Dan Gohman4e724382008-05-31 02:47:54 +00002662CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002663 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002664 default: assert(!"Unknown cmp predicate!");
2665 case ICMP_EQ: case ICMP_NE:
2666 return pred;
2667 case ICMP_SGT: return ICMP_SLT;
2668 case ICMP_SLT: return ICMP_SGT;
2669 case ICMP_SGE: return ICMP_SLE;
2670 case ICMP_SLE: return ICMP_SGE;
2671 case ICMP_UGT: return ICMP_ULT;
2672 case ICMP_ULT: return ICMP_UGT;
2673 case ICMP_UGE: return ICMP_ULE;
2674 case ICMP_ULE: return ICMP_UGE;
2675
Reid Spencerd9436b62006-11-20 01:22:35 +00002676 case FCMP_FALSE: case FCMP_TRUE:
2677 case FCMP_OEQ: case FCMP_ONE:
2678 case FCMP_UEQ: case FCMP_UNE:
2679 case FCMP_ORD: case FCMP_UNO:
2680 return pred;
2681 case FCMP_OGT: return FCMP_OLT;
2682 case FCMP_OLT: return FCMP_OGT;
2683 case FCMP_OGE: return FCMP_OLE;
2684 case FCMP_OLE: return FCMP_OGE;
2685 case FCMP_UGT: return FCMP_ULT;
2686 case FCMP_ULT: return FCMP_UGT;
2687 case FCMP_UGE: return FCMP_ULE;
2688 case FCMP_ULE: return FCMP_UGE;
2689 }
2690}
2691
Reid Spencer266e42b2006-12-23 06:05:41 +00002692bool CmpInst::isUnsigned(unsigned short predicate) {
2693 switch (predicate) {
2694 default: return false;
2695 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2696 case ICmpInst::ICMP_UGE: return true;
2697 }
2698}
2699
2700bool CmpInst::isSigned(unsigned short predicate){
2701 switch (predicate) {
2702 default: return false;
2703 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2704 case ICmpInst::ICMP_SGE: return true;
2705 }
2706}
2707
2708bool CmpInst::isOrdered(unsigned short predicate) {
2709 switch (predicate) {
2710 default: return false;
2711 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2712 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2713 case FCmpInst::FCMP_ORD: return true;
2714 }
2715}
2716
2717bool CmpInst::isUnordered(unsigned short predicate) {
2718 switch (predicate) {
2719 default: return false;
2720 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2721 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2722 case FCmpInst::FCMP_UNO: return true;
2723 }
2724}
2725
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002726//===----------------------------------------------------------------------===//
2727// SwitchInst Implementation
2728//===----------------------------------------------------------------------===//
2729
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002730void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002731 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002732 ReservedSpace = 2+NumCases*2;
2733 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002734 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002735
Gabor Greif2d3024d2008-05-26 21:33:52 +00002736 OperandList[0] = Value;
2737 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002738}
2739
Chris Lattner2195fc42007-02-24 00:55:48 +00002740/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2741/// switch on and a default destination. The number of additional cases can
2742/// be specified here to make memory allocation more efficient. This
2743/// constructor can also autoinsert before another instruction.
2744SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2745 Instruction *InsertBefore)
2746 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2747 init(Value, Default, NumCases);
2748}
2749
2750/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2751/// switch on and a default destination. The number of additional cases can
2752/// be specified here to make memory allocation more efficient. This
2753/// constructor also autoinserts at the end of the specified BasicBlock.
2754SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2755 BasicBlock *InsertAtEnd)
2756 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2757 init(Value, Default, NumCases);
2758}
2759
Misha Brukmanb1c93172005-04-21 23:48:37 +00002760SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002761 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002762 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002763 Use *OL = OperandList, *InOL = SI.OperandList;
2764 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002765 OL[i] = InOL[i];
2766 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002767 }
2768}
2769
Gordon Henriksen14a55692007-12-10 02:14:30 +00002770SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002771 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002772}
2773
2774
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002775/// addCase - Add an entry to the switch instruction...
2776///
Chris Lattner47ac1872005-02-24 05:32:09 +00002777void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002778 unsigned OpNo = NumOperands;
2779 if (OpNo+2 > ReservedSpace)
2780 resizeOperands(0); // Get more space!
2781 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002782 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002783 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002784 OperandList[OpNo] = OnVal;
2785 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002786}
2787
2788/// removeCase - This method removes the specified successor from the switch
2789/// instruction. Note that this cannot be used to remove the default
2790/// destination (successor #0).
2791///
2792void SwitchInst::removeCase(unsigned idx) {
2793 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002794 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2795
2796 unsigned NumOps = getNumOperands();
2797 Use *OL = OperandList;
2798
2799 // Move everything after this operand down.
2800 //
2801 // FIXME: we could just swap with the end of the list, then erase. However,
2802 // client might not expect this to happen. The code as it is thrashes the
2803 // use/def lists, which is kinda lame.
2804 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2805 OL[i-2] = OL[i];
2806 OL[i-2+1] = OL[i+1];
2807 }
2808
2809 // Nuke the last value.
2810 OL[NumOps-2].set(0);
2811 OL[NumOps-2+1].set(0);
2812 NumOperands = NumOps-2;
2813}
2814
2815/// resizeOperands - resize operands - This adjusts the length of the operands
2816/// list according to the following behavior:
2817/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002818/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002819/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2820/// 3. If NumOps == NumOperands, trim the reserved space.
2821///
2822void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002823 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002824 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002825 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002826 } else if (NumOps*2 > NumOperands) {
2827 // No resize needed.
2828 if (ReservedSpace >= NumOps) return;
2829 } else if (NumOps == NumOperands) {
2830 if (ReservedSpace == NumOps) return;
2831 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002832 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002833 }
2834
2835 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002836 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002837 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002838 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002839 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002840 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002841 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002842 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002843}
2844
2845
2846BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2847 return getSuccessor(idx);
2848}
2849unsigned SwitchInst::getNumSuccessorsV() const {
2850 return getNumSuccessors();
2851}
2852void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2853 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002854}
Chris Lattnerf22be932004-10-15 23:52:53 +00002855
Chris Lattnerf22be932004-10-15 23:52:53 +00002856// Define these methods here so vtables don't get emitted into every translation
2857// unit that uses these classes.
2858
2859GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002860 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002861}
2862
2863BinaryOperator *BinaryOperator::clone() const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002864 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002865}
2866
Chris Lattner0b490b02007-08-24 20:48:18 +00002867FCmpInst* FCmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002868 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002869}
2870ICmpInst* ICmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002871 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002872}
2873
Nate Begemand2195702008-05-12 19:01:56 +00002874VFCmpInst* VFCmpInst::clone() const {
2875 return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
2876}
2877VICmpInst* VICmpInst::clone() const {
2878 return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
2879}
2880
Dan Gohman0752bff2008-05-23 00:36:11 +00002881ExtractValueInst *ExtractValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002882 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002883}
2884InsertValueInst *InsertValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002885 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002886}
2887
2888
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002889MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2890AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2891FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2892LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2893StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2894CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2895CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2896CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2897CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2898CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2899CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2900CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2901CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2902CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2903CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2904CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2905CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002906CallInst *CallInst::clone() const {
2907 return new(getNumOperands()) CallInst(*this);
2908}
2909SelectInst *SelectInst::clone() const {
2910 return new(getNumOperands()) SelectInst(*this);
2911}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002912VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2913
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002914ExtractElementInst *ExtractElementInst::clone() const {
2915 return new ExtractElementInst(*this);
2916}
2917InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002918 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002919}
2920ShuffleVectorInst *ShuffleVectorInst::clone() const {
2921 return new ShuffleVectorInst(*this);
2922}
Chris Lattnerf22be932004-10-15 23:52:53 +00002923PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002924ReturnInst *ReturnInst::clone() const {
2925 return new(getNumOperands()) ReturnInst(*this);
2926}
2927BranchInst *BranchInst::clone() const {
2928 return new(getNumOperands()) BranchInst(*this);
2929}
Gabor Greiff6caff662008-05-10 08:32:32 +00002930SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002931InvokeInst *InvokeInst::clone() const {
2932 return new(getNumOperands()) InvokeInst(*this);
2933}
Chris Lattnerf22be932004-10-15 23:52:53 +00002934UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002935UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}