blob: b73ce505f221526bf93ac334d0421b28a596681b [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
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Dan Gohman22571482009-09-03 15:34:35 +000022#include "llvm/Analysis/Dominators.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000025#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Devang Pateladd58652009-09-23 18:32:25 +000027
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000028using namespace llvm;
29
Chris Lattner3e13b8c2008-01-02 23:42:30 +000030//===----------------------------------------------------------------------===//
31// CallSite Class
32//===----------------------------------------------------------------------===//
33
Gabor Greif1b9921a2009-01-11 22:33:22 +000034#define CALLSITE_DELEGATE_GETTER(METHOD) \
35 Instruction *II(getInstruction()); \
36 return isCall() \
37 ? cast<CallInst>(II)->METHOD \
38 : cast<InvokeInst>(II)->METHOD
39
40#define CALLSITE_DELEGATE_SETTER(METHOD) \
41 Instruction *II(getInstruction()); \
42 if (isCall()) \
43 cast<CallInst>(II)->METHOD; \
44 else \
45 cast<InvokeInst>(II)->METHOD
46
Duncan Sands85fab3a2008-02-18 17:32:13 +000047CallSite::CallSite(Instruction *C) {
48 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
Gabor Greif1b9921a2009-01-11 22:33:22 +000049 I.setPointer(C);
50 I.setInt(isa<CallInst>(C));
Duncan Sands85fab3a2008-02-18 17:32:13 +000051}
Sandeep Patel68c5f472009-09-02 08:44:58 +000052CallingConv::ID CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000053 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000054}
Sandeep Patel68c5f472009-09-02 08:44:58 +000055void CallSite::setCallingConv(CallingConv::ID CC) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000056 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
Chris Lattnerf7b6d312005-05-06 20:26:43 +000057}
Devang Patel4c758ea2008-09-25 21:00:45 +000058const AttrListPtr &CallSite::getAttributes() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000059 CALLSITE_DELEGATE_GETTER(getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000060}
Devang Patel4c758ea2008-09-25 21:00:45 +000061void CallSite::setAttributes(const AttrListPtr &PAL) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000062 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000063}
Devang Patelba3fa6c2008-09-23 23:03:40 +000064bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000065 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
Duncan Sands5208d1a2007-11-28 17:07:01 +000066}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000067uint16_t CallSite::getParamAlignment(uint16_t i) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000068 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
Dale Johanneseneabc5f32008-02-22 17:49:45 +000069}
Duncan Sands38ef3a82007-12-03 20:06:50 +000070bool CallSite::doesNotAccessMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000071 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000072}
Duncan Sands78c88722008-07-08 08:38:44 +000073void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000074 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000075}
Duncan Sands38ef3a82007-12-03 20:06:50 +000076bool CallSite::onlyReadsMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000077 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000078}
Duncan Sands78c88722008-07-08 08:38:44 +000079void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000080 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000081}
82bool CallSite::doesNotReturn() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000083 CALLSITE_DELEGATE_GETTER(doesNotReturn());
Duncan Sands78c88722008-07-08 08:38:44 +000084}
85void CallSite::setDoesNotReturn(bool doesNotReturn) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000086 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
Duncan Sands78c88722008-07-08 08:38:44 +000087}
Duncan Sands3353ed02007-12-18 09:59:50 +000088bool CallSite::doesNotThrow() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000089 CALLSITE_DELEGATE_GETTER(doesNotThrow());
Duncan Sands8e4847e2007-12-16 15:51:49 +000090}
Duncan Sandsaa31b922007-12-19 21:13:37 +000091void CallSite::setDoesNotThrow(bool doesNotThrow) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000092 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
Duncan Sandsaa31b922007-12-19 21:13:37 +000093}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000094
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000095bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000096 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
97 if (AI->get() == Arg)
98 return true;
99 return false;
100}
101
Gabor Greif1b9921a2009-01-11 22:33:22 +0000102#undef CALLSITE_DELEGATE_GETTER
103#undef CALLSITE_DELEGATE_SETTER
104
Gordon Henriksen14a55692007-12-10 02:14:30 +0000105//===----------------------------------------------------------------------===//
106// TerminatorInst Class
107//===----------------------------------------------------------------------===//
108
109// Out of line virtual method, so the vtable, etc has a home.
110TerminatorInst::~TerminatorInst() {
111}
112
Gabor Greiff6caff662008-05-10 08:32:32 +0000113//===----------------------------------------------------------------------===//
114// UnaryInstruction Class
115//===----------------------------------------------------------------------===//
116
Gordon Henriksen14a55692007-12-10 02:14:30 +0000117// Out of line virtual method, so the vtable, etc has a home.
118UnaryInstruction::~UnaryInstruction() {
119}
120
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000121//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000122// SelectInst Class
123//===----------------------------------------------------------------------===//
124
125/// areInvalidOperands - Return a string if the specified operands are invalid
126/// for a select operation, otherwise return null.
127const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
128 if (Op1->getType() != Op2->getType())
129 return "both values to select must have same type";
130
131 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
132 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000133 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000134 return "vector select condition element type must be i1";
135 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
136 if (ET == 0)
137 return "selected values for vector select must be vectors";
138 if (ET->getNumElements() != VT->getNumElements())
139 return "vector select requires selected vectors to have "
140 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000141 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000142 return "select condition must be i1 or <n x i1>";
143 }
144 return 0;
145}
146
147
148//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149// PHINode Class
150//===----------------------------------------------------------------------===//
151
152PHINode::PHINode(const PHINode &PN)
153 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000154 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000155 ReservedSpace(PN.getNumOperands()) {
156 Use *OL = OperandList;
157 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000158 OL[i] = PN.getOperand(i);
159 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000160 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000161 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000162}
163
Gordon Henriksen14a55692007-12-10 02:14:30 +0000164PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000165 if (OperandList)
166 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000167}
168
169// removeIncomingValue - Remove an incoming value. This is useful if a
170// predecessor basic block is deleted.
171Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
172 unsigned NumOps = getNumOperands();
173 Use *OL = OperandList;
174 assert(Idx*2 < NumOps && "BB not in PHI node!");
175 Value *Removed = OL[Idx*2];
176
177 // Move everything after this operand down.
178 //
179 // FIXME: we could just swap with the end of the list, then erase. However,
180 // client might not expect this to happen. The code as it is thrashes the
181 // use/def lists, which is kinda lame.
182 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
183 OL[i-2] = OL[i];
184 OL[i-2+1] = OL[i+1];
185 }
186
187 // Nuke the last value.
188 OL[NumOps-2].set(0);
189 OL[NumOps-2+1].set(0);
190 NumOperands = NumOps-2;
191
192 // If the PHI node is dead, because it has zero entries, nuke it now.
193 if (NumOps == 2 && DeletePHIIfEmpty) {
194 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000195 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000196 eraseFromParent();
197 }
198 return Removed;
199}
200
201/// resizeOperands - resize operands - This adjusts the length of the operands
202/// list according to the following behavior:
203/// 1. If NumOps == 0, grow the operand list in response to a push_back style
204/// of operation. This grows the number of ops by 1.5 times.
205/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
206/// 3. If NumOps == NumOperands, trim the reserved space.
207///
208void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000209 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000210 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000211 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000212 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
213 } else if (NumOps*2 > NumOperands) {
214 // No resize needed.
215 if (ReservedSpace >= NumOps) return;
216 } else if (NumOps == NumOperands) {
217 if (ReservedSpace == NumOps) return;
218 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000219 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000220 }
221
222 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000223 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000224 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000225 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000226 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000227 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000228}
229
Nate Begemanb3923212005-08-04 23:24:19 +0000230/// hasConstantValue - If the specified PHI node always merges together the same
231/// value, return the value, otherwise return null.
232///
Dan Gohman22571482009-09-03 15:34:35 +0000233/// If the PHI has undef operands, but all the rest of the operands are
234/// some unique value, return that value if it can be proved that the
235/// value dominates the PHI. If DT is null, use a conservative check,
236/// otherwise use DT to test for dominance.
237///
238Value *PHINode::hasConstantValue(DominatorTree *DT) const {
Chris Lattner7d08da62009-09-21 22:27:34 +0000239 // If the PHI node only has one incoming value, eliminate the PHI node.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000240 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000241 if (getIncomingValue(0) != this) // not X = phi X
242 return getIncomingValue(0);
Chris Lattner7d08da62009-09-21 22:27:34 +0000243 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000244 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000245
Nate Begemanb3923212005-08-04 23:24:19 +0000246 // Otherwise if all of the incoming values are the same for the PHI, replace
247 // the PHI node with the incoming value.
248 //
249 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000250 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000251 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000252 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000253 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000254 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000255 if (InVal && getIncomingValue(i) != InVal)
256 return 0; // Not the same, bail out.
Chris Lattner7d08da62009-09-21 22:27:34 +0000257 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000258 }
Nate Begemanb3923212005-08-04 23:24:19 +0000259
260 // The only case that could cause InVal to be null is if we have a PHI node
261 // that only has entries for itself. In this case, there is no entry into the
262 // loop, so kill the PHI.
263 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000264 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000265
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000266 // If we have a PHI node like phi(X, undef, X), where X is defined by some
267 // instruction, we cannot always return X as the result of the PHI node. Only
268 // do this if X is not an instruction (thus it must dominate the PHI block),
269 // or if the client is prepared to deal with this possibility.
Chris Lattner7d08da62009-09-21 22:27:34 +0000270 if (!HasUndefInput || !isa<Instruction>(InVal))
271 return InVal;
272
273 Instruction *IV = cast<Instruction>(InVal);
274 if (DT) {
275 // We have a DominatorTree. Do a precise test.
276 if (!DT->dominates(IV, this))
277 return 0;
278 } else {
279 // If it is in the entry block, it obviously dominates everything.
280 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
281 isa<InvokeInst>(IV))
282 return 0; // Cannot guarantee that InVal dominates this PHINode.
283 }
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000284
Nate Begemanb3923212005-08-04 23:24:19 +0000285 // All of the incoming values are the same, return the value now.
286 return InVal;
287}
288
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000289
290//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000291// CallInst Implementation
292//===----------------------------------------------------------------------===//
293
Gordon Henriksen14a55692007-12-10 02:14:30 +0000294CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000295}
296
Chris Lattner054ba2c2007-02-13 00:58:44 +0000297void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000298 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
299 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000300 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000301
Misha Brukmanb1c93172005-04-21 23:48:37 +0000302 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000304 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000305
Chris Lattner054ba2c2007-02-13 00:58:44 +0000306 assert((NumParams == FTy->getNumParams() ||
307 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000308 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000309 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000310 assert((i >= FTy->getNumParams() ||
311 FTy->getParamType(i) == Params[i]->getType()) &&
312 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000313 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000314 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315}
316
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000317void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000318 assert(NumOperands == 3 && "NumOperands not set up?");
319 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000320 OL[0] = Func;
321 OL[1] = Actual1;
322 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000323
324 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000325 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000326 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000328 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000329 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000331 assert((0 >= FTy->getNumParams() ||
332 FTy->getParamType(0) == Actual1->getType()) &&
333 "Calling a function with a bad signature!");
334 assert((1 >= FTy->getNumParams() ||
335 FTy->getParamType(1) == Actual2->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, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000340 assert(NumOperands == 2 && "NumOperands not set up?");
341 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000342 OL[0] = Func;
343 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000344
345 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000346 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000347 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000349 assert((FTy->getNumParams() == 1 ||
350 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000351 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000352 assert((0 == FTy->getNumParams() ||
353 FTy->getParamType(0) == Actual->getType()) &&
354 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000355}
356
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000357void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000358 assert(NumOperands == 1 && "NumOperands not set up?");
359 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000360 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000361
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000362 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000363 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000364 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000365
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000366 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000367}
368
Daniel Dunbar4975db62009-07-25 04:41:11 +0000369CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000370 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
372 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000373 Instruction::Call,
374 OperandTraits<CallInst>::op_end(this) - 2,
375 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000376 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000377 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000378}
379
Daniel Dunbar4975db62009-07-25 04:41:11 +0000380CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000381 BasicBlock *InsertAtEnd)
382 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
383 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000384 Instruction::Call,
385 OperandTraits<CallInst>::op_end(this) - 2,
386 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000387 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000388 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000389}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000390CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000391 Instruction *InsertBefore)
392 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
393 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000394 Instruction::Call,
395 OperandTraits<CallInst>::op_end(this) - 1,
396 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000397 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000398 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000399}
400
Daniel Dunbar4975db62009-07-25 04:41:11 +0000401CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000402 BasicBlock *InsertAtEnd)
403 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
404 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000405 Instruction::Call,
406 OperandTraits<CallInst>::op_end(this) - 1,
407 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000408 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000409 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000410}
411
Misha Brukmanb1c93172005-04-21 23:48:37 +0000412CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000413 : Instruction(CI.getType(), Instruction::Call,
414 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000415 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000416 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000417 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000418 Use *OL = OperandList;
419 Use *InOL = CI.OperandList;
420 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000421 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000422 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000423}
424
Devang Patel4c758ea2008-09-25 21:00:45 +0000425void CallInst::addAttribute(unsigned i, Attributes attr) {
426 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000427 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000428 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000429}
430
Devang Patel4c758ea2008-09-25 21:00:45 +0000431void CallInst::removeAttribute(unsigned i, Attributes attr) {
432 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000433 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000434 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000435}
436
Devang Patelba3fa6c2008-09-23 23:03:40 +0000437bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000438 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000439 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000440 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000441 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000442 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000443}
444
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000445/// IsConstantOne - Return true only if val is constant int 1
446static bool IsConstantOne(Value *val) {
447 assert(val && "IsConstantOne does not work with NULL val");
448 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
449}
450
451static Value *checkArraySize(Value *Amt, const Type *IntPtrTy) {
452 if (!Amt)
453 Amt = ConstantInt::get(IntPtrTy, 1);
454 else {
455 assert(!isa<BasicBlock>(Amt) &&
456 "Passed basic block into malloc size parameter! Use other ctor");
457 assert(Amt->getType() == IntPtrTy &&
458 "Malloc array size is not an intptr!");
459 }
460 return Amt;
461}
462
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000463static Instruction *createMalloc(Instruction *InsertBefore,
464 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
465 const Type *AllocTy, Value *ArraySize,
466 Function *MallocF, const Twine &NameStr) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000467 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000468 "createMalloc needs either InsertBefore or InsertAtEnd");
469
470 // malloc(type) becomes:
471 // bitcast (i8* malloc(typeSize)) to type*
472 // malloc(type, arraySize) becomes:
473 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
474 Value *AllocSize = ConstantExpr::getSizeOf(AllocTy);
475 AllocSize = ConstantExpr::getTruncOrBitCast(cast<Constant>(AllocSize),
476 IntPtrTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000477 ArraySize = checkArraySize(ArraySize, IntPtrTy);
478
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000479 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000480 if (IsConstantOne(AllocSize)) {
481 AllocSize = ArraySize; // Operand * 1 = Operand
482 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
483 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
484 false /*ZExt*/);
485 // Malloc arg is constant product of type size and array size
486 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
487 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000488 // Multiply type size by the array size...
489 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000490 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
491 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000492 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000493 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
494 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000495 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000496 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000497
Victor Hernandez788eaab2009-09-18 19:20:02 +0000498 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000499 // Create the call to Malloc.
500 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
501 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000502 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000503 if (!MallocF)
504 // prototype malloc as "void *malloc(size_t)"
505 MallocF = cast<Function>(M->getOrInsertFunction("malloc", BPTy,
506 IntPtrTy, NULL));
507 if (!MallocF->doesNotAlias(0)) MallocF->setDoesNotAlias(0);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000508 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000509 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000510 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000511 if (InsertBefore) {
512 MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000513 Result = MCall;
514 if (Result->getType() != AllocPtrType)
515 // Create a cast instruction to convert to the right type...
516 Result = new BitCastInst(MCall, AllocPtrType, NameStr, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000517 } else {
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000518 MCall = CallInst::Create(MallocF, AllocSize, "malloccall");
519 Result = MCall;
520 if (Result->getType() != AllocPtrType) {
521 InsertAtEnd->getInstList().push_back(MCall);
522 // Create a cast instruction to convert to the right type...
523 Result = new BitCastInst(MCall, AllocPtrType, NameStr);
524 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000525 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000526 MCall->setTailCall();
Daniel Dunbarb9189002009-09-11 22:07:31 +0000527 assert(MCall->getType() != Type::getVoidTy(BB->getContext()) &&
528 "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000529
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000530 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000531}
532
533/// CreateMalloc - Generate the IR for a call to malloc:
534/// 1. Compute the malloc call's argument as the specified type's size,
535/// possibly multiplied by the array size if the array size is not
536/// constant 1.
537/// 2. Call malloc with that argument.
538/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000539Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
540 const Type *IntPtrTy, const Type *AllocTy,
541 Value *ArraySize, const Twine &Name) {
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000542 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy,
543 ArraySize, NULL, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000544}
545
546/// CreateMalloc - Generate the IR for a call to malloc:
547/// 1. Compute the malloc call's argument as the specified type's size,
548/// possibly multiplied by the array size if the array size is not
549/// constant 1.
550/// 2. Call malloc with that argument.
551/// 3. Bitcast the result of the malloc call to the specified type.
552/// Note: This function does not add the bitcast to the basic block, that is the
553/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000554Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
555 const Type *IntPtrTy, const Type *AllocTy,
556 Value *ArraySize, Function* MallocF,
557 const Twine &Name) {
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000558 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy,
559 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000560}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000561
Victor Hernandeze2971492009-10-24 04:23:03 +0000562static Instruction* createFree(Value* Source, Instruction *InsertBefore,
563 BasicBlock *InsertAtEnd) {
564 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
565 "createFree needs either InsertBefore or InsertAtEnd");
566 assert(isa<PointerType>(Source->getType()) &&
567 "Can not free something of nonpointer type!");
568
569 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
570 Module* M = BB->getParent()->getParent();
571
572 const Type *VoidTy = Type::getVoidTy(M->getContext());
573 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
574 // prototype free as "void free(void*)"
575 Constant *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
576
577 CallInst* Result = NULL;
578 Value *PtrCast = Source;
579 if (InsertBefore) {
580 if (Source->getType() != IntPtrTy)
581 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
582 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
583 } else {
584 if (Source->getType() != IntPtrTy)
585 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
586 Result = CallInst::Create(FreeFunc, PtrCast, "");
587 }
588 Result->setTailCall();
589
590 return Result;
591}
592
593/// CreateFree - Generate the IR for a call to the builtin free function.
594void CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
595 createFree(Source, InsertBefore, NULL);
596}
597
598/// CreateFree - Generate the IR for a call to the builtin free function.
599/// Note: This function does not add the call to the basic block, that is the
600/// responsibility of the caller.
601Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
602 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
603 assert(FreeCall && "CreateFree did not create a CallInst");
604 return FreeCall;
605}
606
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000607//===----------------------------------------------------------------------===//
608// InvokeInst Implementation
609//===----------------------------------------------------------------------===//
610
611void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000612 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000613 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000614 Use *OL = OperandList;
615 OL[0] = Fn;
616 OL[1] = IfNormal;
617 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000618 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000619 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000620 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000621
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000622 assert(((NumArgs == FTy->getNumParams()) ||
623 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000624 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000625
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000626 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000627 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000628 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000629 "Invoking a function with a bad signature!");
630
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000631 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000632 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000633}
634
Misha Brukmanb1c93172005-04-21 23:48:37 +0000635InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000636 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000637 OperandTraits<InvokeInst>::op_end(this)
638 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000639 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000640 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000641 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000642 Use *OL = OperandList, *InOL = II.OperandList;
643 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000644 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000645 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000646}
647
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
649 return getSuccessor(idx);
650}
651unsigned InvokeInst::getNumSuccessorsV() const {
652 return getNumSuccessors();
653}
654void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
655 return setSuccessor(idx, B);
656}
657
Devang Patelba3fa6c2008-09-23 23:03:40 +0000658bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000659 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000660 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000661 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000662 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000663 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000664}
665
Devang Patel4c758ea2008-09-25 21:00:45 +0000666void InvokeInst::addAttribute(unsigned i, Attributes attr) {
667 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000668 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000669 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000670}
671
Devang Patel4c758ea2008-09-25 21:00:45 +0000672void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
673 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000674 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000675 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000676}
677
Duncan Sands5208d1a2007-11-28 17:07:01 +0000678
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000679//===----------------------------------------------------------------------===//
680// ReturnInst Implementation
681//===----------------------------------------------------------------------===//
682
Chris Lattner2195fc42007-02-24 00:55:48 +0000683ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000684 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000685 OperandTraits<ReturnInst>::op_end(this) -
686 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000687 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000688 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000689 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000690 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000691}
692
Owen Anderson55f1c092009-08-13 21:58:54 +0000693ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
694 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000695 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
696 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000697 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000698 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000699}
Owen Anderson55f1c092009-08-13 21:58:54 +0000700ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
701 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000702 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
703 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000704 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000705 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000706}
Owen Anderson55f1c092009-08-13 21:58:54 +0000707ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
708 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000709 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000710}
711
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000712unsigned ReturnInst::getNumSuccessorsV() const {
713 return getNumSuccessors();
714}
715
Devang Patelae682fb2008-02-26 17:56:20 +0000716/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
717/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000718void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000719 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000720}
721
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000722BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000723 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000724 return 0;
725}
726
Devang Patel59643e52008-02-23 00:35:18 +0000727ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000728}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000729
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000730//===----------------------------------------------------------------------===//
731// UnwindInst Implementation
732//===----------------------------------------------------------------------===//
733
Owen Anderson55f1c092009-08-13 21:58:54 +0000734UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
735 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
736 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000737}
Owen Anderson55f1c092009-08-13 21:58:54 +0000738UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
739 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
740 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000741}
742
743
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000744unsigned UnwindInst::getNumSuccessorsV() const {
745 return getNumSuccessors();
746}
747
748void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000749 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000750}
751
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000752BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000753 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000754 return 0;
755}
756
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000757//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000758// UnreachableInst Implementation
759//===----------------------------------------------------------------------===//
760
Owen Anderson55f1c092009-08-13 21:58:54 +0000761UnreachableInst::UnreachableInst(LLVMContext &Context,
762 Instruction *InsertBefore)
763 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
764 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000765}
Owen Anderson55f1c092009-08-13 21:58:54 +0000766UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
767 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
768 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000769}
770
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000771unsigned UnreachableInst::getNumSuccessorsV() const {
772 return getNumSuccessors();
773}
774
775void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000776 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000777}
778
779BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000780 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000781 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000782}
783
784//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000785// BranchInst Implementation
786//===----------------------------------------------------------------------===//
787
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000788void BranchInst::AssertOK() {
789 if (isConditional())
Owen Anderson55f1c092009-08-13 21:58:54 +0000790 assert(getCondition()->getType() == Type::getInt1Ty(getContext()) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000791 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000792}
793
Chris Lattner2195fc42007-02-24 00:55:48 +0000794BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000795 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000796 OperandTraits<BranchInst>::op_end(this) - 1,
797 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000798 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000799 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000800}
801BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
802 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000803 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000804 OperandTraits<BranchInst>::op_end(this) - 3,
805 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000806 Op<-1>() = IfTrue;
807 Op<-2>() = IfFalse;
808 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000809#ifndef NDEBUG
810 AssertOK();
811#endif
812}
813
814BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000815 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000816 OperandTraits<BranchInst>::op_end(this) - 1,
817 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000818 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000819 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000820}
821
822BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
823 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000824 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000825 OperandTraits<BranchInst>::op_end(this) - 3,
826 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000827 Op<-1>() = IfTrue;
828 Op<-2>() = IfFalse;
829 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000830#ifndef NDEBUG
831 AssertOK();
832#endif
833}
834
835
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000836BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000837 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000838 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
839 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000840 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000841 if (BI.getNumOperands() != 1) {
842 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000843 Op<-3>() = BI.Op<-3>();
844 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000845 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000846 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000847}
848
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000849
850Use* Use::getPrefix() {
851 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
852 if (PotentialPrefix.getOpaqueValue())
853 return 0;
854
855 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
856}
857
858BranchInst::~BranchInst() {
859 if (NumOperands == 1) {
860 if (Use *Prefix = OperandList->getPrefix()) {
861 Op<-1>() = 0;
862 //
863 // mark OperandList to have a special value for scrutiny
864 // by baseclass destructors and operator delete
865 OperandList = Prefix;
866 } else {
867 NumOperands = 3;
868 OperandList = op_begin();
869 }
870 }
871}
872
873
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000874BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
875 return getSuccessor(idx);
876}
877unsigned BranchInst::getNumSuccessorsV() const {
878 return getNumSuccessors();
879}
880void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
881 setSuccessor(idx, B);
882}
883
884
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000885//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000886// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000887//===----------------------------------------------------------------------===//
888
Owen Andersonb6b25302009-07-14 23:09:55 +0000889static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000890 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000891 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000892 else {
893 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000894 "Passed basic block into allocation size parameter! Use other ctor");
Owen Anderson55f1c092009-08-13 21:58:54 +0000895 assert(Amt->getType() == Type::getInt32Ty(Context) &&
Victor Hernandeza3aaf852009-10-17 01:18:07 +0000896 "Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000897 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000898 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000899}
900
Victor Hernandez8acf2952009-10-23 21:09:37 +0000901AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
902 const Twine &Name, Instruction *InsertBefore)
903 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
904 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
905 setAlignment(0);
906 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
907 setName(Name);
908}
909
910AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
911 const Twine &Name, BasicBlock *InsertAtEnd)
912 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
913 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
914 setAlignment(0);
915 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
916 setName(Name);
917}
918
919AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
920 Instruction *InsertBefore)
921 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
922 getAISize(Ty->getContext(), 0), InsertBefore) {
923 setAlignment(0);
924 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
925 setName(Name);
926}
927
928AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
929 BasicBlock *InsertAtEnd)
930 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
931 getAISize(Ty->getContext(), 0), InsertAtEnd) {
932 setAlignment(0);
933 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
934 setName(Name);
935}
936
937AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
938 const Twine &Name, Instruction *InsertBefore)
939 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000940 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000941 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000942 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000943 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000944}
945
Victor Hernandez8acf2952009-10-23 21:09:37 +0000946AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
947 const Twine &Name, BasicBlock *InsertAtEnd)
948 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000949 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000950 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000951 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000952 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000953}
954
Gordon Henriksen14a55692007-12-10 02:14:30 +0000955// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000956AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000957}
958
Victor Hernandez8acf2952009-10-23 21:09:37 +0000959void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000960 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
961 SubclassData = Log2_32(Align) + 1;
962 assert(getAlignment() == Align && "Alignment representation error!");
963}
964
Victor Hernandez8acf2952009-10-23 21:09:37 +0000965bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000966 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
967 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000968 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000969}
970
Victor Hernandez8acf2952009-10-23 21:09:37 +0000971const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000972 return getType()->getElementType();
973}
974
Chris Lattner8b291e62008-11-26 02:54:17 +0000975/// isStaticAlloca - Return true if this alloca is in the entry block of the
976/// function and is a constant size. If so, the code generator will fold it
977/// into the prolog/epilog code, so it is basically free.
978bool AllocaInst::isStaticAlloca() const {
979 // Must be constant size.
980 if (!isa<ConstantInt>(getArraySize())) return false;
981
982 // Must be in the entry block.
983 const BasicBlock *Parent = getParent();
984 return Parent == &Parent->getParent()->front();
985}
986
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000987//===----------------------------------------------------------------------===//
988// FreeInst Implementation
989//===----------------------------------------------------------------------===//
990
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000991void FreeInst::AssertOK() {
992 assert(isa<PointerType>(getOperand(0)->getType()) &&
993 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000994}
995
996FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000997 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
998 Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000999 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001000}
1001
1002FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001003 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
1004 Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001005 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001006}
1007
1008
1009//===----------------------------------------------------------------------===//
1010// LoadInst Implementation
1011//===----------------------------------------------------------------------===//
1012
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001013void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001014 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001015 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001016}
1017
Daniel Dunbar4975db62009-07-25 04:41:11 +00001018LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001019 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001020 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001021 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001022 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001023 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001024 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001025}
1026
Daniel Dunbar4975db62009-07-25 04:41:11 +00001027LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001028 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001029 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001030 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001031 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001032 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001033 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001034}
1035
Daniel Dunbar4975db62009-07-25 04:41:11 +00001036LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001037 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001038 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001039 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001040 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001041 setAlignment(0);
1042 AssertOK();
1043 setName(Name);
1044}
1045
Daniel Dunbar4975db62009-07-25 04:41:11 +00001046LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001047 unsigned Align, Instruction *InsertBef)
1048 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1049 Load, Ptr, InsertBef) {
1050 setVolatile(isVolatile);
1051 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001052 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001053 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001054}
1055
Daniel Dunbar4975db62009-07-25 04:41:11 +00001056LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001057 unsigned Align, BasicBlock *InsertAE)
1058 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1059 Load, Ptr, InsertAE) {
1060 setVolatile(isVolatile);
1061 setAlignment(Align);
1062 AssertOK();
1063 setName(Name);
1064}
1065
Daniel Dunbar4975db62009-07-25 04:41:11 +00001066LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001067 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001068 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001069 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001070 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001071 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +00001072 AssertOK();
1073 setName(Name);
1074}
1075
Daniel Dunbar27096822009-08-11 18:11:15 +00001076
1077
1078LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1079 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1080 Load, Ptr, InsertBef) {
1081 setVolatile(false);
1082 setAlignment(0);
1083 AssertOK();
1084 if (Name && Name[0]) setName(Name);
1085}
1086
1087LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1088 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1089 Load, Ptr, InsertAE) {
1090 setVolatile(false);
1091 setAlignment(0);
1092 AssertOK();
1093 if (Name && Name[0]) setName(Name);
1094}
1095
1096LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1097 Instruction *InsertBef)
1098: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1099 Load, Ptr, InsertBef) {
1100 setVolatile(isVolatile);
1101 setAlignment(0);
1102 AssertOK();
1103 if (Name && Name[0]) setName(Name);
1104}
1105
1106LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1107 BasicBlock *InsertAE)
1108 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1109 Load, Ptr, InsertAE) {
1110 setVolatile(isVolatile);
1111 setAlignment(0);
1112 AssertOK();
1113 if (Name && Name[0]) setName(Name);
1114}
1115
Christopher Lamb84485702007-04-22 19:24:39 +00001116void LoadInst::setAlignment(unsigned Align) {
1117 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1118 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1119}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001120
1121//===----------------------------------------------------------------------===//
1122// StoreInst Implementation
1123//===----------------------------------------------------------------------===//
1124
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001125void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001126 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001127 assert(isa<PointerType>(getOperand(1)->getType()) &&
1128 "Ptr must have pointer type!");
1129 assert(getOperand(0)->getType() ==
1130 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001131 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001132}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001133
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001134
1135StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001136 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001137 OperandTraits<StoreInst>::op_begin(this),
1138 OperandTraits<StoreInst>::operands(this),
1139 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001140 Op<0>() = val;
1141 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001142 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001143 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001144 AssertOK();
1145}
1146
1147StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001148 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001149 OperandTraits<StoreInst>::op_begin(this),
1150 OperandTraits<StoreInst>::operands(this),
1151 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001152 Op<0>() = val;
1153 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001154 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001155 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001156 AssertOK();
1157}
1158
Misha Brukmanb1c93172005-04-21 23:48:37 +00001159StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001160 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001161 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001162 OperandTraits<StoreInst>::op_begin(this),
1163 OperandTraits<StoreInst>::operands(this),
1164 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001165 Op<0>() = val;
1166 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001167 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001168 setAlignment(0);
1169 AssertOK();
1170}
1171
1172StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1173 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001174 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001175 OperandTraits<StoreInst>::op_begin(this),
1176 OperandTraits<StoreInst>::operands(this),
1177 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001178 Op<0>() = val;
1179 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001180 setVolatile(isVolatile);
1181 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001182 AssertOK();
1183}
1184
Misha Brukmanb1c93172005-04-21 23:48:37 +00001185StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001186 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001187 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001188 OperandTraits<StoreInst>::op_begin(this),
1189 OperandTraits<StoreInst>::operands(this),
1190 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001191 Op<0>() = val;
1192 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001193 setVolatile(isVolatile);
1194 setAlignment(Align);
1195 AssertOK();
1196}
1197
1198StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001199 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001200 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001201 OperandTraits<StoreInst>::op_begin(this),
1202 OperandTraits<StoreInst>::operands(this),
1203 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001204 Op<0>() = val;
1205 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001206 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001207 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001208 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001209}
1210
Christopher Lamb84485702007-04-22 19:24:39 +00001211void StoreInst::setAlignment(unsigned Align) {
1212 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1213 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1214}
1215
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001216//===----------------------------------------------------------------------===//
1217// GetElementPtrInst Implementation
1218//===----------------------------------------------------------------------===//
1219
Christopher Lambedf07882007-12-17 01:12:55 +00001220static unsigned retrieveAddrSpace(const Value *Val) {
1221 return cast<PointerType>(Val->getType())->getAddressSpace();
1222}
1223
Gabor Greif21ba1842008-06-06 20:28:12 +00001224void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001225 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001226 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1227 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001228 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001229
Chris Lattner79807c3d2007-01-31 19:47:18 +00001230 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001231 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001232
1233 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001234}
1235
Daniel Dunbar4975db62009-07-25 04:41:11 +00001236void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001237 assert(NumOperands == 2 && "NumOperands not initialized?");
1238 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001239 OL[0] = Ptr;
1240 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001241
1242 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001243}
1244
Gabor Greiff6caff662008-05-10 08:32:32 +00001245GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001246 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001247 OperandTraits<GetElementPtrInst>::op_end(this)
1248 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001249 GEPI.getNumOperands()) {
1250 Use *OL = OperandList;
1251 Use *GEPIOL = GEPI.OperandList;
1252 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001253 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001254 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001255}
1256
Chris Lattner82981202005-05-03 05:43:30 +00001257GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001258 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001259 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001260 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001261 GetElementPtr,
1262 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1263 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001264 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001265}
1266
1267GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001268 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001269 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001270 checkType(getIndexedType(Ptr->getType(),Idx)),
1271 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001272 GetElementPtr,
1273 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1274 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001275 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001276}
1277
Chris Lattner6090a422009-03-09 04:46:40 +00001278/// getIndexedType - Returns the type of the element that would be accessed with
1279/// a gep instruction with the specified parameters.
1280///
1281/// The Idxs pointer should point to a continuous piece of memory containing the
1282/// indices, either as Value* or uint64_t.
1283///
1284/// A null type is returned if the indices are invalid for the specified
1285/// pointer type.
1286///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001287template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001288static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1289 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001290 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1291 if (!PTy) return 0; // Type isn't a pointer type!
1292 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001293
Chris Lattner6090a422009-03-09 04:46:40 +00001294 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001295 if (NumIdx == 0)
1296 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001297
1298 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001299 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1300 // that contain opaque types) under the assumption that it will be resolved to
1301 // a sane type later.
1302 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001303 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001304
Dan Gohman1ecaf452008-05-31 00:58:22 +00001305 unsigned CurIdx = 1;
1306 for (; CurIdx != NumIdx; ++CurIdx) {
1307 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1308 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001309 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001310 if (!CT->indexValid(Index)) return 0;
1311 Agg = CT->getTypeAtIndex(Index);
1312
1313 // If the new type forwards to another type, then it is in the middle
1314 // of being refined to another type (and hence, may have dropped all
1315 // references to what it was using before). So, use the new forwarded
1316 // type.
1317 if (const Type *Ty = Agg->getForwardedType())
1318 Agg = Ty;
1319 }
1320 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001321}
1322
Matthijs Kooijman04468622008-07-29 08:46:11 +00001323const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1324 Value* const *Idxs,
1325 unsigned NumIdx) {
1326 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1327}
1328
1329const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1330 uint64_t const *Idxs,
1331 unsigned NumIdx) {
1332 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1333}
1334
Chris Lattner82981202005-05-03 05:43:30 +00001335const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1336 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1337 if (!PTy) return 0; // Type isn't a pointer type!
1338
1339 // Check the pointer index.
1340 if (!PTy->indexValid(Idx)) return 0;
1341
Chris Lattnerc2233332005-05-03 16:44:45 +00001342 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001343}
1344
Chris Lattner45f15572007-04-14 00:12:57 +00001345
1346/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1347/// zeros. If so, the result pointer and the first operand have the same
1348/// value, just potentially different types.
1349bool GetElementPtrInst::hasAllZeroIndices() const {
1350 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1351 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1352 if (!CI->isZero()) return false;
1353 } else {
1354 return false;
1355 }
1356 }
1357 return true;
1358}
1359
Chris Lattner27058292007-04-27 20:35:56 +00001360/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1361/// constant integers. If so, the result pointer and the first operand have
1362/// a constant offset between them.
1363bool GetElementPtrInst::hasAllConstantIndices() const {
1364 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1365 if (!isa<ConstantInt>(getOperand(i)))
1366 return false;
1367 }
1368 return true;
1369}
1370
Dan Gohman1b849082009-09-07 23:54:19 +00001371void GetElementPtrInst::setIsInBounds(bool B) {
1372 cast<GEPOperator>(this)->setIsInBounds(B);
1373}
Chris Lattner45f15572007-04-14 00:12:57 +00001374
Nick Lewycky28a5f252009-09-27 21:33:04 +00001375bool GetElementPtrInst::isInBounds() const {
1376 return cast<GEPOperator>(this)->isInBounds();
1377}
1378
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001379//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001380// ExtractElementInst Implementation
1381//===----------------------------------------------------------------------===//
1382
1383ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001384 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001385 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001386 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001387 ExtractElement,
1388 OperandTraits<ExtractElementInst>::op_begin(this),
1389 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001390 assert(isValidOperands(Val, Index) &&
1391 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001392 Op<0>() = Val;
1393 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001394 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001395}
1396
1397ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001398 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001399 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001400 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001401 ExtractElement,
1402 OperandTraits<ExtractElementInst>::op_begin(this),
1403 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001404 assert(isValidOperands(Val, Index) &&
1405 "Invalid extractelement instruction operands!");
1406
Gabor Greif2d3024d2008-05-26 21:33:52 +00001407 Op<0>() = Val;
1408 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001409 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001410}
1411
Chris Lattner65511ff2006-10-05 06:24:58 +00001412
Chris Lattner54865b32006-04-08 04:05:48 +00001413bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Owen Anderson55f1c092009-08-13 21:58:54 +00001414 if (!isa<VectorType>(Val->getType()) ||
1415 Index->getType() != Type::getInt32Ty(Val->getContext()))
Chris Lattner54865b32006-04-08 04:05:48 +00001416 return false;
1417 return true;
1418}
1419
1420
Robert Bocchino23004482006-01-10 19:05:34 +00001421//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001422// InsertElementInst Implementation
1423//===----------------------------------------------------------------------===//
1424
Chris Lattner54865b32006-04-08 04:05:48 +00001425InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001426 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001427 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001428 : Instruction(Vec->getType(), InsertElement,
1429 OperandTraits<InsertElementInst>::op_begin(this),
1430 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001431 assert(isValidOperands(Vec, Elt, Index) &&
1432 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001433 Op<0>() = Vec;
1434 Op<1>() = Elt;
1435 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001436 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001437}
1438
Chris Lattner54865b32006-04-08 04:05:48 +00001439InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001440 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001441 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001442 : Instruction(Vec->getType(), InsertElement,
1443 OperandTraits<InsertElementInst>::op_begin(this),
1444 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001445 assert(isValidOperands(Vec, Elt, Index) &&
1446 "Invalid insertelement instruction operands!");
1447
Gabor Greif2d3024d2008-05-26 21:33:52 +00001448 Op<0>() = Vec;
1449 Op<1>() = Elt;
1450 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001451 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001452}
1453
Chris Lattner54865b32006-04-08 04:05:48 +00001454bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1455 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001456 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001457 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001458
Reid Spencerd84d35b2007-02-15 02:26:10 +00001459 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001460 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001461
Owen Anderson55f1c092009-08-13 21:58:54 +00001462 if (Index->getType() != Type::getInt32Ty(Vec->getContext()))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001463 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001464 return true;
1465}
1466
1467
Robert Bocchinoca27f032006-01-17 20:07:22 +00001468//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001469// ShuffleVectorInst Implementation
1470//===----------------------------------------------------------------------===//
1471
1472ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001473 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001474 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001475: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001476 cast<VectorType>(Mask->getType())->getNumElements()),
1477 ShuffleVector,
1478 OperandTraits<ShuffleVectorInst>::op_begin(this),
1479 OperandTraits<ShuffleVectorInst>::operands(this),
1480 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001481 assert(isValidOperands(V1, V2, Mask) &&
1482 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001483 Op<0>() = V1;
1484 Op<1>() = V2;
1485 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001486 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001487}
1488
1489ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001490 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001491 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001492: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1493 cast<VectorType>(Mask->getType())->getNumElements()),
1494 ShuffleVector,
1495 OperandTraits<ShuffleVectorInst>::op_begin(this),
1496 OperandTraits<ShuffleVectorInst>::operands(this),
1497 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001498 assert(isValidOperands(V1, V2, Mask) &&
1499 "Invalid shuffle vector instruction operands!");
1500
Gabor Greif2d3024d2008-05-26 21:33:52 +00001501 Op<0>() = V1;
1502 Op<1>() = V2;
1503 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001504 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001505}
1506
Mon P Wang25f01062008-11-10 04:46:22 +00001507bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001508 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001509 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001510 return false;
1511
1512 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1513 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Owen Anderson55f1c092009-08-13 21:58:54 +00001514 MaskTy->getElementType() != Type::getInt32Ty(V1->getContext()))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001515 return false;
1516 return true;
1517}
1518
Chris Lattnerf724e342008-03-02 05:28:33 +00001519/// getMaskValue - Return the index from the shuffle mask for the specified
1520/// output result. This is either -1 if the element is undef or a number less
1521/// than 2*numelements.
1522int ShuffleVectorInst::getMaskValue(unsigned i) const {
1523 const Constant *Mask = cast<Constant>(getOperand(2));
1524 if (isa<UndefValue>(Mask)) return -1;
1525 if (isa<ConstantAggregateZero>(Mask)) return 0;
1526 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1527 assert(i < MaskCV->getNumOperands() && "Index out of range");
1528
1529 if (isa<UndefValue>(MaskCV->getOperand(i)))
1530 return -1;
1531 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1532}
1533
Dan Gohman12fce772008-05-15 19:50:34 +00001534//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001535// InsertValueInst Class
1536//===----------------------------------------------------------------------===//
1537
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001538void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001539 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001540 assert(NumOperands == 2 && "NumOperands not initialized?");
1541 Op<0>() = Agg;
1542 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001543
Dan Gohman1ecaf452008-05-31 00:58:22 +00001544 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001545 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001546}
1547
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001548void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001549 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001550 assert(NumOperands == 2 && "NumOperands not initialized?");
1551 Op<0>() = Agg;
1552 Op<1>() = Val;
1553
1554 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001555 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001556}
1557
1558InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001559 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001560 OperandTraits<InsertValueInst>::op_begin(this), 2),
1561 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001562 Op<0>() = IVI.getOperand(0);
1563 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001564 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001565}
1566
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001567InsertValueInst::InsertValueInst(Value *Agg,
1568 Value *Val,
1569 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001570 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001571 Instruction *InsertBefore)
1572 : Instruction(Agg->getType(), InsertValue,
1573 OperandTraits<InsertValueInst>::op_begin(this),
1574 2, InsertBefore) {
1575 init(Agg, Val, Idx, Name);
1576}
1577
1578InsertValueInst::InsertValueInst(Value *Agg,
1579 Value *Val,
1580 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001581 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001582 BasicBlock *InsertAtEnd)
1583 : Instruction(Agg->getType(), InsertValue,
1584 OperandTraits<InsertValueInst>::op_begin(this),
1585 2, InsertAtEnd) {
1586 init(Agg, Val, Idx, Name);
1587}
1588
Dan Gohman0752bff2008-05-23 00:36:11 +00001589//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001590// ExtractValueInst Class
1591//===----------------------------------------------------------------------===//
1592
Gabor Greifcbcc4952008-06-06 21:06:32 +00001593void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001594 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001595 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001596
Dan Gohman1ecaf452008-05-31 00:58:22 +00001597 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001598 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001599}
1600
Daniel Dunbar4975db62009-07-25 04:41:11 +00001601void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001602 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001603
1604 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001605 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001606}
1607
1608ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001609 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001610 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001611 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001612}
1613
Dan Gohman12fce772008-05-15 19:50:34 +00001614// getIndexedType - Returns the type of the element that would be extracted
1615// with an extractvalue instruction with the specified parameters.
1616//
1617// A null type is returned if the indices are invalid for the specified
1618// pointer type.
1619//
1620const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001621 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001622 unsigned NumIdx) {
1623 unsigned CurIdx = 0;
1624 for (; CurIdx != NumIdx; ++CurIdx) {
1625 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001626 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1627 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001628 if (!CT->indexValid(Index)) return 0;
1629 Agg = CT->getTypeAtIndex(Index);
1630
1631 // If the new type forwards to another type, then it is in the middle
1632 // of being refined to another type (and hence, may have dropped all
1633 // references to what it was using before). So, use the new forwarded
1634 // type.
1635 if (const Type *Ty = Agg->getForwardedType())
1636 Agg = Ty;
1637 }
1638 return CurIdx == NumIdx ? Agg : 0;
1639}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001640
Dan Gohman4a3125b2008-06-17 21:07:55 +00001641const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001642 unsigned Idx) {
1643 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001644}
1645
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001646//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001647// BinaryOperator Class
1648//===----------------------------------------------------------------------===//
1649
Dan Gohmana5b96452009-06-04 22:49:04 +00001650/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1651/// type is floating-point, to help provide compatibility with an older API.
1652///
1653static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1654 const Type *Ty) {
1655 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1656 if (Ty->isFPOrFPVector()) {
1657 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1658 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1659 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1660 }
1661 return iType;
1662}
1663
Chris Lattner2195fc42007-02-24 00:55:48 +00001664BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001665 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001666 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001667 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001668 OperandTraits<BinaryOperator>::op_begin(this),
1669 OperandTraits<BinaryOperator>::operands(this),
1670 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001671 Op<0>() = S1;
1672 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001673 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001674 setName(Name);
1675}
1676
1677BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001678 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001679 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001680 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001681 OperandTraits<BinaryOperator>::op_begin(this),
1682 OperandTraits<BinaryOperator>::operands(this),
1683 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001684 Op<0>() = S1;
1685 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001686 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001687 setName(Name);
1688}
1689
1690
1691void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001692 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001693 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001694 assert(LHS->getType() == RHS->getType() &&
1695 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001696#ifndef NDEBUG
1697 switch (iType) {
1698 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001699 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001700 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001701 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001702 assert(getType()->isIntOrIntVector() &&
1703 "Tried to create an integer operation on a non-integer type!");
1704 break;
1705 case FAdd: case FSub:
1706 case FMul:
1707 assert(getType() == LHS->getType() &&
1708 "Arithmetic operation should return same type as operands!");
1709 assert(getType()->isFPOrFPVector() &&
1710 "Tried to create a floating-point operation on a "
1711 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001712 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001713 case UDiv:
1714 case SDiv:
1715 assert(getType() == LHS->getType() &&
1716 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001717 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1718 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001719 "Incorrect operand type (not integer) for S/UDIV");
1720 break;
1721 case FDiv:
1722 assert(getType() == LHS->getType() &&
1723 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001724 assert(getType()->isFPOrFPVector() &&
1725 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001726 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001727 case URem:
1728 case SRem:
1729 assert(getType() == LHS->getType() &&
1730 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001731 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1732 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001733 "Incorrect operand type (not integer) for S/UREM");
1734 break;
1735 case FRem:
1736 assert(getType() == LHS->getType() &&
1737 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001738 assert(getType()->isFPOrFPVector() &&
1739 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001740 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001741 case Shl:
1742 case LShr:
1743 case AShr:
1744 assert(getType() == LHS->getType() &&
1745 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001746 assert((getType()->isInteger() ||
1747 (isa<VectorType>(getType()) &&
1748 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1749 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001750 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001751 case And: case Or:
1752 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001753 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001754 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001755 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001756 (isa<VectorType>(getType()) &&
1757 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001758 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001759 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001760 default:
1761 break;
1762 }
1763#endif
1764}
1765
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001766BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001767 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001768 Instruction *InsertBefore) {
1769 assert(S1->getType() == S2->getType() &&
1770 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001771 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001772}
1773
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001774BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001775 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001776 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001777 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001778 InsertAtEnd->getInstList().push_back(Res);
1779 return Res;
1780}
1781
Dan Gohman5476cfd2009-08-12 16:23:25 +00001782BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001783 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001784 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001785 return new BinaryOperator(Instruction::Sub,
1786 zero, Op,
1787 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001788}
1789
Dan Gohman5476cfd2009-08-12 16:23:25 +00001790BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001791 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001792 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001793 return new BinaryOperator(Instruction::Sub,
1794 zero, Op,
1795 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001796}
1797
Dan Gohman5476cfd2009-08-12 16:23:25 +00001798BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001799 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001800 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001801 return new BinaryOperator(Instruction::FSub,
1802 zero, Op,
1803 Op->getType(), Name, InsertBefore);
1804}
1805
Dan Gohman5476cfd2009-08-12 16:23:25 +00001806BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001807 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001808 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001809 return new BinaryOperator(Instruction::FSub,
1810 zero, Op,
1811 Op->getType(), Name, InsertAtEnd);
1812}
1813
Dan Gohman5476cfd2009-08-12 16:23:25 +00001814BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001815 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001816 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001817 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001818 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001819 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001820 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001821 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001822 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001823 }
1824
1825 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001826 Op->getType(), Name, InsertBefore);
1827}
1828
Dan Gohman5476cfd2009-08-12 16:23:25 +00001829BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001830 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001831 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001832 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001833 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001834 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001835 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001836 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001837 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001838 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001839 }
1840
1841 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001842 Op->getType(), Name, InsertAtEnd);
1843}
1844
1845
1846// isConstantAllOnes - Helper function for several functions below
1847static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001848 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1849 return CI->isAllOnesValue();
1850 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1851 return CV->isAllOnesValue();
1852 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001853}
1854
Owen Andersonbb2501b2009-07-13 22:18:28 +00001855bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001856 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1857 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001858 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1859 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001860 return false;
1861}
1862
Owen Andersonbb2501b2009-07-13 22:18:28 +00001863bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001864 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1865 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001866 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001867 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001868 return false;
1869}
1870
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001871bool BinaryOperator::isNot(const Value *V) {
1872 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1873 return (Bop->getOpcode() == Instruction::Xor &&
1874 (isConstantAllOnes(Bop->getOperand(1)) ||
1875 isConstantAllOnes(Bop->getOperand(0))));
1876 return false;
1877}
1878
Chris Lattner2c7d1772005-04-24 07:28:37 +00001879Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001880 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001881}
1882
Chris Lattner2c7d1772005-04-24 07:28:37 +00001883const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1884 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001885}
1886
Dan Gohmana5b96452009-06-04 22:49:04 +00001887Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001888 return cast<BinaryOperator>(BinOp)->getOperand(1);
1889}
1890
1891const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1892 return getFNegArgument(const_cast<Value*>(BinOp));
1893}
1894
Chris Lattner2c7d1772005-04-24 07:28:37 +00001895Value *BinaryOperator::getNotArgument(Value *BinOp) {
1896 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1897 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1898 Value *Op0 = BO->getOperand(0);
1899 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001900 if (isConstantAllOnes(Op0)) return Op1;
1901
1902 assert(isConstantAllOnes(Op1));
1903 return Op0;
1904}
1905
Chris Lattner2c7d1772005-04-24 07:28:37 +00001906const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1907 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001908}
1909
1910
1911// swapOperands - Exchange the two operands to this instruction. This
1912// instruction is safe to use on any binary instruction and does not
1913// modify the semantics of the instruction. If the instruction is
1914// order dependent (SetLT f.e.) the opcode is changed.
1915//
1916bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001917 if (!isCommutative())
1918 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001919 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001920 return false;
1921}
1922
Dan Gohman1b849082009-09-07 23:54:19 +00001923void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1924 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1925}
1926
1927void BinaryOperator::setHasNoSignedWrap(bool b) {
1928 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1929}
1930
1931void BinaryOperator::setIsExact(bool b) {
1932 cast<SDivOperator>(this)->setIsExact(b);
1933}
1934
Nick Lewycky28a5f252009-09-27 21:33:04 +00001935bool BinaryOperator::hasNoUnsignedWrap() const {
1936 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1937}
1938
1939bool BinaryOperator::hasNoSignedWrap() const {
1940 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1941}
1942
1943bool BinaryOperator::isExact() const {
1944 return cast<SDivOperator>(this)->isExact();
1945}
1946
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001947//===----------------------------------------------------------------------===//
1948// CastInst Class
1949//===----------------------------------------------------------------------===//
1950
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001951// Just determine if this cast only deals with integral->integral conversion.
1952bool CastInst::isIntegerCast() const {
1953 switch (getOpcode()) {
1954 default: return false;
1955 case Instruction::ZExt:
1956 case Instruction::SExt:
1957 case Instruction::Trunc:
1958 return true;
1959 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001960 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001961 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001962}
1963
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001964bool CastInst::isLosslessCast() const {
1965 // Only BitCast can be lossless, exit fast if we're not BitCast
1966 if (getOpcode() != Instruction::BitCast)
1967 return false;
1968
1969 // Identity cast is always lossless
1970 const Type* SrcTy = getOperand(0)->getType();
1971 const Type* DstTy = getType();
1972 if (SrcTy == DstTy)
1973 return true;
1974
Reid Spencer8d9336d2006-12-31 05:26:44 +00001975 // Pointer to pointer is always lossless.
1976 if (isa<PointerType>(SrcTy))
1977 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001978 return false; // Other types have no identity values
1979}
1980
1981/// This function determines if the CastInst does not require any bits to be
1982/// changed in order to effect the cast. Essentially, it identifies cases where
1983/// no code gen is necessary for the cast, hence the name no-op cast. For
1984/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001985/// # bitcast i32* %x to i8*
1986/// # bitcast <2 x i32> %x to <4 x i16>
1987/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001988/// @brief Determine if a cast is a no-op.
1989bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1990 switch (getOpcode()) {
1991 default:
1992 assert(!"Invalid CastOp");
1993 case Instruction::Trunc:
1994 case Instruction::ZExt:
1995 case Instruction::SExt:
1996 case Instruction::FPTrunc:
1997 case Instruction::FPExt:
1998 case Instruction::UIToFP:
1999 case Instruction::SIToFP:
2000 case Instruction::FPToUI:
2001 case Instruction::FPToSI:
2002 return false; // These always modify bits
2003 case Instruction::BitCast:
2004 return true; // BitCast never modifies bits.
2005 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002006 return IntPtrTy->getScalarSizeInBits() ==
2007 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002008 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002009 return IntPtrTy->getScalarSizeInBits() ==
2010 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002011 }
2012}
2013
2014/// This function determines if a pair of casts can be eliminated and what
2015/// opcode should be used in the elimination. This assumes that there are two
2016/// instructions like this:
2017/// * %F = firstOpcode SrcTy %x to MidTy
2018/// * %S = secondOpcode MidTy %F to DstTy
2019/// The function returns a resultOpcode so these two casts can be replaced with:
2020/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2021/// If no such cast is permited, the function returns 0.
2022unsigned CastInst::isEliminableCastPair(
2023 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
2024 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
2025{
2026 // Define the 144 possibilities for these two cast instructions. The values
2027 // in this matrix determine what to do in a given situation and select the
2028 // case in the switch below. The rows correspond to firstOp, the columns
2029 // correspond to secondOp. In looking at the table below, keep in mind
2030 // the following cast properties:
2031 //
2032 // Size Compare Source Destination
2033 // Operator Src ? Size Type Sign Type Sign
2034 // -------- ------------ ------------------- ---------------------
2035 // TRUNC > Integer Any Integral Any
2036 // ZEXT < Integral Unsigned Integer Any
2037 // SEXT < Integral Signed Integer Any
2038 // FPTOUI n/a FloatPt n/a Integral Unsigned
2039 // FPTOSI n/a FloatPt n/a Integral Signed
2040 // UITOFP n/a Integral Unsigned FloatPt n/a
2041 // SITOFP n/a Integral Signed FloatPt n/a
2042 // FPTRUNC > FloatPt n/a FloatPt n/a
2043 // FPEXT < FloatPt n/a FloatPt n/a
2044 // PTRTOINT n/a Pointer n/a Integral Unsigned
2045 // INTTOPTR n/a Integral Unsigned Pointer n/a
2046 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002047 //
2048 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002049 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2050 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002051 // of the produced value (we no longer know the top-part is all zeros).
2052 // Further this conversion is often much more expensive for typical hardware,
2053 // and causes issues when building libgcc. We disallow fptosi+sext for the
2054 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002055 const unsigned numCastOps =
2056 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2057 static const uint8_t CastResults[numCastOps][numCastOps] = {
2058 // T F F U S F F P I B -+
2059 // R Z S P P I I T P 2 N T |
2060 // U E E 2 2 2 2 R E I T C +- secondOp
2061 // N X X U S F F N X N 2 V |
2062 // C T T I I P P C T T P T -+
2063 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2064 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2065 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002066 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2067 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002068 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2069 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2070 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2071 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2072 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2073 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2074 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2075 };
2076
2077 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2078 [secondOp-Instruction::CastOpsBegin];
2079 switch (ElimCase) {
2080 case 0:
2081 // categorically disallowed
2082 return 0;
2083 case 1:
2084 // allowed, use first cast's opcode
2085 return firstOp;
2086 case 2:
2087 // allowed, use second cast's opcode
2088 return secondOp;
2089 case 3:
2090 // no-op cast in second op implies firstOp as long as the DestTy
2091 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00002092 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002093 return firstOp;
2094 return 0;
2095 case 4:
2096 // no-op cast in second op implies firstOp as long as the DestTy
2097 // is floating point
2098 if (DstTy->isFloatingPoint())
2099 return firstOp;
2100 return 0;
2101 case 5:
2102 // no-op cast in first op implies secondOp as long as the SrcTy
2103 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00002104 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002105 return secondOp;
2106 return 0;
2107 case 6:
2108 // no-op cast in first op implies secondOp as long as the SrcTy
2109 // is a floating point
2110 if (SrcTy->isFloatingPoint())
2111 return secondOp;
2112 return 0;
2113 case 7: {
2114 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002115 if (!IntPtrTy)
2116 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002117 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2118 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002119 if (MidSize >= PtrSize)
2120 return Instruction::BitCast;
2121 return 0;
2122 }
2123 case 8: {
2124 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2125 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2126 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002127 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2128 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002129 if (SrcSize == DstSize)
2130 return Instruction::BitCast;
2131 else if (SrcSize < DstSize)
2132 return firstOp;
2133 return secondOp;
2134 }
2135 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2136 return Instruction::ZExt;
2137 case 10:
2138 // fpext followed by ftrunc is allowed if the bit size returned to is
2139 // the same as the original, in which case its just a bitcast
2140 if (SrcTy == DstTy)
2141 return Instruction::BitCast;
2142 return 0; // If the types are not the same we can't eliminate it.
2143 case 11:
2144 // bitcast followed by ptrtoint is allowed as long as the bitcast
2145 // is a pointer to pointer cast.
2146 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
2147 return secondOp;
2148 return 0;
2149 case 12:
2150 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2151 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
2152 return firstOp;
2153 return 0;
2154 case 13: {
2155 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002156 if (!IntPtrTy)
2157 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002158 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2159 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2160 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002161 if (SrcSize <= PtrSize && SrcSize == DstSize)
2162 return Instruction::BitCast;
2163 return 0;
2164 }
2165 case 99:
2166 // cast combination can't happen (error in input). This is for all cases
2167 // where the MidTy is not the same for the two cast instructions.
2168 assert(!"Invalid Cast Combination");
2169 return 0;
2170 default:
2171 assert(!"Error in CastResults table!!!");
2172 return 0;
2173 }
2174 return 0;
2175}
2176
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002177CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002178 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002179 // Construct and return the appropriate CastInst subclass
2180 switch (op) {
2181 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2182 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2183 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2184 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2185 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2186 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2187 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2188 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2189 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2190 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2191 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2192 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2193 default:
2194 assert(!"Invalid opcode provided");
2195 }
2196 return 0;
2197}
2198
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002199CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002200 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002201 // Construct and return the appropriate CastInst subclass
2202 switch (op) {
2203 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2204 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2205 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2206 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2207 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2208 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2209 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2210 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2211 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2212 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2213 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2214 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2215 default:
2216 assert(!"Invalid opcode provided");
2217 }
2218 return 0;
2219}
2220
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002221CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002222 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002223 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002224 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002225 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2226 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002227}
2228
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002229CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002230 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002231 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002232 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002233 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2234 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002235}
2236
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002237CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002238 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002239 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002240 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002241 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2242 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002243}
2244
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002245CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002246 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002247 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002248 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002249 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2250 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002251}
2252
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002253CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002254 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002255 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002256 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002257 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2258 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002259}
2260
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002261CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002262 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002263 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002264 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002265 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2266 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002267}
2268
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002269CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002270 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002271 BasicBlock *InsertAtEnd) {
2272 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002273 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002274 "Invalid cast");
2275
Chris Lattner03c49532007-01-15 02:27:26 +00002276 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002277 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2278 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002279}
2280
2281/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002282CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002283 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002284 Instruction *InsertBefore) {
2285 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002286 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002287 "Invalid cast");
2288
Chris Lattner03c49532007-01-15 02:27:26 +00002289 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002290 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2291 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002292}
2293
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002294CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002295 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002296 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002297 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002298 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2299 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002300 Instruction::CastOps opcode =
2301 (SrcBits == DstBits ? Instruction::BitCast :
2302 (SrcBits > DstBits ? Instruction::Trunc :
2303 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002304 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002305}
2306
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002307CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002308 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002309 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002310 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2311 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002312 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2313 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002314 Instruction::CastOps opcode =
2315 (SrcBits == DstBits ? Instruction::BitCast :
2316 (SrcBits > DstBits ? Instruction::Trunc :
2317 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002318 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002319}
2320
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002321CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002322 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002323 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002324 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002325 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002326 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2327 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002328 Instruction::CastOps opcode =
2329 (SrcBits == DstBits ? Instruction::BitCast :
2330 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002331 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002332}
2333
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002334CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002335 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002336 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002337 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002338 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002339 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2340 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002341 Instruction::CastOps opcode =
2342 (SrcBits == DstBits ? Instruction::BitCast :
2343 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002344 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002345}
2346
Duncan Sands55e50902008-01-06 10:12:28 +00002347// Check whether it is valid to call getCastOpcode for these types.
2348// This routine must be kept in sync with getCastOpcode.
2349bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2350 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2351 return false;
2352
2353 if (SrcTy == DestTy)
2354 return true;
2355
2356 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002357 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2358 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002359
2360 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002361 if (DestTy->isInteger()) { // Casting to integral
2362 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002363 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002364 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002365 return true;
2366 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002367 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002368 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002369 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002370 return isa<PointerType>(SrcTy);
2371 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002372 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2373 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002374 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002375 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002376 return true;
2377 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002378 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002379 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002380 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002381 return false;
2382 }
2383 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002384 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002385 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002386 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002387 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002388 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002389 return DestPTy->getBitWidth() == SrcBits;
2390 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002391 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2392 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002393 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002394 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002395 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002396 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002397 return false;
2398 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002399 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002400 return false;
2401 }
2402}
2403
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002404// Provide a way to get a "cast" where the cast opcode is inferred from the
2405// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002406// logic in the castIsValid function below. This axiom should hold:
2407// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2408// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002409// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002410// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002411Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002412CastInst::getCastOpcode(
2413 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002414 // Get the bit sizes, we'll need these
2415 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002416 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2417 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002418
Duncan Sands55e50902008-01-06 10:12:28 +00002419 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2420 "Only first class types are castable!");
2421
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002423 if (DestTy->isInteger()) { // Casting to integral
2424 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425 if (DestBits < SrcBits)
2426 return Trunc; // int -> smaller int
2427 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002428 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429 return SExt; // signed -> SEXT
2430 else
2431 return ZExt; // unsigned -> ZEXT
2432 } else {
2433 return BitCast; // Same size, No-op cast
2434 }
2435 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002436 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437 return FPToSI; // FP -> sint
2438 else
2439 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002440 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002441 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002442 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002443 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444 return BitCast; // Same size, no-op cast
2445 } else {
2446 assert(isa<PointerType>(SrcTy) &&
2447 "Casting from a value that is not first-class type");
2448 return PtrToInt; // ptr -> int
2449 }
2450 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002451 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002452 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453 return SIToFP; // sint -> FP
2454 else
2455 return UIToFP; // uint -> FP
2456 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2457 if (DestBits < SrcBits) {
2458 return FPTrunc; // FP -> smaller FP
2459 } else if (DestBits > SrcBits) {
2460 return FPExt; // FP -> larger FP
2461 } else {
2462 return BitCast; // same size, no-op cast
2463 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002464 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002466 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002467 PTy = NULL;
2468 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002470 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002472 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2473 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002474 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002475 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002476 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002477 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002479 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002480 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002481 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002482 }
2483 } else if (isa<PointerType>(DestTy)) {
2484 if (isa<PointerType>(SrcTy)) {
2485 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002486 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002487 return IntToPtr; // int -> ptr
2488 } else {
2489 assert(!"Casting pointer to other than pointer or int");
2490 }
2491 } else {
2492 assert(!"Casting to type that is not first-class");
2493 }
2494
2495 // If we fall through to here we probably hit an assertion cast above
2496 // and assertions are not turned on. Anything we return is an error, so
2497 // BitCast is as good a choice as any.
2498 return BitCast;
2499}
2500
2501//===----------------------------------------------------------------------===//
2502// CastInst SubClass Constructors
2503//===----------------------------------------------------------------------===//
2504
2505/// Check that the construction parameters for a CastInst are correct. This
2506/// could be broken out into the separate constructors but it is useful to have
2507/// it in one place and to eliminate the redundant code for getting the sizes
2508/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002509bool
2510CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002511
2512 // Check for type sanity on the arguments
2513 const Type *SrcTy = S->getType();
2514 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2515 return false;
2516
2517 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002518 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2519 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002520
2521 // Switch on the opcode provided
2522 switch (op) {
2523 default: return false; // This is an input error
2524 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002525 return SrcTy->isIntOrIntVector() &&
2526 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002527 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002528 return SrcTy->isIntOrIntVector() &&
2529 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002531 return SrcTy->isIntOrIntVector() &&
2532 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002533 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002534 return SrcTy->isFPOrFPVector() &&
2535 DstTy->isFPOrFPVector() &&
2536 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002537 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002538 return SrcTy->isFPOrFPVector() &&
2539 DstTy->isFPOrFPVector() &&
2540 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002542 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002543 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2544 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002545 return SVTy->getElementType()->isIntOrIntVector() &&
2546 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002547 SVTy->getNumElements() == DVTy->getNumElements();
2548 }
2549 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002550 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002551 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002552 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002553 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2554 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002555 return SVTy->getElementType()->isFPOrFPVector() &&
2556 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002557 SVTy->getNumElements() == DVTy->getNumElements();
2558 }
2559 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002560 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002562 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002564 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002565 case Instruction::BitCast:
2566 // BitCast implies a no-op cast of type only. No bits change.
2567 // However, you can't cast pointers to anything but pointers.
2568 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2569 return false;
2570
Duncan Sands55e50902008-01-06 10:12:28 +00002571 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002572 // these cases, the cast is okay if the source and destination bit widths
2573 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002574 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002575 }
2576}
2577
2578TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002579 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002581 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002582}
2583
2584TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002585 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002587 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002588}
2589
2590ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002591 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002592) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002593 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594}
2595
2596ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002597 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002598) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002599 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600}
2601SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002602 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002603) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002604 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002605}
2606
Jeff Cohencc08c832006-12-02 02:22:01 +00002607SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002608 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002610 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611}
2612
2613FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002614 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002615) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002616 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002617}
2618
2619FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002620 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002621) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002622 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623}
2624
2625FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002626 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002627) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002628 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002629}
2630
2631FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002632 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002633) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002634 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002635}
2636
2637UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002638 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002639) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002640 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002641}
2642
2643UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002644 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002645) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002646 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002647}
2648
2649SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002650 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002651) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002652 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002653}
2654
2655SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002656 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002657) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002658 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659}
2660
2661FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002662 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002663) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002664 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002665}
2666
2667FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002668 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002669) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002670 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002671}
2672
2673FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002674 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002675) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002676 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002677}
2678
2679FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002680 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002681) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002682 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002683}
2684
2685PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002686 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002687) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002688 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002689}
2690
2691PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002692 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002693) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002694 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002695}
2696
2697IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002698 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002699) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002700 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002701}
2702
2703IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002704 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002705) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002706 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002707}
2708
2709BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002710 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002711) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002712 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002713}
2714
2715BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002716 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002717) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002718 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002719}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002720
2721//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002722// CmpInst Classes
2723//===----------------------------------------------------------------------===//
2724
Nate Begemand2195702008-05-12 19:01:56 +00002725CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002726 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002727 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002728 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002729 OperandTraits<CmpInst>::op_begin(this),
2730 OperandTraits<CmpInst>::operands(this),
2731 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002732 Op<0>() = LHS;
2733 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002734 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002735 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002736}
Gabor Greiff6caff662008-05-10 08:32:32 +00002737
Nate Begemand2195702008-05-12 19:01:56 +00002738CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002739 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002740 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002741 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002742 OperandTraits<CmpInst>::op_begin(this),
2743 OperandTraits<CmpInst>::operands(this),
2744 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002745 Op<0>() = LHS;
2746 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002747 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002748 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002749}
2750
2751CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002752CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002753 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002754 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002755 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002756 if (InsertBefore)
2757 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2758 S1, S2, Name);
2759 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002760 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002761 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002762 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002763
2764 if (InsertBefore)
2765 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2766 S1, S2, Name);
2767 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002768 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002769 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002770}
2771
2772CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002773CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002774 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002775 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002776 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2777 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002778 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002779 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2780 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002781}
2782
2783void CmpInst::swapOperands() {
2784 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2785 IC->swapOperands();
2786 else
2787 cast<FCmpInst>(this)->swapOperands();
2788}
2789
2790bool CmpInst::isCommutative() {
2791 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2792 return IC->isCommutative();
2793 return cast<FCmpInst>(this)->isCommutative();
2794}
2795
2796bool CmpInst::isEquality() {
2797 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2798 return IC->isEquality();
2799 return cast<FCmpInst>(this)->isEquality();
2800}
2801
2802
Dan Gohman4e724382008-05-31 02:47:54 +00002803CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002804 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002805 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002806 case ICMP_EQ: return ICMP_NE;
2807 case ICMP_NE: return ICMP_EQ;
2808 case ICMP_UGT: return ICMP_ULE;
2809 case ICMP_ULT: return ICMP_UGE;
2810 case ICMP_UGE: return ICMP_ULT;
2811 case ICMP_ULE: return ICMP_UGT;
2812 case ICMP_SGT: return ICMP_SLE;
2813 case ICMP_SLT: return ICMP_SGE;
2814 case ICMP_SGE: return ICMP_SLT;
2815 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002816
Dan Gohman4e724382008-05-31 02:47:54 +00002817 case FCMP_OEQ: return FCMP_UNE;
2818 case FCMP_ONE: return FCMP_UEQ;
2819 case FCMP_OGT: return FCMP_ULE;
2820 case FCMP_OLT: return FCMP_UGE;
2821 case FCMP_OGE: return FCMP_ULT;
2822 case FCMP_OLE: return FCMP_UGT;
2823 case FCMP_UEQ: return FCMP_ONE;
2824 case FCMP_UNE: return FCMP_OEQ;
2825 case FCMP_UGT: return FCMP_OLE;
2826 case FCMP_ULT: return FCMP_OGE;
2827 case FCMP_UGE: return FCMP_OLT;
2828 case FCMP_ULE: return FCMP_OGT;
2829 case FCMP_ORD: return FCMP_UNO;
2830 case FCMP_UNO: return FCMP_ORD;
2831 case FCMP_TRUE: return FCMP_FALSE;
2832 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002833 }
2834}
2835
Reid Spencer266e42b2006-12-23 06:05:41 +00002836ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2837 switch (pred) {
2838 default: assert(! "Unknown icmp predicate!");
2839 case ICMP_EQ: case ICMP_NE:
2840 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2841 return pred;
2842 case ICMP_UGT: return ICMP_SGT;
2843 case ICMP_ULT: return ICMP_SLT;
2844 case ICMP_UGE: return ICMP_SGE;
2845 case ICMP_ULE: return ICMP_SLE;
2846 }
2847}
2848
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002849ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2850 switch (pred) {
2851 default: assert(! "Unknown icmp predicate!");
2852 case ICMP_EQ: case ICMP_NE:
2853 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2854 return pred;
2855 case ICMP_SGT: return ICMP_UGT;
2856 case ICMP_SLT: return ICMP_ULT;
2857 case ICMP_SGE: return ICMP_UGE;
2858 case ICMP_SLE: return ICMP_ULE;
2859 }
2860}
2861
Reid Spencer266e42b2006-12-23 06:05:41 +00002862bool ICmpInst::isSignedPredicate(Predicate pred) {
2863 switch (pred) {
2864 default: assert(! "Unknown icmp predicate!");
2865 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2866 return true;
2867 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2868 case ICMP_UGE: case ICMP_ULE:
2869 return false;
2870 }
2871}
2872
Reid Spencer0286bc12007-02-28 22:00:54 +00002873/// Initialize a set of values that all satisfy the condition with C.
2874///
2875ConstantRange
2876ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2877 APInt Lower(C);
2878 APInt Upper(C);
2879 uint32_t BitWidth = C.getBitWidth();
2880 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002881 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002882 case ICmpInst::ICMP_EQ: Upper++; break;
2883 case ICmpInst::ICMP_NE: Lower++; break;
2884 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2885 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2886 case ICmpInst::ICMP_UGT:
2887 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2888 break;
2889 case ICmpInst::ICMP_SGT:
2890 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2891 break;
2892 case ICmpInst::ICMP_ULE:
2893 Lower = APInt::getMinValue(BitWidth); Upper++;
2894 break;
2895 case ICmpInst::ICMP_SLE:
2896 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2897 break;
2898 case ICmpInst::ICMP_UGE:
2899 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2900 break;
2901 case ICmpInst::ICMP_SGE:
2902 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2903 break;
2904 }
2905 return ConstantRange(Lower, Upper);
2906}
2907
Dan Gohman4e724382008-05-31 02:47:54 +00002908CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002909 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002910 default: assert(!"Unknown cmp predicate!");
2911 case ICMP_EQ: case ICMP_NE:
2912 return pred;
2913 case ICMP_SGT: return ICMP_SLT;
2914 case ICMP_SLT: return ICMP_SGT;
2915 case ICMP_SGE: return ICMP_SLE;
2916 case ICMP_SLE: return ICMP_SGE;
2917 case ICMP_UGT: return ICMP_ULT;
2918 case ICMP_ULT: return ICMP_UGT;
2919 case ICMP_UGE: return ICMP_ULE;
2920 case ICMP_ULE: return ICMP_UGE;
2921
Reid Spencerd9436b62006-11-20 01:22:35 +00002922 case FCMP_FALSE: case FCMP_TRUE:
2923 case FCMP_OEQ: case FCMP_ONE:
2924 case FCMP_UEQ: case FCMP_UNE:
2925 case FCMP_ORD: case FCMP_UNO:
2926 return pred;
2927 case FCMP_OGT: return FCMP_OLT;
2928 case FCMP_OLT: return FCMP_OGT;
2929 case FCMP_OGE: return FCMP_OLE;
2930 case FCMP_OLE: return FCMP_OGE;
2931 case FCMP_UGT: return FCMP_ULT;
2932 case FCMP_ULT: return FCMP_UGT;
2933 case FCMP_UGE: return FCMP_ULE;
2934 case FCMP_ULE: return FCMP_UGE;
2935 }
2936}
2937
Reid Spencer266e42b2006-12-23 06:05:41 +00002938bool CmpInst::isUnsigned(unsigned short predicate) {
2939 switch (predicate) {
2940 default: return false;
2941 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2942 case ICmpInst::ICMP_UGE: return true;
2943 }
2944}
2945
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002946bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002947 switch (predicate) {
2948 default: return false;
2949 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2950 case ICmpInst::ICMP_SGE: return true;
2951 }
2952}
2953
2954bool CmpInst::isOrdered(unsigned short predicate) {
2955 switch (predicate) {
2956 default: return false;
2957 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2958 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2959 case FCmpInst::FCMP_ORD: return true;
2960 }
2961}
2962
2963bool CmpInst::isUnordered(unsigned short predicate) {
2964 switch (predicate) {
2965 default: return false;
2966 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2967 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2968 case FCmpInst::FCMP_UNO: return true;
2969 }
2970}
2971
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002972bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2973 switch(predicate) {
2974 default: return false;
2975 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2976 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2977 }
2978}
2979
2980bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2981 switch(predicate) {
2982 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2983 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2984 default: return false;
2985 }
2986}
2987
2988
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002989//===----------------------------------------------------------------------===//
2990// SwitchInst Implementation
2991//===----------------------------------------------------------------------===//
2992
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002993void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002994 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002995 ReservedSpace = 2+NumCases*2;
2996 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002997 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002998
Gabor Greif2d3024d2008-05-26 21:33:52 +00002999 OperandList[0] = Value;
3000 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003001}
3002
Chris Lattner2195fc42007-02-24 00:55:48 +00003003/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3004/// switch on and a default destination. The number of additional cases can
3005/// be specified here to make memory allocation more efficient. This
3006/// constructor can also autoinsert before another instruction.
3007SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3008 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003009 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3010 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00003011 init(Value, Default, NumCases);
3012}
3013
3014/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3015/// switch on and a default destination. The number of additional cases can
3016/// be specified here to make memory allocation more efficient. This
3017/// constructor also autoinserts at the end of the specified BasicBlock.
3018SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3019 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003020 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3021 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00003022 init(Value, Default, NumCases);
3023}
3024
Misha Brukmanb1c93172005-04-21 23:48:37 +00003025SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00003026 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00003027 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003028 Use *OL = OperandList, *InOL = SI.OperandList;
3029 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003030 OL[i] = InOL[i];
3031 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003032 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003033 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003034}
3035
Gordon Henriksen14a55692007-12-10 02:14:30 +00003036SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003037 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003038}
3039
3040
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003041/// addCase - Add an entry to the switch instruction...
3042///
Chris Lattner47ac1872005-02-24 05:32:09 +00003043void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003044 unsigned OpNo = NumOperands;
3045 if (OpNo+2 > ReservedSpace)
3046 resizeOperands(0); // Get more space!
3047 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003048 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003049 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003050 OperandList[OpNo] = OnVal;
3051 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003052}
3053
3054/// removeCase - This method removes the specified successor from the switch
3055/// instruction. Note that this cannot be used to remove the default
3056/// destination (successor #0).
3057///
3058void SwitchInst::removeCase(unsigned idx) {
3059 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003060 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3061
3062 unsigned NumOps = getNumOperands();
3063 Use *OL = OperandList;
3064
3065 // Move everything after this operand down.
3066 //
3067 // FIXME: we could just swap with the end of the list, then erase. However,
3068 // client might not expect this to happen. The code as it is thrashes the
3069 // use/def lists, which is kinda lame.
3070 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3071 OL[i-2] = OL[i];
3072 OL[i-2+1] = OL[i+1];
3073 }
3074
3075 // Nuke the last value.
3076 OL[NumOps-2].set(0);
3077 OL[NumOps-2+1].set(0);
3078 NumOperands = NumOps-2;
3079}
3080
3081/// resizeOperands - resize operands - This adjusts the length of the operands
3082/// list according to the following behavior:
3083/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003084/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003085/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3086/// 3. If NumOps == NumOperands, trim the reserved space.
3087///
3088void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003089 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003090 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003091 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003092 } else if (NumOps*2 > NumOperands) {
3093 // No resize needed.
3094 if (ReservedSpace >= NumOps) return;
3095 } else if (NumOps == NumOperands) {
3096 if (ReservedSpace == NumOps) return;
3097 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003098 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003099 }
3100
3101 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003102 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003103 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003104 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003105 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003106 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003107 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003108 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003109}
3110
3111
3112BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3113 return getSuccessor(idx);
3114}
3115unsigned SwitchInst::getNumSuccessorsV() const {
3116 return getNumSuccessors();
3117}
3118void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3119 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003120}
Chris Lattnerf22be932004-10-15 23:52:53 +00003121
Chris Lattnerf22be932004-10-15 23:52:53 +00003122// Define these methods here so vtables don't get emitted into every translation
3123// unit that uses these classes.
3124
Nick Lewycky42fb7452009-09-27 07:38:41 +00003125GetElementPtrInst *GetElementPtrInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003126 GetElementPtrInst *New = new(getNumOperands()) GetElementPtrInst(*this);
3127 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003128 if (hasMetadata()) {
3129 LLVMContext &Context = getContext();
3130 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3131 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003132 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00003133}
3134
Nick Lewycky42fb7452009-09-27 07:38:41 +00003135BinaryOperator *BinaryOperator::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003136 BinaryOperator *New = Create(getOpcode(), Op<0>(), Op<1>());
3137 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003138 if (hasMetadata()) {
3139 LLVMContext &Context = getContext();
3140 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3141 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003142 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00003143}
3144
Nick Lewycky42fb7452009-09-27 07:38:41 +00003145FCmpInst* FCmpInst::clone() const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003146 FCmpInst *New = new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003147 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003148 if (hasMetadata()) {
3149 LLVMContext &Context = getContext();
3150 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3151 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003152 return New;
Chris Lattner0b490b02007-08-24 20:48:18 +00003153}
Nick Lewycky42fb7452009-09-27 07:38:41 +00003154ICmpInst* ICmpInst::clone() const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003155 ICmpInst *New = new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003156 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003157 if (hasMetadata()) {
3158 LLVMContext &Context = getContext();
3159 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3160 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003161 return New;
Reid Spencerd9436b62006-11-20 01:22:35 +00003162}
3163
Nick Lewycky42fb7452009-09-27 07:38:41 +00003164ExtractValueInst *ExtractValueInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003165 ExtractValueInst *New = new ExtractValueInst(*this);
3166 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003167 if (hasMetadata()) {
3168 LLVMContext &Context = getContext();
3169 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3170 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003171 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00003172}
Nick Lewycky42fb7452009-09-27 07:38:41 +00003173InsertValueInst *InsertValueInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003174 InsertValueInst *New = new InsertValueInst(*this);
3175 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003176 if (hasMetadata()) {
3177 LLVMContext &Context = getContext();
3178 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3179 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003180 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00003181}
3182
Nick Lewycky42fb7452009-09-27 07:38:41 +00003183AllocaInst *AllocaInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003184 AllocaInst *New = new AllocaInst(getAllocatedType(),
3185 (Value*)getOperand(0),
3186 getAlignment());
3187 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003188 if (hasMetadata()) {
3189 LLVMContext &Context = getContext();
3190 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3191 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003192 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003193}
3194
Nick Lewycky42fb7452009-09-27 07:38:41 +00003195FreeInst *FreeInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003196 FreeInst *New = new FreeInst(getOperand(0));
3197 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003198 if (hasMetadata()) {
3199 LLVMContext &Context = getContext();
3200 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3201 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003202 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003203}
3204
Nick Lewycky42fb7452009-09-27 07:38:41 +00003205LoadInst *LoadInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003206 LoadInst *New = new LoadInst(getOperand(0),
3207 Twine(), isVolatile(),
3208 getAlignment());
3209 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003210 if (hasMetadata()) {
3211 LLVMContext &Context = getContext();
3212 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3213 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003214 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003215}
3216
Nick Lewycky42fb7452009-09-27 07:38:41 +00003217StoreInst *StoreInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003218 StoreInst *New = new StoreInst(getOperand(0), getOperand(1),
3219 isVolatile(), getAlignment());
3220 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003221 if (hasMetadata()) {
3222 LLVMContext &Context = getContext();
3223 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3224 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003225 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003226}
3227
Nick Lewycky42fb7452009-09-27 07:38:41 +00003228TruncInst *TruncInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003229 TruncInst *New = new TruncInst(getOperand(0), getType());
3230 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003231 if (hasMetadata()) {
3232 LLVMContext &Context = getContext();
3233 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3234 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003235 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003236}
3237
Nick Lewycky42fb7452009-09-27 07:38:41 +00003238ZExtInst *ZExtInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003239 ZExtInst *New = new ZExtInst(getOperand(0), getType());
3240 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003241 if (hasMetadata()) {
3242 LLVMContext &Context = getContext();
3243 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3244 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003245 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003246}
3247
Nick Lewycky42fb7452009-09-27 07:38:41 +00003248SExtInst *SExtInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003249 SExtInst *New = new SExtInst(getOperand(0), getType());
3250 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003251 if (hasMetadata()) {
3252 LLVMContext &Context = getContext();
3253 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3254 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003255 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003256}
3257
Nick Lewycky42fb7452009-09-27 07:38:41 +00003258FPTruncInst *FPTruncInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003259 FPTruncInst *New = new FPTruncInst(getOperand(0), getType());
3260 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003261 if (hasMetadata()) {
3262 LLVMContext &Context = getContext();
3263 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3264 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003265 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003266}
3267
Nick Lewycky42fb7452009-09-27 07:38:41 +00003268FPExtInst *FPExtInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003269 FPExtInst *New = new FPExtInst(getOperand(0), getType());
3270 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003271 if (hasMetadata()) {
3272 LLVMContext &Context = getContext();
3273 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3274 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003275 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003276}
3277
Nick Lewycky42fb7452009-09-27 07:38:41 +00003278UIToFPInst *UIToFPInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003279 UIToFPInst *New = new UIToFPInst(getOperand(0), getType());
3280 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003281 if (hasMetadata()) {
3282 LLVMContext &Context = getContext();
3283 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3284 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003285 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003286}
3287
Nick Lewycky42fb7452009-09-27 07:38:41 +00003288SIToFPInst *SIToFPInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003289 SIToFPInst *New = new SIToFPInst(getOperand(0), getType());
3290 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003291 if (hasMetadata()) {
3292 LLVMContext &Context = getContext();
3293 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3294 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003295 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003296}
3297
Nick Lewycky42fb7452009-09-27 07:38:41 +00003298FPToUIInst *FPToUIInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003299 FPToUIInst *New = new FPToUIInst(getOperand(0), getType());
3300 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003301 if (hasMetadata()) {
3302 LLVMContext &Context = getContext();
3303 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3304 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003305 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003306}
3307
Nick Lewycky42fb7452009-09-27 07:38:41 +00003308FPToSIInst *FPToSIInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003309 FPToSIInst *New = new FPToSIInst(getOperand(0), getType());
3310 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003311 if (hasMetadata()) {
3312 LLVMContext &Context = getContext();
3313 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3314 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003315 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003316}
3317
Nick Lewycky42fb7452009-09-27 07:38:41 +00003318PtrToIntInst *PtrToIntInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003319 PtrToIntInst *New = new PtrToIntInst(getOperand(0), getType());
3320 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003321 if (hasMetadata()) {
3322 LLVMContext &Context = getContext();
3323 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3324 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003325 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003326}
3327
Nick Lewycky42fb7452009-09-27 07:38:41 +00003328IntToPtrInst *IntToPtrInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003329 IntToPtrInst *New = new IntToPtrInst(getOperand(0), getType());
3330 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003331 if (hasMetadata()) {
3332 LLVMContext &Context = getContext();
3333 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3334 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003335 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003336}
3337
Nick Lewycky42fb7452009-09-27 07:38:41 +00003338BitCastInst *BitCastInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003339 BitCastInst *New = new BitCastInst(getOperand(0), getType());
3340 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003341 if (hasMetadata()) {
3342 LLVMContext &Context = getContext();
3343 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3344 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003345 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003346}
3347
Nick Lewycky42fb7452009-09-27 07:38:41 +00003348CallInst *CallInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003349 CallInst *New = new(getNumOperands()) CallInst(*this);
3350 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003351 if (hasMetadata()) {
3352 LLVMContext &Context = getContext();
3353 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3354 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003355 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003356}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003357
Nick Lewycky42fb7452009-09-27 07:38:41 +00003358SelectInst *SelectInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003359 SelectInst *New = SelectInst::Create(getOperand(0),
3360 getOperand(1),
3361 getOperand(2));
3362 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003363 if (hasMetadata()) {
3364 LLVMContext &Context = getContext();
3365 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3366 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003367 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003368}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003369
Nick Lewycky42fb7452009-09-27 07:38:41 +00003370VAArgInst *VAArgInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003371 VAArgInst *New = new VAArgInst(getOperand(0), getType());
3372 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003373 if (hasMetadata()) {
3374 LLVMContext &Context = getContext();
3375 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3376 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003377 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003378}
3379
Nick Lewycky42fb7452009-09-27 07:38:41 +00003380ExtractElementInst *ExtractElementInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003381 ExtractElementInst *New = ExtractElementInst::Create(getOperand(0),
3382 getOperand(1));
3383 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003384 if (hasMetadata()) {
3385 LLVMContext &Context = getContext();
3386 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3387 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003388 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003389}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003390
Nick Lewycky42fb7452009-09-27 07:38:41 +00003391InsertElementInst *InsertElementInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003392 InsertElementInst *New = InsertElementInst::Create(getOperand(0),
3393 getOperand(1),
3394 getOperand(2));
3395 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003396 if (hasMetadata()) {
3397 LLVMContext &Context = getContext();
3398 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3399 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003400 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003401}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003402
Nick Lewycky42fb7452009-09-27 07:38:41 +00003403ShuffleVectorInst *ShuffleVectorInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003404 ShuffleVectorInst *New = new ShuffleVectorInst(getOperand(0),
3405 getOperand(1),
3406 getOperand(2));
3407 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003408 if (hasMetadata()) {
3409 LLVMContext &Context = getContext();
3410 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3411 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003412 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003413}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003414
Nick Lewycky42fb7452009-09-27 07:38:41 +00003415PHINode *PHINode::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003416 PHINode *New = new PHINode(*this);
3417 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003418 if (hasMetadata()) {
3419 LLVMContext &Context = getContext();
3420 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3421 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003422 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003423}
3424
Nick Lewycky42fb7452009-09-27 07:38:41 +00003425ReturnInst *ReturnInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003426 ReturnInst *New = new(getNumOperands()) ReturnInst(*this);
3427 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003428 if (hasMetadata()) {
3429 LLVMContext &Context = getContext();
3430 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3431 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003432 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003433}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003434
Nick Lewycky42fb7452009-09-27 07:38:41 +00003435BranchInst *BranchInst::clone() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003436 unsigned Ops(getNumOperands());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003437 BranchInst *New = new(Ops, Ops == 1) BranchInst(*this);
3438 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003439 if (hasMetadata()) {
3440 LLVMContext &Context = getContext();
3441 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3442 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003443 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003444}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003445
Nick Lewycky42fb7452009-09-27 07:38:41 +00003446SwitchInst *SwitchInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003447 SwitchInst *New = new SwitchInst(*this);
3448 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003449 if (hasMetadata()) {
3450 LLVMContext &Context = getContext();
3451 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3452 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003453 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003454}
3455
Nick Lewycky42fb7452009-09-27 07:38:41 +00003456InvokeInst *InvokeInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003457 InvokeInst *New = new(getNumOperands()) InvokeInst(*this);
3458 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003459 if (hasMetadata()) {
3460 LLVMContext &Context = getContext();
3461 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3462 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003463 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003464}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003465
Nick Lewycky42fb7452009-09-27 07:38:41 +00003466UnwindInst *UnwindInst::clone() const {
3467 LLVMContext &Context = getContext();
3468 UnwindInst *New = new UnwindInst(Context);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003469 New->SubclassOptionalData = SubclassOptionalData;
Nick Lewycky42fb7452009-09-27 07:38:41 +00003470 if (hasMetadata())
Devang Pateladd58652009-09-23 18:32:25 +00003471 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003472 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003473}
3474
Nick Lewycky42fb7452009-09-27 07:38:41 +00003475UnreachableInst *UnreachableInst::clone() const {
3476 LLVMContext &Context = getContext();
3477 UnreachableInst *New = new UnreachableInst(Context);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003478 New->SubclassOptionalData = SubclassOptionalData;
Nick Lewycky42fb7452009-09-27 07:38:41 +00003479 if (hasMetadata())
Devang Pateladd58652009-09-23 18:32:25 +00003480 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003481 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003482}