blob: 4bcd560ee8358782f84ca604c6354491481e46b2 [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
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000094bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000095 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
96 if (AI->get() == Arg)
97 return true;
98 return false;
99}
100
Gordon Henriksen14a55692007-12-10 02:14:30 +0000101//===----------------------------------------------------------------------===//
102// TerminatorInst Class
103//===----------------------------------------------------------------------===//
104
105// Out of line virtual method, so the vtable, etc has a home.
106TerminatorInst::~TerminatorInst() {
107}
108
Gabor Greiff6caff662008-05-10 08:32:32 +0000109//===----------------------------------------------------------------------===//
110// UnaryInstruction Class
111//===----------------------------------------------------------------------===//
112
Gordon Henriksen14a55692007-12-10 02:14:30 +0000113// Out of line virtual method, so the vtable, etc has a home.
114UnaryInstruction::~UnaryInstruction() {
115}
116
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117//===----------------------------------------------------------------------===//
118// PHINode Class
119//===----------------------------------------------------------------------===//
120
121PHINode::PHINode(const PHINode &PN)
122 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000123 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000124 ReservedSpace(PN.getNumOperands()) {
125 Use *OL = OperandList;
126 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000127 OL[i] = PN.getOperand(i);
128 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000129 }
130}
131
Gordon Henriksen14a55692007-12-10 02:14:30 +0000132PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000133 if (OperandList)
134 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000135}
136
137// removeIncomingValue - Remove an incoming value. This is useful if a
138// predecessor basic block is deleted.
139Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
140 unsigned NumOps = getNumOperands();
141 Use *OL = OperandList;
142 assert(Idx*2 < NumOps && "BB not in PHI node!");
143 Value *Removed = OL[Idx*2];
144
145 // Move everything after this operand down.
146 //
147 // FIXME: we could just swap with the end of the list, then erase. However,
148 // client might not expect this to happen. The code as it is thrashes the
149 // use/def lists, which is kinda lame.
150 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
151 OL[i-2] = OL[i];
152 OL[i-2+1] = OL[i+1];
153 }
154
155 // Nuke the last value.
156 OL[NumOps-2].set(0);
157 OL[NumOps-2+1].set(0);
158 NumOperands = NumOps-2;
159
160 // If the PHI node is dead, because it has zero entries, nuke it now.
161 if (NumOps == 2 && DeletePHIIfEmpty) {
162 // If anyone is using this PHI, make them use a dummy value instead...
163 replaceAllUsesWith(UndefValue::get(getType()));
164 eraseFromParent();
165 }
166 return Removed;
167}
168
169/// resizeOperands - resize operands - This adjusts the length of the operands
170/// list according to the following behavior:
171/// 1. If NumOps == 0, grow the operand list in response to a push_back style
172/// of operation. This grows the number of ops by 1.5 times.
173/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
174/// 3. If NumOps == NumOperands, trim the reserved space.
175///
176void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000177 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000178 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000179 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000180 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
181 } else if (NumOps*2 > NumOperands) {
182 // No resize needed.
183 if (ReservedSpace >= NumOps) return;
184 } else if (NumOps == NumOperands) {
185 if (ReservedSpace == NumOps) return;
186 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000187 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000188 }
189
190 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000191 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000192 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000193 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000194 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000195 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000196}
197
Nate Begemanb3923212005-08-04 23:24:19 +0000198/// hasConstantValue - If the specified PHI node always merges together the same
199/// value, return the value, otherwise return null.
200///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000201Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000202 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000203 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000204 if (getIncomingValue(0) != this) // not X = phi X
205 return getIncomingValue(0);
206 else
207 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000208 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000209
Nate Begemanb3923212005-08-04 23:24:19 +0000210 // Otherwise if all of the incoming values are the same for the PHI, replace
211 // the PHI node with the incoming value.
212 //
213 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000214 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000215 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000216 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000217 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000218 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000219 if (InVal && getIncomingValue(i) != InVal)
220 return 0; // Not the same, bail out.
221 else
222 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000223 }
Nate Begemanb3923212005-08-04 23:24:19 +0000224
225 // The only case that could cause InVal to be null is if we have a PHI node
226 // that only has entries for itself. In this case, there is no entry into the
227 // loop, so kill the PHI.
228 //
229 if (InVal == 0) InVal = UndefValue::get(getType());
230
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000231 // If we have a PHI node like phi(X, undef, X), where X is defined by some
232 // instruction, we cannot always return X as the result of the PHI node. Only
233 // do this if X is not an instruction (thus it must dominate the PHI block),
234 // or if the client is prepared to deal with this possibility.
235 if (HasUndefInput && !AllowNonDominatingInstruction)
236 if (Instruction *IV = dyn_cast<Instruction>(InVal))
237 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000238 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000239 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000240 return 0; // Cannot guarantee that InVal dominates this PHINode.
241
Nate Begemanb3923212005-08-04 23:24:19 +0000242 // All of the incoming values are the same, return the value now.
243 return InVal;
244}
245
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000246
247//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000248// CallInst Implementation
249//===----------------------------------------------------------------------===//
250
Gordon Henriksen14a55692007-12-10 02:14:30 +0000251CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000252}
253
Chris Lattner054ba2c2007-02-13 00:58:44 +0000254void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000255 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
256 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000257 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000258
Misha Brukmanb1c93172005-04-21 23:48:37 +0000259 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000261 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262
Chris Lattner054ba2c2007-02-13 00:58:44 +0000263 assert((NumParams == FTy->getNumParams() ||
264 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000265 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000266 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000267 assert((i >= FTy->getNumParams() ||
268 FTy->getParamType(i) == Params[i]->getType()) &&
269 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000270 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000271 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272}
273
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000274void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000275 assert(NumOperands == 3 && "NumOperands not set up?");
276 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000277 OL[0] = Func;
278 OL[1] = Actual1;
279 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000280
281 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000282 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000283 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000285 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000286 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000288 assert((0 >= FTy->getNumParams() ||
289 FTy->getParamType(0) == Actual1->getType()) &&
290 "Calling a function with a bad signature!");
291 assert((1 >= FTy->getNumParams() ||
292 FTy->getParamType(1) == Actual2->getType()) &&
293 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294}
295
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000296void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000297 assert(NumOperands == 2 && "NumOperands not set up?");
298 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000299 OL[0] = Func;
300 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000301
302 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000304 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000305
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000306 assert((FTy->getNumParams() == 1 ||
307 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000308 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000309 assert((0 == FTy->getNumParams() ||
310 FTy->getParamType(0) == Actual->getType()) &&
311 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312}
313
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000314void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000315 assert(NumOperands == 1 && "NumOperands not set up?");
316 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000317 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000318
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000319 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000321 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000323 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324}
325
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000326CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000327 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000328 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
329 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000330 Instruction::Call,
331 OperandTraits<CallInst>::op_end(this) - 2,
332 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000333 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000334 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335}
336
337CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
338 BasicBlock *InsertAtEnd)
339 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
340 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000341 Instruction::Call,
342 OperandTraits<CallInst>::op_end(this) - 2,
343 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000344 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000345 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000346}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000347CallInst::CallInst(Value *Func, const std::string &Name,
348 Instruction *InsertBefore)
349 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
350 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000351 Instruction::Call,
352 OperandTraits<CallInst>::op_end(this) - 1,
353 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000354 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000355 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000356}
357
358CallInst::CallInst(Value *Func, const std::string &Name,
359 BasicBlock *InsertAtEnd)
360 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
361 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000362 Instruction::Call,
363 OperandTraits<CallInst>::op_end(this) - 1,
364 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000365 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000366 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000367}
368
Misha Brukmanb1c93172005-04-21 23:48:37 +0000369CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000370 : Instruction(CI.getType(), Instruction::Call,
371 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000372 CI.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000373 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000374 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000375 Use *OL = OperandList;
376 Use *InOL = CI.OperandList;
377 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000378 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000379}
380
Eric Christopher901b1a72008-05-16 20:39:43 +0000381void CallInst::addParamAttr(unsigned i, ParameterAttributes attr) {
382 PAListPtr PAL = getParamAttrs();
383 PAL = PAL.addAttr(i, attr);
384 setParamAttrs(PAL);
385}
386
Chris Lattnerc994c022008-03-13 05:00:21 +0000387bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000388 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000389 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000390 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000391 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000392 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000393}
394
Duncan Sandsaa31b922007-12-19 21:13:37 +0000395void CallInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000396 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000397 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000398 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000399 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000400 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000401 setParamAttrs(PAL);
402}
403
Duncan Sands5208d1a2007-11-28 17:07:01 +0000404
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000405//===----------------------------------------------------------------------===//
406// InvokeInst Implementation
407//===----------------------------------------------------------------------===//
408
409void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000410 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000411 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
412 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000413 OL[0] = Fn;
414 OL[1] = IfNormal;
415 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000416 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000417 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000418 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000419
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000420 assert(((NumArgs == FTy->getNumParams()) ||
421 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000422 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000423
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000424 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000425 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000426 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000427 "Invoking a function with a bad signature!");
428
Gabor Greif2d3024d2008-05-26 21:33:52 +0000429 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000430 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000431}
432
Misha Brukmanb1c93172005-04-21 23:48:37 +0000433InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000434 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000435 OperandTraits<InvokeInst>::op_end(this)
436 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000437 II.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000438 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000439 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000440 Use *OL = OperandList, *InOL = II.OperandList;
441 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000442 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000443}
444
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000445BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
446 return getSuccessor(idx);
447}
448unsigned InvokeInst::getNumSuccessorsV() const {
449 return getNumSuccessors();
450}
451void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
452 return setSuccessor(idx, B);
453}
454
Chris Lattnerc994c022008-03-13 05:00:21 +0000455bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000456 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000457 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000458 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000459 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000460 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000461}
462
Eric Christopher901b1a72008-05-16 20:39:43 +0000463void InvokeInst::addParamAttr(unsigned i, ParameterAttributes attr) {
464 PAListPtr PAL = getParamAttrs();
465 PAL = PAL.addAttr(i, attr);
466 setParamAttrs(PAL);
467}
468
Duncan Sandsaa31b922007-12-19 21:13:37 +0000469void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000470 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000471 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000472 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000473 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000474 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000475 setParamAttrs(PAL);
476}
477
Duncan Sands5208d1a2007-11-28 17:07:01 +0000478
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000479//===----------------------------------------------------------------------===//
480// ReturnInst Implementation
481//===----------------------------------------------------------------------===//
482
Chris Lattner2195fc42007-02-24 00:55:48 +0000483ReturnInst::ReturnInst(const ReturnInst &RI)
484 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Gabor Greif697e94c2008-05-15 10:04:30 +0000485 OperandTraits<ReturnInst>::op_end(this)
486 - RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000487 RI.getNumOperands()) {
Devang Patel59643e52008-02-23 00:35:18 +0000488 unsigned N = RI.getNumOperands();
Gabor Greiff6caff662008-05-10 08:32:32 +0000489 if (N == 1)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000490 Op<0>() = RI.Op<0>();
Devang Patelae682fb2008-02-26 17:56:20 +0000491 else if (N) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000492 Use *OL = OperandList;
Devang Patelae682fb2008-02-26 17:56:20 +0000493 for (unsigned i = 0; i < N; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000494 OL[i] = RI.getOperand(i);
Devang Patelae682fb2008-02-26 17:56:20 +0000495 }
Chris Lattner2195fc42007-02-24 00:55:48 +0000496}
497
498ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000499 : TerminatorInst(Type::VoidTy, Instruction::Ret,
500 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
501 retVal != 0, InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000502 if (retVal)
503 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000504}
505ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000506 : TerminatorInst(Type::VoidTy, Instruction::Ret,
507 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
508 retVal != 0, InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000509 if (retVal)
510 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000511}
512ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000513 : TerminatorInst(Type::VoidTy, Instruction::Ret,
514 OperandTraits<ReturnInst>::op_end(this),
515 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000516}
517
Devang Patela736c002008-02-26 19:38:17 +0000518ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
519 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000520 : TerminatorInst(Type::VoidTy, Instruction::Ret,
521 OperandTraits<ReturnInst>::op_end(this) - N,
522 N, InsertBefore) {
Devang Patela736c002008-02-26 19:38:17 +0000523 if (N != 0)
524 init(retVals, N);
525}
526ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
527 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000528 : TerminatorInst(Type::VoidTy, Instruction::Ret,
529 OperandTraits<ReturnInst>::op_end(this) - N,
530 N, InsertAtEnd) {
Devang Patela736c002008-02-26 19:38:17 +0000531 if (N != 0)
532 init(retVals, N);
533}
534
Devang Patel060e79c2008-02-26 19:15:26 +0000535void ReturnInst::init(Value * const* retVals, unsigned N) {
Devang Patelc38eb522008-02-26 18:49:29 +0000536 assert (N > 0 && "Invalid operands numbers in ReturnInst init");
Devang Patel59643e52008-02-23 00:35:18 +0000537
Devang Patelc38eb522008-02-26 18:49:29 +0000538 NumOperands = N;
Devang Patel59643e52008-02-23 00:35:18 +0000539 if (NumOperands == 1) {
Devang Patel060e79c2008-02-26 19:15:26 +0000540 Value *V = *retVals;
Devang Patel59643e52008-02-23 00:35:18 +0000541 if (V->getType() == Type::VoidTy)
542 return;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000543 Op<0>() = V;
Devang Patelae682fb2008-02-26 17:56:20 +0000544 return;
Devang Patel59643e52008-02-23 00:35:18 +0000545 }
546
Gabor Greiff6caff662008-05-10 08:32:32 +0000547 Use *OL = OperandList;
Devang Patel59643e52008-02-23 00:35:18 +0000548 for (unsigned i = 0; i < NumOperands; ++i) {
Devang Patel060e79c2008-02-26 19:15:26 +0000549 Value *V = *retVals++;
Devang Patel59643e52008-02-23 00:35:18 +0000550 assert(!isa<BasicBlock>(V) &&
551 "Cannot return basic block. Probably using the incorrect ctor");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000552 OL[i] = V;
Devang Patel59643e52008-02-23 00:35:18 +0000553 }
554}
555
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000556unsigned ReturnInst::getNumSuccessorsV() const {
557 return getNumSuccessors();
558}
559
Devang Patelae682fb2008-02-26 17:56:20 +0000560/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
561/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000562void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000563 assert(0 && "ReturnInst has no successors!");
564}
565
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000566BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
567 assert(0 && "ReturnInst has no successors!");
568 abort();
569 return 0;
570}
571
Devang Patel59643e52008-02-23 00:35:18 +0000572ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000573}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000574
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000575//===----------------------------------------------------------------------===//
576// UnwindInst Implementation
577//===----------------------------------------------------------------------===//
578
Chris Lattner2195fc42007-02-24 00:55:48 +0000579UnwindInst::UnwindInst(Instruction *InsertBefore)
580 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
581}
582UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
583 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
584}
585
586
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000587unsigned UnwindInst::getNumSuccessorsV() const {
588 return getNumSuccessors();
589}
590
591void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000592 assert(0 && "UnwindInst has no successors!");
593}
594
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000595BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
596 assert(0 && "UnwindInst has no successors!");
597 abort();
598 return 0;
599}
600
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000601//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000602// UnreachableInst Implementation
603//===----------------------------------------------------------------------===//
604
Chris Lattner2195fc42007-02-24 00:55:48 +0000605UnreachableInst::UnreachableInst(Instruction *InsertBefore)
606 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
607}
608UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
609 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
610}
611
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000612unsigned UnreachableInst::getNumSuccessorsV() const {
613 return getNumSuccessors();
614}
615
616void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
617 assert(0 && "UnwindInst has no successors!");
618}
619
620BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
621 assert(0 && "UnwindInst has no successors!");
622 abort();
623 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000624}
625
626//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000627// BranchInst Implementation
628//===----------------------------------------------------------------------===//
629
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000630void BranchInst::AssertOK() {
631 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000632 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000633 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000634}
635
Chris Lattner2195fc42007-02-24 00:55:48 +0000636BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000637 : TerminatorInst(Type::VoidTy, Instruction::Br,
638 OperandTraits<BranchInst>::op_end(this) - 1,
639 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000640 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000641 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000642}
643BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
644 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000645 : TerminatorInst(Type::VoidTy, Instruction::Br,
646 OperandTraits<BranchInst>::op_end(this) - 3,
647 3, InsertBefore) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000648 Op<0>() = IfTrue;
649 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000650 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000651#ifndef NDEBUG
652 AssertOK();
653#endif
654}
655
656BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000657 : TerminatorInst(Type::VoidTy, Instruction::Br,
658 OperandTraits<BranchInst>::op_end(this) - 1,
659 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000660 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000661 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000662}
663
664BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
665 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000666 : TerminatorInst(Type::VoidTy, Instruction::Br,
667 OperandTraits<BranchInst>::op_end(this) - 3,
668 3, InsertAtEnd) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000669 Op<0>() = IfTrue;
670 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000671 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000672#ifndef NDEBUG
673 AssertOK();
674#endif
675}
676
677
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000678BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000679 TerminatorInst(Type::VoidTy, Instruction::Br,
680 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
681 BI.getNumOperands()) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000682 OperandList[0] = BI.getOperand(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000683 if (BI.getNumOperands() != 1) {
684 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000685 OperandList[1] = BI.getOperand(1);
686 OperandList[2] = BI.getOperand(2);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000687 }
688}
689
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000690BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
691 return getSuccessor(idx);
692}
693unsigned BranchInst::getNumSuccessorsV() const {
694 return getNumSuccessors();
695}
696void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
697 setSuccessor(idx, B);
698}
699
700
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000701//===----------------------------------------------------------------------===//
702// AllocationInst Implementation
703//===----------------------------------------------------------------------===//
704
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000705static Value *getAISize(Value *Amt) {
706 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000707 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000708 else {
709 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000710 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000711 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000712 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000713 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000714 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000715}
716
Misha Brukmanb1c93172005-04-21 23:48:37 +0000717AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000718 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000719 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000720 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000721 InsertBefore) {
722 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000723 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000724 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725}
726
Misha Brukmanb1c93172005-04-21 23:48:37 +0000727AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000728 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000729 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000730 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000731 InsertAtEnd) {
732 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000733 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000734 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000735}
736
Gordon Henriksen14a55692007-12-10 02:14:30 +0000737// Out of line virtual method, so the vtable, etc has a home.
738AllocationInst::~AllocationInst() {
739}
740
Dan Gohmanaa583d72008-03-24 16:55:58 +0000741void AllocationInst::setAlignment(unsigned Align) {
742 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
743 SubclassData = Log2_32(Align) + 1;
744 assert(getAlignment() == Align && "Alignment representation error!");
745}
746
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000747bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000748 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
749 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000750 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000751}
752
753const Type *AllocationInst::getAllocatedType() const {
754 return getType()->getElementType();
755}
756
757AllocaInst::AllocaInst(const AllocaInst &AI)
758 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000759 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000760}
761
762MallocInst::MallocInst(const MallocInst &MI)
763 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000764 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000765}
766
767//===----------------------------------------------------------------------===//
768// FreeInst Implementation
769//===----------------------------------------------------------------------===//
770
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000771void FreeInst::AssertOK() {
772 assert(isa<PointerType>(getOperand(0)->getType()) &&
773 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000774}
775
776FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000777 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000778 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000779}
780
781FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000782 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000783 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000784}
785
786
787//===----------------------------------------------------------------------===//
788// LoadInst Implementation
789//===----------------------------------------------------------------------===//
790
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000791void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000792 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000793 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000794}
795
796LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000797 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000798 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000799 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000800 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000801 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000802 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803}
804
805LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000806 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000807 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000808 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000809 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000810 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000811 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000812}
813
814LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
815 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000816 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000817 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000818 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000819 setAlignment(0);
820 AssertOK();
821 setName(Name);
822}
823
824LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
825 unsigned Align, Instruction *InsertBef)
826 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
827 Load, Ptr, InsertBef) {
828 setVolatile(isVolatile);
829 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000830 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000831 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000832}
833
Dan Gohman68659282007-07-18 20:51:11 +0000834LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
835 unsigned Align, BasicBlock *InsertAE)
836 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
837 Load, Ptr, InsertAE) {
838 setVolatile(isVolatile);
839 setAlignment(Align);
840 AssertOK();
841 setName(Name);
842}
843
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000844LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
845 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000846 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000847 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000848 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000849 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000850 AssertOK();
851 setName(Name);
852}
853
854
855
856LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000857 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
858 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000859 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000860 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000861 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000862 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000863}
864
865LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000866 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
867 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000868 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000869 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000870 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000871 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000872}
873
874LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
875 Instruction *InsertBef)
876: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000877 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000878 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000879 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000880 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000881 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000882}
883
884LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
885 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000886 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
887 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000888 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000889 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000890 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000891 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000892}
893
Christopher Lamb84485702007-04-22 19:24:39 +0000894void LoadInst::setAlignment(unsigned Align) {
895 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
896 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
897}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000898
899//===----------------------------------------------------------------------===//
900// StoreInst Implementation
901//===----------------------------------------------------------------------===//
902
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000903void StoreInst::AssertOK() {
904 assert(isa<PointerType>(getOperand(1)->getType()) &&
905 "Ptr must have pointer type!");
906 assert(getOperand(0)->getType() ==
907 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000908 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000909}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000910
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000911
912StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000913 : Instruction(Type::VoidTy, Store,
914 OperandTraits<StoreInst>::op_begin(this),
915 OperandTraits<StoreInst>::operands(this),
916 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000917 Op<0>() = val;
918 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000919 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000920 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000921 AssertOK();
922}
923
924StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000925 : Instruction(Type::VoidTy, Store,
926 OperandTraits<StoreInst>::op_begin(this),
927 OperandTraits<StoreInst>::operands(this),
928 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000929 Op<0>() = val;
930 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000931 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000932 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000933 AssertOK();
934}
935
Misha Brukmanb1c93172005-04-21 23:48:37 +0000936StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000937 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000938 : Instruction(Type::VoidTy, Store,
939 OperandTraits<StoreInst>::op_begin(this),
940 OperandTraits<StoreInst>::operands(this),
941 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000942 Op<0>() = val;
943 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000944 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000945 setAlignment(0);
946 AssertOK();
947}
948
949StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
950 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000951 : Instruction(Type::VoidTy, Store,
952 OperandTraits<StoreInst>::op_begin(this),
953 OperandTraits<StoreInst>::operands(this),
954 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000955 Op<0>() = val;
956 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000957 setVolatile(isVolatile);
958 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000959 AssertOK();
960}
961
Misha Brukmanb1c93172005-04-21 23:48:37 +0000962StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000963 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000964 : Instruction(Type::VoidTy, Store,
965 OperandTraits<StoreInst>::op_begin(this),
966 OperandTraits<StoreInst>::operands(this),
967 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000968 Op<0>() = val;
969 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000970 setVolatile(isVolatile);
971 setAlignment(Align);
972 AssertOK();
973}
974
975StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000976 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000977 : Instruction(Type::VoidTy, Store,
978 OperandTraits<StoreInst>::op_begin(this),
979 OperandTraits<StoreInst>::operands(this),
980 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000981 Op<0>() = val;
982 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000983 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000984 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000985 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000986}
987
Christopher Lamb84485702007-04-22 19:24:39 +0000988void StoreInst::setAlignment(unsigned Align) {
989 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
990 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
991}
992
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000993//===----------------------------------------------------------------------===//
994// GetElementPtrInst Implementation
995//===----------------------------------------------------------------------===//
996
Christopher Lambedf07882007-12-17 01:12:55 +0000997static unsigned retrieveAddrSpace(const Value *Val) {
998 return cast<PointerType>(Val->getType())->getAddressSpace();
999}
1000
Gabor Greif21ba1842008-06-06 20:28:12 +00001001void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
1002 const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001003 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1004 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001005 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001006
Chris Lattner79807c3d2007-01-31 19:47:18 +00001007 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001008 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001009
1010 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001011}
1012
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001013void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001014 assert(NumOperands == 2 && "NumOperands not initialized?");
1015 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001016 OL[0] = Ptr;
1017 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001018
1019 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001020}
1021
Gabor Greiff6caff662008-05-10 08:32:32 +00001022GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001023 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001024 OperandTraits<GetElementPtrInst>::op_end(this)
1025 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001026 GEPI.getNumOperands()) {
1027 Use *OL = OperandList;
1028 Use *GEPIOL = GEPI.OperandList;
1029 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001030 OL[i] = GEPIOL[i];
Gabor Greiff6caff662008-05-10 08:32:32 +00001031}
1032
Chris Lattner82981202005-05-03 05:43:30 +00001033GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1034 const std::string &Name, Instruction *InBe)
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, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001040 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001041}
1042
1043GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1044 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001045 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001046 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001047 GetElementPtr,
1048 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1049 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001050 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001051}
1052
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001053// getIndexedType - Returns the type of the element that would be loaded with
1054// a load instruction with the specified parameters.
1055//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001056// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001057// pointer type.
1058//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001059const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001060 Value* const *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001061 unsigned NumIdx) {
1062 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1063 if (!PTy) return 0; // Type isn't a pointer type!
1064 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001065
1066 // Handle the special case of the empty set index set...
Dan Gohman12fce772008-05-15 19:50:34 +00001067 if (NumIdx == 0)
1068 return Agg;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001069
Dan Gohman1ecaf452008-05-31 00:58:22 +00001070 unsigned CurIdx = 1;
1071 for (; CurIdx != NumIdx; ++CurIdx) {
1072 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1073 if (!CT || isa<PointerType>(CT)) return 0;
1074 Value *Index = Idxs[CurIdx];
1075 if (!CT->indexValid(Index)) return 0;
1076 Agg = CT->getTypeAtIndex(Index);
1077
1078 // If the new type forwards to another type, then it is in the middle
1079 // of being refined to another type (and hence, may have dropped all
1080 // references to what it was using before). So, use the new forwarded
1081 // type.
1082 if (const Type *Ty = Agg->getForwardedType())
1083 Agg = Ty;
1084 }
1085 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001086}
1087
Chris Lattner82981202005-05-03 05:43:30 +00001088const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1089 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1090 if (!PTy) return 0; // Type isn't a pointer type!
1091
1092 // Check the pointer index.
1093 if (!PTy->indexValid(Idx)) return 0;
1094
Chris Lattnerc2233332005-05-03 16:44:45 +00001095 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001096}
1097
Chris Lattner45f15572007-04-14 00:12:57 +00001098
1099/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1100/// zeros. If so, the result pointer and the first operand have the same
1101/// value, just potentially different types.
1102bool GetElementPtrInst::hasAllZeroIndices() const {
1103 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1104 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1105 if (!CI->isZero()) return false;
1106 } else {
1107 return false;
1108 }
1109 }
1110 return true;
1111}
1112
Chris Lattner27058292007-04-27 20:35:56 +00001113/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1114/// constant integers. If so, the result pointer and the first operand have
1115/// a constant offset between them.
1116bool GetElementPtrInst::hasAllConstantIndices() const {
1117 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1118 if (!isa<ConstantInt>(getOperand(i)))
1119 return false;
1120 }
1121 return true;
1122}
1123
Chris Lattner45f15572007-04-14 00:12:57 +00001124
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001125//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001126// ExtractElementInst Implementation
1127//===----------------------------------------------------------------------===//
1128
1129ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001130 const std::string &Name,
1131 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001132 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001133 ExtractElement,
1134 OperandTraits<ExtractElementInst>::op_begin(this),
1135 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001136 assert(isValidOperands(Val, Index) &&
1137 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001138 Op<0>() = Val;
1139 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001140 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001141}
1142
Chris Lattner65511ff2006-10-05 06:24:58 +00001143ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1144 const std::string &Name,
1145 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001146 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001147 ExtractElement,
1148 OperandTraits<ExtractElementInst>::op_begin(this),
1149 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001150 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001151 assert(isValidOperands(Val, Index) &&
1152 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001153 Op<0>() = Val;
1154 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001155 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001156}
1157
1158
Robert Bocchino23004482006-01-10 19:05:34 +00001159ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001160 const std::string &Name,
1161 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001162 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001163 ExtractElement,
1164 OperandTraits<ExtractElementInst>::op_begin(this),
1165 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001166 assert(isValidOperands(Val, Index) &&
1167 "Invalid extractelement instruction operands!");
1168
Gabor Greif2d3024d2008-05-26 21:33:52 +00001169 Op<0>() = Val;
1170 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001171 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001172}
1173
Chris Lattner65511ff2006-10-05 06:24:58 +00001174ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1175 const std::string &Name,
1176 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001177 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001178 ExtractElement,
1179 OperandTraits<ExtractElementInst>::op_begin(this),
1180 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001181 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001182 assert(isValidOperands(Val, Index) &&
1183 "Invalid extractelement instruction operands!");
1184
Gabor Greif2d3024d2008-05-26 21:33:52 +00001185 Op<0>() = Val;
1186 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001187 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001188}
1189
1190
Chris Lattner54865b32006-04-08 04:05:48 +00001191bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001192 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001193 return false;
1194 return true;
1195}
1196
1197
Robert Bocchino23004482006-01-10 19:05:34 +00001198//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001199// InsertElementInst Implementation
1200//===----------------------------------------------------------------------===//
1201
Chris Lattner0875d942006-04-14 22:20:32 +00001202InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001203 : Instruction(IE.getType(), InsertElement,
1204 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001205 Op<0>() = IE.Op<0>();
1206 Op<1>() = IE.Op<1>();
1207 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001208}
Chris Lattner54865b32006-04-08 04:05:48 +00001209InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001210 const std::string &Name,
1211 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001212 : Instruction(Vec->getType(), InsertElement,
1213 OperandTraits<InsertElementInst>::op_begin(this),
1214 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001215 assert(isValidOperands(Vec, Elt, Index) &&
1216 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001217 Op<0>() = Vec;
1218 Op<1>() = Elt;
1219 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001220 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001221}
1222
Chris Lattner65511ff2006-10-05 06:24:58 +00001223InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1224 const std::string &Name,
1225 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001226 : Instruction(Vec->getType(), InsertElement,
1227 OperandTraits<InsertElementInst>::op_begin(this),
1228 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001229 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001230 assert(isValidOperands(Vec, Elt, Index) &&
1231 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001232 Op<0>() = Vec;
1233 Op<1>() = Elt;
1234 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001235 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001236}
1237
1238
Chris Lattner54865b32006-04-08 04:05:48 +00001239InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001240 const std::string &Name,
1241 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001242 : Instruction(Vec->getType(), InsertElement,
1243 OperandTraits<InsertElementInst>::op_begin(this),
1244 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001245 assert(isValidOperands(Vec, Elt, Index) &&
1246 "Invalid insertelement instruction operands!");
1247
Gabor Greif2d3024d2008-05-26 21:33:52 +00001248 Op<0>() = Vec;
1249 Op<1>() = Elt;
1250 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001251 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001252}
1253
Chris Lattner65511ff2006-10-05 06:24:58 +00001254InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1255 const std::string &Name,
1256 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001257: Instruction(Vec->getType(), InsertElement,
1258 OperandTraits<InsertElementInst>::op_begin(this),
1259 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001260 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001261 assert(isValidOperands(Vec, Elt, Index) &&
1262 "Invalid insertelement instruction operands!");
1263
Gabor Greif2d3024d2008-05-26 21:33:52 +00001264 Op<0>() = Vec;
1265 Op<1>() = Elt;
1266 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001267 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001268}
1269
Chris Lattner54865b32006-04-08 04:05:48 +00001270bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1271 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001272 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001273 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001274
Reid Spencerd84d35b2007-02-15 02:26:10 +00001275 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001276 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001277
Reid Spencer8d9336d2006-12-31 05:26:44 +00001278 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001279 return false; // Third operand of insertelement must be uint.
1280 return true;
1281}
1282
1283
Robert Bocchinoca27f032006-01-17 20:07:22 +00001284//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001285// ShuffleVectorInst Implementation
1286//===----------------------------------------------------------------------===//
1287
Chris Lattner0875d942006-04-14 22:20:32 +00001288ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001289 : Instruction(SV.getType(), ShuffleVector,
1290 OperandTraits<ShuffleVectorInst>::op_begin(this),
1291 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001292 Op<0>() = SV.Op<0>();
1293 Op<1>() = SV.Op<1>();
1294 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001295}
1296
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001297ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1298 const std::string &Name,
1299 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001300 : Instruction(V1->getType(), ShuffleVector,
1301 OperandTraits<ShuffleVectorInst>::op_begin(this),
1302 OperandTraits<ShuffleVectorInst>::operands(this),
1303 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001304 assert(isValidOperands(V1, V2, Mask) &&
1305 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001306 Op<0>() = V1;
1307 Op<1>() = V2;
1308 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001309 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001310}
1311
1312ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1313 const std::string &Name,
1314 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001315 : Instruction(V1->getType(), ShuffleVector,
1316 OperandTraits<ShuffleVectorInst>::op_begin(this),
1317 OperandTraits<ShuffleVectorInst>::operands(this),
1318 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001319 assert(isValidOperands(V1, V2, Mask) &&
1320 "Invalid shuffle vector instruction operands!");
1321
Gabor Greif2d3024d2008-05-26 21:33:52 +00001322 Op<0>() = V1;
1323 Op<1>() = V2;
1324 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001325 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001326}
1327
1328bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1329 const Value *Mask) {
Chris Lattnerf724e342008-03-02 05:28:33 +00001330 if (!isa<VectorType>(V1->getType()) ||
1331 V1->getType() != V2->getType())
1332 return false;
1333
1334 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1335 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1336 MaskTy->getElementType() != Type::Int32Ty ||
1337 MaskTy->getNumElements() !=
1338 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001339 return false;
1340 return true;
1341}
1342
Chris Lattnerf724e342008-03-02 05:28:33 +00001343/// getMaskValue - Return the index from the shuffle mask for the specified
1344/// output result. This is either -1 if the element is undef or a number less
1345/// than 2*numelements.
1346int ShuffleVectorInst::getMaskValue(unsigned i) const {
1347 const Constant *Mask = cast<Constant>(getOperand(2));
1348 if (isa<UndefValue>(Mask)) return -1;
1349 if (isa<ConstantAggregateZero>(Mask)) return 0;
1350 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1351 assert(i < MaskCV->getNumOperands() && "Index out of range");
1352
1353 if (isa<UndefValue>(MaskCV->getOperand(i)))
1354 return -1;
1355 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1356}
1357
Dan Gohman12fce772008-05-15 19:50:34 +00001358//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001359// InsertValueInst Class
1360//===----------------------------------------------------------------------===//
1361
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001362void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1363 unsigned NumIdx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001364 assert(NumOperands == 2 && "NumOperands not initialized?");
1365 Op<0>() = Agg;
1366 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001367
Dan Gohman1ecaf452008-05-31 00:58:22 +00001368 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001369 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001370}
1371
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001372void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1373 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001374 assert(NumOperands == 2 && "NumOperands not initialized?");
1375 Op<0>() = Agg;
1376 Op<1>() = Val;
1377
1378 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001379 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001380}
1381
1382InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001383 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001384 OperandTraits<InsertValueInst>::op_begin(this), 2),
1385 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001386 Op<0>() = IVI.getOperand(0);
1387 Op<1>() = IVI.getOperand(1);
Dan Gohman0752bff2008-05-23 00:36:11 +00001388}
1389
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001390InsertValueInst::InsertValueInst(Value *Agg,
1391 Value *Val,
1392 unsigned Idx,
1393 const std::string &Name,
1394 Instruction *InsertBefore)
1395 : Instruction(Agg->getType(), InsertValue,
1396 OperandTraits<InsertValueInst>::op_begin(this),
1397 2, InsertBefore) {
1398 init(Agg, Val, Idx, Name);
1399}
1400
1401InsertValueInst::InsertValueInst(Value *Agg,
1402 Value *Val,
1403 unsigned Idx,
1404 const std::string &Name,
1405 BasicBlock *InsertAtEnd)
1406 : Instruction(Agg->getType(), InsertValue,
1407 OperandTraits<InsertValueInst>::op_begin(this),
1408 2, InsertAtEnd) {
1409 init(Agg, Val, Idx, Name);
1410}
1411
Dan Gohman0752bff2008-05-23 00:36:11 +00001412//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001413// ExtractValueInst Class
1414//===----------------------------------------------------------------------===//
1415
Gabor Greifcbcc4952008-06-06 21:06:32 +00001416void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Gabor Greif21ba1842008-06-06 20:28:12 +00001417 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001418 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001419
Dan Gohman1ecaf452008-05-31 00:58:22 +00001420 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001421 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001422}
1423
Gabor Greifcbcc4952008-06-06 21:06:32 +00001424void ExtractValueInst::init(unsigned Idx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001425 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001426
1427 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001428 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001429}
1430
1431ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001432 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001433 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001434}
1435
Dan Gohman12fce772008-05-15 19:50:34 +00001436// getIndexedType - Returns the type of the element that would be extracted
1437// with an extractvalue instruction with the specified parameters.
1438//
1439// A null type is returned if the indices are invalid for the specified
1440// pointer type.
1441//
1442const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001443 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001444 unsigned NumIdx) {
1445 unsigned CurIdx = 0;
1446 for (; CurIdx != NumIdx; ++CurIdx) {
1447 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001448 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1449 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001450 if (!CT->indexValid(Index)) return 0;
1451 Agg = CT->getTypeAtIndex(Index);
1452
1453 // If the new type forwards to another type, then it is in the middle
1454 // of being refined to another type (and hence, may have dropped all
1455 // references to what it was using before). So, use the new forwarded
1456 // type.
1457 if (const Type *Ty = Agg->getForwardedType())
1458 Agg = Ty;
1459 }
1460 return CurIdx == NumIdx ? Agg : 0;
1461}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001462
Dan Gohman4a3125b2008-06-17 21:07:55 +00001463const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001464 unsigned Idx) {
1465 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001466}
1467
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001468//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001469// BinaryOperator Class
1470//===----------------------------------------------------------------------===//
1471
Chris Lattner2195fc42007-02-24 00:55:48 +00001472BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1473 const Type *Ty, const std::string &Name,
1474 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001475 : Instruction(Ty, iType,
1476 OperandTraits<BinaryOperator>::op_begin(this),
1477 OperandTraits<BinaryOperator>::operands(this),
1478 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001479 Op<0>() = S1;
1480 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001481 init(iType);
1482 setName(Name);
1483}
1484
1485BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1486 const Type *Ty, const std::string &Name,
1487 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001488 : Instruction(Ty, iType,
1489 OperandTraits<BinaryOperator>::op_begin(this),
1490 OperandTraits<BinaryOperator>::operands(this),
1491 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001492 Op<0>() = S1;
1493 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001494 init(iType);
1495 setName(Name);
1496}
1497
1498
1499void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001500 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001501 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001502 assert(LHS->getType() == RHS->getType() &&
1503 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001504#ifndef NDEBUG
1505 switch (iType) {
1506 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001507 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001508 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001509 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001510 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001511 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001512 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001513 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001514 case UDiv:
1515 case SDiv:
1516 assert(getType() == LHS->getType() &&
1517 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001518 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1519 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001520 "Incorrect operand type (not integer) for S/UDIV");
1521 break;
1522 case FDiv:
1523 assert(getType() == LHS->getType() &&
1524 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001525 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1526 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001527 && "Incorrect operand type (not floating point) for FDIV");
1528 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001529 case URem:
1530 case SRem:
1531 assert(getType() == LHS->getType() &&
1532 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001533 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1534 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001535 "Incorrect operand type (not integer) for S/UREM");
1536 break;
1537 case FRem:
1538 assert(getType() == LHS->getType() &&
1539 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001540 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1541 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001542 && "Incorrect operand type (not floating point) for FREM");
1543 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001544 case Shl:
1545 case LShr:
1546 case AShr:
1547 assert(getType() == LHS->getType() &&
1548 "Shift operation should return same type as operands!");
1549 assert(getType()->isInteger() &&
1550 "Shift operation requires integer operands");
1551 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001552 case And: case Or:
1553 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001554 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001555 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001556 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001557 (isa<VectorType>(getType()) &&
1558 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001559 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001560 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001561 default:
1562 break;
1563 }
1564#endif
1565}
1566
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001567BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001568 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001569 Instruction *InsertBefore) {
1570 assert(S1->getType() == S2->getType() &&
1571 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001572 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001573}
1574
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001575BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001576 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001577 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001578 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001579 InsertAtEnd->getInstList().push_back(Res);
1580 return Res;
1581}
1582
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001583BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001584 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001585 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1586 return new BinaryOperator(Instruction::Sub,
1587 zero, Op,
1588 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001589}
1590
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001591BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001592 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001593 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1594 return new BinaryOperator(Instruction::Sub,
1595 zero, Op,
1596 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001597}
1598
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001599BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001600 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001601 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001602 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001603 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001604 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001605 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001606 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001607 }
1608
1609 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001610 Op->getType(), Name, InsertBefore);
1611}
1612
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001613BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001614 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001615 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001616 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001617 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001618 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001619 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001620 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001621 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001622 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001623 }
1624
1625 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001626 Op->getType(), Name, InsertAtEnd);
1627}
1628
1629
1630// isConstantAllOnes - Helper function for several functions below
1631static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001632 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1633 return CI->isAllOnesValue();
1634 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1635 return CV->isAllOnesValue();
1636 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001637}
1638
1639bool BinaryOperator::isNeg(const Value *V) {
1640 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1641 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001642 return Bop->getOperand(0) ==
1643 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001644 return false;
1645}
1646
1647bool BinaryOperator::isNot(const Value *V) {
1648 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1649 return (Bop->getOpcode() == Instruction::Xor &&
1650 (isConstantAllOnes(Bop->getOperand(1)) ||
1651 isConstantAllOnes(Bop->getOperand(0))));
1652 return false;
1653}
1654
Chris Lattner2c7d1772005-04-24 07:28:37 +00001655Value *BinaryOperator::getNegArgument(Value *BinOp) {
1656 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1657 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001658}
1659
Chris Lattner2c7d1772005-04-24 07:28:37 +00001660const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1661 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001662}
1663
Chris Lattner2c7d1772005-04-24 07:28:37 +00001664Value *BinaryOperator::getNotArgument(Value *BinOp) {
1665 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1666 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1667 Value *Op0 = BO->getOperand(0);
1668 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001669 if (isConstantAllOnes(Op0)) return Op1;
1670
1671 assert(isConstantAllOnes(Op1));
1672 return Op0;
1673}
1674
Chris Lattner2c7d1772005-04-24 07:28:37 +00001675const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1676 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001677}
1678
1679
1680// swapOperands - Exchange the two operands to this instruction. This
1681// instruction is safe to use on any binary instruction and does not
1682// modify the semantics of the instruction. If the instruction is
1683// order dependent (SetLT f.e.) the opcode is changed.
1684//
1685bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001686 if (!isCommutative())
1687 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001688 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001689 return false;
1690}
1691
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001692//===----------------------------------------------------------------------===//
1693// CastInst Class
1694//===----------------------------------------------------------------------===//
1695
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001696// Just determine if this cast only deals with integral->integral conversion.
1697bool CastInst::isIntegerCast() const {
1698 switch (getOpcode()) {
1699 default: return false;
1700 case Instruction::ZExt:
1701 case Instruction::SExt:
1702 case Instruction::Trunc:
1703 return true;
1704 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001705 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001706 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001707}
1708
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001709bool CastInst::isLosslessCast() const {
1710 // Only BitCast can be lossless, exit fast if we're not BitCast
1711 if (getOpcode() != Instruction::BitCast)
1712 return false;
1713
1714 // Identity cast is always lossless
1715 const Type* SrcTy = getOperand(0)->getType();
1716 const Type* DstTy = getType();
1717 if (SrcTy == DstTy)
1718 return true;
1719
Reid Spencer8d9336d2006-12-31 05:26:44 +00001720 // Pointer to pointer is always lossless.
1721 if (isa<PointerType>(SrcTy))
1722 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001723 return false; // Other types have no identity values
1724}
1725
1726/// This function determines if the CastInst does not require any bits to be
1727/// changed in order to effect the cast. Essentially, it identifies cases where
1728/// no code gen is necessary for the cast, hence the name no-op cast. For
1729/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001730/// # bitcast i32* %x to i8*
1731/// # bitcast <2 x i32> %x to <4 x i16>
1732/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001733/// @brief Determine if a cast is a no-op.
1734bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1735 switch (getOpcode()) {
1736 default:
1737 assert(!"Invalid CastOp");
1738 case Instruction::Trunc:
1739 case Instruction::ZExt:
1740 case Instruction::SExt:
1741 case Instruction::FPTrunc:
1742 case Instruction::FPExt:
1743 case Instruction::UIToFP:
1744 case Instruction::SIToFP:
1745 case Instruction::FPToUI:
1746 case Instruction::FPToSI:
1747 return false; // These always modify bits
1748 case Instruction::BitCast:
1749 return true; // BitCast never modifies bits.
1750 case Instruction::PtrToInt:
1751 return IntPtrTy->getPrimitiveSizeInBits() ==
1752 getType()->getPrimitiveSizeInBits();
1753 case Instruction::IntToPtr:
1754 return IntPtrTy->getPrimitiveSizeInBits() ==
1755 getOperand(0)->getType()->getPrimitiveSizeInBits();
1756 }
1757}
1758
1759/// This function determines if a pair of casts can be eliminated and what
1760/// opcode should be used in the elimination. This assumes that there are two
1761/// instructions like this:
1762/// * %F = firstOpcode SrcTy %x to MidTy
1763/// * %S = secondOpcode MidTy %F to DstTy
1764/// The function returns a resultOpcode so these two casts can be replaced with:
1765/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1766/// If no such cast is permited, the function returns 0.
1767unsigned CastInst::isEliminableCastPair(
1768 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1769 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1770{
1771 // Define the 144 possibilities for these two cast instructions. The values
1772 // in this matrix determine what to do in a given situation and select the
1773 // case in the switch below. The rows correspond to firstOp, the columns
1774 // correspond to secondOp. In looking at the table below, keep in mind
1775 // the following cast properties:
1776 //
1777 // Size Compare Source Destination
1778 // Operator Src ? Size Type Sign Type Sign
1779 // -------- ------------ ------------------- ---------------------
1780 // TRUNC > Integer Any Integral Any
1781 // ZEXT < Integral Unsigned Integer Any
1782 // SEXT < Integral Signed Integer Any
1783 // FPTOUI n/a FloatPt n/a Integral Unsigned
1784 // FPTOSI n/a FloatPt n/a Integral Signed
1785 // UITOFP n/a Integral Unsigned FloatPt n/a
1786 // SITOFP n/a Integral Signed FloatPt n/a
1787 // FPTRUNC > FloatPt n/a FloatPt n/a
1788 // FPEXT < FloatPt n/a FloatPt n/a
1789 // PTRTOINT n/a Pointer n/a Integral Unsigned
1790 // INTTOPTR n/a Integral Unsigned Pointer n/a
1791 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001792 //
1793 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1794 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1795 // into "fptoui double to ulong", but this loses information about the range
1796 // of the produced value (we no longer know the top-part is all zeros).
1797 // Further this conversion is often much more expensive for typical hardware,
1798 // and causes issues when building libgcc. We disallow fptosi+sext for the
1799 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001800 const unsigned numCastOps =
1801 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1802 static const uint8_t CastResults[numCastOps][numCastOps] = {
1803 // T F F U S F F P I B -+
1804 // R Z S P P I I T P 2 N T |
1805 // U E E 2 2 2 2 R E I T C +- secondOp
1806 // N X X U S F F N X N 2 V |
1807 // C T T I I P P C T T P T -+
1808 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1809 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1810 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001811 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1812 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001813 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1814 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1815 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1816 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1817 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1818 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1819 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1820 };
1821
1822 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1823 [secondOp-Instruction::CastOpsBegin];
1824 switch (ElimCase) {
1825 case 0:
1826 // categorically disallowed
1827 return 0;
1828 case 1:
1829 // allowed, use first cast's opcode
1830 return firstOp;
1831 case 2:
1832 // allowed, use second cast's opcode
1833 return secondOp;
1834 case 3:
1835 // no-op cast in second op implies firstOp as long as the DestTy
1836 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001837 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001838 return firstOp;
1839 return 0;
1840 case 4:
1841 // no-op cast in second op implies firstOp as long as the DestTy
1842 // is floating point
1843 if (DstTy->isFloatingPoint())
1844 return firstOp;
1845 return 0;
1846 case 5:
1847 // no-op cast in first op implies secondOp as long as the SrcTy
1848 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001849 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001850 return secondOp;
1851 return 0;
1852 case 6:
1853 // no-op cast in first op implies secondOp as long as the SrcTy
1854 // is a floating point
1855 if (SrcTy->isFloatingPoint())
1856 return secondOp;
1857 return 0;
1858 case 7: {
1859 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1860 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1861 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1862 if (MidSize >= PtrSize)
1863 return Instruction::BitCast;
1864 return 0;
1865 }
1866 case 8: {
1867 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1868 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1869 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1870 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1871 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1872 if (SrcSize == DstSize)
1873 return Instruction::BitCast;
1874 else if (SrcSize < DstSize)
1875 return firstOp;
1876 return secondOp;
1877 }
1878 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1879 return Instruction::ZExt;
1880 case 10:
1881 // fpext followed by ftrunc is allowed if the bit size returned to is
1882 // the same as the original, in which case its just a bitcast
1883 if (SrcTy == DstTy)
1884 return Instruction::BitCast;
1885 return 0; // If the types are not the same we can't eliminate it.
1886 case 11:
1887 // bitcast followed by ptrtoint is allowed as long as the bitcast
1888 // is a pointer to pointer cast.
1889 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1890 return secondOp;
1891 return 0;
1892 case 12:
1893 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1894 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1895 return firstOp;
1896 return 0;
1897 case 13: {
1898 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1899 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1900 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1901 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1902 if (SrcSize <= PtrSize && SrcSize == DstSize)
1903 return Instruction::BitCast;
1904 return 0;
1905 }
1906 case 99:
1907 // cast combination can't happen (error in input). This is for all cases
1908 // where the MidTy is not the same for the two cast instructions.
1909 assert(!"Invalid Cast Combination");
1910 return 0;
1911 default:
1912 assert(!"Error in CastResults table!!!");
1913 return 0;
1914 }
1915 return 0;
1916}
1917
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001918CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001919 const std::string &Name, Instruction *InsertBefore) {
1920 // Construct and return the appropriate CastInst subclass
1921 switch (op) {
1922 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1923 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1924 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1925 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1926 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1927 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1928 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1929 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1930 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1931 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1932 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1933 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1934 default:
1935 assert(!"Invalid opcode provided");
1936 }
1937 return 0;
1938}
1939
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001940CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001941 const std::string &Name, BasicBlock *InsertAtEnd) {
1942 // Construct and return the appropriate CastInst subclass
1943 switch (op) {
1944 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1945 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1946 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1947 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1948 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1949 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1950 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1951 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1952 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1953 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1954 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1955 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1956 default:
1957 assert(!"Invalid opcode provided");
1958 }
1959 return 0;
1960}
1961
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001962CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001963 const std::string &Name,
1964 Instruction *InsertBefore) {
1965 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001966 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1967 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001968}
1969
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001970CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001971 const std::string &Name,
1972 BasicBlock *InsertAtEnd) {
1973 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001974 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1975 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001976}
1977
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001978CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001979 const std::string &Name,
1980 Instruction *InsertBefore) {
1981 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001982 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1983 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001984}
1985
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001986CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001987 const std::string &Name,
1988 BasicBlock *InsertAtEnd) {
1989 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001990 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1991 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001992}
1993
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001994CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001995 const std::string &Name,
1996 Instruction *InsertBefore) {
1997 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001998 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1999 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002000}
2001
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002002CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002003 const std::string &Name,
2004 BasicBlock *InsertAtEnd) {
2005 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002006 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2007 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002008}
2009
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002010CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002011 const std::string &Name,
2012 BasicBlock *InsertAtEnd) {
2013 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002014 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002015 "Invalid cast");
2016
Chris Lattner03c49532007-01-15 02:27:26 +00002017 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002018 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2019 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002020}
2021
2022/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002023CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002024 const std::string &Name,
2025 Instruction *InsertBefore) {
2026 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002027 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002028 "Invalid cast");
2029
Chris Lattner03c49532007-01-15 02:27:26 +00002030 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002031 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2032 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002033}
2034
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002035CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002036 bool isSigned, const std::string &Name,
2037 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002038 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002039 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2040 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2041 Instruction::CastOps opcode =
2042 (SrcBits == DstBits ? Instruction::BitCast :
2043 (SrcBits > DstBits ? Instruction::Trunc :
2044 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002045 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002046}
2047
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002048CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002049 bool isSigned, const std::string &Name,
2050 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00002051 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002052 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2053 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2054 Instruction::CastOps opcode =
2055 (SrcBits == DstBits ? Instruction::BitCast :
2056 (SrcBits > DstBits ? Instruction::Trunc :
2057 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002058 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002059}
2060
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002061CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002062 const std::string &Name,
2063 Instruction *InsertBefore) {
2064 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2065 "Invalid cast");
2066 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2067 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2068 Instruction::CastOps opcode =
2069 (SrcBits == DstBits ? Instruction::BitCast :
2070 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002071 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002072}
2073
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002074CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002075 const std::string &Name,
2076 BasicBlock *InsertAtEnd) {
2077 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2078 "Invalid cast");
2079 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2080 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2081 Instruction::CastOps opcode =
2082 (SrcBits == DstBits ? Instruction::BitCast :
2083 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002084 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002085}
2086
Duncan Sands55e50902008-01-06 10:12:28 +00002087// Check whether it is valid to call getCastOpcode for these types.
2088// This routine must be kept in sync with getCastOpcode.
2089bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2090 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2091 return false;
2092
2093 if (SrcTy == DestTy)
2094 return true;
2095
2096 // Get the bit sizes, we'll need these
2097 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2098 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2099
2100 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002101 if (DestTy->isInteger()) { // Casting to integral
2102 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002103 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002104 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002105 return true;
2106 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002107 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002108 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002109 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002110 return isa<PointerType>(SrcTy);
2111 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002112 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2113 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002114 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002115 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002116 return true;
2117 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002118 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002119 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002120 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002121 return false;
2122 }
2123 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002124 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002125 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002126 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002127 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002128 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002129 return DestPTy->getBitWidth() == SrcBits;
2130 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002131 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2132 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002133 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002134 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002135 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002136 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002137 return false;
2138 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002139 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002140 return false;
2141 }
2142}
2143
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002144// Provide a way to get a "cast" where the cast opcode is inferred from the
2145// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002146// logic in the castIsValid function below. This axiom should hold:
2147// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2148// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002149// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002150// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002151Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002152CastInst::getCastOpcode(
2153 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002154 // Get the bit sizes, we'll need these
2155 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002156 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2157 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002158
Duncan Sands55e50902008-01-06 10:12:28 +00002159 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2160 "Only first class types are castable!");
2161
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002162 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002163 if (DestTy->isInteger()) { // Casting to integral
2164 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002165 if (DestBits < SrcBits)
2166 return Trunc; // int -> smaller int
2167 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002168 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002169 return SExt; // signed -> SEXT
2170 else
2171 return ZExt; // unsigned -> ZEXT
2172 } else {
2173 return BitCast; // Same size, No-op cast
2174 }
2175 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002176 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002177 return FPToSI; // FP -> sint
2178 else
2179 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002180 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002181 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002182 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002183 return BitCast; // Same size, no-op cast
2184 } else {
2185 assert(isa<PointerType>(SrcTy) &&
2186 "Casting from a value that is not first-class type");
2187 return PtrToInt; // ptr -> int
2188 }
2189 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002190 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002191 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002192 return SIToFP; // sint -> FP
2193 else
2194 return UIToFP; // uint -> FP
2195 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2196 if (DestBits < SrcBits) {
2197 return FPTrunc; // FP -> smaller FP
2198 } else if (DestBits > SrcBits) {
2199 return FPExt; // FP -> larger FP
2200 } else {
2201 return BitCast; // same size, no-op cast
2202 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002203 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002204 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002205 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002206 return BitCast; // same size, no-op cast
2207 } else {
2208 assert(0 && "Casting pointer or non-first class to float");
2209 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002210 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2211 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002212 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002213 "Casting vector to vector of different widths");
2214 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002215 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002216 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002217 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002218 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002219 }
2220 } else if (isa<PointerType>(DestTy)) {
2221 if (isa<PointerType>(SrcTy)) {
2222 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002223 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002224 return IntToPtr; // int -> ptr
2225 } else {
2226 assert(!"Casting pointer to other than pointer or int");
2227 }
2228 } else {
2229 assert(!"Casting to type that is not first-class");
2230 }
2231
2232 // If we fall through to here we probably hit an assertion cast above
2233 // and assertions are not turned on. Anything we return is an error, so
2234 // BitCast is as good a choice as any.
2235 return BitCast;
2236}
2237
2238//===----------------------------------------------------------------------===//
2239// CastInst SubClass Constructors
2240//===----------------------------------------------------------------------===//
2241
2242/// Check that the construction parameters for a CastInst are correct. This
2243/// could be broken out into the separate constructors but it is useful to have
2244/// it in one place and to eliminate the redundant code for getting the sizes
2245/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002246bool
2247CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002248
2249 // Check for type sanity on the arguments
2250 const Type *SrcTy = S->getType();
2251 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2252 return false;
2253
2254 // Get the size of the types in bits, we'll need this later
2255 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2256 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2257
2258 // Switch on the opcode provided
2259 switch (op) {
2260 default: return false; // This is an input error
2261 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002262 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002263 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002264 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002265 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002266 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002267 case Instruction::FPTrunc:
2268 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2269 SrcBitSize > DstBitSize;
2270 case Instruction::FPExt:
2271 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2272 SrcBitSize < DstBitSize;
2273 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002274 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002275 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2276 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2277 return SVTy->getElementType()->isInteger() &&
2278 DVTy->getElementType()->isFloatingPoint() &&
2279 SVTy->getNumElements() == DVTy->getNumElements();
2280 }
2281 }
Chris Lattner03c49532007-01-15 02:27:26 +00002282 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002283 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002284 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002285 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2286 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2287 return SVTy->getElementType()->isFloatingPoint() &&
2288 DVTy->getElementType()->isInteger() &&
2289 SVTy->getNumElements() == DVTy->getNumElements();
2290 }
2291 }
Chris Lattner03c49532007-01-15 02:27:26 +00002292 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002293 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002294 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002295 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002296 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002297 case Instruction::BitCast:
2298 // BitCast implies a no-op cast of type only. No bits change.
2299 // However, you can't cast pointers to anything but pointers.
2300 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2301 return false;
2302
Duncan Sands55e50902008-01-06 10:12:28 +00002303 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002304 // these cases, the cast is okay if the source and destination bit widths
2305 // are identical.
2306 return SrcBitSize == DstBitSize;
2307 }
2308}
2309
2310TruncInst::TruncInst(
2311 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2312) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002313 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002314}
2315
2316TruncInst::TruncInst(
2317 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2318) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002319 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002320}
2321
2322ZExtInst::ZExtInst(
2323 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2324) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002325 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002326}
2327
2328ZExtInst::ZExtInst(
2329 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2330) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002331 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002332}
2333SExtInst::SExtInst(
2334 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2335) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002336 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002337}
2338
Jeff Cohencc08c832006-12-02 02:22:01 +00002339SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002340 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2341) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002342 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002343}
2344
2345FPTruncInst::FPTruncInst(
2346 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2347) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002348 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002349}
2350
2351FPTruncInst::FPTruncInst(
2352 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2353) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002354 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002355}
2356
2357FPExtInst::FPExtInst(
2358 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2359) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002360 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002361}
2362
2363FPExtInst::FPExtInst(
2364 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2365) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002366 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002367}
2368
2369UIToFPInst::UIToFPInst(
2370 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2371) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002372 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002373}
2374
2375UIToFPInst::UIToFPInst(
2376 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2377) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002378 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379}
2380
2381SIToFPInst::SIToFPInst(
2382 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2383) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002384 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002385}
2386
2387SIToFPInst::SIToFPInst(
2388 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2389) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002390 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391}
2392
2393FPToUIInst::FPToUIInst(
2394 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2395) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002396 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002397}
2398
2399FPToUIInst::FPToUIInst(
2400 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2401) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002402 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002403}
2404
2405FPToSIInst::FPToSIInst(
2406 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2407) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002408 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002409}
2410
2411FPToSIInst::FPToSIInst(
2412 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2413) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002414 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002415}
2416
2417PtrToIntInst::PtrToIntInst(
2418 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2419) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002420 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002421}
2422
2423PtrToIntInst::PtrToIntInst(
2424 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2425) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002426 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002427}
2428
2429IntToPtrInst::IntToPtrInst(
2430 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2431) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002432 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002433}
2434
2435IntToPtrInst::IntToPtrInst(
2436 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2437) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002438 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002439}
2440
2441BitCastInst::BitCastInst(
2442 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2443) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002444 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002445}
2446
2447BitCastInst::BitCastInst(
2448 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2449) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002450 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002451}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002452
2453//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002454// CmpInst Classes
2455//===----------------------------------------------------------------------===//
2456
Nate Begemand2195702008-05-12 19:01:56 +00002457CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2458 Value *LHS, Value *RHS, const std::string &Name,
2459 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002460 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002461 OperandTraits<CmpInst>::op_begin(this),
2462 OperandTraits<CmpInst>::operands(this),
2463 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002464 Op<0>() = LHS;
2465 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002466 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002467 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002468}
Gabor Greiff6caff662008-05-10 08:32:32 +00002469
Nate Begemand2195702008-05-12 19:01:56 +00002470CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2471 Value *LHS, Value *RHS, const std::string &Name,
2472 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002473 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002474 OperandTraits<CmpInst>::op_begin(this),
2475 OperandTraits<CmpInst>::operands(this),
2476 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002477 Op<0>() = LHS;
2478 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002479 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002480 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002481}
2482
2483CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002484CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002485 const std::string &Name, Instruction *InsertBefore) {
2486 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002487 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002488 InsertBefore);
2489 }
Nate Begemand2195702008-05-12 19:01:56 +00002490 if (Op == Instruction::FCmp) {
2491 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2492 InsertBefore);
2493 }
2494 if (Op == Instruction::VICmp) {
2495 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2496 InsertBefore);
2497 }
2498 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2499 InsertBefore);
Reid Spencerd9436b62006-11-20 01:22:35 +00002500}
2501
2502CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002503CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002504 const std::string &Name, BasicBlock *InsertAtEnd) {
2505 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002506 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002507 InsertAtEnd);
2508 }
Nate Begemand2195702008-05-12 19:01:56 +00002509 if (Op == Instruction::FCmp) {
2510 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2511 InsertAtEnd);
2512 }
2513 if (Op == Instruction::VICmp) {
2514 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2515 InsertAtEnd);
2516 }
2517 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2518 InsertAtEnd);
Reid Spencerd9436b62006-11-20 01:22:35 +00002519}
2520
2521void CmpInst::swapOperands() {
2522 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2523 IC->swapOperands();
2524 else
2525 cast<FCmpInst>(this)->swapOperands();
2526}
2527
2528bool CmpInst::isCommutative() {
2529 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2530 return IC->isCommutative();
2531 return cast<FCmpInst>(this)->isCommutative();
2532}
2533
2534bool CmpInst::isEquality() {
2535 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2536 return IC->isEquality();
2537 return cast<FCmpInst>(this)->isEquality();
2538}
2539
2540
Dan Gohman4e724382008-05-31 02:47:54 +00002541CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002542 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002543 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002544 case ICMP_EQ: return ICMP_NE;
2545 case ICMP_NE: return ICMP_EQ;
2546 case ICMP_UGT: return ICMP_ULE;
2547 case ICMP_ULT: return ICMP_UGE;
2548 case ICMP_UGE: return ICMP_ULT;
2549 case ICMP_ULE: return ICMP_UGT;
2550 case ICMP_SGT: return ICMP_SLE;
2551 case ICMP_SLT: return ICMP_SGE;
2552 case ICMP_SGE: return ICMP_SLT;
2553 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002554
Dan Gohman4e724382008-05-31 02:47:54 +00002555 case FCMP_OEQ: return FCMP_UNE;
2556 case FCMP_ONE: return FCMP_UEQ;
2557 case FCMP_OGT: return FCMP_ULE;
2558 case FCMP_OLT: return FCMP_UGE;
2559 case FCMP_OGE: return FCMP_ULT;
2560 case FCMP_OLE: return FCMP_UGT;
2561 case FCMP_UEQ: return FCMP_ONE;
2562 case FCMP_UNE: return FCMP_OEQ;
2563 case FCMP_UGT: return FCMP_OLE;
2564 case FCMP_ULT: return FCMP_OGE;
2565 case FCMP_UGE: return FCMP_OLT;
2566 case FCMP_ULE: return FCMP_OGT;
2567 case FCMP_ORD: return FCMP_UNO;
2568 case FCMP_UNO: return FCMP_ORD;
2569 case FCMP_TRUE: return FCMP_FALSE;
2570 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002571 }
2572}
2573
Reid Spencer266e42b2006-12-23 06:05:41 +00002574ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2575 switch (pred) {
2576 default: assert(! "Unknown icmp predicate!");
2577 case ICMP_EQ: case ICMP_NE:
2578 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2579 return pred;
2580 case ICMP_UGT: return ICMP_SGT;
2581 case ICMP_ULT: return ICMP_SLT;
2582 case ICMP_UGE: return ICMP_SGE;
2583 case ICMP_ULE: return ICMP_SLE;
2584 }
2585}
2586
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002587ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2588 switch (pred) {
2589 default: assert(! "Unknown icmp predicate!");
2590 case ICMP_EQ: case ICMP_NE:
2591 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2592 return pred;
2593 case ICMP_SGT: return ICMP_UGT;
2594 case ICMP_SLT: return ICMP_ULT;
2595 case ICMP_SGE: return ICMP_UGE;
2596 case ICMP_SLE: return ICMP_ULE;
2597 }
2598}
2599
Reid Spencer266e42b2006-12-23 06:05:41 +00002600bool ICmpInst::isSignedPredicate(Predicate pred) {
2601 switch (pred) {
2602 default: assert(! "Unknown icmp predicate!");
2603 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2604 return true;
2605 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2606 case ICMP_UGE: case ICMP_ULE:
2607 return false;
2608 }
2609}
2610
Reid Spencer0286bc12007-02-28 22:00:54 +00002611/// Initialize a set of values that all satisfy the condition with C.
2612///
2613ConstantRange
2614ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2615 APInt Lower(C);
2616 APInt Upper(C);
2617 uint32_t BitWidth = C.getBitWidth();
2618 switch (pred) {
2619 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2620 case ICmpInst::ICMP_EQ: Upper++; break;
2621 case ICmpInst::ICMP_NE: Lower++; break;
2622 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2623 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2624 case ICmpInst::ICMP_UGT:
2625 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2626 break;
2627 case ICmpInst::ICMP_SGT:
2628 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2629 break;
2630 case ICmpInst::ICMP_ULE:
2631 Lower = APInt::getMinValue(BitWidth); Upper++;
2632 break;
2633 case ICmpInst::ICMP_SLE:
2634 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2635 break;
2636 case ICmpInst::ICMP_UGE:
2637 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2638 break;
2639 case ICmpInst::ICMP_SGE:
2640 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2641 break;
2642 }
2643 return ConstantRange(Lower, Upper);
2644}
2645
Dan Gohman4e724382008-05-31 02:47:54 +00002646CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002647 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002648 default: assert(!"Unknown cmp predicate!");
2649 case ICMP_EQ: case ICMP_NE:
2650 return pred;
2651 case ICMP_SGT: return ICMP_SLT;
2652 case ICMP_SLT: return ICMP_SGT;
2653 case ICMP_SGE: return ICMP_SLE;
2654 case ICMP_SLE: return ICMP_SGE;
2655 case ICMP_UGT: return ICMP_ULT;
2656 case ICMP_ULT: return ICMP_UGT;
2657 case ICMP_UGE: return ICMP_ULE;
2658 case ICMP_ULE: return ICMP_UGE;
2659
Reid Spencerd9436b62006-11-20 01:22:35 +00002660 case FCMP_FALSE: case FCMP_TRUE:
2661 case FCMP_OEQ: case FCMP_ONE:
2662 case FCMP_UEQ: case FCMP_UNE:
2663 case FCMP_ORD: case FCMP_UNO:
2664 return pred;
2665 case FCMP_OGT: return FCMP_OLT;
2666 case FCMP_OLT: return FCMP_OGT;
2667 case FCMP_OGE: return FCMP_OLE;
2668 case FCMP_OLE: return FCMP_OGE;
2669 case FCMP_UGT: return FCMP_ULT;
2670 case FCMP_ULT: return FCMP_UGT;
2671 case FCMP_UGE: return FCMP_ULE;
2672 case FCMP_ULE: return FCMP_UGE;
2673 }
2674}
2675
Reid Spencer266e42b2006-12-23 06:05:41 +00002676bool CmpInst::isUnsigned(unsigned short predicate) {
2677 switch (predicate) {
2678 default: return false;
2679 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2680 case ICmpInst::ICMP_UGE: return true;
2681 }
2682}
2683
2684bool CmpInst::isSigned(unsigned short predicate){
2685 switch (predicate) {
2686 default: return false;
2687 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2688 case ICmpInst::ICMP_SGE: return true;
2689 }
2690}
2691
2692bool CmpInst::isOrdered(unsigned short predicate) {
2693 switch (predicate) {
2694 default: return false;
2695 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2696 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2697 case FCmpInst::FCMP_ORD: return true;
2698 }
2699}
2700
2701bool CmpInst::isUnordered(unsigned short predicate) {
2702 switch (predicate) {
2703 default: return false;
2704 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2705 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2706 case FCmpInst::FCMP_UNO: return true;
2707 }
2708}
2709
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002710//===----------------------------------------------------------------------===//
2711// SwitchInst Implementation
2712//===----------------------------------------------------------------------===//
2713
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002714void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002715 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002716 ReservedSpace = 2+NumCases*2;
2717 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002718 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002719
Gabor Greif2d3024d2008-05-26 21:33:52 +00002720 OperandList[0] = Value;
2721 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002722}
2723
Chris Lattner2195fc42007-02-24 00:55:48 +00002724/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2725/// switch on and a default destination. The number of additional cases can
2726/// be specified here to make memory allocation more efficient. This
2727/// constructor can also autoinsert before another instruction.
2728SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2729 Instruction *InsertBefore)
2730 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2731 init(Value, Default, NumCases);
2732}
2733
2734/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2735/// switch on and a default destination. The number of additional cases can
2736/// be specified here to make memory allocation more efficient. This
2737/// constructor also autoinserts at the end of the specified BasicBlock.
2738SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2739 BasicBlock *InsertAtEnd)
2740 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2741 init(Value, Default, NumCases);
2742}
2743
Misha Brukmanb1c93172005-04-21 23:48:37 +00002744SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002745 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002746 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002747 Use *OL = OperandList, *InOL = SI.OperandList;
2748 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002749 OL[i] = InOL[i];
2750 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002751 }
2752}
2753
Gordon Henriksen14a55692007-12-10 02:14:30 +00002754SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002755 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002756}
2757
2758
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002759/// addCase - Add an entry to the switch instruction...
2760///
Chris Lattner47ac1872005-02-24 05:32:09 +00002761void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002762 unsigned OpNo = NumOperands;
2763 if (OpNo+2 > ReservedSpace)
2764 resizeOperands(0); // Get more space!
2765 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002766 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002767 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002768 OperandList[OpNo] = OnVal;
2769 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002770}
2771
2772/// removeCase - This method removes the specified successor from the switch
2773/// instruction. Note that this cannot be used to remove the default
2774/// destination (successor #0).
2775///
2776void SwitchInst::removeCase(unsigned idx) {
2777 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002778 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2779
2780 unsigned NumOps = getNumOperands();
2781 Use *OL = OperandList;
2782
2783 // Move everything after this operand down.
2784 //
2785 // FIXME: we could just swap with the end of the list, then erase. However,
2786 // client might not expect this to happen. The code as it is thrashes the
2787 // use/def lists, which is kinda lame.
2788 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2789 OL[i-2] = OL[i];
2790 OL[i-2+1] = OL[i+1];
2791 }
2792
2793 // Nuke the last value.
2794 OL[NumOps-2].set(0);
2795 OL[NumOps-2+1].set(0);
2796 NumOperands = NumOps-2;
2797}
2798
2799/// resizeOperands - resize operands - This adjusts the length of the operands
2800/// list according to the following behavior:
2801/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002802/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002803/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2804/// 3. If NumOps == NumOperands, trim the reserved space.
2805///
2806void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002807 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002808 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002809 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002810 } else if (NumOps*2 > NumOperands) {
2811 // No resize needed.
2812 if (ReservedSpace >= NumOps) return;
2813 } else if (NumOps == NumOperands) {
2814 if (ReservedSpace == NumOps) return;
2815 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002816 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002817 }
2818
2819 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002820 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002821 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002822 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002823 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002824 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002825 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002826 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002827}
2828
2829
2830BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2831 return getSuccessor(idx);
2832}
2833unsigned SwitchInst::getNumSuccessorsV() const {
2834 return getNumSuccessors();
2835}
2836void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2837 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002838}
Chris Lattnerf22be932004-10-15 23:52:53 +00002839
Devang Patel295711f2008-02-19 22:15:16 +00002840//===----------------------------------------------------------------------===//
2841// GetResultInst Implementation
2842//===----------------------------------------------------------------------===//
2843
Devang Patel666d4512008-02-20 19:10:47 +00002844GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002845 const std::string &Name,
2846 Instruction *InsertBef)
Gabor Greif0d42adf2008-05-13 07:09:08 +00002847 : UnaryInstruction(cast<StructType>(Aggregate->getType())
2848 ->getElementType(Index),
2849 GetResult, Aggregate, InsertBef),
2850 Idx(Index) {
2851 assert(isValidOperands(Aggregate, Index)
2852 && "Invalid GetResultInst operands!");
Devang Patel295711f2008-02-19 22:15:16 +00002853 setName(Name);
2854}
2855
Devang Patel666d4512008-02-20 19:10:47 +00002856bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2857 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002858 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002859
Devang Patelcf193b82008-02-20 19:39:41 +00002860 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2861 unsigned NumElements = STy->getNumElements();
Chris Lattnerb6917d22008-04-23 05:36:34 +00002862 if (Index >= NumElements || NumElements == 0)
Devang Patelcf193b82008-02-20 19:39:41 +00002863 return false;
2864
2865 // getresult aggregate value's element types are restricted to
2866 // avoid nested aggregates.
2867 for (unsigned i = 0; i < NumElements; ++i)
2868 if (!STy->getElementType(i)->isFirstClassType())
2869 return false;
2870
2871 // Otherwise, Aggregate is valid.
2872 return true;
2873 }
Devang Patel666d4512008-02-20 19:10:47 +00002874 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002875}
2876
Chris Lattnerf22be932004-10-15 23:52:53 +00002877// Define these methods here so vtables don't get emitted into every translation
2878// unit that uses these classes.
2879
2880GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002881 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002882}
2883
2884BinaryOperator *BinaryOperator::clone() const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002885 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002886}
2887
Chris Lattner0b490b02007-08-24 20:48:18 +00002888FCmpInst* FCmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002889 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002890}
2891ICmpInst* ICmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002892 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002893}
2894
Nate Begemand2195702008-05-12 19:01:56 +00002895VFCmpInst* VFCmpInst::clone() const {
2896 return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
2897}
2898VICmpInst* VICmpInst::clone() const {
2899 return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
2900}
2901
Dan Gohman0752bff2008-05-23 00:36:11 +00002902ExtractValueInst *ExtractValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002903 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002904}
2905InsertValueInst *InsertValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002906 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002907}
2908
2909
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002910MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2911AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2912FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2913LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2914StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2915CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2916CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2917CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2918CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2919CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2920CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2921CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2922CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2923CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2924CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2925CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2926CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002927CallInst *CallInst::clone() const {
2928 return new(getNumOperands()) CallInst(*this);
2929}
2930SelectInst *SelectInst::clone() const {
2931 return new(getNumOperands()) SelectInst(*this);
2932}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002933VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2934
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002935ExtractElementInst *ExtractElementInst::clone() const {
2936 return new ExtractElementInst(*this);
2937}
2938InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002939 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002940}
2941ShuffleVectorInst *ShuffleVectorInst::clone() const {
2942 return new ShuffleVectorInst(*this);
2943}
Chris Lattnerf22be932004-10-15 23:52:53 +00002944PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002945ReturnInst *ReturnInst::clone() const {
2946 return new(getNumOperands()) ReturnInst(*this);
2947}
2948BranchInst *BranchInst::clone() const {
2949 return new(getNumOperands()) BranchInst(*this);
2950}
Gabor Greiff6caff662008-05-10 08:32:32 +00002951SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002952InvokeInst *InvokeInst::clone() const {
2953 return new(getNumOperands()) InvokeInst(*this);
2954}
Chris Lattnerf22be932004-10-15 23:52:53 +00002955UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002956UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002957GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }