blob: 2d4ab557217653e107b91a27571f266d549a59c6 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000015#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Function.h"
18#include "llvm/Instructions.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000019#include "llvm/Operator.h"
Dan Gohman22571482009-09-03 15:34:35 +000020#include "llvm/Analysis/Dominators.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000021#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000022#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000023#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000024#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000025using namespace llvm;
26
Chris Lattner3e13b8c2008-01-02 23:42:30 +000027//===----------------------------------------------------------------------===//
28// CallSite Class
29//===----------------------------------------------------------------------===//
30
Gabor Greif1b9921a2009-01-11 22:33:22 +000031#define CALLSITE_DELEGATE_GETTER(METHOD) \
32 Instruction *II(getInstruction()); \
33 return isCall() \
34 ? cast<CallInst>(II)->METHOD \
35 : cast<InvokeInst>(II)->METHOD
36
37#define CALLSITE_DELEGATE_SETTER(METHOD) \
38 Instruction *II(getInstruction()); \
39 if (isCall()) \
40 cast<CallInst>(II)->METHOD; \
41 else \
42 cast<InvokeInst>(II)->METHOD
43
Duncan Sands85fab3a2008-02-18 17:32:13 +000044CallSite::CallSite(Instruction *C) {
45 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
Gabor Greif1b9921a2009-01-11 22:33:22 +000046 I.setPointer(C);
47 I.setInt(isa<CallInst>(C));
Duncan Sands85fab3a2008-02-18 17:32:13 +000048}
Sandeep Patel68c5f472009-09-02 08:44:58 +000049CallingConv::ID CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000050 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000051}
Sandeep Patel68c5f472009-09-02 08:44:58 +000052void CallSite::setCallingConv(CallingConv::ID CC) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000053 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
Chris Lattnerf7b6d312005-05-06 20:26:43 +000054}
Devang Patel4c758ea2008-09-25 21:00:45 +000055const AttrListPtr &CallSite::getAttributes() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000056 CALLSITE_DELEGATE_GETTER(getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000057}
Devang Patel4c758ea2008-09-25 21:00:45 +000058void CallSite::setAttributes(const AttrListPtr &PAL) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000059 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000060}
Devang Patelba3fa6c2008-09-23 23:03:40 +000061bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000062 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
Duncan Sands5208d1a2007-11-28 17:07:01 +000063}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000064uint16_t CallSite::getParamAlignment(uint16_t i) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000065 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
Dale Johanneseneabc5f32008-02-22 17:49:45 +000066}
Duncan Sands38ef3a82007-12-03 20:06:50 +000067bool CallSite::doesNotAccessMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000068 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000069}
Duncan Sands78c88722008-07-08 08:38:44 +000070void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000071 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000072}
Duncan Sands38ef3a82007-12-03 20:06:50 +000073bool CallSite::onlyReadsMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000074 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000075}
Duncan Sands78c88722008-07-08 08:38:44 +000076void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000077 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000078}
79bool CallSite::doesNotReturn() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000080 CALLSITE_DELEGATE_GETTER(doesNotReturn());
Duncan Sands78c88722008-07-08 08:38:44 +000081}
82void CallSite::setDoesNotReturn(bool doesNotReturn) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000083 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
Duncan Sands78c88722008-07-08 08:38:44 +000084}
Duncan Sands3353ed02007-12-18 09:59:50 +000085bool CallSite::doesNotThrow() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000086 CALLSITE_DELEGATE_GETTER(doesNotThrow());
Duncan Sands8e4847e2007-12-16 15:51:49 +000087}
Duncan Sandsaa31b922007-12-19 21:13:37 +000088void CallSite::setDoesNotThrow(bool doesNotThrow) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000089 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
Duncan Sandsaa31b922007-12-19 21:13:37 +000090}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000091
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000092bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000093 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
94 if (AI->get() == Arg)
95 return true;
96 return false;
97}
98
Gabor Greif1b9921a2009-01-11 22:33:22 +000099#undef CALLSITE_DELEGATE_GETTER
100#undef CALLSITE_DELEGATE_SETTER
101
Gordon Henriksen14a55692007-12-10 02:14:30 +0000102//===----------------------------------------------------------------------===//
103// TerminatorInst Class
104//===----------------------------------------------------------------------===//
105
106// Out of line virtual method, so the vtable, etc has a home.
107TerminatorInst::~TerminatorInst() {
108}
109
Gabor Greiff6caff662008-05-10 08:32:32 +0000110//===----------------------------------------------------------------------===//
111// UnaryInstruction Class
112//===----------------------------------------------------------------------===//
113
Gordon Henriksen14a55692007-12-10 02:14:30 +0000114// Out of line virtual method, so the vtable, etc has a home.
115UnaryInstruction::~UnaryInstruction() {
116}
117
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000118//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000119// SelectInst Class
120//===----------------------------------------------------------------------===//
121
122/// areInvalidOperands - Return a string if the specified operands are invalid
123/// for a select operation, otherwise return null.
124const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
125 if (Op1->getType() != Op2->getType())
126 return "both values to select must have same type";
127
128 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
129 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000130 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000131 return "vector select condition element type must be i1";
132 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
133 if (ET == 0)
134 return "selected values for vector select must be vectors";
135 if (ET->getNumElements() != VT->getNumElements())
136 return "vector select requires selected vectors to have "
137 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000138 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000139 return "select condition must be i1 or <n x i1>";
140 }
141 return 0;
142}
143
144
145//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000146// PHINode Class
147//===----------------------------------------------------------------------===//
148
149PHINode::PHINode(const PHINode &PN)
150 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000151 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000152 ReservedSpace(PN.getNumOperands()) {
153 Use *OL = OperandList;
154 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000155 OL[i] = PN.getOperand(i);
156 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000157 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000158 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000159}
160
Gordon Henriksen14a55692007-12-10 02:14:30 +0000161PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000162 if (OperandList)
163 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000164}
165
166// removeIncomingValue - Remove an incoming value. This is useful if a
167// predecessor basic block is deleted.
168Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
169 unsigned NumOps = getNumOperands();
170 Use *OL = OperandList;
171 assert(Idx*2 < NumOps && "BB not in PHI node!");
172 Value *Removed = OL[Idx*2];
173
174 // Move everything after this operand down.
175 //
176 // FIXME: we could just swap with the end of the list, then erase. However,
177 // client might not expect this to happen. The code as it is thrashes the
178 // use/def lists, which is kinda lame.
179 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
180 OL[i-2] = OL[i];
181 OL[i-2+1] = OL[i+1];
182 }
183
184 // Nuke the last value.
185 OL[NumOps-2].set(0);
186 OL[NumOps-2+1].set(0);
187 NumOperands = NumOps-2;
188
189 // If the PHI node is dead, because it has zero entries, nuke it now.
190 if (NumOps == 2 && DeletePHIIfEmpty) {
191 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000192 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000193 eraseFromParent();
194 }
195 return Removed;
196}
197
198/// resizeOperands - resize operands - This adjusts the length of the operands
199/// list according to the following behavior:
200/// 1. If NumOps == 0, grow the operand list in response to a push_back style
201/// of operation. This grows the number of ops by 1.5 times.
202/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
203/// 3. If NumOps == NumOperands, trim the reserved space.
204///
205void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000206 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000207 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000208 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000209 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
210 } else if (NumOps*2 > NumOperands) {
211 // No resize needed.
212 if (ReservedSpace >= NumOps) return;
213 } else if (NumOps == NumOperands) {
214 if (ReservedSpace == NumOps) return;
215 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000216 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000217 }
218
219 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000220 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000221 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000222 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000223 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000224 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000225}
226
Nate Begemanb3923212005-08-04 23:24:19 +0000227/// hasConstantValue - If the specified PHI node always merges together the same
228/// value, return the value, otherwise return null.
229///
Dan Gohman22571482009-09-03 15:34:35 +0000230/// If the PHI has undef operands, but all the rest of the operands are
231/// some unique value, return that value if it can be proved that the
232/// value dominates the PHI. If DT is null, use a conservative check,
233/// otherwise use DT to test for dominance.
234///
235Value *PHINode::hasConstantValue(DominatorTree *DT) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000236 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000237 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000238 if (getIncomingValue(0) != this) // not X = phi X
239 return getIncomingValue(0);
240 else
Owen Andersonb292b8c2009-07-30 23:03:37 +0000241 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000242 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000243
Nate Begemanb3923212005-08-04 23:24:19 +0000244 // Otherwise if all of the incoming values are the same for the PHI, replace
245 // the PHI node with the incoming value.
246 //
247 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000248 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000249 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000250 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000251 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000252 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000253 if (InVal && getIncomingValue(i) != InVal)
254 return 0; // Not the same, bail out.
255 else
256 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000257 }
Nate Begemanb3923212005-08-04 23:24:19 +0000258
259 // The only case that could cause InVal to be null is if we have a PHI node
260 // that only has entries for itself. In this case, there is no entry into the
261 // loop, so kill the PHI.
262 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000263 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000264
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000265 // If we have a PHI node like phi(X, undef, X), where X is defined by some
266 // instruction, we cannot always return X as the result of the PHI node. Only
267 // do this if X is not an instruction (thus it must dominate the PHI block),
268 // or if the client is prepared to deal with this possibility.
Dan Gohman22571482009-09-03 15:34:35 +0000269 if (HasUndefInput)
270 if (Instruction *IV = dyn_cast<Instruction>(InVal)) {
271 if (DT) {
272 // We have a DominatorTree. Do a precise test.
273 if (!DT->dominates(IV, this))
274 return 0;
275 } else {
276 // If it's in the entry block, it dominates everything.
277 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
278 isa<InvokeInst>(IV))
279 return 0; // Cannot guarantee that InVal dominates this PHINode.
280 }
281 }
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000282
Nate Begemanb3923212005-08-04 23:24:19 +0000283 // All of the incoming values are the same, return the value now.
284 return InVal;
285}
286
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000287
288//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289// CallInst Implementation
290//===----------------------------------------------------------------------===//
291
Gordon Henriksen14a55692007-12-10 02:14:30 +0000292CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000293}
294
Chris Lattner054ba2c2007-02-13 00:58:44 +0000295void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000296 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
297 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000298 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299
Misha Brukmanb1c93172005-04-21 23:48:37 +0000300 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000301 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000302 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303
Chris Lattner054ba2c2007-02-13 00:58:44 +0000304 assert((NumParams == FTy->getNumParams() ||
305 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000306 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000307 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000308 assert((i >= FTy->getNumParams() ||
309 FTy->getParamType(i) == Params[i]->getType()) &&
310 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000311 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000312 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313}
314
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000315void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000316 assert(NumOperands == 3 && "NumOperands not set up?");
317 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000318 OL[0] = Func;
319 OL[1] = Actual1;
320 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000321
322 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000324 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000325
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000326 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000327 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000328 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000329 assert((0 >= FTy->getNumParams() ||
330 FTy->getParamType(0) == Actual1->getType()) &&
331 "Calling a function with a bad signature!");
332 assert((1 >= FTy->getNumParams() ||
333 FTy->getParamType(1) == Actual2->getType()) &&
334 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335}
336
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000337void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000338 assert(NumOperands == 2 && "NumOperands not set up?");
339 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000340 OL[0] = Func;
341 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000342
343 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000344 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000345 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000346
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000347 assert((FTy->getNumParams() == 1 ||
348 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000349 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000350 assert((0 == FTy->getNumParams() ||
351 FTy->getParamType(0) == Actual->getType()) &&
352 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000353}
354
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000355void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000356 assert(NumOperands == 1 && "NumOperands not set up?");
357 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000358 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000359
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000360 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000361 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000362 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000363
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000364 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000365}
366
Daniel Dunbar4975db62009-07-25 04:41:11 +0000367CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000368 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000369 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
370 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000371 Instruction::Call,
372 OperandTraits<CallInst>::op_end(this) - 2,
373 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000374 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000375 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000376}
377
Daniel Dunbar4975db62009-07-25 04:41:11 +0000378CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000379 BasicBlock *InsertAtEnd)
380 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
381 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000382 Instruction::Call,
383 OperandTraits<CallInst>::op_end(this) - 2,
384 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000385 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000386 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000387}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000388CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000389 Instruction *InsertBefore)
390 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
391 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000392 Instruction::Call,
393 OperandTraits<CallInst>::op_end(this) - 1,
394 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000395 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000396 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000397}
398
Daniel Dunbar4975db62009-07-25 04:41:11 +0000399CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000400 BasicBlock *InsertAtEnd)
401 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
402 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000403 Instruction::Call,
404 OperandTraits<CallInst>::op_end(this) - 1,
405 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000406 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000407 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000408}
409
Misha Brukmanb1c93172005-04-21 23:48:37 +0000410CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000411 : Instruction(CI.getType(), Instruction::Call,
412 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000413 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000414 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000415 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000416 Use *OL = OperandList;
417 Use *InOL = CI.OperandList;
418 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000419 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000420 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000421}
422
Devang Patel4c758ea2008-09-25 21:00:45 +0000423void CallInst::addAttribute(unsigned i, Attributes attr) {
424 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000425 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000426 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000427}
428
Devang Patel4c758ea2008-09-25 21:00:45 +0000429void CallInst::removeAttribute(unsigned i, Attributes attr) {
430 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000431 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000432 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000433}
434
Devang Patelba3fa6c2008-09-23 23:03:40 +0000435bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000436 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000437 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000438 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000439 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000440 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000441}
442
Duncan Sands5208d1a2007-11-28 17:07:01 +0000443
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000444//===----------------------------------------------------------------------===//
445// InvokeInst Implementation
446//===----------------------------------------------------------------------===//
447
448void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000449 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000450 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000451 Use *OL = OperandList;
452 OL[0] = Fn;
453 OL[1] = IfNormal;
454 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000455 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000456 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000457 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000458
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000459 assert(((NumArgs == FTy->getNumParams()) ||
460 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000461 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000462
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000463 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000464 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000465 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000466 "Invoking a function with a bad signature!");
467
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000468 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000469 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000470}
471
Misha Brukmanb1c93172005-04-21 23:48:37 +0000472InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000473 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000474 OperandTraits<InvokeInst>::op_end(this)
475 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000476 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000477 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000478 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000479 Use *OL = OperandList, *InOL = II.OperandList;
480 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000481 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000482 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000483}
484
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000485BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
486 return getSuccessor(idx);
487}
488unsigned InvokeInst::getNumSuccessorsV() const {
489 return getNumSuccessors();
490}
491void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
492 return setSuccessor(idx, B);
493}
494
Devang Patelba3fa6c2008-09-23 23:03:40 +0000495bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000496 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000497 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000498 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000499 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000500 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000501}
502
Devang Patel4c758ea2008-09-25 21:00:45 +0000503void InvokeInst::addAttribute(unsigned i, Attributes attr) {
504 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000505 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000506 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000507}
508
Devang Patel4c758ea2008-09-25 21:00:45 +0000509void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
510 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000511 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000512 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000513}
514
Duncan Sands5208d1a2007-11-28 17:07:01 +0000515
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000516//===----------------------------------------------------------------------===//
517// ReturnInst Implementation
518//===----------------------------------------------------------------------===//
519
Chris Lattner2195fc42007-02-24 00:55:48 +0000520ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000521 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000522 OperandTraits<ReturnInst>::op_end(this) -
523 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000524 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000525 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000526 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000527 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000528}
529
Owen Anderson55f1c092009-08-13 21:58:54 +0000530ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
531 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000532 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
533 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000534 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000535 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000536}
Owen Anderson55f1c092009-08-13 21:58:54 +0000537ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
538 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000539 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
540 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000541 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000542 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000543}
Owen Anderson55f1c092009-08-13 21:58:54 +0000544ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
545 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000546 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000547}
548
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000549unsigned ReturnInst::getNumSuccessorsV() const {
550 return getNumSuccessors();
551}
552
Devang Patelae682fb2008-02-26 17:56:20 +0000553/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
554/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000555void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000556 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000557}
558
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000559BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000560 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000561 return 0;
562}
563
Devang Patel59643e52008-02-23 00:35:18 +0000564ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000565}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000566
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000567//===----------------------------------------------------------------------===//
568// UnwindInst Implementation
569//===----------------------------------------------------------------------===//
570
Owen Anderson55f1c092009-08-13 21:58:54 +0000571UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
572 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
573 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000574}
Owen Anderson55f1c092009-08-13 21:58:54 +0000575UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
576 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
577 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000578}
579
580
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000581unsigned UnwindInst::getNumSuccessorsV() const {
582 return getNumSuccessors();
583}
584
585void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000586 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000587}
588
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000589BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000590 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000591 return 0;
592}
593
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000594//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000595// UnreachableInst Implementation
596//===----------------------------------------------------------------------===//
597
Owen Anderson55f1c092009-08-13 21:58:54 +0000598UnreachableInst::UnreachableInst(LLVMContext &Context,
599 Instruction *InsertBefore)
600 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
601 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000602}
Owen Anderson55f1c092009-08-13 21:58:54 +0000603UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
604 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
605 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000606}
607
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000608unsigned UnreachableInst::getNumSuccessorsV() const {
609 return getNumSuccessors();
610}
611
612void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000613 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000614}
615
616BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000617 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000618 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000619}
620
621//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000622// BranchInst Implementation
623//===----------------------------------------------------------------------===//
624
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000625void BranchInst::AssertOK() {
626 if (isConditional())
Owen Anderson55f1c092009-08-13 21:58:54 +0000627 assert(getCondition()->getType() == Type::getInt1Ty(getContext()) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000628 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000629}
630
Chris Lattner2195fc42007-02-24 00:55:48 +0000631BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000632 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000633 OperandTraits<BranchInst>::op_end(this) - 1,
634 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000635 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000636 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000637}
638BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
639 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000640 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000641 OperandTraits<BranchInst>::op_end(this) - 3,
642 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000643 Op<-1>() = IfTrue;
644 Op<-2>() = IfFalse;
645 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000646#ifndef NDEBUG
647 AssertOK();
648#endif
649}
650
651BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000652 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000653 OperandTraits<BranchInst>::op_end(this) - 1,
654 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000655 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000656 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000657}
658
659BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
660 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000661 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000662 OperandTraits<BranchInst>::op_end(this) - 3,
663 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000664 Op<-1>() = IfTrue;
665 Op<-2>() = IfFalse;
666 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000667#ifndef NDEBUG
668 AssertOK();
669#endif
670}
671
672
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000673BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000674 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000675 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
676 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000677 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000678 if (BI.getNumOperands() != 1) {
679 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000680 Op<-3>() = BI.Op<-3>();
681 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000682 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000683 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684}
685
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000686
687Use* Use::getPrefix() {
688 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
689 if (PotentialPrefix.getOpaqueValue())
690 return 0;
691
692 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
693}
694
695BranchInst::~BranchInst() {
696 if (NumOperands == 1) {
697 if (Use *Prefix = OperandList->getPrefix()) {
698 Op<-1>() = 0;
699 //
700 // mark OperandList to have a special value for scrutiny
701 // by baseclass destructors and operator delete
702 OperandList = Prefix;
703 } else {
704 NumOperands = 3;
705 OperandList = op_begin();
706 }
707 }
708}
709
710
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000711BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
712 return getSuccessor(idx);
713}
714unsigned BranchInst::getNumSuccessorsV() const {
715 return getNumSuccessors();
716}
717void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
718 setSuccessor(idx, B);
719}
720
721
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000722//===----------------------------------------------------------------------===//
723// AllocationInst Implementation
724//===----------------------------------------------------------------------===//
725
Owen Andersonb6b25302009-07-14 23:09:55 +0000726static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000727 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000728 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000729 else {
730 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000731 "Passed basic block into allocation size parameter! Use other ctor");
Owen Anderson55f1c092009-08-13 21:58:54 +0000732 assert(Amt->getType() == Type::getInt32Ty(Context) &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000733 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000734 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000735 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000736}
737
Owen Anderson4fdeba92009-07-15 23:53:25 +0000738AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000739 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000740 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +0000741 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000742 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000743 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000744 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000745 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000746}
747
Owen Anderson4fdeba92009-07-15 23:53:25 +0000748AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000749 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000750 BasicBlock *InsertAtEnd)
Owen Anderson4056ca92009-07-29 22:17:13 +0000751 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000752 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000753 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000754 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000755 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000756}
757
Gordon Henriksen14a55692007-12-10 02:14:30 +0000758// Out of line virtual method, so the vtable, etc has a home.
759AllocationInst::~AllocationInst() {
760}
761
Dan Gohmanaa583d72008-03-24 16:55:58 +0000762void AllocationInst::setAlignment(unsigned Align) {
763 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
764 SubclassData = Log2_32(Align) + 1;
765 assert(getAlignment() == Align && "Alignment representation error!");
766}
767
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000768bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000769 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
770 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000771 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000772}
773
774const Type *AllocationInst::getAllocatedType() const {
775 return getType()->getElementType();
776}
777
Chris Lattner8b291e62008-11-26 02:54:17 +0000778/// isStaticAlloca - Return true if this alloca is in the entry block of the
779/// function and is a constant size. If so, the code generator will fold it
780/// into the prolog/epilog code, so it is basically free.
781bool AllocaInst::isStaticAlloca() const {
782 // Must be constant size.
783 if (!isa<ConstantInt>(getArraySize())) return false;
784
785 // Must be in the entry block.
786 const BasicBlock *Parent = getParent();
787 return Parent == &Parent->getParent()->front();
788}
789
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000790//===----------------------------------------------------------------------===//
791// FreeInst Implementation
792//===----------------------------------------------------------------------===//
793
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000794void FreeInst::AssertOK() {
795 assert(isa<PointerType>(getOperand(0)->getType()) &&
796 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000797}
798
799FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000800 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
801 Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000802 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803}
804
805FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000806 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
807 Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000808 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000809}
810
811
812//===----------------------------------------------------------------------===//
813// LoadInst Implementation
814//===----------------------------------------------------------------------===//
815
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000816void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000817 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000818 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000819}
820
Daniel Dunbar4975db62009-07-25 04:41:11 +0000821LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000822 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000823 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000824 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000825 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000826 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000827 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000828}
829
Daniel Dunbar4975db62009-07-25 04:41:11 +0000830LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000831 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000832 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000833 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000834 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000835 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000836 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000837}
838
Daniel Dunbar4975db62009-07-25 04:41:11 +0000839LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000840 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000841 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000842 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000843 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000844 setAlignment(0);
845 AssertOK();
846 setName(Name);
847}
848
Daniel Dunbar4975db62009-07-25 04:41:11 +0000849LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000850 unsigned Align, Instruction *InsertBef)
851 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
852 Load, Ptr, InsertBef) {
853 setVolatile(isVolatile);
854 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000855 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000856 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000857}
858
Daniel Dunbar4975db62009-07-25 04:41:11 +0000859LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000860 unsigned Align, BasicBlock *InsertAE)
861 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
862 Load, Ptr, InsertAE) {
863 setVolatile(isVolatile);
864 setAlignment(Align);
865 AssertOK();
866 setName(Name);
867}
868
Daniel Dunbar4975db62009-07-25 04:41:11 +0000869LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000870 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000871 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000872 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000873 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000874 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000875 AssertOK();
876 setName(Name);
877}
878
Daniel Dunbar27096822009-08-11 18:11:15 +0000879
880
881LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
882 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
883 Load, Ptr, InsertBef) {
884 setVolatile(false);
885 setAlignment(0);
886 AssertOK();
887 if (Name && Name[0]) setName(Name);
888}
889
890LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
891 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
892 Load, Ptr, InsertAE) {
893 setVolatile(false);
894 setAlignment(0);
895 AssertOK();
896 if (Name && Name[0]) setName(Name);
897}
898
899LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
900 Instruction *InsertBef)
901: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
902 Load, Ptr, InsertBef) {
903 setVolatile(isVolatile);
904 setAlignment(0);
905 AssertOK();
906 if (Name && Name[0]) setName(Name);
907}
908
909LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
910 BasicBlock *InsertAE)
911 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
912 Load, Ptr, InsertAE) {
913 setVolatile(isVolatile);
914 setAlignment(0);
915 AssertOK();
916 if (Name && Name[0]) setName(Name);
917}
918
Christopher Lamb84485702007-04-22 19:24:39 +0000919void LoadInst::setAlignment(unsigned Align) {
920 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
921 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
922}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000923
924//===----------------------------------------------------------------------===//
925// StoreInst Implementation
926//===----------------------------------------------------------------------===//
927
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000928void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000929 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000930 assert(isa<PointerType>(getOperand(1)->getType()) &&
931 "Ptr must have pointer type!");
932 assert(getOperand(0)->getType() ==
933 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000934 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000935}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000936
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000937
938StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000939 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000940 OperandTraits<StoreInst>::op_begin(this),
941 OperandTraits<StoreInst>::operands(this),
942 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000943 Op<0>() = val;
944 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000945 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000946 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000947 AssertOK();
948}
949
950StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000951 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000952 OperandTraits<StoreInst>::op_begin(this),
953 OperandTraits<StoreInst>::operands(this),
954 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000955 Op<0>() = val;
956 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000957 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000958 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000959 AssertOK();
960}
961
Misha Brukmanb1c93172005-04-21 23:48:37 +0000962StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000963 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000964 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000965 OperandTraits<StoreInst>::op_begin(this),
966 OperandTraits<StoreInst>::operands(this),
967 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000968 Op<0>() = val;
969 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000970 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000971 setAlignment(0);
972 AssertOK();
973}
974
975StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
976 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000977 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000978 OperandTraits<StoreInst>::op_begin(this),
979 OperandTraits<StoreInst>::operands(this),
980 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000981 Op<0>() = val;
982 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000983 setVolatile(isVolatile);
984 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000985 AssertOK();
986}
987
Misha Brukmanb1c93172005-04-21 23:48:37 +0000988StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000989 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000990 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +0000991 OperandTraits<StoreInst>::op_begin(this),
992 OperandTraits<StoreInst>::operands(this),
993 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000994 Op<0>() = val;
995 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000996 setVolatile(isVolatile);
997 setAlignment(Align);
998 AssertOK();
999}
1000
1001StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001002 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001003 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001004 OperandTraits<StoreInst>::op_begin(this),
1005 OperandTraits<StoreInst>::operands(this),
1006 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001007 Op<0>() = val;
1008 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001009 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001010 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001011 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001012}
1013
Christopher Lamb84485702007-04-22 19:24:39 +00001014void StoreInst::setAlignment(unsigned Align) {
1015 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1016 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1017}
1018
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001019//===----------------------------------------------------------------------===//
1020// GetElementPtrInst Implementation
1021//===----------------------------------------------------------------------===//
1022
Christopher Lambedf07882007-12-17 01:12:55 +00001023static unsigned retrieveAddrSpace(const Value *Val) {
1024 return cast<PointerType>(Val->getType())->getAddressSpace();
1025}
1026
Gabor Greif21ba1842008-06-06 20:28:12 +00001027void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001028 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001029 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1030 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001031 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001032
Chris Lattner79807c3d2007-01-31 19:47:18 +00001033 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001034 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001035
1036 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001037}
1038
Daniel Dunbar4975db62009-07-25 04:41:11 +00001039void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001040 assert(NumOperands == 2 && "NumOperands not initialized?");
1041 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001042 OL[0] = Ptr;
1043 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001044
1045 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001046}
1047
Gabor Greiff6caff662008-05-10 08:32:32 +00001048GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001049 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001050 OperandTraits<GetElementPtrInst>::op_end(this)
1051 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001052 GEPI.getNumOperands()) {
1053 Use *OL = OperandList;
1054 Use *GEPIOL = GEPI.OperandList;
1055 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001056 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001057 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001058}
1059
Chris Lattner82981202005-05-03 05:43:30 +00001060GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001061 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001062 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001063 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001064 GetElementPtr,
1065 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1066 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001067 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001068}
1069
1070GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001071 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001072 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001073 checkType(getIndexedType(Ptr->getType(),Idx)),
1074 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001075 GetElementPtr,
1076 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1077 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001078 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001079}
1080
Chris Lattner6090a422009-03-09 04:46:40 +00001081/// getIndexedType - Returns the type of the element that would be accessed with
1082/// a gep instruction with the specified parameters.
1083///
1084/// The Idxs pointer should point to a continuous piece of memory containing the
1085/// indices, either as Value* or uint64_t.
1086///
1087/// A null type is returned if the indices are invalid for the specified
1088/// pointer type.
1089///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001090template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001091static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1092 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001093 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1094 if (!PTy) return 0; // Type isn't a pointer type!
1095 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001096
Chris Lattner6090a422009-03-09 04:46:40 +00001097 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001098 if (NumIdx == 0)
1099 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001100
1101 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001102 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1103 // that contain opaque types) under the assumption that it will be resolved to
1104 // a sane type later.
1105 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001106 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001107
Dan Gohman1ecaf452008-05-31 00:58:22 +00001108 unsigned CurIdx = 1;
1109 for (; CurIdx != NumIdx; ++CurIdx) {
1110 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1111 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001112 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001113 if (!CT->indexValid(Index)) return 0;
1114 Agg = CT->getTypeAtIndex(Index);
1115
1116 // If the new type forwards to another type, then it is in the middle
1117 // of being refined to another type (and hence, may have dropped all
1118 // references to what it was using before). So, use the new forwarded
1119 // type.
1120 if (const Type *Ty = Agg->getForwardedType())
1121 Agg = Ty;
1122 }
1123 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001124}
1125
Matthijs Kooijman04468622008-07-29 08:46:11 +00001126const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1127 Value* const *Idxs,
1128 unsigned NumIdx) {
1129 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1130}
1131
1132const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1133 uint64_t const *Idxs,
1134 unsigned NumIdx) {
1135 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1136}
1137
Chris Lattner82981202005-05-03 05:43:30 +00001138const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1139 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1140 if (!PTy) return 0; // Type isn't a pointer type!
1141
1142 // Check the pointer index.
1143 if (!PTy->indexValid(Idx)) return 0;
1144
Chris Lattnerc2233332005-05-03 16:44:45 +00001145 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001146}
1147
Chris Lattner45f15572007-04-14 00:12:57 +00001148
1149/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1150/// zeros. If so, the result pointer and the first operand have the same
1151/// value, just potentially different types.
1152bool GetElementPtrInst::hasAllZeroIndices() const {
1153 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1154 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1155 if (!CI->isZero()) return false;
1156 } else {
1157 return false;
1158 }
1159 }
1160 return true;
1161}
1162
Chris Lattner27058292007-04-27 20:35:56 +00001163/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1164/// constant integers. If so, the result pointer and the first operand have
1165/// a constant offset between them.
1166bool GetElementPtrInst::hasAllConstantIndices() const {
1167 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1168 if (!isa<ConstantInt>(getOperand(i)))
1169 return false;
1170 }
1171 return true;
1172}
1173
Chris Lattner45f15572007-04-14 00:12:57 +00001174
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001175//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001176// ExtractElementInst Implementation
1177//===----------------------------------------------------------------------===//
1178
1179ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001180 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001181 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001182 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001183 ExtractElement,
1184 OperandTraits<ExtractElementInst>::op_begin(this),
1185 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001186 assert(isValidOperands(Val, Index) &&
1187 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001188 Op<0>() = Val;
1189 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001190 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001191}
1192
1193ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001194 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001195 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001196 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001197 ExtractElement,
1198 OperandTraits<ExtractElementInst>::op_begin(this),
1199 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001200 assert(isValidOperands(Val, Index) &&
1201 "Invalid extractelement instruction operands!");
1202
Gabor Greif2d3024d2008-05-26 21:33:52 +00001203 Op<0>() = Val;
1204 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001205 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001206}
1207
Chris Lattner65511ff2006-10-05 06:24:58 +00001208
Chris Lattner54865b32006-04-08 04:05:48 +00001209bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Owen Anderson55f1c092009-08-13 21:58:54 +00001210 if (!isa<VectorType>(Val->getType()) ||
1211 Index->getType() != Type::getInt32Ty(Val->getContext()))
Chris Lattner54865b32006-04-08 04:05:48 +00001212 return false;
1213 return true;
1214}
1215
1216
Robert Bocchino23004482006-01-10 19:05:34 +00001217//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001218// InsertElementInst Implementation
1219//===----------------------------------------------------------------------===//
1220
Chris Lattner54865b32006-04-08 04:05:48 +00001221InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001222 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001223 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001224 : Instruction(Vec->getType(), InsertElement,
1225 OperandTraits<InsertElementInst>::op_begin(this),
1226 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001227 assert(isValidOperands(Vec, Elt, Index) &&
1228 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001229 Op<0>() = Vec;
1230 Op<1>() = Elt;
1231 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001232 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001233}
1234
Chris Lattner54865b32006-04-08 04:05:48 +00001235InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001236 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001237 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001238 : Instruction(Vec->getType(), InsertElement,
1239 OperandTraits<InsertElementInst>::op_begin(this),
1240 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001241 assert(isValidOperands(Vec, Elt, Index) &&
1242 "Invalid insertelement instruction operands!");
1243
Gabor Greif2d3024d2008-05-26 21:33:52 +00001244 Op<0>() = Vec;
1245 Op<1>() = Elt;
1246 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001247 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001248}
1249
Chris Lattner54865b32006-04-08 04:05:48 +00001250bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1251 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001252 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001253 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001254
Reid Spencerd84d35b2007-02-15 02:26:10 +00001255 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001256 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001257
Owen Anderson55f1c092009-08-13 21:58:54 +00001258 if (Index->getType() != Type::getInt32Ty(Vec->getContext()))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001259 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001260 return true;
1261}
1262
1263
Robert Bocchinoca27f032006-01-17 20:07:22 +00001264//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001265// ShuffleVectorInst Implementation
1266//===----------------------------------------------------------------------===//
1267
1268ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001269 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001270 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001271: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001272 cast<VectorType>(Mask->getType())->getNumElements()),
1273 ShuffleVector,
1274 OperandTraits<ShuffleVectorInst>::op_begin(this),
1275 OperandTraits<ShuffleVectorInst>::operands(this),
1276 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001277 assert(isValidOperands(V1, V2, Mask) &&
1278 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001279 Op<0>() = V1;
1280 Op<1>() = V2;
1281 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001282 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001283}
1284
1285ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001286 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001287 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001288: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1289 cast<VectorType>(Mask->getType())->getNumElements()),
1290 ShuffleVector,
1291 OperandTraits<ShuffleVectorInst>::op_begin(this),
1292 OperandTraits<ShuffleVectorInst>::operands(this),
1293 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001294 assert(isValidOperands(V1, V2, Mask) &&
1295 "Invalid shuffle vector instruction operands!");
1296
Gabor Greif2d3024d2008-05-26 21:33:52 +00001297 Op<0>() = V1;
1298 Op<1>() = V2;
1299 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001300 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001301}
1302
Mon P Wang25f01062008-11-10 04:46:22 +00001303bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001304 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001305 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001306 return false;
1307
1308 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1309 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Owen Anderson55f1c092009-08-13 21:58:54 +00001310 MaskTy->getElementType() != Type::getInt32Ty(V1->getContext()))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001311 return false;
1312 return true;
1313}
1314
Chris Lattnerf724e342008-03-02 05:28:33 +00001315/// getMaskValue - Return the index from the shuffle mask for the specified
1316/// output result. This is either -1 if the element is undef or a number less
1317/// than 2*numelements.
1318int ShuffleVectorInst::getMaskValue(unsigned i) const {
1319 const Constant *Mask = cast<Constant>(getOperand(2));
1320 if (isa<UndefValue>(Mask)) return -1;
1321 if (isa<ConstantAggregateZero>(Mask)) return 0;
1322 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1323 assert(i < MaskCV->getNumOperands() && "Index out of range");
1324
1325 if (isa<UndefValue>(MaskCV->getOperand(i)))
1326 return -1;
1327 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1328}
1329
Dan Gohman12fce772008-05-15 19:50:34 +00001330//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001331// InsertValueInst Class
1332//===----------------------------------------------------------------------===//
1333
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001334void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001335 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001336 assert(NumOperands == 2 && "NumOperands not initialized?");
1337 Op<0>() = Agg;
1338 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001339
Dan Gohman1ecaf452008-05-31 00:58:22 +00001340 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001341 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001342}
1343
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001344void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001345 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001346 assert(NumOperands == 2 && "NumOperands not initialized?");
1347 Op<0>() = Agg;
1348 Op<1>() = Val;
1349
1350 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001351 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001352}
1353
1354InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001355 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001356 OperandTraits<InsertValueInst>::op_begin(this), 2),
1357 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001358 Op<0>() = IVI.getOperand(0);
1359 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001360 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001361}
1362
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001363InsertValueInst::InsertValueInst(Value *Agg,
1364 Value *Val,
1365 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001366 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001367 Instruction *InsertBefore)
1368 : Instruction(Agg->getType(), InsertValue,
1369 OperandTraits<InsertValueInst>::op_begin(this),
1370 2, InsertBefore) {
1371 init(Agg, Val, Idx, Name);
1372}
1373
1374InsertValueInst::InsertValueInst(Value *Agg,
1375 Value *Val,
1376 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001377 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001378 BasicBlock *InsertAtEnd)
1379 : Instruction(Agg->getType(), InsertValue,
1380 OperandTraits<InsertValueInst>::op_begin(this),
1381 2, InsertAtEnd) {
1382 init(Agg, Val, Idx, Name);
1383}
1384
Dan Gohman0752bff2008-05-23 00:36:11 +00001385//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001386// ExtractValueInst Class
1387//===----------------------------------------------------------------------===//
1388
Gabor Greifcbcc4952008-06-06 21:06:32 +00001389void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001390 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001391 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001392
Dan Gohman1ecaf452008-05-31 00:58:22 +00001393 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001394 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001395}
1396
Daniel Dunbar4975db62009-07-25 04:41:11 +00001397void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001398 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001399
1400 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001401 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001402}
1403
1404ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001405 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001406 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001407 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001408}
1409
Dan Gohman12fce772008-05-15 19:50:34 +00001410// getIndexedType - Returns the type of the element that would be extracted
1411// with an extractvalue instruction with the specified parameters.
1412//
1413// A null type is returned if the indices are invalid for the specified
1414// pointer type.
1415//
1416const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001417 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001418 unsigned NumIdx) {
1419 unsigned CurIdx = 0;
1420 for (; CurIdx != NumIdx; ++CurIdx) {
1421 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001422 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1423 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001424 if (!CT->indexValid(Index)) return 0;
1425 Agg = CT->getTypeAtIndex(Index);
1426
1427 // If the new type forwards to another type, then it is in the middle
1428 // of being refined to another type (and hence, may have dropped all
1429 // references to what it was using before). So, use the new forwarded
1430 // type.
1431 if (const Type *Ty = Agg->getForwardedType())
1432 Agg = Ty;
1433 }
1434 return CurIdx == NumIdx ? Agg : 0;
1435}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001436
Dan Gohman4a3125b2008-06-17 21:07:55 +00001437const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001438 unsigned Idx) {
1439 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001440}
1441
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001442//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001443// BinaryOperator Class
1444//===----------------------------------------------------------------------===//
1445
Dan Gohmana5b96452009-06-04 22:49:04 +00001446/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1447/// type is floating-point, to help provide compatibility with an older API.
1448///
1449static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1450 const Type *Ty) {
1451 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1452 if (Ty->isFPOrFPVector()) {
1453 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1454 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1455 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1456 }
1457 return iType;
1458}
1459
Chris Lattner2195fc42007-02-24 00:55:48 +00001460BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001461 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001462 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001463 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001464 OperandTraits<BinaryOperator>::op_begin(this),
1465 OperandTraits<BinaryOperator>::operands(this),
1466 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001467 Op<0>() = S1;
1468 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001469 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001470 setName(Name);
1471}
1472
1473BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001474 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001475 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001476 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001477 OperandTraits<BinaryOperator>::op_begin(this),
1478 OperandTraits<BinaryOperator>::operands(this),
1479 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001480 Op<0>() = S1;
1481 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001482 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001483 setName(Name);
1484}
1485
1486
1487void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001488 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001489 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001490 assert(LHS->getType() == RHS->getType() &&
1491 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001492#ifndef NDEBUG
1493 switch (iType) {
1494 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001495 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001496 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001497 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001498 assert(getType()->isIntOrIntVector() &&
1499 "Tried to create an integer operation on a non-integer type!");
1500 break;
1501 case FAdd: case FSub:
1502 case FMul:
1503 assert(getType() == LHS->getType() &&
1504 "Arithmetic operation should return same type as operands!");
1505 assert(getType()->isFPOrFPVector() &&
1506 "Tried to create a floating-point operation on a "
1507 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001508 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001509 case UDiv:
1510 case SDiv:
1511 assert(getType() == LHS->getType() &&
1512 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001513 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1514 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001515 "Incorrect operand type (not integer) for S/UDIV");
1516 break;
1517 case FDiv:
1518 assert(getType() == LHS->getType() &&
1519 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001520 assert(getType()->isFPOrFPVector() &&
1521 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001522 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001523 case URem:
1524 case SRem:
1525 assert(getType() == LHS->getType() &&
1526 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001527 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1528 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001529 "Incorrect operand type (not integer) for S/UREM");
1530 break;
1531 case FRem:
1532 assert(getType() == LHS->getType() &&
1533 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001534 assert(getType()->isFPOrFPVector() &&
1535 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001536 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001537 case Shl:
1538 case LShr:
1539 case AShr:
1540 assert(getType() == LHS->getType() &&
1541 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001542 assert((getType()->isInteger() ||
1543 (isa<VectorType>(getType()) &&
1544 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1545 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001546 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001547 case And: case Or:
1548 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001549 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001550 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001551 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001552 (isa<VectorType>(getType()) &&
1553 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001554 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001555 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001556 default:
1557 break;
1558 }
1559#endif
1560}
1561
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001562BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001563 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001564 Instruction *InsertBefore) {
1565 assert(S1->getType() == S2->getType() &&
1566 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001567 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001568}
1569
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001570BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001571 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001572 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001573 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001574 InsertAtEnd->getInstList().push_back(Res);
1575 return Res;
1576}
1577
Dan Gohman5476cfd2009-08-12 16:23:25 +00001578BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001579 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001580 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001581 return new BinaryOperator(Instruction::Sub,
1582 zero, Op,
1583 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001584}
1585
Dan Gohman5476cfd2009-08-12 16:23:25 +00001586BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001587 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001588 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001589 return new BinaryOperator(Instruction::Sub,
1590 zero, Op,
1591 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001592}
1593
Dan Gohman5476cfd2009-08-12 16:23:25 +00001594BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001595 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001596 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001597 return new BinaryOperator(Instruction::FSub,
1598 zero, Op,
1599 Op->getType(), Name, InsertBefore);
1600}
1601
Dan Gohman5476cfd2009-08-12 16:23:25 +00001602BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001603 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001604 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001605 return new BinaryOperator(Instruction::FSub,
1606 zero, Op,
1607 Op->getType(), Name, InsertAtEnd);
1608}
1609
Dan Gohman5476cfd2009-08-12 16:23:25 +00001610BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001611 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001612 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001613 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001614 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001615 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001616 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001617 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001618 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001619 }
1620
1621 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001622 Op->getType(), Name, InsertBefore);
1623}
1624
Dan Gohman5476cfd2009-08-12 16:23:25 +00001625BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001626 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001627 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001628 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001629 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001630 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001631 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001632 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001633 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001634 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001635 }
1636
1637 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001638 Op->getType(), Name, InsertAtEnd);
1639}
1640
1641
1642// isConstantAllOnes - Helper function for several functions below
1643static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001644 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1645 return CI->isAllOnesValue();
1646 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1647 return CV->isAllOnesValue();
1648 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001649}
1650
Owen Andersonbb2501b2009-07-13 22:18:28 +00001651bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001652 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1653 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001654 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1655 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001656 return false;
1657}
1658
Owen Andersonbb2501b2009-07-13 22:18:28 +00001659bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001660 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1661 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001662 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1663 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001664 return false;
1665}
1666
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001667bool BinaryOperator::isNot(const Value *V) {
1668 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1669 return (Bop->getOpcode() == Instruction::Xor &&
1670 (isConstantAllOnes(Bop->getOperand(1)) ||
1671 isConstantAllOnes(Bop->getOperand(0))));
1672 return false;
1673}
1674
Chris Lattner2c7d1772005-04-24 07:28:37 +00001675Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001676 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001677}
1678
Chris Lattner2c7d1772005-04-24 07:28:37 +00001679const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1680 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001681}
1682
Dan Gohmana5b96452009-06-04 22:49:04 +00001683Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001684 return cast<BinaryOperator>(BinOp)->getOperand(1);
1685}
1686
1687const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1688 return getFNegArgument(const_cast<Value*>(BinOp));
1689}
1690
Chris Lattner2c7d1772005-04-24 07:28:37 +00001691Value *BinaryOperator::getNotArgument(Value *BinOp) {
1692 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1693 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1694 Value *Op0 = BO->getOperand(0);
1695 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001696 if (isConstantAllOnes(Op0)) return Op1;
1697
1698 assert(isConstantAllOnes(Op1));
1699 return Op0;
1700}
1701
Chris Lattner2c7d1772005-04-24 07:28:37 +00001702const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1703 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001704}
1705
1706
1707// swapOperands - Exchange the two operands to this instruction. This
1708// instruction is safe to use on any binary instruction and does not
1709// modify the semantics of the instruction. If the instruction is
1710// order dependent (SetLT f.e.) the opcode is changed.
1711//
1712bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001713 if (!isCommutative())
1714 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001715 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001716 return false;
1717}
1718
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001719//===----------------------------------------------------------------------===//
1720// CastInst Class
1721//===----------------------------------------------------------------------===//
1722
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001723// Just determine if this cast only deals with integral->integral conversion.
1724bool CastInst::isIntegerCast() const {
1725 switch (getOpcode()) {
1726 default: return false;
1727 case Instruction::ZExt:
1728 case Instruction::SExt:
1729 case Instruction::Trunc:
1730 return true;
1731 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001732 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001733 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001734}
1735
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001736bool CastInst::isLosslessCast() const {
1737 // Only BitCast can be lossless, exit fast if we're not BitCast
1738 if (getOpcode() != Instruction::BitCast)
1739 return false;
1740
1741 // Identity cast is always lossless
1742 const Type* SrcTy = getOperand(0)->getType();
1743 const Type* DstTy = getType();
1744 if (SrcTy == DstTy)
1745 return true;
1746
Reid Spencer8d9336d2006-12-31 05:26:44 +00001747 // Pointer to pointer is always lossless.
1748 if (isa<PointerType>(SrcTy))
1749 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001750 return false; // Other types have no identity values
1751}
1752
1753/// This function determines if the CastInst does not require any bits to be
1754/// changed in order to effect the cast. Essentially, it identifies cases where
1755/// no code gen is necessary for the cast, hence the name no-op cast. For
1756/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001757/// # bitcast i32* %x to i8*
1758/// # bitcast <2 x i32> %x to <4 x i16>
1759/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001760/// @brief Determine if a cast is a no-op.
1761bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1762 switch (getOpcode()) {
1763 default:
1764 assert(!"Invalid CastOp");
1765 case Instruction::Trunc:
1766 case Instruction::ZExt:
1767 case Instruction::SExt:
1768 case Instruction::FPTrunc:
1769 case Instruction::FPExt:
1770 case Instruction::UIToFP:
1771 case Instruction::SIToFP:
1772 case Instruction::FPToUI:
1773 case Instruction::FPToSI:
1774 return false; // These always modify bits
1775 case Instruction::BitCast:
1776 return true; // BitCast never modifies bits.
1777 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001778 return IntPtrTy->getScalarSizeInBits() ==
1779 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001780 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001781 return IntPtrTy->getScalarSizeInBits() ==
1782 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001783 }
1784}
1785
1786/// This function determines if a pair of casts can be eliminated and what
1787/// opcode should be used in the elimination. This assumes that there are two
1788/// instructions like this:
1789/// * %F = firstOpcode SrcTy %x to MidTy
1790/// * %S = secondOpcode MidTy %F to DstTy
1791/// The function returns a resultOpcode so these two casts can be replaced with:
1792/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1793/// If no such cast is permited, the function returns 0.
1794unsigned CastInst::isEliminableCastPair(
1795 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1796 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1797{
1798 // Define the 144 possibilities for these two cast instructions. The values
1799 // in this matrix determine what to do in a given situation and select the
1800 // case in the switch below. The rows correspond to firstOp, the columns
1801 // correspond to secondOp. In looking at the table below, keep in mind
1802 // the following cast properties:
1803 //
1804 // Size Compare Source Destination
1805 // Operator Src ? Size Type Sign Type Sign
1806 // -------- ------------ ------------------- ---------------------
1807 // TRUNC > Integer Any Integral Any
1808 // ZEXT < Integral Unsigned Integer Any
1809 // SEXT < Integral Signed Integer Any
1810 // FPTOUI n/a FloatPt n/a Integral Unsigned
1811 // FPTOSI n/a FloatPt n/a Integral Signed
1812 // UITOFP n/a Integral Unsigned FloatPt n/a
1813 // SITOFP n/a Integral Signed FloatPt n/a
1814 // FPTRUNC > FloatPt n/a FloatPt n/a
1815 // FPEXT < FloatPt n/a FloatPt n/a
1816 // PTRTOINT n/a Pointer n/a Integral Unsigned
1817 // INTTOPTR n/a Integral Unsigned Pointer n/a
1818 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001819 //
1820 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001821 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1822 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001823 // of the produced value (we no longer know the top-part is all zeros).
1824 // Further this conversion is often much more expensive for typical hardware,
1825 // and causes issues when building libgcc. We disallow fptosi+sext for the
1826 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001827 const unsigned numCastOps =
1828 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1829 static const uint8_t CastResults[numCastOps][numCastOps] = {
1830 // T F F U S F F P I B -+
1831 // R Z S P P I I T P 2 N T |
1832 // U E E 2 2 2 2 R E I T C +- secondOp
1833 // N X X U S F F N X N 2 V |
1834 // C T T I I P P C T T P T -+
1835 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1836 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1837 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001838 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1839 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001840 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1841 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1842 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1843 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1844 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1845 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1846 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1847 };
1848
1849 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1850 [secondOp-Instruction::CastOpsBegin];
1851 switch (ElimCase) {
1852 case 0:
1853 // categorically disallowed
1854 return 0;
1855 case 1:
1856 // allowed, use first cast's opcode
1857 return firstOp;
1858 case 2:
1859 // allowed, use second cast's opcode
1860 return secondOp;
1861 case 3:
1862 // no-op cast in second op implies firstOp as long as the DestTy
1863 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001864 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001865 return firstOp;
1866 return 0;
1867 case 4:
1868 // no-op cast in second op implies firstOp as long as the DestTy
1869 // is floating point
1870 if (DstTy->isFloatingPoint())
1871 return firstOp;
1872 return 0;
1873 case 5:
1874 // no-op cast in first op implies secondOp as long as the SrcTy
1875 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001876 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001877 return secondOp;
1878 return 0;
1879 case 6:
1880 // no-op cast in first op implies secondOp as long as the SrcTy
1881 // is a floating point
1882 if (SrcTy->isFloatingPoint())
1883 return secondOp;
1884 return 0;
1885 case 7: {
1886 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00001887 if (!IntPtrTy)
1888 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001889 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1890 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001891 if (MidSize >= PtrSize)
1892 return Instruction::BitCast;
1893 return 0;
1894 }
1895 case 8: {
1896 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1897 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1898 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001899 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1900 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001901 if (SrcSize == DstSize)
1902 return Instruction::BitCast;
1903 else if (SrcSize < DstSize)
1904 return firstOp;
1905 return secondOp;
1906 }
1907 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1908 return Instruction::ZExt;
1909 case 10:
1910 // fpext followed by ftrunc is allowed if the bit size returned to is
1911 // the same as the original, in which case its just a bitcast
1912 if (SrcTy == DstTy)
1913 return Instruction::BitCast;
1914 return 0; // If the types are not the same we can't eliminate it.
1915 case 11:
1916 // bitcast followed by ptrtoint is allowed as long as the bitcast
1917 // is a pointer to pointer cast.
1918 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1919 return secondOp;
1920 return 0;
1921 case 12:
1922 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1923 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1924 return firstOp;
1925 return 0;
1926 case 13: {
1927 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00001928 if (!IntPtrTy)
1929 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001930 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1931 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1932 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001933 if (SrcSize <= PtrSize && SrcSize == DstSize)
1934 return Instruction::BitCast;
1935 return 0;
1936 }
1937 case 99:
1938 // cast combination can't happen (error in input). This is for all cases
1939 // where the MidTy is not the same for the two cast instructions.
1940 assert(!"Invalid Cast Combination");
1941 return 0;
1942 default:
1943 assert(!"Error in CastResults table!!!");
1944 return 0;
1945 }
1946 return 0;
1947}
1948
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001949CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001950 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001951 // Construct and return the appropriate CastInst subclass
1952 switch (op) {
1953 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1954 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1955 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1956 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1957 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1958 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1959 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1960 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1961 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1962 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1963 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1964 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1965 default:
1966 assert(!"Invalid opcode provided");
1967 }
1968 return 0;
1969}
1970
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001971CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001972 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001973 // Construct and return the appropriate CastInst subclass
1974 switch (op) {
1975 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1976 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1977 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1978 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1979 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1980 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1981 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1982 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1983 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1984 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1985 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1986 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1987 default:
1988 assert(!"Invalid opcode provided");
1989 }
1990 return 0;
1991}
1992
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001993CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001994 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00001995 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001996 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001997 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1998 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001999}
2000
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002001CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002002 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002003 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002004 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002005 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2006 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002007}
2008
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002009CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002010 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002011 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002012 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002013 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2014 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002015}
2016
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002017CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002018 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002019 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002020 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002021 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2022 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002023}
2024
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002025CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002026 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002027 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002028 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002029 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2030 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002031}
2032
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002033CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002034 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002035 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002036 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002037 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2038 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002039}
2040
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002041CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002042 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002043 BasicBlock *InsertAtEnd) {
2044 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002045 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002046 "Invalid cast");
2047
Chris Lattner03c49532007-01-15 02:27:26 +00002048 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002049 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2050 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002051}
2052
2053/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002054CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002055 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002056 Instruction *InsertBefore) {
2057 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002058 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002059 "Invalid cast");
2060
Chris Lattner03c49532007-01-15 02:27:26 +00002061 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002062 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2063 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002064}
2065
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002066CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002067 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002068 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002069 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002070 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2071 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002072 Instruction::CastOps opcode =
2073 (SrcBits == DstBits ? Instruction::BitCast :
2074 (SrcBits > DstBits ? Instruction::Trunc :
2075 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002076 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002077}
2078
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002079CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002080 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002081 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002082 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2083 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002084 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2085 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002086 Instruction::CastOps opcode =
2087 (SrcBits == DstBits ? Instruction::BitCast :
2088 (SrcBits > DstBits ? Instruction::Trunc :
2089 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002090 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002091}
2092
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002093CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002094 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002095 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002096 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002097 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002098 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2099 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002100 Instruction::CastOps opcode =
2101 (SrcBits == DstBits ? Instruction::BitCast :
2102 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002103 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002104}
2105
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002106CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002107 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002108 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002109 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002110 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002111 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2112 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002113 Instruction::CastOps opcode =
2114 (SrcBits == DstBits ? Instruction::BitCast :
2115 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002116 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002117}
2118
Duncan Sands55e50902008-01-06 10:12:28 +00002119// Check whether it is valid to call getCastOpcode for these types.
2120// This routine must be kept in sync with getCastOpcode.
2121bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2122 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2123 return false;
2124
2125 if (SrcTy == DestTy)
2126 return true;
2127
2128 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002129 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2130 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002131
2132 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002133 if (DestTy->isInteger()) { // Casting to integral
2134 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002135 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002136 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002137 return true;
2138 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002139 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002140 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002141 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002142 return isa<PointerType>(SrcTy);
2143 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002144 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2145 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002146 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002147 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002148 return true;
2149 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002150 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002151 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002152 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002153 return false;
2154 }
2155 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002156 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002157 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002158 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002159 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002160 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002161 return DestPTy->getBitWidth() == SrcBits;
2162 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002163 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2164 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002165 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002166 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002167 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002168 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002169 return false;
2170 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002171 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002172 return false;
2173 }
2174}
2175
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002176// Provide a way to get a "cast" where the cast opcode is inferred from the
2177// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002178// logic in the castIsValid function below. This axiom should hold:
2179// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2180// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002181// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002182// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002183Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002184CastInst::getCastOpcode(
2185 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002186 // Get the bit sizes, we'll need these
2187 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002188 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2189 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002190
Duncan Sands55e50902008-01-06 10:12:28 +00002191 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2192 "Only first class types are castable!");
2193
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002194 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002195 if (DestTy->isInteger()) { // Casting to integral
2196 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002197 if (DestBits < SrcBits)
2198 return Trunc; // int -> smaller int
2199 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002200 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002201 return SExt; // signed -> SEXT
2202 else
2203 return ZExt; // unsigned -> ZEXT
2204 } else {
2205 return BitCast; // Same size, No-op cast
2206 }
2207 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002208 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002209 return FPToSI; // FP -> sint
2210 else
2211 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002212 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002213 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002214 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002215 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002216 return BitCast; // Same size, no-op cast
2217 } else {
2218 assert(isa<PointerType>(SrcTy) &&
2219 "Casting from a value that is not first-class type");
2220 return PtrToInt; // ptr -> int
2221 }
2222 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002223 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002224 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002225 return SIToFP; // sint -> FP
2226 else
2227 return UIToFP; // uint -> FP
2228 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2229 if (DestBits < SrcBits) {
2230 return FPTrunc; // FP -> smaller FP
2231 } else if (DestBits > SrcBits) {
2232 return FPExt; // FP -> larger FP
2233 } else {
2234 return BitCast; // same size, no-op cast
2235 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002236 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002238 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002239 PTy = NULL;
2240 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002241 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002242 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002243 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002244 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2245 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002246 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002247 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002248 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002249 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002250 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002251 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002252 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002253 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002254 }
2255 } else if (isa<PointerType>(DestTy)) {
2256 if (isa<PointerType>(SrcTy)) {
2257 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002258 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002259 return IntToPtr; // int -> ptr
2260 } else {
2261 assert(!"Casting pointer to other than pointer or int");
2262 }
2263 } else {
2264 assert(!"Casting to type that is not first-class");
2265 }
2266
2267 // If we fall through to here we probably hit an assertion cast above
2268 // and assertions are not turned on. Anything we return is an error, so
2269 // BitCast is as good a choice as any.
2270 return BitCast;
2271}
2272
2273//===----------------------------------------------------------------------===//
2274// CastInst SubClass Constructors
2275//===----------------------------------------------------------------------===//
2276
2277/// Check that the construction parameters for a CastInst are correct. This
2278/// could be broken out into the separate constructors but it is useful to have
2279/// it in one place and to eliminate the redundant code for getting the sizes
2280/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002281bool
2282CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002283
2284 // Check for type sanity on the arguments
2285 const Type *SrcTy = S->getType();
2286 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2287 return false;
2288
2289 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002290 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2291 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002292
2293 // Switch on the opcode provided
2294 switch (op) {
2295 default: return false; // This is an input error
2296 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002297 return SrcTy->isIntOrIntVector() &&
2298 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002300 return SrcTy->isIntOrIntVector() &&
2301 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002302 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002303 return SrcTy->isIntOrIntVector() &&
2304 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002305 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002306 return SrcTy->isFPOrFPVector() &&
2307 DstTy->isFPOrFPVector() &&
2308 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002309 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002310 return SrcTy->isFPOrFPVector() &&
2311 DstTy->isFPOrFPVector() &&
2312 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002313 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002314 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002315 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2316 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002317 return SVTy->getElementType()->isIntOrIntVector() &&
2318 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002319 SVTy->getNumElements() == DVTy->getNumElements();
2320 }
2321 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002322 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002323 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002325 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2326 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002327 return SVTy->getElementType()->isFPOrFPVector() &&
2328 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002329 SVTy->getNumElements() == DVTy->getNumElements();
2330 }
2331 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002332 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002333 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002334 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002335 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002336 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002337 case Instruction::BitCast:
2338 // BitCast implies a no-op cast of type only. No bits change.
2339 // However, you can't cast pointers to anything but pointers.
2340 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2341 return false;
2342
Duncan Sands55e50902008-01-06 10:12:28 +00002343 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344 // these cases, the cast is okay if the source and destination bit widths
2345 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002346 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002347 }
2348}
2349
2350TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002351 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002353 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002354}
2355
2356TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002357 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002358) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002359 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002360}
2361
2362ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002363 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002364) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002365 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002366}
2367
2368ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002369 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002370) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002371 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002372}
2373SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002374 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002375) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002376 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002377}
2378
Jeff Cohencc08c832006-12-02 02:22:01 +00002379SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002380 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002381) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002382 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002383}
2384
2385FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002386 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002387) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002388 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002389}
2390
2391FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002392 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002394 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002395}
2396
2397FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002398 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002399) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002400 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002401}
2402
2403FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002404 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002405) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002406 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002407}
2408
2409UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002410 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002411) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002412 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002413}
2414
2415UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002416 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002417) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002418 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002419}
2420
2421SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002422 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002423) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002424 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425}
2426
2427SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002428 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002430 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002431}
2432
2433FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002434 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002436 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437}
2438
2439FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002440 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002441) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002442 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002443}
2444
2445FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002446 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002448 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002449}
2450
2451FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002452 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002454 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002455}
2456
2457PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002458 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002460 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002461}
2462
2463PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002464 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002466 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002467}
2468
2469IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002470 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002472 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002473}
2474
2475IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002476 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002477) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002478 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002479}
2480
2481BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002482 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002483) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002484 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002485}
2486
2487BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002488 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002489) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002490 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002491}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002492
2493//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002494// CmpInst Classes
2495//===----------------------------------------------------------------------===//
2496
Nate Begemand2195702008-05-12 19:01:56 +00002497CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002498 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002499 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002500 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002501 OperandTraits<CmpInst>::op_begin(this),
2502 OperandTraits<CmpInst>::operands(this),
2503 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002504 Op<0>() = LHS;
2505 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002506 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002507 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002508}
Gabor Greiff6caff662008-05-10 08:32:32 +00002509
Nate Begemand2195702008-05-12 19:01:56 +00002510CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002511 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002512 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002513 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002514 OperandTraits<CmpInst>::op_begin(this),
2515 OperandTraits<CmpInst>::operands(this),
2516 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002517 Op<0>() = LHS;
2518 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002519 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002520 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002521}
2522
2523CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002524CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002525 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002526 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002527 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002528 if (InsertBefore)
2529 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2530 S1, S2, Name);
2531 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002532 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002533 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002534 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002535
2536 if (InsertBefore)
2537 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2538 S1, S2, Name);
2539 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002540 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002541 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002542}
2543
2544CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002545CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002546 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002547 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002548 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2549 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002550 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002551 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2552 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002553}
2554
2555void CmpInst::swapOperands() {
2556 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2557 IC->swapOperands();
2558 else
2559 cast<FCmpInst>(this)->swapOperands();
2560}
2561
2562bool CmpInst::isCommutative() {
2563 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2564 return IC->isCommutative();
2565 return cast<FCmpInst>(this)->isCommutative();
2566}
2567
2568bool CmpInst::isEquality() {
2569 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2570 return IC->isEquality();
2571 return cast<FCmpInst>(this)->isEquality();
2572}
2573
2574
Dan Gohman4e724382008-05-31 02:47:54 +00002575CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002576 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002577 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002578 case ICMP_EQ: return ICMP_NE;
2579 case ICMP_NE: return ICMP_EQ;
2580 case ICMP_UGT: return ICMP_ULE;
2581 case ICMP_ULT: return ICMP_UGE;
2582 case ICMP_UGE: return ICMP_ULT;
2583 case ICMP_ULE: return ICMP_UGT;
2584 case ICMP_SGT: return ICMP_SLE;
2585 case ICMP_SLT: return ICMP_SGE;
2586 case ICMP_SGE: return ICMP_SLT;
2587 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002588
Dan Gohman4e724382008-05-31 02:47:54 +00002589 case FCMP_OEQ: return FCMP_UNE;
2590 case FCMP_ONE: return FCMP_UEQ;
2591 case FCMP_OGT: return FCMP_ULE;
2592 case FCMP_OLT: return FCMP_UGE;
2593 case FCMP_OGE: return FCMP_ULT;
2594 case FCMP_OLE: return FCMP_UGT;
2595 case FCMP_UEQ: return FCMP_ONE;
2596 case FCMP_UNE: return FCMP_OEQ;
2597 case FCMP_UGT: return FCMP_OLE;
2598 case FCMP_ULT: return FCMP_OGE;
2599 case FCMP_UGE: return FCMP_OLT;
2600 case FCMP_ULE: return FCMP_OGT;
2601 case FCMP_ORD: return FCMP_UNO;
2602 case FCMP_UNO: return FCMP_ORD;
2603 case FCMP_TRUE: return FCMP_FALSE;
2604 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002605 }
2606}
2607
Reid Spencer266e42b2006-12-23 06:05:41 +00002608ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2609 switch (pred) {
2610 default: assert(! "Unknown icmp predicate!");
2611 case ICMP_EQ: case ICMP_NE:
2612 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2613 return pred;
2614 case ICMP_UGT: return ICMP_SGT;
2615 case ICMP_ULT: return ICMP_SLT;
2616 case ICMP_UGE: return ICMP_SGE;
2617 case ICMP_ULE: return ICMP_SLE;
2618 }
2619}
2620
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002621ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2622 switch (pred) {
2623 default: assert(! "Unknown icmp predicate!");
2624 case ICMP_EQ: case ICMP_NE:
2625 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2626 return pred;
2627 case ICMP_SGT: return ICMP_UGT;
2628 case ICMP_SLT: return ICMP_ULT;
2629 case ICMP_SGE: return ICMP_UGE;
2630 case ICMP_SLE: return ICMP_ULE;
2631 }
2632}
2633
Reid Spencer266e42b2006-12-23 06:05:41 +00002634bool ICmpInst::isSignedPredicate(Predicate pred) {
2635 switch (pred) {
2636 default: assert(! "Unknown icmp predicate!");
2637 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2638 return true;
2639 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2640 case ICMP_UGE: case ICMP_ULE:
2641 return false;
2642 }
2643}
2644
Reid Spencer0286bc12007-02-28 22:00:54 +00002645/// Initialize a set of values that all satisfy the condition with C.
2646///
2647ConstantRange
2648ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2649 APInt Lower(C);
2650 APInt Upper(C);
2651 uint32_t BitWidth = C.getBitWidth();
2652 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002653 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002654 case ICmpInst::ICMP_EQ: Upper++; break;
2655 case ICmpInst::ICMP_NE: Lower++; break;
2656 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2657 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2658 case ICmpInst::ICMP_UGT:
2659 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2660 break;
2661 case ICmpInst::ICMP_SGT:
2662 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2663 break;
2664 case ICmpInst::ICMP_ULE:
2665 Lower = APInt::getMinValue(BitWidth); Upper++;
2666 break;
2667 case ICmpInst::ICMP_SLE:
2668 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2669 break;
2670 case ICmpInst::ICMP_UGE:
2671 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2672 break;
2673 case ICmpInst::ICMP_SGE:
2674 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2675 break;
2676 }
2677 return ConstantRange(Lower, Upper);
2678}
2679
Dan Gohman4e724382008-05-31 02:47:54 +00002680CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002681 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002682 default: assert(!"Unknown cmp predicate!");
2683 case ICMP_EQ: case ICMP_NE:
2684 return pred;
2685 case ICMP_SGT: return ICMP_SLT;
2686 case ICMP_SLT: return ICMP_SGT;
2687 case ICMP_SGE: return ICMP_SLE;
2688 case ICMP_SLE: return ICMP_SGE;
2689 case ICMP_UGT: return ICMP_ULT;
2690 case ICMP_ULT: return ICMP_UGT;
2691 case ICMP_UGE: return ICMP_ULE;
2692 case ICMP_ULE: return ICMP_UGE;
2693
Reid Spencerd9436b62006-11-20 01:22:35 +00002694 case FCMP_FALSE: case FCMP_TRUE:
2695 case FCMP_OEQ: case FCMP_ONE:
2696 case FCMP_UEQ: case FCMP_UNE:
2697 case FCMP_ORD: case FCMP_UNO:
2698 return pred;
2699 case FCMP_OGT: return FCMP_OLT;
2700 case FCMP_OLT: return FCMP_OGT;
2701 case FCMP_OGE: return FCMP_OLE;
2702 case FCMP_OLE: return FCMP_OGE;
2703 case FCMP_UGT: return FCMP_ULT;
2704 case FCMP_ULT: return FCMP_UGT;
2705 case FCMP_UGE: return FCMP_ULE;
2706 case FCMP_ULE: return FCMP_UGE;
2707 }
2708}
2709
Reid Spencer266e42b2006-12-23 06:05:41 +00002710bool CmpInst::isUnsigned(unsigned short predicate) {
2711 switch (predicate) {
2712 default: return false;
2713 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2714 case ICmpInst::ICMP_UGE: return true;
2715 }
2716}
2717
2718bool CmpInst::isSigned(unsigned short predicate){
2719 switch (predicate) {
2720 default: return false;
2721 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2722 case ICmpInst::ICMP_SGE: return true;
2723 }
2724}
2725
2726bool CmpInst::isOrdered(unsigned short predicate) {
2727 switch (predicate) {
2728 default: return false;
2729 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2730 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2731 case FCmpInst::FCMP_ORD: return true;
2732 }
2733}
2734
2735bool CmpInst::isUnordered(unsigned short predicate) {
2736 switch (predicate) {
2737 default: return false;
2738 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2739 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2740 case FCmpInst::FCMP_UNO: return true;
2741 }
2742}
2743
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002744//===----------------------------------------------------------------------===//
2745// SwitchInst Implementation
2746//===----------------------------------------------------------------------===//
2747
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002748void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002749 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002750 ReservedSpace = 2+NumCases*2;
2751 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002752 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002753
Gabor Greif2d3024d2008-05-26 21:33:52 +00002754 OperandList[0] = Value;
2755 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002756}
2757
Chris Lattner2195fc42007-02-24 00:55:48 +00002758/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2759/// switch on and a default destination. The number of additional cases can
2760/// be specified here to make memory allocation more efficient. This
2761/// constructor can also autoinsert before another instruction.
2762SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2763 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002764 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2765 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002766 init(Value, Default, NumCases);
2767}
2768
2769/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2770/// switch on and a default destination. The number of additional cases can
2771/// be specified here to make memory allocation more efficient. This
2772/// constructor also autoinserts at the end of the specified BasicBlock.
2773SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2774 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002775 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2776 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002777 init(Value, Default, NumCases);
2778}
2779
Misha Brukmanb1c93172005-04-21 23:48:37 +00002780SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002781 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002782 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002783 Use *OL = OperandList, *InOL = SI.OperandList;
2784 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002785 OL[i] = InOL[i];
2786 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002787 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002788 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002789}
2790
Gordon Henriksen14a55692007-12-10 02:14:30 +00002791SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002792 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002793}
2794
2795
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002796/// addCase - Add an entry to the switch instruction...
2797///
Chris Lattner47ac1872005-02-24 05:32:09 +00002798void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002799 unsigned OpNo = NumOperands;
2800 if (OpNo+2 > ReservedSpace)
2801 resizeOperands(0); // Get more space!
2802 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002803 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002804 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002805 OperandList[OpNo] = OnVal;
2806 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002807}
2808
2809/// removeCase - This method removes the specified successor from the switch
2810/// instruction. Note that this cannot be used to remove the default
2811/// destination (successor #0).
2812///
2813void SwitchInst::removeCase(unsigned idx) {
2814 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002815 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2816
2817 unsigned NumOps = getNumOperands();
2818 Use *OL = OperandList;
2819
2820 // Move everything after this operand down.
2821 //
2822 // FIXME: we could just swap with the end of the list, then erase. However,
2823 // client might not expect this to happen. The code as it is thrashes the
2824 // use/def lists, which is kinda lame.
2825 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2826 OL[i-2] = OL[i];
2827 OL[i-2+1] = OL[i+1];
2828 }
2829
2830 // Nuke the last value.
2831 OL[NumOps-2].set(0);
2832 OL[NumOps-2+1].set(0);
2833 NumOperands = NumOps-2;
2834}
2835
2836/// resizeOperands - resize operands - This adjusts the length of the operands
2837/// list according to the following behavior:
2838/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002839/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002840/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2841/// 3. If NumOps == NumOperands, trim the reserved space.
2842///
2843void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002844 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002845 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002846 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002847 } else if (NumOps*2 > NumOperands) {
2848 // No resize needed.
2849 if (ReservedSpace >= NumOps) return;
2850 } else if (NumOps == NumOperands) {
2851 if (ReservedSpace == NumOps) return;
2852 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002853 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002854 }
2855
2856 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002857 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002858 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002859 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002860 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002861 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002862 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002863 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002864}
2865
2866
2867BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2868 return getSuccessor(idx);
2869}
2870unsigned SwitchInst::getNumSuccessorsV() const {
2871 return getNumSuccessors();
2872}
2873void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2874 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002875}
Chris Lattnerf22be932004-10-15 23:52:53 +00002876
Chris Lattnerf22be932004-10-15 23:52:53 +00002877// Define these methods here so vtables don't get emitted into every translation
2878// unit that uses these classes.
2879
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002880GetElementPtrInst *GetElementPtrInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002881 GetElementPtrInst *New = new(getNumOperands()) GetElementPtrInst(*this);
2882 New->SubclassOptionalData = SubclassOptionalData;
2883 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00002884}
2885
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002886BinaryOperator *BinaryOperator::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002887 BinaryOperator *New = Create(getOpcode(), Op<0>(), Op<1>());
2888 New->SubclassOptionalData = SubclassOptionalData;
2889 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00002890}
2891
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002892FCmpInst* FCmpInst::clone(LLVMContext &Context) const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002893 FCmpInst *New = new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002894 New->SubclassOptionalData = SubclassOptionalData;
2895 return New;
Chris Lattner0b490b02007-08-24 20:48:18 +00002896}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002897ICmpInst* ICmpInst::clone(LLVMContext &Context) const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002898 ICmpInst *New = new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002899 New->SubclassOptionalData = SubclassOptionalData;
2900 return New;
Reid Spencerd9436b62006-11-20 01:22:35 +00002901}
2902
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002903ExtractValueInst *ExtractValueInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002904 ExtractValueInst *New = new ExtractValueInst(*this);
2905 New->SubclassOptionalData = SubclassOptionalData;
2906 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00002907}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002908InsertValueInst *InsertValueInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002909 InsertValueInst *New = new InsertValueInst(*this);
2910 New->SubclassOptionalData = SubclassOptionalData;
2911 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00002912}
2913
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002914MallocInst *MallocInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002915 MallocInst *New = new MallocInst(getAllocatedType(),
2916 (Value*)getOperand(0),
2917 getAlignment());
2918 New->SubclassOptionalData = SubclassOptionalData;
2919 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002920}
Dan Gohman0752bff2008-05-23 00:36:11 +00002921
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002922AllocaInst *AllocaInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002923 AllocaInst *New = new AllocaInst(getAllocatedType(),
2924 (Value*)getOperand(0),
2925 getAlignment());
2926 New->SubclassOptionalData = SubclassOptionalData;
2927 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002928}
2929
2930FreeInst *FreeInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002931 FreeInst *New = new FreeInst(getOperand(0));
2932 New->SubclassOptionalData = SubclassOptionalData;
2933 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002934}
2935
2936LoadInst *LoadInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002937 LoadInst *New = new LoadInst(getOperand(0),
2938 Twine(), isVolatile(),
2939 getAlignment());
2940 New->SubclassOptionalData = SubclassOptionalData;
2941 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002942}
2943
2944StoreInst *StoreInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002945 StoreInst *New = new StoreInst(getOperand(0), getOperand(1),
2946 isVolatile(), getAlignment());
2947 New->SubclassOptionalData = SubclassOptionalData;
2948 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002949}
2950
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002951TruncInst *TruncInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002952 TruncInst *New = new TruncInst(getOperand(0), getType());
2953 New->SubclassOptionalData = SubclassOptionalData;
2954 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002955}
2956
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002957ZExtInst *ZExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002958 ZExtInst *New = new ZExtInst(getOperand(0), getType());
2959 New->SubclassOptionalData = SubclassOptionalData;
2960 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002961}
2962
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002963SExtInst *SExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002964 SExtInst *New = new SExtInst(getOperand(0), getType());
2965 New->SubclassOptionalData = SubclassOptionalData;
2966 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002967}
2968
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002969FPTruncInst *FPTruncInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002970 FPTruncInst *New = new FPTruncInst(getOperand(0), getType());
2971 New->SubclassOptionalData = SubclassOptionalData;
2972 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002973}
2974
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002975FPExtInst *FPExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002976 FPExtInst *New = new FPExtInst(getOperand(0), getType());
2977 New->SubclassOptionalData = SubclassOptionalData;
2978 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002979}
2980
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002981UIToFPInst *UIToFPInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002982 UIToFPInst *New = new UIToFPInst(getOperand(0), getType());
2983 New->SubclassOptionalData = SubclassOptionalData;
2984 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002985}
2986
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002987SIToFPInst *SIToFPInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002988 SIToFPInst *New = new SIToFPInst(getOperand(0), getType());
2989 New->SubclassOptionalData = SubclassOptionalData;
2990 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002991}
2992
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002993FPToUIInst *FPToUIInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002994 FPToUIInst *New = new FPToUIInst(getOperand(0), getType());
2995 New->SubclassOptionalData = SubclassOptionalData;
2996 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002997}
2998
Dan Gohmanfaf516f2009-08-25 22:29:08 +00002999FPToSIInst *FPToSIInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003000 FPToSIInst *New = new FPToSIInst(getOperand(0), getType());
3001 New->SubclassOptionalData = SubclassOptionalData;
3002 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003003}
3004
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003005PtrToIntInst *PtrToIntInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003006 PtrToIntInst *New = new PtrToIntInst(getOperand(0), getType());
3007 New->SubclassOptionalData = SubclassOptionalData;
3008 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003009}
3010
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003011IntToPtrInst *IntToPtrInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003012 IntToPtrInst *New = new IntToPtrInst(getOperand(0), getType());
3013 New->SubclassOptionalData = SubclassOptionalData;
3014 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003015}
3016
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003017BitCastInst *BitCastInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003018 BitCastInst *New = new BitCastInst(getOperand(0), getType());
3019 New->SubclassOptionalData = SubclassOptionalData;
3020 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003021}
3022
3023CallInst *CallInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003024 CallInst *New = new(getNumOperands()) CallInst(*this);
3025 New->SubclassOptionalData = SubclassOptionalData;
3026 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003027}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003028
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003029SelectInst *SelectInst::clone(LLVMContext&) const {
3030 SelectInst *New = SelectInst::Create(getOperand(0),
3031 getOperand(1),
3032 getOperand(2));
3033 New->SubclassOptionalData = SubclassOptionalData;
3034 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003035}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003036
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003037VAArgInst *VAArgInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003038 VAArgInst *New = new VAArgInst(getOperand(0), getType());
3039 New->SubclassOptionalData = SubclassOptionalData;
3040 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003041}
3042
3043ExtractElementInst *ExtractElementInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003044 ExtractElementInst *New = ExtractElementInst::Create(getOperand(0),
3045 getOperand(1));
3046 New->SubclassOptionalData = SubclassOptionalData;
3047 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003048}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003049
3050InsertElementInst *InsertElementInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003051 InsertElementInst *New = InsertElementInst::Create(getOperand(0),
3052 getOperand(1),
3053 getOperand(2));
3054 New->SubclassOptionalData = SubclassOptionalData;
3055 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003056}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003057
3058ShuffleVectorInst *ShuffleVectorInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003059 ShuffleVectorInst *New = new ShuffleVectorInst(getOperand(0),
3060 getOperand(1),
3061 getOperand(2));
3062 New->SubclassOptionalData = SubclassOptionalData;
3063 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003064}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003065
3066PHINode *PHINode::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003067 PHINode *New = new PHINode(*this);
3068 New->SubclassOptionalData = SubclassOptionalData;
3069 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003070}
3071
3072ReturnInst *ReturnInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003073 ReturnInst *New = new(getNumOperands()) ReturnInst(*this);
3074 New->SubclassOptionalData = SubclassOptionalData;
3075 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003076}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003077
3078BranchInst *BranchInst::clone(LLVMContext&) const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003079 unsigned Ops(getNumOperands());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003080 BranchInst *New = new(Ops, Ops == 1) BranchInst(*this);
3081 New->SubclassOptionalData = SubclassOptionalData;
3082 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003083}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003084
3085SwitchInst *SwitchInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003086 SwitchInst *New = new SwitchInst(*this);
3087 New->SubclassOptionalData = SubclassOptionalData;
3088 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003089}
3090
3091InvokeInst *InvokeInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003092 InvokeInst *New = new(getNumOperands()) InvokeInst(*this);
3093 New->SubclassOptionalData = SubclassOptionalData;
3094 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003095}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003096
Owen Anderson55f1c092009-08-13 21:58:54 +00003097UnwindInst *UnwindInst::clone(LLVMContext &C) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003098 UnwindInst *New = new UnwindInst(C);
3099 New->SubclassOptionalData = SubclassOptionalData;
3100 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003101}
3102
Owen Anderson55f1c092009-08-13 21:58:54 +00003103UnreachableInst *UnreachableInst::clone(LLVMContext &C) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003104 UnreachableInst *New = new UnreachableInst(C);
3105 New->SubclassOptionalData = SubclassOptionalData;
3106 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003107}