blob: 66caf5f9f0e58c067be68737da3072bd5553f877 [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}
Devang Patel4c758ea2008-09-25 21:00:45 +000044const AttrListPtr &CallSite::getAttributes() const {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000045 if (CallInst *CI = dyn_cast<CallInst>(I))
Devang Patel4c758ea2008-09-25 21:00:45 +000046 return CI->getAttributes();
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000047 else
Devang Patel4c758ea2008-09-25 21:00:45 +000048 return cast<InvokeInst>(I)->getAttributes();
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000049}
Devang Patel4c758ea2008-09-25 21:00:45 +000050void CallSite::setAttributes(const AttrListPtr &PAL) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000051 if (CallInst *CI = dyn_cast<CallInst>(I))
Devang Patel4c758ea2008-09-25 21:00:45 +000052 CI->setAttributes(PAL);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000053 else
Devang Patel4c758ea2008-09-25 21:00:45 +000054 cast<InvokeInst>(I)->setAttributes(PAL);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000055}
Devang Patelba3fa6c2008-09-23 23:03:40 +000056bool CallSite::paramHasAttr(uint16_t i, Attributes 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}
Duncan Sands78c88722008-07-08 08:38:44 +000075void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
76 if (CallInst *CI = dyn_cast<CallInst>(I))
77 CI->setDoesNotAccessMemory(doesNotAccessMemory);
78 else
79 cast<InvokeInst>(I)->setDoesNotAccessMemory(doesNotAccessMemory);
80}
Duncan Sands38ef3a82007-12-03 20:06:50 +000081bool CallSite::onlyReadsMemory() const {
82 if (CallInst *CI = dyn_cast<CallInst>(I))
83 return CI->onlyReadsMemory();
84 else
85 return cast<InvokeInst>(I)->onlyReadsMemory();
86}
Duncan Sands78c88722008-07-08 08:38:44 +000087void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
88 if (CallInst *CI = dyn_cast<CallInst>(I))
89 CI->setOnlyReadsMemory(onlyReadsMemory);
90 else
91 cast<InvokeInst>(I)->setOnlyReadsMemory(onlyReadsMemory);
92}
93bool CallSite::doesNotReturn() const {
94 if (CallInst *CI = dyn_cast<CallInst>(I))
95 return CI->doesNotReturn();
96 else
97 return cast<InvokeInst>(I)->doesNotReturn();
98}
99void CallSite::setDoesNotReturn(bool doesNotReturn) {
100 if (CallInst *CI = dyn_cast<CallInst>(I))
101 CI->setDoesNotReturn(doesNotReturn);
102 else
103 cast<InvokeInst>(I)->setDoesNotReturn(doesNotReturn);
104}
Duncan Sands3353ed02007-12-18 09:59:50 +0000105bool CallSite::doesNotThrow() const {
Duncan Sands8e4847e2007-12-16 15:51:49 +0000106 if (CallInst *CI = dyn_cast<CallInst>(I))
Duncan Sands3353ed02007-12-18 09:59:50 +0000107 return CI->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +0000108 else
Duncan Sands3353ed02007-12-18 09:59:50 +0000109 return cast<InvokeInst>(I)->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +0000110}
Duncan Sandsaa31b922007-12-19 21:13:37 +0000111void CallSite::setDoesNotThrow(bool doesNotThrow) {
112 if (CallInst *CI = dyn_cast<CallInst>(I))
113 CI->setDoesNotThrow(doesNotThrow);
114 else
115 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
116}
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000117
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +0000118bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +0000119 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
120 if (AI->get() == Arg)
121 return true;
122 return false;
123}
124
Gordon Henriksen14a55692007-12-10 02:14:30 +0000125//===----------------------------------------------------------------------===//
126// TerminatorInst Class
127//===----------------------------------------------------------------------===//
128
129// Out of line virtual method, so the vtable, etc has a home.
130TerminatorInst::~TerminatorInst() {
131}
132
Gabor Greiff6caff662008-05-10 08:32:32 +0000133//===----------------------------------------------------------------------===//
134// UnaryInstruction Class
135//===----------------------------------------------------------------------===//
136
Gordon Henriksen14a55692007-12-10 02:14:30 +0000137// Out of line virtual method, so the vtable, etc has a home.
138UnaryInstruction::~UnaryInstruction() {
139}
140
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000141//===----------------------------------------------------------------------===//
142// PHINode Class
143//===----------------------------------------------------------------------===//
144
145PHINode::PHINode(const PHINode &PN)
146 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000147 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000148 ReservedSpace(PN.getNumOperands()) {
149 Use *OL = OperandList;
150 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000151 OL[i] = PN.getOperand(i);
152 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000153 }
154}
155
Gordon Henriksen14a55692007-12-10 02:14:30 +0000156PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000157 if (OperandList)
158 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000159}
160
161// removeIncomingValue - Remove an incoming value. This is useful if a
162// predecessor basic block is deleted.
163Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
164 unsigned NumOps = getNumOperands();
165 Use *OL = OperandList;
166 assert(Idx*2 < NumOps && "BB not in PHI node!");
167 Value *Removed = OL[Idx*2];
168
169 // Move everything after this operand down.
170 //
171 // FIXME: we could just swap with the end of the list, then erase. However,
172 // client might not expect this to happen. The code as it is thrashes the
173 // use/def lists, which is kinda lame.
174 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
175 OL[i-2] = OL[i];
176 OL[i-2+1] = OL[i+1];
177 }
178
179 // Nuke the last value.
180 OL[NumOps-2].set(0);
181 OL[NumOps-2+1].set(0);
182 NumOperands = NumOps-2;
183
184 // If the PHI node is dead, because it has zero entries, nuke it now.
185 if (NumOps == 2 && DeletePHIIfEmpty) {
186 // If anyone is using this PHI, make them use a dummy value instead...
187 replaceAllUsesWith(UndefValue::get(getType()));
188 eraseFromParent();
189 }
190 return Removed;
191}
192
193/// resizeOperands - resize operands - This adjusts the length of the operands
194/// list according to the following behavior:
195/// 1. If NumOps == 0, grow the operand list in response to a push_back style
196/// of operation. This grows the number of ops by 1.5 times.
197/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
198/// 3. If NumOps == NumOperands, trim the reserved space.
199///
200void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000201 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000202 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000203 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000204 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
205 } else if (NumOps*2 > NumOperands) {
206 // No resize needed.
207 if (ReservedSpace >= NumOps) return;
208 } else if (NumOps == NumOperands) {
209 if (ReservedSpace == NumOps) return;
210 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000211 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000212 }
213
214 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000216 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000217 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000218 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000219 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000220}
221
Nate Begemanb3923212005-08-04 23:24:19 +0000222/// hasConstantValue - If the specified PHI node always merges together the same
223/// value, return the value, otherwise return null.
224///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000225Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000226 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000227 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000228 if (getIncomingValue(0) != this) // not X = phi X
229 return getIncomingValue(0);
230 else
231 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000232 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000233
Nate Begemanb3923212005-08-04 23:24:19 +0000234 // Otherwise if all of the incoming values are the same for the PHI, replace
235 // the PHI node with the incoming value.
236 //
237 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000238 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000239 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000240 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000241 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000242 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000243 if (InVal && getIncomingValue(i) != InVal)
244 return 0; // Not the same, bail out.
245 else
246 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000247 }
Nate Begemanb3923212005-08-04 23:24:19 +0000248
249 // The only case that could cause InVal to be null is if we have a PHI node
250 // that only has entries for itself. In this case, there is no entry into the
251 // loop, so kill the PHI.
252 //
253 if (InVal == 0) InVal = UndefValue::get(getType());
254
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000255 // If we have a PHI node like phi(X, undef, X), where X is defined by some
256 // instruction, we cannot always return X as the result of the PHI node. Only
257 // do this if X is not an instruction (thus it must dominate the PHI block),
258 // or if the client is prepared to deal with this possibility.
259 if (HasUndefInput && !AllowNonDominatingInstruction)
260 if (Instruction *IV = dyn_cast<Instruction>(InVal))
261 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000262 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000263 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000264 return 0; // Cannot guarantee that InVal dominates this PHINode.
265
Nate Begemanb3923212005-08-04 23:24:19 +0000266 // All of the incoming values are the same, return the value now.
267 return InVal;
268}
269
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000270
271//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272// CallInst Implementation
273//===----------------------------------------------------------------------===//
274
Gordon Henriksen14a55692007-12-10 02:14:30 +0000275CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000276}
277
Chris Lattner054ba2c2007-02-13 00:58:44 +0000278void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000279 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
280 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000281 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000282
Misha Brukmanb1c93172005-04-21 23:48:37 +0000283 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000285 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286
Chris Lattner054ba2c2007-02-13 00:58:44 +0000287 assert((NumParams == FTy->getNumParams() ||
288 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000289 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000290 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000291 assert((i >= FTy->getNumParams() ||
292 FTy->getParamType(i) == Params[i]->getType()) &&
293 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000294 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000295 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296}
297
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000298void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000299 assert(NumOperands == 3 && "NumOperands not set up?");
300 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000301 OL[0] = Func;
302 OL[1] = Actual1;
303 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000304
305 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000307 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000308
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000309 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000310 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000312 assert((0 >= FTy->getNumParams() ||
313 FTy->getParamType(0) == Actual1->getType()) &&
314 "Calling a function with a bad signature!");
315 assert((1 >= FTy->getNumParams() ||
316 FTy->getParamType(1) == Actual2->getType()) &&
317 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318}
319
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000320void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000321 assert(NumOperands == 2 && "NumOperands not set up?");
322 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000323 OL[0] = Func;
324 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000325
326 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000328 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000330 assert((FTy->getNumParams() == 1 ||
331 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000333 assert((0 == FTy->getNumParams() ||
334 FTy->getParamType(0) == Actual->getType()) &&
335 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336}
337
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000338void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000339 assert(NumOperands == 1 && "NumOperands not set up?");
340 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000341 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000342
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000343 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000344 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000345 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000346
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000347 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348}
349
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000351 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000352 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
353 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000354 Instruction::Call,
355 OperandTraits<CallInst>::op_end(this) - 2,
356 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000357 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000358 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000359}
360
361CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
362 BasicBlock *InsertAtEnd)
363 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
364 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000365 Instruction::Call,
366 OperandTraits<CallInst>::op_end(this) - 2,
367 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000368 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000369 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000370}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371CallInst::CallInst(Value *Func, const std::string &Name,
372 Instruction *InsertBefore)
373 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
374 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000375 Instruction::Call,
376 OperandTraits<CallInst>::op_end(this) - 1,
377 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000378 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000379 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000380}
381
382CallInst::CallInst(Value *Func, const std::string &Name,
383 BasicBlock *InsertAtEnd)
384 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
385 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000386 Instruction::Call,
387 OperandTraits<CallInst>::op_end(this) - 1,
388 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000389 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000390 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000391}
392
Misha Brukmanb1c93172005-04-21 23:48:37 +0000393CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000394 : Instruction(CI.getType(), Instruction::Call,
395 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000396 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000397 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000398 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000399 Use *OL = OperandList;
400 Use *InOL = CI.OperandList;
401 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000402 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000403}
404
Devang Patel4c758ea2008-09-25 21:00:45 +0000405void CallInst::addAttribute(unsigned i, Attributes attr) {
406 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000407 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000408 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000409}
410
Devang Patel4c758ea2008-09-25 21:00:45 +0000411void CallInst::removeAttribute(unsigned i, Attributes attr) {
412 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000413 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000414 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000415}
416
Devang Patelba3fa6c2008-09-23 23:03:40 +0000417bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000418 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000419 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000420 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000421 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000422 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000423}
424
Duncan Sands5208d1a2007-11-28 17:07:01 +0000425
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000426//===----------------------------------------------------------------------===//
427// InvokeInst Implementation
428//===----------------------------------------------------------------------===//
429
430void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000431 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000432 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
433 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000434 OL[0] = Fn;
435 OL[1] = IfNormal;
436 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000437 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000438 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000439 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000440
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000441 assert(((NumArgs == FTy->getNumParams()) ||
442 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000443 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000444
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000445 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000446 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000447 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000448 "Invoking a function with a bad signature!");
449
Gabor Greif2d3024d2008-05-26 21:33:52 +0000450 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000451 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000452}
453
Misha Brukmanb1c93172005-04-21 23:48:37 +0000454InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000455 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000456 OperandTraits<InvokeInst>::op_end(this)
457 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000458 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000459 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000460 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000461 Use *OL = OperandList, *InOL = II.OperandList;
462 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000463 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000464}
465
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000466BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
467 return getSuccessor(idx);
468}
469unsigned InvokeInst::getNumSuccessorsV() const {
470 return getNumSuccessors();
471}
472void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
473 return setSuccessor(idx, B);
474}
475
Devang Patelba3fa6c2008-09-23 23:03:40 +0000476bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000477 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000478 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000479 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000480 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000481 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000482}
483
Devang Patel4c758ea2008-09-25 21:00:45 +0000484void InvokeInst::addAttribute(unsigned i, Attributes attr) {
485 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000486 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000487 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000488}
489
Devang Patel4c758ea2008-09-25 21:00:45 +0000490void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
491 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000492 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000493 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000494}
495
Duncan Sands5208d1a2007-11-28 17:07:01 +0000496
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000497//===----------------------------------------------------------------------===//
498// ReturnInst Implementation
499//===----------------------------------------------------------------------===//
500
Chris Lattner2195fc42007-02-24 00:55:48 +0000501ReturnInst::ReturnInst(const ReturnInst &RI)
502 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000503 OperandTraits<ReturnInst>::op_end(this) -
504 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000505 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000506 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000507 Op<0>() = RI.Op<0>();
Chris Lattner2195fc42007-02-24 00:55:48 +0000508}
509
510ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000511 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000512 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
513 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000514 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000515 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000516}
517ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000518 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000519 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
520 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000521 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000522 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000523}
524ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000525 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000526 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000527}
528
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000529unsigned ReturnInst::getNumSuccessorsV() const {
530 return getNumSuccessors();
531}
532
Devang Patelae682fb2008-02-26 17:56:20 +0000533/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
534/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000535void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000536 assert(0 && "ReturnInst has no successors!");
537}
538
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000539BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
540 assert(0 && "ReturnInst has no successors!");
541 abort();
542 return 0;
543}
544
Devang Patel59643e52008-02-23 00:35:18 +0000545ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000546}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000547
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000548//===----------------------------------------------------------------------===//
549// UnwindInst Implementation
550//===----------------------------------------------------------------------===//
551
Chris Lattner2195fc42007-02-24 00:55:48 +0000552UnwindInst::UnwindInst(Instruction *InsertBefore)
553 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
554}
555UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
556 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
557}
558
559
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000560unsigned UnwindInst::getNumSuccessorsV() const {
561 return getNumSuccessors();
562}
563
564void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000565 assert(0 && "UnwindInst has no successors!");
566}
567
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000568BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
569 assert(0 && "UnwindInst has no successors!");
570 abort();
571 return 0;
572}
573
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000574//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000575// UnreachableInst Implementation
576//===----------------------------------------------------------------------===//
577
Chris Lattner2195fc42007-02-24 00:55:48 +0000578UnreachableInst::UnreachableInst(Instruction *InsertBefore)
579 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
580}
581UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
582 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
583}
584
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000585unsigned UnreachableInst::getNumSuccessorsV() const {
586 return getNumSuccessors();
587}
588
589void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
590 assert(0 && "UnwindInst has no successors!");
591}
592
593BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
594 assert(0 && "UnwindInst has no successors!");
595 abort();
596 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000597}
598
599//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000600// BranchInst Implementation
601//===----------------------------------------------------------------------===//
602
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000603void BranchInst::AssertOK() {
604 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000605 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000606 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000607}
608
Chris Lattner2195fc42007-02-24 00:55:48 +0000609BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000610 : TerminatorInst(Type::VoidTy, Instruction::Br,
611 OperandTraits<BranchInst>::op_end(this) - 1,
612 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000613 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000614 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000615}
616BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
617 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000618 : TerminatorInst(Type::VoidTy, Instruction::Br,
619 OperandTraits<BranchInst>::op_end(this) - 3,
620 3, InsertBefore) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000621 Op<0>() = IfTrue;
622 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000623 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000624#ifndef NDEBUG
625 AssertOK();
626#endif
627}
628
629BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000630 : TerminatorInst(Type::VoidTy, Instruction::Br,
631 OperandTraits<BranchInst>::op_end(this) - 1,
632 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000633 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000634 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000635}
636
637BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
638 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000639 : TerminatorInst(Type::VoidTy, Instruction::Br,
640 OperandTraits<BranchInst>::op_end(this) - 3,
641 3, InsertAtEnd) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000642 Op<0>() = IfTrue;
643 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000644 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000645#ifndef NDEBUG
646 AssertOK();
647#endif
648}
649
650
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000651BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000652 TerminatorInst(Type::VoidTy, Instruction::Br,
653 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
654 BI.getNumOperands()) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000655 OperandList[0] = BI.getOperand(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656 if (BI.getNumOperands() != 1) {
657 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000658 OperandList[1] = BI.getOperand(1);
659 OperandList[2] = BI.getOperand(2);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000660 }
661}
662
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000663BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
664 return getSuccessor(idx);
665}
666unsigned BranchInst::getNumSuccessorsV() const {
667 return getNumSuccessors();
668}
669void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
670 setSuccessor(idx, B);
671}
672
673
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000674//===----------------------------------------------------------------------===//
675// AllocationInst Implementation
676//===----------------------------------------------------------------------===//
677
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000678static Value *getAISize(Value *Amt) {
679 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000680 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000681 else {
682 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000683 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000684 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000685 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000686 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000687 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000688}
689
Misha Brukmanb1c93172005-04-21 23:48:37 +0000690AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000691 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000692 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000693 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000694 InsertBefore) {
695 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000696 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000697 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000698}
699
Misha Brukmanb1c93172005-04-21 23:48:37 +0000700AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000701 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000702 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000703 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000704 InsertAtEnd) {
705 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000706 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000707 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000708}
709
Gordon Henriksen14a55692007-12-10 02:14:30 +0000710// Out of line virtual method, so the vtable, etc has a home.
711AllocationInst::~AllocationInst() {
712}
713
Dan Gohmanaa583d72008-03-24 16:55:58 +0000714void AllocationInst::setAlignment(unsigned Align) {
715 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
716 SubclassData = Log2_32(Align) + 1;
717 assert(getAlignment() == Align && "Alignment representation error!");
718}
719
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000720bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000721 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
722 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000723 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000724}
725
726const Type *AllocationInst::getAllocatedType() const {
727 return getType()->getElementType();
728}
729
730AllocaInst::AllocaInst(const AllocaInst &AI)
731 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000732 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733}
734
735MallocInst::MallocInst(const MallocInst &MI)
736 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000737 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000738}
739
740//===----------------------------------------------------------------------===//
741// FreeInst Implementation
742//===----------------------------------------------------------------------===//
743
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000744void FreeInst::AssertOK() {
745 assert(isa<PointerType>(getOperand(0)->getType()) &&
746 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000747}
748
749FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000750 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000751 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000752}
753
754FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000755 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000756 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000757}
758
759
760//===----------------------------------------------------------------------===//
761// LoadInst Implementation
762//===----------------------------------------------------------------------===//
763
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000764void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000765 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000766 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000767}
768
769LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000770 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000771 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000772 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000773 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000774 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000775 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000776}
777
778LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000779 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000780 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000781 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000782 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000783 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000784 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000785}
786
787LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
788 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000789 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000790 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000791 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000792 setAlignment(0);
793 AssertOK();
794 setName(Name);
795}
796
797LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
798 unsigned Align, Instruction *InsertBef)
799 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
800 Load, Ptr, InsertBef) {
801 setVolatile(isVolatile);
802 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000803 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000804 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000805}
806
Dan Gohman68659282007-07-18 20:51:11 +0000807LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
808 unsigned Align, BasicBlock *InsertAE)
809 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
810 Load, Ptr, InsertAE) {
811 setVolatile(isVolatile);
812 setAlignment(Align);
813 AssertOK();
814 setName(Name);
815}
816
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000817LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
818 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000819 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000820 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000821 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000822 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000823 AssertOK();
824 setName(Name);
825}
826
827
828
829LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000830 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
831 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000832 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000833 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000834 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000835 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000836}
837
838LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000839 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
840 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000841 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000842 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000843 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000844 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000845}
846
847LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
848 Instruction *InsertBef)
849: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000850 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000851 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000852 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000853 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000854 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000855}
856
857LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
858 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000859 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
860 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000861 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000862 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000863 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000864 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000865}
866
Christopher Lamb84485702007-04-22 19:24:39 +0000867void LoadInst::setAlignment(unsigned Align) {
868 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
869 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
870}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000871
872//===----------------------------------------------------------------------===//
873// StoreInst Implementation
874//===----------------------------------------------------------------------===//
875
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000876void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000877 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000878 assert(isa<PointerType>(getOperand(1)->getType()) &&
879 "Ptr must have pointer type!");
880 assert(getOperand(0)->getType() ==
881 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000882 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000883}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000884
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000885
886StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000887 : Instruction(Type::VoidTy, Store,
888 OperandTraits<StoreInst>::op_begin(this),
889 OperandTraits<StoreInst>::operands(this),
890 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000891 Op<0>() = val;
892 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000893 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000894 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000895 AssertOK();
896}
897
898StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000899 : Instruction(Type::VoidTy, Store,
900 OperandTraits<StoreInst>::op_begin(this),
901 OperandTraits<StoreInst>::operands(this),
902 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000903 Op<0>() = val;
904 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000905 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000906 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000907 AssertOK();
908}
909
Misha Brukmanb1c93172005-04-21 23:48:37 +0000910StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000911 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000912 : Instruction(Type::VoidTy, Store,
913 OperandTraits<StoreInst>::op_begin(this),
914 OperandTraits<StoreInst>::operands(this),
915 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000916 Op<0>() = val;
917 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000918 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000919 setAlignment(0);
920 AssertOK();
921}
922
923StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
924 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000925 : Instruction(Type::VoidTy, Store,
926 OperandTraits<StoreInst>::op_begin(this),
927 OperandTraits<StoreInst>::operands(this),
928 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000929 Op<0>() = val;
930 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000931 setVolatile(isVolatile);
932 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000933 AssertOK();
934}
935
Misha Brukmanb1c93172005-04-21 23:48:37 +0000936StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000937 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000938 : Instruction(Type::VoidTy, Store,
939 OperandTraits<StoreInst>::op_begin(this),
940 OperandTraits<StoreInst>::operands(this),
941 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000942 Op<0>() = val;
943 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000944 setVolatile(isVolatile);
945 setAlignment(Align);
946 AssertOK();
947}
948
949StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000950 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000951 : Instruction(Type::VoidTy, Store,
952 OperandTraits<StoreInst>::op_begin(this),
953 OperandTraits<StoreInst>::operands(this),
954 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000955 Op<0>() = val;
956 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000957 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000958 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000959 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000960}
961
Christopher Lamb84485702007-04-22 19:24:39 +0000962void StoreInst::setAlignment(unsigned Align) {
963 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
964 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
965}
966
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000967//===----------------------------------------------------------------------===//
968// GetElementPtrInst Implementation
969//===----------------------------------------------------------------------===//
970
Christopher Lambedf07882007-12-17 01:12:55 +0000971static unsigned retrieveAddrSpace(const Value *Val) {
972 return cast<PointerType>(Val->getType())->getAddressSpace();
973}
974
Gabor Greif21ba1842008-06-06 20:28:12 +0000975void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Nate Begeman8a4afd92008-07-25 17:24:13 +0000976 const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000977 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
978 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000979 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000980
Chris Lattner79807c3d2007-01-31 19:47:18 +0000981 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000982 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +0000983
984 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000985}
986
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +0000987void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000988 assert(NumOperands == 2 && "NumOperands not initialized?");
989 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000990 OL[0] = Ptr;
991 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +0000992
993 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000994}
995
Gabor Greiff6caff662008-05-10 08:32:32 +0000996GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +0000997 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +0000998 OperandTraits<GetElementPtrInst>::op_end(this)
999 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001000 GEPI.getNumOperands()) {
1001 Use *OL = OperandList;
1002 Use *GEPIOL = GEPI.OperandList;
1003 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001004 OL[i] = GEPIOL[i];
Gabor Greiff6caff662008-05-10 08:32:32 +00001005}
1006
Chris Lattner82981202005-05-03 05:43:30 +00001007GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1008 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001009 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001010 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001011 GetElementPtr,
1012 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1013 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001014 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001015}
1016
1017GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1018 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001019 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001020 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001021 GetElementPtr,
1022 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1023 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001024 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001025}
1026
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001027// getIndexedType - Returns the type of the element that would be loaded with
1028// a load instruction with the specified parameters.
1029//
Matthijs Kooijman04468622008-07-29 08:46:11 +00001030// The Idxs pointer should point to a continuous piece of memory containing the
1031// indices, either as Value* or uint64_t.
1032//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001033// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001034// pointer type.
1035//
Matthijs Kooijman04468622008-07-29 08:46:11 +00001036template <typename IndexTy>
1037static const Type* getIndexedTypeInternal(const Type *Ptr,
1038 IndexTy const *Idxs,
1039 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001040 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1041 if (!PTy) return 0; // Type isn't a pointer type!
1042 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001043
1044 // Handle the special case of the empty set index set...
Dan Gohman12fce772008-05-15 19:50:34 +00001045 if (NumIdx == 0)
1046 return Agg;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001047
Dan Gohman1ecaf452008-05-31 00:58:22 +00001048 unsigned CurIdx = 1;
1049 for (; CurIdx != NumIdx; ++CurIdx) {
1050 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1051 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001052 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001053 if (!CT->indexValid(Index)) return 0;
1054 Agg = CT->getTypeAtIndex(Index);
1055
1056 // If the new type forwards to another type, then it is in the middle
1057 // of being refined to another type (and hence, may have dropped all
1058 // references to what it was using before). So, use the new forwarded
1059 // type.
1060 if (const Type *Ty = Agg->getForwardedType())
1061 Agg = Ty;
1062 }
1063 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001064}
1065
Matthijs Kooijman04468622008-07-29 08:46:11 +00001066const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1067 Value* const *Idxs,
1068 unsigned NumIdx) {
1069 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1070}
1071
1072const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1073 uint64_t const *Idxs,
1074 unsigned NumIdx) {
1075 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1076}
1077
Chris Lattner82981202005-05-03 05:43:30 +00001078const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1079 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1080 if (!PTy) return 0; // Type isn't a pointer type!
1081
1082 // Check the pointer index.
1083 if (!PTy->indexValid(Idx)) return 0;
1084
Chris Lattnerc2233332005-05-03 16:44:45 +00001085 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001086}
1087
Chris Lattner45f15572007-04-14 00:12:57 +00001088
1089/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1090/// zeros. If so, the result pointer and the first operand have the same
1091/// value, just potentially different types.
1092bool GetElementPtrInst::hasAllZeroIndices() const {
1093 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1094 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1095 if (!CI->isZero()) return false;
1096 } else {
1097 return false;
1098 }
1099 }
1100 return true;
1101}
1102
Chris Lattner27058292007-04-27 20:35:56 +00001103/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1104/// constant integers. If so, the result pointer and the first operand have
1105/// a constant offset between them.
1106bool GetElementPtrInst::hasAllConstantIndices() const {
1107 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1108 if (!isa<ConstantInt>(getOperand(i)))
1109 return false;
1110 }
1111 return true;
1112}
1113
Chris Lattner45f15572007-04-14 00:12:57 +00001114
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001115//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001116// ExtractElementInst Implementation
1117//===----------------------------------------------------------------------===//
1118
1119ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001120 const std::string &Name,
1121 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001122 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001123 ExtractElement,
1124 OperandTraits<ExtractElementInst>::op_begin(this),
1125 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001126 assert(isValidOperands(Val, Index) &&
1127 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001128 Op<0>() = Val;
1129 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001130 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001131}
1132
Chris Lattner65511ff2006-10-05 06:24:58 +00001133ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1134 const std::string &Name,
1135 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001136 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001137 ExtractElement,
1138 OperandTraits<ExtractElementInst>::op_begin(this),
1139 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001140 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001141 assert(isValidOperands(Val, Index) &&
1142 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001143 Op<0>() = Val;
1144 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001145 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001146}
1147
1148
Robert Bocchino23004482006-01-10 19:05:34 +00001149ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001150 const std::string &Name,
1151 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001152 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001153 ExtractElement,
1154 OperandTraits<ExtractElementInst>::op_begin(this),
1155 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001156 assert(isValidOperands(Val, Index) &&
1157 "Invalid extractelement instruction operands!");
1158
Gabor Greif2d3024d2008-05-26 21:33:52 +00001159 Op<0>() = Val;
1160 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001161 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001162}
1163
Chris Lattner65511ff2006-10-05 06:24:58 +00001164ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1165 const std::string &Name,
1166 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001167 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001168 ExtractElement,
1169 OperandTraits<ExtractElementInst>::op_begin(this),
1170 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001171 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001172 assert(isValidOperands(Val, Index) &&
1173 "Invalid extractelement instruction operands!");
1174
Gabor Greif2d3024d2008-05-26 21:33:52 +00001175 Op<0>() = Val;
1176 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001177 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001178}
1179
1180
Chris Lattner54865b32006-04-08 04:05:48 +00001181bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001182 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001183 return false;
1184 return true;
1185}
1186
1187
Robert Bocchino23004482006-01-10 19:05:34 +00001188//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001189// InsertElementInst Implementation
1190//===----------------------------------------------------------------------===//
1191
Chris Lattner0875d942006-04-14 22:20:32 +00001192InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001193 : Instruction(IE.getType(), InsertElement,
1194 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001195 Op<0>() = IE.Op<0>();
1196 Op<1>() = IE.Op<1>();
1197 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001198}
Chris Lattner54865b32006-04-08 04:05:48 +00001199InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001200 const std::string &Name,
1201 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001202 : Instruction(Vec->getType(), InsertElement,
1203 OperandTraits<InsertElementInst>::op_begin(this),
1204 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001205 assert(isValidOperands(Vec, Elt, Index) &&
1206 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001207 Op<0>() = Vec;
1208 Op<1>() = Elt;
1209 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001210 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001211}
1212
Chris Lattner65511ff2006-10-05 06:24:58 +00001213InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1214 const std::string &Name,
1215 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001216 : Instruction(Vec->getType(), InsertElement,
1217 OperandTraits<InsertElementInst>::op_begin(this),
1218 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001219 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001220 assert(isValidOperands(Vec, Elt, Index) &&
1221 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001222 Op<0>() = Vec;
1223 Op<1>() = Elt;
1224 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001225 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001226}
1227
1228
Chris Lattner54865b32006-04-08 04:05:48 +00001229InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001230 const std::string &Name,
1231 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001232 : Instruction(Vec->getType(), InsertElement,
1233 OperandTraits<InsertElementInst>::op_begin(this),
1234 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001235 assert(isValidOperands(Vec, Elt, Index) &&
1236 "Invalid insertelement instruction operands!");
1237
Gabor Greif2d3024d2008-05-26 21:33:52 +00001238 Op<0>() = Vec;
1239 Op<1>() = Elt;
1240 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001241 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001242}
1243
Chris Lattner65511ff2006-10-05 06:24:58 +00001244InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1245 const std::string &Name,
1246 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001247: Instruction(Vec->getType(), InsertElement,
1248 OperandTraits<InsertElementInst>::op_begin(this),
1249 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001250 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001251 assert(isValidOperands(Vec, Elt, Index) &&
1252 "Invalid insertelement instruction operands!");
1253
Gabor Greif2d3024d2008-05-26 21:33:52 +00001254 Op<0>() = Vec;
1255 Op<1>() = Elt;
1256 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001257 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001258}
1259
Chris Lattner54865b32006-04-08 04:05:48 +00001260bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1261 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001262 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001263 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001264
Reid Spencerd84d35b2007-02-15 02:26:10 +00001265 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001266 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001267
Reid Spencer8d9336d2006-12-31 05:26:44 +00001268 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001269 return false; // Third operand of insertelement must be uint.
1270 return true;
1271}
1272
1273
Robert Bocchinoca27f032006-01-17 20:07:22 +00001274//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001275// ShuffleVectorInst Implementation
1276//===----------------------------------------------------------------------===//
1277
Chris Lattner0875d942006-04-14 22:20:32 +00001278ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001279 : Instruction(SV.getType(), ShuffleVector,
1280 OperandTraits<ShuffleVectorInst>::op_begin(this),
1281 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001282 Op<0>() = SV.Op<0>();
1283 Op<1>() = SV.Op<1>();
1284 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001285}
1286
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001287ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1288 const std::string &Name,
1289 Instruction *InsertBefore)
Mon P Wang25f01062008-11-10 04:46:22 +00001290: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1291 cast<VectorType>(Mask->getType())->getNumElements()),
1292 ShuffleVector,
1293 OperandTraits<ShuffleVectorInst>::op_begin(this),
1294 OperandTraits<ShuffleVectorInst>::operands(this),
1295 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001296 assert(isValidOperands(V1, V2, Mask) &&
1297 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001298 Op<0>() = V1;
1299 Op<1>() = V2;
1300 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001301 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001302}
1303
1304ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Mon P Wang25f01062008-11-10 04:46:22 +00001305 const std::string &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001306 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001307 : Instruction(V1->getType(), ShuffleVector,
1308 OperandTraits<ShuffleVectorInst>::op_begin(this),
1309 OperandTraits<ShuffleVectorInst>::operands(this),
1310 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001311 assert(isValidOperands(V1, V2, Mask) &&
1312 "Invalid shuffle vector instruction operands!");
1313
Gabor Greif2d3024d2008-05-26 21:33:52 +00001314 Op<0>() = V1;
1315 Op<1>() = V2;
1316 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001317 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001318}
1319
Mon P Wang25f01062008-11-10 04:46:22 +00001320bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001321 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001322 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001323 return false;
1324
1325 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1326 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Mon P Wang25f01062008-11-10 04:46:22 +00001327 MaskTy->getElementType() != Type::Int32Ty)
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001328 return false;
1329 return true;
1330}
1331
Chris Lattnerf724e342008-03-02 05:28:33 +00001332/// getMaskValue - Return the index from the shuffle mask for the specified
1333/// output result. This is either -1 if the element is undef or a number less
1334/// than 2*numelements.
1335int ShuffleVectorInst::getMaskValue(unsigned i) const {
1336 const Constant *Mask = cast<Constant>(getOperand(2));
1337 if (isa<UndefValue>(Mask)) return -1;
1338 if (isa<ConstantAggregateZero>(Mask)) return 0;
1339 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1340 assert(i < MaskCV->getNumOperands() && "Index out of range");
1341
1342 if (isa<UndefValue>(MaskCV->getOperand(i)))
1343 return -1;
1344 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1345}
1346
Dan Gohman12fce772008-05-15 19:50:34 +00001347//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001348// InsertValueInst Class
1349//===----------------------------------------------------------------------===//
1350
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001351void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1352 unsigned NumIdx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001353 assert(NumOperands == 2 && "NumOperands not initialized?");
1354 Op<0>() = Agg;
1355 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001356
Dan Gohman1ecaf452008-05-31 00:58:22 +00001357 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001358 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001359}
1360
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001361void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1362 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001363 assert(NumOperands == 2 && "NumOperands not initialized?");
1364 Op<0>() = Agg;
1365 Op<1>() = Val;
1366
1367 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001368 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001369}
1370
1371InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001372 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001373 OperandTraits<InsertValueInst>::op_begin(this), 2),
1374 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001375 Op<0>() = IVI.getOperand(0);
1376 Op<1>() = IVI.getOperand(1);
Dan Gohman0752bff2008-05-23 00:36:11 +00001377}
1378
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001379InsertValueInst::InsertValueInst(Value *Agg,
1380 Value *Val,
1381 unsigned Idx,
1382 const std::string &Name,
1383 Instruction *InsertBefore)
1384 : Instruction(Agg->getType(), InsertValue,
1385 OperandTraits<InsertValueInst>::op_begin(this),
1386 2, InsertBefore) {
1387 init(Agg, Val, Idx, Name);
1388}
1389
1390InsertValueInst::InsertValueInst(Value *Agg,
1391 Value *Val,
1392 unsigned Idx,
1393 const std::string &Name,
1394 BasicBlock *InsertAtEnd)
1395 : Instruction(Agg->getType(), InsertValue,
1396 OperandTraits<InsertValueInst>::op_begin(this),
1397 2, InsertAtEnd) {
1398 init(Agg, Val, Idx, Name);
1399}
1400
Dan Gohman0752bff2008-05-23 00:36:11 +00001401//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001402// ExtractValueInst Class
1403//===----------------------------------------------------------------------===//
1404
Gabor Greifcbcc4952008-06-06 21:06:32 +00001405void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Gabor Greif21ba1842008-06-06 20:28:12 +00001406 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001407 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001408
Dan Gohman1ecaf452008-05-31 00:58:22 +00001409 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001410 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001411}
1412
Gabor Greifcbcc4952008-06-06 21:06:32 +00001413void ExtractValueInst::init(unsigned Idx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001414 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001415
1416 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001417 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001418}
1419
1420ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001421 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001422 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001423}
1424
Dan Gohman12fce772008-05-15 19:50:34 +00001425// getIndexedType - Returns the type of the element that would be extracted
1426// with an extractvalue instruction with the specified parameters.
1427//
1428// A null type is returned if the indices are invalid for the specified
1429// pointer type.
1430//
1431const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001432 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001433 unsigned NumIdx) {
1434 unsigned CurIdx = 0;
1435 for (; CurIdx != NumIdx; ++CurIdx) {
1436 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001437 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1438 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001439 if (!CT->indexValid(Index)) return 0;
1440 Agg = CT->getTypeAtIndex(Index);
1441
1442 // If the new type forwards to another type, then it is in the middle
1443 // of being refined to another type (and hence, may have dropped all
1444 // references to what it was using before). So, use the new forwarded
1445 // type.
1446 if (const Type *Ty = Agg->getForwardedType())
1447 Agg = Ty;
1448 }
1449 return CurIdx == NumIdx ? Agg : 0;
1450}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001451
Dan Gohman4a3125b2008-06-17 21:07:55 +00001452const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001453 unsigned Idx) {
1454 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001455}
1456
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001457//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001458// BinaryOperator Class
1459//===----------------------------------------------------------------------===//
1460
Chris Lattner2195fc42007-02-24 00:55:48 +00001461BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1462 const Type *Ty, const std::string &Name,
1463 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001464 : Instruction(Ty, iType,
1465 OperandTraits<BinaryOperator>::op_begin(this),
1466 OperandTraits<BinaryOperator>::operands(this),
1467 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001468 Op<0>() = S1;
1469 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001470 init(iType);
1471 setName(Name);
1472}
1473
1474BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1475 const Type *Ty, const std::string &Name,
1476 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001477 : Instruction(Ty, iType,
1478 OperandTraits<BinaryOperator>::op_begin(this),
1479 OperandTraits<BinaryOperator>::operands(this),
1480 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001481 Op<0>() = S1;
1482 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001483 init(iType);
1484 setName(Name);
1485}
1486
1487
1488void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001489 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001490 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001491 assert(LHS->getType() == RHS->getType() &&
1492 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001493#ifndef NDEBUG
1494 switch (iType) {
1495 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001496 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001497 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001498 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001499 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001500 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001501 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001502 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001503 case UDiv:
1504 case SDiv:
1505 assert(getType() == LHS->getType() &&
1506 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001507 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1508 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001509 "Incorrect operand type (not integer) for S/UDIV");
1510 break;
1511 case FDiv:
1512 assert(getType() == LHS->getType() &&
1513 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001514 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1515 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001516 && "Incorrect operand type (not floating point) for FDIV");
1517 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001518 case URem:
1519 case SRem:
1520 assert(getType() == LHS->getType() &&
1521 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001522 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1523 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001524 "Incorrect operand type (not integer) for S/UREM");
1525 break;
1526 case FRem:
1527 assert(getType() == LHS->getType() &&
1528 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001529 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1530 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001531 && "Incorrect operand type (not floating point) for FREM");
1532 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001533 case Shl:
1534 case LShr:
1535 case AShr:
1536 assert(getType() == LHS->getType() &&
1537 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001538 assert((getType()->isInteger() ||
1539 (isa<VectorType>(getType()) &&
1540 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1541 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001542 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001543 case And: case Or:
1544 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001545 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001546 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001547 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001548 (isa<VectorType>(getType()) &&
1549 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001550 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001551 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001552 default:
1553 break;
1554 }
1555#endif
1556}
1557
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001558BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001559 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001560 Instruction *InsertBefore) {
1561 assert(S1->getType() == S2->getType() &&
1562 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001563 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001564}
1565
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001566BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001567 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001568 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001569 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001570 InsertAtEnd->getInstList().push_back(Res);
1571 return Res;
1572}
1573
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001574BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001575 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001576 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1577 return new BinaryOperator(Instruction::Sub,
1578 zero, Op,
1579 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001580}
1581
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001582BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001583 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001584 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1585 return new BinaryOperator(Instruction::Sub,
1586 zero, Op,
1587 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001588}
1589
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001590BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001591 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001592 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001593 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001594 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001595 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001596 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001597 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001598 }
1599
1600 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001601 Op->getType(), Name, InsertBefore);
1602}
1603
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001604BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001605 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001606 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001607 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001608 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001609 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001610 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001611 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001612 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001613 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001614 }
1615
1616 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001617 Op->getType(), Name, InsertAtEnd);
1618}
1619
1620
1621// isConstantAllOnes - Helper function for several functions below
1622static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001623 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1624 return CI->isAllOnesValue();
1625 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1626 return CV->isAllOnesValue();
1627 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001628}
1629
1630bool BinaryOperator::isNeg(const Value *V) {
1631 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1632 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001633 return Bop->getOperand(0) ==
1634 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001635 return false;
1636}
1637
1638bool BinaryOperator::isNot(const Value *V) {
1639 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1640 return (Bop->getOpcode() == Instruction::Xor &&
1641 (isConstantAllOnes(Bop->getOperand(1)) ||
1642 isConstantAllOnes(Bop->getOperand(0))));
1643 return false;
1644}
1645
Chris Lattner2c7d1772005-04-24 07:28:37 +00001646Value *BinaryOperator::getNegArgument(Value *BinOp) {
1647 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1648 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001649}
1650
Chris Lattner2c7d1772005-04-24 07:28:37 +00001651const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1652 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001653}
1654
Chris Lattner2c7d1772005-04-24 07:28:37 +00001655Value *BinaryOperator::getNotArgument(Value *BinOp) {
1656 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1657 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1658 Value *Op0 = BO->getOperand(0);
1659 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001660 if (isConstantAllOnes(Op0)) return Op1;
1661
1662 assert(isConstantAllOnes(Op1));
1663 return Op0;
1664}
1665
Chris Lattner2c7d1772005-04-24 07:28:37 +00001666const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1667 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001668}
1669
1670
1671// swapOperands - Exchange the two operands to this instruction. This
1672// instruction is safe to use on any binary instruction and does not
1673// modify the semantics of the instruction. If the instruction is
1674// order dependent (SetLT f.e.) the opcode is changed.
1675//
1676bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001677 if (!isCommutative())
1678 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001679 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001680 return false;
1681}
1682
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001683//===----------------------------------------------------------------------===//
1684// CastInst Class
1685//===----------------------------------------------------------------------===//
1686
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001687// Just determine if this cast only deals with integral->integral conversion.
1688bool CastInst::isIntegerCast() const {
1689 switch (getOpcode()) {
1690 default: return false;
1691 case Instruction::ZExt:
1692 case Instruction::SExt:
1693 case Instruction::Trunc:
1694 return true;
1695 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001696 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001697 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001698}
1699
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001700bool CastInst::isLosslessCast() const {
1701 // Only BitCast can be lossless, exit fast if we're not BitCast
1702 if (getOpcode() != Instruction::BitCast)
1703 return false;
1704
1705 // Identity cast is always lossless
1706 const Type* SrcTy = getOperand(0)->getType();
1707 const Type* DstTy = getType();
1708 if (SrcTy == DstTy)
1709 return true;
1710
Reid Spencer8d9336d2006-12-31 05:26:44 +00001711 // Pointer to pointer is always lossless.
1712 if (isa<PointerType>(SrcTy))
1713 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001714 return false; // Other types have no identity values
1715}
1716
1717/// This function determines if the CastInst does not require any bits to be
1718/// changed in order to effect the cast. Essentially, it identifies cases where
1719/// no code gen is necessary for the cast, hence the name no-op cast. For
1720/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001721/// # bitcast i32* %x to i8*
1722/// # bitcast <2 x i32> %x to <4 x i16>
1723/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001724/// @brief Determine if a cast is a no-op.
1725bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1726 switch (getOpcode()) {
1727 default:
1728 assert(!"Invalid CastOp");
1729 case Instruction::Trunc:
1730 case Instruction::ZExt:
1731 case Instruction::SExt:
1732 case Instruction::FPTrunc:
1733 case Instruction::FPExt:
1734 case Instruction::UIToFP:
1735 case Instruction::SIToFP:
1736 case Instruction::FPToUI:
1737 case Instruction::FPToSI:
1738 return false; // These always modify bits
1739 case Instruction::BitCast:
1740 return true; // BitCast never modifies bits.
1741 case Instruction::PtrToInt:
1742 return IntPtrTy->getPrimitiveSizeInBits() ==
1743 getType()->getPrimitiveSizeInBits();
1744 case Instruction::IntToPtr:
1745 return IntPtrTy->getPrimitiveSizeInBits() ==
1746 getOperand(0)->getType()->getPrimitiveSizeInBits();
1747 }
1748}
1749
1750/// This function determines if a pair of casts can be eliminated and what
1751/// opcode should be used in the elimination. This assumes that there are two
1752/// instructions like this:
1753/// * %F = firstOpcode SrcTy %x to MidTy
1754/// * %S = secondOpcode MidTy %F to DstTy
1755/// The function returns a resultOpcode so these two casts can be replaced with:
1756/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1757/// If no such cast is permited, the function returns 0.
1758unsigned CastInst::isEliminableCastPair(
1759 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1760 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1761{
1762 // Define the 144 possibilities for these two cast instructions. The values
1763 // in this matrix determine what to do in a given situation and select the
1764 // case in the switch below. The rows correspond to firstOp, the columns
1765 // correspond to secondOp. In looking at the table below, keep in mind
1766 // the following cast properties:
1767 //
1768 // Size Compare Source Destination
1769 // Operator Src ? Size Type Sign Type Sign
1770 // -------- ------------ ------------------- ---------------------
1771 // TRUNC > Integer Any Integral Any
1772 // ZEXT < Integral Unsigned Integer Any
1773 // SEXT < Integral Signed Integer Any
1774 // FPTOUI n/a FloatPt n/a Integral Unsigned
1775 // FPTOSI n/a FloatPt n/a Integral Signed
1776 // UITOFP n/a Integral Unsigned FloatPt n/a
1777 // SITOFP n/a Integral Signed FloatPt n/a
1778 // FPTRUNC > FloatPt n/a FloatPt n/a
1779 // FPEXT < FloatPt n/a FloatPt n/a
1780 // PTRTOINT n/a Pointer n/a Integral Unsigned
1781 // INTTOPTR n/a Integral Unsigned Pointer n/a
1782 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001783 //
1784 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1785 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1786 // into "fptoui double to ulong", but this loses information about the range
1787 // of the produced value (we no longer know the top-part is all zeros).
1788 // Further this conversion is often much more expensive for typical hardware,
1789 // and causes issues when building libgcc. We disallow fptosi+sext for the
1790 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001791 const unsigned numCastOps =
1792 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1793 static const uint8_t CastResults[numCastOps][numCastOps] = {
1794 // T F F U S F F P I B -+
1795 // R Z S P P I I T P 2 N T |
1796 // U E E 2 2 2 2 R E I T C +- secondOp
1797 // N X X U S F F N X N 2 V |
1798 // C T T I I P P C T T P T -+
1799 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1800 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1801 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001802 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1803 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001804 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1805 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1806 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1807 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1808 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1809 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1810 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1811 };
1812
1813 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1814 [secondOp-Instruction::CastOpsBegin];
1815 switch (ElimCase) {
1816 case 0:
1817 // categorically disallowed
1818 return 0;
1819 case 1:
1820 // allowed, use first cast's opcode
1821 return firstOp;
1822 case 2:
1823 // allowed, use second cast's opcode
1824 return secondOp;
1825 case 3:
1826 // no-op cast in second op implies firstOp as long as the DestTy
1827 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001828 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001829 return firstOp;
1830 return 0;
1831 case 4:
1832 // no-op cast in second op implies firstOp as long as the DestTy
1833 // is floating point
1834 if (DstTy->isFloatingPoint())
1835 return firstOp;
1836 return 0;
1837 case 5:
1838 // no-op cast in first op implies secondOp as long as the SrcTy
1839 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001840 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001841 return secondOp;
1842 return 0;
1843 case 6:
1844 // no-op cast in first op implies secondOp as long as the SrcTy
1845 // is a floating point
1846 if (SrcTy->isFloatingPoint())
1847 return secondOp;
1848 return 0;
1849 case 7: {
1850 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1851 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1852 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1853 if (MidSize >= PtrSize)
1854 return Instruction::BitCast;
1855 return 0;
1856 }
1857 case 8: {
1858 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1859 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1860 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1861 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1862 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1863 if (SrcSize == DstSize)
1864 return Instruction::BitCast;
1865 else if (SrcSize < DstSize)
1866 return firstOp;
1867 return secondOp;
1868 }
1869 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1870 return Instruction::ZExt;
1871 case 10:
1872 // fpext followed by ftrunc is allowed if the bit size returned to is
1873 // the same as the original, in which case its just a bitcast
1874 if (SrcTy == DstTy)
1875 return Instruction::BitCast;
1876 return 0; // If the types are not the same we can't eliminate it.
1877 case 11:
1878 // bitcast followed by ptrtoint is allowed as long as the bitcast
1879 // is a pointer to pointer cast.
1880 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1881 return secondOp;
1882 return 0;
1883 case 12:
1884 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1885 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1886 return firstOp;
1887 return 0;
1888 case 13: {
1889 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1890 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1891 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1892 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1893 if (SrcSize <= PtrSize && SrcSize == DstSize)
1894 return Instruction::BitCast;
1895 return 0;
1896 }
1897 case 99:
1898 // cast combination can't happen (error in input). This is for all cases
1899 // where the MidTy is not the same for the two cast instructions.
1900 assert(!"Invalid Cast Combination");
1901 return 0;
1902 default:
1903 assert(!"Error in CastResults table!!!");
1904 return 0;
1905 }
1906 return 0;
1907}
1908
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001909CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001910 const std::string &Name, Instruction *InsertBefore) {
1911 // Construct and return the appropriate CastInst subclass
1912 switch (op) {
1913 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1914 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1915 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1916 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1917 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1918 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1919 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1920 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1921 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1922 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1923 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1924 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1925 default:
1926 assert(!"Invalid opcode provided");
1927 }
1928 return 0;
1929}
1930
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001931CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001932 const std::string &Name, BasicBlock *InsertAtEnd) {
1933 // Construct and return the appropriate CastInst subclass
1934 switch (op) {
1935 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1936 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1937 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1938 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1939 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1940 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1941 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1942 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1943 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1944 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1945 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1946 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1947 default:
1948 assert(!"Invalid opcode provided");
1949 }
1950 return 0;
1951}
1952
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001953CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001954 const std::string &Name,
1955 Instruction *InsertBefore) {
1956 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001957 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1958 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001959}
1960
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001961CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001962 const std::string &Name,
1963 BasicBlock *InsertAtEnd) {
1964 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001965 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1966 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001967}
1968
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001969CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001970 const std::string &Name,
1971 Instruction *InsertBefore) {
1972 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001973 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1974 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001975}
1976
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001977CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001978 const std::string &Name,
1979 BasicBlock *InsertAtEnd) {
1980 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001981 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1982 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001983}
1984
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001985CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001986 const std::string &Name,
1987 Instruction *InsertBefore) {
1988 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001989 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1990 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001991}
1992
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001993CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001994 const std::string &Name,
1995 BasicBlock *InsertAtEnd) {
1996 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001997 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1998 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001999}
2000
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002001CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002002 const std::string &Name,
2003 BasicBlock *InsertAtEnd) {
2004 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002005 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002006 "Invalid cast");
2007
Chris Lattner03c49532007-01-15 02:27:26 +00002008 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002009 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2010 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002011}
2012
2013/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002014CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002015 const std::string &Name,
2016 Instruction *InsertBefore) {
2017 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002018 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002019 "Invalid cast");
2020
Chris Lattner03c49532007-01-15 02:27:26 +00002021 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002022 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2023 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002024}
2025
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002026CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002027 bool isSigned, const std::string &Name,
2028 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002029 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002030 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2031 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2032 Instruction::CastOps opcode =
2033 (SrcBits == DstBits ? Instruction::BitCast :
2034 (SrcBits > DstBits ? Instruction::Trunc :
2035 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002036 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002037}
2038
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002039CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002040 bool isSigned, const std::string &Name,
2041 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00002042 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002043 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2044 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2045 Instruction::CastOps opcode =
2046 (SrcBits == DstBits ? Instruction::BitCast :
2047 (SrcBits > DstBits ? Instruction::Trunc :
2048 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002049 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002050}
2051
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002052CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002053 const std::string &Name,
2054 Instruction *InsertBefore) {
2055 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2056 "Invalid cast");
2057 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2058 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2059 Instruction::CastOps opcode =
2060 (SrcBits == DstBits ? Instruction::BitCast :
2061 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
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::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002066 const std::string &Name,
2067 BasicBlock *InsertAtEnd) {
2068 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2069 "Invalid cast");
2070 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2071 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2072 Instruction::CastOps opcode =
2073 (SrcBits == DstBits ? Instruction::BitCast :
2074 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002075 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002076}
2077
Duncan Sands55e50902008-01-06 10:12:28 +00002078// Check whether it is valid to call getCastOpcode for these types.
2079// This routine must be kept in sync with getCastOpcode.
2080bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2081 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2082 return false;
2083
2084 if (SrcTy == DestTy)
2085 return true;
2086
2087 // Get the bit sizes, we'll need these
2088 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2089 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2090
2091 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002092 if (DestTy->isInteger()) { // Casting to integral
2093 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002094 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002095 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002096 return true;
2097 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002098 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002099 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002100 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002101 return isa<PointerType>(SrcTy);
2102 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002103 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2104 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002105 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002106 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002107 return true;
2108 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002109 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002110 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002111 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002112 return false;
2113 }
2114 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002115 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002116 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002117 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002118 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002119 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002120 return DestPTy->getBitWidth() == SrcBits;
2121 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002122 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2123 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002124 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002125 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002126 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002127 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002128 return false;
2129 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002130 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002131 return false;
2132 }
2133}
2134
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002135// Provide a way to get a "cast" where the cast opcode is inferred from the
2136// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002137// logic in the castIsValid function below. This axiom should hold:
2138// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2139// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002140// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002141// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002142Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002143CastInst::getCastOpcode(
2144 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002145 // Get the bit sizes, we'll need these
2146 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002147 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2148 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002149
Duncan Sands55e50902008-01-06 10:12:28 +00002150 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2151 "Only first class types are castable!");
2152
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002153 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002154 if (DestTy->isInteger()) { // Casting to integral
2155 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002156 if (DestBits < SrcBits)
2157 return Trunc; // int -> smaller int
2158 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002159 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002160 return SExt; // signed -> SEXT
2161 else
2162 return ZExt; // unsigned -> ZEXT
2163 } else {
2164 return BitCast; // Same size, No-op cast
2165 }
2166 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002167 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002168 return FPToSI; // FP -> sint
2169 else
2170 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002171 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002172 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002173 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002174 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002175 return BitCast; // Same size, no-op cast
2176 } else {
2177 assert(isa<PointerType>(SrcTy) &&
2178 "Casting from a value that is not first-class type");
2179 return PtrToInt; // ptr -> int
2180 }
2181 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002182 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002183 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002184 return SIToFP; // sint -> FP
2185 else
2186 return UIToFP; // uint -> FP
2187 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2188 if (DestBits < SrcBits) {
2189 return FPTrunc; // FP -> smaller FP
2190 } else if (DestBits > SrcBits) {
2191 return FPExt; // FP -> larger FP
2192 } else {
2193 return BitCast; // same size, no-op cast
2194 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002195 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002196 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002197 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002198 PTy = NULL;
2199 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002200 } else {
2201 assert(0 && "Casting pointer or non-first class to float");
2202 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002203 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2204 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002205 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002206 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002207 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002208 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002209 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002210 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002211 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002212 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002213 }
2214 } else if (isa<PointerType>(DestTy)) {
2215 if (isa<PointerType>(SrcTy)) {
2216 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002217 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002218 return IntToPtr; // int -> ptr
2219 } else {
2220 assert(!"Casting pointer to other than pointer or int");
2221 }
2222 } else {
2223 assert(!"Casting to type that is not first-class");
2224 }
2225
2226 // If we fall through to here we probably hit an assertion cast above
2227 // and assertions are not turned on. Anything we return is an error, so
2228 // BitCast is as good a choice as any.
2229 return BitCast;
2230}
2231
2232//===----------------------------------------------------------------------===//
2233// CastInst SubClass Constructors
2234//===----------------------------------------------------------------------===//
2235
2236/// Check that the construction parameters for a CastInst are correct. This
2237/// could be broken out into the separate constructors but it is useful to have
2238/// it in one place and to eliminate the redundant code for getting the sizes
2239/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002240bool
2241CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002242
2243 // Check for type sanity on the arguments
2244 const Type *SrcTy = S->getType();
2245 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2246 return false;
2247
2248 // Get the size of the types in bits, we'll need this later
2249 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2250 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2251
2252 // Switch on the opcode provided
2253 switch (op) {
2254 default: return false; // This is an input error
2255 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002256 return SrcTy->isIntOrIntVector() &&
2257 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002258 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002259 return SrcTy->isIntOrIntVector() &&
2260 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002261 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002262 return SrcTy->isIntOrIntVector() &&
2263 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002264 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002265 return SrcTy->isFPOrFPVector() &&
2266 DstTy->isFPOrFPVector() &&
2267 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002268 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002269 return SrcTy->isFPOrFPVector() &&
2270 DstTy->isFPOrFPVector() &&
2271 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002272 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002273 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002274 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2275 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002276 return SVTy->getElementType()->isIntOrIntVector() &&
2277 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002278 SVTy->getNumElements() == DVTy->getNumElements();
2279 }
2280 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002281 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002282 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002283 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002284 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2285 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002286 return SVTy->getElementType()->isFPOrFPVector() &&
2287 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002288 SVTy->getNumElements() == DVTy->getNumElements();
2289 }
2290 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002291 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002292 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002293 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002294 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002295 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002296 case Instruction::BitCast:
2297 // BitCast implies a no-op cast of type only. No bits change.
2298 // However, you can't cast pointers to anything but pointers.
2299 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2300 return false;
2301
Duncan Sands55e50902008-01-06 10:12:28 +00002302 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002303 // these cases, the cast is okay if the source and destination bit widths
2304 // are identical.
2305 return SrcBitSize == DstBitSize;
2306 }
2307}
2308
2309TruncInst::TruncInst(
2310 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2311) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002312 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002313}
2314
2315TruncInst::TruncInst(
2316 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2317) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002318 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002319}
2320
2321ZExtInst::ZExtInst(
2322 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2323) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002324 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002325}
2326
2327ZExtInst::ZExtInst(
2328 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2329) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002330 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002331}
2332SExtInst::SExtInst(
2333 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2334) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002335 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002336}
2337
Jeff Cohencc08c832006-12-02 02:22:01 +00002338SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002339 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2340) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002341 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002342}
2343
2344FPTruncInst::FPTruncInst(
2345 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2346) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002347 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002348}
2349
2350FPTruncInst::FPTruncInst(
2351 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2352) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002353 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002354}
2355
2356FPExtInst::FPExtInst(
2357 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2358) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002359 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002360}
2361
2362FPExtInst::FPExtInst(
2363 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2364) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002365 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002366}
2367
2368UIToFPInst::UIToFPInst(
2369 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2370) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002371 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002372}
2373
2374UIToFPInst::UIToFPInst(
2375 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2376) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002377 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378}
2379
2380SIToFPInst::SIToFPInst(
2381 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2382) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002383 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384}
2385
2386SIToFPInst::SIToFPInst(
2387 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2388) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002389 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002390}
2391
2392FPToUIInst::FPToUIInst(
2393 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2394) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002395 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002396}
2397
2398FPToUIInst::FPToUIInst(
2399 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2400) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002401 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402}
2403
2404FPToSIInst::FPToSIInst(
2405 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2406) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002407 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408}
2409
2410FPToSIInst::FPToSIInst(
2411 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2412) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002413 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002414}
2415
2416PtrToIntInst::PtrToIntInst(
2417 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2418) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002419 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420}
2421
2422PtrToIntInst::PtrToIntInst(
2423 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2424) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002425 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002426}
2427
2428IntToPtrInst::IntToPtrInst(
2429 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2430) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002431 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002432}
2433
2434IntToPtrInst::IntToPtrInst(
2435 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2436) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002437 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002438}
2439
2440BitCastInst::BitCastInst(
2441 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2442) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002443 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444}
2445
2446BitCastInst::BitCastInst(
2447 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2448) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002449 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002451
2452//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002453// CmpInst Classes
2454//===----------------------------------------------------------------------===//
2455
Nate Begemand2195702008-05-12 19:01:56 +00002456CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2457 Value *LHS, Value *RHS, const std::string &Name,
2458 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002459 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002460 OperandTraits<CmpInst>::op_begin(this),
2461 OperandTraits<CmpInst>::operands(this),
2462 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002463 Op<0>() = LHS;
2464 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002465 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002466 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002467}
Gabor Greiff6caff662008-05-10 08:32:32 +00002468
Nate Begemand2195702008-05-12 19:01:56 +00002469CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2470 Value *LHS, Value *RHS, const std::string &Name,
2471 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002472 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002473 OperandTraits<CmpInst>::op_begin(this),
2474 OperandTraits<CmpInst>::operands(this),
2475 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002476 Op<0>() = LHS;
2477 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002478 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002479 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002480}
2481
2482CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002483CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002484 const std::string &Name, Instruction *InsertBefore) {
2485 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002486 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002487 InsertBefore);
2488 }
Nate Begemand2195702008-05-12 19:01:56 +00002489 if (Op == Instruction::FCmp) {
2490 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2491 InsertBefore);
2492 }
2493 if (Op == Instruction::VICmp) {
2494 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2495 InsertBefore);
2496 }
2497 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2498 InsertBefore);
Reid Spencerd9436b62006-11-20 01:22:35 +00002499}
2500
2501CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002502CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002503 const std::string &Name, BasicBlock *InsertAtEnd) {
2504 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002505 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002506 InsertAtEnd);
2507 }
Nate Begemand2195702008-05-12 19:01:56 +00002508 if (Op == Instruction::FCmp) {
2509 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2510 InsertAtEnd);
2511 }
2512 if (Op == Instruction::VICmp) {
2513 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2514 InsertAtEnd);
2515 }
2516 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2517 InsertAtEnd);
Reid Spencerd9436b62006-11-20 01:22:35 +00002518}
2519
2520void CmpInst::swapOperands() {
2521 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2522 IC->swapOperands();
2523 else
2524 cast<FCmpInst>(this)->swapOperands();
2525}
2526
2527bool CmpInst::isCommutative() {
2528 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2529 return IC->isCommutative();
2530 return cast<FCmpInst>(this)->isCommutative();
2531}
2532
2533bool CmpInst::isEquality() {
2534 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2535 return IC->isEquality();
2536 return cast<FCmpInst>(this)->isEquality();
2537}
2538
2539
Dan Gohman4e724382008-05-31 02:47:54 +00002540CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002541 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002542 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002543 case ICMP_EQ: return ICMP_NE;
2544 case ICMP_NE: return ICMP_EQ;
2545 case ICMP_UGT: return ICMP_ULE;
2546 case ICMP_ULT: return ICMP_UGE;
2547 case ICMP_UGE: return ICMP_ULT;
2548 case ICMP_ULE: return ICMP_UGT;
2549 case ICMP_SGT: return ICMP_SLE;
2550 case ICMP_SLT: return ICMP_SGE;
2551 case ICMP_SGE: return ICMP_SLT;
2552 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002553
Dan Gohman4e724382008-05-31 02:47:54 +00002554 case FCMP_OEQ: return FCMP_UNE;
2555 case FCMP_ONE: return FCMP_UEQ;
2556 case FCMP_OGT: return FCMP_ULE;
2557 case FCMP_OLT: return FCMP_UGE;
2558 case FCMP_OGE: return FCMP_ULT;
2559 case FCMP_OLE: return FCMP_UGT;
2560 case FCMP_UEQ: return FCMP_ONE;
2561 case FCMP_UNE: return FCMP_OEQ;
2562 case FCMP_UGT: return FCMP_OLE;
2563 case FCMP_ULT: return FCMP_OGE;
2564 case FCMP_UGE: return FCMP_OLT;
2565 case FCMP_ULE: return FCMP_OGT;
2566 case FCMP_ORD: return FCMP_UNO;
2567 case FCMP_UNO: return FCMP_ORD;
2568 case FCMP_TRUE: return FCMP_FALSE;
2569 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002570 }
2571}
2572
Reid Spencer266e42b2006-12-23 06:05:41 +00002573ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2574 switch (pred) {
2575 default: assert(! "Unknown icmp predicate!");
2576 case ICMP_EQ: case ICMP_NE:
2577 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2578 return pred;
2579 case ICMP_UGT: return ICMP_SGT;
2580 case ICMP_ULT: return ICMP_SLT;
2581 case ICMP_UGE: return ICMP_SGE;
2582 case ICMP_ULE: return ICMP_SLE;
2583 }
2584}
2585
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002586ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2587 switch (pred) {
2588 default: assert(! "Unknown icmp predicate!");
2589 case ICMP_EQ: case ICMP_NE:
2590 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2591 return pred;
2592 case ICMP_SGT: return ICMP_UGT;
2593 case ICMP_SLT: return ICMP_ULT;
2594 case ICMP_SGE: return ICMP_UGE;
2595 case ICMP_SLE: return ICMP_ULE;
2596 }
2597}
2598
Reid Spencer266e42b2006-12-23 06:05:41 +00002599bool ICmpInst::isSignedPredicate(Predicate pred) {
2600 switch (pred) {
2601 default: assert(! "Unknown icmp predicate!");
2602 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2603 return true;
2604 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2605 case ICMP_UGE: case ICMP_ULE:
2606 return false;
2607 }
2608}
2609
Reid Spencer0286bc12007-02-28 22:00:54 +00002610/// Initialize a set of values that all satisfy the condition with C.
2611///
2612ConstantRange
2613ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2614 APInt Lower(C);
2615 APInt Upper(C);
2616 uint32_t BitWidth = C.getBitWidth();
2617 switch (pred) {
2618 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2619 case ICmpInst::ICMP_EQ: Upper++; break;
2620 case ICmpInst::ICMP_NE: Lower++; break;
2621 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2622 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2623 case ICmpInst::ICMP_UGT:
2624 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2625 break;
2626 case ICmpInst::ICMP_SGT:
2627 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2628 break;
2629 case ICmpInst::ICMP_ULE:
2630 Lower = APInt::getMinValue(BitWidth); Upper++;
2631 break;
2632 case ICmpInst::ICMP_SLE:
2633 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2634 break;
2635 case ICmpInst::ICMP_UGE:
2636 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2637 break;
2638 case ICmpInst::ICMP_SGE:
2639 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2640 break;
2641 }
2642 return ConstantRange(Lower, Upper);
2643}
2644
Dan Gohman4e724382008-05-31 02:47:54 +00002645CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002646 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002647 default: assert(!"Unknown cmp predicate!");
2648 case ICMP_EQ: case ICMP_NE:
2649 return pred;
2650 case ICMP_SGT: return ICMP_SLT;
2651 case ICMP_SLT: return ICMP_SGT;
2652 case ICMP_SGE: return ICMP_SLE;
2653 case ICMP_SLE: return ICMP_SGE;
2654 case ICMP_UGT: return ICMP_ULT;
2655 case ICMP_ULT: return ICMP_UGT;
2656 case ICMP_UGE: return ICMP_ULE;
2657 case ICMP_ULE: return ICMP_UGE;
2658
Reid Spencerd9436b62006-11-20 01:22:35 +00002659 case FCMP_FALSE: case FCMP_TRUE:
2660 case FCMP_OEQ: case FCMP_ONE:
2661 case FCMP_UEQ: case FCMP_UNE:
2662 case FCMP_ORD: case FCMP_UNO:
2663 return pred;
2664 case FCMP_OGT: return FCMP_OLT;
2665 case FCMP_OLT: return FCMP_OGT;
2666 case FCMP_OGE: return FCMP_OLE;
2667 case FCMP_OLE: return FCMP_OGE;
2668 case FCMP_UGT: return FCMP_ULT;
2669 case FCMP_ULT: return FCMP_UGT;
2670 case FCMP_UGE: return FCMP_ULE;
2671 case FCMP_ULE: return FCMP_UGE;
2672 }
2673}
2674
Reid Spencer266e42b2006-12-23 06:05:41 +00002675bool CmpInst::isUnsigned(unsigned short predicate) {
2676 switch (predicate) {
2677 default: return false;
2678 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2679 case ICmpInst::ICMP_UGE: return true;
2680 }
2681}
2682
2683bool CmpInst::isSigned(unsigned short predicate){
2684 switch (predicate) {
2685 default: return false;
2686 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2687 case ICmpInst::ICMP_SGE: return true;
2688 }
2689}
2690
2691bool CmpInst::isOrdered(unsigned short predicate) {
2692 switch (predicate) {
2693 default: return false;
2694 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2695 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2696 case FCmpInst::FCMP_ORD: return true;
2697 }
2698}
2699
2700bool CmpInst::isUnordered(unsigned short predicate) {
2701 switch (predicate) {
2702 default: return false;
2703 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2704 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2705 case FCmpInst::FCMP_UNO: return true;
2706 }
2707}
2708
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002709//===----------------------------------------------------------------------===//
2710// SwitchInst Implementation
2711//===----------------------------------------------------------------------===//
2712
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002713void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002714 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002715 ReservedSpace = 2+NumCases*2;
2716 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002717 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002718
Gabor Greif2d3024d2008-05-26 21:33:52 +00002719 OperandList[0] = Value;
2720 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002721}
2722
Chris Lattner2195fc42007-02-24 00:55:48 +00002723/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2724/// switch on and a default destination. The number of additional cases can
2725/// be specified here to make memory allocation more efficient. This
2726/// constructor can also autoinsert before another instruction.
2727SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2728 Instruction *InsertBefore)
2729 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2730 init(Value, Default, NumCases);
2731}
2732
2733/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2734/// switch on and a default destination. The number of additional cases can
2735/// be specified here to make memory allocation more efficient. This
2736/// constructor also autoinserts at the end of the specified BasicBlock.
2737SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2738 BasicBlock *InsertAtEnd)
2739 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2740 init(Value, Default, NumCases);
2741}
2742
Misha Brukmanb1c93172005-04-21 23:48:37 +00002743SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002744 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002745 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002746 Use *OL = OperandList, *InOL = SI.OperandList;
2747 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002748 OL[i] = InOL[i];
2749 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002750 }
2751}
2752
Gordon Henriksen14a55692007-12-10 02:14:30 +00002753SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002754 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002755}
2756
2757
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002758/// addCase - Add an entry to the switch instruction...
2759///
Chris Lattner47ac1872005-02-24 05:32:09 +00002760void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002761 unsigned OpNo = NumOperands;
2762 if (OpNo+2 > ReservedSpace)
2763 resizeOperands(0); // Get more space!
2764 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002765 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002766 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002767 OperandList[OpNo] = OnVal;
2768 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002769}
2770
2771/// removeCase - This method removes the specified successor from the switch
2772/// instruction. Note that this cannot be used to remove the default
2773/// destination (successor #0).
2774///
2775void SwitchInst::removeCase(unsigned idx) {
2776 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002777 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2778
2779 unsigned NumOps = getNumOperands();
2780 Use *OL = OperandList;
2781
2782 // Move everything after this operand down.
2783 //
2784 // FIXME: we could just swap with the end of the list, then erase. However,
2785 // client might not expect this to happen. The code as it is thrashes the
2786 // use/def lists, which is kinda lame.
2787 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2788 OL[i-2] = OL[i];
2789 OL[i-2+1] = OL[i+1];
2790 }
2791
2792 // Nuke the last value.
2793 OL[NumOps-2].set(0);
2794 OL[NumOps-2+1].set(0);
2795 NumOperands = NumOps-2;
2796}
2797
2798/// resizeOperands - resize operands - This adjusts the length of the operands
2799/// list according to the following behavior:
2800/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002801/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002802/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2803/// 3. If NumOps == NumOperands, trim the reserved space.
2804///
2805void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002806 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002807 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002808 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002809 } else if (NumOps*2 > NumOperands) {
2810 // No resize needed.
2811 if (ReservedSpace >= NumOps) return;
2812 } else if (NumOps == NumOperands) {
2813 if (ReservedSpace == NumOps) return;
2814 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002815 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002816 }
2817
2818 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002819 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002820 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002821 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002822 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002823 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002824 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002825 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002826}
2827
2828
2829BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2830 return getSuccessor(idx);
2831}
2832unsigned SwitchInst::getNumSuccessorsV() const {
2833 return getNumSuccessors();
2834}
2835void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2836 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002837}
Chris Lattnerf22be932004-10-15 23:52:53 +00002838
Chris Lattnerf22be932004-10-15 23:52:53 +00002839// Define these methods here so vtables don't get emitted into every translation
2840// unit that uses these classes.
2841
2842GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002843 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002844}
2845
2846BinaryOperator *BinaryOperator::clone() const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002847 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002848}
2849
Chris Lattner0b490b02007-08-24 20:48:18 +00002850FCmpInst* FCmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002851 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002852}
2853ICmpInst* ICmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002854 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002855}
2856
Nate Begemand2195702008-05-12 19:01:56 +00002857VFCmpInst* VFCmpInst::clone() const {
2858 return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
2859}
2860VICmpInst* VICmpInst::clone() const {
2861 return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
2862}
2863
Dan Gohman0752bff2008-05-23 00:36:11 +00002864ExtractValueInst *ExtractValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002865 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002866}
2867InsertValueInst *InsertValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002868 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002869}
2870
2871
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002872MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2873AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2874FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2875LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2876StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2877CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2878CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2879CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2880CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2881CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2882CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2883CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2884CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2885CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2886CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2887CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2888CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002889CallInst *CallInst::clone() const {
2890 return new(getNumOperands()) CallInst(*this);
2891}
2892SelectInst *SelectInst::clone() const {
2893 return new(getNumOperands()) SelectInst(*this);
2894}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002895VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2896
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002897ExtractElementInst *ExtractElementInst::clone() const {
2898 return new ExtractElementInst(*this);
2899}
2900InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002901 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002902}
2903ShuffleVectorInst *ShuffleVectorInst::clone() const {
2904 return new ShuffleVectorInst(*this);
2905}
Chris Lattnerf22be932004-10-15 23:52:53 +00002906PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002907ReturnInst *ReturnInst::clone() const {
2908 return new(getNumOperands()) ReturnInst(*this);
2909}
2910BranchInst *BranchInst::clone() const {
2911 return new(getNumOperands()) BranchInst(*this);
2912}
Gabor Greiff6caff662008-05-10 08:32:32 +00002913SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002914InvokeInst *InvokeInst::clone() const {
2915 return new(getNumOperands()) InvokeInst(*this);
2916}
Chris Lattnerf22be932004-10-15 23:52:53 +00002917UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002918UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}