blob: 656a51b0549d167c6a2e765775057eda67b00c0d [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000015#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Function.h"
18#include "llvm/Instructions.h"
19#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000020#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000021#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000022using namespace llvm;
23
Chris Lattner3e13b8c2008-01-02 23:42:30 +000024//===----------------------------------------------------------------------===//
25// CallSite Class
26//===----------------------------------------------------------------------===//
27
Duncan Sands85fab3a2008-02-18 17:32:13 +000028CallSite::CallSite(Instruction *C) {
29 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
30 I = C;
31}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000032unsigned CallSite::getCallingConv() const {
33 if (CallInst *CI = dyn_cast<CallInst>(I))
34 return CI->getCallingConv();
35 else
36 return cast<InvokeInst>(I)->getCallingConv();
37}
38void CallSite::setCallingConv(unsigned CC) {
39 if (CallInst *CI = dyn_cast<CallInst>(I))
40 CI->setCallingConv(CC);
41 else
42 cast<InvokeInst>(I)->setCallingConv(CC);
43}
Chris Lattner8a923e72008-03-12 17:45:29 +000044const PAListPtr &CallSite::getParamAttrs() const {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000045 if (CallInst *CI = dyn_cast<CallInst>(I))
46 return CI->getParamAttrs();
47 else
48 return cast<InvokeInst>(I)->getParamAttrs();
49}
Chris Lattner8a923e72008-03-12 17:45:29 +000050void CallSite::setParamAttrs(const PAListPtr &PAL) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000051 if (CallInst *CI = dyn_cast<CallInst>(I))
52 CI->setParamAttrs(PAL);
53 else
54 cast<InvokeInst>(I)->setParamAttrs(PAL);
55}
Dale Johannesen89268bc2008-02-19 21:38:47 +000056bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
Duncan Sands5208d1a2007-11-28 17:07:01 +000057 if (CallInst *CI = dyn_cast<CallInst>(I))
Dale Johannesen89268bc2008-02-19 21:38:47 +000058 return CI->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000059 else
Dale Johannesen89268bc2008-02-19 21:38:47 +000060 return cast<InvokeInst>(I)->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000061}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000062uint16_t CallSite::getParamAlignment(uint16_t i) const {
63 if (CallInst *CI = dyn_cast<CallInst>(I))
64 return CI->getParamAlignment(i);
65 else
66 return cast<InvokeInst>(I)->getParamAlignment(i);
67}
68
Duncan Sands38ef3a82007-12-03 20:06:50 +000069bool CallSite::doesNotAccessMemory() const {
70 if (CallInst *CI = dyn_cast<CallInst>(I))
71 return CI->doesNotAccessMemory();
72 else
73 return cast<InvokeInst>(I)->doesNotAccessMemory();
74}
75bool CallSite::onlyReadsMemory() const {
76 if (CallInst *CI = dyn_cast<CallInst>(I))
77 return CI->onlyReadsMemory();
78 else
79 return cast<InvokeInst>(I)->onlyReadsMemory();
80}
Duncan Sands3353ed02007-12-18 09:59:50 +000081bool CallSite::doesNotThrow() const {
Duncan Sands8e4847e2007-12-16 15:51:49 +000082 if (CallInst *CI = dyn_cast<CallInst>(I))
Duncan Sands3353ed02007-12-18 09:59:50 +000083 return CI->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000084 else
Duncan Sands3353ed02007-12-18 09:59:50 +000085 return cast<InvokeInst>(I)->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000086}
Duncan Sandsaa31b922007-12-19 21:13:37 +000087void CallSite::setDoesNotThrow(bool doesNotThrow) {
88 if (CallInst *CI = dyn_cast<CallInst>(I))
89 CI->setDoesNotThrow(doesNotThrow);
90 else
91 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
92}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000093
Gordon Henriksen14a55692007-12-10 02:14:30 +000094//===----------------------------------------------------------------------===//
95// TerminatorInst Class
96//===----------------------------------------------------------------------===//
97
98// Out of line virtual method, so the vtable, etc has a home.
99TerminatorInst::~TerminatorInst() {
100}
101
Gabor Greiff6caff662008-05-10 08:32:32 +0000102//===----------------------------------------------------------------------===//
103// UnaryInstruction Class
104//===----------------------------------------------------------------------===//
105
Gordon Henriksen14a55692007-12-10 02:14:30 +0000106// Out of line virtual method, so the vtable, etc has a home.
107UnaryInstruction::~UnaryInstruction() {
108}
109
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000110//===----------------------------------------------------------------------===//
111// PHINode Class
112//===----------------------------------------------------------------------===//
113
114PHINode::PHINode(const PHINode &PN)
115 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000116 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117 ReservedSpace(PN.getNumOperands()) {
118 Use *OL = OperandList;
119 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000120 OL[i] = PN.getOperand(i);
121 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000122 }
123}
124
Gordon Henriksen14a55692007-12-10 02:14:30 +0000125PHINode::~PHINode() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000126 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000127}
128
129// removeIncomingValue - Remove an incoming value. This is useful if a
130// predecessor basic block is deleted.
131Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
132 unsigned NumOps = getNumOperands();
133 Use *OL = OperandList;
134 assert(Idx*2 < NumOps && "BB not in PHI node!");
135 Value *Removed = OL[Idx*2];
136
137 // Move everything after this operand down.
138 //
139 // FIXME: we could just swap with the end of the list, then erase. However,
140 // client might not expect this to happen. The code as it is thrashes the
141 // use/def lists, which is kinda lame.
142 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
143 OL[i-2] = OL[i];
144 OL[i-2+1] = OL[i+1];
145 }
146
147 // Nuke the last value.
148 OL[NumOps-2].set(0);
149 OL[NumOps-2+1].set(0);
150 NumOperands = NumOps-2;
151
152 // If the PHI node is dead, because it has zero entries, nuke it now.
153 if (NumOps == 2 && DeletePHIIfEmpty) {
154 // If anyone is using this PHI, make them use a dummy value instead...
155 replaceAllUsesWith(UndefValue::get(getType()));
156 eraseFromParent();
157 }
158 return Removed;
159}
160
161/// resizeOperands - resize operands - This adjusts the length of the operands
162/// list according to the following behavior:
163/// 1. If NumOps == 0, grow the operand list in response to a push_back style
164/// of operation. This grows the number of ops by 1.5 times.
165/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
166/// 3. If NumOps == NumOperands, trim the reserved space.
167///
168void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000169 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000170 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000171 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000172 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
173 } else if (NumOps*2 > NumOperands) {
174 // No resize needed.
175 if (ReservedSpace >= NumOps) return;
176 } else if (NumOps == NumOperands) {
177 if (ReservedSpace == NumOps) return;
178 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000179 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000180 }
181
182 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000183 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000184 Use *NewOps = allocHungoffUses(NumOps);
185 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000186 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000187 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000188 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000189 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000190}
191
Nate Begemanb3923212005-08-04 23:24:19 +0000192/// hasConstantValue - If the specified PHI node always merges together the same
193/// value, return the value, otherwise return null.
194///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000195Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000196 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000197 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000198 if (getIncomingValue(0) != this) // not X = phi X
199 return getIncomingValue(0);
200 else
201 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000202 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000203
Nate Begemanb3923212005-08-04 23:24:19 +0000204 // Otherwise if all of the incoming values are the same for the PHI, replace
205 // the PHI node with the incoming value.
206 //
207 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000208 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000209 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000210 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000211 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000212 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000213 if (InVal && getIncomingValue(i) != InVal)
214 return 0; // Not the same, bail out.
215 else
216 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000217 }
Nate Begemanb3923212005-08-04 23:24:19 +0000218
219 // The only case that could cause InVal to be null is if we have a PHI node
220 // that only has entries for itself. In this case, there is no entry into the
221 // loop, so kill the PHI.
222 //
223 if (InVal == 0) InVal = UndefValue::get(getType());
224
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000225 // If we have a PHI node like phi(X, undef, X), where X is defined by some
226 // instruction, we cannot always return X as the result of the PHI node. Only
227 // do this if X is not an instruction (thus it must dominate the PHI block),
228 // or if the client is prepared to deal with this possibility.
229 if (HasUndefInput && !AllowNonDominatingInstruction)
230 if (Instruction *IV = dyn_cast<Instruction>(InVal))
231 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000232 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000233 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000234 return 0; // Cannot guarantee that InVal dominates this PHINode.
235
Nate Begemanb3923212005-08-04 23:24:19 +0000236 // All of the incoming values are the same, return the value now.
237 return InVal;
238}
239
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240
241//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000242// CallInst Implementation
243//===----------------------------------------------------------------------===//
244
Gordon Henriksen14a55692007-12-10 02:14:30 +0000245CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000246}
247
Chris Lattner054ba2c2007-02-13 00:58:44 +0000248void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000249 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
250 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000251 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000252
Misha Brukmanb1c93172005-04-21 23:48:37 +0000253 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000254 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000255 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000256
Chris Lattner054ba2c2007-02-13 00:58:44 +0000257 assert((NumParams == FTy->getNumParams() ||
258 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000259 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000260 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000261 assert((i >= FTy->getNumParams() ||
262 FTy->getParamType(i) == Params[i]->getType()) &&
263 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000264 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000265 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000266}
267
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000268void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000269 assert(NumOperands == 3 && "NumOperands not set up?");
270 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000271 OL[0] = Func;
272 OL[1] = Actual1;
273 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000274
275 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000276 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000277 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000278
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000279 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000280 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000281 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000282 assert((0 >= FTy->getNumParams() ||
283 FTy->getParamType(0) == Actual1->getType()) &&
284 "Calling a function with a bad signature!");
285 assert((1 >= FTy->getNumParams() ||
286 FTy->getParamType(1) == Actual2->getType()) &&
287 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000288}
289
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000290void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000291 assert(NumOperands == 2 && "NumOperands not set up?");
292 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000293 OL[0] = Func;
294 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000295
296 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000297 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000298 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000300 assert((FTy->getNumParams() == 1 ||
301 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000303 assert((0 == FTy->getNumParams() ||
304 FTy->getParamType(0) == Actual->getType()) &&
305 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306}
307
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000308void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000309 assert(NumOperands == 1 && "NumOperands not set up?");
310 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000311 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000312
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000313 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000315 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000316
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000317 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318}
319
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000321 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
323 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000324 Instruction::Call,
325 OperandTraits<CallInst>::op_end(this) - 2,
326 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000328 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329}
330
331CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
332 BasicBlock *InsertAtEnd)
333 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
334 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000335 Instruction::Call,
336 OperandTraits<CallInst>::op_end(this) - 2,
337 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000338 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000339 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000340}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341CallInst::CallInst(Value *Func, const std::string &Name,
342 Instruction *InsertBefore)
343 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
344 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000345 Instruction::Call,
346 OperandTraits<CallInst>::op_end(this) - 1,
347 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000349 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350}
351
352CallInst::CallInst(Value *Func, const std::string &Name,
353 BasicBlock *InsertAtEnd)
354 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
355 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000356 Instruction::Call,
357 OperandTraits<CallInst>::op_end(this) - 1,
358 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000359 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000360 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000361}
362
Misha Brukmanb1c93172005-04-21 23:48:37 +0000363CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000364 : Instruction(CI.getType(), Instruction::Call,
365 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000366 CI.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000367 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000368 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000369 Use *OL = OperandList;
370 Use *InOL = CI.OperandList;
371 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000372 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000373}
374
Eric Christopher901b1a72008-05-16 20:39:43 +0000375void CallInst::addParamAttr(unsigned i, ParameterAttributes attr) {
376 PAListPtr PAL = getParamAttrs();
377 PAL = PAL.addAttr(i, attr);
378 setParamAttrs(PAL);
379}
380
Chris Lattnerc994c022008-03-13 05:00:21 +0000381bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000382 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000383 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000384 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000385 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000386 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000387}
388
Duncan Sandsaa31b922007-12-19 21:13:37 +0000389void CallInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000390 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000391 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000392 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000393 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000394 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000395 setParamAttrs(PAL);
396}
397
Duncan Sands5208d1a2007-11-28 17:07:01 +0000398
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000399//===----------------------------------------------------------------------===//
400// InvokeInst Implementation
401//===----------------------------------------------------------------------===//
402
403void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000404 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000405 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
406 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000407 OL[0] = Fn;
408 OL[1] = IfNormal;
409 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000410 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000411 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000412 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000413
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000414 assert(((NumArgs == FTy->getNumParams()) ||
415 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000416 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000417
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000418 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000419 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000420 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000421 "Invoking a function with a bad signature!");
422
Gabor Greif2d3024d2008-05-26 21:33:52 +0000423 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000424 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000425}
426
Misha Brukmanb1c93172005-04-21 23:48:37 +0000427InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000428 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000429 OperandTraits<InvokeInst>::op_end(this)
430 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000431 II.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000432 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000433 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000434 Use *OL = OperandList, *InOL = II.OperandList;
435 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000436 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000437}
438
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000439BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
440 return getSuccessor(idx);
441}
442unsigned InvokeInst::getNumSuccessorsV() const {
443 return getNumSuccessors();
444}
445void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
446 return setSuccessor(idx, B);
447}
448
Chris Lattnerc994c022008-03-13 05:00:21 +0000449bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000450 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000451 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000452 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000453 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000454 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000455}
456
Eric Christopher901b1a72008-05-16 20:39:43 +0000457void InvokeInst::addParamAttr(unsigned i, ParameterAttributes attr) {
458 PAListPtr PAL = getParamAttrs();
459 PAL = PAL.addAttr(i, attr);
460 setParamAttrs(PAL);
461}
462
Duncan Sandsaa31b922007-12-19 21:13:37 +0000463void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000464 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000465 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000466 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000467 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000468 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000469 setParamAttrs(PAL);
470}
471
Duncan Sands5208d1a2007-11-28 17:07:01 +0000472
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000473//===----------------------------------------------------------------------===//
474// ReturnInst Implementation
475//===----------------------------------------------------------------------===//
476
Chris Lattner2195fc42007-02-24 00:55:48 +0000477ReturnInst::ReturnInst(const ReturnInst &RI)
478 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Gabor Greif697e94c2008-05-15 10:04:30 +0000479 OperandTraits<ReturnInst>::op_end(this)
480 - RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000481 RI.getNumOperands()) {
Devang Patel59643e52008-02-23 00:35:18 +0000482 unsigned N = RI.getNumOperands();
Gabor Greiff6caff662008-05-10 08:32:32 +0000483 if (N == 1)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000484 Op<0>() = RI.Op<0>();
Devang Patelae682fb2008-02-26 17:56:20 +0000485 else if (N) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000486 Use *OL = OperandList;
Devang Patelae682fb2008-02-26 17:56:20 +0000487 for (unsigned i = 0; i < N; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000488 OL[i] = RI.getOperand(i);
Devang Patelae682fb2008-02-26 17:56:20 +0000489 }
Chris Lattner2195fc42007-02-24 00:55:48 +0000490}
491
492ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000493 : TerminatorInst(Type::VoidTy, Instruction::Ret,
494 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
495 retVal != 0, InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000496 if (retVal)
497 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000498}
499ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000500 : TerminatorInst(Type::VoidTy, Instruction::Ret,
501 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
502 retVal != 0, InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000503 if (retVal)
504 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000505}
506ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000507 : TerminatorInst(Type::VoidTy, Instruction::Ret,
508 OperandTraits<ReturnInst>::op_end(this),
509 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000510}
511
Devang Patela736c002008-02-26 19:38:17 +0000512ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
513 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000514 : TerminatorInst(Type::VoidTy, Instruction::Ret,
515 OperandTraits<ReturnInst>::op_end(this) - N,
516 N, InsertBefore) {
Devang Patela736c002008-02-26 19:38:17 +0000517 if (N != 0)
518 init(retVals, N);
519}
520ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
521 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000522 : TerminatorInst(Type::VoidTy, Instruction::Ret,
523 OperandTraits<ReturnInst>::op_end(this) - N,
524 N, InsertAtEnd) {
Devang Patela736c002008-02-26 19:38:17 +0000525 if (N != 0)
526 init(retVals, N);
527}
528
Devang Patel060e79c2008-02-26 19:15:26 +0000529void ReturnInst::init(Value * const* retVals, unsigned N) {
Devang Patelc38eb522008-02-26 18:49:29 +0000530 assert (N > 0 && "Invalid operands numbers in ReturnInst init");
Devang Patel59643e52008-02-23 00:35:18 +0000531
Devang Patelc38eb522008-02-26 18:49:29 +0000532 NumOperands = N;
Devang Patel59643e52008-02-23 00:35:18 +0000533 if (NumOperands == 1) {
Devang Patel060e79c2008-02-26 19:15:26 +0000534 Value *V = *retVals;
Devang Patel59643e52008-02-23 00:35:18 +0000535 if (V->getType() == Type::VoidTy)
536 return;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000537 Op<0>() = V;
Devang Patelae682fb2008-02-26 17:56:20 +0000538 return;
Devang Patel59643e52008-02-23 00:35:18 +0000539 }
540
Gabor Greiff6caff662008-05-10 08:32:32 +0000541 Use *OL = OperandList;
Devang Patel59643e52008-02-23 00:35:18 +0000542 for (unsigned i = 0; i < NumOperands; ++i) {
Devang Patel060e79c2008-02-26 19:15:26 +0000543 Value *V = *retVals++;
Devang Patel59643e52008-02-23 00:35:18 +0000544 assert(!isa<BasicBlock>(V) &&
545 "Cannot return basic block. Probably using the incorrect ctor");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000546 OL[i] = V;
Devang Patel59643e52008-02-23 00:35:18 +0000547 }
548}
549
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000550unsigned ReturnInst::getNumSuccessorsV() const {
551 return getNumSuccessors();
552}
553
Devang Patelae682fb2008-02-26 17:56:20 +0000554/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
555/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000556void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000557 assert(0 && "ReturnInst has no successors!");
558}
559
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000560BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
561 assert(0 && "ReturnInst has no successors!");
562 abort();
563 return 0;
564}
565
Devang Patel59643e52008-02-23 00:35:18 +0000566ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000567}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000568
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000569//===----------------------------------------------------------------------===//
570// UnwindInst Implementation
571//===----------------------------------------------------------------------===//
572
Chris Lattner2195fc42007-02-24 00:55:48 +0000573UnwindInst::UnwindInst(Instruction *InsertBefore)
574 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
575}
576UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
577 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
578}
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) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000586 assert(0 && "UnwindInst has no successors!");
587}
588
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000589BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
590 assert(0 && "UnwindInst has no successors!");
591 abort();
592 return 0;
593}
594
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000595//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000596// UnreachableInst Implementation
597//===----------------------------------------------------------------------===//
598
Chris Lattner2195fc42007-02-24 00:55:48 +0000599UnreachableInst::UnreachableInst(Instruction *InsertBefore)
600 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
601}
602UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
603 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
604}
605
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000606unsigned UnreachableInst::getNumSuccessorsV() const {
607 return getNumSuccessors();
608}
609
610void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
611 assert(0 && "UnwindInst has no successors!");
612}
613
614BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
615 assert(0 && "UnwindInst has no successors!");
616 abort();
617 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000618}
619
620//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000621// BranchInst Implementation
622//===----------------------------------------------------------------------===//
623
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000624void BranchInst::AssertOK() {
625 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000626 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000627 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000628}
629
Chris Lattner2195fc42007-02-24 00:55:48 +0000630BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000631 : TerminatorInst(Type::VoidTy, Instruction::Br,
632 OperandTraits<BranchInst>::op_end(this) - 1,
633 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000634 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000635 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000636}
637BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
638 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000639 : TerminatorInst(Type::VoidTy, Instruction::Br,
640 OperandTraits<BranchInst>::op_end(this) - 3,
641 3, InsertBefore) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000642 Op<0>() = IfTrue;
643 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000644 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000645#ifndef NDEBUG
646 AssertOK();
647#endif
648}
649
650BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000651 : TerminatorInst(Type::VoidTy, Instruction::Br,
652 OperandTraits<BranchInst>::op_end(this) - 1,
653 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000654 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000655 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000656}
657
658BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
659 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000660 : TerminatorInst(Type::VoidTy, Instruction::Br,
661 OperandTraits<BranchInst>::op_end(this) - 3,
662 3, InsertAtEnd) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000663 Op<0>() = IfTrue;
664 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000665 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000666#ifndef NDEBUG
667 AssertOK();
668#endif
669}
670
671
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000672BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000673 TerminatorInst(Type::VoidTy, Instruction::Br,
674 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
675 BI.getNumOperands()) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000676 OperandList[0] = BI.getOperand(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000677 if (BI.getNumOperands() != 1) {
678 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000679 OperandList[1] = BI.getOperand(1);
680 OperandList[2] = BI.getOperand(2);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000681 }
682}
683
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000684BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
685 return getSuccessor(idx);
686}
687unsigned BranchInst::getNumSuccessorsV() const {
688 return getNumSuccessors();
689}
690void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
691 setSuccessor(idx, B);
692}
693
694
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000695//===----------------------------------------------------------------------===//
696// AllocationInst Implementation
697//===----------------------------------------------------------------------===//
698
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000699static Value *getAISize(Value *Amt) {
700 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000701 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000702 else {
703 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000704 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000705 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000706 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000707 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000708 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000709}
710
Misha Brukmanb1c93172005-04-21 23:48:37 +0000711AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000712 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000713 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000714 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000715 InsertBefore) {
716 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000717 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000718 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000719}
720
Misha Brukmanb1c93172005-04-21 23:48:37 +0000721AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000722 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000723 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000724 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000725 InsertAtEnd) {
726 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000727 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000728 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000729}
730
Gordon Henriksen14a55692007-12-10 02:14:30 +0000731// Out of line virtual method, so the vtable, etc has a home.
732AllocationInst::~AllocationInst() {
733}
734
Dan Gohmanaa583d72008-03-24 16:55:58 +0000735void AllocationInst::setAlignment(unsigned Align) {
736 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
737 SubclassData = Log2_32(Align) + 1;
738 assert(getAlignment() == Align && "Alignment representation error!");
739}
740
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000741bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000742 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
743 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000744 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000745}
746
747const Type *AllocationInst::getAllocatedType() const {
748 return getType()->getElementType();
749}
750
751AllocaInst::AllocaInst(const AllocaInst &AI)
752 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000753 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000754}
755
756MallocInst::MallocInst(const MallocInst &MI)
757 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000758 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000759}
760
761//===----------------------------------------------------------------------===//
762// FreeInst Implementation
763//===----------------------------------------------------------------------===//
764
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000765void FreeInst::AssertOK() {
766 assert(isa<PointerType>(getOperand(0)->getType()) &&
767 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000768}
769
770FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000771 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000772 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000773}
774
775FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000776 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000777 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000778}
779
780
781//===----------------------------------------------------------------------===//
782// LoadInst Implementation
783//===----------------------------------------------------------------------===//
784
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000785void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000786 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000787 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000788}
789
790LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000791 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000792 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000793 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000794 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000795 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000796 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000797}
798
799LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000800 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000801 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000802 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000803 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000804 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000805 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000806}
807
808LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
809 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000810 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000811 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000812 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000813 setAlignment(0);
814 AssertOK();
815 setName(Name);
816}
817
818LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
819 unsigned Align, Instruction *InsertBef)
820 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
821 Load, Ptr, InsertBef) {
822 setVolatile(isVolatile);
823 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000824 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000825 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000826}
827
Dan Gohman68659282007-07-18 20:51:11 +0000828LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
829 unsigned Align, BasicBlock *InsertAE)
830 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
831 Load, Ptr, InsertAE) {
832 setVolatile(isVolatile);
833 setAlignment(Align);
834 AssertOK();
835 setName(Name);
836}
837
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000838LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
839 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000840 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000841 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000842 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000843 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000844 AssertOK();
845 setName(Name);
846}
847
848
849
850LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000851 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
852 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000853 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000854 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000855 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000856 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000857}
858
859LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000860 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
861 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000862 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000863 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000864 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000865 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000866}
867
868LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
869 Instruction *InsertBef)
870: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000871 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000872 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000873 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000874 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000875 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000876}
877
878LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
879 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000880 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
881 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000882 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000883 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000884 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000885 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000886}
887
Christopher Lamb84485702007-04-22 19:24:39 +0000888void LoadInst::setAlignment(unsigned Align) {
889 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
890 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
891}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000892
893//===----------------------------------------------------------------------===//
894// StoreInst Implementation
895//===----------------------------------------------------------------------===//
896
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000897void StoreInst::AssertOK() {
898 assert(isa<PointerType>(getOperand(1)->getType()) &&
899 "Ptr must have pointer type!");
900 assert(getOperand(0)->getType() ==
901 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000902 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000903}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000904
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000905
906StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000907 : Instruction(Type::VoidTy, Store,
908 OperandTraits<StoreInst>::op_begin(this),
909 OperandTraits<StoreInst>::operands(this),
910 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000911 Op<0>() = val;
912 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000913 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000914 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000915 AssertOK();
916}
917
918StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000919 : Instruction(Type::VoidTy, Store,
920 OperandTraits<StoreInst>::op_begin(this),
921 OperandTraits<StoreInst>::operands(this),
922 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000923 Op<0>() = val;
924 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000925 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000926 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000927 AssertOK();
928}
929
Misha Brukmanb1c93172005-04-21 23:48:37 +0000930StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000931 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000932 : Instruction(Type::VoidTy, Store,
933 OperandTraits<StoreInst>::op_begin(this),
934 OperandTraits<StoreInst>::operands(this),
935 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000936 Op<0>() = val;
937 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000938 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000939 setAlignment(0);
940 AssertOK();
941}
942
943StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
944 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000945 : Instruction(Type::VoidTy, Store,
946 OperandTraits<StoreInst>::op_begin(this),
947 OperandTraits<StoreInst>::operands(this),
948 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000949 Op<0>() = val;
950 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000951 setVolatile(isVolatile);
952 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000953 AssertOK();
954}
955
Misha Brukmanb1c93172005-04-21 23:48:37 +0000956StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000957 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000958 : Instruction(Type::VoidTy, Store,
959 OperandTraits<StoreInst>::op_begin(this),
960 OperandTraits<StoreInst>::operands(this),
961 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000962 Op<0>() = val;
963 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000964 setVolatile(isVolatile);
965 setAlignment(Align);
966 AssertOK();
967}
968
969StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000970 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000971 : Instruction(Type::VoidTy, Store,
972 OperandTraits<StoreInst>::op_begin(this),
973 OperandTraits<StoreInst>::operands(this),
974 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000975 Op<0>() = val;
976 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000977 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000978 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000979 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000980}
981
Christopher Lamb84485702007-04-22 19:24:39 +0000982void StoreInst::setAlignment(unsigned Align) {
983 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
984 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
985}
986
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000987//===----------------------------------------------------------------------===//
988// GetElementPtrInst Implementation
989//===----------------------------------------------------------------------===//
990
Christopher Lambedf07882007-12-17 01:12:55 +0000991static unsigned retrieveAddrSpace(const Value *Val) {
992 return cast<PointerType>(Val->getType())->getAddressSpace();
993}
994
Chris Lattner79807c3d2007-01-31 19:47:18 +0000995void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000996 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
997 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000998 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000999
Chris Lattner79807c3d2007-01-31 19:47:18 +00001000 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001001 OL[i+1] = Idx[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001002}
1003
Chris Lattner82981202005-05-03 05:43:30 +00001004void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001005 assert(NumOperands == 2 && "NumOperands not initialized?");
1006 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001007 OL[0] = Ptr;
1008 OL[1] = Idx;
Chris Lattner82981202005-05-03 05:43:30 +00001009}
1010
Gabor Greiff6caff662008-05-10 08:32:32 +00001011GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001012 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001013 OperandTraits<GetElementPtrInst>::op_end(this)
1014 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001015 GEPI.getNumOperands()) {
1016 Use *OL = OperandList;
1017 Use *GEPIOL = GEPI.OperandList;
1018 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001019 OL[i] = GEPIOL[i];
Gabor Greiff6caff662008-05-10 08:32:32 +00001020}
1021
Chris Lattner82981202005-05-03 05:43:30 +00001022GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1023 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001024 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001025 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001026 GetElementPtr,
1027 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1028 2, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +00001029 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001030 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001031}
1032
1033GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1034 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001035 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001036 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001037 GetElementPtr,
1038 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1039 2, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +00001040 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001041 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001042}
1043
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001044// getIndexedType - Returns the type of the element that would be loaded with
1045// a load instruction with the specified parameters.
1046//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001047// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001048// pointer type.
1049//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001050const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001051 Value* const *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001052 unsigned NumIdx) {
1053 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1054 if (!PTy) return 0; // Type isn't a pointer type!
1055 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001056
1057 // Handle the special case of the empty set index set...
Dan Gohman12fce772008-05-15 19:50:34 +00001058 if (NumIdx == 0)
1059 return Agg;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001060
Dan Gohman1ecaf452008-05-31 00:58:22 +00001061 unsigned CurIdx = 1;
1062 for (; CurIdx != NumIdx; ++CurIdx) {
1063 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1064 if (!CT || isa<PointerType>(CT)) return 0;
1065 Value *Index = Idxs[CurIdx];
1066 if (!CT->indexValid(Index)) return 0;
1067 Agg = CT->getTypeAtIndex(Index);
1068
1069 // If the new type forwards to another type, then it is in the middle
1070 // of being refined to another type (and hence, may have dropped all
1071 // references to what it was using before). So, use the new forwarded
1072 // type.
1073 if (const Type *Ty = Agg->getForwardedType())
1074 Agg = Ty;
1075 }
1076 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001077}
1078
Chris Lattner82981202005-05-03 05:43:30 +00001079const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1080 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1081 if (!PTy) return 0; // Type isn't a pointer type!
1082
1083 // Check the pointer index.
1084 if (!PTy->indexValid(Idx)) return 0;
1085
Chris Lattnerc2233332005-05-03 16:44:45 +00001086 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001087}
1088
Chris Lattner45f15572007-04-14 00:12:57 +00001089
1090/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1091/// zeros. If so, the result pointer and the first operand have the same
1092/// value, just potentially different types.
1093bool GetElementPtrInst::hasAllZeroIndices() const {
1094 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1095 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1096 if (!CI->isZero()) return false;
1097 } else {
1098 return false;
1099 }
1100 }
1101 return true;
1102}
1103
Chris Lattner27058292007-04-27 20:35:56 +00001104/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1105/// constant integers. If so, the result pointer and the first operand have
1106/// a constant offset between them.
1107bool GetElementPtrInst::hasAllConstantIndices() const {
1108 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1109 if (!isa<ConstantInt>(getOperand(i)))
1110 return false;
1111 }
1112 return true;
1113}
1114
Chris Lattner45f15572007-04-14 00:12:57 +00001115
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001116//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001117// ExtractElementInst Implementation
1118//===----------------------------------------------------------------------===//
1119
1120ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001121 const std::string &Name,
1122 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001123 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001124 ExtractElement,
1125 OperandTraits<ExtractElementInst>::op_begin(this),
1126 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001127 assert(isValidOperands(Val, Index) &&
1128 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001129 Op<0>() = Val;
1130 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001131 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001132}
1133
Chris Lattner65511ff2006-10-05 06:24:58 +00001134ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1135 const std::string &Name,
1136 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001137 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001138 ExtractElement,
1139 OperandTraits<ExtractElementInst>::op_begin(this),
1140 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001141 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001142 assert(isValidOperands(Val, Index) &&
1143 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001144 Op<0>() = Val;
1145 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001146 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001147}
1148
1149
Robert Bocchino23004482006-01-10 19:05:34 +00001150ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001151 const std::string &Name,
1152 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001153 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001154 ExtractElement,
1155 OperandTraits<ExtractElementInst>::op_begin(this),
1156 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001157 assert(isValidOperands(Val, Index) &&
1158 "Invalid extractelement instruction operands!");
1159
Gabor Greif2d3024d2008-05-26 21:33:52 +00001160 Op<0>() = Val;
1161 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001162 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001163}
1164
Chris Lattner65511ff2006-10-05 06:24:58 +00001165ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1166 const std::string &Name,
1167 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001168 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001169 ExtractElement,
1170 OperandTraits<ExtractElementInst>::op_begin(this),
1171 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001172 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001173 assert(isValidOperands(Val, Index) &&
1174 "Invalid extractelement instruction operands!");
1175
Gabor Greif2d3024d2008-05-26 21:33:52 +00001176 Op<0>() = Val;
1177 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001178 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001179}
1180
1181
Chris Lattner54865b32006-04-08 04:05:48 +00001182bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001183 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001184 return false;
1185 return true;
1186}
1187
1188
Robert Bocchino23004482006-01-10 19:05:34 +00001189//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001190// InsertElementInst Implementation
1191//===----------------------------------------------------------------------===//
1192
Chris Lattner0875d942006-04-14 22:20:32 +00001193InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001194 : Instruction(IE.getType(), InsertElement,
1195 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001196 Op<0>() = IE.Op<0>();
1197 Op<1>() = IE.Op<1>();
1198 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001199}
Chris Lattner54865b32006-04-08 04:05:48 +00001200InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001201 const std::string &Name,
1202 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001203 : Instruction(Vec->getType(), InsertElement,
1204 OperandTraits<InsertElementInst>::op_begin(this),
1205 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001206 assert(isValidOperands(Vec, Elt, Index) &&
1207 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001208 Op<0>() = Vec;
1209 Op<1>() = Elt;
1210 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001211 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001212}
1213
Chris Lattner65511ff2006-10-05 06:24:58 +00001214InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1215 const std::string &Name,
1216 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001217 : Instruction(Vec->getType(), InsertElement,
1218 OperandTraits<InsertElementInst>::op_begin(this),
1219 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001220 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001221 assert(isValidOperands(Vec, Elt, Index) &&
1222 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001223 Op<0>() = Vec;
1224 Op<1>() = Elt;
1225 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001226 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001227}
1228
1229
Chris Lattner54865b32006-04-08 04:05:48 +00001230InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001231 const std::string &Name,
1232 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001233 : Instruction(Vec->getType(), InsertElement,
1234 OperandTraits<InsertElementInst>::op_begin(this),
1235 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001236 assert(isValidOperands(Vec, Elt, Index) &&
1237 "Invalid insertelement instruction operands!");
1238
Gabor Greif2d3024d2008-05-26 21:33:52 +00001239 Op<0>() = Vec;
1240 Op<1>() = Elt;
1241 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001242 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001243}
1244
Chris Lattner65511ff2006-10-05 06:24:58 +00001245InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1246 const std::string &Name,
1247 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001248: Instruction(Vec->getType(), InsertElement,
1249 OperandTraits<InsertElementInst>::op_begin(this),
1250 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001251 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001252 assert(isValidOperands(Vec, Elt, Index) &&
1253 "Invalid insertelement instruction operands!");
1254
Gabor Greif2d3024d2008-05-26 21:33:52 +00001255 Op<0>() = Vec;
1256 Op<1>() = Elt;
1257 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001258 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001259}
1260
Chris Lattner54865b32006-04-08 04:05:48 +00001261bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1262 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001263 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001264 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001265
Reid Spencerd84d35b2007-02-15 02:26:10 +00001266 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001267 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001268
Reid Spencer8d9336d2006-12-31 05:26:44 +00001269 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001270 return false; // Third operand of insertelement must be uint.
1271 return true;
1272}
1273
1274
Robert Bocchinoca27f032006-01-17 20:07:22 +00001275//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001276// ShuffleVectorInst Implementation
1277//===----------------------------------------------------------------------===//
1278
Chris Lattner0875d942006-04-14 22:20:32 +00001279ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001280 : Instruction(SV.getType(), ShuffleVector,
1281 OperandTraits<ShuffleVectorInst>::op_begin(this),
1282 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001283 Op<0>() = SV.Op<0>();
1284 Op<1>() = SV.Op<1>();
1285 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001286}
1287
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001288ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1289 const std::string &Name,
1290 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001291 : Instruction(V1->getType(), ShuffleVector,
1292 OperandTraits<ShuffleVectorInst>::op_begin(this),
1293 OperandTraits<ShuffleVectorInst>::operands(this),
1294 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001295 assert(isValidOperands(V1, V2, Mask) &&
1296 "Invalid shuffle vector instruction operands!");
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
1303ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1304 const std::string &Name,
1305 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001306 : Instruction(V1->getType(), ShuffleVector,
1307 OperandTraits<ShuffleVectorInst>::op_begin(this),
1308 OperandTraits<ShuffleVectorInst>::operands(this),
1309 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001310 assert(isValidOperands(V1, V2, Mask) &&
1311 "Invalid shuffle vector instruction operands!");
1312
Gabor Greif2d3024d2008-05-26 21:33:52 +00001313 Op<0>() = V1;
1314 Op<1>() = V2;
1315 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001316 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001317}
1318
1319bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1320 const Value *Mask) {
Chris Lattnerf724e342008-03-02 05:28:33 +00001321 if (!isa<VectorType>(V1->getType()) ||
1322 V1->getType() != V2->getType())
1323 return false;
1324
1325 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1326 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1327 MaskTy->getElementType() != Type::Int32Ty ||
1328 MaskTy->getNumElements() !=
1329 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001330 return false;
1331 return true;
1332}
1333
Chris Lattnerf724e342008-03-02 05:28:33 +00001334/// getMaskValue - Return the index from the shuffle mask for the specified
1335/// output result. This is either -1 if the element is undef or a number less
1336/// than 2*numelements.
1337int ShuffleVectorInst::getMaskValue(unsigned i) const {
1338 const Constant *Mask = cast<Constant>(getOperand(2));
1339 if (isa<UndefValue>(Mask)) return -1;
1340 if (isa<ConstantAggregateZero>(Mask)) return 0;
1341 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1342 assert(i < MaskCV->getNumOperands() && "Index out of range");
1343
1344 if (isa<UndefValue>(MaskCV->getOperand(i)))
1345 return -1;
1346 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1347}
1348
Dan Gohman12fce772008-05-15 19:50:34 +00001349//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001350// InsertValueInst Class
1351//===----------------------------------------------------------------------===//
1352
Dan Gohman1ecaf452008-05-31 00:58:22 +00001353void InsertValueInst::init(Value *Agg, Value *Val,
1354 const unsigned *Idx, unsigned NumIdx) {
1355 assert(NumOperands == 2 && "NumOperands not initialized?");
1356 Op<0>() = Agg;
1357 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001358
Dan Gohman1ecaf452008-05-31 00:58:22 +00001359 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Dan Gohman0752bff2008-05-23 00:36:11 +00001360}
1361
Dan Gohman1ecaf452008-05-31 00:58:22 +00001362void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx) {
1363 assert(NumOperands == 2 && "NumOperands not initialized?");
1364 Op<0>() = Agg;
1365 Op<1>() = Val;
1366
1367 Indices.push_back(Idx);
Dan Gohman0752bff2008-05-23 00:36:11 +00001368}
1369
1370InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001371 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001372 OperandTraits<InsertValueInst>::op_begin(this), 2),
1373 Indices(IVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001374}
1375
1376//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001377// ExtractValueInst Class
1378//===----------------------------------------------------------------------===//
1379
Dan Gohman1ecaf452008-05-31 00:58:22 +00001380void ExtractValueInst::init(Value *Agg, const unsigned *Idx, unsigned NumIdx) {
1381 assert(NumOperands == 1 && "NumOperands not initialized?");
1382 Op<0>() = Agg;
Dan Gohman0752bff2008-05-23 00:36:11 +00001383
Dan Gohman1ecaf452008-05-31 00:58:22 +00001384 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Dan Gohman0752bff2008-05-23 00:36:11 +00001385}
1386
Dan Gohman1ecaf452008-05-31 00:58:22 +00001387void ExtractValueInst::init(Value *Agg, unsigned Idx) {
1388 assert(NumOperands == 1 && "NumOperands not initialized?");
1389 Op<0>() = Agg;
1390
1391 Indices.push_back(Idx);
Dan Gohman0752bff2008-05-23 00:36:11 +00001392}
1393
1394ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
1395 : Instruction(reinterpret_cast<const Type*>(EVI.getType()), ExtractValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001396 OperandTraits<ExtractValueInst>::op_begin(this), 1),
1397 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001398}
1399
Dan Gohman12fce772008-05-15 19:50:34 +00001400// getIndexedType - Returns the type of the element that would be extracted
1401// with an extractvalue instruction with the specified parameters.
1402//
1403// A null type is returned if the indices are invalid for the specified
1404// pointer type.
1405//
1406const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001407 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001408 unsigned NumIdx) {
1409 unsigned CurIdx = 0;
1410 for (; CurIdx != NumIdx; ++CurIdx) {
1411 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001412 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1413 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001414 if (!CT->indexValid(Index)) return 0;
1415 Agg = CT->getTypeAtIndex(Index);
1416
1417 // If the new type forwards to another type, then it is in the middle
1418 // of being refined to another type (and hence, may have dropped all
1419 // references to what it was using before). So, use the new forwarded
1420 // type.
1421 if (const Type *Ty = Agg->getForwardedType())
1422 Agg = Ty;
1423 }
1424 return CurIdx == NumIdx ? Agg : 0;
1425}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001426
1427//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001428// BinaryOperator Class
1429//===----------------------------------------------------------------------===//
1430
Chris Lattner2195fc42007-02-24 00:55:48 +00001431BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1432 const Type *Ty, const std::string &Name,
1433 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001434 : Instruction(Ty, iType,
1435 OperandTraits<BinaryOperator>::op_begin(this),
1436 OperandTraits<BinaryOperator>::operands(this),
1437 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001438 Op<0>() = S1;
1439 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001440 init(iType);
1441 setName(Name);
1442}
1443
1444BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1445 const Type *Ty, const std::string &Name,
1446 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001447 : Instruction(Ty, iType,
1448 OperandTraits<BinaryOperator>::op_begin(this),
1449 OperandTraits<BinaryOperator>::operands(this),
1450 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001451 Op<0>() = S1;
1452 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001453 init(iType);
1454 setName(Name);
1455}
1456
1457
1458void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001459 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001460 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001461 assert(LHS->getType() == RHS->getType() &&
1462 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001463#ifndef NDEBUG
1464 switch (iType) {
1465 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001466 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001467 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001468 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001469 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001470 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001471 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001472 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001473 case UDiv:
1474 case SDiv:
1475 assert(getType() == LHS->getType() &&
1476 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001477 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1478 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001479 "Incorrect operand type (not integer) for S/UDIV");
1480 break;
1481 case FDiv:
1482 assert(getType() == LHS->getType() &&
1483 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001484 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1485 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001486 && "Incorrect operand type (not floating point) for FDIV");
1487 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001488 case URem:
1489 case SRem:
1490 assert(getType() == LHS->getType() &&
1491 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001492 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1493 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001494 "Incorrect operand type (not integer) for S/UREM");
1495 break;
1496 case FRem:
1497 assert(getType() == LHS->getType() &&
1498 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001499 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1500 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001501 && "Incorrect operand type (not floating point) for FREM");
1502 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001503 case Shl:
1504 case LShr:
1505 case AShr:
1506 assert(getType() == LHS->getType() &&
1507 "Shift operation should return same type as operands!");
1508 assert(getType()->isInteger() &&
1509 "Shift operation requires integer operands");
1510 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001511 case And: case Or:
1512 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001513 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001514 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001515 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001516 (isa<VectorType>(getType()) &&
1517 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001518 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001519 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001520 default:
1521 break;
1522 }
1523#endif
1524}
1525
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001526BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001527 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001528 Instruction *InsertBefore) {
1529 assert(S1->getType() == S2->getType() &&
1530 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001531 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001532}
1533
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001534BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001535 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001536 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001537 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001538 InsertAtEnd->getInstList().push_back(Res);
1539 return Res;
1540}
1541
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001542BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001543 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001544 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1545 return new BinaryOperator(Instruction::Sub,
1546 zero, Op,
1547 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001548}
1549
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001550BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001551 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001552 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1553 return new BinaryOperator(Instruction::Sub,
1554 zero, Op,
1555 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001556}
1557
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001558BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001559 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001560 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001561 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001562 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001563 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001564 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001565 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001566 }
1567
1568 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001569 Op->getType(), Name, InsertBefore);
1570}
1571
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001572BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001573 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001574 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001575 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001576 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001577 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001578 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001579 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001580 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001581 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001582 }
1583
1584 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001585 Op->getType(), Name, InsertAtEnd);
1586}
1587
1588
1589// isConstantAllOnes - Helper function for several functions below
1590static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001591 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1592 return CI->isAllOnesValue();
1593 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1594 return CV->isAllOnesValue();
1595 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001596}
1597
1598bool BinaryOperator::isNeg(const Value *V) {
1599 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1600 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001601 return Bop->getOperand(0) ==
1602 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001603 return false;
1604}
1605
1606bool BinaryOperator::isNot(const Value *V) {
1607 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1608 return (Bop->getOpcode() == Instruction::Xor &&
1609 (isConstantAllOnes(Bop->getOperand(1)) ||
1610 isConstantAllOnes(Bop->getOperand(0))));
1611 return false;
1612}
1613
Chris Lattner2c7d1772005-04-24 07:28:37 +00001614Value *BinaryOperator::getNegArgument(Value *BinOp) {
1615 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1616 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001617}
1618
Chris Lattner2c7d1772005-04-24 07:28:37 +00001619const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1620 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001621}
1622
Chris Lattner2c7d1772005-04-24 07:28:37 +00001623Value *BinaryOperator::getNotArgument(Value *BinOp) {
1624 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1625 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1626 Value *Op0 = BO->getOperand(0);
1627 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001628 if (isConstantAllOnes(Op0)) return Op1;
1629
1630 assert(isConstantAllOnes(Op1));
1631 return Op0;
1632}
1633
Chris Lattner2c7d1772005-04-24 07:28:37 +00001634const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1635 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001636}
1637
1638
1639// swapOperands - Exchange the two operands to this instruction. This
1640// instruction is safe to use on any binary instruction and does not
1641// modify the semantics of the instruction. If the instruction is
1642// order dependent (SetLT f.e.) the opcode is changed.
1643//
1644bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001645 if (!isCommutative())
1646 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001647 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001648 return false;
1649}
1650
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001651//===----------------------------------------------------------------------===//
1652// CastInst Class
1653//===----------------------------------------------------------------------===//
1654
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001655// Just determine if this cast only deals with integral->integral conversion.
1656bool CastInst::isIntegerCast() const {
1657 switch (getOpcode()) {
1658 default: return false;
1659 case Instruction::ZExt:
1660 case Instruction::SExt:
1661 case Instruction::Trunc:
1662 return true;
1663 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001664 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001665 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001666}
1667
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001668bool CastInst::isLosslessCast() const {
1669 // Only BitCast can be lossless, exit fast if we're not BitCast
1670 if (getOpcode() != Instruction::BitCast)
1671 return false;
1672
1673 // Identity cast is always lossless
1674 const Type* SrcTy = getOperand(0)->getType();
1675 const Type* DstTy = getType();
1676 if (SrcTy == DstTy)
1677 return true;
1678
Reid Spencer8d9336d2006-12-31 05:26:44 +00001679 // Pointer to pointer is always lossless.
1680 if (isa<PointerType>(SrcTy))
1681 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001682 return false; // Other types have no identity values
1683}
1684
1685/// This function determines if the CastInst does not require any bits to be
1686/// changed in order to effect the cast. Essentially, it identifies cases where
1687/// no code gen is necessary for the cast, hence the name no-op cast. For
1688/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001689/// # bitcast i32* %x to i8*
1690/// # bitcast <2 x i32> %x to <4 x i16>
1691/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001692/// @brief Determine if a cast is a no-op.
1693bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1694 switch (getOpcode()) {
1695 default:
1696 assert(!"Invalid CastOp");
1697 case Instruction::Trunc:
1698 case Instruction::ZExt:
1699 case Instruction::SExt:
1700 case Instruction::FPTrunc:
1701 case Instruction::FPExt:
1702 case Instruction::UIToFP:
1703 case Instruction::SIToFP:
1704 case Instruction::FPToUI:
1705 case Instruction::FPToSI:
1706 return false; // These always modify bits
1707 case Instruction::BitCast:
1708 return true; // BitCast never modifies bits.
1709 case Instruction::PtrToInt:
1710 return IntPtrTy->getPrimitiveSizeInBits() ==
1711 getType()->getPrimitiveSizeInBits();
1712 case Instruction::IntToPtr:
1713 return IntPtrTy->getPrimitiveSizeInBits() ==
1714 getOperand(0)->getType()->getPrimitiveSizeInBits();
1715 }
1716}
1717
1718/// This function determines if a pair of casts can be eliminated and what
1719/// opcode should be used in the elimination. This assumes that there are two
1720/// instructions like this:
1721/// * %F = firstOpcode SrcTy %x to MidTy
1722/// * %S = secondOpcode MidTy %F to DstTy
1723/// The function returns a resultOpcode so these two casts can be replaced with:
1724/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1725/// If no such cast is permited, the function returns 0.
1726unsigned CastInst::isEliminableCastPair(
1727 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1728 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1729{
1730 // Define the 144 possibilities for these two cast instructions. The values
1731 // in this matrix determine what to do in a given situation and select the
1732 // case in the switch below. The rows correspond to firstOp, the columns
1733 // correspond to secondOp. In looking at the table below, keep in mind
1734 // the following cast properties:
1735 //
1736 // Size Compare Source Destination
1737 // Operator Src ? Size Type Sign Type Sign
1738 // -------- ------------ ------------------- ---------------------
1739 // TRUNC > Integer Any Integral Any
1740 // ZEXT < Integral Unsigned Integer Any
1741 // SEXT < Integral Signed Integer Any
1742 // FPTOUI n/a FloatPt n/a Integral Unsigned
1743 // FPTOSI n/a FloatPt n/a Integral Signed
1744 // UITOFP n/a Integral Unsigned FloatPt n/a
1745 // SITOFP n/a Integral Signed FloatPt n/a
1746 // FPTRUNC > FloatPt n/a FloatPt n/a
1747 // FPEXT < FloatPt n/a FloatPt n/a
1748 // PTRTOINT n/a Pointer n/a Integral Unsigned
1749 // INTTOPTR n/a Integral Unsigned Pointer n/a
1750 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001751 //
1752 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1753 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1754 // into "fptoui double to ulong", but this loses information about the range
1755 // of the produced value (we no longer know the top-part is all zeros).
1756 // Further this conversion is often much more expensive for typical hardware,
1757 // and causes issues when building libgcc. We disallow fptosi+sext for the
1758 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001759 const unsigned numCastOps =
1760 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1761 static const uint8_t CastResults[numCastOps][numCastOps] = {
1762 // T F F U S F F P I B -+
1763 // R Z S P P I I T P 2 N T |
1764 // U E E 2 2 2 2 R E I T C +- secondOp
1765 // N X X U S F F N X N 2 V |
1766 // C T T I I P P C T T P T -+
1767 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1768 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1769 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001770 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1771 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001772 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1773 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1774 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1775 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1776 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1777 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1778 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1779 };
1780
1781 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1782 [secondOp-Instruction::CastOpsBegin];
1783 switch (ElimCase) {
1784 case 0:
1785 // categorically disallowed
1786 return 0;
1787 case 1:
1788 // allowed, use first cast's opcode
1789 return firstOp;
1790 case 2:
1791 // allowed, use second cast's opcode
1792 return secondOp;
1793 case 3:
1794 // no-op cast in second op implies firstOp as long as the DestTy
1795 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001796 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001797 return firstOp;
1798 return 0;
1799 case 4:
1800 // no-op cast in second op implies firstOp as long as the DestTy
1801 // is floating point
1802 if (DstTy->isFloatingPoint())
1803 return firstOp;
1804 return 0;
1805 case 5:
1806 // no-op cast in first op implies secondOp as long as the SrcTy
1807 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001808 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001809 return secondOp;
1810 return 0;
1811 case 6:
1812 // no-op cast in first op implies secondOp as long as the SrcTy
1813 // is a floating point
1814 if (SrcTy->isFloatingPoint())
1815 return secondOp;
1816 return 0;
1817 case 7: {
1818 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1819 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1820 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1821 if (MidSize >= PtrSize)
1822 return Instruction::BitCast;
1823 return 0;
1824 }
1825 case 8: {
1826 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1827 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1828 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1829 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1830 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1831 if (SrcSize == DstSize)
1832 return Instruction::BitCast;
1833 else if (SrcSize < DstSize)
1834 return firstOp;
1835 return secondOp;
1836 }
1837 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1838 return Instruction::ZExt;
1839 case 10:
1840 // fpext followed by ftrunc is allowed if the bit size returned to is
1841 // the same as the original, in which case its just a bitcast
1842 if (SrcTy == DstTy)
1843 return Instruction::BitCast;
1844 return 0; // If the types are not the same we can't eliminate it.
1845 case 11:
1846 // bitcast followed by ptrtoint is allowed as long as the bitcast
1847 // is a pointer to pointer cast.
1848 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1849 return secondOp;
1850 return 0;
1851 case 12:
1852 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1853 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1854 return firstOp;
1855 return 0;
1856 case 13: {
1857 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1858 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1859 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1860 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1861 if (SrcSize <= PtrSize && SrcSize == DstSize)
1862 return Instruction::BitCast;
1863 return 0;
1864 }
1865 case 99:
1866 // cast combination can't happen (error in input). This is for all cases
1867 // where the MidTy is not the same for the two cast instructions.
1868 assert(!"Invalid Cast Combination");
1869 return 0;
1870 default:
1871 assert(!"Error in CastResults table!!!");
1872 return 0;
1873 }
1874 return 0;
1875}
1876
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001877CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001878 const std::string &Name, Instruction *InsertBefore) {
1879 // Construct and return the appropriate CastInst subclass
1880 switch (op) {
1881 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1882 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1883 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1884 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1885 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1886 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1887 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1888 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1889 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1890 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1891 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1892 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1893 default:
1894 assert(!"Invalid opcode provided");
1895 }
1896 return 0;
1897}
1898
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001899CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001900 const std::string &Name, BasicBlock *InsertAtEnd) {
1901 // Construct and return the appropriate CastInst subclass
1902 switch (op) {
1903 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1904 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1905 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1906 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1907 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1908 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1909 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1910 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1911 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1912 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1913 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1914 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1915 default:
1916 assert(!"Invalid opcode provided");
1917 }
1918 return 0;
1919}
1920
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001921CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001922 const std::string &Name,
1923 Instruction *InsertBefore) {
1924 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001925 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1926 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001927}
1928
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001929CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001930 const std::string &Name,
1931 BasicBlock *InsertAtEnd) {
1932 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001933 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1934 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001935}
1936
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001937CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001938 const std::string &Name,
1939 Instruction *InsertBefore) {
1940 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001941 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1942 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001943}
1944
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001945CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001946 const std::string &Name,
1947 BasicBlock *InsertAtEnd) {
1948 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001949 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1950 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001951}
1952
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001953CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001954 const std::string &Name,
1955 Instruction *InsertBefore) {
1956 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001957 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1958 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001959}
1960
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001961CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001962 const std::string &Name,
1963 BasicBlock *InsertAtEnd) {
1964 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001965 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1966 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001967}
1968
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001969CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001970 const std::string &Name,
1971 BasicBlock *InsertAtEnd) {
1972 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001973 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001974 "Invalid cast");
1975
Chris Lattner03c49532007-01-15 02:27:26 +00001976 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001977 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1978 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001979}
1980
1981/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001982CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001983 const std::string &Name,
1984 Instruction *InsertBefore) {
1985 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001986 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001987 "Invalid cast");
1988
Chris Lattner03c49532007-01-15 02:27:26 +00001989 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001990 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1991 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001992}
1993
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001994CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00001995 bool isSigned, const std::string &Name,
1996 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001997 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001998 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1999 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2000 Instruction::CastOps opcode =
2001 (SrcBits == DstBits ? Instruction::BitCast :
2002 (SrcBits > DstBits ? Instruction::Trunc :
2003 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002004 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002005}
2006
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002007CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002008 bool isSigned, const std::string &Name,
2009 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00002010 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002011 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2012 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2013 Instruction::CastOps opcode =
2014 (SrcBits == DstBits ? Instruction::BitCast :
2015 (SrcBits > DstBits ? Instruction::Trunc :
2016 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002017 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002018}
2019
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002020CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002021 const std::string &Name,
2022 Instruction *InsertBefore) {
2023 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2024 "Invalid cast");
2025 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2026 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2027 Instruction::CastOps opcode =
2028 (SrcBits == DstBits ? Instruction::BitCast :
2029 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002030 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002031}
2032
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002033CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002034 const std::string &Name,
2035 BasicBlock *InsertAtEnd) {
2036 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2037 "Invalid cast");
2038 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2039 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2040 Instruction::CastOps opcode =
2041 (SrcBits == DstBits ? Instruction::BitCast :
2042 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002043 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002044}
2045
Duncan Sands55e50902008-01-06 10:12:28 +00002046// Check whether it is valid to call getCastOpcode for these types.
2047// This routine must be kept in sync with getCastOpcode.
2048bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2049 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2050 return false;
2051
2052 if (SrcTy == DestTy)
2053 return true;
2054
2055 // Get the bit sizes, we'll need these
2056 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2057 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2058
2059 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002060 if (DestTy->isInteger()) { // Casting to integral
2061 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002062 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002063 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002064 return true;
2065 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002066 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002067 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002068 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002069 return isa<PointerType>(SrcTy);
2070 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002071 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2072 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002073 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002074 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002075 return true;
2076 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002077 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002078 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002079 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002080 return false;
2081 }
2082 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002083 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002084 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002085 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002086 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002087 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002088 return DestPTy->getBitWidth() == SrcBits;
2089 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002090 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2091 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002092 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002093 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002094 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002095 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002096 return false;
2097 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002098 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002099 return false;
2100 }
2101}
2102
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002103// Provide a way to get a "cast" where the cast opcode is inferred from the
2104// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002105// logic in the castIsValid function below. This axiom should hold:
2106// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2107// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002108// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002109// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002110Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002111CastInst::getCastOpcode(
2112 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002113 // Get the bit sizes, we'll need these
2114 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002115 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2116 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002117
Duncan Sands55e50902008-01-06 10:12:28 +00002118 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2119 "Only first class types are castable!");
2120
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002121 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002122 if (DestTy->isInteger()) { // Casting to integral
2123 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002124 if (DestBits < SrcBits)
2125 return Trunc; // int -> smaller int
2126 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002127 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002128 return SExt; // signed -> SEXT
2129 else
2130 return ZExt; // unsigned -> ZEXT
2131 } else {
2132 return BitCast; // Same size, No-op cast
2133 }
2134 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002135 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002136 return FPToSI; // FP -> sint
2137 else
2138 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002139 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002140 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002141 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002142 return BitCast; // Same size, no-op cast
2143 } else {
2144 assert(isa<PointerType>(SrcTy) &&
2145 "Casting from a value that is not first-class type");
2146 return PtrToInt; // ptr -> int
2147 }
2148 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002149 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002150 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002151 return SIToFP; // sint -> FP
2152 else
2153 return UIToFP; // uint -> FP
2154 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2155 if (DestBits < SrcBits) {
2156 return FPTrunc; // FP -> smaller FP
2157 } else if (DestBits > SrcBits) {
2158 return FPExt; // FP -> larger FP
2159 } else {
2160 return BitCast; // same size, no-op cast
2161 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002162 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002163 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002164 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002165 return BitCast; // same size, no-op cast
2166 } else {
2167 assert(0 && "Casting pointer or non-first class to float");
2168 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002169 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2170 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002171 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002172 "Casting vector to vector of different widths");
2173 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002174 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002175 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002176 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002177 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002178 }
2179 } else if (isa<PointerType>(DestTy)) {
2180 if (isa<PointerType>(SrcTy)) {
2181 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002182 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002183 return IntToPtr; // int -> ptr
2184 } else {
2185 assert(!"Casting pointer to other than pointer or int");
2186 }
2187 } else {
2188 assert(!"Casting to type that is not first-class");
2189 }
2190
2191 // If we fall through to here we probably hit an assertion cast above
2192 // and assertions are not turned on. Anything we return is an error, so
2193 // BitCast is as good a choice as any.
2194 return BitCast;
2195}
2196
2197//===----------------------------------------------------------------------===//
2198// CastInst SubClass Constructors
2199//===----------------------------------------------------------------------===//
2200
2201/// Check that the construction parameters for a CastInst are correct. This
2202/// could be broken out into the separate constructors but it is useful to have
2203/// it in one place and to eliminate the redundant code for getting the sizes
2204/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002205bool
2206CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002207
2208 // Check for type sanity on the arguments
2209 const Type *SrcTy = S->getType();
2210 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2211 return false;
2212
2213 // Get the size of the types in bits, we'll need this later
2214 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2215 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2216
2217 // Switch on the opcode provided
2218 switch (op) {
2219 default: return false; // This is an input error
2220 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002221 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002222 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002223 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002224 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002225 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002226 case Instruction::FPTrunc:
2227 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2228 SrcBitSize > DstBitSize;
2229 case Instruction::FPExt:
2230 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2231 SrcBitSize < DstBitSize;
2232 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002233 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002234 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2235 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2236 return SVTy->getElementType()->isInteger() &&
2237 DVTy->getElementType()->isFloatingPoint() &&
2238 SVTy->getNumElements() == DVTy->getNumElements();
2239 }
2240 }
Chris Lattner03c49532007-01-15 02:27:26 +00002241 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002242 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002243 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002244 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2245 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2246 return SVTy->getElementType()->isFloatingPoint() &&
2247 DVTy->getElementType()->isInteger() &&
2248 SVTy->getNumElements() == DVTy->getNumElements();
2249 }
2250 }
Chris Lattner03c49532007-01-15 02:27:26 +00002251 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002252 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002253 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002254 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002255 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002256 case Instruction::BitCast:
2257 // BitCast implies a no-op cast of type only. No bits change.
2258 // However, you can't cast pointers to anything but pointers.
2259 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2260 return false;
2261
Duncan Sands55e50902008-01-06 10:12:28 +00002262 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002263 // these cases, the cast is okay if the source and destination bit widths
2264 // are identical.
2265 return SrcBitSize == DstBitSize;
2266 }
2267}
2268
2269TruncInst::TruncInst(
2270 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2271) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002272 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002273}
2274
2275TruncInst::TruncInst(
2276 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2277) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002278 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002279}
2280
2281ZExtInst::ZExtInst(
2282 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2283) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002284 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002285}
2286
2287ZExtInst::ZExtInst(
2288 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2289) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002290 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002291}
2292SExtInst::SExtInst(
2293 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2294) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002295 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002296}
2297
Jeff Cohencc08c832006-12-02 02:22:01 +00002298SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2300) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002301 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002302}
2303
2304FPTruncInst::FPTruncInst(
2305 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2306) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002307 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002308}
2309
2310FPTruncInst::FPTruncInst(
2311 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2312) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002313 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002314}
2315
2316FPExtInst::FPExtInst(
2317 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2318) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002319 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002320}
2321
2322FPExtInst::FPExtInst(
2323 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2324) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002325 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002326}
2327
2328UIToFPInst::UIToFPInst(
2329 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2330) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002331 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002332}
2333
2334UIToFPInst::UIToFPInst(
2335 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2336) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002337 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002338}
2339
2340SIToFPInst::SIToFPInst(
2341 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2342) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002343 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344}
2345
2346SIToFPInst::SIToFPInst(
2347 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2348) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002349 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002350}
2351
2352FPToUIInst::FPToUIInst(
2353 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2354) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002355 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002356}
2357
2358FPToUIInst::FPToUIInst(
2359 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2360) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002361 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362}
2363
2364FPToSIInst::FPToSIInst(
2365 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2366) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002367 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002368}
2369
2370FPToSIInst::FPToSIInst(
2371 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2372) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002373 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002374}
2375
2376PtrToIntInst::PtrToIntInst(
2377 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2378) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002379 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002380}
2381
2382PtrToIntInst::PtrToIntInst(
2383 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2384) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002385 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002386}
2387
2388IntToPtrInst::IntToPtrInst(
2389 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2390) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002391 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392}
2393
2394IntToPtrInst::IntToPtrInst(
2395 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2396) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002397 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398}
2399
2400BitCastInst::BitCastInst(
2401 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2402) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002403 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002404}
2405
2406BitCastInst::BitCastInst(
2407 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2408) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002409 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002410}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002411
2412//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002413// CmpInst Classes
2414//===----------------------------------------------------------------------===//
2415
Nate Begemand2195702008-05-12 19:01:56 +00002416CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2417 Value *LHS, Value *RHS, const std::string &Name,
2418 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002419 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002420 OperandTraits<CmpInst>::op_begin(this),
2421 OperandTraits<CmpInst>::operands(this),
2422 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002423 Op<0>() = LHS;
2424 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002425 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002426 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002427}
Gabor Greiff6caff662008-05-10 08:32:32 +00002428
Nate Begemand2195702008-05-12 19:01:56 +00002429CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2430 Value *LHS, Value *RHS, const std::string &Name,
2431 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002432 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002433 OperandTraits<CmpInst>::op_begin(this),
2434 OperandTraits<CmpInst>::operands(this),
2435 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002436 Op<0>() = LHS;
2437 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002438 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002439 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002440}
2441
2442CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002443CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002444 const std::string &Name, Instruction *InsertBefore) {
2445 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002446 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002447 InsertBefore);
2448 }
Nate Begemand2195702008-05-12 19:01:56 +00002449 if (Op == Instruction::FCmp) {
2450 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2451 InsertBefore);
2452 }
2453 if (Op == Instruction::VICmp) {
2454 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2455 InsertBefore);
2456 }
2457 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2458 InsertBefore);
Reid Spencerd9436b62006-11-20 01:22:35 +00002459}
2460
2461CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002462CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002463 const std::string &Name, BasicBlock *InsertAtEnd) {
2464 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002465 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002466 InsertAtEnd);
2467 }
Nate Begemand2195702008-05-12 19:01:56 +00002468 if (Op == Instruction::FCmp) {
2469 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2470 InsertAtEnd);
2471 }
2472 if (Op == Instruction::VICmp) {
2473 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2474 InsertAtEnd);
2475 }
2476 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2477 InsertAtEnd);
Reid Spencerd9436b62006-11-20 01:22:35 +00002478}
2479
2480void CmpInst::swapOperands() {
2481 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2482 IC->swapOperands();
2483 else
2484 cast<FCmpInst>(this)->swapOperands();
2485}
2486
2487bool CmpInst::isCommutative() {
2488 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2489 return IC->isCommutative();
2490 return cast<FCmpInst>(this)->isCommutative();
2491}
2492
2493bool CmpInst::isEquality() {
2494 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2495 return IC->isEquality();
2496 return cast<FCmpInst>(this)->isEquality();
2497}
2498
2499
Dan Gohman4e724382008-05-31 02:47:54 +00002500CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002501 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002502 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002503 case ICMP_EQ: return ICMP_NE;
2504 case ICMP_NE: return ICMP_EQ;
2505 case ICMP_UGT: return ICMP_ULE;
2506 case ICMP_ULT: return ICMP_UGE;
2507 case ICMP_UGE: return ICMP_ULT;
2508 case ICMP_ULE: return ICMP_UGT;
2509 case ICMP_SGT: return ICMP_SLE;
2510 case ICMP_SLT: return ICMP_SGE;
2511 case ICMP_SGE: return ICMP_SLT;
2512 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002513
Dan Gohman4e724382008-05-31 02:47:54 +00002514 case FCMP_OEQ: return FCMP_UNE;
2515 case FCMP_ONE: return FCMP_UEQ;
2516 case FCMP_OGT: return FCMP_ULE;
2517 case FCMP_OLT: return FCMP_UGE;
2518 case FCMP_OGE: return FCMP_ULT;
2519 case FCMP_OLE: return FCMP_UGT;
2520 case FCMP_UEQ: return FCMP_ONE;
2521 case FCMP_UNE: return FCMP_OEQ;
2522 case FCMP_UGT: return FCMP_OLE;
2523 case FCMP_ULT: return FCMP_OGE;
2524 case FCMP_UGE: return FCMP_OLT;
2525 case FCMP_ULE: return FCMP_OGT;
2526 case FCMP_ORD: return FCMP_UNO;
2527 case FCMP_UNO: return FCMP_ORD;
2528 case FCMP_TRUE: return FCMP_FALSE;
2529 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002530 }
2531}
2532
Reid Spencer266e42b2006-12-23 06:05:41 +00002533ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2534 switch (pred) {
2535 default: assert(! "Unknown icmp predicate!");
2536 case ICMP_EQ: case ICMP_NE:
2537 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2538 return pred;
2539 case ICMP_UGT: return ICMP_SGT;
2540 case ICMP_ULT: return ICMP_SLT;
2541 case ICMP_UGE: return ICMP_SGE;
2542 case ICMP_ULE: return ICMP_SLE;
2543 }
2544}
2545
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002546ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2547 switch (pred) {
2548 default: assert(! "Unknown icmp predicate!");
2549 case ICMP_EQ: case ICMP_NE:
2550 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2551 return pred;
2552 case ICMP_SGT: return ICMP_UGT;
2553 case ICMP_SLT: return ICMP_ULT;
2554 case ICMP_SGE: return ICMP_UGE;
2555 case ICMP_SLE: return ICMP_ULE;
2556 }
2557}
2558
Reid Spencer266e42b2006-12-23 06:05:41 +00002559bool ICmpInst::isSignedPredicate(Predicate pred) {
2560 switch (pred) {
2561 default: assert(! "Unknown icmp predicate!");
2562 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2563 return true;
2564 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2565 case ICMP_UGE: case ICMP_ULE:
2566 return false;
2567 }
2568}
2569
Reid Spencer0286bc12007-02-28 22:00:54 +00002570/// Initialize a set of values that all satisfy the condition with C.
2571///
2572ConstantRange
2573ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2574 APInt Lower(C);
2575 APInt Upper(C);
2576 uint32_t BitWidth = C.getBitWidth();
2577 switch (pred) {
2578 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2579 case ICmpInst::ICMP_EQ: Upper++; break;
2580 case ICmpInst::ICMP_NE: Lower++; break;
2581 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2582 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2583 case ICmpInst::ICMP_UGT:
2584 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2585 break;
2586 case ICmpInst::ICMP_SGT:
2587 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2588 break;
2589 case ICmpInst::ICMP_ULE:
2590 Lower = APInt::getMinValue(BitWidth); Upper++;
2591 break;
2592 case ICmpInst::ICMP_SLE:
2593 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2594 break;
2595 case ICmpInst::ICMP_UGE:
2596 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2597 break;
2598 case ICmpInst::ICMP_SGE:
2599 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2600 break;
2601 }
2602 return ConstantRange(Lower, Upper);
2603}
2604
Dan Gohman4e724382008-05-31 02:47:54 +00002605CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002606 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002607 default: assert(!"Unknown cmp predicate!");
2608 case ICMP_EQ: case ICMP_NE:
2609 return pred;
2610 case ICMP_SGT: return ICMP_SLT;
2611 case ICMP_SLT: return ICMP_SGT;
2612 case ICMP_SGE: return ICMP_SLE;
2613 case ICMP_SLE: return ICMP_SGE;
2614 case ICMP_UGT: return ICMP_ULT;
2615 case ICMP_ULT: return ICMP_UGT;
2616 case ICMP_UGE: return ICMP_ULE;
2617 case ICMP_ULE: return ICMP_UGE;
2618
Reid Spencerd9436b62006-11-20 01:22:35 +00002619 case FCMP_FALSE: case FCMP_TRUE:
2620 case FCMP_OEQ: case FCMP_ONE:
2621 case FCMP_UEQ: case FCMP_UNE:
2622 case FCMP_ORD: case FCMP_UNO:
2623 return pred;
2624 case FCMP_OGT: return FCMP_OLT;
2625 case FCMP_OLT: return FCMP_OGT;
2626 case FCMP_OGE: return FCMP_OLE;
2627 case FCMP_OLE: return FCMP_OGE;
2628 case FCMP_UGT: return FCMP_ULT;
2629 case FCMP_ULT: return FCMP_UGT;
2630 case FCMP_UGE: return FCMP_ULE;
2631 case FCMP_ULE: return FCMP_UGE;
2632 }
2633}
2634
Reid Spencer266e42b2006-12-23 06:05:41 +00002635bool CmpInst::isUnsigned(unsigned short predicate) {
2636 switch (predicate) {
2637 default: return false;
2638 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2639 case ICmpInst::ICMP_UGE: return true;
2640 }
2641}
2642
2643bool CmpInst::isSigned(unsigned short predicate){
2644 switch (predicate) {
2645 default: return false;
2646 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2647 case ICmpInst::ICMP_SGE: return true;
2648 }
2649}
2650
2651bool CmpInst::isOrdered(unsigned short predicate) {
2652 switch (predicate) {
2653 default: return false;
2654 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2655 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2656 case FCmpInst::FCMP_ORD: return true;
2657 }
2658}
2659
2660bool CmpInst::isUnordered(unsigned short predicate) {
2661 switch (predicate) {
2662 default: return false;
2663 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2664 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2665 case FCmpInst::FCMP_UNO: return true;
2666 }
2667}
2668
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002669//===----------------------------------------------------------------------===//
2670// SwitchInst Implementation
2671//===----------------------------------------------------------------------===//
2672
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002673void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002674 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002675 ReservedSpace = 2+NumCases*2;
2676 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002677 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002678
Gabor Greif2d3024d2008-05-26 21:33:52 +00002679 OperandList[0] = Value;
2680 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002681}
2682
Chris Lattner2195fc42007-02-24 00:55:48 +00002683/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2684/// switch on and a default destination. The number of additional cases can
2685/// be specified here to make memory allocation more efficient. This
2686/// constructor can also autoinsert before another instruction.
2687SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2688 Instruction *InsertBefore)
2689 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2690 init(Value, Default, NumCases);
2691}
2692
2693/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2694/// switch on and a default destination. The number of additional cases can
2695/// be specified here to make memory allocation more efficient. This
2696/// constructor also autoinserts at the end of the specified BasicBlock.
2697SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2698 BasicBlock *InsertAtEnd)
2699 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2700 init(Value, Default, NumCases);
2701}
2702
Misha Brukmanb1c93172005-04-21 23:48:37 +00002703SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002704 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002705 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002706 Use *OL = OperandList, *InOL = SI.OperandList;
2707 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002708 OL[i] = InOL[i];
2709 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002710 }
2711}
2712
Gordon Henriksen14a55692007-12-10 02:14:30 +00002713SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002714 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002715}
2716
2717
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002718/// addCase - Add an entry to the switch instruction...
2719///
Chris Lattner47ac1872005-02-24 05:32:09 +00002720void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002721 unsigned OpNo = NumOperands;
2722 if (OpNo+2 > ReservedSpace)
2723 resizeOperands(0); // Get more space!
2724 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002725 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002726 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002727 OperandList[OpNo] = OnVal;
2728 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002729}
2730
2731/// removeCase - This method removes the specified successor from the switch
2732/// instruction. Note that this cannot be used to remove the default
2733/// destination (successor #0).
2734///
2735void SwitchInst::removeCase(unsigned idx) {
2736 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002737 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2738
2739 unsigned NumOps = getNumOperands();
2740 Use *OL = OperandList;
2741
2742 // Move everything after this operand down.
2743 //
2744 // FIXME: we could just swap with the end of the list, then erase. However,
2745 // client might not expect this to happen. The code as it is thrashes the
2746 // use/def lists, which is kinda lame.
2747 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2748 OL[i-2] = OL[i];
2749 OL[i-2+1] = OL[i+1];
2750 }
2751
2752 // Nuke the last value.
2753 OL[NumOps-2].set(0);
2754 OL[NumOps-2+1].set(0);
2755 NumOperands = NumOps-2;
2756}
2757
2758/// resizeOperands - resize operands - This adjusts the length of the operands
2759/// list according to the following behavior:
2760/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002761/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002762/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2763/// 3. If NumOps == NumOperands, trim the reserved space.
2764///
2765void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002766 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002767 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002768 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002769 } else if (NumOps*2 > NumOperands) {
2770 // No resize needed.
2771 if (ReservedSpace >= NumOps) return;
2772 } else if (NumOps == NumOperands) {
2773 if (ReservedSpace == NumOps) return;
2774 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002775 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002776 }
2777
2778 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002779 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002780 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002781 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002782 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002783 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002784 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002785 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002786}
2787
2788
2789BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2790 return getSuccessor(idx);
2791}
2792unsigned SwitchInst::getNumSuccessorsV() const {
2793 return getNumSuccessors();
2794}
2795void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2796 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002797}
Chris Lattnerf22be932004-10-15 23:52:53 +00002798
Devang Patel295711f2008-02-19 22:15:16 +00002799//===----------------------------------------------------------------------===//
2800// GetResultInst Implementation
2801//===----------------------------------------------------------------------===//
2802
Devang Patel666d4512008-02-20 19:10:47 +00002803GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002804 const std::string &Name,
2805 Instruction *InsertBef)
Gabor Greif0d42adf2008-05-13 07:09:08 +00002806 : UnaryInstruction(cast<StructType>(Aggregate->getType())
2807 ->getElementType(Index),
2808 GetResult, Aggregate, InsertBef),
2809 Idx(Index) {
2810 assert(isValidOperands(Aggregate, Index)
2811 && "Invalid GetResultInst operands!");
Devang Patel295711f2008-02-19 22:15:16 +00002812 setName(Name);
2813}
2814
Devang Patel666d4512008-02-20 19:10:47 +00002815bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2816 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002817 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002818
Devang Patelcf193b82008-02-20 19:39:41 +00002819 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2820 unsigned NumElements = STy->getNumElements();
Chris Lattnerb6917d22008-04-23 05:36:34 +00002821 if (Index >= NumElements || NumElements == 0)
Devang Patelcf193b82008-02-20 19:39:41 +00002822 return false;
2823
2824 // getresult aggregate value's element types are restricted to
2825 // avoid nested aggregates.
2826 for (unsigned i = 0; i < NumElements; ++i)
2827 if (!STy->getElementType(i)->isFirstClassType())
2828 return false;
2829
2830 // Otherwise, Aggregate is valid.
2831 return true;
2832 }
Devang Patel666d4512008-02-20 19:10:47 +00002833 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002834}
2835
Chris Lattnerf22be932004-10-15 23:52:53 +00002836// Define these methods here so vtables don't get emitted into every translation
2837// unit that uses these classes.
2838
2839GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002840 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002841}
2842
2843BinaryOperator *BinaryOperator::clone() const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002844 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002845}
2846
Chris Lattner0b490b02007-08-24 20:48:18 +00002847FCmpInst* FCmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002848 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002849}
2850ICmpInst* ICmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002851 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002852}
2853
Nate Begemand2195702008-05-12 19:01:56 +00002854VFCmpInst* VFCmpInst::clone() const {
2855 return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
2856}
2857VICmpInst* VICmpInst::clone() const {
2858 return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
2859}
2860
Dan Gohman0752bff2008-05-23 00:36:11 +00002861ExtractValueInst *ExtractValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002862 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002863}
2864InsertValueInst *InsertValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002865 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002866}
2867
2868
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002869MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2870AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2871FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2872LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2873StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2874CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2875CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2876CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2877CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2878CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2879CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2880CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2881CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2882CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2883CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2884CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2885CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002886CallInst *CallInst::clone() const {
2887 return new(getNumOperands()) CallInst(*this);
2888}
2889SelectInst *SelectInst::clone() const {
2890 return new(getNumOperands()) SelectInst(*this);
2891}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002892VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2893
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002894ExtractElementInst *ExtractElementInst::clone() const {
2895 return new ExtractElementInst(*this);
2896}
2897InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002898 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002899}
2900ShuffleVectorInst *ShuffleVectorInst::clone() const {
2901 return new ShuffleVectorInst(*this);
2902}
Chris Lattnerf22be932004-10-15 23:52:53 +00002903PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002904ReturnInst *ReturnInst::clone() const {
2905 return new(getNumOperands()) ReturnInst(*this);
2906}
2907BranchInst *BranchInst::clone() const {
2908 return new(getNumOperands()) BranchInst(*this);
2909}
Gabor Greiff6caff662008-05-10 08:32:32 +00002910SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002911InvokeInst *InvokeInst::clone() const {
2912 return new(getNumOperands()) InvokeInst(*this);
2913}
Chris Lattnerf22be932004-10-15 23:52:53 +00002914UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002915UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002916GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }