blob: f66076d24e864730f53c40478c455149477d0334 [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() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000133 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000134}
135
136// removeIncomingValue - Remove an incoming value. This is useful if a
137// predecessor basic block is deleted.
138Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
139 unsigned NumOps = getNumOperands();
140 Use *OL = OperandList;
141 assert(Idx*2 < NumOps && "BB not in PHI node!");
142 Value *Removed = OL[Idx*2];
143
144 // Move everything after this operand down.
145 //
146 // FIXME: we could just swap with the end of the list, then erase. However,
147 // client might not expect this to happen. The code as it is thrashes the
148 // use/def lists, which is kinda lame.
149 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
150 OL[i-2] = OL[i];
151 OL[i-2+1] = OL[i+1];
152 }
153
154 // Nuke the last value.
155 OL[NumOps-2].set(0);
156 OL[NumOps-2+1].set(0);
157 NumOperands = NumOps-2;
158
159 // If the PHI node is dead, because it has zero entries, nuke it now.
160 if (NumOps == 2 && DeletePHIIfEmpty) {
161 // If anyone is using this PHI, make them use a dummy value instead...
162 replaceAllUsesWith(UndefValue::get(getType()));
163 eraseFromParent();
164 }
165 return Removed;
166}
167
168/// resizeOperands - resize operands - This adjusts the length of the operands
169/// list according to the following behavior:
170/// 1. If NumOps == 0, grow the operand list in response to a push_back style
171/// of operation. This grows the number of ops by 1.5 times.
172/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
173/// 3. If NumOps == NumOperands, trim the reserved space.
174///
175void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000176 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000177 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000178 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000179 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
180 } else if (NumOps*2 > NumOperands) {
181 // No resize needed.
182 if (ReservedSpace >= NumOps) return;
183 } else if (NumOps == NumOperands) {
184 if (ReservedSpace == NumOps) return;
185 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000186 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000187 }
188
189 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000190 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000191 Use *NewOps = allocHungoffUses(NumOps);
192 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000193 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000194 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000195 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000196 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000197}
198
Nate Begemanb3923212005-08-04 23:24:19 +0000199/// hasConstantValue - If the specified PHI node always merges together the same
200/// value, return the value, otherwise return null.
201///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000202Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000203 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000204 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000205 if (getIncomingValue(0) != this) // not X = phi X
206 return getIncomingValue(0);
207 else
208 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000209 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000210
Nate Begemanb3923212005-08-04 23:24:19 +0000211 // Otherwise if all of the incoming values are the same for the PHI, replace
212 // the PHI node with the incoming value.
213 //
214 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000215 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000216 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000217 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000218 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000219 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000220 if (InVal && getIncomingValue(i) != InVal)
221 return 0; // Not the same, bail out.
222 else
223 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000224 }
Nate Begemanb3923212005-08-04 23:24:19 +0000225
226 // The only case that could cause InVal to be null is if we have a PHI node
227 // that only has entries for itself. In this case, there is no entry into the
228 // loop, so kill the PHI.
229 //
230 if (InVal == 0) InVal = UndefValue::get(getType());
231
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000232 // If we have a PHI node like phi(X, undef, X), where X is defined by some
233 // instruction, we cannot always return X as the result of the PHI node. Only
234 // do this if X is not an instruction (thus it must dominate the PHI block),
235 // or if the client is prepared to deal with this possibility.
236 if (HasUndefInput && !AllowNonDominatingInstruction)
237 if (Instruction *IV = dyn_cast<Instruction>(InVal))
238 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000239 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000240 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000241 return 0; // Cannot guarantee that InVal dominates this PHINode.
242
Nate Begemanb3923212005-08-04 23:24:19 +0000243 // All of the incoming values are the same, return the value now.
244 return InVal;
245}
246
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000247
248//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000249// CallInst Implementation
250//===----------------------------------------------------------------------===//
251
Gordon Henriksen14a55692007-12-10 02:14:30 +0000252CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000253}
254
Chris Lattner054ba2c2007-02-13 00:58:44 +0000255void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000256 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
257 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000258 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000259
Misha Brukmanb1c93172005-04-21 23:48:37 +0000260 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000261 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000262 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000263
Chris Lattner054ba2c2007-02-13 00:58:44 +0000264 assert((NumParams == FTy->getNumParams() ||
265 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000266 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000267 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000268 assert((i >= FTy->getNumParams() ||
269 FTy->getParamType(i) == Params[i]->getType()) &&
270 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000271 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000272 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273}
274
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000275void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000276 assert(NumOperands == 3 && "NumOperands not set up?");
277 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000278 OL[0] = Func;
279 OL[1] = Actual1;
280 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000281
282 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000283 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000284 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000286 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000287 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000288 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000289 assert((0 >= FTy->getNumParams() ||
290 FTy->getParamType(0) == Actual1->getType()) &&
291 "Calling a function with a bad signature!");
292 assert((1 >= FTy->getNumParams() ||
293 FTy->getParamType(1) == Actual2->getType()) &&
294 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000295}
296
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000297void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000298 assert(NumOperands == 2 && "NumOperands not set up?");
299 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000300 OL[0] = Func;
301 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000302
303 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000305 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000307 assert((FTy->getNumParams() == 1 ||
308 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000310 assert((0 == FTy->getNumParams() ||
311 FTy->getParamType(0) == Actual->getType()) &&
312 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313}
314
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000315void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000316 assert(NumOperands == 1 && "NumOperands not set up?");
317 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000318 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000319
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000320 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000321 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000322 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000324 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000325}
326
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000328 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
330 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000331 Instruction::Call,
332 OperandTraits<CallInst>::op_end(this) - 2,
333 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000334 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000335 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336}
337
338CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
339 BasicBlock *InsertAtEnd)
340 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
341 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000342 Instruction::Call,
343 OperandTraits<CallInst>::op_end(this) - 2,
344 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000345 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000346 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000347}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348CallInst::CallInst(Value *Func, const std::string &Name,
349 Instruction *InsertBefore)
350 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
351 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000352 Instruction::Call,
353 OperandTraits<CallInst>::op_end(this) - 1,
354 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000355 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000356 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000357}
358
359CallInst::CallInst(Value *Func, const std::string &Name,
360 BasicBlock *InsertAtEnd)
361 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
362 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000363 Instruction::Call,
364 OperandTraits<CallInst>::op_end(this) - 1,
365 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000366 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000367 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000368}
369
Misha Brukmanb1c93172005-04-21 23:48:37 +0000370CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000371 : Instruction(CI.getType(), Instruction::Call,
372 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000373 CI.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000374 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000375 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000376 Use *OL = OperandList;
377 Use *InOL = CI.OperandList;
378 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000379 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000380}
381
Eric Christopher901b1a72008-05-16 20:39:43 +0000382void CallInst::addParamAttr(unsigned i, ParameterAttributes attr) {
383 PAListPtr PAL = getParamAttrs();
384 PAL = PAL.addAttr(i, attr);
385 setParamAttrs(PAL);
386}
387
Chris Lattnerc994c022008-03-13 05:00:21 +0000388bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000389 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000390 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000391 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000392 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000393 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000394}
395
Duncan Sandsaa31b922007-12-19 21:13:37 +0000396void CallInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000397 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000398 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000399 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000400 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000401 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000402 setParamAttrs(PAL);
403}
404
Duncan Sands5208d1a2007-11-28 17:07:01 +0000405
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000406//===----------------------------------------------------------------------===//
407// InvokeInst Implementation
408//===----------------------------------------------------------------------===//
409
410void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000411 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000412 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
413 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000414 OL[0] = Fn;
415 OL[1] = IfNormal;
416 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000417 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000418 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000419 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000420
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000421 assert(((NumArgs == FTy->getNumParams()) ||
422 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000423 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000424
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000425 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000426 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000427 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000428 "Invoking a function with a bad signature!");
429
Gabor Greif2d3024d2008-05-26 21:33:52 +0000430 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000431 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000432}
433
Misha Brukmanb1c93172005-04-21 23:48:37 +0000434InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000435 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000436 OperandTraits<InvokeInst>::op_end(this)
437 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000438 II.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000439 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000440 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000441 Use *OL = OperandList, *InOL = II.OperandList;
442 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000443 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000444}
445
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000446BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
447 return getSuccessor(idx);
448}
449unsigned InvokeInst::getNumSuccessorsV() const {
450 return getNumSuccessors();
451}
452void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
453 return setSuccessor(idx, B);
454}
455
Chris Lattnerc994c022008-03-13 05:00:21 +0000456bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000457 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000458 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000459 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000460 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000461 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000462}
463
Eric Christopher901b1a72008-05-16 20:39:43 +0000464void InvokeInst::addParamAttr(unsigned i, ParameterAttributes attr) {
465 PAListPtr PAL = getParamAttrs();
466 PAL = PAL.addAttr(i, attr);
467 setParamAttrs(PAL);
468}
469
Duncan Sandsaa31b922007-12-19 21:13:37 +0000470void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000471 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000472 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000473 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000474 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000475 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000476 setParamAttrs(PAL);
477}
478
Duncan Sands5208d1a2007-11-28 17:07:01 +0000479
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000480//===----------------------------------------------------------------------===//
481// ReturnInst Implementation
482//===----------------------------------------------------------------------===//
483
Chris Lattner2195fc42007-02-24 00:55:48 +0000484ReturnInst::ReturnInst(const ReturnInst &RI)
485 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Gabor Greif697e94c2008-05-15 10:04:30 +0000486 OperandTraits<ReturnInst>::op_end(this)
487 - RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000488 RI.getNumOperands()) {
Devang Patel59643e52008-02-23 00:35:18 +0000489 unsigned N = RI.getNumOperands();
Gabor Greiff6caff662008-05-10 08:32:32 +0000490 if (N == 1)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000491 Op<0>() = RI.Op<0>();
Devang Patelae682fb2008-02-26 17:56:20 +0000492 else if (N) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000493 Use *OL = OperandList;
Devang Patelae682fb2008-02-26 17:56:20 +0000494 for (unsigned i = 0; i < N; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000495 OL[i] = RI.getOperand(i);
Devang Patelae682fb2008-02-26 17:56:20 +0000496 }
Chris Lattner2195fc42007-02-24 00:55:48 +0000497}
498
499ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000500 : TerminatorInst(Type::VoidTy, Instruction::Ret,
501 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
502 retVal != 0, InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000503 if (retVal)
504 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000505}
506ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000507 : TerminatorInst(Type::VoidTy, Instruction::Ret,
508 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
509 retVal != 0, InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000510 if (retVal)
511 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000512}
513ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000514 : TerminatorInst(Type::VoidTy, Instruction::Ret,
515 OperandTraits<ReturnInst>::op_end(this),
516 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000517}
518
Devang Patela736c002008-02-26 19:38:17 +0000519ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
520 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000521 : TerminatorInst(Type::VoidTy, Instruction::Ret,
522 OperandTraits<ReturnInst>::op_end(this) - N,
523 N, InsertBefore) {
Devang Patela736c002008-02-26 19:38:17 +0000524 if (N != 0)
525 init(retVals, N);
526}
527ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
528 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000529 : TerminatorInst(Type::VoidTy, Instruction::Ret,
530 OperandTraits<ReturnInst>::op_end(this) - N,
531 N, InsertAtEnd) {
Devang Patela736c002008-02-26 19:38:17 +0000532 if (N != 0)
533 init(retVals, N);
534}
535
Devang Patel060e79c2008-02-26 19:15:26 +0000536void ReturnInst::init(Value * const* retVals, unsigned N) {
Devang Patelc38eb522008-02-26 18:49:29 +0000537 assert (N > 0 && "Invalid operands numbers in ReturnInst init");
Devang Patel59643e52008-02-23 00:35:18 +0000538
Devang Patelc38eb522008-02-26 18:49:29 +0000539 NumOperands = N;
Devang Patel59643e52008-02-23 00:35:18 +0000540 if (NumOperands == 1) {
Devang Patel060e79c2008-02-26 19:15:26 +0000541 Value *V = *retVals;
Devang Patel59643e52008-02-23 00:35:18 +0000542 if (V->getType() == Type::VoidTy)
543 return;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000544 Op<0>() = V;
Devang Patelae682fb2008-02-26 17:56:20 +0000545 return;
Devang Patel59643e52008-02-23 00:35:18 +0000546 }
547
Gabor Greiff6caff662008-05-10 08:32:32 +0000548 Use *OL = OperandList;
Devang Patel59643e52008-02-23 00:35:18 +0000549 for (unsigned i = 0; i < NumOperands; ++i) {
Devang Patel060e79c2008-02-26 19:15:26 +0000550 Value *V = *retVals++;
Devang Patel59643e52008-02-23 00:35:18 +0000551 assert(!isa<BasicBlock>(V) &&
552 "Cannot return basic block. Probably using the incorrect ctor");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000553 OL[i] = V;
Devang Patel59643e52008-02-23 00:35:18 +0000554 }
555}
556
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000557unsigned ReturnInst::getNumSuccessorsV() const {
558 return getNumSuccessors();
559}
560
Devang Patelae682fb2008-02-26 17:56:20 +0000561/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
562/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000563void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000564 assert(0 && "ReturnInst has no successors!");
565}
566
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000567BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
568 assert(0 && "ReturnInst has no successors!");
569 abort();
570 return 0;
571}
572
Devang Patel59643e52008-02-23 00:35:18 +0000573ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000574}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000575
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000576//===----------------------------------------------------------------------===//
577// UnwindInst Implementation
578//===----------------------------------------------------------------------===//
579
Chris Lattner2195fc42007-02-24 00:55:48 +0000580UnwindInst::UnwindInst(Instruction *InsertBefore)
581 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
582}
583UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
584 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
585}
586
587
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000588unsigned UnwindInst::getNumSuccessorsV() const {
589 return getNumSuccessors();
590}
591
592void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000593 assert(0 && "UnwindInst has no successors!");
594}
595
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
597 assert(0 && "UnwindInst has no successors!");
598 abort();
599 return 0;
600}
601
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000602//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000603// UnreachableInst Implementation
604//===----------------------------------------------------------------------===//
605
Chris Lattner2195fc42007-02-24 00:55:48 +0000606UnreachableInst::UnreachableInst(Instruction *InsertBefore)
607 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
608}
609UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
610 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
611}
612
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000613unsigned UnreachableInst::getNumSuccessorsV() const {
614 return getNumSuccessors();
615}
616
617void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
618 assert(0 && "UnwindInst has no successors!");
619}
620
621BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
622 assert(0 && "UnwindInst has no successors!");
623 abort();
624 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000625}
626
627//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000628// BranchInst Implementation
629//===----------------------------------------------------------------------===//
630
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000631void BranchInst::AssertOK() {
632 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000633 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000634 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000635}
636
Chris Lattner2195fc42007-02-24 00:55:48 +0000637BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000638 : TerminatorInst(Type::VoidTy, Instruction::Br,
639 OperandTraits<BranchInst>::op_end(this) - 1,
640 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000641 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000642 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000643}
644BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
645 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000646 : TerminatorInst(Type::VoidTy, Instruction::Br,
647 OperandTraits<BranchInst>::op_end(this) - 3,
648 3, InsertBefore) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000649 Op<0>() = IfTrue;
650 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000651 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000652#ifndef NDEBUG
653 AssertOK();
654#endif
655}
656
657BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000658 : TerminatorInst(Type::VoidTy, Instruction::Br,
659 OperandTraits<BranchInst>::op_end(this) - 1,
660 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000661 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000662 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000663}
664
665BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
666 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000667 : TerminatorInst(Type::VoidTy, Instruction::Br,
668 OperandTraits<BranchInst>::op_end(this) - 3,
669 3, InsertAtEnd) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000670 Op<0>() = IfTrue;
671 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000672 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000673#ifndef NDEBUG
674 AssertOK();
675#endif
676}
677
678
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000679BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000680 TerminatorInst(Type::VoidTy, Instruction::Br,
681 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
682 BI.getNumOperands()) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000683 OperandList[0] = BI.getOperand(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000684 if (BI.getNumOperands() != 1) {
685 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000686 OperandList[1] = BI.getOperand(1);
687 OperandList[2] = BI.getOperand(2);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000688 }
689}
690
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000691BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
692 return getSuccessor(idx);
693}
694unsigned BranchInst::getNumSuccessorsV() const {
695 return getNumSuccessors();
696}
697void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
698 setSuccessor(idx, B);
699}
700
701
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000702//===----------------------------------------------------------------------===//
703// AllocationInst Implementation
704//===----------------------------------------------------------------------===//
705
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000706static Value *getAISize(Value *Amt) {
707 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000708 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000709 else {
710 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000711 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000712 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000713 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000714 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000715 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000716}
717
Misha Brukmanb1c93172005-04-21 23:48:37 +0000718AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000719 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000720 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000721 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000722 InsertBefore) {
723 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000724 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000725 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000726}
727
Misha Brukmanb1c93172005-04-21 23:48:37 +0000728AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000729 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000730 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000731 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000732 InsertAtEnd) {
733 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000734 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000735 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000736}
737
Gordon Henriksen14a55692007-12-10 02:14:30 +0000738// Out of line virtual method, so the vtable, etc has a home.
739AllocationInst::~AllocationInst() {
740}
741
Dan Gohmanaa583d72008-03-24 16:55:58 +0000742void AllocationInst::setAlignment(unsigned Align) {
743 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
744 SubclassData = Log2_32(Align) + 1;
745 assert(getAlignment() == Align && "Alignment representation error!");
746}
747
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000748bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000749 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
750 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000751 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000752}
753
754const Type *AllocationInst::getAllocatedType() const {
755 return getType()->getElementType();
756}
757
758AllocaInst::AllocaInst(const AllocaInst &AI)
759 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000760 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000761}
762
763MallocInst::MallocInst(const MallocInst &MI)
764 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000765 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000766}
767
768//===----------------------------------------------------------------------===//
769// FreeInst Implementation
770//===----------------------------------------------------------------------===//
771
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000772void FreeInst::AssertOK() {
773 assert(isa<PointerType>(getOperand(0)->getType()) &&
774 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000775}
776
777FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000778 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000779 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000780}
781
782FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000783 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000784 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000785}
786
787
788//===----------------------------------------------------------------------===//
789// LoadInst Implementation
790//===----------------------------------------------------------------------===//
791
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000792void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000793 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000794 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000795}
796
797LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000798 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000799 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000800 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000801 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000802 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000803 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000804}
805
806LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000807 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000808 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000809 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000810 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000811 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000812 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000813}
814
815LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
816 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000817 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000818 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000819 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000820 setAlignment(0);
821 AssertOK();
822 setName(Name);
823}
824
825LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
826 unsigned Align, Instruction *InsertBef)
827 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
828 Load, Ptr, InsertBef) {
829 setVolatile(isVolatile);
830 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000831 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000832 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000833}
834
Dan Gohman68659282007-07-18 20:51:11 +0000835LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
836 unsigned Align, BasicBlock *InsertAE)
837 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
838 Load, Ptr, InsertAE) {
839 setVolatile(isVolatile);
840 setAlignment(Align);
841 AssertOK();
842 setName(Name);
843}
844
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000845LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
846 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000847 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000848 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000849 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000850 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000851 AssertOK();
852 setName(Name);
853}
854
855
856
857LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000858 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
859 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000860 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000861 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000862 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000863 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000864}
865
866LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000867 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
868 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000869 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000870 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000871 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000872 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000873}
874
875LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
876 Instruction *InsertBef)
877: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000878 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000879 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000880 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000881 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000882 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000883}
884
885LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
886 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000887 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
888 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000889 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000890 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000891 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000892 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000893}
894
Christopher Lamb84485702007-04-22 19:24:39 +0000895void LoadInst::setAlignment(unsigned Align) {
896 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
897 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
898}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000899
900//===----------------------------------------------------------------------===//
901// StoreInst Implementation
902//===----------------------------------------------------------------------===//
903
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000904void StoreInst::AssertOK() {
905 assert(isa<PointerType>(getOperand(1)->getType()) &&
906 "Ptr must have pointer type!");
907 assert(getOperand(0)->getType() ==
908 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000909 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000910}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000911
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000912
913StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000914 : Instruction(Type::VoidTy, Store,
915 OperandTraits<StoreInst>::op_begin(this),
916 OperandTraits<StoreInst>::operands(this),
917 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000918 Op<0>() = val;
919 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000920 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000921 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000922 AssertOK();
923}
924
925StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000926 : Instruction(Type::VoidTy, Store,
927 OperandTraits<StoreInst>::op_begin(this),
928 OperandTraits<StoreInst>::operands(this),
929 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000930 Op<0>() = val;
931 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000932 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000933 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000934 AssertOK();
935}
936
Misha Brukmanb1c93172005-04-21 23:48:37 +0000937StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000938 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000939 : Instruction(Type::VoidTy, Store,
940 OperandTraits<StoreInst>::op_begin(this),
941 OperandTraits<StoreInst>::operands(this),
942 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000943 Op<0>() = val;
944 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000945 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000946 setAlignment(0);
947 AssertOK();
948}
949
950StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
951 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000952 : Instruction(Type::VoidTy, Store,
953 OperandTraits<StoreInst>::op_begin(this),
954 OperandTraits<StoreInst>::operands(this),
955 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000956 Op<0>() = val;
957 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000958 setVolatile(isVolatile);
959 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000960 AssertOK();
961}
962
Misha Brukmanb1c93172005-04-21 23:48:37 +0000963StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000964 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000965 : Instruction(Type::VoidTy, Store,
966 OperandTraits<StoreInst>::op_begin(this),
967 OperandTraits<StoreInst>::operands(this),
968 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000969 Op<0>() = val;
970 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000971 setVolatile(isVolatile);
972 setAlignment(Align);
973 AssertOK();
974}
975
976StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000977 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000978 : Instruction(Type::VoidTy, Store,
979 OperandTraits<StoreInst>::op_begin(this),
980 OperandTraits<StoreInst>::operands(this),
981 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000982 Op<0>() = val;
983 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000984 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000985 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000986 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000987}
988
Christopher Lamb84485702007-04-22 19:24:39 +0000989void StoreInst::setAlignment(unsigned Align) {
990 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
991 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
992}
993
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000994//===----------------------------------------------------------------------===//
995// GetElementPtrInst Implementation
996//===----------------------------------------------------------------------===//
997
Christopher Lambedf07882007-12-17 01:12:55 +0000998static unsigned retrieveAddrSpace(const Value *Val) {
999 return cast<PointerType>(Val->getType())->getAddressSpace();
1000}
1001
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001002void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx, 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 Gohman0752bff2008-05-23 00:36:11 +00001386}
1387
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001388InsertValueInst::InsertValueInst(Value *Agg,
1389 Value *Val,
1390 unsigned Idx,
1391 const std::string &Name,
1392 Instruction *InsertBefore)
1393 : Instruction(Agg->getType(), InsertValue,
1394 OperandTraits<InsertValueInst>::op_begin(this),
1395 2, InsertBefore) {
1396 init(Agg, Val, Idx, Name);
1397}
1398
1399InsertValueInst::InsertValueInst(Value *Agg,
1400 Value *Val,
1401 unsigned Idx,
1402 const std::string &Name,
1403 BasicBlock *InsertAtEnd)
1404 : Instruction(Agg->getType(), InsertValue,
1405 OperandTraits<InsertValueInst>::op_begin(this),
1406 2, InsertAtEnd) {
1407 init(Agg, Val, Idx, Name);
1408}
1409
Dan Gohman0752bff2008-05-23 00:36:11 +00001410//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001411// ExtractValueInst Class
1412//===----------------------------------------------------------------------===//
1413
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001414void ExtractValueInst::init(Value *Agg, const unsigned *Idx, unsigned NumIdx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001415 assert(NumOperands == 1 && "NumOperands not initialized?");
1416 Op<0>() = Agg;
Dan Gohman0752bff2008-05-23 00:36:11 +00001417
Dan Gohman1ecaf452008-05-31 00:58:22 +00001418 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001419 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001420}
1421
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001422void ExtractValueInst::init(Value *Agg, unsigned Idx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001423 assert(NumOperands == 1 && "NumOperands not initialized?");
1424 Op<0>() = Agg;
1425
1426 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001427 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001428}
1429
1430ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
1431 : Instruction(reinterpret_cast<const Type*>(EVI.getType()), ExtractValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001432 OperandTraits<ExtractValueInst>::op_begin(this), 1),
1433 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
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001463ExtractValueInst::ExtractValueInst(Value *Agg,
1464 unsigned Idx,
1465 const std::string &Name,
1466 BasicBlock *InsertAtEnd)
1467 : Instruction(checkType(getIndexedType(Agg->getType(), &Idx, 1)),
1468 ExtractValue,
1469 OperandTraits<ExtractValueInst>::op_begin(this),
1470 1, InsertAtEnd) {
1471 init(Agg, Idx, Name);
1472}
1473
1474ExtractValueInst::ExtractValueInst(Value *Agg,
1475 unsigned Idx,
1476 const std::string &Name,
1477 Instruction *InsertBefore)
1478 : Instruction(checkType(getIndexedType(Agg->getType(), &Idx, 1)),
1479 ExtractValue,
1480 OperandTraits<ExtractValueInst>::op_begin(this),
1481 1, InsertBefore) {
1482 init(Agg, Idx, Name);
1483}
1484
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001485//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001486// BinaryOperator Class
1487//===----------------------------------------------------------------------===//
1488
Chris Lattner2195fc42007-02-24 00:55:48 +00001489BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1490 const Type *Ty, const std::string &Name,
1491 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001492 : Instruction(Ty, iType,
1493 OperandTraits<BinaryOperator>::op_begin(this),
1494 OperandTraits<BinaryOperator>::operands(this),
1495 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001496 Op<0>() = S1;
1497 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001498 init(iType);
1499 setName(Name);
1500}
1501
1502BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1503 const Type *Ty, const std::string &Name,
1504 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001505 : Instruction(Ty, iType,
1506 OperandTraits<BinaryOperator>::op_begin(this),
1507 OperandTraits<BinaryOperator>::operands(this),
1508 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001509 Op<0>() = S1;
1510 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001511 init(iType);
1512 setName(Name);
1513}
1514
1515
1516void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001517 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001518 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001519 assert(LHS->getType() == RHS->getType() &&
1520 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001521#ifndef NDEBUG
1522 switch (iType) {
1523 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001524 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001525 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001526 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001527 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001528 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001529 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001530 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001531 case UDiv:
1532 case SDiv:
1533 assert(getType() == LHS->getType() &&
1534 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001535 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1536 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001537 "Incorrect operand type (not integer) for S/UDIV");
1538 break;
1539 case FDiv:
1540 assert(getType() == LHS->getType() &&
1541 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001542 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1543 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001544 && "Incorrect operand type (not floating point) for FDIV");
1545 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001546 case URem:
1547 case SRem:
1548 assert(getType() == LHS->getType() &&
1549 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001550 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1551 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001552 "Incorrect operand type (not integer) for S/UREM");
1553 break;
1554 case FRem:
1555 assert(getType() == LHS->getType() &&
1556 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001557 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1558 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001559 && "Incorrect operand type (not floating point) for FREM");
1560 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001561 case Shl:
1562 case LShr:
1563 case AShr:
1564 assert(getType() == LHS->getType() &&
1565 "Shift operation should return same type as operands!");
1566 assert(getType()->isInteger() &&
1567 "Shift operation requires integer operands");
1568 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001569 case And: case Or:
1570 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001571 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001572 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001573 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001574 (isa<VectorType>(getType()) &&
1575 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001576 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001577 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001578 default:
1579 break;
1580 }
1581#endif
1582}
1583
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001584BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001585 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001586 Instruction *InsertBefore) {
1587 assert(S1->getType() == S2->getType() &&
1588 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001589 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001590}
1591
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001592BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001593 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001594 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001595 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001596 InsertAtEnd->getInstList().push_back(Res);
1597 return Res;
1598}
1599
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001600BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001601 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001602 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1603 return new BinaryOperator(Instruction::Sub,
1604 zero, Op,
1605 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001606}
1607
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001608BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001609 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001610 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1611 return new BinaryOperator(Instruction::Sub,
1612 zero, Op,
1613 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001614}
1615
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001616BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001617 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001618 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001619 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001620 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001621 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001622 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001623 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001624 }
1625
1626 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001627 Op->getType(), Name, InsertBefore);
1628}
1629
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001630BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001631 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001632 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001633 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001634 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001635 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001636 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001637 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001638 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001639 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001640 }
1641
1642 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001643 Op->getType(), Name, InsertAtEnd);
1644}
1645
1646
1647// isConstantAllOnes - Helper function for several functions below
1648static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001649 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1650 return CI->isAllOnesValue();
1651 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1652 return CV->isAllOnesValue();
1653 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001654}
1655
1656bool BinaryOperator::isNeg(const Value *V) {
1657 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1658 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001659 return Bop->getOperand(0) ==
1660 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001661 return false;
1662}
1663
1664bool BinaryOperator::isNot(const Value *V) {
1665 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1666 return (Bop->getOpcode() == Instruction::Xor &&
1667 (isConstantAllOnes(Bop->getOperand(1)) ||
1668 isConstantAllOnes(Bop->getOperand(0))));
1669 return false;
1670}
1671
Chris Lattner2c7d1772005-04-24 07:28:37 +00001672Value *BinaryOperator::getNegArgument(Value *BinOp) {
1673 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1674 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001675}
1676
Chris Lattner2c7d1772005-04-24 07:28:37 +00001677const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1678 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001679}
1680
Chris Lattner2c7d1772005-04-24 07:28:37 +00001681Value *BinaryOperator::getNotArgument(Value *BinOp) {
1682 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1683 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1684 Value *Op0 = BO->getOperand(0);
1685 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001686 if (isConstantAllOnes(Op0)) return Op1;
1687
1688 assert(isConstantAllOnes(Op1));
1689 return Op0;
1690}
1691
Chris Lattner2c7d1772005-04-24 07:28:37 +00001692const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1693 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001694}
1695
1696
1697// swapOperands - Exchange the two operands to this instruction. This
1698// instruction is safe to use on any binary instruction and does not
1699// modify the semantics of the instruction. If the instruction is
1700// order dependent (SetLT f.e.) the opcode is changed.
1701//
1702bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001703 if (!isCommutative())
1704 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001705 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001706 return false;
1707}
1708
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001709//===----------------------------------------------------------------------===//
1710// CastInst Class
1711//===----------------------------------------------------------------------===//
1712
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001713// Just determine if this cast only deals with integral->integral conversion.
1714bool CastInst::isIntegerCast() const {
1715 switch (getOpcode()) {
1716 default: return false;
1717 case Instruction::ZExt:
1718 case Instruction::SExt:
1719 case Instruction::Trunc:
1720 return true;
1721 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001722 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001723 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001724}
1725
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001726bool CastInst::isLosslessCast() const {
1727 // Only BitCast can be lossless, exit fast if we're not BitCast
1728 if (getOpcode() != Instruction::BitCast)
1729 return false;
1730
1731 // Identity cast is always lossless
1732 const Type* SrcTy = getOperand(0)->getType();
1733 const Type* DstTy = getType();
1734 if (SrcTy == DstTy)
1735 return true;
1736
Reid Spencer8d9336d2006-12-31 05:26:44 +00001737 // Pointer to pointer is always lossless.
1738 if (isa<PointerType>(SrcTy))
1739 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001740 return false; // Other types have no identity values
1741}
1742
1743/// This function determines if the CastInst does not require any bits to be
1744/// changed in order to effect the cast. Essentially, it identifies cases where
1745/// no code gen is necessary for the cast, hence the name no-op cast. For
1746/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001747/// # bitcast i32* %x to i8*
1748/// # bitcast <2 x i32> %x to <4 x i16>
1749/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001750/// @brief Determine if a cast is a no-op.
1751bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1752 switch (getOpcode()) {
1753 default:
1754 assert(!"Invalid CastOp");
1755 case Instruction::Trunc:
1756 case Instruction::ZExt:
1757 case Instruction::SExt:
1758 case Instruction::FPTrunc:
1759 case Instruction::FPExt:
1760 case Instruction::UIToFP:
1761 case Instruction::SIToFP:
1762 case Instruction::FPToUI:
1763 case Instruction::FPToSI:
1764 return false; // These always modify bits
1765 case Instruction::BitCast:
1766 return true; // BitCast never modifies bits.
1767 case Instruction::PtrToInt:
1768 return IntPtrTy->getPrimitiveSizeInBits() ==
1769 getType()->getPrimitiveSizeInBits();
1770 case Instruction::IntToPtr:
1771 return IntPtrTy->getPrimitiveSizeInBits() ==
1772 getOperand(0)->getType()->getPrimitiveSizeInBits();
1773 }
1774}
1775
1776/// This function determines if a pair of casts can be eliminated and what
1777/// opcode should be used in the elimination. This assumes that there are two
1778/// instructions like this:
1779/// * %F = firstOpcode SrcTy %x to MidTy
1780/// * %S = secondOpcode MidTy %F to DstTy
1781/// The function returns a resultOpcode so these two casts can be replaced with:
1782/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1783/// If no such cast is permited, the function returns 0.
1784unsigned CastInst::isEliminableCastPair(
1785 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1786 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1787{
1788 // Define the 144 possibilities for these two cast instructions. The values
1789 // in this matrix determine what to do in a given situation and select the
1790 // case in the switch below. The rows correspond to firstOp, the columns
1791 // correspond to secondOp. In looking at the table below, keep in mind
1792 // the following cast properties:
1793 //
1794 // Size Compare Source Destination
1795 // Operator Src ? Size Type Sign Type Sign
1796 // -------- ------------ ------------------- ---------------------
1797 // TRUNC > Integer Any Integral Any
1798 // ZEXT < Integral Unsigned Integer Any
1799 // SEXT < Integral Signed Integer Any
1800 // FPTOUI n/a FloatPt n/a Integral Unsigned
1801 // FPTOSI n/a FloatPt n/a Integral Signed
1802 // UITOFP n/a Integral Unsigned FloatPt n/a
1803 // SITOFP n/a Integral Signed FloatPt n/a
1804 // FPTRUNC > FloatPt n/a FloatPt n/a
1805 // FPEXT < FloatPt n/a FloatPt n/a
1806 // PTRTOINT n/a Pointer n/a Integral Unsigned
1807 // INTTOPTR n/a Integral Unsigned Pointer n/a
1808 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001809 //
1810 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1811 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1812 // into "fptoui double to ulong", but this loses information about the range
1813 // of the produced value (we no longer know the top-part is all zeros).
1814 // Further this conversion is often much more expensive for typical hardware,
1815 // and causes issues when building libgcc. We disallow fptosi+sext for the
1816 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001817 const unsigned numCastOps =
1818 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1819 static const uint8_t CastResults[numCastOps][numCastOps] = {
1820 // T F F U S F F P I B -+
1821 // R Z S P P I I T P 2 N T |
1822 // U E E 2 2 2 2 R E I T C +- secondOp
1823 // N X X U S F F N X N 2 V |
1824 // C T T I I P P C T T P T -+
1825 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1826 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1827 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001828 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1829 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001830 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1831 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1832 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1833 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1834 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1835 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1836 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1837 };
1838
1839 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1840 [secondOp-Instruction::CastOpsBegin];
1841 switch (ElimCase) {
1842 case 0:
1843 // categorically disallowed
1844 return 0;
1845 case 1:
1846 // allowed, use first cast's opcode
1847 return firstOp;
1848 case 2:
1849 // allowed, use second cast's opcode
1850 return secondOp;
1851 case 3:
1852 // no-op cast in second op implies firstOp as long as the DestTy
1853 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001854 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001855 return firstOp;
1856 return 0;
1857 case 4:
1858 // no-op cast in second op implies firstOp as long as the DestTy
1859 // is floating point
1860 if (DstTy->isFloatingPoint())
1861 return firstOp;
1862 return 0;
1863 case 5:
1864 // no-op cast in first op implies secondOp as long as the SrcTy
1865 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001866 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001867 return secondOp;
1868 return 0;
1869 case 6:
1870 // no-op cast in first op implies secondOp as long as the SrcTy
1871 // is a floating point
1872 if (SrcTy->isFloatingPoint())
1873 return secondOp;
1874 return 0;
1875 case 7: {
1876 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1877 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1878 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1879 if (MidSize >= PtrSize)
1880 return Instruction::BitCast;
1881 return 0;
1882 }
1883 case 8: {
1884 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1885 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1886 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1887 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1888 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1889 if (SrcSize == DstSize)
1890 return Instruction::BitCast;
1891 else if (SrcSize < DstSize)
1892 return firstOp;
1893 return secondOp;
1894 }
1895 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1896 return Instruction::ZExt;
1897 case 10:
1898 // fpext followed by ftrunc is allowed if the bit size returned to is
1899 // the same as the original, in which case its just a bitcast
1900 if (SrcTy == DstTy)
1901 return Instruction::BitCast;
1902 return 0; // If the types are not the same we can't eliminate it.
1903 case 11:
1904 // bitcast followed by ptrtoint is allowed as long as the bitcast
1905 // is a pointer to pointer cast.
1906 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1907 return secondOp;
1908 return 0;
1909 case 12:
1910 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1911 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1912 return firstOp;
1913 return 0;
1914 case 13: {
1915 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1916 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1917 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1918 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1919 if (SrcSize <= PtrSize && SrcSize == DstSize)
1920 return Instruction::BitCast;
1921 return 0;
1922 }
1923 case 99:
1924 // cast combination can't happen (error in input). This is for all cases
1925 // where the MidTy is not the same for the two cast instructions.
1926 assert(!"Invalid Cast Combination");
1927 return 0;
1928 default:
1929 assert(!"Error in CastResults table!!!");
1930 return 0;
1931 }
1932 return 0;
1933}
1934
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001935CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001936 const std::string &Name, Instruction *InsertBefore) {
1937 // Construct and return the appropriate CastInst subclass
1938 switch (op) {
1939 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1940 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1941 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1942 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1943 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1944 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1945 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1946 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1947 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1948 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1949 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1950 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1951 default:
1952 assert(!"Invalid opcode provided");
1953 }
1954 return 0;
1955}
1956
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001957CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001958 const std::string &Name, BasicBlock *InsertAtEnd) {
1959 // Construct and return the appropriate CastInst subclass
1960 switch (op) {
1961 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1962 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1963 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1964 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1965 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1966 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1967 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1968 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1969 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1970 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1971 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1972 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1973 default:
1974 assert(!"Invalid opcode provided");
1975 }
1976 return 0;
1977}
1978
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001979CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001980 const std::string &Name,
1981 Instruction *InsertBefore) {
1982 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001983 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1984 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001985}
1986
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001987CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001988 const std::string &Name,
1989 BasicBlock *InsertAtEnd) {
1990 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001991 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1992 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001993}
1994
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001995CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001996 const std::string &Name,
1997 Instruction *InsertBefore) {
1998 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001999 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2000 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002001}
2002
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002003CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002004 const std::string &Name,
2005 BasicBlock *InsertAtEnd) {
2006 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002007 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2008 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002009}
2010
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002011CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002012 const std::string &Name,
2013 Instruction *InsertBefore) {
2014 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002015 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2016 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002017}
2018
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002019CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002020 const std::string &Name,
2021 BasicBlock *InsertAtEnd) {
2022 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002023 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2024 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002025}
2026
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002027CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002028 const std::string &Name,
2029 BasicBlock *InsertAtEnd) {
2030 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002031 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002032 "Invalid cast");
2033
Chris Lattner03c49532007-01-15 02:27:26 +00002034 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002035 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2036 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002037}
2038
2039/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002040CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002041 const std::string &Name,
2042 Instruction *InsertBefore) {
2043 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002044 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002045 "Invalid cast");
2046
Chris Lattner03c49532007-01-15 02:27:26 +00002047 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002048 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2049 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002050}
2051
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002052CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002053 bool isSigned, const std::string &Name,
2054 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002055 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002056 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2057 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2058 Instruction::CastOps opcode =
2059 (SrcBits == DstBits ? Instruction::BitCast :
2060 (SrcBits > DstBits ? Instruction::Trunc :
2061 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002062 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002063}
2064
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002065CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002066 bool isSigned, const std::string &Name,
2067 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00002068 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002069 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2070 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2071 Instruction::CastOps opcode =
2072 (SrcBits == DstBits ? Instruction::BitCast :
2073 (SrcBits > DstBits ? Instruction::Trunc :
2074 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002075 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002076}
2077
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002078CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002079 const std::string &Name,
2080 Instruction *InsertBefore) {
2081 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2082 "Invalid cast");
2083 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2084 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2085 Instruction::CastOps opcode =
2086 (SrcBits == DstBits ? Instruction::BitCast :
2087 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002088 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002089}
2090
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002091CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002092 const std::string &Name,
2093 BasicBlock *InsertAtEnd) {
2094 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2095 "Invalid cast");
2096 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2097 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2098 Instruction::CastOps opcode =
2099 (SrcBits == DstBits ? Instruction::BitCast :
2100 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002101 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002102}
2103
Duncan Sands55e50902008-01-06 10:12:28 +00002104// Check whether it is valid to call getCastOpcode for these types.
2105// This routine must be kept in sync with getCastOpcode.
2106bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2107 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2108 return false;
2109
2110 if (SrcTy == DestTy)
2111 return true;
2112
2113 // Get the bit sizes, we'll need these
2114 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2115 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2116
2117 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002118 if (DestTy->isInteger()) { // Casting to integral
2119 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002120 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002121 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002122 return true;
2123 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002124 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002125 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002126 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002127 return isa<PointerType>(SrcTy);
2128 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002129 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2130 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002131 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002132 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002133 return true;
2134 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002135 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002136 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002137 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002138 return false;
2139 }
2140 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002141 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002142 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002143 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002144 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002145 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002146 return DestPTy->getBitWidth() == SrcBits;
2147 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002148 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2149 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002150 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002151 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002152 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002153 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002154 return false;
2155 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002156 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002157 return false;
2158 }
2159}
2160
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002161// Provide a way to get a "cast" where the cast opcode is inferred from the
2162// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002163// logic in the castIsValid function below. This axiom should hold:
2164// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2165// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002166// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002167// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002168Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002169CastInst::getCastOpcode(
2170 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002171 // Get the bit sizes, we'll need these
2172 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002173 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2174 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002175
Duncan Sands55e50902008-01-06 10:12:28 +00002176 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2177 "Only first class types are castable!");
2178
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002179 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002180 if (DestTy->isInteger()) { // Casting to integral
2181 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002182 if (DestBits < SrcBits)
2183 return Trunc; // int -> smaller int
2184 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002185 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002186 return SExt; // signed -> SEXT
2187 else
2188 return ZExt; // unsigned -> ZEXT
2189 } else {
2190 return BitCast; // Same size, No-op cast
2191 }
2192 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002193 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002194 return FPToSI; // FP -> sint
2195 else
2196 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002197 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002198 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002199 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002200 return BitCast; // Same size, no-op cast
2201 } else {
2202 assert(isa<PointerType>(SrcTy) &&
2203 "Casting from a value that is not first-class type");
2204 return PtrToInt; // ptr -> int
2205 }
2206 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002207 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002208 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002209 return SIToFP; // sint -> FP
2210 else
2211 return UIToFP; // uint -> FP
2212 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2213 if (DestBits < SrcBits) {
2214 return FPTrunc; // FP -> smaller FP
2215 } else if (DestBits > SrcBits) {
2216 return FPExt; // FP -> larger FP
2217 } else {
2218 return BitCast; // same size, no-op cast
2219 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002220 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002221 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002222 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002223 return BitCast; // same size, no-op cast
2224 } else {
2225 assert(0 && "Casting pointer or non-first class to float");
2226 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002227 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2228 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002229 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002230 "Casting vector to vector of different widths");
2231 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002232 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002233 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002234 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002235 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002236 }
2237 } else if (isa<PointerType>(DestTy)) {
2238 if (isa<PointerType>(SrcTy)) {
2239 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002240 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002241 return IntToPtr; // int -> ptr
2242 } else {
2243 assert(!"Casting pointer to other than pointer or int");
2244 }
2245 } else {
2246 assert(!"Casting to type that is not first-class");
2247 }
2248
2249 // If we fall through to here we probably hit an assertion cast above
2250 // and assertions are not turned on. Anything we return is an error, so
2251 // BitCast is as good a choice as any.
2252 return BitCast;
2253}
2254
2255//===----------------------------------------------------------------------===//
2256// CastInst SubClass Constructors
2257//===----------------------------------------------------------------------===//
2258
2259/// Check that the construction parameters for a CastInst are correct. This
2260/// could be broken out into the separate constructors but it is useful to have
2261/// it in one place and to eliminate the redundant code for getting the sizes
2262/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002263bool
2264CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002265
2266 // Check for type sanity on the arguments
2267 const Type *SrcTy = S->getType();
2268 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2269 return false;
2270
2271 // Get the size of the types in bits, we'll need this later
2272 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2273 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2274
2275 // Switch on the opcode provided
2276 switch (op) {
2277 default: return false; // This is an input error
2278 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002279 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002280 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002281 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002282 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002283 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002284 case Instruction::FPTrunc:
2285 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2286 SrcBitSize > DstBitSize;
2287 case Instruction::FPExt:
2288 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2289 SrcBitSize < DstBitSize;
2290 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002291 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002292 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2293 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2294 return SVTy->getElementType()->isInteger() &&
2295 DVTy->getElementType()->isFloatingPoint() &&
2296 SVTy->getNumElements() == DVTy->getNumElements();
2297 }
2298 }
Chris Lattner03c49532007-01-15 02:27:26 +00002299 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002300 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002302 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2303 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2304 return SVTy->getElementType()->isFloatingPoint() &&
2305 DVTy->getElementType()->isInteger() &&
2306 SVTy->getNumElements() == DVTy->getNumElements();
2307 }
2308 }
Chris Lattner03c49532007-01-15 02:27:26 +00002309 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002310 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002311 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002312 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002313 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002314 case Instruction::BitCast:
2315 // BitCast implies a no-op cast of type only. No bits change.
2316 // However, you can't cast pointers to anything but pointers.
2317 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2318 return false;
2319
Duncan Sands55e50902008-01-06 10:12:28 +00002320 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002321 // these cases, the cast is okay if the source and destination bit widths
2322 // are identical.
2323 return SrcBitSize == DstBitSize;
2324 }
2325}
2326
2327TruncInst::TruncInst(
2328 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2329) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002330 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002331}
2332
2333TruncInst::TruncInst(
2334 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2335) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002336 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002337}
2338
2339ZExtInst::ZExtInst(
2340 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2341) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002342 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002343}
2344
2345ZExtInst::ZExtInst(
2346 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2347) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002348 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002349}
2350SExtInst::SExtInst(
2351 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2352) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002353 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002354}
2355
Jeff Cohencc08c832006-12-02 02:22:01 +00002356SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2358) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002359 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002360}
2361
2362FPTruncInst::FPTruncInst(
2363 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2364) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002365 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002366}
2367
2368FPTruncInst::FPTruncInst(
2369 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2370) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002371 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002372}
2373
2374FPExtInst::FPExtInst(
2375 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2376) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002377 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378}
2379
2380FPExtInst::FPExtInst(
2381 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2382) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002383 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384}
2385
2386UIToFPInst::UIToFPInst(
2387 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2388) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002389 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002390}
2391
2392UIToFPInst::UIToFPInst(
2393 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2394) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002395 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002396}
2397
2398SIToFPInst::SIToFPInst(
2399 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2400) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002401 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402}
2403
2404SIToFPInst::SIToFPInst(
2405 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2406) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002407 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408}
2409
2410FPToUIInst::FPToUIInst(
2411 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2412) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002413 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002414}
2415
2416FPToUIInst::FPToUIInst(
2417 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2418) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002419 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420}
2421
2422FPToSIInst::FPToSIInst(
2423 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2424) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002425 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002426}
2427
2428FPToSIInst::FPToSIInst(
2429 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2430) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002431 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002432}
2433
2434PtrToIntInst::PtrToIntInst(
2435 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2436) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002437 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002438}
2439
2440PtrToIntInst::PtrToIntInst(
2441 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2442) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002443 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444}
2445
2446IntToPtrInst::IntToPtrInst(
2447 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2448) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002449 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450}
2451
2452IntToPtrInst::IntToPtrInst(
2453 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2454) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002455 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456}
2457
2458BitCastInst::BitCastInst(
2459 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2460) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002461 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002462}
2463
2464BitCastInst::BitCastInst(
2465 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2466) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002467 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002468}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002469
2470//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002471// CmpInst Classes
2472//===----------------------------------------------------------------------===//
2473
Nate Begemand2195702008-05-12 19:01:56 +00002474CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2475 Value *LHS, Value *RHS, const std::string &Name,
2476 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002477 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002478 OperandTraits<CmpInst>::op_begin(this),
2479 OperandTraits<CmpInst>::operands(this),
2480 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002481 Op<0>() = LHS;
2482 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002483 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002484 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002485}
Gabor Greiff6caff662008-05-10 08:32:32 +00002486
Nate Begemand2195702008-05-12 19:01:56 +00002487CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2488 Value *LHS, Value *RHS, const std::string &Name,
2489 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002490 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002491 OperandTraits<CmpInst>::op_begin(this),
2492 OperandTraits<CmpInst>::operands(this),
2493 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002494 Op<0>() = LHS;
2495 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002496 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002497 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002498}
2499
2500CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002501CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002502 const std::string &Name, Instruction *InsertBefore) {
2503 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002504 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002505 InsertBefore);
2506 }
Nate Begemand2195702008-05-12 19:01:56 +00002507 if (Op == Instruction::FCmp) {
2508 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2509 InsertBefore);
2510 }
2511 if (Op == Instruction::VICmp) {
2512 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2513 InsertBefore);
2514 }
2515 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2516 InsertBefore);
Reid Spencerd9436b62006-11-20 01:22:35 +00002517}
2518
2519CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002520CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002521 const std::string &Name, BasicBlock *InsertAtEnd) {
2522 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002523 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002524 InsertAtEnd);
2525 }
Nate Begemand2195702008-05-12 19:01:56 +00002526 if (Op == Instruction::FCmp) {
2527 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2528 InsertAtEnd);
2529 }
2530 if (Op == Instruction::VICmp) {
2531 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2532 InsertAtEnd);
2533 }
2534 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2535 InsertAtEnd);
Reid Spencerd9436b62006-11-20 01:22:35 +00002536}
2537
2538void CmpInst::swapOperands() {
2539 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2540 IC->swapOperands();
2541 else
2542 cast<FCmpInst>(this)->swapOperands();
2543}
2544
2545bool CmpInst::isCommutative() {
2546 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2547 return IC->isCommutative();
2548 return cast<FCmpInst>(this)->isCommutative();
2549}
2550
2551bool CmpInst::isEquality() {
2552 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2553 return IC->isEquality();
2554 return cast<FCmpInst>(this)->isEquality();
2555}
2556
2557
Dan Gohman4e724382008-05-31 02:47:54 +00002558CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002559 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002560 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002561 case ICMP_EQ: return ICMP_NE;
2562 case ICMP_NE: return ICMP_EQ;
2563 case ICMP_UGT: return ICMP_ULE;
2564 case ICMP_ULT: return ICMP_UGE;
2565 case ICMP_UGE: return ICMP_ULT;
2566 case ICMP_ULE: return ICMP_UGT;
2567 case ICMP_SGT: return ICMP_SLE;
2568 case ICMP_SLT: return ICMP_SGE;
2569 case ICMP_SGE: return ICMP_SLT;
2570 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002571
Dan Gohman4e724382008-05-31 02:47:54 +00002572 case FCMP_OEQ: return FCMP_UNE;
2573 case FCMP_ONE: return FCMP_UEQ;
2574 case FCMP_OGT: return FCMP_ULE;
2575 case FCMP_OLT: return FCMP_UGE;
2576 case FCMP_OGE: return FCMP_ULT;
2577 case FCMP_OLE: return FCMP_UGT;
2578 case FCMP_UEQ: return FCMP_ONE;
2579 case FCMP_UNE: return FCMP_OEQ;
2580 case FCMP_UGT: return FCMP_OLE;
2581 case FCMP_ULT: return FCMP_OGE;
2582 case FCMP_UGE: return FCMP_OLT;
2583 case FCMP_ULE: return FCMP_OGT;
2584 case FCMP_ORD: return FCMP_UNO;
2585 case FCMP_UNO: return FCMP_ORD;
2586 case FCMP_TRUE: return FCMP_FALSE;
2587 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002588 }
2589}
2590
Reid Spencer266e42b2006-12-23 06:05:41 +00002591ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2592 switch (pred) {
2593 default: assert(! "Unknown icmp predicate!");
2594 case ICMP_EQ: case ICMP_NE:
2595 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2596 return pred;
2597 case ICMP_UGT: return ICMP_SGT;
2598 case ICMP_ULT: return ICMP_SLT;
2599 case ICMP_UGE: return ICMP_SGE;
2600 case ICMP_ULE: return ICMP_SLE;
2601 }
2602}
2603
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002604ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2605 switch (pred) {
2606 default: assert(! "Unknown icmp predicate!");
2607 case ICMP_EQ: case ICMP_NE:
2608 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2609 return pred;
2610 case ICMP_SGT: return ICMP_UGT;
2611 case ICMP_SLT: return ICMP_ULT;
2612 case ICMP_SGE: return ICMP_UGE;
2613 case ICMP_SLE: return ICMP_ULE;
2614 }
2615}
2616
Reid Spencer266e42b2006-12-23 06:05:41 +00002617bool ICmpInst::isSignedPredicate(Predicate pred) {
2618 switch (pred) {
2619 default: assert(! "Unknown icmp predicate!");
2620 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2621 return true;
2622 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2623 case ICMP_UGE: case ICMP_ULE:
2624 return false;
2625 }
2626}
2627
Reid Spencer0286bc12007-02-28 22:00:54 +00002628/// Initialize a set of values that all satisfy the condition with C.
2629///
2630ConstantRange
2631ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2632 APInt Lower(C);
2633 APInt Upper(C);
2634 uint32_t BitWidth = C.getBitWidth();
2635 switch (pred) {
2636 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2637 case ICmpInst::ICMP_EQ: Upper++; break;
2638 case ICmpInst::ICMP_NE: Lower++; break;
2639 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2640 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2641 case ICmpInst::ICMP_UGT:
2642 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2643 break;
2644 case ICmpInst::ICMP_SGT:
2645 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2646 break;
2647 case ICmpInst::ICMP_ULE:
2648 Lower = APInt::getMinValue(BitWidth); Upper++;
2649 break;
2650 case ICmpInst::ICMP_SLE:
2651 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2652 break;
2653 case ICmpInst::ICMP_UGE:
2654 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2655 break;
2656 case ICmpInst::ICMP_SGE:
2657 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2658 break;
2659 }
2660 return ConstantRange(Lower, Upper);
2661}
2662
Dan Gohman4e724382008-05-31 02:47:54 +00002663CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002664 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002665 default: assert(!"Unknown cmp predicate!");
2666 case ICMP_EQ: case ICMP_NE:
2667 return pred;
2668 case ICMP_SGT: return ICMP_SLT;
2669 case ICMP_SLT: return ICMP_SGT;
2670 case ICMP_SGE: return ICMP_SLE;
2671 case ICMP_SLE: return ICMP_SGE;
2672 case ICMP_UGT: return ICMP_ULT;
2673 case ICMP_ULT: return ICMP_UGT;
2674 case ICMP_UGE: return ICMP_ULE;
2675 case ICMP_ULE: return ICMP_UGE;
2676
Reid Spencerd9436b62006-11-20 01:22:35 +00002677 case FCMP_FALSE: case FCMP_TRUE:
2678 case FCMP_OEQ: case FCMP_ONE:
2679 case FCMP_UEQ: case FCMP_UNE:
2680 case FCMP_ORD: case FCMP_UNO:
2681 return pred;
2682 case FCMP_OGT: return FCMP_OLT;
2683 case FCMP_OLT: return FCMP_OGT;
2684 case FCMP_OGE: return FCMP_OLE;
2685 case FCMP_OLE: return FCMP_OGE;
2686 case FCMP_UGT: return FCMP_ULT;
2687 case FCMP_ULT: return FCMP_UGT;
2688 case FCMP_UGE: return FCMP_ULE;
2689 case FCMP_ULE: return FCMP_UGE;
2690 }
2691}
2692
Reid Spencer266e42b2006-12-23 06:05:41 +00002693bool CmpInst::isUnsigned(unsigned short predicate) {
2694 switch (predicate) {
2695 default: return false;
2696 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2697 case ICmpInst::ICMP_UGE: return true;
2698 }
2699}
2700
2701bool CmpInst::isSigned(unsigned short predicate){
2702 switch (predicate) {
2703 default: return false;
2704 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2705 case ICmpInst::ICMP_SGE: return true;
2706 }
2707}
2708
2709bool CmpInst::isOrdered(unsigned short predicate) {
2710 switch (predicate) {
2711 default: return false;
2712 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2713 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2714 case FCmpInst::FCMP_ORD: return true;
2715 }
2716}
2717
2718bool CmpInst::isUnordered(unsigned short predicate) {
2719 switch (predicate) {
2720 default: return false;
2721 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2722 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2723 case FCmpInst::FCMP_UNO: return true;
2724 }
2725}
2726
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002727//===----------------------------------------------------------------------===//
2728// SwitchInst Implementation
2729//===----------------------------------------------------------------------===//
2730
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002731void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002732 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002733 ReservedSpace = 2+NumCases*2;
2734 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002735 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002736
Gabor Greif2d3024d2008-05-26 21:33:52 +00002737 OperandList[0] = Value;
2738 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002739}
2740
Chris Lattner2195fc42007-02-24 00:55:48 +00002741/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2742/// switch on and a default destination. The number of additional cases can
2743/// be specified here to make memory allocation more efficient. This
2744/// constructor can also autoinsert before another instruction.
2745SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2746 Instruction *InsertBefore)
2747 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2748 init(Value, Default, NumCases);
2749}
2750
2751/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2752/// switch on and a default destination. The number of additional cases can
2753/// be specified here to make memory allocation more efficient. This
2754/// constructor also autoinserts at the end of the specified BasicBlock.
2755SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2756 BasicBlock *InsertAtEnd)
2757 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2758 init(Value, Default, NumCases);
2759}
2760
Misha Brukmanb1c93172005-04-21 23:48:37 +00002761SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002762 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002763 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002764 Use *OL = OperandList, *InOL = SI.OperandList;
2765 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002766 OL[i] = InOL[i];
2767 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002768 }
2769}
2770
Gordon Henriksen14a55692007-12-10 02:14:30 +00002771SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002772 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002773}
2774
2775
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002776/// addCase - Add an entry to the switch instruction...
2777///
Chris Lattner47ac1872005-02-24 05:32:09 +00002778void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002779 unsigned OpNo = NumOperands;
2780 if (OpNo+2 > ReservedSpace)
2781 resizeOperands(0); // Get more space!
2782 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002783 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002784 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002785 OperandList[OpNo] = OnVal;
2786 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002787}
2788
2789/// removeCase - This method removes the specified successor from the switch
2790/// instruction. Note that this cannot be used to remove the default
2791/// destination (successor #0).
2792///
2793void SwitchInst::removeCase(unsigned idx) {
2794 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002795 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2796
2797 unsigned NumOps = getNumOperands();
2798 Use *OL = OperandList;
2799
2800 // Move everything after this operand down.
2801 //
2802 // FIXME: we could just swap with the end of the list, then erase. However,
2803 // client might not expect this to happen. The code as it is thrashes the
2804 // use/def lists, which is kinda lame.
2805 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2806 OL[i-2] = OL[i];
2807 OL[i-2+1] = OL[i+1];
2808 }
2809
2810 // Nuke the last value.
2811 OL[NumOps-2].set(0);
2812 OL[NumOps-2+1].set(0);
2813 NumOperands = NumOps-2;
2814}
2815
2816/// resizeOperands - resize operands - This adjusts the length of the operands
2817/// list according to the following behavior:
2818/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002819/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002820/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2821/// 3. If NumOps == NumOperands, trim the reserved space.
2822///
2823void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002824 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002825 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002826 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002827 } else if (NumOps*2 > NumOperands) {
2828 // No resize needed.
2829 if (ReservedSpace >= NumOps) return;
2830 } else if (NumOps == NumOperands) {
2831 if (ReservedSpace == NumOps) return;
2832 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002833 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002834 }
2835
2836 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002837 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002838 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002839 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002840 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002841 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002842 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002843 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002844}
2845
2846
2847BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2848 return getSuccessor(idx);
2849}
2850unsigned SwitchInst::getNumSuccessorsV() const {
2851 return getNumSuccessors();
2852}
2853void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2854 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002855}
Chris Lattnerf22be932004-10-15 23:52:53 +00002856
Devang Patel295711f2008-02-19 22:15:16 +00002857//===----------------------------------------------------------------------===//
2858// GetResultInst Implementation
2859//===----------------------------------------------------------------------===//
2860
Devang Patel666d4512008-02-20 19:10:47 +00002861GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002862 const std::string &Name,
2863 Instruction *InsertBef)
Gabor Greif0d42adf2008-05-13 07:09:08 +00002864 : UnaryInstruction(cast<StructType>(Aggregate->getType())
2865 ->getElementType(Index),
2866 GetResult, Aggregate, InsertBef),
2867 Idx(Index) {
2868 assert(isValidOperands(Aggregate, Index)
2869 && "Invalid GetResultInst operands!");
Devang Patel295711f2008-02-19 22:15:16 +00002870 setName(Name);
2871}
2872
Devang Patel666d4512008-02-20 19:10:47 +00002873bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2874 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002875 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002876
Devang Patelcf193b82008-02-20 19:39:41 +00002877 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2878 unsigned NumElements = STy->getNumElements();
Chris Lattnerb6917d22008-04-23 05:36:34 +00002879 if (Index >= NumElements || NumElements == 0)
Devang Patelcf193b82008-02-20 19:39:41 +00002880 return false;
2881
2882 // getresult aggregate value's element types are restricted to
2883 // avoid nested aggregates.
2884 for (unsigned i = 0; i < NumElements; ++i)
2885 if (!STy->getElementType(i)->isFirstClassType())
2886 return false;
2887
2888 // Otherwise, Aggregate is valid.
2889 return true;
2890 }
Devang Patel666d4512008-02-20 19:10:47 +00002891 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002892}
2893
Chris Lattnerf22be932004-10-15 23:52:53 +00002894// Define these methods here so vtables don't get emitted into every translation
2895// unit that uses these classes.
2896
2897GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002898 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002899}
2900
2901BinaryOperator *BinaryOperator::clone() const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002902 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002903}
2904
Chris Lattner0b490b02007-08-24 20:48:18 +00002905FCmpInst* FCmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002906 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002907}
2908ICmpInst* ICmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002909 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002910}
2911
Nate Begemand2195702008-05-12 19:01:56 +00002912VFCmpInst* VFCmpInst::clone() const {
2913 return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
2914}
2915VICmpInst* VICmpInst::clone() const {
2916 return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
2917}
2918
Dan Gohman0752bff2008-05-23 00:36:11 +00002919ExtractValueInst *ExtractValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002920 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002921}
2922InsertValueInst *InsertValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002923 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002924}
2925
2926
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002927MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2928AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2929FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2930LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2931StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2932CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2933CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2934CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2935CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2936CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2937CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2938CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2939CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2940CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2941CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2942CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2943CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002944CallInst *CallInst::clone() const {
2945 return new(getNumOperands()) CallInst(*this);
2946}
2947SelectInst *SelectInst::clone() const {
2948 return new(getNumOperands()) SelectInst(*this);
2949}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002950VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2951
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002952ExtractElementInst *ExtractElementInst::clone() const {
2953 return new ExtractElementInst(*this);
2954}
2955InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002956 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002957}
2958ShuffleVectorInst *ShuffleVectorInst::clone() const {
2959 return new ShuffleVectorInst(*this);
2960}
Chris Lattnerf22be932004-10-15 23:52:53 +00002961PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002962ReturnInst *ReturnInst::clone() const {
2963 return new(getNumOperands()) ReturnInst(*this);
2964}
2965BranchInst *BranchInst::clone() const {
2966 return new(getNumOperands()) BranchInst(*this);
2967}
Gabor Greiff6caff662008-05-10 08:32:32 +00002968SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002969InvokeInst *InvokeInst::clone() const {
2970 return new(getNumOperands()) InvokeInst(*this);
2971}
Chris Lattnerf22be932004-10-15 23:52:53 +00002972UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002973UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002974GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }