blob: 99a28594e6ca5e14f0d9241ac704a1f9dcb24fc1 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Dan Gohman22571482009-09-03 15:34:35 +000022#include "llvm/Analysis/Dominators.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000025#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Devang Pateladd58652009-09-23 18:32:25 +000027
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000028using namespace llvm;
29
Chris Lattner3e13b8c2008-01-02 23:42:30 +000030//===----------------------------------------------------------------------===//
31// CallSite Class
32//===----------------------------------------------------------------------===//
33
Gabor Greif1b9921a2009-01-11 22:33:22 +000034#define CALLSITE_DELEGATE_GETTER(METHOD) \
35 Instruction *II(getInstruction()); \
36 return isCall() \
37 ? cast<CallInst>(II)->METHOD \
38 : cast<InvokeInst>(II)->METHOD
39
40#define CALLSITE_DELEGATE_SETTER(METHOD) \
41 Instruction *II(getInstruction()); \
42 if (isCall()) \
43 cast<CallInst>(II)->METHOD; \
44 else \
45 cast<InvokeInst>(II)->METHOD
46
Duncan Sands85fab3a2008-02-18 17:32:13 +000047CallSite::CallSite(Instruction *C) {
48 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
Gabor Greif1b9921a2009-01-11 22:33:22 +000049 I.setPointer(C);
50 I.setInt(isa<CallInst>(C));
Duncan Sands85fab3a2008-02-18 17:32:13 +000051}
Sandeep Patel68c5f472009-09-02 08:44:58 +000052CallingConv::ID CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000053 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000054}
Sandeep Patel68c5f472009-09-02 08:44:58 +000055void CallSite::setCallingConv(CallingConv::ID CC) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000056 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
Chris Lattnerf7b6d312005-05-06 20:26:43 +000057}
Devang Patel4c758ea2008-09-25 21:00:45 +000058const AttrListPtr &CallSite::getAttributes() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000059 CALLSITE_DELEGATE_GETTER(getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000060}
Devang Patel4c758ea2008-09-25 21:00:45 +000061void CallSite::setAttributes(const AttrListPtr &PAL) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000062 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000063}
Devang Patelba3fa6c2008-09-23 23:03:40 +000064bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000065 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
Duncan Sands5208d1a2007-11-28 17:07:01 +000066}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000067uint16_t CallSite::getParamAlignment(uint16_t i) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000068 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
Dale Johanneseneabc5f32008-02-22 17:49:45 +000069}
Duncan Sands38ef3a82007-12-03 20:06:50 +000070bool CallSite::doesNotAccessMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000071 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000072}
Duncan Sands78c88722008-07-08 08:38:44 +000073void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000074 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000075}
Duncan Sands38ef3a82007-12-03 20:06:50 +000076bool CallSite::onlyReadsMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000077 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000078}
Duncan Sands78c88722008-07-08 08:38:44 +000079void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000080 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000081}
82bool CallSite::doesNotReturn() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000083 CALLSITE_DELEGATE_GETTER(doesNotReturn());
Duncan Sands78c88722008-07-08 08:38:44 +000084}
85void CallSite::setDoesNotReturn(bool doesNotReturn) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000086 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
Duncan Sands78c88722008-07-08 08:38:44 +000087}
Duncan Sands3353ed02007-12-18 09:59:50 +000088bool CallSite::doesNotThrow() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000089 CALLSITE_DELEGATE_GETTER(doesNotThrow());
Duncan Sands8e4847e2007-12-16 15:51:49 +000090}
Duncan Sandsaa31b922007-12-19 21:13:37 +000091void CallSite::setDoesNotThrow(bool doesNotThrow) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000092 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
Duncan Sandsaa31b922007-12-19 21:13:37 +000093}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000094
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000095bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000096 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
97 if (AI->get() == Arg)
98 return true;
99 return false;
100}
101
Gabor Greif1b9921a2009-01-11 22:33:22 +0000102#undef CALLSITE_DELEGATE_GETTER
103#undef CALLSITE_DELEGATE_SETTER
104
Gordon Henriksen14a55692007-12-10 02:14:30 +0000105//===----------------------------------------------------------------------===//
106// TerminatorInst Class
107//===----------------------------------------------------------------------===//
108
109// Out of line virtual method, so the vtable, etc has a home.
110TerminatorInst::~TerminatorInst() {
111}
112
Gabor Greiff6caff662008-05-10 08:32:32 +0000113//===----------------------------------------------------------------------===//
114// UnaryInstruction Class
115//===----------------------------------------------------------------------===//
116
Gordon Henriksen14a55692007-12-10 02:14:30 +0000117// Out of line virtual method, so the vtable, etc has a home.
118UnaryInstruction::~UnaryInstruction() {
119}
120
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000121//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000122// SelectInst Class
123//===----------------------------------------------------------------------===//
124
125/// areInvalidOperands - Return a string if the specified operands are invalid
126/// for a select operation, otherwise return null.
127const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
128 if (Op1->getType() != Op2->getType())
129 return "both values to select must have same type";
130
131 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
132 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000133 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000134 return "vector select condition element type must be i1";
135 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
136 if (ET == 0)
137 return "selected values for vector select must be vectors";
138 if (ET->getNumElements() != VT->getNumElements())
139 return "vector select requires selected vectors to have "
140 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000141 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000142 return "select condition must be i1 or <n x i1>";
143 }
144 return 0;
145}
146
147
148//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149// PHINode Class
150//===----------------------------------------------------------------------===//
151
152PHINode::PHINode(const PHINode &PN)
153 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000154 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000155 ReservedSpace(PN.getNumOperands()) {
156 Use *OL = OperandList;
157 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000158 OL[i] = PN.getOperand(i);
159 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000160 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000161 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000162}
163
Gordon Henriksen14a55692007-12-10 02:14:30 +0000164PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000165 if (OperandList)
166 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000167}
168
169// removeIncomingValue - Remove an incoming value. This is useful if a
170// predecessor basic block is deleted.
171Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
172 unsigned NumOps = getNumOperands();
173 Use *OL = OperandList;
174 assert(Idx*2 < NumOps && "BB not in PHI node!");
175 Value *Removed = OL[Idx*2];
176
177 // Move everything after this operand down.
178 //
179 // FIXME: we could just swap with the end of the list, then erase. However,
180 // client might not expect this to happen. The code as it is thrashes the
181 // use/def lists, which is kinda lame.
182 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
183 OL[i-2] = OL[i];
184 OL[i-2+1] = OL[i+1];
185 }
186
187 // Nuke the last value.
188 OL[NumOps-2].set(0);
189 OL[NumOps-2+1].set(0);
190 NumOperands = NumOps-2;
191
192 // If the PHI node is dead, because it has zero entries, nuke it now.
193 if (NumOps == 2 && DeletePHIIfEmpty) {
194 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000195 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000196 eraseFromParent();
197 }
198 return Removed;
199}
200
201/// resizeOperands - resize operands - This adjusts the length of the operands
202/// list according to the following behavior:
203/// 1. If NumOps == 0, grow the operand list in response to a push_back style
204/// of operation. This grows the number of ops by 1.5 times.
205/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
206/// 3. If NumOps == NumOperands, trim the reserved space.
207///
208void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000209 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000210 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000211 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000212 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
213 } else if (NumOps*2 > NumOperands) {
214 // No resize needed.
215 if (ReservedSpace >= NumOps) return;
216 } else if (NumOps == NumOperands) {
217 if (ReservedSpace == NumOps) return;
218 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000219 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000220 }
221
222 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000223 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000224 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000225 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000226 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000227 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000228}
229
Nate Begemanb3923212005-08-04 23:24:19 +0000230/// hasConstantValue - If the specified PHI node always merges together the same
231/// value, return the value, otherwise return null.
232///
Dan Gohman22571482009-09-03 15:34:35 +0000233/// If the PHI has undef operands, but all the rest of the operands are
234/// some unique value, return that value if it can be proved that the
235/// value dominates the PHI. If DT is null, use a conservative check,
236/// otherwise use DT to test for dominance.
237///
238Value *PHINode::hasConstantValue(DominatorTree *DT) const {
Chris Lattner7d08da62009-09-21 22:27:34 +0000239 // If the PHI node only has one incoming value, eliminate the PHI node.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000240 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000241 if (getIncomingValue(0) != this) // not X = phi X
242 return getIncomingValue(0);
Chris Lattner7d08da62009-09-21 22:27:34 +0000243 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000244 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000245
Nate Begemanb3923212005-08-04 23:24:19 +0000246 // Otherwise if all of the incoming values are the same for the PHI, replace
247 // the PHI node with the incoming value.
248 //
249 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000250 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000251 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000252 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000253 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000254 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000255 if (InVal && getIncomingValue(i) != InVal)
256 return 0; // Not the same, bail out.
Chris Lattner7d08da62009-09-21 22:27:34 +0000257 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000258 }
Nate Begemanb3923212005-08-04 23:24:19 +0000259
260 // The only case that could cause InVal to be null is if we have a PHI node
261 // that only has entries for itself. In this case, there is no entry into the
262 // loop, so kill the PHI.
263 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000264 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000265
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000266 // If we have a PHI node like phi(X, undef, X), where X is defined by some
267 // instruction, we cannot always return X as the result of the PHI node. Only
268 // do this if X is not an instruction (thus it must dominate the PHI block),
269 // or if the client is prepared to deal with this possibility.
Chris Lattner7d08da62009-09-21 22:27:34 +0000270 if (!HasUndefInput || !isa<Instruction>(InVal))
271 return InVal;
272
273 Instruction *IV = cast<Instruction>(InVal);
274 if (DT) {
275 // We have a DominatorTree. Do a precise test.
276 if (!DT->dominates(IV, this))
277 return 0;
278 } else {
279 // If it is in the entry block, it obviously dominates everything.
280 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
281 isa<InvokeInst>(IV))
282 return 0; // Cannot guarantee that InVal dominates this PHINode.
283 }
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000284
Nate Begemanb3923212005-08-04 23:24:19 +0000285 // All of the incoming values are the same, return the value now.
286 return InVal;
287}
288
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000289
290//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000291// CallInst Implementation
292//===----------------------------------------------------------------------===//
293
Gordon Henriksen14a55692007-12-10 02:14:30 +0000294CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000295}
296
Chris Lattner054ba2c2007-02-13 00:58:44 +0000297void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000298 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
299 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000300 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000301
Misha Brukmanb1c93172005-04-21 23:48:37 +0000302 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000304 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000305
Chris Lattner054ba2c2007-02-13 00:58:44 +0000306 assert((NumParams == FTy->getNumParams() ||
307 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000308 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000309 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000310 assert((i >= FTy->getNumParams() ||
311 FTy->getParamType(i) == Params[i]->getType()) &&
312 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000313 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000314 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315}
316
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000317void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000318 assert(NumOperands == 3 && "NumOperands not set up?");
319 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000320 OL[0] = Func;
321 OL[1] = Actual1;
322 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000323
324 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000325 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000326 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000328 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000329 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000331 assert((0 >= FTy->getNumParams() ||
332 FTy->getParamType(0) == Actual1->getType()) &&
333 "Calling a function with a bad signature!");
334 assert((1 >= FTy->getNumParams() ||
335 FTy->getParamType(1) == Actual2->getType()) &&
336 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000337}
338
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000339void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000340 assert(NumOperands == 2 && "NumOperands not set up?");
341 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000342 OL[0] = Func;
343 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000344
345 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000346 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000347 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000349 assert((FTy->getNumParams() == 1 ||
350 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000351 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000352 assert((0 == FTy->getNumParams() ||
353 FTy->getParamType(0) == Actual->getType()) &&
354 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000355}
356
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000357void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000358 assert(NumOperands == 1 && "NumOperands not set up?");
359 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000360 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000361
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000362 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000363 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000364 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000365
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000366 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000367}
368
Daniel Dunbar4975db62009-07-25 04:41:11 +0000369CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000370 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
372 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000373 Instruction::Call,
374 OperandTraits<CallInst>::op_end(this) - 2,
375 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000376 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000377 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000378}
379
Daniel Dunbar4975db62009-07-25 04:41:11 +0000380CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000381 BasicBlock *InsertAtEnd)
382 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
383 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000384 Instruction::Call,
385 OperandTraits<CallInst>::op_end(this) - 2,
386 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000387 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000388 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000389}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000390CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000391 Instruction *InsertBefore)
392 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
393 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000394 Instruction::Call,
395 OperandTraits<CallInst>::op_end(this) - 1,
396 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000397 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000398 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000399}
400
Daniel Dunbar4975db62009-07-25 04:41:11 +0000401CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000402 BasicBlock *InsertAtEnd)
403 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
404 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000405 Instruction::Call,
406 OperandTraits<CallInst>::op_end(this) - 1,
407 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000408 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000409 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000410}
411
Misha Brukmanb1c93172005-04-21 23:48:37 +0000412CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000413 : Instruction(CI.getType(), Instruction::Call,
414 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000415 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000416 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000417 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000418 Use *OL = OperandList;
419 Use *InOL = CI.OperandList;
420 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000421 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000422 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000423}
424
Devang Patel4c758ea2008-09-25 21:00:45 +0000425void CallInst::addAttribute(unsigned i, Attributes attr) {
426 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000427 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000428 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000429}
430
Devang Patel4c758ea2008-09-25 21:00:45 +0000431void CallInst::removeAttribute(unsigned i, Attributes attr) {
432 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000433 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000434 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000435}
436
Devang Patelba3fa6c2008-09-23 23:03:40 +0000437bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000438 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000439 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000440 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000441 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000442 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000443}
444
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000445/// IsConstantOne - Return true only if val is constant int 1
446static bool IsConstantOne(Value *val) {
447 assert(val && "IsConstantOne does not work with NULL val");
448 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
449}
450
451static Value *checkArraySize(Value *Amt, const Type *IntPtrTy) {
452 if (!Amt)
453 Amt = ConstantInt::get(IntPtrTy, 1);
454 else {
455 assert(!isa<BasicBlock>(Amt) &&
456 "Passed basic block into malloc size parameter! Use other ctor");
457 assert(Amt->getType() == IntPtrTy &&
458 "Malloc array size is not an intptr!");
459 }
460 return Amt;
461}
462
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000463static Instruction *createMalloc(Instruction *InsertBefore,
464 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
465 const Type *AllocTy, Value *ArraySize,
466 Function *MallocF, const Twine &NameStr) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000467 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000468 "createMalloc needs either InsertBefore or InsertAtEnd");
469
470 // malloc(type) becomes:
471 // bitcast (i8* malloc(typeSize)) to type*
472 // malloc(type, arraySize) becomes:
473 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
474 Value *AllocSize = ConstantExpr::getSizeOf(AllocTy);
475 AllocSize = ConstantExpr::getTruncOrBitCast(cast<Constant>(AllocSize),
476 IntPtrTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000477 ArraySize = checkArraySize(ArraySize, IntPtrTy);
478
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000479 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000480 if (IsConstantOne(AllocSize)) {
481 AllocSize = ArraySize; // Operand * 1 = Operand
482 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
483 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
484 false /*ZExt*/);
485 // Malloc arg is constant product of type size and array size
486 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
487 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000488 // Multiply type size by the array size...
489 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000490 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
491 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000492 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000493 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
494 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000495 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000496 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000497
Victor Hernandez788eaab2009-09-18 19:20:02 +0000498 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000499 // Create the call to Malloc.
500 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
501 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000502 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000503 if (!MallocF)
504 // prototype malloc as "void *malloc(size_t)"
505 MallocF = cast<Function>(M->getOrInsertFunction("malloc", BPTy,
506 IntPtrTy, NULL));
507 if (!MallocF->doesNotAlias(0)) MallocF->setDoesNotAlias(0);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000508 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000509 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000510 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000511 if (InsertBefore) {
512 MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000513 Result = MCall;
514 if (Result->getType() != AllocPtrType)
515 // Create a cast instruction to convert to the right type...
516 Result = new BitCastInst(MCall, AllocPtrType, NameStr, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000517 } else {
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000518 MCall = CallInst::Create(MallocF, AllocSize, "malloccall");
519 Result = MCall;
520 if (Result->getType() != AllocPtrType) {
521 InsertAtEnd->getInstList().push_back(MCall);
522 // Create a cast instruction to convert to the right type...
523 Result = new BitCastInst(MCall, AllocPtrType, NameStr);
524 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000525 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000526 MCall->setTailCall();
Daniel Dunbarb9189002009-09-11 22:07:31 +0000527 assert(MCall->getType() != Type::getVoidTy(BB->getContext()) &&
528 "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000529
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000530 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000531}
532
533/// CreateMalloc - Generate the IR for a call to malloc:
534/// 1. Compute the malloc call's argument as the specified type's size,
535/// possibly multiplied by the array size if the array size is not
536/// constant 1.
537/// 2. Call malloc with that argument.
538/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000539Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
540 const Type *IntPtrTy, const Type *AllocTy,
541 Value *ArraySize, const Twine &Name) {
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000542 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy,
543 ArraySize, NULL, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000544}
545
546/// CreateMalloc - Generate the IR for a call to malloc:
547/// 1. Compute the malloc call's argument as the specified type's size,
548/// possibly multiplied by the array size if the array size is not
549/// constant 1.
550/// 2. Call malloc with that argument.
551/// 3. Bitcast the result of the malloc call to the specified type.
552/// Note: This function does not add the bitcast to the basic block, that is the
553/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000554Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
555 const Type *IntPtrTy, const Type *AllocTy,
556 Value *ArraySize, Function* MallocF,
557 const Twine &Name) {
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000558 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy,
559 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000560}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000561
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000562//===----------------------------------------------------------------------===//
563// InvokeInst Implementation
564//===----------------------------------------------------------------------===//
565
566void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000567 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000568 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000569 Use *OL = OperandList;
570 OL[0] = Fn;
571 OL[1] = IfNormal;
572 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000573 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000574 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000575 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000576
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000577 assert(((NumArgs == FTy->getNumParams()) ||
578 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000579 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000580
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000581 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000582 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000583 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000584 "Invoking a function with a bad signature!");
585
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000586 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000587 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000588}
589
Misha Brukmanb1c93172005-04-21 23:48:37 +0000590InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000591 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000592 OperandTraits<InvokeInst>::op_end(this)
593 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000594 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000595 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000596 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000597 Use *OL = OperandList, *InOL = II.OperandList;
598 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000599 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000600 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000601}
602
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000603BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
604 return getSuccessor(idx);
605}
606unsigned InvokeInst::getNumSuccessorsV() const {
607 return getNumSuccessors();
608}
609void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
610 return setSuccessor(idx, B);
611}
612
Devang Patelba3fa6c2008-09-23 23:03:40 +0000613bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000614 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000615 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000616 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000617 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000618 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000619}
620
Devang Patel4c758ea2008-09-25 21:00:45 +0000621void InvokeInst::addAttribute(unsigned i, Attributes attr) {
622 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000623 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000624 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000625}
626
Devang Patel4c758ea2008-09-25 21:00:45 +0000627void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
628 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000629 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000630 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000631}
632
Duncan Sands5208d1a2007-11-28 17:07:01 +0000633
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000634//===----------------------------------------------------------------------===//
635// ReturnInst Implementation
636//===----------------------------------------------------------------------===//
637
Chris Lattner2195fc42007-02-24 00:55:48 +0000638ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000639 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000640 OperandTraits<ReturnInst>::op_end(this) -
641 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000642 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000643 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000644 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000645 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000646}
647
Owen Anderson55f1c092009-08-13 21:58:54 +0000648ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
649 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000650 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
651 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000652 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000653 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000654}
Owen Anderson55f1c092009-08-13 21:58:54 +0000655ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
656 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000657 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
658 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000659 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000660 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000661}
Owen Anderson55f1c092009-08-13 21:58:54 +0000662ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
663 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000664 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000665}
666
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000667unsigned ReturnInst::getNumSuccessorsV() const {
668 return getNumSuccessors();
669}
670
Devang Patelae682fb2008-02-26 17:56:20 +0000671/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
672/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000673void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000674 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000675}
676
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000677BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000678 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000679 return 0;
680}
681
Devang Patel59643e52008-02-23 00:35:18 +0000682ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000683}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000684
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000685//===----------------------------------------------------------------------===//
686// UnwindInst Implementation
687//===----------------------------------------------------------------------===//
688
Owen Anderson55f1c092009-08-13 21:58:54 +0000689UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
690 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
691 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000692}
Owen Anderson55f1c092009-08-13 21:58:54 +0000693UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
694 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
695 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000696}
697
698
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000699unsigned UnwindInst::getNumSuccessorsV() const {
700 return getNumSuccessors();
701}
702
703void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000704 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000705}
706
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000707BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000708 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000709 return 0;
710}
711
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000712//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000713// UnreachableInst Implementation
714//===----------------------------------------------------------------------===//
715
Owen Anderson55f1c092009-08-13 21:58:54 +0000716UnreachableInst::UnreachableInst(LLVMContext &Context,
717 Instruction *InsertBefore)
718 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
719 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000720}
Owen Anderson55f1c092009-08-13 21:58:54 +0000721UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
722 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
723 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000724}
725
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000726unsigned UnreachableInst::getNumSuccessorsV() const {
727 return getNumSuccessors();
728}
729
730void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000731 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000732}
733
734BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000735 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000736 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000737}
738
739//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000740// BranchInst Implementation
741//===----------------------------------------------------------------------===//
742
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000743void BranchInst::AssertOK() {
744 if (isConditional())
Owen Anderson55f1c092009-08-13 21:58:54 +0000745 assert(getCondition()->getType() == Type::getInt1Ty(getContext()) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000746 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000747}
748
Chris Lattner2195fc42007-02-24 00:55:48 +0000749BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000750 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000751 OperandTraits<BranchInst>::op_end(this) - 1,
752 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000753 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000754 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000755}
756BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
757 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000758 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000759 OperandTraits<BranchInst>::op_end(this) - 3,
760 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000761 Op<-1>() = IfTrue;
762 Op<-2>() = IfFalse;
763 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000764#ifndef NDEBUG
765 AssertOK();
766#endif
767}
768
769BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000770 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000771 OperandTraits<BranchInst>::op_end(this) - 1,
772 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000773 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000774 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000775}
776
777BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
778 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000779 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000780 OperandTraits<BranchInst>::op_end(this) - 3,
781 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000782 Op<-1>() = IfTrue;
783 Op<-2>() = IfFalse;
784 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000785#ifndef NDEBUG
786 AssertOK();
787#endif
788}
789
790
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000791BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000792 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000793 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
794 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000795 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000796 if (BI.getNumOperands() != 1) {
797 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000798 Op<-3>() = BI.Op<-3>();
799 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000800 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000801 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000802}
803
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000804
805Use* Use::getPrefix() {
806 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
807 if (PotentialPrefix.getOpaqueValue())
808 return 0;
809
810 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
811}
812
813BranchInst::~BranchInst() {
814 if (NumOperands == 1) {
815 if (Use *Prefix = OperandList->getPrefix()) {
816 Op<-1>() = 0;
817 //
818 // mark OperandList to have a special value for scrutiny
819 // by baseclass destructors and operator delete
820 OperandList = Prefix;
821 } else {
822 NumOperands = 3;
823 OperandList = op_begin();
824 }
825 }
826}
827
828
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000829BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
830 return getSuccessor(idx);
831}
832unsigned BranchInst::getNumSuccessorsV() const {
833 return getNumSuccessors();
834}
835void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
836 setSuccessor(idx, B);
837}
838
839
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000840//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000841// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000842//===----------------------------------------------------------------------===//
843
Owen Andersonb6b25302009-07-14 23:09:55 +0000844static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000845 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000846 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000847 else {
848 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000849 "Passed basic block into allocation size parameter! Use other ctor");
Owen Anderson55f1c092009-08-13 21:58:54 +0000850 assert(Amt->getType() == Type::getInt32Ty(Context) &&
Victor Hernandeza3aaf852009-10-17 01:18:07 +0000851 "Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000852 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000853 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000854}
855
Victor Hernandez8acf2952009-10-23 21:09:37 +0000856AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
857 const Twine &Name, Instruction *InsertBefore)
858 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
859 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
860 setAlignment(0);
861 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
862 setName(Name);
863}
864
865AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
866 const Twine &Name, BasicBlock *InsertAtEnd)
867 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
868 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
869 setAlignment(0);
870 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
871 setName(Name);
872}
873
874AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
875 Instruction *InsertBefore)
876 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
877 getAISize(Ty->getContext(), 0), InsertBefore) {
878 setAlignment(0);
879 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
880 setName(Name);
881}
882
883AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
884 BasicBlock *InsertAtEnd)
885 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
886 getAISize(Ty->getContext(), 0), InsertAtEnd) {
887 setAlignment(0);
888 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
889 setName(Name);
890}
891
892AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
893 const Twine &Name, Instruction *InsertBefore)
894 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000895 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000896 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000897 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000898 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000899}
900
Victor Hernandez8acf2952009-10-23 21:09:37 +0000901AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
902 const Twine &Name, BasicBlock *InsertAtEnd)
903 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000904 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000905 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000906 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000907 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000908}
909
Gordon Henriksen14a55692007-12-10 02:14:30 +0000910// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000911AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000912}
913
Victor Hernandez8acf2952009-10-23 21:09:37 +0000914void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000915 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
916 SubclassData = Log2_32(Align) + 1;
917 assert(getAlignment() == Align && "Alignment representation error!");
918}
919
Victor Hernandez8acf2952009-10-23 21:09:37 +0000920bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000921 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
922 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000923 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000924}
925
Victor Hernandez8acf2952009-10-23 21:09:37 +0000926const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000927 return getType()->getElementType();
928}
929
Chris Lattner8b291e62008-11-26 02:54:17 +0000930/// isStaticAlloca - Return true if this alloca is in the entry block of the
931/// function and is a constant size. If so, the code generator will fold it
932/// into the prolog/epilog code, so it is basically free.
933bool AllocaInst::isStaticAlloca() const {
934 // Must be constant size.
935 if (!isa<ConstantInt>(getArraySize())) return false;
936
937 // Must be in the entry block.
938 const BasicBlock *Parent = getParent();
939 return Parent == &Parent->getParent()->front();
940}
941
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000942//===----------------------------------------------------------------------===//
943// FreeInst Implementation
944//===----------------------------------------------------------------------===//
945
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000946void FreeInst::AssertOK() {
947 assert(isa<PointerType>(getOperand(0)->getType()) &&
948 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000949}
950
951FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000952 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
953 Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000954 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000955}
956
957FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000958 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
959 Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000960 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000961}
962
963
964//===----------------------------------------------------------------------===//
965// LoadInst Implementation
966//===----------------------------------------------------------------------===//
967
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000968void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000969 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000970 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000971}
972
Daniel Dunbar4975db62009-07-25 04:41:11 +0000973LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000974 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000975 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000976 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000977 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000978 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000979 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000980}
981
Daniel Dunbar4975db62009-07-25 04:41:11 +0000982LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000983 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000984 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000985 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000986 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000987 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000988 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000989}
990
Daniel Dunbar4975db62009-07-25 04:41:11 +0000991LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000992 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000993 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000994 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000995 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000996 setAlignment(0);
997 AssertOK();
998 setName(Name);
999}
1000
Daniel Dunbar4975db62009-07-25 04:41:11 +00001001LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001002 unsigned Align, Instruction *InsertBef)
1003 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1004 Load, Ptr, InsertBef) {
1005 setVolatile(isVolatile);
1006 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001007 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001008 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001009}
1010
Daniel Dunbar4975db62009-07-25 04:41:11 +00001011LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001012 unsigned Align, BasicBlock *InsertAE)
1013 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1014 Load, Ptr, InsertAE) {
1015 setVolatile(isVolatile);
1016 setAlignment(Align);
1017 AssertOK();
1018 setName(Name);
1019}
1020
Daniel Dunbar4975db62009-07-25 04:41:11 +00001021LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001022 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001023 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001024 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001025 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001026 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +00001027 AssertOK();
1028 setName(Name);
1029}
1030
Daniel Dunbar27096822009-08-11 18:11:15 +00001031
1032
1033LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1034 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1035 Load, Ptr, InsertBef) {
1036 setVolatile(false);
1037 setAlignment(0);
1038 AssertOK();
1039 if (Name && Name[0]) setName(Name);
1040}
1041
1042LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1043 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1044 Load, Ptr, InsertAE) {
1045 setVolatile(false);
1046 setAlignment(0);
1047 AssertOK();
1048 if (Name && Name[0]) setName(Name);
1049}
1050
1051LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1052 Instruction *InsertBef)
1053: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1054 Load, Ptr, InsertBef) {
1055 setVolatile(isVolatile);
1056 setAlignment(0);
1057 AssertOK();
1058 if (Name && Name[0]) setName(Name);
1059}
1060
1061LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1062 BasicBlock *InsertAE)
1063 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1064 Load, Ptr, InsertAE) {
1065 setVolatile(isVolatile);
1066 setAlignment(0);
1067 AssertOK();
1068 if (Name && Name[0]) setName(Name);
1069}
1070
Christopher Lamb84485702007-04-22 19:24:39 +00001071void LoadInst::setAlignment(unsigned Align) {
1072 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1073 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1074}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001075
1076//===----------------------------------------------------------------------===//
1077// StoreInst Implementation
1078//===----------------------------------------------------------------------===//
1079
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001080void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001081 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001082 assert(isa<PointerType>(getOperand(1)->getType()) &&
1083 "Ptr must have pointer type!");
1084 assert(getOperand(0)->getType() ==
1085 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001086 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001087}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001088
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001089
1090StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001091 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001092 OperandTraits<StoreInst>::op_begin(this),
1093 OperandTraits<StoreInst>::operands(this),
1094 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001095 Op<0>() = val;
1096 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001097 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001098 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001099 AssertOK();
1100}
1101
1102StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001103 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001104 OperandTraits<StoreInst>::op_begin(this),
1105 OperandTraits<StoreInst>::operands(this),
1106 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001107 Op<0>() = val;
1108 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001109 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001110 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001111 AssertOK();
1112}
1113
Misha Brukmanb1c93172005-04-21 23:48:37 +00001114StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001115 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001116 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001117 OperandTraits<StoreInst>::op_begin(this),
1118 OperandTraits<StoreInst>::operands(this),
1119 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001120 Op<0>() = val;
1121 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001122 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001123 setAlignment(0);
1124 AssertOK();
1125}
1126
1127StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1128 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001129 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001130 OperandTraits<StoreInst>::op_begin(this),
1131 OperandTraits<StoreInst>::operands(this),
1132 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001133 Op<0>() = val;
1134 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001135 setVolatile(isVolatile);
1136 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001137 AssertOK();
1138}
1139
Misha Brukmanb1c93172005-04-21 23:48:37 +00001140StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001141 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001142 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001143 OperandTraits<StoreInst>::op_begin(this),
1144 OperandTraits<StoreInst>::operands(this),
1145 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001146 Op<0>() = val;
1147 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001148 setVolatile(isVolatile);
1149 setAlignment(Align);
1150 AssertOK();
1151}
1152
1153StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001154 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001155 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001156 OperandTraits<StoreInst>::op_begin(this),
1157 OperandTraits<StoreInst>::operands(this),
1158 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001159 Op<0>() = val;
1160 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001161 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001162 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001163 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001164}
1165
Christopher Lamb84485702007-04-22 19:24:39 +00001166void StoreInst::setAlignment(unsigned Align) {
1167 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1168 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1169}
1170
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001171//===----------------------------------------------------------------------===//
1172// GetElementPtrInst Implementation
1173//===----------------------------------------------------------------------===//
1174
Christopher Lambedf07882007-12-17 01:12:55 +00001175static unsigned retrieveAddrSpace(const Value *Val) {
1176 return cast<PointerType>(Val->getType())->getAddressSpace();
1177}
1178
Gabor Greif21ba1842008-06-06 20:28:12 +00001179void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001180 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001181 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1182 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001183 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001184
Chris Lattner79807c3d2007-01-31 19:47:18 +00001185 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001186 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001187
1188 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001189}
1190
Daniel Dunbar4975db62009-07-25 04:41:11 +00001191void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001192 assert(NumOperands == 2 && "NumOperands not initialized?");
1193 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001194 OL[0] = Ptr;
1195 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001196
1197 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001198}
1199
Gabor Greiff6caff662008-05-10 08:32:32 +00001200GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001201 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001202 OperandTraits<GetElementPtrInst>::op_end(this)
1203 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001204 GEPI.getNumOperands()) {
1205 Use *OL = OperandList;
1206 Use *GEPIOL = GEPI.OperandList;
1207 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001208 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001209 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001210}
1211
Chris Lattner82981202005-05-03 05:43:30 +00001212GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001213 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001214 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001215 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001216 GetElementPtr,
1217 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1218 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001219 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001220}
1221
1222GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001223 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001224 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001225 checkType(getIndexedType(Ptr->getType(),Idx)),
1226 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001227 GetElementPtr,
1228 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1229 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001230 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001231}
1232
Chris Lattner6090a422009-03-09 04:46:40 +00001233/// getIndexedType - Returns the type of the element that would be accessed with
1234/// a gep instruction with the specified parameters.
1235///
1236/// The Idxs pointer should point to a continuous piece of memory containing the
1237/// indices, either as Value* or uint64_t.
1238///
1239/// A null type is returned if the indices are invalid for the specified
1240/// pointer type.
1241///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001242template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001243static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1244 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001245 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1246 if (!PTy) return 0; // Type isn't a pointer type!
1247 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001248
Chris Lattner6090a422009-03-09 04:46:40 +00001249 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001250 if (NumIdx == 0)
1251 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001252
1253 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001254 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1255 // that contain opaque types) under the assumption that it will be resolved to
1256 // a sane type later.
1257 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001258 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001259
Dan Gohman1ecaf452008-05-31 00:58:22 +00001260 unsigned CurIdx = 1;
1261 for (; CurIdx != NumIdx; ++CurIdx) {
1262 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1263 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001264 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001265 if (!CT->indexValid(Index)) return 0;
1266 Agg = CT->getTypeAtIndex(Index);
1267
1268 // If the new type forwards to another type, then it is in the middle
1269 // of being refined to another type (and hence, may have dropped all
1270 // references to what it was using before). So, use the new forwarded
1271 // type.
1272 if (const Type *Ty = Agg->getForwardedType())
1273 Agg = Ty;
1274 }
1275 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001276}
1277
Matthijs Kooijman04468622008-07-29 08:46:11 +00001278const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1279 Value* const *Idxs,
1280 unsigned NumIdx) {
1281 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1282}
1283
1284const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1285 uint64_t const *Idxs,
1286 unsigned NumIdx) {
1287 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1288}
1289
Chris Lattner82981202005-05-03 05:43:30 +00001290const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1291 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1292 if (!PTy) return 0; // Type isn't a pointer type!
1293
1294 // Check the pointer index.
1295 if (!PTy->indexValid(Idx)) return 0;
1296
Chris Lattnerc2233332005-05-03 16:44:45 +00001297 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001298}
1299
Chris Lattner45f15572007-04-14 00:12:57 +00001300
1301/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1302/// zeros. If so, the result pointer and the first operand have the same
1303/// value, just potentially different types.
1304bool GetElementPtrInst::hasAllZeroIndices() const {
1305 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1306 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1307 if (!CI->isZero()) return false;
1308 } else {
1309 return false;
1310 }
1311 }
1312 return true;
1313}
1314
Chris Lattner27058292007-04-27 20:35:56 +00001315/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1316/// constant integers. If so, the result pointer and the first operand have
1317/// a constant offset between them.
1318bool GetElementPtrInst::hasAllConstantIndices() const {
1319 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1320 if (!isa<ConstantInt>(getOperand(i)))
1321 return false;
1322 }
1323 return true;
1324}
1325
Dan Gohman1b849082009-09-07 23:54:19 +00001326void GetElementPtrInst::setIsInBounds(bool B) {
1327 cast<GEPOperator>(this)->setIsInBounds(B);
1328}
Chris Lattner45f15572007-04-14 00:12:57 +00001329
Nick Lewycky28a5f252009-09-27 21:33:04 +00001330bool GetElementPtrInst::isInBounds() const {
1331 return cast<GEPOperator>(this)->isInBounds();
1332}
1333
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001334//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001335// ExtractElementInst Implementation
1336//===----------------------------------------------------------------------===//
1337
1338ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001339 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001340 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001341 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001342 ExtractElement,
1343 OperandTraits<ExtractElementInst>::op_begin(this),
1344 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001345 assert(isValidOperands(Val, Index) &&
1346 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001347 Op<0>() = Val;
1348 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001349 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001350}
1351
1352ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001353 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001354 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001355 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001356 ExtractElement,
1357 OperandTraits<ExtractElementInst>::op_begin(this),
1358 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001359 assert(isValidOperands(Val, Index) &&
1360 "Invalid extractelement instruction operands!");
1361
Gabor Greif2d3024d2008-05-26 21:33:52 +00001362 Op<0>() = Val;
1363 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001364 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001365}
1366
Chris Lattner65511ff2006-10-05 06:24:58 +00001367
Chris Lattner54865b32006-04-08 04:05:48 +00001368bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Owen Anderson55f1c092009-08-13 21:58:54 +00001369 if (!isa<VectorType>(Val->getType()) ||
1370 Index->getType() != Type::getInt32Ty(Val->getContext()))
Chris Lattner54865b32006-04-08 04:05:48 +00001371 return false;
1372 return true;
1373}
1374
1375
Robert Bocchino23004482006-01-10 19:05:34 +00001376//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001377// InsertElementInst Implementation
1378//===----------------------------------------------------------------------===//
1379
Chris Lattner54865b32006-04-08 04:05:48 +00001380InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001381 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001382 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001383 : Instruction(Vec->getType(), InsertElement,
1384 OperandTraits<InsertElementInst>::op_begin(this),
1385 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001386 assert(isValidOperands(Vec, Elt, Index) &&
1387 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001388 Op<0>() = Vec;
1389 Op<1>() = Elt;
1390 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001391 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001392}
1393
Chris Lattner54865b32006-04-08 04:05:48 +00001394InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001395 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001396 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001397 : Instruction(Vec->getType(), InsertElement,
1398 OperandTraits<InsertElementInst>::op_begin(this),
1399 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001400 assert(isValidOperands(Vec, Elt, Index) &&
1401 "Invalid insertelement instruction operands!");
1402
Gabor Greif2d3024d2008-05-26 21:33:52 +00001403 Op<0>() = Vec;
1404 Op<1>() = Elt;
1405 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001406 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001407}
1408
Chris Lattner54865b32006-04-08 04:05:48 +00001409bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1410 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001411 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001412 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001413
Reid Spencerd84d35b2007-02-15 02:26:10 +00001414 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001415 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001416
Owen Anderson55f1c092009-08-13 21:58:54 +00001417 if (Index->getType() != Type::getInt32Ty(Vec->getContext()))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001418 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001419 return true;
1420}
1421
1422
Robert Bocchinoca27f032006-01-17 20:07:22 +00001423//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001424// ShuffleVectorInst Implementation
1425//===----------------------------------------------------------------------===//
1426
1427ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001428 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001429 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001430: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001431 cast<VectorType>(Mask->getType())->getNumElements()),
1432 ShuffleVector,
1433 OperandTraits<ShuffleVectorInst>::op_begin(this),
1434 OperandTraits<ShuffleVectorInst>::operands(this),
1435 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001436 assert(isValidOperands(V1, V2, Mask) &&
1437 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001438 Op<0>() = V1;
1439 Op<1>() = V2;
1440 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001441 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001442}
1443
1444ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001445 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001446 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001447: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1448 cast<VectorType>(Mask->getType())->getNumElements()),
1449 ShuffleVector,
1450 OperandTraits<ShuffleVectorInst>::op_begin(this),
1451 OperandTraits<ShuffleVectorInst>::operands(this),
1452 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001453 assert(isValidOperands(V1, V2, Mask) &&
1454 "Invalid shuffle vector instruction operands!");
1455
Gabor Greif2d3024d2008-05-26 21:33:52 +00001456 Op<0>() = V1;
1457 Op<1>() = V2;
1458 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001459 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001460}
1461
Mon P Wang25f01062008-11-10 04:46:22 +00001462bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001463 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001464 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001465 return false;
1466
1467 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1468 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Owen Anderson55f1c092009-08-13 21:58:54 +00001469 MaskTy->getElementType() != Type::getInt32Ty(V1->getContext()))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001470 return false;
1471 return true;
1472}
1473
Chris Lattnerf724e342008-03-02 05:28:33 +00001474/// getMaskValue - Return the index from the shuffle mask for the specified
1475/// output result. This is either -1 if the element is undef or a number less
1476/// than 2*numelements.
1477int ShuffleVectorInst::getMaskValue(unsigned i) const {
1478 const Constant *Mask = cast<Constant>(getOperand(2));
1479 if (isa<UndefValue>(Mask)) return -1;
1480 if (isa<ConstantAggregateZero>(Mask)) return 0;
1481 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1482 assert(i < MaskCV->getNumOperands() && "Index out of range");
1483
1484 if (isa<UndefValue>(MaskCV->getOperand(i)))
1485 return -1;
1486 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1487}
1488
Dan Gohman12fce772008-05-15 19:50:34 +00001489//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001490// InsertValueInst Class
1491//===----------------------------------------------------------------------===//
1492
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001493void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001494 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001495 assert(NumOperands == 2 && "NumOperands not initialized?");
1496 Op<0>() = Agg;
1497 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001498
Dan Gohman1ecaf452008-05-31 00:58:22 +00001499 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001500 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001501}
1502
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001503void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001504 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001505 assert(NumOperands == 2 && "NumOperands not initialized?");
1506 Op<0>() = Agg;
1507 Op<1>() = Val;
1508
1509 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001510 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001511}
1512
1513InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001514 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001515 OperandTraits<InsertValueInst>::op_begin(this), 2),
1516 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001517 Op<0>() = IVI.getOperand(0);
1518 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001519 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001520}
1521
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001522InsertValueInst::InsertValueInst(Value *Agg,
1523 Value *Val,
1524 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001525 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001526 Instruction *InsertBefore)
1527 : Instruction(Agg->getType(), InsertValue,
1528 OperandTraits<InsertValueInst>::op_begin(this),
1529 2, InsertBefore) {
1530 init(Agg, Val, Idx, Name);
1531}
1532
1533InsertValueInst::InsertValueInst(Value *Agg,
1534 Value *Val,
1535 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001536 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001537 BasicBlock *InsertAtEnd)
1538 : Instruction(Agg->getType(), InsertValue,
1539 OperandTraits<InsertValueInst>::op_begin(this),
1540 2, InsertAtEnd) {
1541 init(Agg, Val, Idx, Name);
1542}
1543
Dan Gohman0752bff2008-05-23 00:36:11 +00001544//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001545// ExtractValueInst Class
1546//===----------------------------------------------------------------------===//
1547
Gabor Greifcbcc4952008-06-06 21:06:32 +00001548void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001549 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001550 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001551
Dan Gohman1ecaf452008-05-31 00:58:22 +00001552 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001553 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001554}
1555
Daniel Dunbar4975db62009-07-25 04:41:11 +00001556void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001557 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001558
1559 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001560 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001561}
1562
1563ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001564 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001565 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001566 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001567}
1568
Dan Gohman12fce772008-05-15 19:50:34 +00001569// getIndexedType - Returns the type of the element that would be extracted
1570// with an extractvalue instruction with the specified parameters.
1571//
1572// A null type is returned if the indices are invalid for the specified
1573// pointer type.
1574//
1575const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001576 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001577 unsigned NumIdx) {
1578 unsigned CurIdx = 0;
1579 for (; CurIdx != NumIdx; ++CurIdx) {
1580 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001581 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1582 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001583 if (!CT->indexValid(Index)) return 0;
1584 Agg = CT->getTypeAtIndex(Index);
1585
1586 // If the new type forwards to another type, then it is in the middle
1587 // of being refined to another type (and hence, may have dropped all
1588 // references to what it was using before). So, use the new forwarded
1589 // type.
1590 if (const Type *Ty = Agg->getForwardedType())
1591 Agg = Ty;
1592 }
1593 return CurIdx == NumIdx ? Agg : 0;
1594}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001595
Dan Gohman4a3125b2008-06-17 21:07:55 +00001596const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001597 unsigned Idx) {
1598 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001599}
1600
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001601//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001602// BinaryOperator Class
1603//===----------------------------------------------------------------------===//
1604
Dan Gohmana5b96452009-06-04 22:49:04 +00001605/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1606/// type is floating-point, to help provide compatibility with an older API.
1607///
1608static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1609 const Type *Ty) {
1610 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1611 if (Ty->isFPOrFPVector()) {
1612 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1613 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1614 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1615 }
1616 return iType;
1617}
1618
Chris Lattner2195fc42007-02-24 00:55:48 +00001619BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001620 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001621 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001622 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001623 OperandTraits<BinaryOperator>::op_begin(this),
1624 OperandTraits<BinaryOperator>::operands(this),
1625 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001626 Op<0>() = S1;
1627 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001628 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001629 setName(Name);
1630}
1631
1632BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001633 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001634 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001635 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001636 OperandTraits<BinaryOperator>::op_begin(this),
1637 OperandTraits<BinaryOperator>::operands(this),
1638 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001639 Op<0>() = S1;
1640 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001641 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001642 setName(Name);
1643}
1644
1645
1646void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001647 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001648 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001649 assert(LHS->getType() == RHS->getType() &&
1650 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001651#ifndef NDEBUG
1652 switch (iType) {
1653 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001654 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001655 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001656 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001657 assert(getType()->isIntOrIntVector() &&
1658 "Tried to create an integer operation on a non-integer type!");
1659 break;
1660 case FAdd: case FSub:
1661 case FMul:
1662 assert(getType() == LHS->getType() &&
1663 "Arithmetic operation should return same type as operands!");
1664 assert(getType()->isFPOrFPVector() &&
1665 "Tried to create a floating-point operation on a "
1666 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001667 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001668 case UDiv:
1669 case SDiv:
1670 assert(getType() == LHS->getType() &&
1671 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001672 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1673 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001674 "Incorrect operand type (not integer) for S/UDIV");
1675 break;
1676 case FDiv:
1677 assert(getType() == LHS->getType() &&
1678 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001679 assert(getType()->isFPOrFPVector() &&
1680 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001681 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001682 case URem:
1683 case SRem:
1684 assert(getType() == LHS->getType() &&
1685 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001686 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1687 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001688 "Incorrect operand type (not integer) for S/UREM");
1689 break;
1690 case FRem:
1691 assert(getType() == LHS->getType() &&
1692 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001693 assert(getType()->isFPOrFPVector() &&
1694 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001695 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001696 case Shl:
1697 case LShr:
1698 case AShr:
1699 assert(getType() == LHS->getType() &&
1700 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001701 assert((getType()->isInteger() ||
1702 (isa<VectorType>(getType()) &&
1703 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1704 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001705 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001706 case And: case Or:
1707 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001708 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001709 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001710 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001711 (isa<VectorType>(getType()) &&
1712 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001713 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001714 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001715 default:
1716 break;
1717 }
1718#endif
1719}
1720
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001721BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001722 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001723 Instruction *InsertBefore) {
1724 assert(S1->getType() == S2->getType() &&
1725 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001726 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001727}
1728
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001729BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001730 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001731 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001732 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001733 InsertAtEnd->getInstList().push_back(Res);
1734 return Res;
1735}
1736
Dan Gohman5476cfd2009-08-12 16:23:25 +00001737BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001738 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001739 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001740 return new BinaryOperator(Instruction::Sub,
1741 zero, Op,
1742 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001743}
1744
Dan Gohman5476cfd2009-08-12 16:23:25 +00001745BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001746 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001747 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001748 return new BinaryOperator(Instruction::Sub,
1749 zero, Op,
1750 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001751}
1752
Dan Gohman5476cfd2009-08-12 16:23:25 +00001753BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001754 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001755 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001756 return new BinaryOperator(Instruction::FSub,
1757 zero, Op,
1758 Op->getType(), Name, InsertBefore);
1759}
1760
Dan Gohman5476cfd2009-08-12 16:23:25 +00001761BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001762 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001763 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001764 return new BinaryOperator(Instruction::FSub,
1765 zero, Op,
1766 Op->getType(), Name, InsertAtEnd);
1767}
1768
Dan Gohman5476cfd2009-08-12 16:23:25 +00001769BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001770 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001771 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001772 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001773 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001774 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001775 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001776 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001777 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001778 }
1779
1780 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001781 Op->getType(), Name, InsertBefore);
1782}
1783
Dan Gohman5476cfd2009-08-12 16:23:25 +00001784BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001785 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001786 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001787 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001788 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001789 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001790 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001791 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001792 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001793 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001794 }
1795
1796 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001797 Op->getType(), Name, InsertAtEnd);
1798}
1799
1800
1801// isConstantAllOnes - Helper function for several functions below
1802static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001803 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1804 return CI->isAllOnesValue();
1805 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1806 return CV->isAllOnesValue();
1807 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001808}
1809
Owen Andersonbb2501b2009-07-13 22:18:28 +00001810bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001811 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1812 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001813 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1814 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001815 return false;
1816}
1817
Owen Andersonbb2501b2009-07-13 22:18:28 +00001818bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001819 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1820 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001821 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001822 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001823 return false;
1824}
1825
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001826bool BinaryOperator::isNot(const Value *V) {
1827 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1828 return (Bop->getOpcode() == Instruction::Xor &&
1829 (isConstantAllOnes(Bop->getOperand(1)) ||
1830 isConstantAllOnes(Bop->getOperand(0))));
1831 return false;
1832}
1833
Chris Lattner2c7d1772005-04-24 07:28:37 +00001834Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001835 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001836}
1837
Chris Lattner2c7d1772005-04-24 07:28:37 +00001838const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1839 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001840}
1841
Dan Gohmana5b96452009-06-04 22:49:04 +00001842Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001843 return cast<BinaryOperator>(BinOp)->getOperand(1);
1844}
1845
1846const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1847 return getFNegArgument(const_cast<Value*>(BinOp));
1848}
1849
Chris Lattner2c7d1772005-04-24 07:28:37 +00001850Value *BinaryOperator::getNotArgument(Value *BinOp) {
1851 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1852 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1853 Value *Op0 = BO->getOperand(0);
1854 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001855 if (isConstantAllOnes(Op0)) return Op1;
1856
1857 assert(isConstantAllOnes(Op1));
1858 return Op0;
1859}
1860
Chris Lattner2c7d1772005-04-24 07:28:37 +00001861const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1862 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001863}
1864
1865
1866// swapOperands - Exchange the two operands to this instruction. This
1867// instruction is safe to use on any binary instruction and does not
1868// modify the semantics of the instruction. If the instruction is
1869// order dependent (SetLT f.e.) the opcode is changed.
1870//
1871bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001872 if (!isCommutative())
1873 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001874 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001875 return false;
1876}
1877
Dan Gohman1b849082009-09-07 23:54:19 +00001878void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1879 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1880}
1881
1882void BinaryOperator::setHasNoSignedWrap(bool b) {
1883 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1884}
1885
1886void BinaryOperator::setIsExact(bool b) {
1887 cast<SDivOperator>(this)->setIsExact(b);
1888}
1889
Nick Lewycky28a5f252009-09-27 21:33:04 +00001890bool BinaryOperator::hasNoUnsignedWrap() const {
1891 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1892}
1893
1894bool BinaryOperator::hasNoSignedWrap() const {
1895 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1896}
1897
1898bool BinaryOperator::isExact() const {
1899 return cast<SDivOperator>(this)->isExact();
1900}
1901
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001902//===----------------------------------------------------------------------===//
1903// CastInst Class
1904//===----------------------------------------------------------------------===//
1905
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001906// Just determine if this cast only deals with integral->integral conversion.
1907bool CastInst::isIntegerCast() const {
1908 switch (getOpcode()) {
1909 default: return false;
1910 case Instruction::ZExt:
1911 case Instruction::SExt:
1912 case Instruction::Trunc:
1913 return true;
1914 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001915 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001916 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001917}
1918
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001919bool CastInst::isLosslessCast() const {
1920 // Only BitCast can be lossless, exit fast if we're not BitCast
1921 if (getOpcode() != Instruction::BitCast)
1922 return false;
1923
1924 // Identity cast is always lossless
1925 const Type* SrcTy = getOperand(0)->getType();
1926 const Type* DstTy = getType();
1927 if (SrcTy == DstTy)
1928 return true;
1929
Reid Spencer8d9336d2006-12-31 05:26:44 +00001930 // Pointer to pointer is always lossless.
1931 if (isa<PointerType>(SrcTy))
1932 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001933 return false; // Other types have no identity values
1934}
1935
1936/// This function determines if the CastInst does not require any bits to be
1937/// changed in order to effect the cast. Essentially, it identifies cases where
1938/// no code gen is necessary for the cast, hence the name no-op cast. For
1939/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001940/// # bitcast i32* %x to i8*
1941/// # bitcast <2 x i32> %x to <4 x i16>
1942/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001943/// @brief Determine if a cast is a no-op.
1944bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1945 switch (getOpcode()) {
1946 default:
1947 assert(!"Invalid CastOp");
1948 case Instruction::Trunc:
1949 case Instruction::ZExt:
1950 case Instruction::SExt:
1951 case Instruction::FPTrunc:
1952 case Instruction::FPExt:
1953 case Instruction::UIToFP:
1954 case Instruction::SIToFP:
1955 case Instruction::FPToUI:
1956 case Instruction::FPToSI:
1957 return false; // These always modify bits
1958 case Instruction::BitCast:
1959 return true; // BitCast never modifies bits.
1960 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001961 return IntPtrTy->getScalarSizeInBits() ==
1962 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001963 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001964 return IntPtrTy->getScalarSizeInBits() ==
1965 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001966 }
1967}
1968
1969/// This function determines if a pair of casts can be eliminated and what
1970/// opcode should be used in the elimination. This assumes that there are two
1971/// instructions like this:
1972/// * %F = firstOpcode SrcTy %x to MidTy
1973/// * %S = secondOpcode MidTy %F to DstTy
1974/// The function returns a resultOpcode so these two casts can be replaced with:
1975/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1976/// If no such cast is permited, the function returns 0.
1977unsigned CastInst::isEliminableCastPair(
1978 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1979 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1980{
1981 // Define the 144 possibilities for these two cast instructions. The values
1982 // in this matrix determine what to do in a given situation and select the
1983 // case in the switch below. The rows correspond to firstOp, the columns
1984 // correspond to secondOp. In looking at the table below, keep in mind
1985 // the following cast properties:
1986 //
1987 // Size Compare Source Destination
1988 // Operator Src ? Size Type Sign Type Sign
1989 // -------- ------------ ------------------- ---------------------
1990 // TRUNC > Integer Any Integral Any
1991 // ZEXT < Integral Unsigned Integer Any
1992 // SEXT < Integral Signed Integer Any
1993 // FPTOUI n/a FloatPt n/a Integral Unsigned
1994 // FPTOSI n/a FloatPt n/a Integral Signed
1995 // UITOFP n/a Integral Unsigned FloatPt n/a
1996 // SITOFP n/a Integral Signed FloatPt n/a
1997 // FPTRUNC > FloatPt n/a FloatPt n/a
1998 // FPEXT < FloatPt n/a FloatPt n/a
1999 // PTRTOINT n/a Pointer n/a Integral Unsigned
2000 // INTTOPTR n/a Integral Unsigned Pointer n/a
2001 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002002 //
2003 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002004 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2005 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002006 // of the produced value (we no longer know the top-part is all zeros).
2007 // Further this conversion is often much more expensive for typical hardware,
2008 // and causes issues when building libgcc. We disallow fptosi+sext for the
2009 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002010 const unsigned numCastOps =
2011 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2012 static const uint8_t CastResults[numCastOps][numCastOps] = {
2013 // T F F U S F F P I B -+
2014 // R Z S P P I I T P 2 N T |
2015 // U E E 2 2 2 2 R E I T C +- secondOp
2016 // N X X U S F F N X N 2 V |
2017 // C T T I I P P C T T P T -+
2018 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2019 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2020 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002021 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2022 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002023 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2024 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2025 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2026 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2027 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2028 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2029 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2030 };
2031
2032 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2033 [secondOp-Instruction::CastOpsBegin];
2034 switch (ElimCase) {
2035 case 0:
2036 // categorically disallowed
2037 return 0;
2038 case 1:
2039 // allowed, use first cast's opcode
2040 return firstOp;
2041 case 2:
2042 // allowed, use second cast's opcode
2043 return secondOp;
2044 case 3:
2045 // no-op cast in second op implies firstOp as long as the DestTy
2046 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00002047 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002048 return firstOp;
2049 return 0;
2050 case 4:
2051 // no-op cast in second op implies firstOp as long as the DestTy
2052 // is floating point
2053 if (DstTy->isFloatingPoint())
2054 return firstOp;
2055 return 0;
2056 case 5:
2057 // no-op cast in first op implies secondOp as long as the SrcTy
2058 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00002059 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002060 return secondOp;
2061 return 0;
2062 case 6:
2063 // no-op cast in first op implies secondOp as long as the SrcTy
2064 // is a floating point
2065 if (SrcTy->isFloatingPoint())
2066 return secondOp;
2067 return 0;
2068 case 7: {
2069 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002070 if (!IntPtrTy)
2071 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002072 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2073 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002074 if (MidSize >= PtrSize)
2075 return Instruction::BitCast;
2076 return 0;
2077 }
2078 case 8: {
2079 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2080 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2081 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002082 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2083 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002084 if (SrcSize == DstSize)
2085 return Instruction::BitCast;
2086 else if (SrcSize < DstSize)
2087 return firstOp;
2088 return secondOp;
2089 }
2090 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2091 return Instruction::ZExt;
2092 case 10:
2093 // fpext followed by ftrunc is allowed if the bit size returned to is
2094 // the same as the original, in which case its just a bitcast
2095 if (SrcTy == DstTy)
2096 return Instruction::BitCast;
2097 return 0; // If the types are not the same we can't eliminate it.
2098 case 11:
2099 // bitcast followed by ptrtoint is allowed as long as the bitcast
2100 // is a pointer to pointer cast.
2101 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
2102 return secondOp;
2103 return 0;
2104 case 12:
2105 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2106 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
2107 return firstOp;
2108 return 0;
2109 case 13: {
2110 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002111 if (!IntPtrTy)
2112 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002113 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2114 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2115 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002116 if (SrcSize <= PtrSize && SrcSize == DstSize)
2117 return Instruction::BitCast;
2118 return 0;
2119 }
2120 case 99:
2121 // cast combination can't happen (error in input). This is for all cases
2122 // where the MidTy is not the same for the two cast instructions.
2123 assert(!"Invalid Cast Combination");
2124 return 0;
2125 default:
2126 assert(!"Error in CastResults table!!!");
2127 return 0;
2128 }
2129 return 0;
2130}
2131
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002132CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002133 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002134 // Construct and return the appropriate CastInst subclass
2135 switch (op) {
2136 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2137 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2138 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2139 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2140 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2141 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2142 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2143 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2144 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2145 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2146 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2147 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2148 default:
2149 assert(!"Invalid opcode provided");
2150 }
2151 return 0;
2152}
2153
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002154CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002155 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002156 // Construct and return the appropriate CastInst subclass
2157 switch (op) {
2158 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2159 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2160 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2161 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2162 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2163 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2164 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2165 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2166 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2167 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2168 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2169 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2170 default:
2171 assert(!"Invalid opcode provided");
2172 }
2173 return 0;
2174}
2175
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002176CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002177 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002178 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002179 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002180 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2181 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002182}
2183
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002184CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002185 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002186 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002187 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002188 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2189 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002190}
2191
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002192CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002193 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002194 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002195 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002196 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2197 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002198}
2199
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002200CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002201 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002202 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002203 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002204 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2205 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002206}
2207
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002208CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002209 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002210 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002211 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002212 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2213 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002214}
2215
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002216CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002217 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002218 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002219 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002220 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2221 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002222}
2223
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002224CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002225 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002226 BasicBlock *InsertAtEnd) {
2227 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002228 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002229 "Invalid cast");
2230
Chris Lattner03c49532007-01-15 02:27:26 +00002231 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002232 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2233 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002234}
2235
2236/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002237CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002238 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002239 Instruction *InsertBefore) {
2240 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002241 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002242 "Invalid cast");
2243
Chris Lattner03c49532007-01-15 02:27:26 +00002244 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002245 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2246 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002247}
2248
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002249CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002250 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002251 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002252 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002253 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2254 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002255 Instruction::CastOps opcode =
2256 (SrcBits == DstBits ? Instruction::BitCast :
2257 (SrcBits > DstBits ? Instruction::Trunc :
2258 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002259 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002260}
2261
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002262CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002263 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002264 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002265 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2266 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002267 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2268 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002269 Instruction::CastOps opcode =
2270 (SrcBits == DstBits ? Instruction::BitCast :
2271 (SrcBits > DstBits ? Instruction::Trunc :
2272 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002273 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002274}
2275
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002276CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002277 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002278 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002279 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002280 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002281 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2282 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002283 Instruction::CastOps opcode =
2284 (SrcBits == DstBits ? Instruction::BitCast :
2285 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002286 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002287}
2288
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002289CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002290 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002291 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002292 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002293 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002294 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2295 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002296 Instruction::CastOps opcode =
2297 (SrcBits == DstBits ? Instruction::BitCast :
2298 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002299 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002300}
2301
Duncan Sands55e50902008-01-06 10:12:28 +00002302// Check whether it is valid to call getCastOpcode for these types.
2303// This routine must be kept in sync with getCastOpcode.
2304bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2305 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2306 return false;
2307
2308 if (SrcTy == DestTy)
2309 return true;
2310
2311 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002312 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2313 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002314
2315 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002316 if (DestTy->isInteger()) { // Casting to integral
2317 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002318 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002319 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002320 return true;
2321 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002322 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002323 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002324 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002325 return isa<PointerType>(SrcTy);
2326 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002327 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2328 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002329 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002330 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002331 return true;
2332 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002333 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002334 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002335 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002336 return false;
2337 }
2338 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002339 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002340 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002341 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002342 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002343 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002344 return DestPTy->getBitWidth() == SrcBits;
2345 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002346 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2347 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002348 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002349 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002350 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002351 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002352 return false;
2353 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002354 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002355 return false;
2356 }
2357}
2358
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359// Provide a way to get a "cast" where the cast opcode is inferred from the
2360// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002361// logic in the castIsValid function below. This axiom should hold:
2362// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2363// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002364// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002365// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002366Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002367CastInst::getCastOpcode(
2368 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002369 // Get the bit sizes, we'll need these
2370 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002371 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2372 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002373
Duncan Sands55e50902008-01-06 10:12:28 +00002374 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2375 "Only first class types are castable!");
2376
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002377 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002378 if (DestTy->isInteger()) { // Casting to integral
2379 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002380 if (DestBits < SrcBits)
2381 return Trunc; // int -> smaller int
2382 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002383 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384 return SExt; // signed -> SEXT
2385 else
2386 return ZExt; // unsigned -> ZEXT
2387 } else {
2388 return BitCast; // Same size, No-op cast
2389 }
2390 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002391 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392 return FPToSI; // FP -> sint
2393 else
2394 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002395 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002396 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002397 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002398 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002399 return BitCast; // Same size, no-op cast
2400 } else {
2401 assert(isa<PointerType>(SrcTy) &&
2402 "Casting from a value that is not first-class type");
2403 return PtrToInt; // ptr -> int
2404 }
2405 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002406 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002407 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408 return SIToFP; // sint -> FP
2409 else
2410 return UIToFP; // uint -> FP
2411 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2412 if (DestBits < SrcBits) {
2413 return FPTrunc; // FP -> smaller FP
2414 } else if (DestBits > SrcBits) {
2415 return FPExt; // FP -> larger FP
2416 } else {
2417 return BitCast; // same size, no-op cast
2418 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002419 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002421 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002422 PTy = NULL;
2423 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002424 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002425 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002426 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002427 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2428 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002430 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002431 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002432 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002433 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002434 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002436 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437 }
2438 } else if (isa<PointerType>(DestTy)) {
2439 if (isa<PointerType>(SrcTy)) {
2440 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002441 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002442 return IntToPtr; // int -> ptr
2443 } else {
2444 assert(!"Casting pointer to other than pointer or int");
2445 }
2446 } else {
2447 assert(!"Casting to type that is not first-class");
2448 }
2449
2450 // If we fall through to here we probably hit an assertion cast above
2451 // and assertions are not turned on. Anything we return is an error, so
2452 // BitCast is as good a choice as any.
2453 return BitCast;
2454}
2455
2456//===----------------------------------------------------------------------===//
2457// CastInst SubClass Constructors
2458//===----------------------------------------------------------------------===//
2459
2460/// Check that the construction parameters for a CastInst are correct. This
2461/// could be broken out into the separate constructors but it is useful to have
2462/// it in one place and to eliminate the redundant code for getting the sizes
2463/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002464bool
2465CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002466
2467 // Check for type sanity on the arguments
2468 const Type *SrcTy = S->getType();
2469 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2470 return false;
2471
2472 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002473 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2474 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002475
2476 // Switch on the opcode provided
2477 switch (op) {
2478 default: return false; // This is an input error
2479 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002480 return SrcTy->isIntOrIntVector() &&
2481 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002482 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002483 return SrcTy->isIntOrIntVector() &&
2484 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002485 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002486 return SrcTy->isIntOrIntVector() &&
2487 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002488 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002489 return SrcTy->isFPOrFPVector() &&
2490 DstTy->isFPOrFPVector() &&
2491 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002492 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002493 return SrcTy->isFPOrFPVector() &&
2494 DstTy->isFPOrFPVector() &&
2495 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002496 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002497 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002498 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2499 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002500 return SVTy->getElementType()->isIntOrIntVector() &&
2501 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002502 SVTy->getNumElements() == DVTy->getNumElements();
2503 }
2504 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002505 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002506 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002508 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2509 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002510 return SVTy->getElementType()->isFPOrFPVector() &&
2511 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002512 SVTy->getNumElements() == DVTy->getNumElements();
2513 }
2514 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002515 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002517 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002519 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002520 case Instruction::BitCast:
2521 // BitCast implies a no-op cast of type only. No bits change.
2522 // However, you can't cast pointers to anything but pointers.
2523 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2524 return false;
2525
Duncan Sands55e50902008-01-06 10:12:28 +00002526 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002527 // these cases, the cast is okay if the source and destination bit widths
2528 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002529 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530 }
2531}
2532
2533TruncInst::TruncInst(
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, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002536 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002537}
2538
2539TruncInst::TruncInst(
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, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002542 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002543}
2544
2545ZExtInst::ZExtInst(
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, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002548 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002549}
2550
2551ZExtInst::ZExtInst(
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, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002554 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002555}
2556SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002557 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002558) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002559 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002560}
2561
Jeff Cohencc08c832006-12-02 02:22:01 +00002562SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002563 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002565 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002566}
2567
2568FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002569 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002570) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002571 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002572}
2573
2574FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002575 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002576) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002577 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002578}
2579
2580FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002581 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002582) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002583 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002584}
2585
2586FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002587 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002588) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002589 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002590}
2591
2592UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002593 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002595 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002596}
2597
2598UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002599 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002601 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002602}
2603
2604SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002605 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002607 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002608}
2609
2610SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002611 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002613 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002614}
2615
2616FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002617 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002618) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002619 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002620}
2621
2622FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002623 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002624) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002625 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002626}
2627
2628FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002629 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002630) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002631 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002632}
2633
2634FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002635 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002636) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002637 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002638}
2639
2640PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002641 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002643 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002644}
2645
2646PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002647 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002649 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002650}
2651
2652IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002653 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002654) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002655 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002656}
2657
2658IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002659 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002660) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002661 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002662}
2663
2664BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002665 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002666) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002667 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002668}
2669
2670BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002671 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002672) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002673 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002674}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002675
2676//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002677// CmpInst Classes
2678//===----------------------------------------------------------------------===//
2679
Nate Begemand2195702008-05-12 19:01:56 +00002680CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002681 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002682 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002683 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002684 OperandTraits<CmpInst>::op_begin(this),
2685 OperandTraits<CmpInst>::operands(this),
2686 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002687 Op<0>() = LHS;
2688 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002689 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002690 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002691}
Gabor Greiff6caff662008-05-10 08:32:32 +00002692
Nate Begemand2195702008-05-12 19:01:56 +00002693CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002694 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002695 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002696 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002697 OperandTraits<CmpInst>::op_begin(this),
2698 OperandTraits<CmpInst>::operands(this),
2699 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002700 Op<0>() = LHS;
2701 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002702 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002703 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002704}
2705
2706CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002707CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002708 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002709 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002710 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002711 if (InsertBefore)
2712 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2713 S1, S2, Name);
2714 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002715 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002716 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002717 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002718
2719 if (InsertBefore)
2720 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2721 S1, S2, Name);
2722 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002723 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002724 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002725}
2726
2727CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002728CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002729 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002730 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002731 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2732 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002733 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002734 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2735 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002736}
2737
2738void CmpInst::swapOperands() {
2739 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2740 IC->swapOperands();
2741 else
2742 cast<FCmpInst>(this)->swapOperands();
2743}
2744
2745bool CmpInst::isCommutative() {
2746 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2747 return IC->isCommutative();
2748 return cast<FCmpInst>(this)->isCommutative();
2749}
2750
2751bool CmpInst::isEquality() {
2752 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2753 return IC->isEquality();
2754 return cast<FCmpInst>(this)->isEquality();
2755}
2756
2757
Dan Gohman4e724382008-05-31 02:47:54 +00002758CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002759 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002760 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002761 case ICMP_EQ: return ICMP_NE;
2762 case ICMP_NE: return ICMP_EQ;
2763 case ICMP_UGT: return ICMP_ULE;
2764 case ICMP_ULT: return ICMP_UGE;
2765 case ICMP_UGE: return ICMP_ULT;
2766 case ICMP_ULE: return ICMP_UGT;
2767 case ICMP_SGT: return ICMP_SLE;
2768 case ICMP_SLT: return ICMP_SGE;
2769 case ICMP_SGE: return ICMP_SLT;
2770 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002771
Dan Gohman4e724382008-05-31 02:47:54 +00002772 case FCMP_OEQ: return FCMP_UNE;
2773 case FCMP_ONE: return FCMP_UEQ;
2774 case FCMP_OGT: return FCMP_ULE;
2775 case FCMP_OLT: return FCMP_UGE;
2776 case FCMP_OGE: return FCMP_ULT;
2777 case FCMP_OLE: return FCMP_UGT;
2778 case FCMP_UEQ: return FCMP_ONE;
2779 case FCMP_UNE: return FCMP_OEQ;
2780 case FCMP_UGT: return FCMP_OLE;
2781 case FCMP_ULT: return FCMP_OGE;
2782 case FCMP_UGE: return FCMP_OLT;
2783 case FCMP_ULE: return FCMP_OGT;
2784 case FCMP_ORD: return FCMP_UNO;
2785 case FCMP_UNO: return FCMP_ORD;
2786 case FCMP_TRUE: return FCMP_FALSE;
2787 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002788 }
2789}
2790
Reid Spencer266e42b2006-12-23 06:05:41 +00002791ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2792 switch (pred) {
2793 default: assert(! "Unknown icmp predicate!");
2794 case ICMP_EQ: case ICMP_NE:
2795 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2796 return pred;
2797 case ICMP_UGT: return ICMP_SGT;
2798 case ICMP_ULT: return ICMP_SLT;
2799 case ICMP_UGE: return ICMP_SGE;
2800 case ICMP_ULE: return ICMP_SLE;
2801 }
2802}
2803
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002804ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2805 switch (pred) {
2806 default: assert(! "Unknown icmp predicate!");
2807 case ICMP_EQ: case ICMP_NE:
2808 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2809 return pred;
2810 case ICMP_SGT: return ICMP_UGT;
2811 case ICMP_SLT: return ICMP_ULT;
2812 case ICMP_SGE: return ICMP_UGE;
2813 case ICMP_SLE: return ICMP_ULE;
2814 }
2815}
2816
Reid Spencer266e42b2006-12-23 06:05:41 +00002817bool ICmpInst::isSignedPredicate(Predicate pred) {
2818 switch (pred) {
2819 default: assert(! "Unknown icmp predicate!");
2820 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2821 return true;
2822 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2823 case ICMP_UGE: case ICMP_ULE:
2824 return false;
2825 }
2826}
2827
Reid Spencer0286bc12007-02-28 22:00:54 +00002828/// Initialize a set of values that all satisfy the condition with C.
2829///
2830ConstantRange
2831ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2832 APInt Lower(C);
2833 APInt Upper(C);
2834 uint32_t BitWidth = C.getBitWidth();
2835 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002836 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002837 case ICmpInst::ICMP_EQ: Upper++; break;
2838 case ICmpInst::ICMP_NE: Lower++; break;
2839 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2840 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2841 case ICmpInst::ICMP_UGT:
2842 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2843 break;
2844 case ICmpInst::ICMP_SGT:
2845 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2846 break;
2847 case ICmpInst::ICMP_ULE:
2848 Lower = APInt::getMinValue(BitWidth); Upper++;
2849 break;
2850 case ICmpInst::ICMP_SLE:
2851 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2852 break;
2853 case ICmpInst::ICMP_UGE:
2854 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2855 break;
2856 case ICmpInst::ICMP_SGE:
2857 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2858 break;
2859 }
2860 return ConstantRange(Lower, Upper);
2861}
2862
Dan Gohman4e724382008-05-31 02:47:54 +00002863CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002864 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002865 default: assert(!"Unknown cmp predicate!");
2866 case ICMP_EQ: case ICMP_NE:
2867 return pred;
2868 case ICMP_SGT: return ICMP_SLT;
2869 case ICMP_SLT: return ICMP_SGT;
2870 case ICMP_SGE: return ICMP_SLE;
2871 case ICMP_SLE: return ICMP_SGE;
2872 case ICMP_UGT: return ICMP_ULT;
2873 case ICMP_ULT: return ICMP_UGT;
2874 case ICMP_UGE: return ICMP_ULE;
2875 case ICMP_ULE: return ICMP_UGE;
2876
Reid Spencerd9436b62006-11-20 01:22:35 +00002877 case FCMP_FALSE: case FCMP_TRUE:
2878 case FCMP_OEQ: case FCMP_ONE:
2879 case FCMP_UEQ: case FCMP_UNE:
2880 case FCMP_ORD: case FCMP_UNO:
2881 return pred;
2882 case FCMP_OGT: return FCMP_OLT;
2883 case FCMP_OLT: return FCMP_OGT;
2884 case FCMP_OGE: return FCMP_OLE;
2885 case FCMP_OLE: return FCMP_OGE;
2886 case FCMP_UGT: return FCMP_ULT;
2887 case FCMP_ULT: return FCMP_UGT;
2888 case FCMP_UGE: return FCMP_ULE;
2889 case FCMP_ULE: return FCMP_UGE;
2890 }
2891}
2892
Reid Spencer266e42b2006-12-23 06:05:41 +00002893bool CmpInst::isUnsigned(unsigned short predicate) {
2894 switch (predicate) {
2895 default: return false;
2896 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2897 case ICmpInst::ICMP_UGE: return true;
2898 }
2899}
2900
2901bool CmpInst::isSigned(unsigned short predicate){
2902 switch (predicate) {
2903 default: return false;
2904 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2905 case ICmpInst::ICMP_SGE: return true;
2906 }
2907}
2908
2909bool CmpInst::isOrdered(unsigned short predicate) {
2910 switch (predicate) {
2911 default: return false;
2912 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2913 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2914 case FCmpInst::FCMP_ORD: return true;
2915 }
2916}
2917
2918bool CmpInst::isUnordered(unsigned short predicate) {
2919 switch (predicate) {
2920 default: return false;
2921 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2922 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2923 case FCmpInst::FCMP_UNO: return true;
2924 }
2925}
2926
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002927//===----------------------------------------------------------------------===//
2928// SwitchInst Implementation
2929//===----------------------------------------------------------------------===//
2930
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002931void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002932 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002933 ReservedSpace = 2+NumCases*2;
2934 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002935 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002936
Gabor Greif2d3024d2008-05-26 21:33:52 +00002937 OperandList[0] = Value;
2938 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002939}
2940
Chris Lattner2195fc42007-02-24 00:55:48 +00002941/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2942/// switch on and a default destination. The number of additional cases can
2943/// be specified here to make memory allocation more efficient. This
2944/// constructor can also autoinsert before another instruction.
2945SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2946 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002947 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2948 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002949 init(Value, Default, NumCases);
2950}
2951
2952/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2953/// switch on and a default destination. The number of additional cases can
2954/// be specified here to make memory allocation more efficient. This
2955/// constructor also autoinserts at the end of the specified BasicBlock.
2956SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2957 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002958 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2959 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002960 init(Value, Default, NumCases);
2961}
2962
Misha Brukmanb1c93172005-04-21 23:48:37 +00002963SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002964 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002965 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002966 Use *OL = OperandList, *InOL = SI.OperandList;
2967 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002968 OL[i] = InOL[i];
2969 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002970 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002971 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002972}
2973
Gordon Henriksen14a55692007-12-10 02:14:30 +00002974SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002975 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002976}
2977
2978
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002979/// addCase - Add an entry to the switch instruction...
2980///
Chris Lattner47ac1872005-02-24 05:32:09 +00002981void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002982 unsigned OpNo = NumOperands;
2983 if (OpNo+2 > ReservedSpace)
2984 resizeOperands(0); // Get more space!
2985 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002986 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002987 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002988 OperandList[OpNo] = OnVal;
2989 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002990}
2991
2992/// removeCase - This method removes the specified successor from the switch
2993/// instruction. Note that this cannot be used to remove the default
2994/// destination (successor #0).
2995///
2996void SwitchInst::removeCase(unsigned idx) {
2997 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002998 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2999
3000 unsigned NumOps = getNumOperands();
3001 Use *OL = OperandList;
3002
3003 // Move everything after this operand down.
3004 //
3005 // FIXME: we could just swap with the end of the list, then erase. However,
3006 // client might not expect this to happen. The code as it is thrashes the
3007 // use/def lists, which is kinda lame.
3008 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3009 OL[i-2] = OL[i];
3010 OL[i-2+1] = OL[i+1];
3011 }
3012
3013 // Nuke the last value.
3014 OL[NumOps-2].set(0);
3015 OL[NumOps-2+1].set(0);
3016 NumOperands = NumOps-2;
3017}
3018
3019/// resizeOperands - resize operands - This adjusts the length of the operands
3020/// list according to the following behavior:
3021/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003022/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003023/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3024/// 3. If NumOps == NumOperands, trim the reserved space.
3025///
3026void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003027 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003028 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003029 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003030 } else if (NumOps*2 > NumOperands) {
3031 // No resize needed.
3032 if (ReservedSpace >= NumOps) return;
3033 } else if (NumOps == NumOperands) {
3034 if (ReservedSpace == NumOps) return;
3035 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003036 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003037 }
3038
3039 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003040 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003041 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003042 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003043 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003044 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003045 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003046 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003047}
3048
3049
3050BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3051 return getSuccessor(idx);
3052}
3053unsigned SwitchInst::getNumSuccessorsV() const {
3054 return getNumSuccessors();
3055}
3056void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3057 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003058}
Chris Lattnerf22be932004-10-15 23:52:53 +00003059
Chris Lattnerf22be932004-10-15 23:52:53 +00003060// Define these methods here so vtables don't get emitted into every translation
3061// unit that uses these classes.
3062
Nick Lewycky42fb7452009-09-27 07:38:41 +00003063GetElementPtrInst *GetElementPtrInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003064 GetElementPtrInst *New = new(getNumOperands()) GetElementPtrInst(*this);
3065 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003066 if (hasMetadata()) {
3067 LLVMContext &Context = getContext();
3068 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3069 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003070 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00003071}
3072
Nick Lewycky42fb7452009-09-27 07:38:41 +00003073BinaryOperator *BinaryOperator::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003074 BinaryOperator *New = Create(getOpcode(), Op<0>(), Op<1>());
3075 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003076 if (hasMetadata()) {
3077 LLVMContext &Context = getContext();
3078 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3079 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003080 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00003081}
3082
Nick Lewycky42fb7452009-09-27 07:38:41 +00003083FCmpInst* FCmpInst::clone() const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003084 FCmpInst *New = new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003085 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003086 if (hasMetadata()) {
3087 LLVMContext &Context = getContext();
3088 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3089 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003090 return New;
Chris Lattner0b490b02007-08-24 20:48:18 +00003091}
Nick Lewycky42fb7452009-09-27 07:38:41 +00003092ICmpInst* ICmpInst::clone() const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003093 ICmpInst *New = new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003094 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003095 if (hasMetadata()) {
3096 LLVMContext &Context = getContext();
3097 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3098 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003099 return New;
Reid Spencerd9436b62006-11-20 01:22:35 +00003100}
3101
Nick Lewycky42fb7452009-09-27 07:38:41 +00003102ExtractValueInst *ExtractValueInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003103 ExtractValueInst *New = new ExtractValueInst(*this);
3104 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003105 if (hasMetadata()) {
3106 LLVMContext &Context = getContext();
3107 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3108 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003109 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00003110}
Nick Lewycky42fb7452009-09-27 07:38:41 +00003111InsertValueInst *InsertValueInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003112 InsertValueInst *New = new InsertValueInst(*this);
3113 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003114 if (hasMetadata()) {
3115 LLVMContext &Context = getContext();
3116 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3117 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003118 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00003119}
3120
Nick Lewycky42fb7452009-09-27 07:38:41 +00003121AllocaInst *AllocaInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003122 AllocaInst *New = new AllocaInst(getAllocatedType(),
3123 (Value*)getOperand(0),
3124 getAlignment());
3125 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003126 if (hasMetadata()) {
3127 LLVMContext &Context = getContext();
3128 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3129 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003130 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003131}
3132
Nick Lewycky42fb7452009-09-27 07:38:41 +00003133FreeInst *FreeInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003134 FreeInst *New = new FreeInst(getOperand(0));
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 +00003143LoadInst *LoadInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003144 LoadInst *New = new LoadInst(getOperand(0),
3145 Twine(), isVolatile(),
3146 getAlignment());
3147 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003148 if (hasMetadata()) {
3149 LLVMContext &Context = getContext();
3150 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3151 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003152 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003153}
3154
Nick Lewycky42fb7452009-09-27 07:38:41 +00003155StoreInst *StoreInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003156 StoreInst *New = new StoreInst(getOperand(0), getOperand(1),
3157 isVolatile(), getAlignment());
3158 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003159 if (hasMetadata()) {
3160 LLVMContext &Context = getContext();
3161 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3162 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003163 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003164}
3165
Nick Lewycky42fb7452009-09-27 07:38:41 +00003166TruncInst *TruncInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003167 TruncInst *New = new TruncInst(getOperand(0), getType());
3168 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003169 if (hasMetadata()) {
3170 LLVMContext &Context = getContext();
3171 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3172 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003173 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003174}
3175
Nick Lewycky42fb7452009-09-27 07:38:41 +00003176ZExtInst *ZExtInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003177 ZExtInst *New = new ZExtInst(getOperand(0), getType());
3178 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003179 if (hasMetadata()) {
3180 LLVMContext &Context = getContext();
3181 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3182 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003183 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003184}
3185
Nick Lewycky42fb7452009-09-27 07:38:41 +00003186SExtInst *SExtInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003187 SExtInst *New = new SExtInst(getOperand(0), getType());
3188 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003189 if (hasMetadata()) {
3190 LLVMContext &Context = getContext();
3191 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3192 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003193 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003194}
3195
Nick Lewycky42fb7452009-09-27 07:38:41 +00003196FPTruncInst *FPTruncInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003197 FPTruncInst *New = new FPTruncInst(getOperand(0), getType());
3198 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003199 if (hasMetadata()) {
3200 LLVMContext &Context = getContext();
3201 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3202 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003203 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003204}
3205
Nick Lewycky42fb7452009-09-27 07:38:41 +00003206FPExtInst *FPExtInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003207 FPExtInst *New = new FPExtInst(getOperand(0), getType());
3208 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003209 if (hasMetadata()) {
3210 LLVMContext &Context = getContext();
3211 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3212 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003213 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003214}
3215
Nick Lewycky42fb7452009-09-27 07:38:41 +00003216UIToFPInst *UIToFPInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003217 UIToFPInst *New = new UIToFPInst(getOperand(0), getType());
3218 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003219 if (hasMetadata()) {
3220 LLVMContext &Context = getContext();
3221 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3222 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003223 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003224}
3225
Nick Lewycky42fb7452009-09-27 07:38:41 +00003226SIToFPInst *SIToFPInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003227 SIToFPInst *New = new SIToFPInst(getOperand(0), getType());
3228 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003229 if (hasMetadata()) {
3230 LLVMContext &Context = getContext();
3231 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3232 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003233 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003234}
3235
Nick Lewycky42fb7452009-09-27 07:38:41 +00003236FPToUIInst *FPToUIInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003237 FPToUIInst *New = new FPToUIInst(getOperand(0), getType());
3238 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003239 if (hasMetadata()) {
3240 LLVMContext &Context = getContext();
3241 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3242 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003243 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003244}
3245
Nick Lewycky42fb7452009-09-27 07:38:41 +00003246FPToSIInst *FPToSIInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003247 FPToSIInst *New = new FPToSIInst(getOperand(0), getType());
3248 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003249 if (hasMetadata()) {
3250 LLVMContext &Context = getContext();
3251 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3252 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003253 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003254}
3255
Nick Lewycky42fb7452009-09-27 07:38:41 +00003256PtrToIntInst *PtrToIntInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003257 PtrToIntInst *New = new PtrToIntInst(getOperand(0), getType());
3258 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003259 if (hasMetadata()) {
3260 LLVMContext &Context = getContext();
3261 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3262 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003263 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003264}
3265
Nick Lewycky42fb7452009-09-27 07:38:41 +00003266IntToPtrInst *IntToPtrInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003267 IntToPtrInst *New = new IntToPtrInst(getOperand(0), getType());
3268 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003269 if (hasMetadata()) {
3270 LLVMContext &Context = getContext();
3271 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3272 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003273 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003274}
3275
Nick Lewycky42fb7452009-09-27 07:38:41 +00003276BitCastInst *BitCastInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003277 BitCastInst *New = new BitCastInst(getOperand(0), getType());
3278 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003279 if (hasMetadata()) {
3280 LLVMContext &Context = getContext();
3281 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3282 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003283 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003284}
3285
Nick Lewycky42fb7452009-09-27 07:38:41 +00003286CallInst *CallInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003287 CallInst *New = new(getNumOperands()) CallInst(*this);
3288 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003289 if (hasMetadata()) {
3290 LLVMContext &Context = getContext();
3291 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3292 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003293 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003294}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003295
Nick Lewycky42fb7452009-09-27 07:38:41 +00003296SelectInst *SelectInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003297 SelectInst *New = SelectInst::Create(getOperand(0),
3298 getOperand(1),
3299 getOperand(2));
3300 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003301 if (hasMetadata()) {
3302 LLVMContext &Context = getContext();
3303 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3304 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003305 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003306}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003307
Nick Lewycky42fb7452009-09-27 07:38:41 +00003308VAArgInst *VAArgInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003309 VAArgInst *New = new VAArgInst(getOperand(0), getType());
3310 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003311 if (hasMetadata()) {
3312 LLVMContext &Context = getContext();
3313 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3314 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003315 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003316}
3317
Nick Lewycky42fb7452009-09-27 07:38:41 +00003318ExtractElementInst *ExtractElementInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003319 ExtractElementInst *New = ExtractElementInst::Create(getOperand(0),
3320 getOperand(1));
3321 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003322 if (hasMetadata()) {
3323 LLVMContext &Context = getContext();
3324 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3325 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003326 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003327}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003328
Nick Lewycky42fb7452009-09-27 07:38:41 +00003329InsertElementInst *InsertElementInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003330 InsertElementInst *New = InsertElementInst::Create(getOperand(0),
3331 getOperand(1),
3332 getOperand(2));
3333 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003334 if (hasMetadata()) {
3335 LLVMContext &Context = getContext();
3336 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3337 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003338 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003339}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003340
Nick Lewycky42fb7452009-09-27 07:38:41 +00003341ShuffleVectorInst *ShuffleVectorInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003342 ShuffleVectorInst *New = new ShuffleVectorInst(getOperand(0),
3343 getOperand(1),
3344 getOperand(2));
3345 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003346 if (hasMetadata()) {
3347 LLVMContext &Context = getContext();
3348 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3349 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003350 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003351}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003352
Nick Lewycky42fb7452009-09-27 07:38:41 +00003353PHINode *PHINode::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003354 PHINode *New = new PHINode(*this);
3355 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003356 if (hasMetadata()) {
3357 LLVMContext &Context = getContext();
3358 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3359 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003360 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003361}
3362
Nick Lewycky42fb7452009-09-27 07:38:41 +00003363ReturnInst *ReturnInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003364 ReturnInst *New = new(getNumOperands()) ReturnInst(*this);
3365 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003366 if (hasMetadata()) {
3367 LLVMContext &Context = getContext();
3368 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3369 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003370 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003371}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003372
Nick Lewycky42fb7452009-09-27 07:38:41 +00003373BranchInst *BranchInst::clone() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003374 unsigned Ops(getNumOperands());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003375 BranchInst *New = new(Ops, Ops == 1) BranchInst(*this);
3376 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003377 if (hasMetadata()) {
3378 LLVMContext &Context = getContext();
3379 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3380 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003381 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003382}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003383
Nick Lewycky42fb7452009-09-27 07:38:41 +00003384SwitchInst *SwitchInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003385 SwitchInst *New = new SwitchInst(*this);
3386 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003387 if (hasMetadata()) {
3388 LLVMContext &Context = getContext();
3389 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3390 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003391 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003392}
3393
Nick Lewycky42fb7452009-09-27 07:38:41 +00003394InvokeInst *InvokeInst::clone() const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003395 InvokeInst *New = new(getNumOperands()) InvokeInst(*this);
3396 New->SubclassOptionalData = SubclassOptionalData;
Devang Pateladd58652009-09-23 18:32:25 +00003397 if (hasMetadata()) {
3398 LLVMContext &Context = getContext();
3399 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3400 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003401 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003402}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003403
Nick Lewycky42fb7452009-09-27 07:38:41 +00003404UnwindInst *UnwindInst::clone() const {
3405 LLVMContext &Context = getContext();
3406 UnwindInst *New = new UnwindInst(Context);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003407 New->SubclassOptionalData = SubclassOptionalData;
Nick Lewycky42fb7452009-09-27 07:38:41 +00003408 if (hasMetadata())
Devang Pateladd58652009-09-23 18:32:25 +00003409 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003410 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003411}
3412
Nick Lewycky42fb7452009-09-27 07:38:41 +00003413UnreachableInst *UnreachableInst::clone() const {
3414 LLVMContext &Context = getContext();
3415 UnreachableInst *New = new UnreachableInst(Context);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003416 New->SubclassOptionalData = SubclassOptionalData;
Nick Lewycky42fb7452009-09-27 07:38:41 +00003417 if (hasMetadata())
Devang Pateladd58652009-09-23 18:32:25 +00003418 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003419 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003420}