blob: 4ff253a7dbe6bb6d778589580dbab364ed4024c8 [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
463static Value *createMalloc(Instruction *InsertBefore, BasicBlock *InsertAtEnd,
Victor Hernandez788eaab2009-09-18 19:20:02 +0000464 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000465 Value *ArraySize, Function* MallocF,
466 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;
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000510 Value *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.
Victor Hernandez788eaab2009-09-18 19:20:02 +0000539Value *CallInst::CreateMalloc(Instruction *InsertBefore, const Type *IntPtrTy,
540 const Type *AllocTy, Value *ArraySize,
541 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.
Victor Hernandez788eaab2009-09-18 19:20:02 +0000554Value *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, const Type *IntPtrTy,
555 const Type *AllocTy, Value *ArraySize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000556 Function* MallocF, const Twine &Name) {
557 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy,
558 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000559}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000560
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000561//===----------------------------------------------------------------------===//
562// InvokeInst Implementation
563//===----------------------------------------------------------------------===//
564
565void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000566 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000567 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000568 Use *OL = OperandList;
569 OL[0] = Fn;
570 OL[1] = IfNormal;
571 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000572 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000573 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000574 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000575
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000576 assert(((NumArgs == FTy->getNumParams()) ||
577 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000578 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000579
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000580 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000581 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000582 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000583 "Invoking a function with a bad signature!");
584
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000585 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000586 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000587}
588
Misha Brukmanb1c93172005-04-21 23:48:37 +0000589InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000590 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000591 OperandTraits<InvokeInst>::op_end(this)
592 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000593 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000594 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000595 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596 Use *OL = OperandList, *InOL = II.OperandList;
597 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000598 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000599 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000600}
601
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000602BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
603 return getSuccessor(idx);
604}
605unsigned InvokeInst::getNumSuccessorsV() const {
606 return getNumSuccessors();
607}
608void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
609 return setSuccessor(idx, B);
610}
611
Devang Patelba3fa6c2008-09-23 23:03:40 +0000612bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000613 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000614 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000615 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000616 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000617 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000618}
619
Devang Patel4c758ea2008-09-25 21:00:45 +0000620void InvokeInst::addAttribute(unsigned i, Attributes attr) {
621 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000622 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000623 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000624}
625
Devang Patel4c758ea2008-09-25 21:00:45 +0000626void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
627 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000628 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000629 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000630}
631
Duncan Sands5208d1a2007-11-28 17:07:01 +0000632
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000633//===----------------------------------------------------------------------===//
634// ReturnInst Implementation
635//===----------------------------------------------------------------------===//
636
Chris Lattner2195fc42007-02-24 00:55:48 +0000637ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000638 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000639 OperandTraits<ReturnInst>::op_end(this) -
640 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000641 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000642 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000643 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000644 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000645}
646
Owen Anderson55f1c092009-08-13 21:58:54 +0000647ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
648 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000649 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
650 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000651 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000652 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000653}
Owen Anderson55f1c092009-08-13 21:58:54 +0000654ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
655 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000656 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
657 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000658 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000659 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000660}
Owen Anderson55f1c092009-08-13 21:58:54 +0000661ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
662 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000663 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000664}
665
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000666unsigned ReturnInst::getNumSuccessorsV() const {
667 return getNumSuccessors();
668}
669
Devang Patelae682fb2008-02-26 17:56:20 +0000670/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
671/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000672void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000673 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000674}
675
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000676BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000677 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000678 return 0;
679}
680
Devang Patel59643e52008-02-23 00:35:18 +0000681ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000682}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000683
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684//===----------------------------------------------------------------------===//
685// UnwindInst Implementation
686//===----------------------------------------------------------------------===//
687
Owen Anderson55f1c092009-08-13 21:58:54 +0000688UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
689 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
690 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000691}
Owen Anderson55f1c092009-08-13 21:58:54 +0000692UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
693 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
694 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000695}
696
697
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000698unsigned UnwindInst::getNumSuccessorsV() const {
699 return getNumSuccessors();
700}
701
702void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000703 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000704}
705
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000706BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000707 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000708 return 0;
709}
710
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000711//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000712// UnreachableInst Implementation
713//===----------------------------------------------------------------------===//
714
Owen Anderson55f1c092009-08-13 21:58:54 +0000715UnreachableInst::UnreachableInst(LLVMContext &Context,
716 Instruction *InsertBefore)
717 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
718 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000719}
Owen Anderson55f1c092009-08-13 21:58:54 +0000720UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
721 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
722 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000723}
724
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000725unsigned UnreachableInst::getNumSuccessorsV() const {
726 return getNumSuccessors();
727}
728
729void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000730 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000731}
732
733BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000734 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000735 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000736}
737
738//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000739// BranchInst Implementation
740//===----------------------------------------------------------------------===//
741
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000742void BranchInst::AssertOK() {
743 if (isConditional())
Owen Anderson55f1c092009-08-13 21:58:54 +0000744 assert(getCondition()->getType() == Type::getInt1Ty(getContext()) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000745 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000746}
747
Chris Lattner2195fc42007-02-24 00:55:48 +0000748BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000749 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000750 OperandTraits<BranchInst>::op_end(this) - 1,
751 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000752 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000753 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000754}
755BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
756 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000757 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000758 OperandTraits<BranchInst>::op_end(this) - 3,
759 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000760 Op<-1>() = IfTrue;
761 Op<-2>() = IfFalse;
762 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000763#ifndef NDEBUG
764 AssertOK();
765#endif
766}
767
768BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000769 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000770 OperandTraits<BranchInst>::op_end(this) - 1,
771 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000772 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000773 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000774}
775
776BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
777 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000778 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000779 OperandTraits<BranchInst>::op_end(this) - 3,
780 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000781 Op<-1>() = IfTrue;
782 Op<-2>() = IfFalse;
783 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000784#ifndef NDEBUG
785 AssertOK();
786#endif
787}
788
789
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000790BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000791 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000792 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
793 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000794 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000795 if (BI.getNumOperands() != 1) {
796 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000797 Op<-3>() = BI.Op<-3>();
798 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000799 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000800 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000801}
802
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000803
804Use* Use::getPrefix() {
805 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
806 if (PotentialPrefix.getOpaqueValue())
807 return 0;
808
809 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
810}
811
812BranchInst::~BranchInst() {
813 if (NumOperands == 1) {
814 if (Use *Prefix = OperandList->getPrefix()) {
815 Op<-1>() = 0;
816 //
817 // mark OperandList to have a special value for scrutiny
818 // by baseclass destructors and operator delete
819 OperandList = Prefix;
820 } else {
821 NumOperands = 3;
822 OperandList = op_begin();
823 }
824 }
825}
826
827
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000828BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
829 return getSuccessor(idx);
830}
831unsigned BranchInst::getNumSuccessorsV() const {
832 return getNumSuccessors();
833}
834void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
835 setSuccessor(idx, B);
836}
837
838
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000839//===----------------------------------------------------------------------===//
840// AllocationInst Implementation
841//===----------------------------------------------------------------------===//
842
Owen Andersonb6b25302009-07-14 23:09:55 +0000843static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000844 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000845 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000846 else {
847 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000848 "Passed basic block into allocation size parameter! Use other ctor");
Owen Anderson55f1c092009-08-13 21:58:54 +0000849 assert(Amt->getType() == Type::getInt32Ty(Context) &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000850 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000851 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000852 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000853}
854
Owen Anderson4fdeba92009-07-15 23:53:25 +0000855AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000856 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000857 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +0000858 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000859 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000860 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000861 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000862 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000863}
864
Owen Anderson4fdeba92009-07-15 23:53:25 +0000865AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000866 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000867 BasicBlock *InsertAtEnd)
Owen Anderson4056ca92009-07-29 22:17:13 +0000868 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000869 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000870 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000871 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000872 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000873}
874
Gordon Henriksen14a55692007-12-10 02:14:30 +0000875// Out of line virtual method, so the vtable, etc has a home.
876AllocationInst::~AllocationInst() {
877}
878
Dan Gohmanaa583d72008-03-24 16:55:58 +0000879void AllocationInst::setAlignment(unsigned Align) {
880 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
881 SubclassData = Log2_32(Align) + 1;
882 assert(getAlignment() == Align && "Alignment representation error!");
883}
884
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000885bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000886 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
887 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000888 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000889}
890
891const Type *AllocationInst::getAllocatedType() const {
892 return getType()->getElementType();
893}
894
Chris Lattner8b291e62008-11-26 02:54:17 +0000895/// isStaticAlloca - Return true if this alloca is in the entry block of the
896/// function and is a constant size. If so, the code generator will fold it
897/// into the prolog/epilog code, so it is basically free.
898bool AllocaInst::isStaticAlloca() const {
899 // Must be constant size.
900 if (!isa<ConstantInt>(getArraySize())) return false;
901
902 // Must be in the entry block.
903 const BasicBlock *Parent = getParent();
904 return Parent == &Parent->getParent()->front();
905}
906
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000907//===----------------------------------------------------------------------===//
908// FreeInst Implementation
909//===----------------------------------------------------------------------===//
910
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000911void FreeInst::AssertOK() {
912 assert(isa<PointerType>(getOperand(0)->getType()) &&
913 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000914}
915
916FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000917 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
918 Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000919 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000920}
921
922FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000923 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
924 Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000925 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000926}
927
928
929//===----------------------------------------------------------------------===//
930// LoadInst Implementation
931//===----------------------------------------------------------------------===//
932
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000933void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000934 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000935 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000936}
937
Daniel Dunbar4975db62009-07-25 04:41:11 +0000938LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000939 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000940 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000941 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000942 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000943 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000944 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000945}
946
Daniel Dunbar4975db62009-07-25 04:41:11 +0000947LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000948 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000949 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000950 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000951 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000952 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000953 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000954}
955
Daniel Dunbar4975db62009-07-25 04:41:11 +0000956LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000957 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000958 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000959 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000960 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000961 setAlignment(0);
962 AssertOK();
963 setName(Name);
964}
965
Daniel Dunbar4975db62009-07-25 04:41:11 +0000966LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000967 unsigned Align, Instruction *InsertBef)
968 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
969 Load, Ptr, InsertBef) {
970 setVolatile(isVolatile);
971 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000972 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000973 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000974}
975
Daniel Dunbar4975db62009-07-25 04:41:11 +0000976LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000977 unsigned Align, BasicBlock *InsertAE)
978 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
979 Load, Ptr, InsertAE) {
980 setVolatile(isVolatile);
981 setAlignment(Align);
982 AssertOK();
983 setName(Name);
984}
985
Daniel Dunbar4975db62009-07-25 04:41:11 +0000986LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000987 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000988 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000989 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000990 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000991 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000992 AssertOK();
993 setName(Name);
994}
995
Daniel Dunbar27096822009-08-11 18:11:15 +0000996
997
998LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
999 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1000 Load, Ptr, InsertBef) {
1001 setVolatile(false);
1002 setAlignment(0);
1003 AssertOK();
1004 if (Name && Name[0]) setName(Name);
1005}
1006
1007LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1008 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1009 Load, Ptr, InsertAE) {
1010 setVolatile(false);
1011 setAlignment(0);
1012 AssertOK();
1013 if (Name && Name[0]) setName(Name);
1014}
1015
1016LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1017 Instruction *InsertBef)
1018: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1019 Load, Ptr, InsertBef) {
1020 setVolatile(isVolatile);
1021 setAlignment(0);
1022 AssertOK();
1023 if (Name && Name[0]) setName(Name);
1024}
1025
1026LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1027 BasicBlock *InsertAE)
1028 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1029 Load, Ptr, InsertAE) {
1030 setVolatile(isVolatile);
1031 setAlignment(0);
1032 AssertOK();
1033 if (Name && Name[0]) setName(Name);
1034}
1035
Christopher Lamb84485702007-04-22 19:24:39 +00001036void LoadInst::setAlignment(unsigned Align) {
1037 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1038 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1039}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001040
1041//===----------------------------------------------------------------------===//
1042// StoreInst Implementation
1043//===----------------------------------------------------------------------===//
1044
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001045void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001046 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001047 assert(isa<PointerType>(getOperand(1)->getType()) &&
1048 "Ptr must have pointer type!");
1049 assert(getOperand(0)->getType() ==
1050 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001051 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001052}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001053
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001054
1055StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001056 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001057 OperandTraits<StoreInst>::op_begin(this),
1058 OperandTraits<StoreInst>::operands(this),
1059 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001060 Op<0>() = val;
1061 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001062 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001063 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001064 AssertOK();
1065}
1066
1067StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001068 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001069 OperandTraits<StoreInst>::op_begin(this),
1070 OperandTraits<StoreInst>::operands(this),
1071 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001072 Op<0>() = val;
1073 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001074 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001075 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001076 AssertOK();
1077}
1078
Misha Brukmanb1c93172005-04-21 23:48:37 +00001079StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001080 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001081 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001082 OperandTraits<StoreInst>::op_begin(this),
1083 OperandTraits<StoreInst>::operands(this),
1084 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001085 Op<0>() = val;
1086 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001087 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001088 setAlignment(0);
1089 AssertOK();
1090}
1091
1092StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1093 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001094 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001095 OperandTraits<StoreInst>::op_begin(this),
1096 OperandTraits<StoreInst>::operands(this),
1097 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001098 Op<0>() = val;
1099 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001100 setVolatile(isVolatile);
1101 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001102 AssertOK();
1103}
1104
Misha Brukmanb1c93172005-04-21 23:48:37 +00001105StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001106 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001107 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001108 OperandTraits<StoreInst>::op_begin(this),
1109 OperandTraits<StoreInst>::operands(this),
1110 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001111 Op<0>() = val;
1112 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001113 setVolatile(isVolatile);
1114 setAlignment(Align);
1115 AssertOK();
1116}
1117
1118StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001119 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001120 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001121 OperandTraits<StoreInst>::op_begin(this),
1122 OperandTraits<StoreInst>::operands(this),
1123 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001124 Op<0>() = val;
1125 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001126 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001127 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001128 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001129}
1130
Christopher Lamb84485702007-04-22 19:24:39 +00001131void StoreInst::setAlignment(unsigned Align) {
1132 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1133 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1134}
1135
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001136//===----------------------------------------------------------------------===//
1137// GetElementPtrInst Implementation
1138//===----------------------------------------------------------------------===//
1139
Christopher Lambedf07882007-12-17 01:12:55 +00001140static unsigned retrieveAddrSpace(const Value *Val) {
1141 return cast<PointerType>(Val->getType())->getAddressSpace();
1142}
1143
Gabor Greif21ba1842008-06-06 20:28:12 +00001144void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001145 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001146 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1147 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001148 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001149
Chris Lattner79807c3d2007-01-31 19:47:18 +00001150 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001151 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001152
1153 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001154}
1155
Daniel Dunbar4975db62009-07-25 04:41:11 +00001156void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001157 assert(NumOperands == 2 && "NumOperands not initialized?");
1158 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001159 OL[0] = Ptr;
1160 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001161
1162 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001163}
1164
Gabor Greiff6caff662008-05-10 08:32:32 +00001165GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001166 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001167 OperandTraits<GetElementPtrInst>::op_end(this)
1168 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001169 GEPI.getNumOperands()) {
1170 Use *OL = OperandList;
1171 Use *GEPIOL = GEPI.OperandList;
1172 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001173 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001174 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001175}
1176
Chris Lattner82981202005-05-03 05:43:30 +00001177GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001178 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001179 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001180 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001181 GetElementPtr,
1182 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1183 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001184 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001185}
1186
1187GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001188 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001189 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001190 checkType(getIndexedType(Ptr->getType(),Idx)),
1191 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001192 GetElementPtr,
1193 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1194 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001195 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001196}
1197
Chris Lattner6090a422009-03-09 04:46:40 +00001198/// getIndexedType - Returns the type of the element that would be accessed with
1199/// a gep instruction with the specified parameters.
1200///
1201/// The Idxs pointer should point to a continuous piece of memory containing the
1202/// indices, either as Value* or uint64_t.
1203///
1204/// A null type is returned if the indices are invalid for the specified
1205/// pointer type.
1206///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001207template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001208static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1209 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001210 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1211 if (!PTy) return 0; // Type isn't a pointer type!
1212 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001213
Chris Lattner6090a422009-03-09 04:46:40 +00001214 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001215 if (NumIdx == 0)
1216 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001217
1218 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001219 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1220 // that contain opaque types) under the assumption that it will be resolved to
1221 // a sane type later.
1222 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001223 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001224
Dan Gohman1ecaf452008-05-31 00:58:22 +00001225 unsigned CurIdx = 1;
1226 for (; CurIdx != NumIdx; ++CurIdx) {
1227 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1228 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001229 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001230 if (!CT->indexValid(Index)) return 0;
1231 Agg = CT->getTypeAtIndex(Index);
1232
1233 // If the new type forwards to another type, then it is in the middle
1234 // of being refined to another type (and hence, may have dropped all
1235 // references to what it was using before). So, use the new forwarded
1236 // type.
1237 if (const Type *Ty = Agg->getForwardedType())
1238 Agg = Ty;
1239 }
1240 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001241}
1242
Matthijs Kooijman04468622008-07-29 08:46:11 +00001243const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1244 Value* const *Idxs,
1245 unsigned NumIdx) {
1246 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1247}
1248
1249const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1250 uint64_t const *Idxs,
1251 unsigned NumIdx) {
1252 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1253}
1254
Chris Lattner82981202005-05-03 05:43:30 +00001255const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1256 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1257 if (!PTy) return 0; // Type isn't a pointer type!
1258
1259 // Check the pointer index.
1260 if (!PTy->indexValid(Idx)) return 0;
1261
Chris Lattnerc2233332005-05-03 16:44:45 +00001262 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001263}
1264
Chris Lattner45f15572007-04-14 00:12:57 +00001265
1266/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1267/// zeros. If so, the result pointer and the first operand have the same
1268/// value, just potentially different types.
1269bool GetElementPtrInst::hasAllZeroIndices() const {
1270 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1271 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1272 if (!CI->isZero()) return false;
1273 } else {
1274 return false;
1275 }
1276 }
1277 return true;
1278}
1279
Chris Lattner27058292007-04-27 20:35:56 +00001280/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1281/// constant integers. If so, the result pointer and the first operand have
1282/// a constant offset between them.
1283bool GetElementPtrInst::hasAllConstantIndices() const {
1284 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1285 if (!isa<ConstantInt>(getOperand(i)))
1286 return false;
1287 }
1288 return true;
1289}
1290
Dan Gohman1b849082009-09-07 23:54:19 +00001291void GetElementPtrInst::setIsInBounds(bool B) {
1292 cast<GEPOperator>(this)->setIsInBounds(B);
1293}
Chris Lattner45f15572007-04-14 00:12:57 +00001294
Nick Lewycky28a5f252009-09-27 21:33:04 +00001295bool GetElementPtrInst::isInBounds() const {
1296 return cast<GEPOperator>(this)->isInBounds();
1297}
1298
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001299//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001300// ExtractElementInst Implementation
1301//===----------------------------------------------------------------------===//
1302
1303ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001304 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001305 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001306 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001307 ExtractElement,
1308 OperandTraits<ExtractElementInst>::op_begin(this),
1309 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001310 assert(isValidOperands(Val, Index) &&
1311 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001312 Op<0>() = Val;
1313 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001314 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001315}
1316
1317ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001318 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001319 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001320 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001321 ExtractElement,
1322 OperandTraits<ExtractElementInst>::op_begin(this),
1323 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001324 assert(isValidOperands(Val, Index) &&
1325 "Invalid extractelement instruction operands!");
1326
Gabor Greif2d3024d2008-05-26 21:33:52 +00001327 Op<0>() = Val;
1328 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001329 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001330}
1331
Chris Lattner65511ff2006-10-05 06:24:58 +00001332
Chris Lattner54865b32006-04-08 04:05:48 +00001333bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Owen Anderson55f1c092009-08-13 21:58:54 +00001334 if (!isa<VectorType>(Val->getType()) ||
1335 Index->getType() != Type::getInt32Ty(Val->getContext()))
Chris Lattner54865b32006-04-08 04:05:48 +00001336 return false;
1337 return true;
1338}
1339
1340
Robert Bocchino23004482006-01-10 19:05:34 +00001341//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001342// InsertElementInst Implementation
1343//===----------------------------------------------------------------------===//
1344
Chris Lattner54865b32006-04-08 04:05:48 +00001345InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001346 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001347 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001348 : Instruction(Vec->getType(), InsertElement,
1349 OperandTraits<InsertElementInst>::op_begin(this),
1350 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001351 assert(isValidOperands(Vec, Elt, Index) &&
1352 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001353 Op<0>() = Vec;
1354 Op<1>() = Elt;
1355 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001356 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001357}
1358
Chris Lattner54865b32006-04-08 04:05:48 +00001359InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001360 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001361 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001362 : Instruction(Vec->getType(), InsertElement,
1363 OperandTraits<InsertElementInst>::op_begin(this),
1364 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001365 assert(isValidOperands(Vec, Elt, Index) &&
1366 "Invalid insertelement instruction operands!");
1367
Gabor Greif2d3024d2008-05-26 21:33:52 +00001368 Op<0>() = Vec;
1369 Op<1>() = Elt;
1370 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001371 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001372}
1373
Chris Lattner54865b32006-04-08 04:05:48 +00001374bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1375 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001376 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001377 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001378
Reid Spencerd84d35b2007-02-15 02:26:10 +00001379 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001380 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001381
Owen Anderson55f1c092009-08-13 21:58:54 +00001382 if (Index->getType() != Type::getInt32Ty(Vec->getContext()))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001383 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001384 return true;
1385}
1386
1387
Robert Bocchinoca27f032006-01-17 20:07:22 +00001388//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001389// ShuffleVectorInst Implementation
1390//===----------------------------------------------------------------------===//
1391
1392ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001393 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001394 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001395: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001396 cast<VectorType>(Mask->getType())->getNumElements()),
1397 ShuffleVector,
1398 OperandTraits<ShuffleVectorInst>::op_begin(this),
1399 OperandTraits<ShuffleVectorInst>::operands(this),
1400 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001401 assert(isValidOperands(V1, V2, Mask) &&
1402 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001403 Op<0>() = V1;
1404 Op<1>() = V2;
1405 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001406 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001407}
1408
1409ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001410 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001411 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001412: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1413 cast<VectorType>(Mask->getType())->getNumElements()),
1414 ShuffleVector,
1415 OperandTraits<ShuffleVectorInst>::op_begin(this),
1416 OperandTraits<ShuffleVectorInst>::operands(this),
1417 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001418 assert(isValidOperands(V1, V2, Mask) &&
1419 "Invalid shuffle vector instruction operands!");
1420
Gabor Greif2d3024d2008-05-26 21:33:52 +00001421 Op<0>() = V1;
1422 Op<1>() = V2;
1423 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001424 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001425}
1426
Mon P Wang25f01062008-11-10 04:46:22 +00001427bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001428 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001429 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001430 return false;
1431
1432 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1433 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Owen Anderson55f1c092009-08-13 21:58:54 +00001434 MaskTy->getElementType() != Type::getInt32Ty(V1->getContext()))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001435 return false;
1436 return true;
1437}
1438
Chris Lattnerf724e342008-03-02 05:28:33 +00001439/// getMaskValue - Return the index from the shuffle mask for the specified
1440/// output result. This is either -1 if the element is undef or a number less
1441/// than 2*numelements.
1442int ShuffleVectorInst::getMaskValue(unsigned i) const {
1443 const Constant *Mask = cast<Constant>(getOperand(2));
1444 if (isa<UndefValue>(Mask)) return -1;
1445 if (isa<ConstantAggregateZero>(Mask)) return 0;
1446 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1447 assert(i < MaskCV->getNumOperands() && "Index out of range");
1448
1449 if (isa<UndefValue>(MaskCV->getOperand(i)))
1450 return -1;
1451 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1452}
1453
Dan Gohman12fce772008-05-15 19:50:34 +00001454//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001455// InsertValueInst Class
1456//===----------------------------------------------------------------------===//
1457
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001458void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001459 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001460 assert(NumOperands == 2 && "NumOperands not initialized?");
1461 Op<0>() = Agg;
1462 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001463
Dan Gohman1ecaf452008-05-31 00:58:22 +00001464 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001465 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001466}
1467
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001468void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001469 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001470 assert(NumOperands == 2 && "NumOperands not initialized?");
1471 Op<0>() = Agg;
1472 Op<1>() = Val;
1473
1474 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001475 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001476}
1477
1478InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001479 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001480 OperandTraits<InsertValueInst>::op_begin(this), 2),
1481 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001482 Op<0>() = IVI.getOperand(0);
1483 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001484 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001485}
1486
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001487InsertValueInst::InsertValueInst(Value *Agg,
1488 Value *Val,
1489 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001490 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001491 Instruction *InsertBefore)
1492 : Instruction(Agg->getType(), InsertValue,
1493 OperandTraits<InsertValueInst>::op_begin(this),
1494 2, InsertBefore) {
1495 init(Agg, Val, Idx, Name);
1496}
1497
1498InsertValueInst::InsertValueInst(Value *Agg,
1499 Value *Val,
1500 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001501 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001502 BasicBlock *InsertAtEnd)
1503 : Instruction(Agg->getType(), InsertValue,
1504 OperandTraits<InsertValueInst>::op_begin(this),
1505 2, InsertAtEnd) {
1506 init(Agg, Val, Idx, Name);
1507}
1508
Dan Gohman0752bff2008-05-23 00:36:11 +00001509//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001510// ExtractValueInst Class
1511//===----------------------------------------------------------------------===//
1512
Gabor Greifcbcc4952008-06-06 21:06:32 +00001513void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001514 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001515 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001516
Dan Gohman1ecaf452008-05-31 00:58:22 +00001517 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001518 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001519}
1520
Daniel Dunbar4975db62009-07-25 04:41:11 +00001521void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001522 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001523
1524 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001525 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001526}
1527
1528ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001529 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001530 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001531 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001532}
1533
Dan Gohman12fce772008-05-15 19:50:34 +00001534// getIndexedType - Returns the type of the element that would be extracted
1535// with an extractvalue instruction with the specified parameters.
1536//
1537// A null type is returned if the indices are invalid for the specified
1538// pointer type.
1539//
1540const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001541 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001542 unsigned NumIdx) {
1543 unsigned CurIdx = 0;
1544 for (; CurIdx != NumIdx; ++CurIdx) {
1545 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001546 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1547 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001548 if (!CT->indexValid(Index)) return 0;
1549 Agg = CT->getTypeAtIndex(Index);
1550
1551 // If the new type forwards to another type, then it is in the middle
1552 // of being refined to another type (and hence, may have dropped all
1553 // references to what it was using before). So, use the new forwarded
1554 // type.
1555 if (const Type *Ty = Agg->getForwardedType())
1556 Agg = Ty;
1557 }
1558 return CurIdx == NumIdx ? Agg : 0;
1559}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001560
Dan Gohman4a3125b2008-06-17 21:07:55 +00001561const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001562 unsigned Idx) {
1563 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001564}
1565
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001566//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001567// BinaryOperator Class
1568//===----------------------------------------------------------------------===//
1569
Dan Gohmana5b96452009-06-04 22:49:04 +00001570/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1571/// type is floating-point, to help provide compatibility with an older API.
1572///
1573static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1574 const Type *Ty) {
1575 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1576 if (Ty->isFPOrFPVector()) {
1577 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1578 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1579 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1580 }
1581 return iType;
1582}
1583
Chris Lattner2195fc42007-02-24 00:55:48 +00001584BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001585 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001586 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001587 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001588 OperandTraits<BinaryOperator>::op_begin(this),
1589 OperandTraits<BinaryOperator>::operands(this),
1590 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001591 Op<0>() = S1;
1592 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001593 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001594 setName(Name);
1595}
1596
1597BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001598 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001599 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001600 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001601 OperandTraits<BinaryOperator>::op_begin(this),
1602 OperandTraits<BinaryOperator>::operands(this),
1603 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001604 Op<0>() = S1;
1605 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001606 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001607 setName(Name);
1608}
1609
1610
1611void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001612 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001613 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001614 assert(LHS->getType() == RHS->getType() &&
1615 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001616#ifndef NDEBUG
1617 switch (iType) {
1618 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001619 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001620 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001621 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001622 assert(getType()->isIntOrIntVector() &&
1623 "Tried to create an integer operation on a non-integer type!");
1624 break;
1625 case FAdd: case FSub:
1626 case FMul:
1627 assert(getType() == LHS->getType() &&
1628 "Arithmetic operation should return same type as operands!");
1629 assert(getType()->isFPOrFPVector() &&
1630 "Tried to create a floating-point operation on a "
1631 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001632 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001633 case UDiv:
1634 case SDiv:
1635 assert(getType() == LHS->getType() &&
1636 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001637 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1638 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001639 "Incorrect operand type (not integer) for S/UDIV");
1640 break;
1641 case FDiv:
1642 assert(getType() == LHS->getType() &&
1643 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001644 assert(getType()->isFPOrFPVector() &&
1645 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001646 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001647 case URem:
1648 case SRem:
1649 assert(getType() == LHS->getType() &&
1650 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001651 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1652 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001653 "Incorrect operand type (not integer) for S/UREM");
1654 break;
1655 case FRem:
1656 assert(getType() == LHS->getType() &&
1657 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001658 assert(getType()->isFPOrFPVector() &&
1659 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001660 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001661 case Shl:
1662 case LShr:
1663 case AShr:
1664 assert(getType() == LHS->getType() &&
1665 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001666 assert((getType()->isInteger() ||
1667 (isa<VectorType>(getType()) &&
1668 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1669 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001670 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001671 case And: case Or:
1672 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001673 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001674 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001675 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001676 (isa<VectorType>(getType()) &&
1677 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001678 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001679 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001680 default:
1681 break;
1682 }
1683#endif
1684}
1685
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001686BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001687 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001688 Instruction *InsertBefore) {
1689 assert(S1->getType() == S2->getType() &&
1690 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001691 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001692}
1693
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001694BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001695 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001696 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001697 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001698 InsertAtEnd->getInstList().push_back(Res);
1699 return Res;
1700}
1701
Dan Gohman5476cfd2009-08-12 16:23:25 +00001702BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001703 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001704 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001705 return new BinaryOperator(Instruction::Sub,
1706 zero, Op,
1707 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001708}
1709
Dan Gohman5476cfd2009-08-12 16:23:25 +00001710BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001711 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001712 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001713 return new BinaryOperator(Instruction::Sub,
1714 zero, Op,
1715 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001716}
1717
Dan Gohman5476cfd2009-08-12 16:23:25 +00001718BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001719 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001720 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001721 return new BinaryOperator(Instruction::FSub,
1722 zero, Op,
1723 Op->getType(), Name, InsertBefore);
1724}
1725
Dan Gohman5476cfd2009-08-12 16:23:25 +00001726BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001727 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001728 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001729 return new BinaryOperator(Instruction::FSub,
1730 zero, Op,
1731 Op->getType(), Name, InsertAtEnd);
1732}
1733
Dan Gohman5476cfd2009-08-12 16:23:25 +00001734BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001735 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001736 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001737 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001738 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001739 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001740 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001741 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001742 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001743 }
1744
1745 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001746 Op->getType(), Name, InsertBefore);
1747}
1748
Dan Gohman5476cfd2009-08-12 16:23:25 +00001749BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001750 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001751 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001752 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001753 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001754 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001755 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001756 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001757 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001758 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001759 }
1760
1761 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001762 Op->getType(), Name, InsertAtEnd);
1763}
1764
1765
1766// isConstantAllOnes - Helper function for several functions below
1767static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001768 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1769 return CI->isAllOnesValue();
1770 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1771 return CV->isAllOnesValue();
1772 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001773}
1774
Owen Andersonbb2501b2009-07-13 22:18:28 +00001775bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001776 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1777 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001778 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1779 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001780 return false;
1781}
1782
Owen Andersonbb2501b2009-07-13 22:18:28 +00001783bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001784 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1785 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001786 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001787 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001788 return false;
1789}
1790
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001791bool BinaryOperator::isNot(const Value *V) {
1792 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1793 return (Bop->getOpcode() == Instruction::Xor &&
1794 (isConstantAllOnes(Bop->getOperand(1)) ||
1795 isConstantAllOnes(Bop->getOperand(0))));
1796 return false;
1797}
1798
Chris Lattner2c7d1772005-04-24 07:28:37 +00001799Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001800 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001801}
1802
Chris Lattner2c7d1772005-04-24 07:28:37 +00001803const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1804 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001805}
1806
Dan Gohmana5b96452009-06-04 22:49:04 +00001807Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001808 return cast<BinaryOperator>(BinOp)->getOperand(1);
1809}
1810
1811const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1812 return getFNegArgument(const_cast<Value*>(BinOp));
1813}
1814
Chris Lattner2c7d1772005-04-24 07:28:37 +00001815Value *BinaryOperator::getNotArgument(Value *BinOp) {
1816 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1817 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1818 Value *Op0 = BO->getOperand(0);
1819 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001820 if (isConstantAllOnes(Op0)) return Op1;
1821
1822 assert(isConstantAllOnes(Op1));
1823 return Op0;
1824}
1825
Chris Lattner2c7d1772005-04-24 07:28:37 +00001826const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1827 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001828}
1829
1830
1831// swapOperands - Exchange the two operands to this instruction. This
1832// instruction is safe to use on any binary instruction and does not
1833// modify the semantics of the instruction. If the instruction is
1834// order dependent (SetLT f.e.) the opcode is changed.
1835//
1836bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001837 if (!isCommutative())
1838 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001839 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001840 return false;
1841}
1842
Dan Gohman1b849082009-09-07 23:54:19 +00001843void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1844 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1845}
1846
1847void BinaryOperator::setHasNoSignedWrap(bool b) {
1848 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1849}
1850
1851void BinaryOperator::setIsExact(bool b) {
1852 cast<SDivOperator>(this)->setIsExact(b);
1853}
1854
Nick Lewycky28a5f252009-09-27 21:33:04 +00001855bool BinaryOperator::hasNoUnsignedWrap() const {
1856 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1857}
1858
1859bool BinaryOperator::hasNoSignedWrap() const {
1860 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1861}
1862
1863bool BinaryOperator::isExact() const {
1864 return cast<SDivOperator>(this)->isExact();
1865}
1866
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001867//===----------------------------------------------------------------------===//
1868// CastInst Class
1869//===----------------------------------------------------------------------===//
1870
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001871// Just determine if this cast only deals with integral->integral conversion.
1872bool CastInst::isIntegerCast() const {
1873 switch (getOpcode()) {
1874 default: return false;
1875 case Instruction::ZExt:
1876 case Instruction::SExt:
1877 case Instruction::Trunc:
1878 return true;
1879 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001880 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001881 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001882}
1883
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001884bool CastInst::isLosslessCast() const {
1885 // Only BitCast can be lossless, exit fast if we're not BitCast
1886 if (getOpcode() != Instruction::BitCast)
1887 return false;
1888
1889 // Identity cast is always lossless
1890 const Type* SrcTy = getOperand(0)->getType();
1891 const Type* DstTy = getType();
1892 if (SrcTy == DstTy)
1893 return true;
1894
Reid Spencer8d9336d2006-12-31 05:26:44 +00001895 // Pointer to pointer is always lossless.
1896 if (isa<PointerType>(SrcTy))
1897 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001898 return false; // Other types have no identity values
1899}
1900
1901/// This function determines if the CastInst does not require any bits to be
1902/// changed in order to effect the cast. Essentially, it identifies cases where
1903/// no code gen is necessary for the cast, hence the name no-op cast. For
1904/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001905/// # bitcast i32* %x to i8*
1906/// # bitcast <2 x i32> %x to <4 x i16>
1907/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001908/// @brief Determine if a cast is a no-op.
1909bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1910 switch (getOpcode()) {
1911 default:
1912 assert(!"Invalid CastOp");
1913 case Instruction::Trunc:
1914 case Instruction::ZExt:
1915 case Instruction::SExt:
1916 case Instruction::FPTrunc:
1917 case Instruction::FPExt:
1918 case Instruction::UIToFP:
1919 case Instruction::SIToFP:
1920 case Instruction::FPToUI:
1921 case Instruction::FPToSI:
1922 return false; // These always modify bits
1923 case Instruction::BitCast:
1924 return true; // BitCast never modifies bits.
1925 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001926 return IntPtrTy->getScalarSizeInBits() ==
1927 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001928 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001929 return IntPtrTy->getScalarSizeInBits() ==
1930 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001931 }
1932}
1933
1934/// This function determines if a pair of casts can be eliminated and what
1935/// opcode should be used in the elimination. This assumes that there are two
1936/// instructions like this:
1937/// * %F = firstOpcode SrcTy %x to MidTy
1938/// * %S = secondOpcode MidTy %F to DstTy
1939/// The function returns a resultOpcode so these two casts can be replaced with:
1940/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1941/// If no such cast is permited, the function returns 0.
1942unsigned CastInst::isEliminableCastPair(
1943 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1944 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1945{
1946 // Define the 144 possibilities for these two cast instructions. The values
1947 // in this matrix determine what to do in a given situation and select the
1948 // case in the switch below. The rows correspond to firstOp, the columns
1949 // correspond to secondOp. In looking at the table below, keep in mind
1950 // the following cast properties:
1951 //
1952 // Size Compare Source Destination
1953 // Operator Src ? Size Type Sign Type Sign
1954 // -------- ------------ ------------------- ---------------------
1955 // TRUNC > Integer Any Integral Any
1956 // ZEXT < Integral Unsigned Integer Any
1957 // SEXT < Integral Signed Integer Any
1958 // FPTOUI n/a FloatPt n/a Integral Unsigned
1959 // FPTOSI n/a FloatPt n/a Integral Signed
1960 // UITOFP n/a Integral Unsigned FloatPt n/a
1961 // SITOFP n/a Integral Signed FloatPt n/a
1962 // FPTRUNC > FloatPt n/a FloatPt n/a
1963 // FPEXT < FloatPt n/a FloatPt n/a
1964 // PTRTOINT n/a Pointer n/a Integral Unsigned
1965 // INTTOPTR n/a Integral Unsigned Pointer n/a
1966 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001967 //
1968 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001969 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1970 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001971 // of the produced value (we no longer know the top-part is all zeros).
1972 // Further this conversion is often much more expensive for typical hardware,
1973 // and causes issues when building libgcc. We disallow fptosi+sext for the
1974 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001975 const unsigned numCastOps =
1976 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1977 static const uint8_t CastResults[numCastOps][numCastOps] = {
1978 // T F F U S F F P I B -+
1979 // R Z S P P I I T P 2 N T |
1980 // U E E 2 2 2 2 R E I T C +- secondOp
1981 // N X X U S F F N X N 2 V |
1982 // C T T I I P P C T T P T -+
1983 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1984 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1985 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001986 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1987 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001988 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1989 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1990 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1991 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1992 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1993 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1994 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1995 };
1996
1997 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1998 [secondOp-Instruction::CastOpsBegin];
1999 switch (ElimCase) {
2000 case 0:
2001 // categorically disallowed
2002 return 0;
2003 case 1:
2004 // allowed, use first cast's opcode
2005 return firstOp;
2006 case 2:
2007 // allowed, use second cast's opcode
2008 return secondOp;
2009 case 3:
2010 // no-op cast in second op implies firstOp as long as the DestTy
2011 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00002012 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002013 return firstOp;
2014 return 0;
2015 case 4:
2016 // no-op cast in second op implies firstOp as long as the DestTy
2017 // is floating point
2018 if (DstTy->isFloatingPoint())
2019 return firstOp;
2020 return 0;
2021 case 5:
2022 // no-op cast in first op implies secondOp as long as the SrcTy
2023 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00002024 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002025 return secondOp;
2026 return 0;
2027 case 6:
2028 // no-op cast in first op implies secondOp as long as the SrcTy
2029 // is a floating point
2030 if (SrcTy->isFloatingPoint())
2031 return secondOp;
2032 return 0;
2033 case 7: {
2034 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002035 if (!IntPtrTy)
2036 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002037 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2038 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002039 if (MidSize >= PtrSize)
2040 return Instruction::BitCast;
2041 return 0;
2042 }
2043 case 8: {
2044 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2045 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2046 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002047 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2048 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002049 if (SrcSize == DstSize)
2050 return Instruction::BitCast;
2051 else if (SrcSize < DstSize)
2052 return firstOp;
2053 return secondOp;
2054 }
2055 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2056 return Instruction::ZExt;
2057 case 10:
2058 // fpext followed by ftrunc is allowed if the bit size returned to is
2059 // the same as the original, in which case its just a bitcast
2060 if (SrcTy == DstTy)
2061 return Instruction::BitCast;
2062 return 0; // If the types are not the same we can't eliminate it.
2063 case 11:
2064 // bitcast followed by ptrtoint is allowed as long as the bitcast
2065 // is a pointer to pointer cast.
2066 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
2067 return secondOp;
2068 return 0;
2069 case 12:
2070 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2071 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
2072 return firstOp;
2073 return 0;
2074 case 13: {
2075 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002076 if (!IntPtrTy)
2077 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002078 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2079 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2080 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002081 if (SrcSize <= PtrSize && SrcSize == DstSize)
2082 return Instruction::BitCast;
2083 return 0;
2084 }
2085 case 99:
2086 // cast combination can't happen (error in input). This is for all cases
2087 // where the MidTy is not the same for the two cast instructions.
2088 assert(!"Invalid Cast Combination");
2089 return 0;
2090 default:
2091 assert(!"Error in CastResults table!!!");
2092 return 0;
2093 }
2094 return 0;
2095}
2096
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002097CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002098 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002099 // Construct and return the appropriate CastInst subclass
2100 switch (op) {
2101 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2102 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2103 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2104 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2105 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2106 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2107 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2108 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2109 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2110 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2111 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2112 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2113 default:
2114 assert(!"Invalid opcode provided");
2115 }
2116 return 0;
2117}
2118
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002119CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002120 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002121 // Construct and return the appropriate CastInst subclass
2122 switch (op) {
2123 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2124 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2125 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2126 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2127 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2128 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2129 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2130 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2131 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2132 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2133 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2134 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2135 default:
2136 assert(!"Invalid opcode provided");
2137 }
2138 return 0;
2139}
2140
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002141CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002142 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002143 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002144 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002145 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2146 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002147}
2148
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002149CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002150 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002151 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002152 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002153 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2154 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002155}
2156
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002157CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002158 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002159 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002160 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002161 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2162 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002163}
2164
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002165CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002166 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002167 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002168 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002169 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2170 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002171}
2172
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002173CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002174 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002175 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002176 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002177 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2178 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002179}
2180
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002181CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002182 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002183 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002184 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002185 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2186 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002187}
2188
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002189CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002190 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002191 BasicBlock *InsertAtEnd) {
2192 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002193 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002194 "Invalid cast");
2195
Chris Lattner03c49532007-01-15 02:27:26 +00002196 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002197 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2198 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002199}
2200
2201/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002202CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002203 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002204 Instruction *InsertBefore) {
2205 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002206 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002207 "Invalid cast");
2208
Chris Lattner03c49532007-01-15 02:27:26 +00002209 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002210 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2211 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002212}
2213
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002214CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002215 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002216 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002217 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002218 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2219 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002220 Instruction::CastOps opcode =
2221 (SrcBits == DstBits ? Instruction::BitCast :
2222 (SrcBits > DstBits ? Instruction::Trunc :
2223 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002224 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002225}
2226
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002227CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002228 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002229 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002230 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2231 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002232 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2233 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002234 Instruction::CastOps opcode =
2235 (SrcBits == DstBits ? Instruction::BitCast :
2236 (SrcBits > DstBits ? Instruction::Trunc :
2237 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002238 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002239}
2240
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002241CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002242 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002243 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002244 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002245 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002246 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2247 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002248 Instruction::CastOps opcode =
2249 (SrcBits == DstBits ? Instruction::BitCast :
2250 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002251 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002252}
2253
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002254CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002255 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002256 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002257 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002258 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002259 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2260 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002261 Instruction::CastOps opcode =
2262 (SrcBits == DstBits ? Instruction::BitCast :
2263 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002264 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002265}
2266
Duncan Sands55e50902008-01-06 10:12:28 +00002267// Check whether it is valid to call getCastOpcode for these types.
2268// This routine must be kept in sync with getCastOpcode.
2269bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2270 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2271 return false;
2272
2273 if (SrcTy == DestTy)
2274 return true;
2275
2276 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002277 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2278 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002279
2280 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002281 if (DestTy->isInteger()) { // Casting to integral
2282 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002283 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002284 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002285 return true;
2286 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002287 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002288 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002289 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002290 return isa<PointerType>(SrcTy);
2291 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002292 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2293 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002294 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002295 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002296 return true;
2297 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002298 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002299 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002300 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002301 return false;
2302 }
2303 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002304 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002305 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002306 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002307 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002308 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002309 return DestPTy->getBitWidth() == SrcBits;
2310 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002311 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2312 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002313 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002314 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002315 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002316 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002317 return false;
2318 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002319 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002320 return false;
2321 }
2322}
2323
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324// Provide a way to get a "cast" where the cast opcode is inferred from the
2325// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002326// logic in the castIsValid function below. This axiom should hold:
2327// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2328// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002329// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002330// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002331Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002332CastInst::getCastOpcode(
2333 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002334 // Get the bit sizes, we'll need these
2335 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002336 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2337 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002338
Duncan Sands55e50902008-01-06 10:12:28 +00002339 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2340 "Only first class types are castable!");
2341
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002342 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002343 if (DestTy->isInteger()) { // Casting to integral
2344 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002345 if (DestBits < SrcBits)
2346 return Trunc; // int -> smaller int
2347 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002348 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002349 return SExt; // signed -> SEXT
2350 else
2351 return ZExt; // unsigned -> ZEXT
2352 } else {
2353 return BitCast; // Same size, No-op cast
2354 }
2355 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002356 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357 return FPToSI; // FP -> sint
2358 else
2359 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002360 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002361 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002362 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002363 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002364 return BitCast; // Same size, no-op cast
2365 } else {
2366 assert(isa<PointerType>(SrcTy) &&
2367 "Casting from a value that is not first-class type");
2368 return PtrToInt; // ptr -> int
2369 }
2370 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002371 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002372 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002373 return SIToFP; // sint -> FP
2374 else
2375 return UIToFP; // uint -> FP
2376 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2377 if (DestBits < SrcBits) {
2378 return FPTrunc; // FP -> smaller FP
2379 } else if (DestBits > SrcBits) {
2380 return FPExt; // FP -> larger FP
2381 } else {
2382 return BitCast; // same size, no-op cast
2383 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002384 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002385 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002386 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002387 PTy = NULL;
2388 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002389 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002390 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002392 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2393 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002394 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002395 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002396 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002397 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002399 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002401 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402 }
2403 } else if (isa<PointerType>(DestTy)) {
2404 if (isa<PointerType>(SrcTy)) {
2405 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002406 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002407 return IntToPtr; // int -> ptr
2408 } else {
2409 assert(!"Casting pointer to other than pointer or int");
2410 }
2411 } else {
2412 assert(!"Casting to type that is not first-class");
2413 }
2414
2415 // If we fall through to here we probably hit an assertion cast above
2416 // and assertions are not turned on. Anything we return is an error, so
2417 // BitCast is as good a choice as any.
2418 return BitCast;
2419}
2420
2421//===----------------------------------------------------------------------===//
2422// CastInst SubClass Constructors
2423//===----------------------------------------------------------------------===//
2424
2425/// Check that the construction parameters for a CastInst are correct. This
2426/// could be broken out into the separate constructors but it is useful to have
2427/// it in one place and to eliminate the redundant code for getting the sizes
2428/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002429bool
2430CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002431
2432 // Check for type sanity on the arguments
2433 const Type *SrcTy = S->getType();
2434 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2435 return false;
2436
2437 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002438 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2439 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002440
2441 // Switch on the opcode provided
2442 switch (op) {
2443 default: return false; // This is an input error
2444 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002445 return SrcTy->isIntOrIntVector() &&
2446 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002448 return SrcTy->isIntOrIntVector() &&
2449 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002451 return SrcTy->isIntOrIntVector() &&
2452 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002454 return SrcTy->isFPOrFPVector() &&
2455 DstTy->isFPOrFPVector() &&
2456 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002457 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002458 return SrcTy->isFPOrFPVector() &&
2459 DstTy->isFPOrFPVector() &&
2460 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002461 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002462 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002463 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2464 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002465 return SVTy->getElementType()->isIntOrIntVector() &&
2466 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002467 SVTy->getNumElements() == DVTy->getNumElements();
2468 }
2469 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002470 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002473 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2474 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002475 return SVTy->getElementType()->isFPOrFPVector() &&
2476 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002477 SVTy->getNumElements() == DVTy->getNumElements();
2478 }
2479 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002480 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002481 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002482 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002483 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002484 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002485 case Instruction::BitCast:
2486 // BitCast implies a no-op cast of type only. No bits change.
2487 // However, you can't cast pointers to anything but pointers.
2488 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2489 return false;
2490
Duncan Sands55e50902008-01-06 10:12:28 +00002491 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002492 // these cases, the cast is okay if the source and destination bit widths
2493 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002494 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002495 }
2496}
2497
2498TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002499 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002500) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002501 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502}
2503
2504TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002505 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002506) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002507 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002508}
2509
2510ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002511 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002512) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002513 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002514}
2515
2516ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002517 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002519 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002520}
2521SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002522 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002523) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002524 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002525}
2526
Jeff Cohencc08c832006-12-02 02:22:01 +00002527SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002528 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002529) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002530 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002531}
2532
2533FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002534 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002535) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002536 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002537}
2538
2539FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002540 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002542 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002543}
2544
2545FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002546 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002548 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002549}
2550
2551FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002552 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002553) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002554 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002555}
2556
2557UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002558 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002559) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002560 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561}
2562
2563UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002564 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002565) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002566 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002567}
2568
2569SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002570 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002572 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002573}
2574
2575SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002576 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002578 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579}
2580
2581FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002582 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002584 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585}
2586
2587FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002588 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002589) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002590 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591}
2592
2593FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002594 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002595) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002596 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002597}
2598
2599FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002600 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002601) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002602 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002603}
2604
2605PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002606 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002607) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002608 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609}
2610
2611PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002612 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002613) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002614 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002615}
2616
2617IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002618 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002619) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002620 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002621}
2622
2623IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002624 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002625) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002626 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002627}
2628
2629BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002630 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002631) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002632 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002633}
2634
2635BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002636 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002638 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002639}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002640
2641//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002642// CmpInst Classes
2643//===----------------------------------------------------------------------===//
2644
Nate Begemand2195702008-05-12 19:01:56 +00002645CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002646 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002647 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002648 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002649 OperandTraits<CmpInst>::op_begin(this),
2650 OperandTraits<CmpInst>::operands(this),
2651 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002652 Op<0>() = LHS;
2653 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002654 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002655 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002656}
Gabor Greiff6caff662008-05-10 08:32:32 +00002657
Nate Begemand2195702008-05-12 19:01:56 +00002658CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002659 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002660 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002661 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002662 OperandTraits<CmpInst>::op_begin(this),
2663 OperandTraits<CmpInst>::operands(this),
2664 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002665 Op<0>() = LHS;
2666 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002667 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002668 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002669}
2670
2671CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002672CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002673 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002674 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002675 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002676 if (InsertBefore)
2677 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2678 S1, S2, Name);
2679 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002680 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002681 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002682 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002683
2684 if (InsertBefore)
2685 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2686 S1, S2, Name);
2687 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002688 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002689 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002690}
2691
2692CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002693CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002694 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002695 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002696 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2697 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002698 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002699 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2700 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002701}
2702
2703void CmpInst::swapOperands() {
2704 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2705 IC->swapOperands();
2706 else
2707 cast<FCmpInst>(this)->swapOperands();
2708}
2709
2710bool CmpInst::isCommutative() {
2711 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2712 return IC->isCommutative();
2713 return cast<FCmpInst>(this)->isCommutative();
2714}
2715
2716bool CmpInst::isEquality() {
2717 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2718 return IC->isEquality();
2719 return cast<FCmpInst>(this)->isEquality();
2720}
2721
2722
Dan Gohman4e724382008-05-31 02:47:54 +00002723CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002724 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002725 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002726 case ICMP_EQ: return ICMP_NE;
2727 case ICMP_NE: return ICMP_EQ;
2728 case ICMP_UGT: return ICMP_ULE;
2729 case ICMP_ULT: return ICMP_UGE;
2730 case ICMP_UGE: return ICMP_ULT;
2731 case ICMP_ULE: return ICMP_UGT;
2732 case ICMP_SGT: return ICMP_SLE;
2733 case ICMP_SLT: return ICMP_SGE;
2734 case ICMP_SGE: return ICMP_SLT;
2735 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002736
Dan Gohman4e724382008-05-31 02:47:54 +00002737 case FCMP_OEQ: return FCMP_UNE;
2738 case FCMP_ONE: return FCMP_UEQ;
2739 case FCMP_OGT: return FCMP_ULE;
2740 case FCMP_OLT: return FCMP_UGE;
2741 case FCMP_OGE: return FCMP_ULT;
2742 case FCMP_OLE: return FCMP_UGT;
2743 case FCMP_UEQ: return FCMP_ONE;
2744 case FCMP_UNE: return FCMP_OEQ;
2745 case FCMP_UGT: return FCMP_OLE;
2746 case FCMP_ULT: return FCMP_OGE;
2747 case FCMP_UGE: return FCMP_OLT;
2748 case FCMP_ULE: return FCMP_OGT;
2749 case FCMP_ORD: return FCMP_UNO;
2750 case FCMP_UNO: return FCMP_ORD;
2751 case FCMP_TRUE: return FCMP_FALSE;
2752 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002753 }
2754}
2755
Reid Spencer266e42b2006-12-23 06:05:41 +00002756ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2757 switch (pred) {
2758 default: assert(! "Unknown icmp predicate!");
2759 case ICMP_EQ: case ICMP_NE:
2760 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2761 return pred;
2762 case ICMP_UGT: return ICMP_SGT;
2763 case ICMP_ULT: return ICMP_SLT;
2764 case ICMP_UGE: return ICMP_SGE;
2765 case ICMP_ULE: return ICMP_SLE;
2766 }
2767}
2768
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002769ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2770 switch (pred) {
2771 default: assert(! "Unknown icmp predicate!");
2772 case ICMP_EQ: case ICMP_NE:
2773 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2774 return pred;
2775 case ICMP_SGT: return ICMP_UGT;
2776 case ICMP_SLT: return ICMP_ULT;
2777 case ICMP_SGE: return ICMP_UGE;
2778 case ICMP_SLE: return ICMP_ULE;
2779 }
2780}
2781
Reid Spencer266e42b2006-12-23 06:05:41 +00002782bool ICmpInst::isSignedPredicate(Predicate pred) {
2783 switch (pred) {
2784 default: assert(! "Unknown icmp predicate!");
2785 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2786 return true;
2787 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2788 case ICMP_UGE: case ICMP_ULE:
2789 return false;
2790 }
2791}
2792
Reid Spencer0286bc12007-02-28 22:00:54 +00002793/// Initialize a set of values that all satisfy the condition with C.
2794///
2795ConstantRange
2796ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2797 APInt Lower(C);
2798 APInt Upper(C);
2799 uint32_t BitWidth = C.getBitWidth();
2800 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002801 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002802 case ICmpInst::ICMP_EQ: Upper++; break;
2803 case ICmpInst::ICMP_NE: Lower++; break;
2804 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2805 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2806 case ICmpInst::ICMP_UGT:
2807 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2808 break;
2809 case ICmpInst::ICMP_SGT:
2810 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2811 break;
2812 case ICmpInst::ICMP_ULE:
2813 Lower = APInt::getMinValue(BitWidth); Upper++;
2814 break;
2815 case ICmpInst::ICMP_SLE:
2816 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2817 break;
2818 case ICmpInst::ICMP_UGE:
2819 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2820 break;
2821 case ICmpInst::ICMP_SGE:
2822 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2823 break;
2824 }
2825 return ConstantRange(Lower, Upper);
2826}
2827
Dan Gohman4e724382008-05-31 02:47:54 +00002828CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002829 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002830 default: assert(!"Unknown cmp predicate!");
2831 case ICMP_EQ: case ICMP_NE:
2832 return pred;
2833 case ICMP_SGT: return ICMP_SLT;
2834 case ICMP_SLT: return ICMP_SGT;
2835 case ICMP_SGE: return ICMP_SLE;
2836 case ICMP_SLE: return ICMP_SGE;
2837 case ICMP_UGT: return ICMP_ULT;
2838 case ICMP_ULT: return ICMP_UGT;
2839 case ICMP_UGE: return ICMP_ULE;
2840 case ICMP_ULE: return ICMP_UGE;
2841
Reid Spencerd9436b62006-11-20 01:22:35 +00002842 case FCMP_FALSE: case FCMP_TRUE:
2843 case FCMP_OEQ: case FCMP_ONE:
2844 case FCMP_UEQ: case FCMP_UNE:
2845 case FCMP_ORD: case FCMP_UNO:
2846 return pred;
2847 case FCMP_OGT: return FCMP_OLT;
2848 case FCMP_OLT: return FCMP_OGT;
2849 case FCMP_OGE: return FCMP_OLE;
2850 case FCMP_OLE: return FCMP_OGE;
2851 case FCMP_UGT: return FCMP_ULT;
2852 case FCMP_ULT: return FCMP_UGT;
2853 case FCMP_UGE: return FCMP_ULE;
2854 case FCMP_ULE: return FCMP_UGE;
2855 }
2856}
2857
Reid Spencer266e42b2006-12-23 06:05:41 +00002858bool CmpInst::isUnsigned(unsigned short predicate) {
2859 switch (predicate) {
2860 default: return false;
2861 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2862 case ICmpInst::ICMP_UGE: return true;
2863 }
2864}
2865
2866bool CmpInst::isSigned(unsigned short predicate){
2867 switch (predicate) {
2868 default: return false;
2869 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2870 case ICmpInst::ICMP_SGE: return true;
2871 }
2872}
2873
2874bool CmpInst::isOrdered(unsigned short predicate) {
2875 switch (predicate) {
2876 default: return false;
2877 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2878 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2879 case FCmpInst::FCMP_ORD: return true;
2880 }
2881}
2882
2883bool CmpInst::isUnordered(unsigned short predicate) {
2884 switch (predicate) {
2885 default: return false;
2886 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2887 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2888 case FCmpInst::FCMP_UNO: return true;
2889 }
2890}
2891
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002892//===----------------------------------------------------------------------===//
2893// SwitchInst Implementation
2894//===----------------------------------------------------------------------===//
2895
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002896void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002897 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002898 ReservedSpace = 2+NumCases*2;
2899 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002900 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002901
Gabor Greif2d3024d2008-05-26 21:33:52 +00002902 OperandList[0] = Value;
2903 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002904}
2905
Chris Lattner2195fc42007-02-24 00:55:48 +00002906/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2907/// switch on and a default destination. The number of additional cases can
2908/// be specified here to make memory allocation more efficient. This
2909/// constructor can also autoinsert before another instruction.
2910SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2911 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002912 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2913 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002914 init(Value, Default, NumCases);
2915}
2916
2917/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2918/// switch on and a default destination. The number of additional cases can
2919/// be specified here to make memory allocation more efficient. This
2920/// constructor also autoinserts at the end of the specified BasicBlock.
2921SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2922 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002923 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2924 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002925 init(Value, Default, NumCases);
2926}
2927
Misha Brukmanb1c93172005-04-21 23:48:37 +00002928SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002929 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002930 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002931 Use *OL = OperandList, *InOL = SI.OperandList;
2932 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002933 OL[i] = InOL[i];
2934 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002935 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002936 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002937}
2938
Gordon Henriksen14a55692007-12-10 02:14:30 +00002939SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002940 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002941}
2942
2943
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002944/// addCase - Add an entry to the switch instruction...
2945///
Chris Lattner47ac1872005-02-24 05:32:09 +00002946void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002947 unsigned OpNo = NumOperands;
2948 if (OpNo+2 > ReservedSpace)
2949 resizeOperands(0); // Get more space!
2950 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002951 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002952 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002953 OperandList[OpNo] = OnVal;
2954 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002955}
2956
2957/// removeCase - This method removes the specified successor from the switch
2958/// instruction. Note that this cannot be used to remove the default
2959/// destination (successor #0).
2960///
2961void SwitchInst::removeCase(unsigned idx) {
2962 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002963 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2964
2965 unsigned NumOps = getNumOperands();
2966 Use *OL = OperandList;
2967
2968 // Move everything after this operand down.
2969 //
2970 // FIXME: we could just swap with the end of the list, then erase. However,
2971 // client might not expect this to happen. The code as it is thrashes the
2972 // use/def lists, which is kinda lame.
2973 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2974 OL[i-2] = OL[i];
2975 OL[i-2+1] = OL[i+1];
2976 }
2977
2978 // Nuke the last value.
2979 OL[NumOps-2].set(0);
2980 OL[NumOps-2+1].set(0);
2981 NumOperands = NumOps-2;
2982}
2983
2984/// resizeOperands - resize operands - This adjusts the length of the operands
2985/// list according to the following behavior:
2986/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002987/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002988/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2989/// 3. If NumOps == NumOperands, trim the reserved space.
2990///
2991void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002992 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002993 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002994 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002995 } else if (NumOps*2 > NumOperands) {
2996 // No resize needed.
2997 if (ReservedSpace >= NumOps) return;
2998 } else if (NumOps == NumOperands) {
2999 if (ReservedSpace == NumOps) return;
3000 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003001 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003002 }
3003
3004 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003005 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003006 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003007 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003008 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003009 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003010 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003011 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003012}
3013
3014
3015BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3016 return getSuccessor(idx);
3017}
3018unsigned SwitchInst::getNumSuccessorsV() const {
3019 return getNumSuccessors();
3020}
3021void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3022 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003023}
Chris Lattnerf22be932004-10-15 23:52:53 +00003024
Chris Lattnerf22be932004-10-15 23:52:53 +00003025// Define these methods here so vtables don't get emitted into every translation
3026// unit that uses these classes.
3027
Nick Lewycky42fb7452009-09-27 07:38:41 +00003028GetElementPtrInst *GetElementPtrInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003029 GetElementPtrInst *New = new(getNumOperands()) GetElementPtrInst(*this);
3030 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003031 if (hasMetadata()) {
3032 LLVMContext &Context = getContext();
3033 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3034 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003035 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00003036}
3037
Nick Lewycky42fb7452009-09-27 07:38:41 +00003038BinaryOperator *BinaryOperator::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003039 BinaryOperator *New = Create(getOpcode(), Op<0>(), Op<1>());
3040 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003041 if (hasMetadata()) {
3042 LLVMContext &Context = getContext();
3043 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3044 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003045 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00003046}
3047
Nick Lewycky42fb7452009-09-27 07:38:41 +00003048FCmpInst* FCmpInst::clone() const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003049 FCmpInst *New = new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003050 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003051 if (hasMetadata()) {
3052 LLVMContext &Context = getContext();
3053 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3054 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003055 return New;
Chris Lattner0b490b02007-08-24 20:48:18 +00003056}
Nick Lewycky42fb7452009-09-27 07:38:41 +00003057ICmpInst* ICmpInst::clone() const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003058 ICmpInst *New = new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003059 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003060 if (hasMetadata()) {
3061 LLVMContext &Context = getContext();
3062 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3063 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003064 return New;
Reid Spencerd9436b62006-11-20 01:22:35 +00003065}
3066
Nick Lewycky42fb7452009-09-27 07:38:41 +00003067ExtractValueInst *ExtractValueInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003068 ExtractValueInst *New = new ExtractValueInst(*this);
3069 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003070 if (hasMetadata()) {
3071 LLVMContext &Context = getContext();
3072 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3073 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003074 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00003075}
Nick Lewycky42fb7452009-09-27 07:38:41 +00003076InsertValueInst *InsertValueInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003077 InsertValueInst *New = new InsertValueInst(*this);
3078 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003079 if (hasMetadata()) {
3080 LLVMContext &Context = getContext();
3081 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3082 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003083 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00003084}
3085
Nick Lewycky42fb7452009-09-27 07:38:41 +00003086MallocInst *MallocInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003087 MallocInst *New = new MallocInst(getAllocatedType(),
3088 (Value*)getOperand(0),
3089 getAlignment());
3090 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003091 if (hasMetadata()) {
3092 LLVMContext &Context = getContext();
3093 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3094 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003095 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003096}
Dan Gohman0752bff2008-05-23 00:36:11 +00003097
Nick Lewycky42fb7452009-09-27 07:38:41 +00003098AllocaInst *AllocaInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003099 AllocaInst *New = new AllocaInst(getAllocatedType(),
3100 (Value*)getOperand(0),
3101 getAlignment());
3102 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003103 if (hasMetadata()) {
3104 LLVMContext &Context = getContext();
3105 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3106 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003107 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003108}
3109
Nick Lewycky42fb7452009-09-27 07:38:41 +00003110FreeInst *FreeInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003111 FreeInst *New = new FreeInst(getOperand(0));
3112 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003113 if (hasMetadata()) {
3114 LLVMContext &Context = getContext();
3115 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3116 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003117 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003118}
3119
Nick Lewycky42fb7452009-09-27 07:38:41 +00003120LoadInst *LoadInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003121 LoadInst *New = new LoadInst(getOperand(0),
3122 Twine(), isVolatile(),
3123 getAlignment());
3124 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003125 if (hasMetadata()) {
3126 LLVMContext &Context = getContext();
3127 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3128 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003129 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003130}
3131
Nick Lewycky42fb7452009-09-27 07:38:41 +00003132StoreInst *StoreInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003133 StoreInst *New = new StoreInst(getOperand(0), getOperand(1),
3134 isVolatile(), getAlignment());
3135 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003136 if (hasMetadata()) {
3137 LLVMContext &Context = getContext();
3138 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3139 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003140 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003141}
3142
Nick Lewycky42fb7452009-09-27 07:38:41 +00003143TruncInst *TruncInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003144 TruncInst *New = new TruncInst(getOperand(0), getType());
3145 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003146 if (hasMetadata()) {
3147 LLVMContext &Context = getContext();
3148 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3149 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003150 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003151}
3152
Nick Lewycky42fb7452009-09-27 07:38:41 +00003153ZExtInst *ZExtInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003154 ZExtInst *New = new ZExtInst(getOperand(0), getType());
3155 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003156 if (hasMetadata()) {
3157 LLVMContext &Context = getContext();
3158 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3159 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003160 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003161}
3162
Nick Lewycky42fb7452009-09-27 07:38:41 +00003163SExtInst *SExtInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003164 SExtInst *New = new SExtInst(getOperand(0), getType());
3165 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003166 if (hasMetadata()) {
3167 LLVMContext &Context = getContext();
3168 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3169 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003170 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003171}
3172
Nick Lewycky42fb7452009-09-27 07:38:41 +00003173FPTruncInst *FPTruncInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003174 FPTruncInst *New = new FPTruncInst(getOperand(0), getType());
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;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003181}
3182
Nick Lewycky42fb7452009-09-27 07:38:41 +00003183FPExtInst *FPExtInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003184 FPExtInst *New = new FPExtInst(getOperand(0), getType());
3185 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003186 if (hasMetadata()) {
3187 LLVMContext &Context = getContext();
3188 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3189 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003190 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003191}
3192
Nick Lewycky42fb7452009-09-27 07:38:41 +00003193UIToFPInst *UIToFPInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003194 UIToFPInst *New = new UIToFPInst(getOperand(0), getType());
3195 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003196 if (hasMetadata()) {
3197 LLVMContext &Context = getContext();
3198 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3199 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003200 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003201}
3202
Nick Lewycky42fb7452009-09-27 07:38:41 +00003203SIToFPInst *SIToFPInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003204 SIToFPInst *New = new SIToFPInst(getOperand(0), getType());
3205 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003206 if (hasMetadata()) {
3207 LLVMContext &Context = getContext();
3208 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3209 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003210 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003211}
3212
Nick Lewycky42fb7452009-09-27 07:38:41 +00003213FPToUIInst *FPToUIInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003214 FPToUIInst *New = new FPToUIInst(getOperand(0), getType());
3215 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003216 if (hasMetadata()) {
3217 LLVMContext &Context = getContext();
3218 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3219 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003220 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003221}
3222
Nick Lewycky42fb7452009-09-27 07:38:41 +00003223FPToSIInst *FPToSIInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003224 FPToSIInst *New = new FPToSIInst(getOperand(0), getType());
3225 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003226 if (hasMetadata()) {
3227 LLVMContext &Context = getContext();
3228 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3229 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003230 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003231}
3232
Nick Lewycky42fb7452009-09-27 07:38:41 +00003233PtrToIntInst *PtrToIntInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003234 PtrToIntInst *New = new PtrToIntInst(getOperand(0), getType());
3235 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003236 if (hasMetadata()) {
3237 LLVMContext &Context = getContext();
3238 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3239 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003240 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003241}
3242
Nick Lewycky42fb7452009-09-27 07:38:41 +00003243IntToPtrInst *IntToPtrInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003244 IntToPtrInst *New = new IntToPtrInst(getOperand(0), getType());
3245 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003246 if (hasMetadata()) {
3247 LLVMContext &Context = getContext();
3248 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3249 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003250 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003251}
3252
Nick Lewycky42fb7452009-09-27 07:38:41 +00003253BitCastInst *BitCastInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003254 BitCastInst *New = new BitCastInst(getOperand(0), getType());
3255 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003256 if (hasMetadata()) {
3257 LLVMContext &Context = getContext();
3258 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3259 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003260 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003261}
3262
Nick Lewycky42fb7452009-09-27 07:38:41 +00003263CallInst *CallInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003264 CallInst *New = new(getNumOperands()) CallInst(*this);
3265 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003266 if (hasMetadata()) {
3267 LLVMContext &Context = getContext();
3268 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3269 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003270 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003271}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003272
Nick Lewycky42fb7452009-09-27 07:38:41 +00003273SelectInst *SelectInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003274 SelectInst *New = SelectInst::Create(getOperand(0),
3275 getOperand(1),
3276 getOperand(2));
3277 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003278 if (hasMetadata()) {
3279 LLVMContext &Context = getContext();
3280 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3281 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003282 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003283}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003284
Nick Lewycky42fb7452009-09-27 07:38:41 +00003285VAArgInst *VAArgInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003286 VAArgInst *New = new VAArgInst(getOperand(0), getType());
3287 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003288 if (hasMetadata()) {
3289 LLVMContext &Context = getContext();
3290 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3291 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003292 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003293}
3294
Nick Lewycky42fb7452009-09-27 07:38:41 +00003295ExtractElementInst *ExtractElementInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003296 ExtractElementInst *New = ExtractElementInst::Create(getOperand(0),
3297 getOperand(1));
3298 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003299 if (hasMetadata()) {
3300 LLVMContext &Context = getContext();
3301 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3302 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003303 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003304}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003305
Nick Lewycky42fb7452009-09-27 07:38:41 +00003306InsertElementInst *InsertElementInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003307 InsertElementInst *New = InsertElementInst::Create(getOperand(0),
3308 getOperand(1),
3309 getOperand(2));
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;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003316}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003317
Nick Lewycky42fb7452009-09-27 07:38:41 +00003318ShuffleVectorInst *ShuffleVectorInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003319 ShuffleVectorInst *New = new ShuffleVectorInst(getOperand(0),
3320 getOperand(1),
3321 getOperand(2));
3322 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003323 if (hasMetadata()) {
3324 LLVMContext &Context = getContext();
3325 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3326 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003327 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003328}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003329
Nick Lewycky42fb7452009-09-27 07:38:41 +00003330PHINode *PHINode::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003331 PHINode *New = new PHINode(*this);
3332 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003333 if (hasMetadata()) {
3334 LLVMContext &Context = getContext();
3335 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3336 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003337 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003338}
3339
Nick Lewycky42fb7452009-09-27 07:38:41 +00003340ReturnInst *ReturnInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003341 ReturnInst *New = new(getNumOperands()) ReturnInst(*this);
3342 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003343 if (hasMetadata()) {
3344 LLVMContext &Context = getContext();
3345 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3346 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003347 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003348}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003349
Nick Lewycky42fb7452009-09-27 07:38:41 +00003350BranchInst *BranchInst::clone() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003351 unsigned Ops(getNumOperands());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003352 BranchInst *New = new(Ops, Ops == 1) BranchInst(*this);
3353 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003354 if (hasMetadata()) {
3355 LLVMContext &Context = getContext();
3356 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3357 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003358 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003359}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003360
Nick Lewycky42fb7452009-09-27 07:38:41 +00003361SwitchInst *SwitchInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003362 SwitchInst *New = new SwitchInst(*this);
3363 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003364 if (hasMetadata()) {
3365 LLVMContext &Context = getContext();
3366 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3367 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003368 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003369}
3370
Nick Lewycky42fb7452009-09-27 07:38:41 +00003371InvokeInst *InvokeInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003372 InvokeInst *New = new(getNumOperands()) InvokeInst(*this);
3373 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003374 if (hasMetadata()) {
3375 LLVMContext &Context = getContext();
3376 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3377 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003378 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003379}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003380
Nick Lewycky42fb7452009-09-27 07:38:41 +00003381UnwindInst *UnwindInst::clone() const {
3382 LLVMContext &Context = getContext();
3383 UnwindInst *New = new UnwindInst(Context);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003384 New->SubclassOptionalData = SubclassOptionalData;
Nick Lewycky42fb7452009-09-27 07:38:41 +00003385 if (hasMetadata())
Devang Pateladd58652009-09-23 18:32:25 +00003386 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003387 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003388}
3389
Nick Lewycky42fb7452009-09-27 07:38:41 +00003390UnreachableInst *UnreachableInst::clone() const {
3391 LLVMContext &Context = getContext();
3392 UnreachableInst *New = new UnreachableInst(Context);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003393 New->SubclassOptionalData = SubclassOptionalData;
Nick Lewycky42fb7452009-09-27 07:38:41 +00003394 if (hasMetadata())
Devang Pateladd58652009-09-23 18:32:25 +00003395 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003396 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003397}