blob: 6da04e7178f99051949ecb48352e6bbc47ac0b79 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000015#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Function.h"
18#include "llvm/Instructions.h"
19#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000020#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000021#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000022using namespace llvm;
23
Chris Lattner3e13b8c2008-01-02 23:42:30 +000024//===----------------------------------------------------------------------===//
25// CallSite Class
26//===----------------------------------------------------------------------===//
27
Duncan Sands85fab3a2008-02-18 17:32:13 +000028CallSite::CallSite(Instruction *C) {
29 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
30 I = C;
31}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000032unsigned CallSite::getCallingConv() const {
33 if (CallInst *CI = dyn_cast<CallInst>(I))
34 return CI->getCallingConv();
35 else
36 return cast<InvokeInst>(I)->getCallingConv();
37}
38void CallSite::setCallingConv(unsigned CC) {
39 if (CallInst *CI = dyn_cast<CallInst>(I))
40 CI->setCallingConv(CC);
41 else
42 cast<InvokeInst>(I)->setCallingConv(CC);
43}
Chris Lattner8a923e72008-03-12 17:45:29 +000044const PAListPtr &CallSite::getParamAttrs() const {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000045 if (CallInst *CI = dyn_cast<CallInst>(I))
46 return CI->getParamAttrs();
47 else
48 return cast<InvokeInst>(I)->getParamAttrs();
49}
Chris Lattner8a923e72008-03-12 17:45:29 +000050void CallSite::setParamAttrs(const PAListPtr &PAL) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000051 if (CallInst *CI = dyn_cast<CallInst>(I))
52 CI->setParamAttrs(PAL);
53 else
54 cast<InvokeInst>(I)->setParamAttrs(PAL);
55}
Dale Johannesen89268bc2008-02-19 21:38:47 +000056bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
Duncan Sands5208d1a2007-11-28 17:07:01 +000057 if (CallInst *CI = dyn_cast<CallInst>(I))
Dale Johannesen89268bc2008-02-19 21:38:47 +000058 return CI->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000059 else
Dale Johannesen89268bc2008-02-19 21:38:47 +000060 return cast<InvokeInst>(I)->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000061}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000062uint16_t CallSite::getParamAlignment(uint16_t i) const {
63 if (CallInst *CI = dyn_cast<CallInst>(I))
64 return CI->getParamAlignment(i);
65 else
66 return cast<InvokeInst>(I)->getParamAlignment(i);
67}
68
Duncan Sands38ef3a82007-12-03 20:06:50 +000069bool CallSite::doesNotAccessMemory() const {
70 if (CallInst *CI = dyn_cast<CallInst>(I))
71 return CI->doesNotAccessMemory();
72 else
73 return cast<InvokeInst>(I)->doesNotAccessMemory();
74}
75bool CallSite::onlyReadsMemory() const {
76 if (CallInst *CI = dyn_cast<CallInst>(I))
77 return CI->onlyReadsMemory();
78 else
79 return cast<InvokeInst>(I)->onlyReadsMemory();
80}
Duncan Sands3353ed02007-12-18 09:59:50 +000081bool CallSite::doesNotThrow() const {
Duncan Sands8e4847e2007-12-16 15:51:49 +000082 if (CallInst *CI = dyn_cast<CallInst>(I))
Duncan Sands3353ed02007-12-18 09:59:50 +000083 return CI->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000084 else
Duncan Sands3353ed02007-12-18 09:59:50 +000085 return cast<InvokeInst>(I)->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000086}
Duncan Sandsaa31b922007-12-19 21:13:37 +000087void CallSite::setDoesNotThrow(bool doesNotThrow) {
88 if (CallInst *CI = dyn_cast<CallInst>(I))
89 CI->setDoesNotThrow(doesNotThrow);
90 else
91 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
92}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000093
Gordon Henriksen14a55692007-12-10 02:14:30 +000094//===----------------------------------------------------------------------===//
95// TerminatorInst Class
96//===----------------------------------------------------------------------===//
97
98// Out of line virtual method, so the vtable, etc has a home.
99TerminatorInst::~TerminatorInst() {
100}
101
Gabor Greiff6caff662008-05-10 08:32:32 +0000102//===----------------------------------------------------------------------===//
103// UnaryInstruction Class
104//===----------------------------------------------------------------------===//
105
Gordon Henriksen14a55692007-12-10 02:14:30 +0000106// Out of line virtual method, so the vtable, etc has a home.
107UnaryInstruction::~UnaryInstruction() {
108}
109
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000110//===----------------------------------------------------------------------===//
111// PHINode Class
112//===----------------------------------------------------------------------===//
113
114PHINode::PHINode(const PHINode &PN)
115 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000116 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117 ReservedSpace(PN.getNumOperands()) {
118 Use *OL = OperandList;
119 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000120 OL[i] = PN.getOperand(i);
121 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000122 }
123}
124
Gordon Henriksen14a55692007-12-10 02:14:30 +0000125PHINode::~PHINode() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000126 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000127}
128
129// removeIncomingValue - Remove an incoming value. This is useful if a
130// predecessor basic block is deleted.
131Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
132 unsigned NumOps = getNumOperands();
133 Use *OL = OperandList;
134 assert(Idx*2 < NumOps && "BB not in PHI node!");
135 Value *Removed = OL[Idx*2];
136
137 // Move everything after this operand down.
138 //
139 // FIXME: we could just swap with the end of the list, then erase. However,
140 // client might not expect this to happen. The code as it is thrashes the
141 // use/def lists, which is kinda lame.
142 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
143 OL[i-2] = OL[i];
144 OL[i-2+1] = OL[i+1];
145 }
146
147 // Nuke the last value.
148 OL[NumOps-2].set(0);
149 OL[NumOps-2+1].set(0);
150 NumOperands = NumOps-2;
151
152 // If the PHI node is dead, because it has zero entries, nuke it now.
153 if (NumOps == 2 && DeletePHIIfEmpty) {
154 // If anyone is using this PHI, make them use a dummy value instead...
155 replaceAllUsesWith(UndefValue::get(getType()));
156 eraseFromParent();
157 }
158 return Removed;
159}
160
161/// resizeOperands - resize operands - This adjusts the length of the operands
162/// list according to the following behavior:
163/// 1. If NumOps == 0, grow the operand list in response to a push_back style
164/// of operation. This grows the number of ops by 1.5 times.
165/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
166/// 3. If NumOps == NumOperands, trim the reserved space.
167///
168void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000169 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000170 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000171 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000172 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
173 } else if (NumOps*2 > NumOperands) {
174 // No resize needed.
175 if (ReservedSpace >= NumOps) return;
176 } else if (NumOps == NumOperands) {
177 if (ReservedSpace == NumOps) return;
178 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000179 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000180 }
181
182 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000183 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000184 Use *NewOps = allocHungoffUses(NumOps);
185 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000186 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000187 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000188 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000189 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000190}
191
Nate Begemanb3923212005-08-04 23:24:19 +0000192/// hasConstantValue - If the specified PHI node always merges together the same
193/// value, return the value, otherwise return null.
194///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000195Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000196 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000197 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000198 if (getIncomingValue(0) != this) // not X = phi X
199 return getIncomingValue(0);
200 else
201 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000202 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000203
Nate Begemanb3923212005-08-04 23:24:19 +0000204 // Otherwise if all of the incoming values are the same for the PHI, replace
205 // the PHI node with the incoming value.
206 //
207 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000208 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000209 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000210 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000211 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000212 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000213 if (InVal && getIncomingValue(i) != InVal)
214 return 0; // Not the same, bail out.
215 else
216 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000217 }
Nate Begemanb3923212005-08-04 23:24:19 +0000218
219 // The only case that could cause InVal to be null is if we have a PHI node
220 // that only has entries for itself. In this case, there is no entry into the
221 // loop, so kill the PHI.
222 //
223 if (InVal == 0) InVal = UndefValue::get(getType());
224
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000225 // If we have a PHI node like phi(X, undef, X), where X is defined by some
226 // instruction, we cannot always return X as the result of the PHI node. Only
227 // do this if X is not an instruction (thus it must dominate the PHI block),
228 // or if the client is prepared to deal with this possibility.
229 if (HasUndefInput && !AllowNonDominatingInstruction)
230 if (Instruction *IV = dyn_cast<Instruction>(InVal))
231 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000232 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000233 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000234 return 0; // Cannot guarantee that InVal dominates this PHINode.
235
Nate Begemanb3923212005-08-04 23:24:19 +0000236 // All of the incoming values are the same, return the value now.
237 return InVal;
238}
239
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240
241//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000242// CallInst Implementation
243//===----------------------------------------------------------------------===//
244
Gordon Henriksen14a55692007-12-10 02:14:30 +0000245CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000246}
247
Chris Lattner054ba2c2007-02-13 00:58:44 +0000248void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000249 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
250 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000251 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000252
Misha Brukmanb1c93172005-04-21 23:48:37 +0000253 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000254 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000255 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000256
Chris Lattner054ba2c2007-02-13 00:58:44 +0000257 assert((NumParams == FTy->getNumParams() ||
258 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000259 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000260 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000261 assert((i >= FTy->getNumParams() ||
262 FTy->getParamType(i) == Params[i]->getType()) &&
263 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000264 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000265 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000266}
267
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000268void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000269 assert(NumOperands == 3 && "NumOperands not set up?");
270 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000271 OL[0] = Func;
272 OL[1] = Actual1;
273 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000274
275 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000276 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000277 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000278
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000279 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000280 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000281 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000282 assert((0 >= FTy->getNumParams() ||
283 FTy->getParamType(0) == Actual1->getType()) &&
284 "Calling a function with a bad signature!");
285 assert((1 >= FTy->getNumParams() ||
286 FTy->getParamType(1) == Actual2->getType()) &&
287 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000288}
289
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000290void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000291 assert(NumOperands == 2 && "NumOperands not set up?");
292 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000293 OL[0] = Func;
294 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000295
296 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000297 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000298 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000300 assert((FTy->getNumParams() == 1 ||
301 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000303 assert((0 == FTy->getNumParams() ||
304 FTy->getParamType(0) == Actual->getType()) &&
305 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306}
307
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000308void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000309 assert(NumOperands == 1 && "NumOperands not set up?");
310 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000311 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000312
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000313 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000315 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000316
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000317 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318}
319
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000321 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
323 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000324 Instruction::Call,
325 OperandTraits<CallInst>::op_end(this) - 2,
326 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000328 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329}
330
331CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
332 BasicBlock *InsertAtEnd)
333 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
334 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000335 Instruction::Call,
336 OperandTraits<CallInst>::op_end(this) - 2,
337 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000338 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000339 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000340}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341CallInst::CallInst(Value *Func, const std::string &Name,
342 Instruction *InsertBefore)
343 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
344 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000345 Instruction::Call,
346 OperandTraits<CallInst>::op_end(this) - 1,
347 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000349 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350}
351
352CallInst::CallInst(Value *Func, const std::string &Name,
353 BasicBlock *InsertAtEnd)
354 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
355 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000356 Instruction::Call,
357 OperandTraits<CallInst>::op_end(this) - 1,
358 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000359 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000360 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000361}
362
Misha Brukmanb1c93172005-04-21 23:48:37 +0000363CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000364 : Instruction(CI.getType(), Instruction::Call,
365 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000366 CI.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000367 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000368 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000369 Use *OL = OperandList;
370 Use *InOL = CI.OperandList;
371 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000372 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000373}
374
Eric Christopher901b1a72008-05-16 20:39:43 +0000375void CallInst::addParamAttr(unsigned i, ParameterAttributes attr) {
376 PAListPtr PAL = getParamAttrs();
377 PAL = PAL.addAttr(i, attr);
378 setParamAttrs(PAL);
379}
380
Chris Lattnerc994c022008-03-13 05:00:21 +0000381bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000382 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000383 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000384 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000385 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000386 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000387}
388
Duncan Sandsaa31b922007-12-19 21:13:37 +0000389void CallInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000390 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000391 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000392 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000393 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000394 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000395 setParamAttrs(PAL);
396}
397
Duncan Sands5208d1a2007-11-28 17:07:01 +0000398
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000399//===----------------------------------------------------------------------===//
400// InvokeInst Implementation
401//===----------------------------------------------------------------------===//
402
403void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000404 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000405 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
406 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000407 OL[0] = Fn;
408 OL[1] = IfNormal;
409 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000410 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000411 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000412 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000413
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000414 assert(((NumArgs == FTy->getNumParams()) ||
415 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000416 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000417
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000418 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000419 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000420 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000421 "Invoking a function with a bad signature!");
422
Gabor Greif2d3024d2008-05-26 21:33:52 +0000423 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000424 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000425}
426
Misha Brukmanb1c93172005-04-21 23:48:37 +0000427InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000428 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000429 OperandTraits<InvokeInst>::op_end(this)
430 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000431 II.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000432 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000433 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000434 Use *OL = OperandList, *InOL = II.OperandList;
435 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000436 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000437}
438
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000439BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
440 return getSuccessor(idx);
441}
442unsigned InvokeInst::getNumSuccessorsV() const {
443 return getNumSuccessors();
444}
445void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
446 return setSuccessor(idx, B);
447}
448
Chris Lattnerc994c022008-03-13 05:00:21 +0000449bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000450 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000451 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000452 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000453 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000454 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000455}
456
Eric Christopher901b1a72008-05-16 20:39:43 +0000457void InvokeInst::addParamAttr(unsigned i, ParameterAttributes attr) {
458 PAListPtr PAL = getParamAttrs();
459 PAL = PAL.addAttr(i, attr);
460 setParamAttrs(PAL);
461}
462
Duncan Sandsaa31b922007-12-19 21:13:37 +0000463void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000464 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000465 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000466 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000467 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000468 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000469 setParamAttrs(PAL);
470}
471
Duncan Sands5208d1a2007-11-28 17:07:01 +0000472
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000473//===----------------------------------------------------------------------===//
474// ReturnInst Implementation
475//===----------------------------------------------------------------------===//
476
Chris Lattner2195fc42007-02-24 00:55:48 +0000477ReturnInst::ReturnInst(const ReturnInst &RI)
478 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Gabor Greif697e94c2008-05-15 10:04:30 +0000479 OperandTraits<ReturnInst>::op_end(this)
480 - RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000481 RI.getNumOperands()) {
Devang Patel59643e52008-02-23 00:35:18 +0000482 unsigned N = RI.getNumOperands();
Gabor Greiff6caff662008-05-10 08:32:32 +0000483 if (N == 1)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000484 Op<0>() = RI.Op<0>();
Devang Patelae682fb2008-02-26 17:56:20 +0000485 else if (N) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000486 Use *OL = OperandList;
Devang Patelae682fb2008-02-26 17:56:20 +0000487 for (unsigned i = 0; i < N; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000488 OL[i] = RI.getOperand(i);
Devang Patelae682fb2008-02-26 17:56:20 +0000489 }
Chris Lattner2195fc42007-02-24 00:55:48 +0000490}
491
492ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000493 : TerminatorInst(Type::VoidTy, Instruction::Ret,
494 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
495 retVal != 0, InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000496 if (retVal)
497 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000498}
499ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000500 : TerminatorInst(Type::VoidTy, Instruction::Ret,
501 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
502 retVal != 0, InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000503 if (retVal)
504 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000505}
506ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000507 : TerminatorInst(Type::VoidTy, Instruction::Ret,
508 OperandTraits<ReturnInst>::op_end(this),
509 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000510}
511
Devang Patela736c002008-02-26 19:38:17 +0000512ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
513 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000514 : TerminatorInst(Type::VoidTy, Instruction::Ret,
515 OperandTraits<ReturnInst>::op_end(this) - N,
516 N, InsertBefore) {
Devang Patela736c002008-02-26 19:38:17 +0000517 if (N != 0)
518 init(retVals, N);
519}
520ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
521 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000522 : TerminatorInst(Type::VoidTy, Instruction::Ret,
523 OperandTraits<ReturnInst>::op_end(this) - N,
524 N, InsertAtEnd) {
Devang Patela736c002008-02-26 19:38:17 +0000525 if (N != 0)
526 init(retVals, N);
527}
528
Devang Patel060e79c2008-02-26 19:15:26 +0000529void ReturnInst::init(Value * const* retVals, unsigned N) {
Devang Patelc38eb522008-02-26 18:49:29 +0000530 assert (N > 0 && "Invalid operands numbers in ReturnInst init");
Devang Patel59643e52008-02-23 00:35:18 +0000531
Devang Patelc38eb522008-02-26 18:49:29 +0000532 NumOperands = N;
Devang Patel59643e52008-02-23 00:35:18 +0000533 if (NumOperands == 1) {
Devang Patel060e79c2008-02-26 19:15:26 +0000534 Value *V = *retVals;
Devang Patel59643e52008-02-23 00:35:18 +0000535 if (V->getType() == Type::VoidTy)
536 return;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000537 Op<0>() = V;
Devang Patelae682fb2008-02-26 17:56:20 +0000538 return;
Devang Patel59643e52008-02-23 00:35:18 +0000539 }
540
Gabor Greiff6caff662008-05-10 08:32:32 +0000541 Use *OL = OperandList;
Devang Patel59643e52008-02-23 00:35:18 +0000542 for (unsigned i = 0; i < NumOperands; ++i) {
Devang Patel060e79c2008-02-26 19:15:26 +0000543 Value *V = *retVals++;
Devang Patel59643e52008-02-23 00:35:18 +0000544 assert(!isa<BasicBlock>(V) &&
545 "Cannot return basic block. Probably using the incorrect ctor");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000546 OL[i] = V;
Devang Patel59643e52008-02-23 00:35:18 +0000547 }
548}
549
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000550unsigned ReturnInst::getNumSuccessorsV() const {
551 return getNumSuccessors();
552}
553
Devang Patelae682fb2008-02-26 17:56:20 +0000554/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
555/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000556void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000557 assert(0 && "ReturnInst has no successors!");
558}
559
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000560BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
561 assert(0 && "ReturnInst has no successors!");
562 abort();
563 return 0;
564}
565
Devang Patel59643e52008-02-23 00:35:18 +0000566ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000567}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000568
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000569//===----------------------------------------------------------------------===//
570// UnwindInst Implementation
571//===----------------------------------------------------------------------===//
572
Chris Lattner2195fc42007-02-24 00:55:48 +0000573UnwindInst::UnwindInst(Instruction *InsertBefore)
574 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
575}
576UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
577 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
578}
579
580
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000581unsigned UnwindInst::getNumSuccessorsV() const {
582 return getNumSuccessors();
583}
584
585void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000586 assert(0 && "UnwindInst has no successors!");
587}
588
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000589BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
590 assert(0 && "UnwindInst has no successors!");
591 abort();
592 return 0;
593}
594
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000595//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000596// UnreachableInst Implementation
597//===----------------------------------------------------------------------===//
598
Chris Lattner2195fc42007-02-24 00:55:48 +0000599UnreachableInst::UnreachableInst(Instruction *InsertBefore)
600 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
601}
602UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
603 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
604}
605
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000606unsigned UnreachableInst::getNumSuccessorsV() const {
607 return getNumSuccessors();
608}
609
610void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
611 assert(0 && "UnwindInst has no successors!");
612}
613
614BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
615 assert(0 && "UnwindInst has no successors!");
616 abort();
617 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000618}
619
620//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000621// BranchInst Implementation
622//===----------------------------------------------------------------------===//
623
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000624void BranchInst::AssertOK() {
625 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000626 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000627 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000628}
629
Chris Lattner2195fc42007-02-24 00:55:48 +0000630BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000631 : TerminatorInst(Type::VoidTy, Instruction::Br,
632 OperandTraits<BranchInst>::op_end(this) - 1,
633 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000634 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000635 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000636}
637BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
638 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000639 : TerminatorInst(Type::VoidTy, Instruction::Br,
640 OperandTraits<BranchInst>::op_end(this) - 3,
641 3, InsertBefore) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000642 Op<0>() = IfTrue;
643 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000644 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000645#ifndef NDEBUG
646 AssertOK();
647#endif
648}
649
650BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000651 : TerminatorInst(Type::VoidTy, Instruction::Br,
652 OperandTraits<BranchInst>::op_end(this) - 1,
653 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000654 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000655 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000656}
657
658BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
659 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000660 : TerminatorInst(Type::VoidTy, Instruction::Br,
661 OperandTraits<BranchInst>::op_end(this) - 3,
662 3, InsertAtEnd) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000663 Op<0>() = IfTrue;
664 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000665 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000666#ifndef NDEBUG
667 AssertOK();
668#endif
669}
670
671
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000672BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000673 TerminatorInst(Type::VoidTy, Instruction::Br,
674 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
675 BI.getNumOperands()) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000676 OperandList[0] = BI.getOperand(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000677 if (BI.getNumOperands() != 1) {
678 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000679 OperandList[1] = BI.getOperand(1);
680 OperandList[2] = BI.getOperand(2);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000681 }
682}
683
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000684BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
685 return getSuccessor(idx);
686}
687unsigned BranchInst::getNumSuccessorsV() const {
688 return getNumSuccessors();
689}
690void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
691 setSuccessor(idx, B);
692}
693
694
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000695//===----------------------------------------------------------------------===//
696// AllocationInst Implementation
697//===----------------------------------------------------------------------===//
698
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000699static Value *getAISize(Value *Amt) {
700 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000701 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000702 else {
703 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000704 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000705 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000706 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000707 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000708 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000709}
710
Misha Brukmanb1c93172005-04-21 23:48:37 +0000711AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000712 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000713 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000714 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000715 InsertBefore) {
716 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000717 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000718 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000719}
720
Misha Brukmanb1c93172005-04-21 23:48:37 +0000721AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000722 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000723 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000724 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000725 InsertAtEnd) {
726 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000727 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000728 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000729}
730
Gordon Henriksen14a55692007-12-10 02:14:30 +0000731// Out of line virtual method, so the vtable, etc has a home.
732AllocationInst::~AllocationInst() {
733}
734
Dan Gohmanaa583d72008-03-24 16:55:58 +0000735void AllocationInst::setAlignment(unsigned Align) {
736 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
737 SubclassData = Log2_32(Align) + 1;
738 assert(getAlignment() == Align && "Alignment representation error!");
739}
740
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000741bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000742 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
743 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000744 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000745}
746
747const Type *AllocationInst::getAllocatedType() const {
748 return getType()->getElementType();
749}
750
751AllocaInst::AllocaInst(const AllocaInst &AI)
752 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000753 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000754}
755
756MallocInst::MallocInst(const MallocInst &MI)
757 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000758 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000759}
760
761//===----------------------------------------------------------------------===//
762// FreeInst Implementation
763//===----------------------------------------------------------------------===//
764
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000765void FreeInst::AssertOK() {
766 assert(isa<PointerType>(getOperand(0)->getType()) &&
767 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000768}
769
770FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000771 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000772 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000773}
774
775FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000776 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000777 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000778}
779
780
781//===----------------------------------------------------------------------===//
782// LoadInst Implementation
783//===----------------------------------------------------------------------===//
784
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000785void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000786 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000787 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000788}
789
790LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000791 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000792 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000793 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000794 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000795 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000796 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000797}
798
799LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000800 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000801 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000802 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000803 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000804 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000805 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000806}
807
808LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
809 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000810 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000811 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000812 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000813 setAlignment(0);
814 AssertOK();
815 setName(Name);
816}
817
818LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
819 unsigned Align, Instruction *InsertBef)
820 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
821 Load, Ptr, InsertBef) {
822 setVolatile(isVolatile);
823 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000824 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000825 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000826}
827
Dan Gohman68659282007-07-18 20:51:11 +0000828LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
829 unsigned Align, BasicBlock *InsertAE)
830 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
831 Load, Ptr, InsertAE) {
832 setVolatile(isVolatile);
833 setAlignment(Align);
834 AssertOK();
835 setName(Name);
836}
837
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000838LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
839 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000840 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000841 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000842 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000843 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000844 AssertOK();
845 setName(Name);
846}
847
848
849
850LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000851 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
852 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000853 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000854 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000855 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000856 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000857}
858
859LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000860 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
861 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000862 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000863 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000864 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000865 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000866}
867
868LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
869 Instruction *InsertBef)
870: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000871 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000872 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000873 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000874 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000875 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000876}
877
878LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
879 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000880 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
881 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000882 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000883 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000884 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000885 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000886}
887
Christopher Lamb84485702007-04-22 19:24:39 +0000888void LoadInst::setAlignment(unsigned Align) {
889 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
890 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
891}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000892
893//===----------------------------------------------------------------------===//
894// StoreInst Implementation
895//===----------------------------------------------------------------------===//
896
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000897void StoreInst::AssertOK() {
898 assert(isa<PointerType>(getOperand(1)->getType()) &&
899 "Ptr must have pointer type!");
900 assert(getOperand(0)->getType() ==
901 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000902 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000903}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000904
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000905
906StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000907 : Instruction(Type::VoidTy, Store,
908 OperandTraits<StoreInst>::op_begin(this),
909 OperandTraits<StoreInst>::operands(this),
910 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000911 Op<0>() = val;
912 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000913 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000914 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000915 AssertOK();
916}
917
918StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000919 : Instruction(Type::VoidTy, Store,
920 OperandTraits<StoreInst>::op_begin(this),
921 OperandTraits<StoreInst>::operands(this),
922 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000923 Op<0>() = val;
924 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000925 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000926 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000927 AssertOK();
928}
929
Misha Brukmanb1c93172005-04-21 23:48:37 +0000930StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000931 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000932 : Instruction(Type::VoidTy, Store,
933 OperandTraits<StoreInst>::op_begin(this),
934 OperandTraits<StoreInst>::operands(this),
935 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000936 Op<0>() = val;
937 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000938 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000939 setAlignment(0);
940 AssertOK();
941}
942
943StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
944 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000945 : Instruction(Type::VoidTy, Store,
946 OperandTraits<StoreInst>::op_begin(this),
947 OperandTraits<StoreInst>::operands(this),
948 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000949 Op<0>() = val;
950 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000951 setVolatile(isVolatile);
952 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000953 AssertOK();
954}
955
Misha Brukmanb1c93172005-04-21 23:48:37 +0000956StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000957 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000958 : Instruction(Type::VoidTy, Store,
959 OperandTraits<StoreInst>::op_begin(this),
960 OperandTraits<StoreInst>::operands(this),
961 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000962 Op<0>() = val;
963 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000964 setVolatile(isVolatile);
965 setAlignment(Align);
966 AssertOK();
967}
968
969StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000970 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000971 : Instruction(Type::VoidTy, Store,
972 OperandTraits<StoreInst>::op_begin(this),
973 OperandTraits<StoreInst>::operands(this),
974 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000975 Op<0>() = val;
976 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000977 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000978 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000979 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000980}
981
Christopher Lamb84485702007-04-22 19:24:39 +0000982void StoreInst::setAlignment(unsigned Align) {
983 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
984 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
985}
986
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000987//===----------------------------------------------------------------------===//
988// GetElementPtrInst Implementation
989//===----------------------------------------------------------------------===//
990
Christopher Lambedf07882007-12-17 01:12:55 +0000991static unsigned retrieveAddrSpace(const Value *Val) {
992 return cast<PointerType>(Val->getType())->getAddressSpace();
993}
994
Chris Lattner79807c3d2007-01-31 19:47:18 +0000995void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000996 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
997 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000998 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000999
Chris Lattner79807c3d2007-01-31 19:47:18 +00001000 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001001 OL[i+1] = Idx[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001002}
1003
Chris Lattner82981202005-05-03 05:43:30 +00001004void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001005 assert(NumOperands == 2 && "NumOperands not initialized?");
1006 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001007 OL[0] = Ptr;
1008 OL[1] = Idx;
Chris Lattner82981202005-05-03 05:43:30 +00001009}
1010
Gabor Greiff6caff662008-05-10 08:32:32 +00001011GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001012 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001013 OperandTraits<GetElementPtrInst>::op_end(this)
1014 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001015 GEPI.getNumOperands()) {
1016 Use *OL = OperandList;
1017 Use *GEPIOL = GEPI.OperandList;
1018 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001019 OL[i] = GEPIOL[i];
Gabor Greiff6caff662008-05-10 08:32:32 +00001020}
1021
Chris Lattner82981202005-05-03 05:43:30 +00001022GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1023 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001024 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001025 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001026 GetElementPtr,
1027 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1028 2, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +00001029 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001030 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001031}
1032
1033GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1034 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001035 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001036 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001037 GetElementPtr,
1038 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1039 2, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +00001040 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001041 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001042}
1043
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001044// getIndexedType - Returns the type of the element that would be loaded with
1045// a load instruction with the specified parameters.
1046//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001047// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001048// pointer type.
1049//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001050const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001051 Value* const *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001052 unsigned NumIdx) {
1053 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1054 if (!PTy) return 0; // Type isn't a pointer type!
1055 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001056
1057 // Handle the special case of the empty set index set...
Dan Gohman12fce772008-05-15 19:50:34 +00001058 if (NumIdx == 0)
1059 return Agg;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001060
Dan Gohman1ecaf452008-05-31 00:58:22 +00001061 unsigned CurIdx = 1;
1062 for (; CurIdx != NumIdx; ++CurIdx) {
1063 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1064 if (!CT || isa<PointerType>(CT)) return 0;
1065 Value *Index = Idxs[CurIdx];
1066 if (!CT->indexValid(Index)) return 0;
1067 Agg = CT->getTypeAtIndex(Index);
1068
1069 // If the new type forwards to another type, then it is in the middle
1070 // of being refined to another type (and hence, may have dropped all
1071 // references to what it was using before). So, use the new forwarded
1072 // type.
1073 if (const Type *Ty = Agg->getForwardedType())
1074 Agg = Ty;
1075 }
1076 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001077}
1078
Chris Lattner82981202005-05-03 05:43:30 +00001079const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1080 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1081 if (!PTy) return 0; // Type isn't a pointer type!
1082
1083 // Check the pointer index.
1084 if (!PTy->indexValid(Idx)) return 0;
1085
Chris Lattnerc2233332005-05-03 16:44:45 +00001086 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001087}
1088
Chris Lattner45f15572007-04-14 00:12:57 +00001089
1090/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1091/// zeros. If so, the result pointer and the first operand have the same
1092/// value, just potentially different types.
1093bool GetElementPtrInst::hasAllZeroIndices() const {
1094 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1095 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1096 if (!CI->isZero()) return false;
1097 } else {
1098 return false;
1099 }
1100 }
1101 return true;
1102}
1103
Chris Lattner27058292007-04-27 20:35:56 +00001104/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1105/// constant integers. If so, the result pointer and the first operand have
1106/// a constant offset between them.
1107bool GetElementPtrInst::hasAllConstantIndices() const {
1108 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1109 if (!isa<ConstantInt>(getOperand(i)))
1110 return false;
1111 }
1112 return true;
1113}
1114
Chris Lattner45f15572007-04-14 00:12:57 +00001115
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001116//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001117// ExtractElementInst Implementation
1118//===----------------------------------------------------------------------===//
1119
1120ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001121 const std::string &Name,
1122 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001123 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001124 ExtractElement,
1125 OperandTraits<ExtractElementInst>::op_begin(this),
1126 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001127 assert(isValidOperands(Val, Index) &&
1128 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001129 Op<0>() = Val;
1130 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001131 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001132}
1133
Chris Lattner65511ff2006-10-05 06:24:58 +00001134ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1135 const std::string &Name,
1136 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001137 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001138 ExtractElement,
1139 OperandTraits<ExtractElementInst>::op_begin(this),
1140 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001141 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001142 assert(isValidOperands(Val, Index) &&
1143 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001144 Op<0>() = Val;
1145 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001146 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001147}
1148
1149
Robert Bocchino23004482006-01-10 19:05:34 +00001150ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001151 const std::string &Name,
1152 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001153 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001154 ExtractElement,
1155 OperandTraits<ExtractElementInst>::op_begin(this),
1156 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001157 assert(isValidOperands(Val, Index) &&
1158 "Invalid extractelement instruction operands!");
1159
Gabor Greif2d3024d2008-05-26 21:33:52 +00001160 Op<0>() = Val;
1161 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001162 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001163}
1164
Chris Lattner65511ff2006-10-05 06:24:58 +00001165ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1166 const std::string &Name,
1167 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001168 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001169 ExtractElement,
1170 OperandTraits<ExtractElementInst>::op_begin(this),
1171 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001172 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001173 assert(isValidOperands(Val, Index) &&
1174 "Invalid extractelement instruction operands!");
1175
Gabor Greif2d3024d2008-05-26 21:33:52 +00001176 Op<0>() = Val;
1177 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001178 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001179}
1180
1181
Chris Lattner54865b32006-04-08 04:05:48 +00001182bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001183 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001184 return false;
1185 return true;
1186}
1187
1188
Robert Bocchino23004482006-01-10 19:05:34 +00001189//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001190// InsertElementInst Implementation
1191//===----------------------------------------------------------------------===//
1192
Chris Lattner0875d942006-04-14 22:20:32 +00001193InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001194 : Instruction(IE.getType(), InsertElement,
1195 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001196 Op<0>() = IE.Op<0>();
1197 Op<1>() = IE.Op<1>();
1198 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001199}
Chris Lattner54865b32006-04-08 04:05:48 +00001200InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001201 const std::string &Name,
1202 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001203 : Instruction(Vec->getType(), InsertElement,
1204 OperandTraits<InsertElementInst>::op_begin(this),
1205 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001206 assert(isValidOperands(Vec, Elt, Index) &&
1207 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001208 Op<0>() = Vec;
1209 Op<1>() = Elt;
1210 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001211 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001212}
1213
Chris Lattner65511ff2006-10-05 06:24:58 +00001214InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1215 const std::string &Name,
1216 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001217 : Instruction(Vec->getType(), InsertElement,
1218 OperandTraits<InsertElementInst>::op_begin(this),
1219 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001220 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001221 assert(isValidOperands(Vec, Elt, Index) &&
1222 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001223 Op<0>() = Vec;
1224 Op<1>() = Elt;
1225 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001226 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001227}
1228
1229
Chris Lattner54865b32006-04-08 04:05:48 +00001230InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001231 const std::string &Name,
1232 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001233 : Instruction(Vec->getType(), InsertElement,
1234 OperandTraits<InsertElementInst>::op_begin(this),
1235 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001236 assert(isValidOperands(Vec, Elt, Index) &&
1237 "Invalid insertelement instruction operands!");
1238
Gabor Greif2d3024d2008-05-26 21:33:52 +00001239 Op<0>() = Vec;
1240 Op<1>() = Elt;
1241 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001242 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001243}
1244
Chris Lattner65511ff2006-10-05 06:24:58 +00001245InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1246 const std::string &Name,
1247 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001248: Instruction(Vec->getType(), InsertElement,
1249 OperandTraits<InsertElementInst>::op_begin(this),
1250 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001251 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001252 assert(isValidOperands(Vec, Elt, Index) &&
1253 "Invalid insertelement instruction operands!");
1254
Gabor Greif2d3024d2008-05-26 21:33:52 +00001255 Op<0>() = Vec;
1256 Op<1>() = Elt;
1257 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001258 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001259}
1260
Chris Lattner54865b32006-04-08 04:05:48 +00001261bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1262 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001263 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001264 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001265
Reid Spencerd84d35b2007-02-15 02:26:10 +00001266 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001267 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001268
Reid Spencer8d9336d2006-12-31 05:26:44 +00001269 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001270 return false; // Third operand of insertelement must be uint.
1271 return true;
1272}
1273
1274
Robert Bocchinoca27f032006-01-17 20:07:22 +00001275//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001276// ShuffleVectorInst Implementation
1277//===----------------------------------------------------------------------===//
1278
Chris Lattner0875d942006-04-14 22:20:32 +00001279ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001280 : Instruction(SV.getType(), ShuffleVector,
1281 OperandTraits<ShuffleVectorInst>::op_begin(this),
1282 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001283 Op<0>() = SV.Op<0>();
1284 Op<1>() = SV.Op<1>();
1285 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001286}
1287
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001288ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1289 const std::string &Name,
1290 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001291 : Instruction(V1->getType(), ShuffleVector,
1292 OperandTraits<ShuffleVectorInst>::op_begin(this),
1293 OperandTraits<ShuffleVectorInst>::operands(this),
1294 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001295 assert(isValidOperands(V1, V2, Mask) &&
1296 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001297 Op<0>() = V1;
1298 Op<1>() = V2;
1299 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001300 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001301}
1302
1303ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1304 const std::string &Name,
1305 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001306 : Instruction(V1->getType(), ShuffleVector,
1307 OperandTraits<ShuffleVectorInst>::op_begin(this),
1308 OperandTraits<ShuffleVectorInst>::operands(this),
1309 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001310 assert(isValidOperands(V1, V2, Mask) &&
1311 "Invalid shuffle vector instruction operands!");
1312
Gabor Greif2d3024d2008-05-26 21:33:52 +00001313 Op<0>() = V1;
1314 Op<1>() = V2;
1315 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001316 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001317}
1318
1319bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1320 const Value *Mask) {
Chris Lattnerf724e342008-03-02 05:28:33 +00001321 if (!isa<VectorType>(V1->getType()) ||
1322 V1->getType() != V2->getType())
1323 return false;
1324
1325 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1326 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1327 MaskTy->getElementType() != Type::Int32Ty ||
1328 MaskTy->getNumElements() !=
1329 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001330 return false;
1331 return true;
1332}
1333
Chris Lattnerf724e342008-03-02 05:28:33 +00001334/// getMaskValue - Return the index from the shuffle mask for the specified
1335/// output result. This is either -1 if the element is undef or a number less
1336/// than 2*numelements.
1337int ShuffleVectorInst::getMaskValue(unsigned i) const {
1338 const Constant *Mask = cast<Constant>(getOperand(2));
1339 if (isa<UndefValue>(Mask)) return -1;
1340 if (isa<ConstantAggregateZero>(Mask)) return 0;
1341 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1342 assert(i < MaskCV->getNumOperands() && "Index out of range");
1343
1344 if (isa<UndefValue>(MaskCV->getOperand(i)))
1345 return -1;
1346 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1347}
1348
Dan Gohman12fce772008-05-15 19:50:34 +00001349//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001350// InsertValueInst Class
1351//===----------------------------------------------------------------------===//
1352
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001353void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1354 unsigned NumIdx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001355 assert(NumOperands == 2 && "NumOperands not initialized?");
1356 Op<0>() = Agg;
1357 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001358
Dan Gohman1ecaf452008-05-31 00:58:22 +00001359 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001360 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001361}
1362
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001363void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1364 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001365 assert(NumOperands == 2 && "NumOperands not initialized?");
1366 Op<0>() = Agg;
1367 Op<1>() = Val;
1368
1369 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001370 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001371}
1372
1373InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001374 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001375 OperandTraits<InsertValueInst>::op_begin(this), 2),
1376 Indices(IVI.Indices) {
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
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001405void ExtractValueInst::init(Value *Agg, const unsigned *Idx, unsigned NumIdx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001406 assert(NumOperands == 1 && "NumOperands not initialized?");
1407 Op<0>() = Agg;
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
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001413void ExtractValueInst::init(Value *Agg, unsigned Idx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001414 assert(NumOperands == 1 && "NumOperands not initialized?");
1415 Op<0>() = Agg;
1416
1417 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001418 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001419}
1420
1421ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
1422 : Instruction(reinterpret_cast<const Type*>(EVI.getType()), ExtractValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001423 OperandTraits<ExtractValueInst>::op_begin(this), 1),
1424 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001425}
1426
Dan Gohman12fce772008-05-15 19:50:34 +00001427// getIndexedType - Returns the type of the element that would be extracted
1428// with an extractvalue instruction with the specified parameters.
1429//
1430// A null type is returned if the indices are invalid for the specified
1431// pointer type.
1432//
1433const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001434 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001435 unsigned NumIdx) {
1436 unsigned CurIdx = 0;
1437 for (; CurIdx != NumIdx; ++CurIdx) {
1438 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001439 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1440 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001441 if (!CT->indexValid(Index)) return 0;
1442 Agg = CT->getTypeAtIndex(Index);
1443
1444 // If the new type forwards to another type, then it is in the middle
1445 // of being refined to another type (and hence, may have dropped all
1446 // references to what it was using before). So, use the new forwarded
1447 // type.
1448 if (const Type *Ty = Agg->getForwardedType())
1449 Agg = Ty;
1450 }
1451 return CurIdx == NumIdx ? Agg : 0;
1452}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001453
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001454ExtractValueInst::ExtractValueInst(Value *Agg,
1455 unsigned Idx,
1456 const std::string &Name,
1457 BasicBlock *InsertAtEnd)
1458 : Instruction(checkType(getIndexedType(Agg->getType(), &Idx, 1)),
1459 ExtractValue,
1460 OperandTraits<ExtractValueInst>::op_begin(this),
1461 1, InsertAtEnd) {
1462 init(Agg, Idx, Name);
1463}
1464
1465ExtractValueInst::ExtractValueInst(Value *Agg,
1466 unsigned Idx,
1467 const std::string &Name,
1468 Instruction *InsertBefore)
1469 : Instruction(checkType(getIndexedType(Agg->getType(), &Idx, 1)),
1470 ExtractValue,
1471 OperandTraits<ExtractValueInst>::op_begin(this),
1472 1, InsertBefore) {
1473 init(Agg, Idx, Name);
1474}
1475
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001476//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001477// BinaryOperator Class
1478//===----------------------------------------------------------------------===//
1479
Chris Lattner2195fc42007-02-24 00:55:48 +00001480BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1481 const Type *Ty, const std::string &Name,
1482 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001483 : Instruction(Ty, iType,
1484 OperandTraits<BinaryOperator>::op_begin(this),
1485 OperandTraits<BinaryOperator>::operands(this),
1486 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001487 Op<0>() = S1;
1488 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001489 init(iType);
1490 setName(Name);
1491}
1492
1493BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1494 const Type *Ty, const std::string &Name,
1495 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001496 : Instruction(Ty, iType,
1497 OperandTraits<BinaryOperator>::op_begin(this),
1498 OperandTraits<BinaryOperator>::operands(this),
1499 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001500 Op<0>() = S1;
1501 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001502 init(iType);
1503 setName(Name);
1504}
1505
1506
1507void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001508 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001509 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001510 assert(LHS->getType() == RHS->getType() &&
1511 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001512#ifndef NDEBUG
1513 switch (iType) {
1514 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001515 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001516 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001517 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001518 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001519 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001520 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001521 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001522 case UDiv:
1523 case SDiv:
1524 assert(getType() == LHS->getType() &&
1525 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001526 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1527 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001528 "Incorrect operand type (not integer) for S/UDIV");
1529 break;
1530 case FDiv:
1531 assert(getType() == LHS->getType() &&
1532 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001533 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1534 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001535 && "Incorrect operand type (not floating point) for FDIV");
1536 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001537 case URem:
1538 case SRem:
1539 assert(getType() == LHS->getType() &&
1540 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001541 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1542 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001543 "Incorrect operand type (not integer) for S/UREM");
1544 break;
1545 case FRem:
1546 assert(getType() == LHS->getType() &&
1547 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001548 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1549 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001550 && "Incorrect operand type (not floating point) for FREM");
1551 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001552 case Shl:
1553 case LShr:
1554 case AShr:
1555 assert(getType() == LHS->getType() &&
1556 "Shift operation should return same type as operands!");
1557 assert(getType()->isInteger() &&
1558 "Shift operation requires integer operands");
1559 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001560 case And: case Or:
1561 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001562 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001563 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001564 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001565 (isa<VectorType>(getType()) &&
1566 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001567 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001568 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001569 default:
1570 break;
1571 }
1572#endif
1573}
1574
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001575BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001576 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001577 Instruction *InsertBefore) {
1578 assert(S1->getType() == S2->getType() &&
1579 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001580 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001581}
1582
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001583BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001584 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001585 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001586 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001587 InsertAtEnd->getInstList().push_back(Res);
1588 return Res;
1589}
1590
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001591BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001592 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001593 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1594 return new BinaryOperator(Instruction::Sub,
1595 zero, Op,
1596 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001597}
1598
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001599BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001600 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001601 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1602 return new BinaryOperator(Instruction::Sub,
1603 zero, Op,
1604 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001605}
1606
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001607BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001608 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001609 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001610 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001611 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001612 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001613 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001614 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001615 }
1616
1617 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001618 Op->getType(), Name, InsertBefore);
1619}
1620
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001621BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001622 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001623 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001624 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001625 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001626 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001627 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001628 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001629 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001630 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001631 }
1632
1633 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001634 Op->getType(), Name, InsertAtEnd);
1635}
1636
1637
1638// isConstantAllOnes - Helper function for several functions below
1639static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001640 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1641 return CI->isAllOnesValue();
1642 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1643 return CV->isAllOnesValue();
1644 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001645}
1646
1647bool BinaryOperator::isNeg(const Value *V) {
1648 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1649 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001650 return Bop->getOperand(0) ==
1651 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001652 return false;
1653}
1654
1655bool BinaryOperator::isNot(const Value *V) {
1656 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1657 return (Bop->getOpcode() == Instruction::Xor &&
1658 (isConstantAllOnes(Bop->getOperand(1)) ||
1659 isConstantAllOnes(Bop->getOperand(0))));
1660 return false;
1661}
1662
Chris Lattner2c7d1772005-04-24 07:28:37 +00001663Value *BinaryOperator::getNegArgument(Value *BinOp) {
1664 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1665 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001666}
1667
Chris Lattner2c7d1772005-04-24 07:28:37 +00001668const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1669 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001670}
1671
Chris Lattner2c7d1772005-04-24 07:28:37 +00001672Value *BinaryOperator::getNotArgument(Value *BinOp) {
1673 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1674 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1675 Value *Op0 = BO->getOperand(0);
1676 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001677 if (isConstantAllOnes(Op0)) return Op1;
1678
1679 assert(isConstantAllOnes(Op1));
1680 return Op0;
1681}
1682
Chris Lattner2c7d1772005-04-24 07:28:37 +00001683const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1684 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001685}
1686
1687
1688// swapOperands - Exchange the two operands to this instruction. This
1689// instruction is safe to use on any binary instruction and does not
1690// modify the semantics of the instruction. If the instruction is
1691// order dependent (SetLT f.e.) the opcode is changed.
1692//
1693bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001694 if (!isCommutative())
1695 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001696 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001697 return false;
1698}
1699
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001700//===----------------------------------------------------------------------===//
1701// CastInst Class
1702//===----------------------------------------------------------------------===//
1703
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001704// Just determine if this cast only deals with integral->integral conversion.
1705bool CastInst::isIntegerCast() const {
1706 switch (getOpcode()) {
1707 default: return false;
1708 case Instruction::ZExt:
1709 case Instruction::SExt:
1710 case Instruction::Trunc:
1711 return true;
1712 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001713 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001714 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001715}
1716
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001717bool CastInst::isLosslessCast() const {
1718 // Only BitCast can be lossless, exit fast if we're not BitCast
1719 if (getOpcode() != Instruction::BitCast)
1720 return false;
1721
1722 // Identity cast is always lossless
1723 const Type* SrcTy = getOperand(0)->getType();
1724 const Type* DstTy = getType();
1725 if (SrcTy == DstTy)
1726 return true;
1727
Reid Spencer8d9336d2006-12-31 05:26:44 +00001728 // Pointer to pointer is always lossless.
1729 if (isa<PointerType>(SrcTy))
1730 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001731 return false; // Other types have no identity values
1732}
1733
1734/// This function determines if the CastInst does not require any bits to be
1735/// changed in order to effect the cast. Essentially, it identifies cases where
1736/// no code gen is necessary for the cast, hence the name no-op cast. For
1737/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001738/// # bitcast i32* %x to i8*
1739/// # bitcast <2 x i32> %x to <4 x i16>
1740/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001741/// @brief Determine if a cast is a no-op.
1742bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1743 switch (getOpcode()) {
1744 default:
1745 assert(!"Invalid CastOp");
1746 case Instruction::Trunc:
1747 case Instruction::ZExt:
1748 case Instruction::SExt:
1749 case Instruction::FPTrunc:
1750 case Instruction::FPExt:
1751 case Instruction::UIToFP:
1752 case Instruction::SIToFP:
1753 case Instruction::FPToUI:
1754 case Instruction::FPToSI:
1755 return false; // These always modify bits
1756 case Instruction::BitCast:
1757 return true; // BitCast never modifies bits.
1758 case Instruction::PtrToInt:
1759 return IntPtrTy->getPrimitiveSizeInBits() ==
1760 getType()->getPrimitiveSizeInBits();
1761 case Instruction::IntToPtr:
1762 return IntPtrTy->getPrimitiveSizeInBits() ==
1763 getOperand(0)->getType()->getPrimitiveSizeInBits();
1764 }
1765}
1766
1767/// This function determines if a pair of casts can be eliminated and what
1768/// opcode should be used in the elimination. This assumes that there are two
1769/// instructions like this:
1770/// * %F = firstOpcode SrcTy %x to MidTy
1771/// * %S = secondOpcode MidTy %F to DstTy
1772/// The function returns a resultOpcode so these two casts can be replaced with:
1773/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1774/// If no such cast is permited, the function returns 0.
1775unsigned CastInst::isEliminableCastPair(
1776 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1777 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1778{
1779 // Define the 144 possibilities for these two cast instructions. The values
1780 // in this matrix determine what to do in a given situation and select the
1781 // case in the switch below. The rows correspond to firstOp, the columns
1782 // correspond to secondOp. In looking at the table below, keep in mind
1783 // the following cast properties:
1784 //
1785 // Size Compare Source Destination
1786 // Operator Src ? Size Type Sign Type Sign
1787 // -------- ------------ ------------------- ---------------------
1788 // TRUNC > Integer Any Integral Any
1789 // ZEXT < Integral Unsigned Integer Any
1790 // SEXT < Integral Signed Integer Any
1791 // FPTOUI n/a FloatPt n/a Integral Unsigned
1792 // FPTOSI n/a FloatPt n/a Integral Signed
1793 // UITOFP n/a Integral Unsigned FloatPt n/a
1794 // SITOFP n/a Integral Signed FloatPt n/a
1795 // FPTRUNC > FloatPt n/a FloatPt n/a
1796 // FPEXT < FloatPt n/a FloatPt n/a
1797 // PTRTOINT n/a Pointer n/a Integral Unsigned
1798 // INTTOPTR n/a Integral Unsigned Pointer n/a
1799 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001800 //
1801 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1802 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1803 // into "fptoui double to ulong", but this loses information about the range
1804 // of the produced value (we no longer know the top-part is all zeros).
1805 // Further this conversion is often much more expensive for typical hardware,
1806 // and causes issues when building libgcc. We disallow fptosi+sext for the
1807 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001808 const unsigned numCastOps =
1809 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1810 static const uint8_t CastResults[numCastOps][numCastOps] = {
1811 // T F F U S F F P I B -+
1812 // R Z S P P I I T P 2 N T |
1813 // U E E 2 2 2 2 R E I T C +- secondOp
1814 // N X X U S F F N X N 2 V |
1815 // C T T I I P P C T T P T -+
1816 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1817 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1818 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001819 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1820 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001821 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1822 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1823 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1824 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1825 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1826 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1827 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1828 };
1829
1830 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1831 [secondOp-Instruction::CastOpsBegin];
1832 switch (ElimCase) {
1833 case 0:
1834 // categorically disallowed
1835 return 0;
1836 case 1:
1837 // allowed, use first cast's opcode
1838 return firstOp;
1839 case 2:
1840 // allowed, use second cast's opcode
1841 return secondOp;
1842 case 3:
1843 // no-op cast in second op implies firstOp as long as the DestTy
1844 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001845 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001846 return firstOp;
1847 return 0;
1848 case 4:
1849 // no-op cast in second op implies firstOp as long as the DestTy
1850 // is floating point
1851 if (DstTy->isFloatingPoint())
1852 return firstOp;
1853 return 0;
1854 case 5:
1855 // no-op cast in first op implies secondOp as long as the SrcTy
1856 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001857 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001858 return secondOp;
1859 return 0;
1860 case 6:
1861 // no-op cast in first op implies secondOp as long as the SrcTy
1862 // is a floating point
1863 if (SrcTy->isFloatingPoint())
1864 return secondOp;
1865 return 0;
1866 case 7: {
1867 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1868 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1869 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1870 if (MidSize >= PtrSize)
1871 return Instruction::BitCast;
1872 return 0;
1873 }
1874 case 8: {
1875 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1876 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1877 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1878 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1879 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1880 if (SrcSize == DstSize)
1881 return Instruction::BitCast;
1882 else if (SrcSize < DstSize)
1883 return firstOp;
1884 return secondOp;
1885 }
1886 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1887 return Instruction::ZExt;
1888 case 10:
1889 // fpext followed by ftrunc is allowed if the bit size returned to is
1890 // the same as the original, in which case its just a bitcast
1891 if (SrcTy == DstTy)
1892 return Instruction::BitCast;
1893 return 0; // If the types are not the same we can't eliminate it.
1894 case 11:
1895 // bitcast followed by ptrtoint is allowed as long as the bitcast
1896 // is a pointer to pointer cast.
1897 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1898 return secondOp;
1899 return 0;
1900 case 12:
1901 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1902 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1903 return firstOp;
1904 return 0;
1905 case 13: {
1906 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1907 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1908 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1909 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1910 if (SrcSize <= PtrSize && SrcSize == DstSize)
1911 return Instruction::BitCast;
1912 return 0;
1913 }
1914 case 99:
1915 // cast combination can't happen (error in input). This is for all cases
1916 // where the MidTy is not the same for the two cast instructions.
1917 assert(!"Invalid Cast Combination");
1918 return 0;
1919 default:
1920 assert(!"Error in CastResults table!!!");
1921 return 0;
1922 }
1923 return 0;
1924}
1925
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001926CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001927 const std::string &Name, Instruction *InsertBefore) {
1928 // Construct and return the appropriate CastInst subclass
1929 switch (op) {
1930 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1931 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1932 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1933 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1934 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1935 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1936 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1937 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1938 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1939 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1940 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1941 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1942 default:
1943 assert(!"Invalid opcode provided");
1944 }
1945 return 0;
1946}
1947
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001948CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001949 const std::string &Name, BasicBlock *InsertAtEnd) {
1950 // Construct and return the appropriate CastInst subclass
1951 switch (op) {
1952 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1953 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1954 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1955 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1956 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1957 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1958 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1959 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1960 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1961 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1962 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1963 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1964 default:
1965 assert(!"Invalid opcode provided");
1966 }
1967 return 0;
1968}
1969
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001970CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001971 const std::string &Name,
1972 Instruction *InsertBefore) {
1973 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001974 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1975 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001976}
1977
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001978CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001979 const std::string &Name,
1980 BasicBlock *InsertAtEnd) {
1981 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001982 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1983 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001984}
1985
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001986CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001987 const std::string &Name,
1988 Instruction *InsertBefore) {
1989 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001990 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1991 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001992}
1993
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001994CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001995 const std::string &Name,
1996 BasicBlock *InsertAtEnd) {
1997 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001998 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1999 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002000}
2001
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002002CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002003 const std::string &Name,
2004 Instruction *InsertBefore) {
2005 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002006 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2007 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002008}
2009
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002010CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002011 const std::string &Name,
2012 BasicBlock *InsertAtEnd) {
2013 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002014 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2015 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002016}
2017
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002018CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002019 const std::string &Name,
2020 BasicBlock *InsertAtEnd) {
2021 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002022 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002023 "Invalid cast");
2024
Chris Lattner03c49532007-01-15 02:27:26 +00002025 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002026 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2027 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002028}
2029
2030/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002031CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002032 const std::string &Name,
2033 Instruction *InsertBefore) {
2034 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002035 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002036 "Invalid cast");
2037
Chris Lattner03c49532007-01-15 02:27:26 +00002038 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002039 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2040 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002041}
2042
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002043CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002044 bool isSigned, const std::string &Name,
2045 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002046 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002047 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2048 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2049 Instruction::CastOps opcode =
2050 (SrcBits == DstBits ? Instruction::BitCast :
2051 (SrcBits > DstBits ? Instruction::Trunc :
2052 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002053 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002054}
2055
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002056CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002057 bool isSigned, const std::string &Name,
2058 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00002059 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002060 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2061 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2062 Instruction::CastOps opcode =
2063 (SrcBits == DstBits ? Instruction::BitCast :
2064 (SrcBits > DstBits ? Instruction::Trunc :
2065 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002066 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002067}
2068
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002069CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002070 const std::string &Name,
2071 Instruction *InsertBefore) {
2072 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2073 "Invalid cast");
2074 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2075 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2076 Instruction::CastOps opcode =
2077 (SrcBits == DstBits ? Instruction::BitCast :
2078 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002079 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002080}
2081
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002082CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002083 const std::string &Name,
2084 BasicBlock *InsertAtEnd) {
2085 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2086 "Invalid cast");
2087 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2088 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2089 Instruction::CastOps opcode =
2090 (SrcBits == DstBits ? Instruction::BitCast :
2091 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002092 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002093}
2094
Duncan Sands55e50902008-01-06 10:12:28 +00002095// Check whether it is valid to call getCastOpcode for these types.
2096// This routine must be kept in sync with getCastOpcode.
2097bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2098 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2099 return false;
2100
2101 if (SrcTy == DestTy)
2102 return true;
2103
2104 // Get the bit sizes, we'll need these
2105 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2106 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2107
2108 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002109 if (DestTy->isInteger()) { // Casting to integral
2110 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002111 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002112 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002113 return true;
2114 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002115 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002116 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002117 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002118 return isa<PointerType>(SrcTy);
2119 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002120 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2121 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002122 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002123 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002124 return true;
2125 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002126 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002127 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002128 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002129 return false;
2130 }
2131 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002132 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002133 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002134 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002135 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002136 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002137 return DestPTy->getBitWidth() == SrcBits;
2138 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002139 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2140 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002141 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002142 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002143 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002144 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002145 return false;
2146 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002147 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002148 return false;
2149 }
2150}
2151
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002152// Provide a way to get a "cast" where the cast opcode is inferred from the
2153// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002154// logic in the castIsValid function below. This axiom should hold:
2155// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2156// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002157// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002158// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002159Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002160CastInst::getCastOpcode(
2161 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002162 // Get the bit sizes, we'll need these
2163 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002164 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2165 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002166
Duncan Sands55e50902008-01-06 10:12:28 +00002167 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2168 "Only first class types are castable!");
2169
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002170 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002171 if (DestTy->isInteger()) { // Casting to integral
2172 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002173 if (DestBits < SrcBits)
2174 return Trunc; // int -> smaller int
2175 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002176 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002177 return SExt; // signed -> SEXT
2178 else
2179 return ZExt; // unsigned -> ZEXT
2180 } else {
2181 return BitCast; // Same size, No-op cast
2182 }
2183 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002184 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002185 return FPToSI; // FP -> sint
2186 else
2187 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002188 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002189 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002190 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002191 return BitCast; // Same size, no-op cast
2192 } else {
2193 assert(isa<PointerType>(SrcTy) &&
2194 "Casting from a value that is not first-class type");
2195 return PtrToInt; // ptr -> int
2196 }
2197 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002198 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002199 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002200 return SIToFP; // sint -> FP
2201 else
2202 return UIToFP; // uint -> FP
2203 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2204 if (DestBits < SrcBits) {
2205 return FPTrunc; // FP -> smaller FP
2206 } else if (DestBits > SrcBits) {
2207 return FPExt; // FP -> larger FP
2208 } else {
2209 return BitCast; // same size, no-op cast
2210 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002211 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002212 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002213 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002214 return BitCast; // same size, no-op cast
2215 } else {
2216 assert(0 && "Casting pointer or non-first class to float");
2217 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002218 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2219 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002220 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002221 "Casting vector to vector of different widths");
2222 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002223 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002224 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002225 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002226 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002227 }
2228 } else if (isa<PointerType>(DestTy)) {
2229 if (isa<PointerType>(SrcTy)) {
2230 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002231 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002232 return IntToPtr; // int -> ptr
2233 } else {
2234 assert(!"Casting pointer to other than pointer or int");
2235 }
2236 } else {
2237 assert(!"Casting to type that is not first-class");
2238 }
2239
2240 // If we fall through to here we probably hit an assertion cast above
2241 // and assertions are not turned on. Anything we return is an error, so
2242 // BitCast is as good a choice as any.
2243 return BitCast;
2244}
2245
2246//===----------------------------------------------------------------------===//
2247// CastInst SubClass Constructors
2248//===----------------------------------------------------------------------===//
2249
2250/// Check that the construction parameters for a CastInst are correct. This
2251/// could be broken out into the separate constructors but it is useful to have
2252/// it in one place and to eliminate the redundant code for getting the sizes
2253/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002254bool
2255CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002256
2257 // Check for type sanity on the arguments
2258 const Type *SrcTy = S->getType();
2259 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2260 return false;
2261
2262 // Get the size of the types in bits, we'll need this later
2263 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2264 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2265
2266 // Switch on the opcode provided
2267 switch (op) {
2268 default: return false; // This is an input error
2269 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002270 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002271 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002272 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002273 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002274 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002275 case Instruction::FPTrunc:
2276 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2277 SrcBitSize > DstBitSize;
2278 case Instruction::FPExt:
2279 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2280 SrcBitSize < DstBitSize;
2281 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002282 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002283 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2284 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2285 return SVTy->getElementType()->isInteger() &&
2286 DVTy->getElementType()->isFloatingPoint() &&
2287 SVTy->getNumElements() == DVTy->getNumElements();
2288 }
2289 }
Chris Lattner03c49532007-01-15 02:27:26 +00002290 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002291 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002292 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002293 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2294 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2295 return SVTy->getElementType()->isFloatingPoint() &&
2296 DVTy->getElementType()->isInteger() &&
2297 SVTy->getNumElements() == DVTy->getNumElements();
2298 }
2299 }
Chris Lattner03c49532007-01-15 02:27:26 +00002300 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002302 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002303 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002304 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002305 case Instruction::BitCast:
2306 // BitCast implies a no-op cast of type only. No bits change.
2307 // However, you can't cast pointers to anything but pointers.
2308 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2309 return false;
2310
Duncan Sands55e50902008-01-06 10:12:28 +00002311 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002312 // these cases, the cast is okay if the source and destination bit widths
2313 // are identical.
2314 return SrcBitSize == DstBitSize;
2315 }
2316}
2317
2318TruncInst::TruncInst(
2319 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2320) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002321 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002322}
2323
2324TruncInst::TruncInst(
2325 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2326) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002327 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002328}
2329
2330ZExtInst::ZExtInst(
2331 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2332) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002333 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002334}
2335
2336ZExtInst::ZExtInst(
2337 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2338) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002339 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002340}
2341SExtInst::SExtInst(
2342 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2343) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002344 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002345}
2346
Jeff Cohencc08c832006-12-02 02:22:01 +00002347SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002348 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2349) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002350 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002351}
2352
2353FPTruncInst::FPTruncInst(
2354 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2355) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002356 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357}
2358
2359FPTruncInst::FPTruncInst(
2360 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2361) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002362 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363}
2364
2365FPExtInst::FPExtInst(
2366 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2367) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002368 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002369}
2370
2371FPExtInst::FPExtInst(
2372 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2373) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002374 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002375}
2376
2377UIToFPInst::UIToFPInst(
2378 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2379) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002380 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002381}
2382
2383UIToFPInst::UIToFPInst(
2384 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2385) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002386 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002387}
2388
2389SIToFPInst::SIToFPInst(
2390 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2391) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002392 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393}
2394
2395SIToFPInst::SIToFPInst(
2396 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2397) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002398 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002399}
2400
2401FPToUIInst::FPToUIInst(
2402 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2403) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002404 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002405}
2406
2407FPToUIInst::FPToUIInst(
2408 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2409) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002410 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002411}
2412
2413FPToSIInst::FPToSIInst(
2414 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2415) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002416 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002417}
2418
2419FPToSIInst::FPToSIInst(
2420 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2421) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002422 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002423}
2424
2425PtrToIntInst::PtrToIntInst(
2426 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2427) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002428 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429}
2430
2431PtrToIntInst::PtrToIntInst(
2432 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2433) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002434 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435}
2436
2437IntToPtrInst::IntToPtrInst(
2438 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2439) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002440 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002441}
2442
2443IntToPtrInst::IntToPtrInst(
2444 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2445) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002446 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447}
2448
2449BitCastInst::BitCastInst(
2450 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2451) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002452 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453}
2454
2455BitCastInst::BitCastInst(
2456 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2457) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002458 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002460
2461//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002462// CmpInst Classes
2463//===----------------------------------------------------------------------===//
2464
Nate Begemand2195702008-05-12 19:01:56 +00002465CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2466 Value *LHS, Value *RHS, const std::string &Name,
2467 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002468 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002469 OperandTraits<CmpInst>::op_begin(this),
2470 OperandTraits<CmpInst>::operands(this),
2471 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002472 Op<0>() = LHS;
2473 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002474 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002475 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002476}
Gabor Greiff6caff662008-05-10 08:32:32 +00002477
Nate Begemand2195702008-05-12 19:01:56 +00002478CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2479 Value *LHS, Value *RHS, const std::string &Name,
2480 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002481 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002482 OperandTraits<CmpInst>::op_begin(this),
2483 OperandTraits<CmpInst>::operands(this),
2484 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002485 Op<0>() = LHS;
2486 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002487 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002488 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002489}
2490
2491CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002492CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002493 const std::string &Name, Instruction *InsertBefore) {
2494 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002495 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002496 InsertBefore);
2497 }
Nate Begemand2195702008-05-12 19:01:56 +00002498 if (Op == Instruction::FCmp) {
2499 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2500 InsertBefore);
2501 }
2502 if (Op == Instruction::VICmp) {
2503 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2504 InsertBefore);
2505 }
2506 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2507 InsertBefore);
Reid Spencerd9436b62006-11-20 01:22:35 +00002508}
2509
2510CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002511CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002512 const std::string &Name, BasicBlock *InsertAtEnd) {
2513 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002514 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002515 InsertAtEnd);
2516 }
Nate Begemand2195702008-05-12 19:01:56 +00002517 if (Op == Instruction::FCmp) {
2518 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2519 InsertAtEnd);
2520 }
2521 if (Op == Instruction::VICmp) {
2522 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2523 InsertAtEnd);
2524 }
2525 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2526 InsertAtEnd);
Reid Spencerd9436b62006-11-20 01:22:35 +00002527}
2528
2529void CmpInst::swapOperands() {
2530 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2531 IC->swapOperands();
2532 else
2533 cast<FCmpInst>(this)->swapOperands();
2534}
2535
2536bool CmpInst::isCommutative() {
2537 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2538 return IC->isCommutative();
2539 return cast<FCmpInst>(this)->isCommutative();
2540}
2541
2542bool CmpInst::isEquality() {
2543 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2544 return IC->isEquality();
2545 return cast<FCmpInst>(this)->isEquality();
2546}
2547
2548
Dan Gohman4e724382008-05-31 02:47:54 +00002549CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002550 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002551 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002552 case ICMP_EQ: return ICMP_NE;
2553 case ICMP_NE: return ICMP_EQ;
2554 case ICMP_UGT: return ICMP_ULE;
2555 case ICMP_ULT: return ICMP_UGE;
2556 case ICMP_UGE: return ICMP_ULT;
2557 case ICMP_ULE: return ICMP_UGT;
2558 case ICMP_SGT: return ICMP_SLE;
2559 case ICMP_SLT: return ICMP_SGE;
2560 case ICMP_SGE: return ICMP_SLT;
2561 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002562
Dan Gohman4e724382008-05-31 02:47:54 +00002563 case FCMP_OEQ: return FCMP_UNE;
2564 case FCMP_ONE: return FCMP_UEQ;
2565 case FCMP_OGT: return FCMP_ULE;
2566 case FCMP_OLT: return FCMP_UGE;
2567 case FCMP_OGE: return FCMP_ULT;
2568 case FCMP_OLE: return FCMP_UGT;
2569 case FCMP_UEQ: return FCMP_ONE;
2570 case FCMP_UNE: return FCMP_OEQ;
2571 case FCMP_UGT: return FCMP_OLE;
2572 case FCMP_ULT: return FCMP_OGE;
2573 case FCMP_UGE: return FCMP_OLT;
2574 case FCMP_ULE: return FCMP_OGT;
2575 case FCMP_ORD: return FCMP_UNO;
2576 case FCMP_UNO: return FCMP_ORD;
2577 case FCMP_TRUE: return FCMP_FALSE;
2578 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002579 }
2580}
2581
Reid Spencer266e42b2006-12-23 06:05:41 +00002582ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2583 switch (pred) {
2584 default: assert(! "Unknown icmp predicate!");
2585 case ICMP_EQ: case ICMP_NE:
2586 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2587 return pred;
2588 case ICMP_UGT: return ICMP_SGT;
2589 case ICMP_ULT: return ICMP_SLT;
2590 case ICMP_UGE: return ICMP_SGE;
2591 case ICMP_ULE: return ICMP_SLE;
2592 }
2593}
2594
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002595ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2596 switch (pred) {
2597 default: assert(! "Unknown icmp predicate!");
2598 case ICMP_EQ: case ICMP_NE:
2599 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2600 return pred;
2601 case ICMP_SGT: return ICMP_UGT;
2602 case ICMP_SLT: return ICMP_ULT;
2603 case ICMP_SGE: return ICMP_UGE;
2604 case ICMP_SLE: return ICMP_ULE;
2605 }
2606}
2607
Reid Spencer266e42b2006-12-23 06:05:41 +00002608bool ICmpInst::isSignedPredicate(Predicate pred) {
2609 switch (pred) {
2610 default: assert(! "Unknown icmp predicate!");
2611 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2612 return true;
2613 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2614 case ICMP_UGE: case ICMP_ULE:
2615 return false;
2616 }
2617}
2618
Reid Spencer0286bc12007-02-28 22:00:54 +00002619/// Initialize a set of values that all satisfy the condition with C.
2620///
2621ConstantRange
2622ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2623 APInt Lower(C);
2624 APInt Upper(C);
2625 uint32_t BitWidth = C.getBitWidth();
2626 switch (pred) {
2627 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2628 case ICmpInst::ICMP_EQ: Upper++; break;
2629 case ICmpInst::ICMP_NE: Lower++; break;
2630 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2631 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2632 case ICmpInst::ICMP_UGT:
2633 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2634 break;
2635 case ICmpInst::ICMP_SGT:
2636 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2637 break;
2638 case ICmpInst::ICMP_ULE:
2639 Lower = APInt::getMinValue(BitWidth); Upper++;
2640 break;
2641 case ICmpInst::ICMP_SLE:
2642 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2643 break;
2644 case ICmpInst::ICMP_UGE:
2645 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2646 break;
2647 case ICmpInst::ICMP_SGE:
2648 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2649 break;
2650 }
2651 return ConstantRange(Lower, Upper);
2652}
2653
Dan Gohman4e724382008-05-31 02:47:54 +00002654CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002655 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002656 default: assert(!"Unknown cmp predicate!");
2657 case ICMP_EQ: case ICMP_NE:
2658 return pred;
2659 case ICMP_SGT: return ICMP_SLT;
2660 case ICMP_SLT: return ICMP_SGT;
2661 case ICMP_SGE: return ICMP_SLE;
2662 case ICMP_SLE: return ICMP_SGE;
2663 case ICMP_UGT: return ICMP_ULT;
2664 case ICMP_ULT: return ICMP_UGT;
2665 case ICMP_UGE: return ICMP_ULE;
2666 case ICMP_ULE: return ICMP_UGE;
2667
Reid Spencerd9436b62006-11-20 01:22:35 +00002668 case FCMP_FALSE: case FCMP_TRUE:
2669 case FCMP_OEQ: case FCMP_ONE:
2670 case FCMP_UEQ: case FCMP_UNE:
2671 case FCMP_ORD: case FCMP_UNO:
2672 return pred;
2673 case FCMP_OGT: return FCMP_OLT;
2674 case FCMP_OLT: return FCMP_OGT;
2675 case FCMP_OGE: return FCMP_OLE;
2676 case FCMP_OLE: return FCMP_OGE;
2677 case FCMP_UGT: return FCMP_ULT;
2678 case FCMP_ULT: return FCMP_UGT;
2679 case FCMP_UGE: return FCMP_ULE;
2680 case FCMP_ULE: return FCMP_UGE;
2681 }
2682}
2683
Reid Spencer266e42b2006-12-23 06:05:41 +00002684bool CmpInst::isUnsigned(unsigned short predicate) {
2685 switch (predicate) {
2686 default: return false;
2687 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2688 case ICmpInst::ICMP_UGE: return true;
2689 }
2690}
2691
2692bool CmpInst::isSigned(unsigned short predicate){
2693 switch (predicate) {
2694 default: return false;
2695 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2696 case ICmpInst::ICMP_SGE: return true;
2697 }
2698}
2699
2700bool CmpInst::isOrdered(unsigned short predicate) {
2701 switch (predicate) {
2702 default: return false;
2703 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2704 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2705 case FCmpInst::FCMP_ORD: return true;
2706 }
2707}
2708
2709bool CmpInst::isUnordered(unsigned short predicate) {
2710 switch (predicate) {
2711 default: return false;
2712 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2713 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2714 case FCmpInst::FCMP_UNO: return true;
2715 }
2716}
2717
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002718//===----------------------------------------------------------------------===//
2719// SwitchInst Implementation
2720//===----------------------------------------------------------------------===//
2721
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002722void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002723 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002724 ReservedSpace = 2+NumCases*2;
2725 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002726 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002727
Gabor Greif2d3024d2008-05-26 21:33:52 +00002728 OperandList[0] = Value;
2729 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002730}
2731
Chris Lattner2195fc42007-02-24 00:55:48 +00002732/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2733/// switch on and a default destination. The number of additional cases can
2734/// be specified here to make memory allocation more efficient. This
2735/// constructor can also autoinsert before another instruction.
2736SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2737 Instruction *InsertBefore)
2738 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2739 init(Value, Default, NumCases);
2740}
2741
2742/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2743/// switch on and a default destination. The number of additional cases can
2744/// be specified here to make memory allocation more efficient. This
2745/// constructor also autoinserts at the end of the specified BasicBlock.
2746SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2747 BasicBlock *InsertAtEnd)
2748 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2749 init(Value, Default, NumCases);
2750}
2751
Misha Brukmanb1c93172005-04-21 23:48:37 +00002752SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002753 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002754 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002755 Use *OL = OperandList, *InOL = SI.OperandList;
2756 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002757 OL[i] = InOL[i];
2758 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002759 }
2760}
2761
Gordon Henriksen14a55692007-12-10 02:14:30 +00002762SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002763 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002764}
2765
2766
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002767/// addCase - Add an entry to the switch instruction...
2768///
Chris Lattner47ac1872005-02-24 05:32:09 +00002769void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002770 unsigned OpNo = NumOperands;
2771 if (OpNo+2 > ReservedSpace)
2772 resizeOperands(0); // Get more space!
2773 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002774 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002775 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002776 OperandList[OpNo] = OnVal;
2777 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002778}
2779
2780/// removeCase - This method removes the specified successor from the switch
2781/// instruction. Note that this cannot be used to remove the default
2782/// destination (successor #0).
2783///
2784void SwitchInst::removeCase(unsigned idx) {
2785 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002786 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2787
2788 unsigned NumOps = getNumOperands();
2789 Use *OL = OperandList;
2790
2791 // Move everything after this operand down.
2792 //
2793 // FIXME: we could just swap with the end of the list, then erase. However,
2794 // client might not expect this to happen. The code as it is thrashes the
2795 // use/def lists, which is kinda lame.
2796 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2797 OL[i-2] = OL[i];
2798 OL[i-2+1] = OL[i+1];
2799 }
2800
2801 // Nuke the last value.
2802 OL[NumOps-2].set(0);
2803 OL[NumOps-2+1].set(0);
2804 NumOperands = NumOps-2;
2805}
2806
2807/// resizeOperands - resize operands - This adjusts the length of the operands
2808/// list according to the following behavior:
2809/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002810/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002811/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2812/// 3. If NumOps == NumOperands, trim the reserved space.
2813///
2814void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002815 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002816 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002817 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002818 } else if (NumOps*2 > NumOperands) {
2819 // No resize needed.
2820 if (ReservedSpace >= NumOps) return;
2821 } else if (NumOps == NumOperands) {
2822 if (ReservedSpace == NumOps) return;
2823 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002824 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002825 }
2826
2827 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002828 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002829 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002830 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002831 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002832 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002833 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002834 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002835}
2836
2837
2838BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2839 return getSuccessor(idx);
2840}
2841unsigned SwitchInst::getNumSuccessorsV() const {
2842 return getNumSuccessors();
2843}
2844void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2845 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002846}
Chris Lattnerf22be932004-10-15 23:52:53 +00002847
Devang Patel295711f2008-02-19 22:15:16 +00002848//===----------------------------------------------------------------------===//
2849// GetResultInst Implementation
2850//===----------------------------------------------------------------------===//
2851
Devang Patel666d4512008-02-20 19:10:47 +00002852GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002853 const std::string &Name,
2854 Instruction *InsertBef)
Gabor Greif0d42adf2008-05-13 07:09:08 +00002855 : UnaryInstruction(cast<StructType>(Aggregate->getType())
2856 ->getElementType(Index),
2857 GetResult, Aggregate, InsertBef),
2858 Idx(Index) {
2859 assert(isValidOperands(Aggregate, Index)
2860 && "Invalid GetResultInst operands!");
Devang Patel295711f2008-02-19 22:15:16 +00002861 setName(Name);
2862}
2863
Devang Patel666d4512008-02-20 19:10:47 +00002864bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2865 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002866 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002867
Devang Patelcf193b82008-02-20 19:39:41 +00002868 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2869 unsigned NumElements = STy->getNumElements();
Chris Lattnerb6917d22008-04-23 05:36:34 +00002870 if (Index >= NumElements || NumElements == 0)
Devang Patelcf193b82008-02-20 19:39:41 +00002871 return false;
2872
2873 // getresult aggregate value's element types are restricted to
2874 // avoid nested aggregates.
2875 for (unsigned i = 0; i < NumElements; ++i)
2876 if (!STy->getElementType(i)->isFirstClassType())
2877 return false;
2878
2879 // Otherwise, Aggregate is valid.
2880 return true;
2881 }
Devang Patel666d4512008-02-20 19:10:47 +00002882 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002883}
2884
Chris Lattnerf22be932004-10-15 23:52:53 +00002885// Define these methods here so vtables don't get emitted into every translation
2886// unit that uses these classes.
2887
2888GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002889 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002890}
2891
2892BinaryOperator *BinaryOperator::clone() const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002893 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002894}
2895
Chris Lattner0b490b02007-08-24 20:48:18 +00002896FCmpInst* FCmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002897 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002898}
2899ICmpInst* ICmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002900 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002901}
2902
Nate Begemand2195702008-05-12 19:01:56 +00002903VFCmpInst* VFCmpInst::clone() const {
2904 return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
2905}
2906VICmpInst* VICmpInst::clone() const {
2907 return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
2908}
2909
Dan Gohman0752bff2008-05-23 00:36:11 +00002910ExtractValueInst *ExtractValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002911 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002912}
2913InsertValueInst *InsertValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002914 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002915}
2916
2917
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002918MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2919AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2920FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2921LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2922StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2923CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2924CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2925CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2926CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2927CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2928CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2929CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2930CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2931CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2932CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2933CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2934CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002935CallInst *CallInst::clone() const {
2936 return new(getNumOperands()) CallInst(*this);
2937}
2938SelectInst *SelectInst::clone() const {
2939 return new(getNumOperands()) SelectInst(*this);
2940}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002941VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2942
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002943ExtractElementInst *ExtractElementInst::clone() const {
2944 return new ExtractElementInst(*this);
2945}
2946InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002947 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002948}
2949ShuffleVectorInst *ShuffleVectorInst::clone() const {
2950 return new ShuffleVectorInst(*this);
2951}
Chris Lattnerf22be932004-10-15 23:52:53 +00002952PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002953ReturnInst *ReturnInst::clone() const {
2954 return new(getNumOperands()) ReturnInst(*this);
2955}
2956BranchInst *BranchInst::clone() const {
2957 return new(getNumOperands()) BranchInst(*this);
2958}
Gabor Greiff6caff662008-05-10 08:32:32 +00002959SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002960InvokeInst *InvokeInst::clone() const {
2961 return new(getNumOperands()) InvokeInst(*this);
2962}
Chris Lattnerf22be932004-10-15 23:52:53 +00002963UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002964UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002965GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }