blob: 8d14766c21a6911027f39a0034bdefb68ee2835e [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?");
Bill Wendling4bb96e92009-03-13 21:15:59 +0000434 Use *OL = OperandList;
435 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
Bill Wendling4bb96e92009-03-13 21:15:59 +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 Greifc91aa9b2009-03-12 18:34:49 +0000615 Op<-1>() = 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 Greifc91aa9b2009-03-12 18:34:49 +0000622 Op<-1>() = IfTrue;
623 Op<-2>() = IfFalse;
624 Op<-3>() = 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 Greifc91aa9b2009-03-12 18:34:49 +0000635 Op<-1>() = 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 Greifc91aa9b2009-03-12 18:34:49 +0000643 Op<-1>() = IfTrue;
644 Op<-2>() = IfFalse;
645 Op<-3>() = 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 Greifc91aa9b2009-03-12 18:34:49 +0000656 Op<-1>() = BI.Op<-1>();
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 Greifc91aa9b2009-03-12 18:34:49 +0000659 Op<-3>() = BI.Op<-3>();
660 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000661 }
662}
663
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000664
665Use* Use::getPrefix() {
666 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
667 if (PotentialPrefix.getOpaqueValue())
668 return 0;
669
670 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
671}
672
673BranchInst::~BranchInst() {
674 if (NumOperands == 1) {
675 if (Use *Prefix = OperandList->getPrefix()) {
676 Op<-1>() = 0;
677 //
678 // mark OperandList to have a special value for scrutiny
679 // by baseclass destructors and operator delete
680 OperandList = Prefix;
681 } else {
682 NumOperands = 3;
683 OperandList = op_begin();
684 }
685 }
686}
687
688
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000689BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
690 return getSuccessor(idx);
691}
692unsigned BranchInst::getNumSuccessorsV() const {
693 return getNumSuccessors();
694}
695void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
696 setSuccessor(idx, B);
697}
698
699
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000700//===----------------------------------------------------------------------===//
701// AllocationInst Implementation
702//===----------------------------------------------------------------------===//
703
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000704static Value *getAISize(Value *Amt) {
705 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000706 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000707 else {
708 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000709 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000710 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000711 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000712 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000713 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000714}
715
Misha Brukmanb1c93172005-04-21 23:48:37 +0000716AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000717 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000718 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000719 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000720 InsertBefore) {
721 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000722 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000723 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000724}
725
Misha Brukmanb1c93172005-04-21 23:48:37 +0000726AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000727 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000728 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000729 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000730 InsertAtEnd) {
731 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000732 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000733 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000734}
735
Gordon Henriksen14a55692007-12-10 02:14:30 +0000736// Out of line virtual method, so the vtable, etc has a home.
737AllocationInst::~AllocationInst() {
738}
739
Dan Gohmanaa583d72008-03-24 16:55:58 +0000740void AllocationInst::setAlignment(unsigned Align) {
741 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
742 SubclassData = Log2_32(Align) + 1;
743 assert(getAlignment() == Align && "Alignment representation error!");
744}
745
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000746bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000747 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
748 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000749 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000750}
751
752const Type *AllocationInst::getAllocatedType() const {
753 return getType()->getElementType();
754}
755
756AllocaInst::AllocaInst(const AllocaInst &AI)
757 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000758 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000759}
760
Chris Lattner8b291e62008-11-26 02:54:17 +0000761/// isStaticAlloca - Return true if this alloca is in the entry block of the
762/// function and is a constant size. If so, the code generator will fold it
763/// into the prolog/epilog code, so it is basically free.
764bool AllocaInst::isStaticAlloca() const {
765 // Must be constant size.
766 if (!isa<ConstantInt>(getArraySize())) return false;
767
768 // Must be in the entry block.
769 const BasicBlock *Parent = getParent();
770 return Parent == &Parent->getParent()->front();
771}
772
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000773MallocInst::MallocInst(const MallocInst &MI)
774 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000775 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000776}
777
778//===----------------------------------------------------------------------===//
779// FreeInst Implementation
780//===----------------------------------------------------------------------===//
781
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000782void FreeInst::AssertOK() {
783 assert(isa<PointerType>(getOperand(0)->getType()) &&
784 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000785}
786
787FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000788 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000789 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000790}
791
792FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000793 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000794 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000795}
796
797
798//===----------------------------------------------------------------------===//
799// LoadInst Implementation
800//===----------------------------------------------------------------------===//
801
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000802void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000803 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000804 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000805}
806
807LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000808 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000809 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000810 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000811 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000812 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000813 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000814}
815
816LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000817 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000818 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000819 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000820 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000821 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000822 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000823}
824
825LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
826 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000827 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000828 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000829 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000830 setAlignment(0);
831 AssertOK();
832 setName(Name);
833}
834
835LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
836 unsigned Align, Instruction *InsertBef)
837 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
838 Load, Ptr, InsertBef) {
839 setVolatile(isVolatile);
840 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000841 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000842 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000843}
844
Dan Gohman68659282007-07-18 20:51:11 +0000845LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
846 unsigned Align, BasicBlock *InsertAE)
847 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
848 Load, Ptr, InsertAE) {
849 setVolatile(isVolatile);
850 setAlignment(Align);
851 AssertOK();
852 setName(Name);
853}
854
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000855LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
856 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000857 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000858 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000859 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000860 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000861 AssertOK();
862 setName(Name);
863}
864
865
866
867LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000868 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
869 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000870 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000871 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000872 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000873 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000874}
875
876LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000877 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
878 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000879 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000880 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000881 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000882 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000883}
884
885LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
886 Instruction *InsertBef)
887: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000888 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000889 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000890 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000891 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000892 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000893}
894
895LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
896 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000897 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
898 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000899 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000900 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000901 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000902 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000903}
904
Christopher Lamb84485702007-04-22 19:24:39 +0000905void LoadInst::setAlignment(unsigned Align) {
906 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
907 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
908}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000909
910//===----------------------------------------------------------------------===//
911// StoreInst Implementation
912//===----------------------------------------------------------------------===//
913
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000914void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000915 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000916 assert(isa<PointerType>(getOperand(1)->getType()) &&
917 "Ptr must have pointer type!");
918 assert(getOperand(0)->getType() ==
919 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000920 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000921}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000922
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000923
924StoreInst::StoreInst(Value *val, Value *addr, 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(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000932 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000933 AssertOK();
934}
935
936StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000937 : Instruction(Type::VoidTy, Store,
938 OperandTraits<StoreInst>::op_begin(this),
939 OperandTraits<StoreInst>::operands(this),
940 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000941 Op<0>() = val;
942 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000943 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000944 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000945 AssertOK();
946}
947
Misha Brukmanb1c93172005-04-21 23:48:37 +0000948StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000949 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000950 : Instruction(Type::VoidTy, Store,
951 OperandTraits<StoreInst>::op_begin(this),
952 OperandTraits<StoreInst>::operands(this),
953 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000954 Op<0>() = val;
955 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000956 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000957 setAlignment(0);
958 AssertOK();
959}
960
961StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
962 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000963 : Instruction(Type::VoidTy, Store,
964 OperandTraits<StoreInst>::op_begin(this),
965 OperandTraits<StoreInst>::operands(this),
966 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000967 Op<0>() = val;
968 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000969 setVolatile(isVolatile);
970 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000971 AssertOK();
972}
973
Misha Brukmanb1c93172005-04-21 23:48:37 +0000974StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000975 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000976 : Instruction(Type::VoidTy, Store,
977 OperandTraits<StoreInst>::op_begin(this),
978 OperandTraits<StoreInst>::operands(this),
979 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000980 Op<0>() = val;
981 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000982 setVolatile(isVolatile);
983 setAlignment(Align);
984 AssertOK();
985}
986
987StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000988 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000989 : Instruction(Type::VoidTy, Store,
990 OperandTraits<StoreInst>::op_begin(this),
991 OperandTraits<StoreInst>::operands(this),
992 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000993 Op<0>() = val;
994 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000995 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000996 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000997 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000998}
999
Christopher Lamb84485702007-04-22 19:24:39 +00001000void StoreInst::setAlignment(unsigned Align) {
1001 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1002 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1003}
1004
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001005//===----------------------------------------------------------------------===//
1006// GetElementPtrInst Implementation
1007//===----------------------------------------------------------------------===//
1008
Christopher Lambedf07882007-12-17 01:12:55 +00001009static unsigned retrieveAddrSpace(const Value *Val) {
1010 return cast<PointerType>(Val->getType())->getAddressSpace();
1011}
1012
Gabor Greif21ba1842008-06-06 20:28:12 +00001013void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Nate Begeman8a4afd92008-07-25 17:24:13 +00001014 const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001015 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1016 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001017 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001018
Chris Lattner79807c3d2007-01-31 19:47:18 +00001019 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001020 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001021
1022 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001023}
1024
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001025void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001026 assert(NumOperands == 2 && "NumOperands not initialized?");
1027 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001028 OL[0] = Ptr;
1029 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001030
1031 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001032}
1033
Gabor Greiff6caff662008-05-10 08:32:32 +00001034GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001035 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001036 OperandTraits<GetElementPtrInst>::op_end(this)
1037 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001038 GEPI.getNumOperands()) {
1039 Use *OL = OperandList;
1040 Use *GEPIOL = GEPI.OperandList;
1041 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001042 OL[i] = GEPIOL[i];
Gabor Greiff6caff662008-05-10 08:32:32 +00001043}
1044
Chris Lattner82981202005-05-03 05:43:30 +00001045GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1046 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001047 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001048 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001049 GetElementPtr,
1050 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1051 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001052 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001053}
1054
1055GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1056 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001057 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001058 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001059 GetElementPtr,
1060 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1061 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001062 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001063}
1064
Chris Lattner6090a422009-03-09 04:46:40 +00001065/// getIndexedType - Returns the type of the element that would be accessed with
1066/// a gep instruction with the specified parameters.
1067///
1068/// The Idxs pointer should point to a continuous piece of memory containing the
1069/// indices, either as Value* or uint64_t.
1070///
1071/// A null type is returned if the indices are invalid for the specified
1072/// pointer type.
1073///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001074template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001075static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1076 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001077 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1078 if (!PTy) return 0; // Type isn't a pointer type!
1079 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001080
Chris Lattner6090a422009-03-09 04:46:40 +00001081 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001082 if (NumIdx == 0)
1083 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001084
1085 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001086 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1087 // that contain opaque types) under the assumption that it will be resolved to
1088 // a sane type later.
1089 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001090 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001091
Dan Gohman1ecaf452008-05-31 00:58:22 +00001092 unsigned CurIdx = 1;
1093 for (; CurIdx != NumIdx; ++CurIdx) {
1094 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1095 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001096 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001097 if (!CT->indexValid(Index)) return 0;
1098 Agg = CT->getTypeAtIndex(Index);
1099
1100 // If the new type forwards to another type, then it is in the middle
1101 // of being refined to another type (and hence, may have dropped all
1102 // references to what it was using before). So, use the new forwarded
1103 // type.
1104 if (const Type *Ty = Agg->getForwardedType())
1105 Agg = Ty;
1106 }
1107 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001108}
1109
Matthijs Kooijman04468622008-07-29 08:46:11 +00001110const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1111 Value* const *Idxs,
1112 unsigned NumIdx) {
1113 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1114}
1115
1116const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1117 uint64_t const *Idxs,
1118 unsigned NumIdx) {
1119 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1120}
1121
Chris Lattner82981202005-05-03 05:43:30 +00001122const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1123 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1124 if (!PTy) return 0; // Type isn't a pointer type!
1125
1126 // Check the pointer index.
1127 if (!PTy->indexValid(Idx)) return 0;
1128
Chris Lattnerc2233332005-05-03 16:44:45 +00001129 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001130}
1131
Chris Lattner45f15572007-04-14 00:12:57 +00001132
1133/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1134/// zeros. If so, the result pointer and the first operand have the same
1135/// value, just potentially different types.
1136bool GetElementPtrInst::hasAllZeroIndices() const {
1137 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1138 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1139 if (!CI->isZero()) return false;
1140 } else {
1141 return false;
1142 }
1143 }
1144 return true;
1145}
1146
Chris Lattner27058292007-04-27 20:35:56 +00001147/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1148/// constant integers. If so, the result pointer and the first operand have
1149/// a constant offset between them.
1150bool GetElementPtrInst::hasAllConstantIndices() const {
1151 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1152 if (!isa<ConstantInt>(getOperand(i)))
1153 return false;
1154 }
1155 return true;
1156}
1157
Chris Lattner45f15572007-04-14 00:12:57 +00001158
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001159//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001160// ExtractElementInst Implementation
1161//===----------------------------------------------------------------------===//
1162
1163ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001164 const std::string &Name,
1165 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001166 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001167 ExtractElement,
1168 OperandTraits<ExtractElementInst>::op_begin(this),
1169 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001170 assert(isValidOperands(Val, Index) &&
1171 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001172 Op<0>() = Val;
1173 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001174 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001175}
1176
Chris Lattner65511ff2006-10-05 06:24:58 +00001177ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1178 const std::string &Name,
1179 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001180 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001181 ExtractElement,
1182 OperandTraits<ExtractElementInst>::op_begin(this),
1183 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001184 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001185 assert(isValidOperands(Val, Index) &&
1186 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001187 Op<0>() = Val;
1188 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001189 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001190}
1191
1192
Robert Bocchino23004482006-01-10 19:05:34 +00001193ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001194 const std::string &Name,
1195 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001196 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001197 ExtractElement,
1198 OperandTraits<ExtractElementInst>::op_begin(this),
1199 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001200 assert(isValidOperands(Val, Index) &&
1201 "Invalid extractelement instruction operands!");
1202
Gabor Greif2d3024d2008-05-26 21:33:52 +00001203 Op<0>() = Val;
1204 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001205 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001206}
1207
Chris Lattner65511ff2006-10-05 06:24:58 +00001208ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1209 const std::string &Name,
1210 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001211 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001212 ExtractElement,
1213 OperandTraits<ExtractElementInst>::op_begin(this),
1214 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001215 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001216 assert(isValidOperands(Val, Index) &&
1217 "Invalid extractelement instruction operands!");
1218
Gabor Greif2d3024d2008-05-26 21:33:52 +00001219 Op<0>() = Val;
1220 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001221 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001222}
1223
1224
Chris Lattner54865b32006-04-08 04:05:48 +00001225bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001226 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001227 return false;
1228 return true;
1229}
1230
1231
Robert Bocchino23004482006-01-10 19:05:34 +00001232//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001233// InsertElementInst Implementation
1234//===----------------------------------------------------------------------===//
1235
Chris Lattner0875d942006-04-14 22:20:32 +00001236InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001237 : Instruction(IE.getType(), InsertElement,
1238 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001239 Op<0>() = IE.Op<0>();
1240 Op<1>() = IE.Op<1>();
1241 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001242}
Chris Lattner54865b32006-04-08 04:05:48 +00001243InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001244 const std::string &Name,
1245 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001246 : Instruction(Vec->getType(), InsertElement,
1247 OperandTraits<InsertElementInst>::op_begin(this),
1248 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001249 assert(isValidOperands(Vec, Elt, Index) &&
1250 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001251 Op<0>() = Vec;
1252 Op<1>() = Elt;
1253 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001254 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001255}
1256
Chris Lattner65511ff2006-10-05 06:24:58 +00001257InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1258 const std::string &Name,
1259 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001260 : Instruction(Vec->getType(), InsertElement,
1261 OperandTraits<InsertElementInst>::op_begin(this),
1262 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001263 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001264 assert(isValidOperands(Vec, Elt, Index) &&
1265 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001266 Op<0>() = Vec;
1267 Op<1>() = Elt;
1268 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001269 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001270}
1271
1272
Chris Lattner54865b32006-04-08 04:05:48 +00001273InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001274 const std::string &Name,
1275 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001276 : Instruction(Vec->getType(), InsertElement,
1277 OperandTraits<InsertElementInst>::op_begin(this),
1278 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001279 assert(isValidOperands(Vec, Elt, Index) &&
1280 "Invalid insertelement instruction operands!");
1281
Gabor Greif2d3024d2008-05-26 21:33:52 +00001282 Op<0>() = Vec;
1283 Op<1>() = Elt;
1284 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001285 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001286}
1287
Chris Lattner65511ff2006-10-05 06:24:58 +00001288InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1289 const std::string &Name,
1290 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001291: Instruction(Vec->getType(), InsertElement,
1292 OperandTraits<InsertElementInst>::op_begin(this),
1293 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001294 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001295 assert(isValidOperands(Vec, Elt, Index) &&
1296 "Invalid insertelement instruction operands!");
1297
Gabor Greif2d3024d2008-05-26 21:33:52 +00001298 Op<0>() = Vec;
1299 Op<1>() = Elt;
1300 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001301 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001302}
1303
Chris Lattner54865b32006-04-08 04:05:48 +00001304bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1305 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001306 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001307 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001308
Reid Spencerd84d35b2007-02-15 02:26:10 +00001309 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001310 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001311
Reid Spencer8d9336d2006-12-31 05:26:44 +00001312 if (Index->getType() != Type::Int32Ty)
Dan Gohman4fe64de2009-06-14 23:30:43 +00001313 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001314 return true;
1315}
1316
1317
Robert Bocchinoca27f032006-01-17 20:07:22 +00001318//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001319// ShuffleVectorInst Implementation
1320//===----------------------------------------------------------------------===//
1321
Chris Lattner0875d942006-04-14 22:20:32 +00001322ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001323 : Instruction(SV.getType(), ShuffleVector,
1324 OperandTraits<ShuffleVectorInst>::op_begin(this),
1325 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001326 Op<0>() = SV.Op<0>();
1327 Op<1>() = SV.Op<1>();
1328 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001329}
1330
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001331ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1332 const std::string &Name,
1333 Instruction *InsertBefore)
Mon P Wang25f01062008-11-10 04:46:22 +00001334: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1335 cast<VectorType>(Mask->getType())->getNumElements()),
1336 ShuffleVector,
1337 OperandTraits<ShuffleVectorInst>::op_begin(this),
1338 OperandTraits<ShuffleVectorInst>::operands(this),
1339 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001340 assert(isValidOperands(V1, V2, Mask) &&
1341 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001342 Op<0>() = V1;
1343 Op<1>() = V2;
1344 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001345 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001346}
1347
1348ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Mon P Wang25f01062008-11-10 04:46:22 +00001349 const std::string &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001350 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001351 : Instruction(V1->getType(), ShuffleVector,
1352 OperandTraits<ShuffleVectorInst>::op_begin(this),
1353 OperandTraits<ShuffleVectorInst>::operands(this),
1354 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001355 assert(isValidOperands(V1, V2, Mask) &&
1356 "Invalid shuffle vector instruction operands!");
1357
Gabor Greif2d3024d2008-05-26 21:33:52 +00001358 Op<0>() = V1;
1359 Op<1>() = V2;
1360 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001361 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001362}
1363
Mon P Wang25f01062008-11-10 04:46:22 +00001364bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001365 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001366 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001367 return false;
1368
1369 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1370 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Mon P Wang25f01062008-11-10 04:46:22 +00001371 MaskTy->getElementType() != Type::Int32Ty)
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001372 return false;
1373 return true;
1374}
1375
Chris Lattnerf724e342008-03-02 05:28:33 +00001376/// getMaskValue - Return the index from the shuffle mask for the specified
1377/// output result. This is either -1 if the element is undef or a number less
1378/// than 2*numelements.
1379int ShuffleVectorInst::getMaskValue(unsigned i) const {
1380 const Constant *Mask = cast<Constant>(getOperand(2));
1381 if (isa<UndefValue>(Mask)) return -1;
1382 if (isa<ConstantAggregateZero>(Mask)) return 0;
1383 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1384 assert(i < MaskCV->getNumOperands() && "Index out of range");
1385
1386 if (isa<UndefValue>(MaskCV->getOperand(i)))
1387 return -1;
1388 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1389}
1390
Dan Gohman12fce772008-05-15 19:50:34 +00001391//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001392// InsertValueInst Class
1393//===----------------------------------------------------------------------===//
1394
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001395void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1396 unsigned NumIdx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001397 assert(NumOperands == 2 && "NumOperands not initialized?");
1398 Op<0>() = Agg;
1399 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001400
Dan Gohman1ecaf452008-05-31 00:58:22 +00001401 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001402 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001403}
1404
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001405void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1406 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001407 assert(NumOperands == 2 && "NumOperands not initialized?");
1408 Op<0>() = Agg;
1409 Op<1>() = Val;
1410
1411 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001412 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001413}
1414
1415InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001416 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001417 OperandTraits<InsertValueInst>::op_begin(this), 2),
1418 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001419 Op<0>() = IVI.getOperand(0);
1420 Op<1>() = IVI.getOperand(1);
Dan Gohman0752bff2008-05-23 00:36:11 +00001421}
1422
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001423InsertValueInst::InsertValueInst(Value *Agg,
1424 Value *Val,
1425 unsigned Idx,
1426 const std::string &Name,
1427 Instruction *InsertBefore)
1428 : Instruction(Agg->getType(), InsertValue,
1429 OperandTraits<InsertValueInst>::op_begin(this),
1430 2, InsertBefore) {
1431 init(Agg, Val, Idx, Name);
1432}
1433
1434InsertValueInst::InsertValueInst(Value *Agg,
1435 Value *Val,
1436 unsigned Idx,
1437 const std::string &Name,
1438 BasicBlock *InsertAtEnd)
1439 : Instruction(Agg->getType(), InsertValue,
1440 OperandTraits<InsertValueInst>::op_begin(this),
1441 2, InsertAtEnd) {
1442 init(Agg, Val, Idx, Name);
1443}
1444
Dan Gohman0752bff2008-05-23 00:36:11 +00001445//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001446// ExtractValueInst Class
1447//===----------------------------------------------------------------------===//
1448
Gabor Greifcbcc4952008-06-06 21:06:32 +00001449void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Gabor Greif1b9921a2009-01-11 22:33:22 +00001450 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001451 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001452
Dan Gohman1ecaf452008-05-31 00:58:22 +00001453 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001454 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001455}
1456
Gabor Greifcbcc4952008-06-06 21:06:32 +00001457void ExtractValueInst::init(unsigned Idx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001458 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001459
1460 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001461 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001462}
1463
1464ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001465 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001466 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001467}
1468
Dan Gohman12fce772008-05-15 19:50:34 +00001469// getIndexedType - Returns the type of the element that would be extracted
1470// with an extractvalue instruction with the specified parameters.
1471//
1472// A null type is returned if the indices are invalid for the specified
1473// pointer type.
1474//
1475const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001476 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001477 unsigned NumIdx) {
1478 unsigned CurIdx = 0;
1479 for (; CurIdx != NumIdx; ++CurIdx) {
1480 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001481 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1482 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001483 if (!CT->indexValid(Index)) return 0;
1484 Agg = CT->getTypeAtIndex(Index);
1485
1486 // If the new type forwards to another type, then it is in the middle
1487 // of being refined to another type (and hence, may have dropped all
1488 // references to what it was using before). So, use the new forwarded
1489 // type.
1490 if (const Type *Ty = Agg->getForwardedType())
1491 Agg = Ty;
1492 }
1493 return CurIdx == NumIdx ? Agg : 0;
1494}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001495
Dan Gohman4a3125b2008-06-17 21:07:55 +00001496const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001497 unsigned Idx) {
1498 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001499}
1500
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001501//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001502// BinaryOperator Class
1503//===----------------------------------------------------------------------===//
1504
Dan Gohmana5b96452009-06-04 22:49:04 +00001505/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1506/// type is floating-point, to help provide compatibility with an older API.
1507///
1508static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1509 const Type *Ty) {
1510 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1511 if (Ty->isFPOrFPVector()) {
1512 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1513 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1514 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1515 }
1516 return iType;
1517}
1518
Chris Lattner2195fc42007-02-24 00:55:48 +00001519BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1520 const Type *Ty, const std::string &Name,
1521 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001522 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001523 OperandTraits<BinaryOperator>::op_begin(this),
1524 OperandTraits<BinaryOperator>::operands(this),
1525 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001526 Op<0>() = S1;
1527 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001528 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001529 setName(Name);
1530}
1531
1532BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1533 const Type *Ty, const std::string &Name,
1534 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001535 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001536 OperandTraits<BinaryOperator>::op_begin(this),
1537 OperandTraits<BinaryOperator>::operands(this),
1538 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001539 Op<0>() = S1;
1540 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001541 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001542 setName(Name);
1543}
1544
1545
1546void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001547 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001548 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001549 assert(LHS->getType() == RHS->getType() &&
1550 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001551#ifndef NDEBUG
1552 switch (iType) {
1553 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001554 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001555 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001556 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001557 assert(getType()->isIntOrIntVector() &&
1558 "Tried to create an integer operation on a non-integer type!");
1559 break;
1560 case FAdd: case FSub:
1561 case FMul:
1562 assert(getType() == LHS->getType() &&
1563 "Arithmetic operation should return same type as operands!");
1564 assert(getType()->isFPOrFPVector() &&
1565 "Tried to create a floating-point operation on a "
1566 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001567 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001568 case UDiv:
1569 case SDiv:
1570 assert(getType() == LHS->getType() &&
1571 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001572 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1573 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001574 "Incorrect operand type (not integer) for S/UDIV");
1575 break;
1576 case FDiv:
1577 assert(getType() == LHS->getType() &&
1578 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001579 assert(getType()->isFPOrFPVector() &&
1580 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001581 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001582 case URem:
1583 case SRem:
1584 assert(getType() == LHS->getType() &&
1585 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001586 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1587 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001588 "Incorrect operand type (not integer) for S/UREM");
1589 break;
1590 case FRem:
1591 assert(getType() == LHS->getType() &&
1592 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001593 assert(getType()->isFPOrFPVector() &&
1594 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001595 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001596 case Shl:
1597 case LShr:
1598 case AShr:
1599 assert(getType() == LHS->getType() &&
1600 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001601 assert((getType()->isInteger() ||
1602 (isa<VectorType>(getType()) &&
1603 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1604 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001605 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001606 case And: case Or:
1607 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001608 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001609 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001610 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001611 (isa<VectorType>(getType()) &&
1612 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001613 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001614 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001615 default:
1616 break;
1617 }
1618#endif
1619}
1620
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001621BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001622 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001623 Instruction *InsertBefore) {
1624 assert(S1->getType() == S2->getType() &&
1625 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001626 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001627}
1628
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001629BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001630 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001631 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001632 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001633 InsertAtEnd->getInstList().push_back(Res);
1634 return Res;
1635}
1636
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001637BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001638 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001639 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1640 return new BinaryOperator(Instruction::Sub,
1641 zero, Op,
1642 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001643}
1644
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001645BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001646 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001647 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1648 return new BinaryOperator(Instruction::Sub,
1649 zero, Op,
1650 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001651}
1652
Dan Gohmana5b96452009-06-04 22:49:04 +00001653BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const std::string &Name,
1654 Instruction *InsertBefore) {
1655 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1656 return new BinaryOperator(Instruction::FSub,
1657 zero, Op,
1658 Op->getType(), Name, InsertBefore);
1659}
1660
1661BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const std::string &Name,
1662 BasicBlock *InsertAtEnd) {
1663 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1664 return new BinaryOperator(Instruction::FSub,
1665 zero, Op,
1666 Op->getType(), Name, InsertAtEnd);
1667}
1668
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001669BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001670 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001671 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001672 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001673 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001674 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001675 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001676 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001677 }
1678
1679 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001680 Op->getType(), Name, InsertBefore);
1681}
1682
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001683BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001684 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001685 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001686 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001687 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001688 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001689 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001690 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001691 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001692 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001693 }
1694
1695 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001696 Op->getType(), Name, InsertAtEnd);
1697}
1698
1699
1700// isConstantAllOnes - Helper function for several functions below
1701static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001702 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1703 return CI->isAllOnesValue();
1704 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1705 return CV->isAllOnesValue();
1706 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001707}
1708
1709bool BinaryOperator::isNeg(const Value *V) {
1710 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1711 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001712 return Bop->getOperand(0) ==
1713 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001714 return false;
1715}
1716
Dan Gohmana5b96452009-06-04 22:49:04 +00001717bool BinaryOperator::isFNeg(const Value *V) {
1718 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1719 if (Bop->getOpcode() == Instruction::FSub)
1720 return Bop->getOperand(0) ==
1721 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
1722 return false;
1723}
1724
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001725bool BinaryOperator::isNot(const Value *V) {
1726 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1727 return (Bop->getOpcode() == Instruction::Xor &&
1728 (isConstantAllOnes(Bop->getOperand(1)) ||
1729 isConstantAllOnes(Bop->getOperand(0))));
1730 return false;
1731}
1732
Chris Lattner2c7d1772005-04-24 07:28:37 +00001733Value *BinaryOperator::getNegArgument(Value *BinOp) {
1734 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1735 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001736}
1737
Chris Lattner2c7d1772005-04-24 07:28:37 +00001738const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1739 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001740}
1741
Dan Gohmana5b96452009-06-04 22:49:04 +00001742Value *BinaryOperator::getFNegArgument(Value *BinOp) {
1743 assert(isFNeg(BinOp) && "getFNegArgument from non-'fneg' instruction!");
1744 return cast<BinaryOperator>(BinOp)->getOperand(1);
1745}
1746
1747const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1748 return getFNegArgument(const_cast<Value*>(BinOp));
1749}
1750
Chris Lattner2c7d1772005-04-24 07:28:37 +00001751Value *BinaryOperator::getNotArgument(Value *BinOp) {
1752 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1753 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1754 Value *Op0 = BO->getOperand(0);
1755 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001756 if (isConstantAllOnes(Op0)) return Op1;
1757
1758 assert(isConstantAllOnes(Op1));
1759 return Op0;
1760}
1761
Chris Lattner2c7d1772005-04-24 07:28:37 +00001762const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1763 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001764}
1765
1766
1767// swapOperands - Exchange the two operands to this instruction. This
1768// instruction is safe to use on any binary instruction and does not
1769// modify the semantics of the instruction. If the instruction is
1770// order dependent (SetLT f.e.) the opcode is changed.
1771//
1772bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001773 if (!isCommutative())
1774 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001775 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001776 return false;
1777}
1778
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001779//===----------------------------------------------------------------------===//
1780// CastInst Class
1781//===----------------------------------------------------------------------===//
1782
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001783// Just determine if this cast only deals with integral->integral conversion.
1784bool CastInst::isIntegerCast() const {
1785 switch (getOpcode()) {
1786 default: return false;
1787 case Instruction::ZExt:
1788 case Instruction::SExt:
1789 case Instruction::Trunc:
1790 return true;
1791 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001792 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001793 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001794}
1795
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001796bool CastInst::isLosslessCast() const {
1797 // Only BitCast can be lossless, exit fast if we're not BitCast
1798 if (getOpcode() != Instruction::BitCast)
1799 return false;
1800
1801 // Identity cast is always lossless
1802 const Type* SrcTy = getOperand(0)->getType();
1803 const Type* DstTy = getType();
1804 if (SrcTy == DstTy)
1805 return true;
1806
Reid Spencer8d9336d2006-12-31 05:26:44 +00001807 // Pointer to pointer is always lossless.
1808 if (isa<PointerType>(SrcTy))
1809 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001810 return false; // Other types have no identity values
1811}
1812
1813/// This function determines if the CastInst does not require any bits to be
1814/// changed in order to effect the cast. Essentially, it identifies cases where
1815/// no code gen is necessary for the cast, hence the name no-op cast. For
1816/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001817/// # bitcast i32* %x to i8*
1818/// # bitcast <2 x i32> %x to <4 x i16>
1819/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001820/// @brief Determine if a cast is a no-op.
1821bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1822 switch (getOpcode()) {
1823 default:
1824 assert(!"Invalid CastOp");
1825 case Instruction::Trunc:
1826 case Instruction::ZExt:
1827 case Instruction::SExt:
1828 case Instruction::FPTrunc:
1829 case Instruction::FPExt:
1830 case Instruction::UIToFP:
1831 case Instruction::SIToFP:
1832 case Instruction::FPToUI:
1833 case Instruction::FPToSI:
1834 return false; // These always modify bits
1835 case Instruction::BitCast:
1836 return true; // BitCast never modifies bits.
1837 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001838 return IntPtrTy->getScalarSizeInBits() ==
1839 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001840 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001841 return IntPtrTy->getScalarSizeInBits() ==
1842 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001843 }
1844}
1845
1846/// This function determines if a pair of casts can be eliminated and what
1847/// opcode should be used in the elimination. This assumes that there are two
1848/// instructions like this:
1849/// * %F = firstOpcode SrcTy %x to MidTy
1850/// * %S = secondOpcode MidTy %F to DstTy
1851/// The function returns a resultOpcode so these two casts can be replaced with:
1852/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1853/// If no such cast is permited, the function returns 0.
1854unsigned CastInst::isEliminableCastPair(
1855 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1856 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1857{
1858 // Define the 144 possibilities for these two cast instructions. The values
1859 // in this matrix determine what to do in a given situation and select the
1860 // case in the switch below. The rows correspond to firstOp, the columns
1861 // correspond to secondOp. In looking at the table below, keep in mind
1862 // the following cast properties:
1863 //
1864 // Size Compare Source Destination
1865 // Operator Src ? Size Type Sign Type Sign
1866 // -------- ------------ ------------------- ---------------------
1867 // TRUNC > Integer Any Integral Any
1868 // ZEXT < Integral Unsigned Integer Any
1869 // SEXT < Integral Signed Integer Any
1870 // FPTOUI n/a FloatPt n/a Integral Unsigned
1871 // FPTOSI n/a FloatPt n/a Integral Signed
1872 // UITOFP n/a Integral Unsigned FloatPt n/a
1873 // SITOFP n/a Integral Signed FloatPt n/a
1874 // FPTRUNC > FloatPt n/a FloatPt n/a
1875 // FPEXT < FloatPt n/a FloatPt n/a
1876 // PTRTOINT n/a Pointer n/a Integral Unsigned
1877 // INTTOPTR n/a Integral Unsigned Pointer n/a
1878 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001879 //
1880 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001881 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1882 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001883 // of the produced value (we no longer know the top-part is all zeros).
1884 // Further this conversion is often much more expensive for typical hardware,
1885 // and causes issues when building libgcc. We disallow fptosi+sext for the
1886 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001887 const unsigned numCastOps =
1888 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1889 static const uint8_t CastResults[numCastOps][numCastOps] = {
1890 // T F F U S F F P I B -+
1891 // R Z S P P I I T P 2 N T |
1892 // U E E 2 2 2 2 R E I T C +- secondOp
1893 // N X X U S F F N X N 2 V |
1894 // C T T I I P P C T T P T -+
1895 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1896 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1897 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001898 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1899 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001900 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1901 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1902 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1903 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1904 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1905 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1906 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1907 };
1908
1909 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1910 [secondOp-Instruction::CastOpsBegin];
1911 switch (ElimCase) {
1912 case 0:
1913 // categorically disallowed
1914 return 0;
1915 case 1:
1916 // allowed, use first cast's opcode
1917 return firstOp;
1918 case 2:
1919 // allowed, use second cast's opcode
1920 return secondOp;
1921 case 3:
1922 // no-op cast in second op implies firstOp as long as the DestTy
1923 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001924 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001925 return firstOp;
1926 return 0;
1927 case 4:
1928 // no-op cast in second op implies firstOp as long as the DestTy
1929 // is floating point
1930 if (DstTy->isFloatingPoint())
1931 return firstOp;
1932 return 0;
1933 case 5:
1934 // no-op cast in first op implies secondOp as long as the SrcTy
1935 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001936 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001937 return secondOp;
1938 return 0;
1939 case 6:
1940 // no-op cast in first op implies secondOp as long as the SrcTy
1941 // is a floating point
1942 if (SrcTy->isFloatingPoint())
1943 return secondOp;
1944 return 0;
1945 case 7: {
1946 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001947 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1948 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001949 if (MidSize >= PtrSize)
1950 return Instruction::BitCast;
1951 return 0;
1952 }
1953 case 8: {
1954 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1955 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1956 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001957 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1958 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001959 if (SrcSize == DstSize)
1960 return Instruction::BitCast;
1961 else if (SrcSize < DstSize)
1962 return firstOp;
1963 return secondOp;
1964 }
1965 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1966 return Instruction::ZExt;
1967 case 10:
1968 // fpext followed by ftrunc is allowed if the bit size returned to is
1969 // the same as the original, in which case its just a bitcast
1970 if (SrcTy == DstTy)
1971 return Instruction::BitCast;
1972 return 0; // If the types are not the same we can't eliminate it.
1973 case 11:
1974 // bitcast followed by ptrtoint is allowed as long as the bitcast
1975 // is a pointer to pointer cast.
1976 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1977 return secondOp;
1978 return 0;
1979 case 12:
1980 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1981 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1982 return firstOp;
1983 return 0;
1984 case 13: {
1985 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001986 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1987 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1988 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001989 if (SrcSize <= PtrSize && SrcSize == DstSize)
1990 return Instruction::BitCast;
1991 return 0;
1992 }
1993 case 99:
1994 // cast combination can't happen (error in input). This is for all cases
1995 // where the MidTy is not the same for the two cast instructions.
1996 assert(!"Invalid Cast Combination");
1997 return 0;
1998 default:
1999 assert(!"Error in CastResults table!!!");
2000 return 0;
2001 }
2002 return 0;
2003}
2004
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002005CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002006 const std::string &Name, Instruction *InsertBefore) {
2007 // Construct and return the appropriate CastInst subclass
2008 switch (op) {
2009 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2010 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2011 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2012 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2013 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2014 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2015 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2016 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2017 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2018 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2019 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2020 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2021 default:
2022 assert(!"Invalid opcode provided");
2023 }
2024 return 0;
2025}
2026
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002027CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002028 const std::string &Name, BasicBlock *InsertAtEnd) {
2029 // Construct and return the appropriate CastInst subclass
2030 switch (op) {
2031 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2032 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2033 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2034 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2035 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2036 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2037 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2038 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2039 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2040 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2041 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2042 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2043 default:
2044 assert(!"Invalid opcode provided");
2045 }
2046 return 0;
2047}
2048
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002049CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002050 const std::string &Name,
2051 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002052 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002053 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2054 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002055}
2056
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002057CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002058 const std::string &Name,
2059 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002060 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002061 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2062 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002063}
2064
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002065CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002066 const std::string &Name,
2067 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002068 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002069 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2070 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002071}
2072
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002073CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002074 const std::string &Name,
2075 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002076 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002077 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2078 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002079}
2080
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002081CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002082 const std::string &Name,
2083 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002084 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002085 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2086 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002087}
2088
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002089CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002090 const std::string &Name,
2091 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002092 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002093 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2094 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002095}
2096
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002097CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002098 const std::string &Name,
2099 BasicBlock *InsertAtEnd) {
2100 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002101 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002102 "Invalid cast");
2103
Chris Lattner03c49532007-01-15 02:27:26 +00002104 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002105 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2106 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002107}
2108
2109/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002110CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002111 const std::string &Name,
2112 Instruction *InsertBefore) {
2113 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002114 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002115 "Invalid cast");
2116
Chris Lattner03c49532007-01-15 02:27:26 +00002117 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002118 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2119 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002120}
2121
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002122CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002123 bool isSigned, const std::string &Name,
2124 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002125 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002126 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2127 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002128 Instruction::CastOps opcode =
2129 (SrcBits == DstBits ? Instruction::BitCast :
2130 (SrcBits > DstBits ? Instruction::Trunc :
2131 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002132 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002133}
2134
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002135CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002136 bool isSigned, const std::string &Name,
2137 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002138 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2139 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002140 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2141 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002142 Instruction::CastOps opcode =
2143 (SrcBits == DstBits ? Instruction::BitCast :
2144 (SrcBits > DstBits ? Instruction::Trunc :
2145 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002146 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002147}
2148
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002149CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002150 const std::string &Name,
2151 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002152 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002153 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002154 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2155 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002156 Instruction::CastOps opcode =
2157 (SrcBits == DstBits ? Instruction::BitCast :
2158 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002159 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002160}
2161
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002162CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002163 const std::string &Name,
2164 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002165 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002166 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002167 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2168 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002169 Instruction::CastOps opcode =
2170 (SrcBits == DstBits ? Instruction::BitCast :
2171 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002172 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002173}
2174
Duncan Sands55e50902008-01-06 10:12:28 +00002175// Check whether it is valid to call getCastOpcode for these types.
2176// This routine must be kept in sync with getCastOpcode.
2177bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2178 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2179 return false;
2180
2181 if (SrcTy == DestTy)
2182 return true;
2183
2184 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002185 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2186 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002187
2188 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002189 if (DestTy->isInteger()) { // Casting to integral
2190 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002191 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002192 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002193 return true;
2194 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002195 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002196 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002197 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002198 return isa<PointerType>(SrcTy);
2199 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002200 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2201 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002202 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002203 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002204 return true;
2205 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002206 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002207 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002208 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002209 return false;
2210 }
2211 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002212 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002213 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002214 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002215 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002216 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002217 return DestPTy->getBitWidth() == SrcBits;
2218 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002219 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2220 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002221 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002222 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002223 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002224 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002225 return false;
2226 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002227 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002228 return false;
2229 }
2230}
2231
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002232// Provide a way to get a "cast" where the cast opcode is inferred from the
2233// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002234// logic in the castIsValid function below. This axiom should hold:
2235// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2236// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002238// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002239Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002240CastInst::getCastOpcode(
2241 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002242 // Get the bit sizes, we'll need these
2243 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002244 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2245 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002246
Duncan Sands55e50902008-01-06 10:12:28 +00002247 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2248 "Only first class types are castable!");
2249
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002250 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002251 if (DestTy->isInteger()) { // Casting to integral
2252 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002253 if (DestBits < SrcBits)
2254 return Trunc; // int -> smaller int
2255 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002256 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002257 return SExt; // signed -> SEXT
2258 else
2259 return ZExt; // unsigned -> ZEXT
2260 } else {
2261 return BitCast; // Same size, No-op cast
2262 }
2263 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002264 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002265 return FPToSI; // FP -> sint
2266 else
2267 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002268 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002269 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002270 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002271 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002272 return BitCast; // Same size, no-op cast
2273 } else {
2274 assert(isa<PointerType>(SrcTy) &&
2275 "Casting from a value that is not first-class type");
2276 return PtrToInt; // ptr -> int
2277 }
2278 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002279 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002280 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002281 return SIToFP; // sint -> FP
2282 else
2283 return UIToFP; // uint -> FP
2284 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2285 if (DestBits < SrcBits) {
2286 return FPTrunc; // FP -> smaller FP
2287 } else if (DestBits > SrcBits) {
2288 return FPExt; // FP -> larger FP
2289 } else {
2290 return BitCast; // same size, no-op cast
2291 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002292 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002293 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002294 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002295 PTy = NULL;
2296 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002297 } else {
2298 assert(0 && "Casting pointer or non-first class to float");
2299 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002300 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2301 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002302 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002303 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002304 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002305 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002307 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002308 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002309 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002310 }
2311 } else if (isa<PointerType>(DestTy)) {
2312 if (isa<PointerType>(SrcTy)) {
2313 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002314 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002315 return IntToPtr; // int -> ptr
2316 } else {
2317 assert(!"Casting pointer to other than pointer or int");
2318 }
2319 } else {
2320 assert(!"Casting to type that is not first-class");
2321 }
2322
2323 // If we fall through to here we probably hit an assertion cast above
2324 // and assertions are not turned on. Anything we return is an error, so
2325 // BitCast is as good a choice as any.
2326 return BitCast;
2327}
2328
2329//===----------------------------------------------------------------------===//
2330// CastInst SubClass Constructors
2331//===----------------------------------------------------------------------===//
2332
2333/// Check that the construction parameters for a CastInst are correct. This
2334/// could be broken out into the separate constructors but it is useful to have
2335/// it in one place and to eliminate the redundant code for getting the sizes
2336/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002337bool
2338CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002339
2340 // Check for type sanity on the arguments
2341 const Type *SrcTy = S->getType();
2342 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2343 return false;
2344
2345 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002346 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2347 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002348
2349 // Switch on the opcode provided
2350 switch (op) {
2351 default: return false; // This is an input error
2352 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002353 return SrcTy->isIntOrIntVector() &&
2354 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002355 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002356 return SrcTy->isIntOrIntVector() &&
2357 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002358 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002359 return SrcTy->isIntOrIntVector() &&
2360 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002361 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002362 return SrcTy->isFPOrFPVector() &&
2363 DstTy->isFPOrFPVector() &&
2364 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002365 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002366 return SrcTy->isFPOrFPVector() &&
2367 DstTy->isFPOrFPVector() &&
2368 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002369 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002370 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002371 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2372 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002373 return SVTy->getElementType()->isIntOrIntVector() &&
2374 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002375 SVTy->getNumElements() == DVTy->getNumElements();
2376 }
2377 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002378 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002380 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002381 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2382 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002383 return SVTy->getElementType()->isFPOrFPVector() &&
2384 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002385 SVTy->getNumElements() == DVTy->getNumElements();
2386 }
2387 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002388 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002389 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002390 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002392 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393 case Instruction::BitCast:
2394 // BitCast implies a no-op cast of type only. No bits change.
2395 // However, you can't cast pointers to anything but pointers.
2396 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2397 return false;
2398
Duncan Sands55e50902008-01-06 10:12:28 +00002399 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400 // these cases, the cast is okay if the source and destination bit widths
2401 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002402 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002403 }
2404}
2405
2406TruncInst::TruncInst(
2407 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2408) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002409 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002410}
2411
2412TruncInst::TruncInst(
2413 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2414) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002415 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002416}
2417
2418ZExtInst::ZExtInst(
2419 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2420) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002421 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422}
2423
2424ZExtInst::ZExtInst(
2425 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2426) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002427 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428}
2429SExtInst::SExtInst(
2430 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2431) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002432 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002433}
2434
Jeff Cohencc08c832006-12-02 02:22:01 +00002435SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002436 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2437) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002438 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002439}
2440
2441FPTruncInst::FPTruncInst(
2442 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2443) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002444 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002445}
2446
2447FPTruncInst::FPTruncInst(
2448 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2449) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002450 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002451}
2452
2453FPExtInst::FPExtInst(
2454 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2455) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002456 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002457}
2458
2459FPExtInst::FPExtInst(
2460 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2461) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002462 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463}
2464
2465UIToFPInst::UIToFPInst(
2466 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2467) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002468 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469}
2470
2471UIToFPInst::UIToFPInst(
2472 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2473) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002474 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002475}
2476
2477SIToFPInst::SIToFPInst(
2478 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2479) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002480 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002481}
2482
2483SIToFPInst::SIToFPInst(
2484 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2485) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002486 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002487}
2488
2489FPToUIInst::FPToUIInst(
2490 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2491) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002492 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002493}
2494
2495FPToUIInst::FPToUIInst(
2496 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2497) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002498 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002499}
2500
2501FPToSIInst::FPToSIInst(
2502 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2503) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002504 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002505}
2506
2507FPToSIInst::FPToSIInst(
2508 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2509) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002510 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002511}
2512
2513PtrToIntInst::PtrToIntInst(
2514 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2515) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002516 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002517}
2518
2519PtrToIntInst::PtrToIntInst(
2520 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2521) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002522 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002523}
2524
2525IntToPtrInst::IntToPtrInst(
2526 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2527) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002528 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002529}
2530
2531IntToPtrInst::IntToPtrInst(
2532 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2533) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002534 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002535}
2536
2537BitCastInst::BitCastInst(
2538 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2539) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002540 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541}
2542
2543BitCastInst::BitCastInst(
2544 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2545) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002546 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002548
2549//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002550// CmpInst Classes
2551//===----------------------------------------------------------------------===//
2552
Nate Begemand2195702008-05-12 19:01:56 +00002553CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2554 Value *LHS, Value *RHS, const std::string &Name,
2555 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002556 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002557 OperandTraits<CmpInst>::op_begin(this),
2558 OperandTraits<CmpInst>::operands(this),
2559 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002560 Op<0>() = LHS;
2561 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002562 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002563 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002564}
Gabor Greiff6caff662008-05-10 08:32:32 +00002565
Nate Begemand2195702008-05-12 19:01:56 +00002566CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2567 Value *LHS, Value *RHS, const std::string &Name,
2568 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002569 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002570 OperandTraits<CmpInst>::op_begin(this),
2571 OperandTraits<CmpInst>::operands(this),
2572 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002573 Op<0>() = LHS;
2574 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002575 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002576 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002577}
2578
2579CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002580CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002581 const std::string &Name, Instruction *InsertBefore) {
2582 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002583 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002584 InsertBefore);
2585 }
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002586 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2587 InsertBefore);
Reid Spencerd9436b62006-11-20 01:22:35 +00002588}
2589
2590CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002591CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002592 const std::string &Name, BasicBlock *InsertAtEnd) {
2593 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002594 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002595 InsertAtEnd);
2596 }
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002597 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2598 InsertAtEnd);
Reid Spencerd9436b62006-11-20 01:22:35 +00002599}
2600
2601void CmpInst::swapOperands() {
2602 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2603 IC->swapOperands();
2604 else
2605 cast<FCmpInst>(this)->swapOperands();
2606}
2607
2608bool CmpInst::isCommutative() {
2609 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2610 return IC->isCommutative();
2611 return cast<FCmpInst>(this)->isCommutative();
2612}
2613
2614bool CmpInst::isEquality() {
2615 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2616 return IC->isEquality();
2617 return cast<FCmpInst>(this)->isEquality();
2618}
2619
2620
Dan Gohman4e724382008-05-31 02:47:54 +00002621CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002622 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002623 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002624 case ICMP_EQ: return ICMP_NE;
2625 case ICMP_NE: return ICMP_EQ;
2626 case ICMP_UGT: return ICMP_ULE;
2627 case ICMP_ULT: return ICMP_UGE;
2628 case ICMP_UGE: return ICMP_ULT;
2629 case ICMP_ULE: return ICMP_UGT;
2630 case ICMP_SGT: return ICMP_SLE;
2631 case ICMP_SLT: return ICMP_SGE;
2632 case ICMP_SGE: return ICMP_SLT;
2633 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002634
Dan Gohman4e724382008-05-31 02:47:54 +00002635 case FCMP_OEQ: return FCMP_UNE;
2636 case FCMP_ONE: return FCMP_UEQ;
2637 case FCMP_OGT: return FCMP_ULE;
2638 case FCMP_OLT: return FCMP_UGE;
2639 case FCMP_OGE: return FCMP_ULT;
2640 case FCMP_OLE: return FCMP_UGT;
2641 case FCMP_UEQ: return FCMP_ONE;
2642 case FCMP_UNE: return FCMP_OEQ;
2643 case FCMP_UGT: return FCMP_OLE;
2644 case FCMP_ULT: return FCMP_OGE;
2645 case FCMP_UGE: return FCMP_OLT;
2646 case FCMP_ULE: return FCMP_OGT;
2647 case FCMP_ORD: return FCMP_UNO;
2648 case FCMP_UNO: return FCMP_ORD;
2649 case FCMP_TRUE: return FCMP_FALSE;
2650 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002651 }
2652}
2653
Reid Spencer266e42b2006-12-23 06:05:41 +00002654ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2655 switch (pred) {
2656 default: assert(! "Unknown icmp predicate!");
2657 case ICMP_EQ: case ICMP_NE:
2658 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2659 return pred;
2660 case ICMP_UGT: return ICMP_SGT;
2661 case ICMP_ULT: return ICMP_SLT;
2662 case ICMP_UGE: return ICMP_SGE;
2663 case ICMP_ULE: return ICMP_SLE;
2664 }
2665}
2666
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002667ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2668 switch (pred) {
2669 default: assert(! "Unknown icmp predicate!");
2670 case ICMP_EQ: case ICMP_NE:
2671 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2672 return pred;
2673 case ICMP_SGT: return ICMP_UGT;
2674 case ICMP_SLT: return ICMP_ULT;
2675 case ICMP_SGE: return ICMP_UGE;
2676 case ICMP_SLE: return ICMP_ULE;
2677 }
2678}
2679
Reid Spencer266e42b2006-12-23 06:05:41 +00002680bool ICmpInst::isSignedPredicate(Predicate pred) {
2681 switch (pred) {
2682 default: assert(! "Unknown icmp predicate!");
2683 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2684 return true;
2685 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2686 case ICMP_UGE: case ICMP_ULE:
2687 return false;
2688 }
2689}
2690
Reid Spencer0286bc12007-02-28 22:00:54 +00002691/// Initialize a set of values that all satisfy the condition with C.
2692///
2693ConstantRange
2694ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2695 APInt Lower(C);
2696 APInt Upper(C);
2697 uint32_t BitWidth = C.getBitWidth();
2698 switch (pred) {
2699 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2700 case ICmpInst::ICMP_EQ: Upper++; break;
2701 case ICmpInst::ICMP_NE: Lower++; break;
2702 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2703 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2704 case ICmpInst::ICMP_UGT:
2705 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2706 break;
2707 case ICmpInst::ICMP_SGT:
2708 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2709 break;
2710 case ICmpInst::ICMP_ULE:
2711 Lower = APInt::getMinValue(BitWidth); Upper++;
2712 break;
2713 case ICmpInst::ICMP_SLE:
2714 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2715 break;
2716 case ICmpInst::ICMP_UGE:
2717 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2718 break;
2719 case ICmpInst::ICMP_SGE:
2720 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2721 break;
2722 }
2723 return ConstantRange(Lower, Upper);
2724}
2725
Dan Gohman4e724382008-05-31 02:47:54 +00002726CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002727 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002728 default: assert(!"Unknown cmp predicate!");
2729 case ICMP_EQ: case ICMP_NE:
2730 return pred;
2731 case ICMP_SGT: return ICMP_SLT;
2732 case ICMP_SLT: return ICMP_SGT;
2733 case ICMP_SGE: return ICMP_SLE;
2734 case ICMP_SLE: return ICMP_SGE;
2735 case ICMP_UGT: return ICMP_ULT;
2736 case ICMP_ULT: return ICMP_UGT;
2737 case ICMP_UGE: return ICMP_ULE;
2738 case ICMP_ULE: return ICMP_UGE;
2739
Reid Spencerd9436b62006-11-20 01:22:35 +00002740 case FCMP_FALSE: case FCMP_TRUE:
2741 case FCMP_OEQ: case FCMP_ONE:
2742 case FCMP_UEQ: case FCMP_UNE:
2743 case FCMP_ORD: case FCMP_UNO:
2744 return pred;
2745 case FCMP_OGT: return FCMP_OLT;
2746 case FCMP_OLT: return FCMP_OGT;
2747 case FCMP_OGE: return FCMP_OLE;
2748 case FCMP_OLE: return FCMP_OGE;
2749 case FCMP_UGT: return FCMP_ULT;
2750 case FCMP_ULT: return FCMP_UGT;
2751 case FCMP_UGE: return FCMP_ULE;
2752 case FCMP_ULE: return FCMP_UGE;
2753 }
2754}
2755
Reid Spencer266e42b2006-12-23 06:05:41 +00002756bool CmpInst::isUnsigned(unsigned short predicate) {
2757 switch (predicate) {
2758 default: return false;
2759 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2760 case ICmpInst::ICMP_UGE: return true;
2761 }
2762}
2763
2764bool CmpInst::isSigned(unsigned short predicate){
2765 switch (predicate) {
2766 default: return false;
2767 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2768 case ICmpInst::ICMP_SGE: return true;
2769 }
2770}
2771
2772bool CmpInst::isOrdered(unsigned short predicate) {
2773 switch (predicate) {
2774 default: return false;
2775 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2776 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2777 case FCmpInst::FCMP_ORD: return true;
2778 }
2779}
2780
2781bool CmpInst::isUnordered(unsigned short predicate) {
2782 switch (predicate) {
2783 default: return false;
2784 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2785 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2786 case FCmpInst::FCMP_UNO: return true;
2787 }
2788}
2789
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002790//===----------------------------------------------------------------------===//
2791// SwitchInst Implementation
2792//===----------------------------------------------------------------------===//
2793
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002794void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002795 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002796 ReservedSpace = 2+NumCases*2;
2797 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002798 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002799
Gabor Greif2d3024d2008-05-26 21:33:52 +00002800 OperandList[0] = Value;
2801 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002802}
2803
Chris Lattner2195fc42007-02-24 00:55:48 +00002804/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2805/// switch on and a default destination. The number of additional cases can
2806/// be specified here to make memory allocation more efficient. This
2807/// constructor can also autoinsert before another instruction.
2808SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2809 Instruction *InsertBefore)
2810 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2811 init(Value, Default, NumCases);
2812}
2813
2814/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2815/// switch on and a default destination. The number of additional cases can
2816/// be specified here to make memory allocation more efficient. This
2817/// constructor also autoinserts at the end of the specified BasicBlock.
2818SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2819 BasicBlock *InsertAtEnd)
2820 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2821 init(Value, Default, NumCases);
2822}
2823
Misha Brukmanb1c93172005-04-21 23:48:37 +00002824SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002825 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002826 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002827 Use *OL = OperandList, *InOL = SI.OperandList;
2828 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002829 OL[i] = InOL[i];
2830 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002831 }
2832}
2833
Gordon Henriksen14a55692007-12-10 02:14:30 +00002834SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002835 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002836}
2837
2838
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002839/// addCase - Add an entry to the switch instruction...
2840///
Chris Lattner47ac1872005-02-24 05:32:09 +00002841void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002842 unsigned OpNo = NumOperands;
2843 if (OpNo+2 > ReservedSpace)
2844 resizeOperands(0); // Get more space!
2845 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002846 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002847 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002848 OperandList[OpNo] = OnVal;
2849 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002850}
2851
2852/// removeCase - This method removes the specified successor from the switch
2853/// instruction. Note that this cannot be used to remove the default
2854/// destination (successor #0).
2855///
2856void SwitchInst::removeCase(unsigned idx) {
2857 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002858 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2859
2860 unsigned NumOps = getNumOperands();
2861 Use *OL = OperandList;
2862
2863 // Move everything after this operand down.
2864 //
2865 // FIXME: we could just swap with the end of the list, then erase. However,
2866 // client might not expect this to happen. The code as it is thrashes the
2867 // use/def lists, which is kinda lame.
2868 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2869 OL[i-2] = OL[i];
2870 OL[i-2+1] = OL[i+1];
2871 }
2872
2873 // Nuke the last value.
2874 OL[NumOps-2].set(0);
2875 OL[NumOps-2+1].set(0);
2876 NumOperands = NumOps-2;
2877}
2878
2879/// resizeOperands - resize operands - This adjusts the length of the operands
2880/// list according to the following behavior:
2881/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002882/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002883/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2884/// 3. If NumOps == NumOperands, trim the reserved space.
2885///
2886void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002887 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002888 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002889 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002890 } else if (NumOps*2 > NumOperands) {
2891 // No resize needed.
2892 if (ReservedSpace >= NumOps) return;
2893 } else if (NumOps == NumOperands) {
2894 if (ReservedSpace == NumOps) return;
2895 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002896 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002897 }
2898
2899 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002900 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002901 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002902 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002903 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002904 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002905 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002906 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002907}
2908
2909
2910BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2911 return getSuccessor(idx);
2912}
2913unsigned SwitchInst::getNumSuccessorsV() const {
2914 return getNumSuccessors();
2915}
2916void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2917 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002918}
Chris Lattnerf22be932004-10-15 23:52:53 +00002919
Chris Lattnerf22be932004-10-15 23:52:53 +00002920// Define these methods here so vtables don't get emitted into every translation
2921// unit that uses these classes.
2922
2923GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002924 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002925}
2926
2927BinaryOperator *BinaryOperator::clone() const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002928 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002929}
2930
Chris Lattner0b490b02007-08-24 20:48:18 +00002931FCmpInst* FCmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002932 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002933}
2934ICmpInst* ICmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002935 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002936}
2937
Dan Gohman0752bff2008-05-23 00:36:11 +00002938ExtractValueInst *ExtractValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002939 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002940}
2941InsertValueInst *InsertValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002942 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002943}
2944
2945
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002946MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2947AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2948FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2949LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2950StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2951CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2952CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2953CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2954CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2955CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2956CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2957CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2958CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2959CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2960CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2961CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2962CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002963CallInst *CallInst::clone() const {
2964 return new(getNumOperands()) CallInst(*this);
2965}
2966SelectInst *SelectInst::clone() const {
2967 return new(getNumOperands()) SelectInst(*this);
2968}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002969VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2970
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002971ExtractElementInst *ExtractElementInst::clone() const {
2972 return new ExtractElementInst(*this);
2973}
2974InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002975 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002976}
2977ShuffleVectorInst *ShuffleVectorInst::clone() const {
2978 return new ShuffleVectorInst(*this);
2979}
Chris Lattnerf22be932004-10-15 23:52:53 +00002980PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002981ReturnInst *ReturnInst::clone() const {
2982 return new(getNumOperands()) ReturnInst(*this);
2983}
2984BranchInst *BranchInst::clone() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00002985 unsigned Ops(getNumOperands());
2986 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00002987}
Gabor Greiff6caff662008-05-10 08:32:32 +00002988SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002989InvokeInst *InvokeInst::clone() const {
2990 return new(getNumOperands()) InvokeInst(*this);
2991}
Chris Lattnerf22be932004-10-15 23:52:53 +00002992UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002993UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}