blob: 3421292930d490f4995e436b2357ce21a35cf386 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000015#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Function.h"
18#include "llvm/Instructions.h"
19#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000020#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000021#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000022using namespace llvm;
23
Chris Lattner3e13b8c2008-01-02 23:42:30 +000024//===----------------------------------------------------------------------===//
25// CallSite Class
26//===----------------------------------------------------------------------===//
27
Duncan Sands85fab3a2008-02-18 17:32:13 +000028CallSite::CallSite(Instruction *C) {
29 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
30 I = C;
31}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000032unsigned CallSite::getCallingConv() const {
33 if (CallInst *CI = dyn_cast<CallInst>(I))
34 return CI->getCallingConv();
35 else
36 return cast<InvokeInst>(I)->getCallingConv();
37}
38void CallSite::setCallingConv(unsigned CC) {
39 if (CallInst *CI = dyn_cast<CallInst>(I))
40 CI->setCallingConv(CC);
41 else
42 cast<InvokeInst>(I)->setCallingConv(CC);
43}
Chris Lattner8a923e72008-03-12 17:45:29 +000044const PAListPtr &CallSite::getParamAttrs() const {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000045 if (CallInst *CI = dyn_cast<CallInst>(I))
46 return CI->getParamAttrs();
47 else
48 return cast<InvokeInst>(I)->getParamAttrs();
49}
Chris Lattner8a923e72008-03-12 17:45:29 +000050void CallSite::setParamAttrs(const PAListPtr &PAL) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000051 if (CallInst *CI = dyn_cast<CallInst>(I))
52 CI->setParamAttrs(PAL);
53 else
54 cast<InvokeInst>(I)->setParamAttrs(PAL);
55}
Dale Johannesen89268bc2008-02-19 21:38:47 +000056bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
Duncan Sands5208d1a2007-11-28 17:07:01 +000057 if (CallInst *CI = dyn_cast<CallInst>(I))
Dale Johannesen89268bc2008-02-19 21:38:47 +000058 return CI->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000059 else
Dale Johannesen89268bc2008-02-19 21:38:47 +000060 return cast<InvokeInst>(I)->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000061}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000062uint16_t CallSite::getParamAlignment(uint16_t i) const {
63 if (CallInst *CI = dyn_cast<CallInst>(I))
64 return CI->getParamAlignment(i);
65 else
66 return cast<InvokeInst>(I)->getParamAlignment(i);
67}
68
Duncan Sands38ef3a82007-12-03 20:06:50 +000069bool CallSite::doesNotAccessMemory() const {
70 if (CallInst *CI = dyn_cast<CallInst>(I))
71 return CI->doesNotAccessMemory();
72 else
73 return cast<InvokeInst>(I)->doesNotAccessMemory();
74}
75bool CallSite::onlyReadsMemory() const {
76 if (CallInst *CI = dyn_cast<CallInst>(I))
77 return CI->onlyReadsMemory();
78 else
79 return cast<InvokeInst>(I)->onlyReadsMemory();
80}
Duncan Sands3353ed02007-12-18 09:59:50 +000081bool CallSite::doesNotThrow() const {
Duncan Sands8e4847e2007-12-16 15:51:49 +000082 if (CallInst *CI = dyn_cast<CallInst>(I))
Duncan Sands3353ed02007-12-18 09:59:50 +000083 return CI->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000084 else
Duncan Sands3353ed02007-12-18 09:59:50 +000085 return cast<InvokeInst>(I)->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000086}
Duncan Sandsaa31b922007-12-19 21:13:37 +000087void CallSite::setDoesNotThrow(bool doesNotThrow) {
88 if (CallInst *CI = dyn_cast<CallInst>(I))
89 CI->setDoesNotThrow(doesNotThrow);
90 else
91 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
92}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000093
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000094bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000095 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
96 if (AI->get() == Arg)
97 return true;
98 return false;
99}
100
Gordon Henriksen14a55692007-12-10 02:14:30 +0000101//===----------------------------------------------------------------------===//
102// TerminatorInst Class
103//===----------------------------------------------------------------------===//
104
105// Out of line virtual method, so the vtable, etc has a home.
106TerminatorInst::~TerminatorInst() {
107}
108
Gabor Greiff6caff662008-05-10 08:32:32 +0000109//===----------------------------------------------------------------------===//
110// UnaryInstruction Class
111//===----------------------------------------------------------------------===//
112
Gordon Henriksen14a55692007-12-10 02:14:30 +0000113// Out of line virtual method, so the vtable, etc has a home.
114UnaryInstruction::~UnaryInstruction() {
115}
116
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117//===----------------------------------------------------------------------===//
118// PHINode Class
119//===----------------------------------------------------------------------===//
120
121PHINode::PHINode(const PHINode &PN)
122 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000123 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000124 ReservedSpace(PN.getNumOperands()) {
125 Use *OL = OperandList;
126 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000127 OL[i] = PN.getOperand(i);
128 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000129 }
130}
131
Gordon Henriksen14a55692007-12-10 02:14:30 +0000132PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000133 if (OperandList)
134 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000135}
136
137// removeIncomingValue - Remove an incoming value. This is useful if a
138// predecessor basic block is deleted.
139Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
140 unsigned NumOps = getNumOperands();
141 Use *OL = OperandList;
142 assert(Idx*2 < NumOps && "BB not in PHI node!");
143 Value *Removed = OL[Idx*2];
144
145 // Move everything after this operand down.
146 //
147 // FIXME: we could just swap with the end of the list, then erase. However,
148 // client might not expect this to happen. The code as it is thrashes the
149 // use/def lists, which is kinda lame.
150 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
151 OL[i-2] = OL[i];
152 OL[i-2+1] = OL[i+1];
153 }
154
155 // Nuke the last value.
156 OL[NumOps-2].set(0);
157 OL[NumOps-2+1].set(0);
158 NumOperands = NumOps-2;
159
160 // If the PHI node is dead, because it has zero entries, nuke it now.
161 if (NumOps == 2 && DeletePHIIfEmpty) {
162 // If anyone is using this PHI, make them use a dummy value instead...
163 replaceAllUsesWith(UndefValue::get(getType()));
164 eraseFromParent();
165 }
166 return Removed;
167}
168
169/// resizeOperands - resize operands - This adjusts the length of the operands
170/// list according to the following behavior:
171/// 1. If NumOps == 0, grow the operand list in response to a push_back style
172/// of operation. This grows the number of ops by 1.5 times.
173/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
174/// 3. If NumOps == NumOperands, trim the reserved space.
175///
176void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000177 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000178 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000179 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000180 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
181 } else if (NumOps*2 > NumOperands) {
182 // No resize needed.
183 if (ReservedSpace >= NumOps) return;
184 } else if (NumOps == NumOperands) {
185 if (ReservedSpace == NumOps) return;
186 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000187 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000188 }
189
190 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000191 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000192 Use *NewOps = allocHungoffUses(NumOps);
193 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000194 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000195 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000196 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000197 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000198}
199
Nate Begemanb3923212005-08-04 23:24:19 +0000200/// hasConstantValue - If the specified PHI node always merges together the same
201/// value, return the value, otherwise return null.
202///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000203Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000204 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000205 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000206 if (getIncomingValue(0) != this) // not X = phi X
207 return getIncomingValue(0);
208 else
209 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000210 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000211
Nate Begemanb3923212005-08-04 23:24:19 +0000212 // Otherwise if all of the incoming values are the same for the PHI, replace
213 // the PHI node with the incoming value.
214 //
215 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000216 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000217 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000218 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000219 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000220 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000221 if (InVal && getIncomingValue(i) != InVal)
222 return 0; // Not the same, bail out.
223 else
224 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000225 }
Nate Begemanb3923212005-08-04 23:24:19 +0000226
227 // The only case that could cause InVal to be null is if we have a PHI node
228 // that only has entries for itself. In this case, there is no entry into the
229 // loop, so kill the PHI.
230 //
231 if (InVal == 0) InVal = UndefValue::get(getType());
232
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000233 // If we have a PHI node like phi(X, undef, X), where X is defined by some
234 // instruction, we cannot always return X as the result of the PHI node. Only
235 // do this if X is not an instruction (thus it must dominate the PHI block),
236 // or if the client is prepared to deal with this possibility.
237 if (HasUndefInput && !AllowNonDominatingInstruction)
238 if (Instruction *IV = dyn_cast<Instruction>(InVal))
239 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000240 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000241 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000242 return 0; // Cannot guarantee that InVal dominates this PHINode.
243
Nate Begemanb3923212005-08-04 23:24:19 +0000244 // All of the incoming values are the same, return the value now.
245 return InVal;
246}
247
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000248
249//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000250// CallInst Implementation
251//===----------------------------------------------------------------------===//
252
Gordon Henriksen14a55692007-12-10 02:14:30 +0000253CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000254}
255
Chris Lattner054ba2c2007-02-13 00:58:44 +0000256void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000257 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
258 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000259 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260
Misha Brukmanb1c93172005-04-21 23:48:37 +0000261 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000263 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000264
Chris Lattner054ba2c2007-02-13 00:58:44 +0000265 assert((NumParams == FTy->getNumParams() ||
266 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000267 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000268 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000269 assert((i >= FTy->getNumParams() ||
270 FTy->getParamType(i) == Params[i]->getType()) &&
271 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000272 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000273 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000274}
275
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000276void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000277 assert(NumOperands == 3 && "NumOperands not set up?");
278 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000279 OL[0] = Func;
280 OL[1] = Actual1;
281 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000282
283 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000285 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000287 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000288 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000290 assert((0 >= FTy->getNumParams() ||
291 FTy->getParamType(0) == Actual1->getType()) &&
292 "Calling a function with a bad signature!");
293 assert((1 >= FTy->getNumParams() ||
294 FTy->getParamType(1) == Actual2->getType()) &&
295 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296}
297
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000298void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000299 assert(NumOperands == 2 && "NumOperands not set up?");
300 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000301 OL[0] = Func;
302 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000303
304 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000305 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000306 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000308 assert((FTy->getNumParams() == 1 ||
309 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000310 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000311 assert((0 == FTy->getNumParams() ||
312 FTy->getParamType(0) == Actual->getType()) &&
313 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314}
315
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000316void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000317 assert(NumOperands == 1 && "NumOperands not set up?");
318 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000319 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000320
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000321 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000323 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000325 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000326}
327
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000328CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000329 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
331 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000332 Instruction::Call,
333 OperandTraits<CallInst>::op_end(this) - 2,
334 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000336 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000337}
338
339CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
340 BasicBlock *InsertAtEnd)
341 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
342 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000343 Instruction::Call,
344 OperandTraits<CallInst>::op_end(this) - 2,
345 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000346 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000347 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000349CallInst::CallInst(Value *Func, const std::string &Name,
350 Instruction *InsertBefore)
351 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
352 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000353 Instruction::Call,
354 OperandTraits<CallInst>::op_end(this) - 1,
355 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000356 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000357 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000358}
359
360CallInst::CallInst(Value *Func, const std::string &Name,
361 BasicBlock *InsertAtEnd)
362 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
363 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000364 Instruction::Call,
365 OperandTraits<CallInst>::op_end(this) - 1,
366 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000367 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000368 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000369}
370
Misha Brukmanb1c93172005-04-21 23:48:37 +0000371CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000372 : Instruction(CI.getType(), Instruction::Call,
373 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000374 CI.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000375 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000376 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000377 Use *OL = OperandList;
378 Use *InOL = CI.OperandList;
379 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000380 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000381}
382
Eric Christopher901b1a72008-05-16 20:39:43 +0000383void CallInst::addParamAttr(unsigned i, ParameterAttributes attr) {
384 PAListPtr PAL = getParamAttrs();
385 PAL = PAL.addAttr(i, attr);
386 setParamAttrs(PAL);
387}
388
Chris Lattnerc994c022008-03-13 05:00:21 +0000389bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000390 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000391 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000392 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000393 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000394 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000395}
396
Duncan Sandsaa31b922007-12-19 21:13:37 +0000397void CallInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000398 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000399 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000400 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000401 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000402 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000403 setParamAttrs(PAL);
404}
405
Duncan Sands5208d1a2007-11-28 17:07:01 +0000406
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000407//===----------------------------------------------------------------------===//
408// InvokeInst Implementation
409//===----------------------------------------------------------------------===//
410
411void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000412 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000413 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
414 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000415 OL[0] = Fn;
416 OL[1] = IfNormal;
417 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000418 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000419 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000420 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000421
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000422 assert(((NumArgs == FTy->getNumParams()) ||
423 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000424 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000425
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000426 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000427 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000428 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000429 "Invoking a function with a bad signature!");
430
Gabor Greif2d3024d2008-05-26 21:33:52 +0000431 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000432 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000433}
434
Misha Brukmanb1c93172005-04-21 23:48:37 +0000435InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000436 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000437 OperandTraits<InvokeInst>::op_end(this)
438 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000439 II.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000440 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000441 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000442 Use *OL = OperandList, *InOL = II.OperandList;
443 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000444 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000445}
446
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000447BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
448 return getSuccessor(idx);
449}
450unsigned InvokeInst::getNumSuccessorsV() const {
451 return getNumSuccessors();
452}
453void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
454 return setSuccessor(idx, B);
455}
456
Chris Lattnerc994c022008-03-13 05:00:21 +0000457bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000458 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000459 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000460 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000461 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000462 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000463}
464
Eric Christopher901b1a72008-05-16 20:39:43 +0000465void InvokeInst::addParamAttr(unsigned i, ParameterAttributes attr) {
466 PAListPtr PAL = getParamAttrs();
467 PAL = PAL.addAttr(i, attr);
468 setParamAttrs(PAL);
469}
470
Duncan Sandsaa31b922007-12-19 21:13:37 +0000471void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000472 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000473 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000474 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000475 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000476 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000477 setParamAttrs(PAL);
478}
479
Duncan Sands5208d1a2007-11-28 17:07:01 +0000480
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000481//===----------------------------------------------------------------------===//
482// ReturnInst Implementation
483//===----------------------------------------------------------------------===//
484
Chris Lattner2195fc42007-02-24 00:55:48 +0000485ReturnInst::ReturnInst(const ReturnInst &RI)
486 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Gabor Greif697e94c2008-05-15 10:04:30 +0000487 OperandTraits<ReturnInst>::op_end(this)
488 - RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000489 RI.getNumOperands()) {
Devang Patel59643e52008-02-23 00:35:18 +0000490 unsigned N = RI.getNumOperands();
Gabor Greiff6caff662008-05-10 08:32:32 +0000491 if (N == 1)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000492 Op<0>() = RI.Op<0>();
Devang Patelae682fb2008-02-26 17:56:20 +0000493 else if (N) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000494 Use *OL = OperandList;
Devang Patelae682fb2008-02-26 17:56:20 +0000495 for (unsigned i = 0; i < N; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000496 OL[i] = RI.getOperand(i);
Devang Patelae682fb2008-02-26 17:56:20 +0000497 }
Chris Lattner2195fc42007-02-24 00:55:48 +0000498}
499
500ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000501 : TerminatorInst(Type::VoidTy, Instruction::Ret,
502 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
503 retVal != 0, InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000504 if (retVal)
505 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000506}
507ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000508 : TerminatorInst(Type::VoidTy, Instruction::Ret,
509 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
510 retVal != 0, InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000511 if (retVal)
512 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000513}
514ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000515 : TerminatorInst(Type::VoidTy, Instruction::Ret,
516 OperandTraits<ReturnInst>::op_end(this),
517 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000518}
519
Devang Patela736c002008-02-26 19:38:17 +0000520ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
521 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000522 : TerminatorInst(Type::VoidTy, Instruction::Ret,
523 OperandTraits<ReturnInst>::op_end(this) - N,
524 N, InsertBefore) {
Devang Patela736c002008-02-26 19:38:17 +0000525 if (N != 0)
526 init(retVals, N);
527}
528ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
529 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000530 : TerminatorInst(Type::VoidTy, Instruction::Ret,
531 OperandTraits<ReturnInst>::op_end(this) - N,
532 N, InsertAtEnd) {
Devang Patela736c002008-02-26 19:38:17 +0000533 if (N != 0)
534 init(retVals, N);
535}
536
Devang Patel060e79c2008-02-26 19:15:26 +0000537void ReturnInst::init(Value * const* retVals, unsigned N) {
Devang Patelc38eb522008-02-26 18:49:29 +0000538 assert (N > 0 && "Invalid operands numbers in ReturnInst init");
Devang Patel59643e52008-02-23 00:35:18 +0000539
Devang Patelc38eb522008-02-26 18:49:29 +0000540 NumOperands = N;
Devang Patel59643e52008-02-23 00:35:18 +0000541 if (NumOperands == 1) {
Devang Patel060e79c2008-02-26 19:15:26 +0000542 Value *V = *retVals;
Devang Patel59643e52008-02-23 00:35:18 +0000543 if (V->getType() == Type::VoidTy)
544 return;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000545 Op<0>() = V;
Devang Patelae682fb2008-02-26 17:56:20 +0000546 return;
Devang Patel59643e52008-02-23 00:35:18 +0000547 }
548
Gabor Greiff6caff662008-05-10 08:32:32 +0000549 Use *OL = OperandList;
Devang Patel59643e52008-02-23 00:35:18 +0000550 for (unsigned i = 0; i < NumOperands; ++i) {
Devang Patel060e79c2008-02-26 19:15:26 +0000551 Value *V = *retVals++;
Devang Patel59643e52008-02-23 00:35:18 +0000552 assert(!isa<BasicBlock>(V) &&
553 "Cannot return basic block. Probably using the incorrect ctor");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000554 OL[i] = V;
Devang Patel59643e52008-02-23 00:35:18 +0000555 }
556}
557
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000558unsigned ReturnInst::getNumSuccessorsV() const {
559 return getNumSuccessors();
560}
561
Devang Patelae682fb2008-02-26 17:56:20 +0000562/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
563/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000564void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000565 assert(0 && "ReturnInst has no successors!");
566}
567
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000568BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
569 assert(0 && "ReturnInst has no successors!");
570 abort();
571 return 0;
572}
573
Devang Patel59643e52008-02-23 00:35:18 +0000574ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000575}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000576
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000577//===----------------------------------------------------------------------===//
578// UnwindInst Implementation
579//===----------------------------------------------------------------------===//
580
Chris Lattner2195fc42007-02-24 00:55:48 +0000581UnwindInst::UnwindInst(Instruction *InsertBefore)
582 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
583}
584UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
585 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
586}
587
588
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000589unsigned UnwindInst::getNumSuccessorsV() const {
590 return getNumSuccessors();
591}
592
593void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000594 assert(0 && "UnwindInst has no successors!");
595}
596
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000597BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
598 assert(0 && "UnwindInst has no successors!");
599 abort();
600 return 0;
601}
602
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000603//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000604// UnreachableInst Implementation
605//===----------------------------------------------------------------------===//
606
Chris Lattner2195fc42007-02-24 00:55:48 +0000607UnreachableInst::UnreachableInst(Instruction *InsertBefore)
608 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
609}
610UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
611 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
612}
613
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000614unsigned UnreachableInst::getNumSuccessorsV() const {
615 return getNumSuccessors();
616}
617
618void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
619 assert(0 && "UnwindInst has no successors!");
620}
621
622BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
623 assert(0 && "UnwindInst has no successors!");
624 abort();
625 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000626}
627
628//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000629// BranchInst Implementation
630//===----------------------------------------------------------------------===//
631
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000632void BranchInst::AssertOK() {
633 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000634 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000635 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000636}
637
Chris Lattner2195fc42007-02-24 00:55:48 +0000638BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000639 : TerminatorInst(Type::VoidTy, Instruction::Br,
640 OperandTraits<BranchInst>::op_end(this) - 1,
641 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000642 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000643 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000644}
645BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
646 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000647 : TerminatorInst(Type::VoidTy, Instruction::Br,
648 OperandTraits<BranchInst>::op_end(this) - 3,
649 3, InsertBefore) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000650 Op<0>() = IfTrue;
651 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000652 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000653#ifndef NDEBUG
654 AssertOK();
655#endif
656}
657
658BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000659 : TerminatorInst(Type::VoidTy, Instruction::Br,
660 OperandTraits<BranchInst>::op_end(this) - 1,
661 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000662 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000663 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000664}
665
666BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
667 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000668 : TerminatorInst(Type::VoidTy, Instruction::Br,
669 OperandTraits<BranchInst>::op_end(this) - 3,
670 3, InsertAtEnd) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000671 Op<0>() = IfTrue;
672 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000673 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000674#ifndef NDEBUG
675 AssertOK();
676#endif
677}
678
679
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000680BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000681 TerminatorInst(Type::VoidTy, Instruction::Br,
682 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
683 BI.getNumOperands()) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000684 OperandList[0] = BI.getOperand(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000685 if (BI.getNumOperands() != 1) {
686 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000687 OperandList[1] = BI.getOperand(1);
688 OperandList[2] = BI.getOperand(2);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000689 }
690}
691
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000692BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
693 return getSuccessor(idx);
694}
695unsigned BranchInst::getNumSuccessorsV() const {
696 return getNumSuccessors();
697}
698void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
699 setSuccessor(idx, B);
700}
701
702
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000703//===----------------------------------------------------------------------===//
704// AllocationInst Implementation
705//===----------------------------------------------------------------------===//
706
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000707static Value *getAISize(Value *Amt) {
708 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000709 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000710 else {
711 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000712 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000713 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000714 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000715 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000716 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000717}
718
Misha Brukmanb1c93172005-04-21 23:48:37 +0000719AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000720 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000721 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000722 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000723 InsertBefore) {
724 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000725 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000726 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000727}
728
Misha Brukmanb1c93172005-04-21 23:48:37 +0000729AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000730 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000731 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000732 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000733 InsertAtEnd) {
734 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000735 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000736 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000737}
738
Gordon Henriksen14a55692007-12-10 02:14:30 +0000739// Out of line virtual method, so the vtable, etc has a home.
740AllocationInst::~AllocationInst() {
741}
742
Dan Gohmanaa583d72008-03-24 16:55:58 +0000743void AllocationInst::setAlignment(unsigned Align) {
744 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
745 SubclassData = Log2_32(Align) + 1;
746 assert(getAlignment() == Align && "Alignment representation error!");
747}
748
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000749bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000750 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
751 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000752 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000753}
754
755const Type *AllocationInst::getAllocatedType() const {
756 return getType()->getElementType();
757}
758
759AllocaInst::AllocaInst(const AllocaInst &AI)
760 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000761 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000762}
763
764MallocInst::MallocInst(const MallocInst &MI)
765 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000766 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000767}
768
769//===----------------------------------------------------------------------===//
770// FreeInst Implementation
771//===----------------------------------------------------------------------===//
772
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000773void FreeInst::AssertOK() {
774 assert(isa<PointerType>(getOperand(0)->getType()) &&
775 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000776}
777
778FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000779 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000780 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000781}
782
783FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000784 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000785 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000786}
787
788
789//===----------------------------------------------------------------------===//
790// LoadInst Implementation
791//===----------------------------------------------------------------------===//
792
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000793void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000794 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000795 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000796}
797
798LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000799 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000800 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000801 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000802 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000803 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000804 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000805}
806
807LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000808 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000809 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000810 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000811 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000812 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000813 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000814}
815
816LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
817 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000818 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000819 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000820 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000821 setAlignment(0);
822 AssertOK();
823 setName(Name);
824}
825
826LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
827 unsigned Align, Instruction *InsertBef)
828 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
829 Load, Ptr, InsertBef) {
830 setVolatile(isVolatile);
831 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000832 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000833 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000834}
835
Dan Gohman68659282007-07-18 20:51:11 +0000836LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
837 unsigned Align, BasicBlock *InsertAE)
838 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
839 Load, Ptr, InsertAE) {
840 setVolatile(isVolatile);
841 setAlignment(Align);
842 AssertOK();
843 setName(Name);
844}
845
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000846LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
847 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000848 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000849 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000850 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000851 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000852 AssertOK();
853 setName(Name);
854}
855
856
857
858LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000859 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
860 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000861 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000862 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000863 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000864 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000865}
866
867LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000868 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
869 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000870 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000871 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000872 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000873 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000874}
875
876LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
877 Instruction *InsertBef)
878: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000879 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000880 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000881 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000882 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000883 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000884}
885
886LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
887 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000888 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
889 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000890 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000891 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000892 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000893 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000894}
895
Christopher Lamb84485702007-04-22 19:24:39 +0000896void LoadInst::setAlignment(unsigned Align) {
897 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
898 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
899}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000900
901//===----------------------------------------------------------------------===//
902// StoreInst Implementation
903//===----------------------------------------------------------------------===//
904
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000905void StoreInst::AssertOK() {
906 assert(isa<PointerType>(getOperand(1)->getType()) &&
907 "Ptr must have pointer type!");
908 assert(getOperand(0)->getType() ==
909 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000910 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000911}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000912
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000913
914StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000915 : Instruction(Type::VoidTy, Store,
916 OperandTraits<StoreInst>::op_begin(this),
917 OperandTraits<StoreInst>::operands(this),
918 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000919 Op<0>() = val;
920 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000921 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000922 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000923 AssertOK();
924}
925
926StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000927 : Instruction(Type::VoidTy, Store,
928 OperandTraits<StoreInst>::op_begin(this),
929 OperandTraits<StoreInst>::operands(this),
930 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000931 Op<0>() = val;
932 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000933 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000934 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000935 AssertOK();
936}
937
Misha Brukmanb1c93172005-04-21 23:48:37 +0000938StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000939 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000940 : Instruction(Type::VoidTy, Store,
941 OperandTraits<StoreInst>::op_begin(this),
942 OperandTraits<StoreInst>::operands(this),
943 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000944 Op<0>() = val;
945 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000946 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000947 setAlignment(0);
948 AssertOK();
949}
950
951StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
952 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000953 : Instruction(Type::VoidTy, Store,
954 OperandTraits<StoreInst>::op_begin(this),
955 OperandTraits<StoreInst>::operands(this),
956 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000957 Op<0>() = val;
958 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000959 setVolatile(isVolatile);
960 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000961 AssertOK();
962}
963
Misha Brukmanb1c93172005-04-21 23:48:37 +0000964StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000965 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000966 : Instruction(Type::VoidTy, Store,
967 OperandTraits<StoreInst>::op_begin(this),
968 OperandTraits<StoreInst>::operands(this),
969 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000970 Op<0>() = val;
971 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000972 setVolatile(isVolatile);
973 setAlignment(Align);
974 AssertOK();
975}
976
977StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000978 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000979 : Instruction(Type::VoidTy, Store,
980 OperandTraits<StoreInst>::op_begin(this),
981 OperandTraits<StoreInst>::operands(this),
982 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000983 Op<0>() = val;
984 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000985 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000986 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000987 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000988}
989
Christopher Lamb84485702007-04-22 19:24:39 +0000990void StoreInst::setAlignment(unsigned Align) {
991 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
992 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
993}
994
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000995//===----------------------------------------------------------------------===//
996// GetElementPtrInst Implementation
997//===----------------------------------------------------------------------===//
998
Christopher Lambedf07882007-12-17 01:12:55 +0000999static unsigned retrieveAddrSpace(const Value *Val) {
1000 return cast<PointerType>(Val->getType())->getAddressSpace();
1001}
1002
Gabor Greif21ba1842008-06-06 20:28:12 +00001003void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
1004 const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001005 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1006 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001007 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001008
Chris Lattner79807c3d2007-01-31 19:47:18 +00001009 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001010 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001011
1012 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001013}
1014
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001015void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001016 assert(NumOperands == 2 && "NumOperands not initialized?");
1017 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001018 OL[0] = Ptr;
1019 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001020
1021 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001022}
1023
Gabor Greiff6caff662008-05-10 08:32:32 +00001024GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001025 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001026 OperandTraits<GetElementPtrInst>::op_end(this)
1027 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001028 GEPI.getNumOperands()) {
1029 Use *OL = OperandList;
1030 Use *GEPIOL = GEPI.OperandList;
1031 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001032 OL[i] = GEPIOL[i];
Gabor Greiff6caff662008-05-10 08:32:32 +00001033}
1034
Chris Lattner82981202005-05-03 05:43:30 +00001035GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1036 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001037 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001038 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001039 GetElementPtr,
1040 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1041 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001042 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001043}
1044
1045GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1046 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001047 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001048 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001049 GetElementPtr,
1050 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1051 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001052 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001053}
1054
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001055// getIndexedType - Returns the type of the element that would be loaded with
1056// a load instruction with the specified parameters.
1057//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001058// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001059// pointer type.
1060//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001061const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001062 Value* const *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001063 unsigned NumIdx) {
1064 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1065 if (!PTy) return 0; // Type isn't a pointer type!
1066 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001067
1068 // Handle the special case of the empty set index set...
Dan Gohman12fce772008-05-15 19:50:34 +00001069 if (NumIdx == 0)
1070 return Agg;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001071
Dan Gohman1ecaf452008-05-31 00:58:22 +00001072 unsigned CurIdx = 1;
1073 for (; CurIdx != NumIdx; ++CurIdx) {
1074 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1075 if (!CT || isa<PointerType>(CT)) return 0;
1076 Value *Index = Idxs[CurIdx];
1077 if (!CT->indexValid(Index)) return 0;
1078 Agg = CT->getTypeAtIndex(Index);
1079
1080 // If the new type forwards to another type, then it is in the middle
1081 // of being refined to another type (and hence, may have dropped all
1082 // references to what it was using before). So, use the new forwarded
1083 // type.
1084 if (const Type *Ty = Agg->getForwardedType())
1085 Agg = Ty;
1086 }
1087 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001088}
1089
Chris Lattner82981202005-05-03 05:43:30 +00001090const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1091 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1092 if (!PTy) return 0; // Type isn't a pointer type!
1093
1094 // Check the pointer index.
1095 if (!PTy->indexValid(Idx)) return 0;
1096
Chris Lattnerc2233332005-05-03 16:44:45 +00001097 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001098}
1099
Chris Lattner45f15572007-04-14 00:12:57 +00001100
1101/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1102/// zeros. If so, the result pointer and the first operand have the same
1103/// value, just potentially different types.
1104bool GetElementPtrInst::hasAllZeroIndices() const {
1105 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1106 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1107 if (!CI->isZero()) return false;
1108 } else {
1109 return false;
1110 }
1111 }
1112 return true;
1113}
1114
Chris Lattner27058292007-04-27 20:35:56 +00001115/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1116/// constant integers. If so, the result pointer and the first operand have
1117/// a constant offset between them.
1118bool GetElementPtrInst::hasAllConstantIndices() const {
1119 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1120 if (!isa<ConstantInt>(getOperand(i)))
1121 return false;
1122 }
1123 return true;
1124}
1125
Chris Lattner45f15572007-04-14 00:12:57 +00001126
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001127//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001128// ExtractElementInst Implementation
1129//===----------------------------------------------------------------------===//
1130
1131ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001132 const std::string &Name,
1133 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001134 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001135 ExtractElement,
1136 OperandTraits<ExtractElementInst>::op_begin(this),
1137 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001138 assert(isValidOperands(Val, Index) &&
1139 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001140 Op<0>() = Val;
1141 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001142 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001143}
1144
Chris Lattner65511ff2006-10-05 06:24:58 +00001145ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1146 const std::string &Name,
1147 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001148 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001149 ExtractElement,
1150 OperandTraits<ExtractElementInst>::op_begin(this),
1151 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001152 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001153 assert(isValidOperands(Val, Index) &&
1154 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001155 Op<0>() = Val;
1156 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001157 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001158}
1159
1160
Robert Bocchino23004482006-01-10 19:05:34 +00001161ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001162 const std::string &Name,
1163 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001164 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001165 ExtractElement,
1166 OperandTraits<ExtractElementInst>::op_begin(this),
1167 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001168 assert(isValidOperands(Val, Index) &&
1169 "Invalid extractelement instruction operands!");
1170
Gabor Greif2d3024d2008-05-26 21:33:52 +00001171 Op<0>() = Val;
1172 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001173 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001174}
1175
Chris Lattner65511ff2006-10-05 06:24:58 +00001176ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1177 const std::string &Name,
1178 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001179 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001180 ExtractElement,
1181 OperandTraits<ExtractElementInst>::op_begin(this),
1182 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001183 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001184 assert(isValidOperands(Val, Index) &&
1185 "Invalid extractelement instruction operands!");
1186
Gabor Greif2d3024d2008-05-26 21:33:52 +00001187 Op<0>() = Val;
1188 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001189 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001190}
1191
1192
Chris Lattner54865b32006-04-08 04:05:48 +00001193bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001194 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001195 return false;
1196 return true;
1197}
1198
1199
Robert Bocchino23004482006-01-10 19:05:34 +00001200//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001201// InsertElementInst Implementation
1202//===----------------------------------------------------------------------===//
1203
Chris Lattner0875d942006-04-14 22:20:32 +00001204InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001205 : Instruction(IE.getType(), InsertElement,
1206 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001207 Op<0>() = IE.Op<0>();
1208 Op<1>() = IE.Op<1>();
1209 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001210}
Chris Lattner54865b32006-04-08 04:05:48 +00001211InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001212 const std::string &Name,
1213 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001214 : Instruction(Vec->getType(), InsertElement,
1215 OperandTraits<InsertElementInst>::op_begin(this),
1216 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001217 assert(isValidOperands(Vec, Elt, Index) &&
1218 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001219 Op<0>() = Vec;
1220 Op<1>() = Elt;
1221 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001222 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001223}
1224
Chris Lattner65511ff2006-10-05 06:24:58 +00001225InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1226 const std::string &Name,
1227 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001228 : Instruction(Vec->getType(), InsertElement,
1229 OperandTraits<InsertElementInst>::op_begin(this),
1230 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001231 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001232 assert(isValidOperands(Vec, Elt, Index) &&
1233 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001234 Op<0>() = Vec;
1235 Op<1>() = Elt;
1236 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001237 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001238}
1239
1240
Chris Lattner54865b32006-04-08 04:05:48 +00001241InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001242 const std::string &Name,
1243 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001244 : Instruction(Vec->getType(), InsertElement,
1245 OperandTraits<InsertElementInst>::op_begin(this),
1246 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001247 assert(isValidOperands(Vec, Elt, Index) &&
1248 "Invalid insertelement instruction operands!");
1249
Gabor Greif2d3024d2008-05-26 21:33:52 +00001250 Op<0>() = Vec;
1251 Op<1>() = Elt;
1252 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001253 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001254}
1255
Chris Lattner65511ff2006-10-05 06:24:58 +00001256InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1257 const std::string &Name,
1258 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001259: Instruction(Vec->getType(), InsertElement,
1260 OperandTraits<InsertElementInst>::op_begin(this),
1261 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001262 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001263 assert(isValidOperands(Vec, Elt, Index) &&
1264 "Invalid insertelement instruction operands!");
1265
Gabor Greif2d3024d2008-05-26 21:33:52 +00001266 Op<0>() = Vec;
1267 Op<1>() = Elt;
1268 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001269 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001270}
1271
Chris Lattner54865b32006-04-08 04:05:48 +00001272bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1273 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001274 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001275 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001276
Reid Spencerd84d35b2007-02-15 02:26:10 +00001277 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001278 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001279
Reid Spencer8d9336d2006-12-31 05:26:44 +00001280 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001281 return false; // Third operand of insertelement must be uint.
1282 return true;
1283}
1284
1285
Robert Bocchinoca27f032006-01-17 20:07:22 +00001286//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001287// ShuffleVectorInst Implementation
1288//===----------------------------------------------------------------------===//
1289
Chris Lattner0875d942006-04-14 22:20:32 +00001290ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001291 : Instruction(SV.getType(), ShuffleVector,
1292 OperandTraits<ShuffleVectorInst>::op_begin(this),
1293 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001294 Op<0>() = SV.Op<0>();
1295 Op<1>() = SV.Op<1>();
1296 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001297}
1298
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001299ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1300 const std::string &Name,
1301 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001302 : Instruction(V1->getType(), ShuffleVector,
1303 OperandTraits<ShuffleVectorInst>::op_begin(this),
1304 OperandTraits<ShuffleVectorInst>::operands(this),
1305 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001306 assert(isValidOperands(V1, V2, Mask) &&
1307 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001308 Op<0>() = V1;
1309 Op<1>() = V2;
1310 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001311 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001312}
1313
1314ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1315 const std::string &Name,
1316 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001317 : Instruction(V1->getType(), ShuffleVector,
1318 OperandTraits<ShuffleVectorInst>::op_begin(this),
1319 OperandTraits<ShuffleVectorInst>::operands(this),
1320 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001321 assert(isValidOperands(V1, V2, Mask) &&
1322 "Invalid shuffle vector instruction operands!");
1323
Gabor Greif2d3024d2008-05-26 21:33:52 +00001324 Op<0>() = V1;
1325 Op<1>() = V2;
1326 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001327 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001328}
1329
1330bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1331 const Value *Mask) {
Chris Lattnerf724e342008-03-02 05:28:33 +00001332 if (!isa<VectorType>(V1->getType()) ||
1333 V1->getType() != V2->getType())
1334 return false;
1335
1336 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1337 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1338 MaskTy->getElementType() != Type::Int32Ty ||
1339 MaskTy->getNumElements() !=
1340 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001341 return false;
1342 return true;
1343}
1344
Chris Lattnerf724e342008-03-02 05:28:33 +00001345/// getMaskValue - Return the index from the shuffle mask for the specified
1346/// output result. This is either -1 if the element is undef or a number less
1347/// than 2*numelements.
1348int ShuffleVectorInst::getMaskValue(unsigned i) const {
1349 const Constant *Mask = cast<Constant>(getOperand(2));
1350 if (isa<UndefValue>(Mask)) return -1;
1351 if (isa<ConstantAggregateZero>(Mask)) return 0;
1352 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1353 assert(i < MaskCV->getNumOperands() && "Index out of range");
1354
1355 if (isa<UndefValue>(MaskCV->getOperand(i)))
1356 return -1;
1357 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1358}
1359
Dan Gohman12fce772008-05-15 19:50:34 +00001360//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001361// InsertValueInst Class
1362//===----------------------------------------------------------------------===//
1363
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001364void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1365 unsigned NumIdx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001366 assert(NumOperands == 2 && "NumOperands not initialized?");
1367 Op<0>() = Agg;
1368 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001369
Dan Gohman1ecaf452008-05-31 00:58:22 +00001370 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001371 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001372}
1373
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001374void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1375 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001376 assert(NumOperands == 2 && "NumOperands not initialized?");
1377 Op<0>() = Agg;
1378 Op<1>() = Val;
1379
1380 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001381 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001382}
1383
1384InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001385 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001386 OperandTraits<InsertValueInst>::op_begin(this), 2),
1387 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001388 Op<0>() = IVI.getOperand(0);
1389 Op<1>() = IVI.getOperand(1);
Dan Gohman0752bff2008-05-23 00:36:11 +00001390}
1391
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001392InsertValueInst::InsertValueInst(Value *Agg,
1393 Value *Val,
1394 unsigned Idx,
1395 const std::string &Name,
1396 Instruction *InsertBefore)
1397 : Instruction(Agg->getType(), InsertValue,
1398 OperandTraits<InsertValueInst>::op_begin(this),
1399 2, InsertBefore) {
1400 init(Agg, Val, Idx, Name);
1401}
1402
1403InsertValueInst::InsertValueInst(Value *Agg,
1404 Value *Val,
1405 unsigned Idx,
1406 const std::string &Name,
1407 BasicBlock *InsertAtEnd)
1408 : Instruction(Agg->getType(), InsertValue,
1409 OperandTraits<InsertValueInst>::op_begin(this),
1410 2, InsertAtEnd) {
1411 init(Agg, Val, Idx, Name);
1412}
1413
Dan Gohman0752bff2008-05-23 00:36:11 +00001414//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001415// ExtractValueInst Class
1416//===----------------------------------------------------------------------===//
1417
Gabor Greifcbcc4952008-06-06 21:06:32 +00001418void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Gabor Greif21ba1842008-06-06 20:28:12 +00001419 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001420 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001421
Dan Gohman1ecaf452008-05-31 00:58:22 +00001422 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001423 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001424}
1425
Gabor Greifcbcc4952008-06-06 21:06:32 +00001426void ExtractValueInst::init(unsigned Idx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001427 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001428
1429 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001430 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001431}
1432
1433ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001434 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001435 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001436}
1437
Dan Gohman12fce772008-05-15 19:50:34 +00001438// getIndexedType - Returns the type of the element that would be extracted
1439// with an extractvalue instruction with the specified parameters.
1440//
1441// A null type is returned if the indices are invalid for the specified
1442// pointer type.
1443//
1444const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001445 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001446 unsigned NumIdx) {
1447 unsigned CurIdx = 0;
1448 for (; CurIdx != NumIdx; ++CurIdx) {
1449 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001450 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1451 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001452 if (!CT->indexValid(Index)) return 0;
1453 Agg = CT->getTypeAtIndex(Index);
1454
1455 // If the new type forwards to another type, then it is in the middle
1456 // of being refined to another type (and hence, may have dropped all
1457 // references to what it was using before). So, use the new forwarded
1458 // type.
1459 if (const Type *Ty = Agg->getForwardedType())
1460 Agg = Ty;
1461 }
1462 return CurIdx == NumIdx ? Agg : 0;
1463}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001464
Dan Gohman4a3125b2008-06-17 21:07:55 +00001465const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001466 unsigned Idx) {
1467 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001468}
1469
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001470ExtractValueInst::ExtractValueInst(Value *Agg,
1471 unsigned Idx,
1472 const std::string &Name,
1473 BasicBlock *InsertAtEnd)
Gabor Greif21ba1842008-06-06 20:28:12 +00001474 : UnaryInstruction(checkType(getIndexedType(Agg->getType(), &Idx, 1)),
1475 ExtractValue, Agg, InsertAtEnd) {
Gabor Greifcbcc4952008-06-06 21:06:32 +00001476 init(Idx, Name);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001477}
1478
1479ExtractValueInst::ExtractValueInst(Value *Agg,
1480 unsigned Idx,
1481 const std::string &Name,
1482 Instruction *InsertBefore)
Gabor Greif21ba1842008-06-06 20:28:12 +00001483 : UnaryInstruction(checkType(getIndexedType(Agg->getType(), &Idx, 1)),
1484 ExtractValue, Agg, InsertBefore) {
Gabor Greifcbcc4952008-06-06 21:06:32 +00001485 init(Idx, Name);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001486}
1487
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001488//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001489// BinaryOperator Class
1490//===----------------------------------------------------------------------===//
1491
Chris Lattner2195fc42007-02-24 00:55:48 +00001492BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1493 const Type *Ty, const std::string &Name,
1494 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001495 : Instruction(Ty, iType,
1496 OperandTraits<BinaryOperator>::op_begin(this),
1497 OperandTraits<BinaryOperator>::operands(this),
1498 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001499 Op<0>() = S1;
1500 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001501 init(iType);
1502 setName(Name);
1503}
1504
1505BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1506 const Type *Ty, const std::string &Name,
1507 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001508 : Instruction(Ty, iType,
1509 OperandTraits<BinaryOperator>::op_begin(this),
1510 OperandTraits<BinaryOperator>::operands(this),
1511 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001512 Op<0>() = S1;
1513 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001514 init(iType);
1515 setName(Name);
1516}
1517
1518
1519void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001520 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001521 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001522 assert(LHS->getType() == RHS->getType() &&
1523 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001524#ifndef NDEBUG
1525 switch (iType) {
1526 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001527 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001528 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001529 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001530 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001531 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001532 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001533 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001534 case UDiv:
1535 case SDiv:
1536 assert(getType() == LHS->getType() &&
1537 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001538 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1539 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001540 "Incorrect operand type (not integer) for S/UDIV");
1541 break;
1542 case FDiv:
1543 assert(getType() == LHS->getType() &&
1544 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001545 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1546 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001547 && "Incorrect operand type (not floating point) for FDIV");
1548 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001549 case URem:
1550 case SRem:
1551 assert(getType() == LHS->getType() &&
1552 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001553 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1554 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001555 "Incorrect operand type (not integer) for S/UREM");
1556 break;
1557 case FRem:
1558 assert(getType() == LHS->getType() &&
1559 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001560 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1561 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001562 && "Incorrect operand type (not floating point) for FREM");
1563 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001564 case Shl:
1565 case LShr:
1566 case AShr:
1567 assert(getType() == LHS->getType() &&
1568 "Shift operation should return same type as operands!");
1569 assert(getType()->isInteger() &&
1570 "Shift operation requires integer operands");
1571 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001572 case And: case Or:
1573 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001574 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001575 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001576 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001577 (isa<VectorType>(getType()) &&
1578 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001579 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001580 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001581 default:
1582 break;
1583 }
1584#endif
1585}
1586
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001587BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001588 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001589 Instruction *InsertBefore) {
1590 assert(S1->getType() == S2->getType() &&
1591 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001592 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001593}
1594
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001595BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001596 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001597 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001598 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001599 InsertAtEnd->getInstList().push_back(Res);
1600 return Res;
1601}
1602
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001603BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001604 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001605 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1606 return new BinaryOperator(Instruction::Sub,
1607 zero, Op,
1608 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001609}
1610
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001611BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001612 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001613 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1614 return new BinaryOperator(Instruction::Sub,
1615 zero, Op,
1616 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001617}
1618
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001619BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001620 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001621 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001622 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001623 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001624 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001625 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001626 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001627 }
1628
1629 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001630 Op->getType(), Name, InsertBefore);
1631}
1632
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001633BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001634 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001635 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001636 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001637 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001638 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001639 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001640 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001641 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001642 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001643 }
1644
1645 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001646 Op->getType(), Name, InsertAtEnd);
1647}
1648
1649
1650// isConstantAllOnes - Helper function for several functions below
1651static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001652 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1653 return CI->isAllOnesValue();
1654 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1655 return CV->isAllOnesValue();
1656 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001657}
1658
1659bool BinaryOperator::isNeg(const Value *V) {
1660 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1661 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001662 return Bop->getOperand(0) ==
1663 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001664 return false;
1665}
1666
1667bool BinaryOperator::isNot(const Value *V) {
1668 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1669 return (Bop->getOpcode() == Instruction::Xor &&
1670 (isConstantAllOnes(Bop->getOperand(1)) ||
1671 isConstantAllOnes(Bop->getOperand(0))));
1672 return false;
1673}
1674
Chris Lattner2c7d1772005-04-24 07:28:37 +00001675Value *BinaryOperator::getNegArgument(Value *BinOp) {
1676 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1677 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001678}
1679
Chris Lattner2c7d1772005-04-24 07:28:37 +00001680const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1681 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001682}
1683
Chris Lattner2c7d1772005-04-24 07:28:37 +00001684Value *BinaryOperator::getNotArgument(Value *BinOp) {
1685 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1686 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1687 Value *Op0 = BO->getOperand(0);
1688 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001689 if (isConstantAllOnes(Op0)) return Op1;
1690
1691 assert(isConstantAllOnes(Op1));
1692 return Op0;
1693}
1694
Chris Lattner2c7d1772005-04-24 07:28:37 +00001695const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1696 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001697}
1698
1699
1700// swapOperands - Exchange the two operands to this instruction. This
1701// instruction is safe to use on any binary instruction and does not
1702// modify the semantics of the instruction. If the instruction is
1703// order dependent (SetLT f.e.) the opcode is changed.
1704//
1705bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001706 if (!isCommutative())
1707 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001708 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001709 return false;
1710}
1711
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001712//===----------------------------------------------------------------------===//
1713// CastInst Class
1714//===----------------------------------------------------------------------===//
1715
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001716// Just determine if this cast only deals with integral->integral conversion.
1717bool CastInst::isIntegerCast() const {
1718 switch (getOpcode()) {
1719 default: return false;
1720 case Instruction::ZExt:
1721 case Instruction::SExt:
1722 case Instruction::Trunc:
1723 return true;
1724 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001725 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001726 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001727}
1728
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001729bool CastInst::isLosslessCast() const {
1730 // Only BitCast can be lossless, exit fast if we're not BitCast
1731 if (getOpcode() != Instruction::BitCast)
1732 return false;
1733
1734 // Identity cast is always lossless
1735 const Type* SrcTy = getOperand(0)->getType();
1736 const Type* DstTy = getType();
1737 if (SrcTy == DstTy)
1738 return true;
1739
Reid Spencer8d9336d2006-12-31 05:26:44 +00001740 // Pointer to pointer is always lossless.
1741 if (isa<PointerType>(SrcTy))
1742 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001743 return false; // Other types have no identity values
1744}
1745
1746/// This function determines if the CastInst does not require any bits to be
1747/// changed in order to effect the cast. Essentially, it identifies cases where
1748/// no code gen is necessary for the cast, hence the name no-op cast. For
1749/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001750/// # bitcast i32* %x to i8*
1751/// # bitcast <2 x i32> %x to <4 x i16>
1752/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001753/// @brief Determine if a cast is a no-op.
1754bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1755 switch (getOpcode()) {
1756 default:
1757 assert(!"Invalid CastOp");
1758 case Instruction::Trunc:
1759 case Instruction::ZExt:
1760 case Instruction::SExt:
1761 case Instruction::FPTrunc:
1762 case Instruction::FPExt:
1763 case Instruction::UIToFP:
1764 case Instruction::SIToFP:
1765 case Instruction::FPToUI:
1766 case Instruction::FPToSI:
1767 return false; // These always modify bits
1768 case Instruction::BitCast:
1769 return true; // BitCast never modifies bits.
1770 case Instruction::PtrToInt:
1771 return IntPtrTy->getPrimitiveSizeInBits() ==
1772 getType()->getPrimitiveSizeInBits();
1773 case Instruction::IntToPtr:
1774 return IntPtrTy->getPrimitiveSizeInBits() ==
1775 getOperand(0)->getType()->getPrimitiveSizeInBits();
1776 }
1777}
1778
1779/// This function determines if a pair of casts can be eliminated and what
1780/// opcode should be used in the elimination. This assumes that there are two
1781/// instructions like this:
1782/// * %F = firstOpcode SrcTy %x to MidTy
1783/// * %S = secondOpcode MidTy %F to DstTy
1784/// The function returns a resultOpcode so these two casts can be replaced with:
1785/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1786/// If no such cast is permited, the function returns 0.
1787unsigned CastInst::isEliminableCastPair(
1788 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1789 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1790{
1791 // Define the 144 possibilities for these two cast instructions. The values
1792 // in this matrix determine what to do in a given situation and select the
1793 // case in the switch below. The rows correspond to firstOp, the columns
1794 // correspond to secondOp. In looking at the table below, keep in mind
1795 // the following cast properties:
1796 //
1797 // Size Compare Source Destination
1798 // Operator Src ? Size Type Sign Type Sign
1799 // -------- ------------ ------------------- ---------------------
1800 // TRUNC > Integer Any Integral Any
1801 // ZEXT < Integral Unsigned Integer Any
1802 // SEXT < Integral Signed Integer Any
1803 // FPTOUI n/a FloatPt n/a Integral Unsigned
1804 // FPTOSI n/a FloatPt n/a Integral Signed
1805 // UITOFP n/a Integral Unsigned FloatPt n/a
1806 // SITOFP n/a Integral Signed FloatPt n/a
1807 // FPTRUNC > FloatPt n/a FloatPt n/a
1808 // FPEXT < FloatPt n/a FloatPt n/a
1809 // PTRTOINT n/a Pointer n/a Integral Unsigned
1810 // INTTOPTR n/a Integral Unsigned Pointer n/a
1811 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001812 //
1813 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1814 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1815 // into "fptoui double to ulong", but this loses information about the range
1816 // of the produced value (we no longer know the top-part is all zeros).
1817 // Further this conversion is often much more expensive for typical hardware,
1818 // and causes issues when building libgcc. We disallow fptosi+sext for the
1819 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001820 const unsigned numCastOps =
1821 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1822 static const uint8_t CastResults[numCastOps][numCastOps] = {
1823 // T F F U S F F P I B -+
1824 // R Z S P P I I T P 2 N T |
1825 // U E E 2 2 2 2 R E I T C +- secondOp
1826 // N X X U S F F N X N 2 V |
1827 // C T T I I P P C T T P T -+
1828 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1829 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1830 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001831 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1832 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001833 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1834 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1835 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1836 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1837 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1838 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1839 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1840 };
1841
1842 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1843 [secondOp-Instruction::CastOpsBegin];
1844 switch (ElimCase) {
1845 case 0:
1846 // categorically disallowed
1847 return 0;
1848 case 1:
1849 // allowed, use first cast's opcode
1850 return firstOp;
1851 case 2:
1852 // allowed, use second cast's opcode
1853 return secondOp;
1854 case 3:
1855 // no-op cast in second op implies firstOp as long as the DestTy
1856 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001857 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001858 return firstOp;
1859 return 0;
1860 case 4:
1861 // no-op cast in second op implies firstOp as long as the DestTy
1862 // is floating point
1863 if (DstTy->isFloatingPoint())
1864 return firstOp;
1865 return 0;
1866 case 5:
1867 // no-op cast in first op implies secondOp as long as the SrcTy
1868 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001869 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001870 return secondOp;
1871 return 0;
1872 case 6:
1873 // no-op cast in first op implies secondOp as long as the SrcTy
1874 // is a floating point
1875 if (SrcTy->isFloatingPoint())
1876 return secondOp;
1877 return 0;
1878 case 7: {
1879 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1880 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1881 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1882 if (MidSize >= PtrSize)
1883 return Instruction::BitCast;
1884 return 0;
1885 }
1886 case 8: {
1887 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1888 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1889 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1890 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1891 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1892 if (SrcSize == DstSize)
1893 return Instruction::BitCast;
1894 else if (SrcSize < DstSize)
1895 return firstOp;
1896 return secondOp;
1897 }
1898 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1899 return Instruction::ZExt;
1900 case 10:
1901 // fpext followed by ftrunc is allowed if the bit size returned to is
1902 // the same as the original, in which case its just a bitcast
1903 if (SrcTy == DstTy)
1904 return Instruction::BitCast;
1905 return 0; // If the types are not the same we can't eliminate it.
1906 case 11:
1907 // bitcast followed by ptrtoint is allowed as long as the bitcast
1908 // is a pointer to pointer cast.
1909 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1910 return secondOp;
1911 return 0;
1912 case 12:
1913 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1914 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1915 return firstOp;
1916 return 0;
1917 case 13: {
1918 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1919 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1920 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1921 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1922 if (SrcSize <= PtrSize && SrcSize == DstSize)
1923 return Instruction::BitCast;
1924 return 0;
1925 }
1926 case 99:
1927 // cast combination can't happen (error in input). This is for all cases
1928 // where the MidTy is not the same for the two cast instructions.
1929 assert(!"Invalid Cast Combination");
1930 return 0;
1931 default:
1932 assert(!"Error in CastResults table!!!");
1933 return 0;
1934 }
1935 return 0;
1936}
1937
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001938CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001939 const std::string &Name, Instruction *InsertBefore) {
1940 // Construct and return the appropriate CastInst subclass
1941 switch (op) {
1942 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1943 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1944 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1945 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1946 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1947 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1948 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1949 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1950 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1951 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1952 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1953 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1954 default:
1955 assert(!"Invalid opcode provided");
1956 }
1957 return 0;
1958}
1959
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001960CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001961 const std::string &Name, BasicBlock *InsertAtEnd) {
1962 // Construct and return the appropriate CastInst subclass
1963 switch (op) {
1964 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1965 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1966 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1967 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1968 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1969 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1970 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1971 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1972 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1973 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1974 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1975 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1976 default:
1977 assert(!"Invalid opcode provided");
1978 }
1979 return 0;
1980}
1981
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001982CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001983 const std::string &Name,
1984 Instruction *InsertBefore) {
1985 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001986 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1987 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001988}
1989
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001990CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001991 const std::string &Name,
1992 BasicBlock *InsertAtEnd) {
1993 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001994 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1995 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001996}
1997
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001998CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001999 const std::string &Name,
2000 Instruction *InsertBefore) {
2001 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002002 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2003 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002004}
2005
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002006CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002007 const std::string &Name,
2008 BasicBlock *InsertAtEnd) {
2009 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002010 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2011 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002012}
2013
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002014CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002015 const std::string &Name,
2016 Instruction *InsertBefore) {
2017 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002018 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2019 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002020}
2021
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002022CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002023 const std::string &Name,
2024 BasicBlock *InsertAtEnd) {
2025 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002026 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2027 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002028}
2029
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002030CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002031 const std::string &Name,
2032 BasicBlock *InsertAtEnd) {
2033 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002034 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002035 "Invalid cast");
2036
Chris Lattner03c49532007-01-15 02:27:26 +00002037 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002038 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2039 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002040}
2041
2042/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002043CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002044 const std::string &Name,
2045 Instruction *InsertBefore) {
2046 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002047 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002048 "Invalid cast");
2049
Chris Lattner03c49532007-01-15 02:27:26 +00002050 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002051 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2052 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002053}
2054
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002055CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002056 bool isSigned, const std::string &Name,
2057 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002058 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002059 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2060 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2061 Instruction::CastOps opcode =
2062 (SrcBits == DstBits ? Instruction::BitCast :
2063 (SrcBits > DstBits ? Instruction::Trunc :
2064 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002065 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002066}
2067
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002068CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002069 bool isSigned, const std::string &Name,
2070 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00002071 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002072 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2073 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2074 Instruction::CastOps opcode =
2075 (SrcBits == DstBits ? Instruction::BitCast :
2076 (SrcBits > DstBits ? Instruction::Trunc :
2077 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002078 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002079}
2080
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002081CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002082 const std::string &Name,
2083 Instruction *InsertBefore) {
2084 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2085 "Invalid cast");
2086 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2087 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2088 Instruction::CastOps opcode =
2089 (SrcBits == DstBits ? Instruction::BitCast :
2090 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002091 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002092}
2093
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002094CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002095 const std::string &Name,
2096 BasicBlock *InsertAtEnd) {
2097 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2098 "Invalid cast");
2099 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2100 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2101 Instruction::CastOps opcode =
2102 (SrcBits == DstBits ? Instruction::BitCast :
2103 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002104 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002105}
2106
Duncan Sands55e50902008-01-06 10:12:28 +00002107// Check whether it is valid to call getCastOpcode for these types.
2108// This routine must be kept in sync with getCastOpcode.
2109bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2110 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2111 return false;
2112
2113 if (SrcTy == DestTy)
2114 return true;
2115
2116 // Get the bit sizes, we'll need these
2117 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2118 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2119
2120 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002121 if (DestTy->isInteger()) { // Casting to integral
2122 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002123 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002124 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002125 return true;
2126 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002127 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002128 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002129 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002130 return isa<PointerType>(SrcTy);
2131 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002132 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2133 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002134 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002135 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002136 return true;
2137 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002138 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002139 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002140 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002141 return false;
2142 }
2143 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002144 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002145 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002146 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002147 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002148 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002149 return DestPTy->getBitWidth() == SrcBits;
2150 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002151 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2152 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002153 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002154 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002155 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002156 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002157 return false;
2158 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002159 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002160 return false;
2161 }
2162}
2163
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002164// Provide a way to get a "cast" where the cast opcode is inferred from the
2165// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002166// logic in the castIsValid function below. This axiom should hold:
2167// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2168// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002169// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002170// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002171Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002172CastInst::getCastOpcode(
2173 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002174 // Get the bit sizes, we'll need these
2175 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002176 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2177 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002178
Duncan Sands55e50902008-01-06 10:12:28 +00002179 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2180 "Only first class types are castable!");
2181
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002182 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002183 if (DestTy->isInteger()) { // Casting to integral
2184 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002185 if (DestBits < SrcBits)
2186 return Trunc; // int -> smaller int
2187 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002188 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002189 return SExt; // signed -> SEXT
2190 else
2191 return ZExt; // unsigned -> ZEXT
2192 } else {
2193 return BitCast; // Same size, No-op cast
2194 }
2195 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002196 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002197 return FPToSI; // FP -> sint
2198 else
2199 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002200 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002201 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002202 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002203 return BitCast; // Same size, no-op cast
2204 } else {
2205 assert(isa<PointerType>(SrcTy) &&
2206 "Casting from a value that is not first-class type");
2207 return PtrToInt; // ptr -> int
2208 }
2209 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002210 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002211 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002212 return SIToFP; // sint -> FP
2213 else
2214 return UIToFP; // uint -> FP
2215 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2216 if (DestBits < SrcBits) {
2217 return FPTrunc; // FP -> smaller FP
2218 } else if (DestBits > SrcBits) {
2219 return FPExt; // FP -> larger FP
2220 } else {
2221 return BitCast; // same size, no-op cast
2222 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002223 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002224 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002225 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002226 return BitCast; // same size, no-op cast
2227 } else {
2228 assert(0 && "Casting pointer or non-first class to float");
2229 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002230 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2231 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002232 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002233 "Casting vector to vector of different widths");
2234 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002235 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002236 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002238 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002239 }
2240 } else if (isa<PointerType>(DestTy)) {
2241 if (isa<PointerType>(SrcTy)) {
2242 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002243 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002244 return IntToPtr; // int -> ptr
2245 } else {
2246 assert(!"Casting pointer to other than pointer or int");
2247 }
2248 } else {
2249 assert(!"Casting to type that is not first-class");
2250 }
2251
2252 // If we fall through to here we probably hit an assertion cast above
2253 // and assertions are not turned on. Anything we return is an error, so
2254 // BitCast is as good a choice as any.
2255 return BitCast;
2256}
2257
2258//===----------------------------------------------------------------------===//
2259// CastInst SubClass Constructors
2260//===----------------------------------------------------------------------===//
2261
2262/// Check that the construction parameters for a CastInst are correct. This
2263/// could be broken out into the separate constructors but it is useful to have
2264/// it in one place and to eliminate the redundant code for getting the sizes
2265/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002266bool
2267CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002268
2269 // Check for type sanity on the arguments
2270 const Type *SrcTy = S->getType();
2271 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2272 return false;
2273
2274 // Get the size of the types in bits, we'll need this later
2275 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2276 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2277
2278 // Switch on the opcode provided
2279 switch (op) {
2280 default: return false; // This is an input error
2281 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002282 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002283 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002284 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002285 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002286 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002287 case Instruction::FPTrunc:
2288 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2289 SrcBitSize > DstBitSize;
2290 case Instruction::FPExt:
2291 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2292 SrcBitSize < DstBitSize;
2293 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002294 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002295 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2296 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2297 return SVTy->getElementType()->isInteger() &&
2298 DVTy->getElementType()->isFloatingPoint() &&
2299 SVTy->getNumElements() == DVTy->getNumElements();
2300 }
2301 }
Chris Lattner03c49532007-01-15 02:27:26 +00002302 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002303 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002304 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002305 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2306 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2307 return SVTy->getElementType()->isFloatingPoint() &&
2308 DVTy->getElementType()->isInteger() &&
2309 SVTy->getNumElements() == DVTy->getNumElements();
2310 }
2311 }
Chris Lattner03c49532007-01-15 02:27:26 +00002312 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002313 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002314 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002315 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002316 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002317 case Instruction::BitCast:
2318 // BitCast implies a no-op cast of type only. No bits change.
2319 // However, you can't cast pointers to anything but pointers.
2320 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2321 return false;
2322
Duncan Sands55e50902008-01-06 10:12:28 +00002323 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324 // these cases, the cast is okay if the source and destination bit widths
2325 // are identical.
2326 return SrcBitSize == DstBitSize;
2327 }
2328}
2329
2330TruncInst::TruncInst(
2331 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2332) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002333 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002334}
2335
2336TruncInst::TruncInst(
2337 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2338) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002339 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002340}
2341
2342ZExtInst::ZExtInst(
2343 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2344) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002345 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002346}
2347
2348ZExtInst::ZExtInst(
2349 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2350) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002351 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002352}
2353SExtInst::SExtInst(
2354 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2355) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002356 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357}
2358
Jeff Cohencc08c832006-12-02 02:22:01 +00002359SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002360 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2361) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002362 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363}
2364
2365FPTruncInst::FPTruncInst(
2366 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2367) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002368 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002369}
2370
2371FPTruncInst::FPTruncInst(
2372 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2373) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002374 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002375}
2376
2377FPExtInst::FPExtInst(
2378 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2379) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002380 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002381}
2382
2383FPExtInst::FPExtInst(
2384 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2385) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002386 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002387}
2388
2389UIToFPInst::UIToFPInst(
2390 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2391) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002392 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393}
2394
2395UIToFPInst::UIToFPInst(
2396 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2397) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002398 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002399}
2400
2401SIToFPInst::SIToFPInst(
2402 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2403) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002404 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002405}
2406
2407SIToFPInst::SIToFPInst(
2408 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2409) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002410 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002411}
2412
2413FPToUIInst::FPToUIInst(
2414 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2415) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002416 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002417}
2418
2419FPToUIInst::FPToUIInst(
2420 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2421) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002422 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002423}
2424
2425FPToSIInst::FPToSIInst(
2426 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2427) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002428 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429}
2430
2431FPToSIInst::FPToSIInst(
2432 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2433) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002434 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435}
2436
2437PtrToIntInst::PtrToIntInst(
2438 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2439) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002440 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002441}
2442
2443PtrToIntInst::PtrToIntInst(
2444 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2445) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002446 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447}
2448
2449IntToPtrInst::IntToPtrInst(
2450 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2451) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002452 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453}
2454
2455IntToPtrInst::IntToPtrInst(
2456 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2457) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002458 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459}
2460
2461BitCastInst::BitCastInst(
2462 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2463) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002464 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465}
2466
2467BitCastInst::BitCastInst(
2468 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2469) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002470 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002472
2473//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002474// CmpInst Classes
2475//===----------------------------------------------------------------------===//
2476
Nate Begemand2195702008-05-12 19:01:56 +00002477CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2478 Value *LHS, Value *RHS, const std::string &Name,
2479 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002480 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002481 OperandTraits<CmpInst>::op_begin(this),
2482 OperandTraits<CmpInst>::operands(this),
2483 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002484 Op<0>() = LHS;
2485 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002486 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002487 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002488}
Gabor Greiff6caff662008-05-10 08:32:32 +00002489
Nate Begemand2195702008-05-12 19:01:56 +00002490CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2491 Value *LHS, Value *RHS, const std::string &Name,
2492 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002493 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002494 OperandTraits<CmpInst>::op_begin(this),
2495 OperandTraits<CmpInst>::operands(this),
2496 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002497 Op<0>() = LHS;
2498 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002499 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002500 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002501}
2502
2503CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002504CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002505 const std::string &Name, Instruction *InsertBefore) {
2506 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002507 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002508 InsertBefore);
2509 }
Nate Begemand2195702008-05-12 19:01:56 +00002510 if (Op == Instruction::FCmp) {
2511 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2512 InsertBefore);
2513 }
2514 if (Op == Instruction::VICmp) {
2515 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2516 InsertBefore);
2517 }
2518 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2519 InsertBefore);
Reid Spencerd9436b62006-11-20 01:22:35 +00002520}
2521
2522CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002523CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002524 const std::string &Name, BasicBlock *InsertAtEnd) {
2525 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002526 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002527 InsertAtEnd);
2528 }
Nate Begemand2195702008-05-12 19:01:56 +00002529 if (Op == Instruction::FCmp) {
2530 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2531 InsertAtEnd);
2532 }
2533 if (Op == Instruction::VICmp) {
2534 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2535 InsertAtEnd);
2536 }
2537 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2538 InsertAtEnd);
Reid Spencerd9436b62006-11-20 01:22:35 +00002539}
2540
2541void CmpInst::swapOperands() {
2542 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2543 IC->swapOperands();
2544 else
2545 cast<FCmpInst>(this)->swapOperands();
2546}
2547
2548bool CmpInst::isCommutative() {
2549 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2550 return IC->isCommutative();
2551 return cast<FCmpInst>(this)->isCommutative();
2552}
2553
2554bool CmpInst::isEquality() {
2555 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2556 return IC->isEquality();
2557 return cast<FCmpInst>(this)->isEquality();
2558}
2559
2560
Dan Gohman4e724382008-05-31 02:47:54 +00002561CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002562 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002563 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002564 case ICMP_EQ: return ICMP_NE;
2565 case ICMP_NE: return ICMP_EQ;
2566 case ICMP_UGT: return ICMP_ULE;
2567 case ICMP_ULT: return ICMP_UGE;
2568 case ICMP_UGE: return ICMP_ULT;
2569 case ICMP_ULE: return ICMP_UGT;
2570 case ICMP_SGT: return ICMP_SLE;
2571 case ICMP_SLT: return ICMP_SGE;
2572 case ICMP_SGE: return ICMP_SLT;
2573 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002574
Dan Gohman4e724382008-05-31 02:47:54 +00002575 case FCMP_OEQ: return FCMP_UNE;
2576 case FCMP_ONE: return FCMP_UEQ;
2577 case FCMP_OGT: return FCMP_ULE;
2578 case FCMP_OLT: return FCMP_UGE;
2579 case FCMP_OGE: return FCMP_ULT;
2580 case FCMP_OLE: return FCMP_UGT;
2581 case FCMP_UEQ: return FCMP_ONE;
2582 case FCMP_UNE: return FCMP_OEQ;
2583 case FCMP_UGT: return FCMP_OLE;
2584 case FCMP_ULT: return FCMP_OGE;
2585 case FCMP_UGE: return FCMP_OLT;
2586 case FCMP_ULE: return FCMP_OGT;
2587 case FCMP_ORD: return FCMP_UNO;
2588 case FCMP_UNO: return FCMP_ORD;
2589 case FCMP_TRUE: return FCMP_FALSE;
2590 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002591 }
2592}
2593
Reid Spencer266e42b2006-12-23 06:05:41 +00002594ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2595 switch (pred) {
2596 default: assert(! "Unknown icmp predicate!");
2597 case ICMP_EQ: case ICMP_NE:
2598 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2599 return pred;
2600 case ICMP_UGT: return ICMP_SGT;
2601 case ICMP_ULT: return ICMP_SLT;
2602 case ICMP_UGE: return ICMP_SGE;
2603 case ICMP_ULE: return ICMP_SLE;
2604 }
2605}
2606
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002607ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2608 switch (pred) {
2609 default: assert(! "Unknown icmp predicate!");
2610 case ICMP_EQ: case ICMP_NE:
2611 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2612 return pred;
2613 case ICMP_SGT: return ICMP_UGT;
2614 case ICMP_SLT: return ICMP_ULT;
2615 case ICMP_SGE: return ICMP_UGE;
2616 case ICMP_SLE: return ICMP_ULE;
2617 }
2618}
2619
Reid Spencer266e42b2006-12-23 06:05:41 +00002620bool ICmpInst::isSignedPredicate(Predicate pred) {
2621 switch (pred) {
2622 default: assert(! "Unknown icmp predicate!");
2623 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2624 return true;
2625 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2626 case ICMP_UGE: case ICMP_ULE:
2627 return false;
2628 }
2629}
2630
Reid Spencer0286bc12007-02-28 22:00:54 +00002631/// Initialize a set of values that all satisfy the condition with C.
2632///
2633ConstantRange
2634ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2635 APInt Lower(C);
2636 APInt Upper(C);
2637 uint32_t BitWidth = C.getBitWidth();
2638 switch (pred) {
2639 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2640 case ICmpInst::ICMP_EQ: Upper++; break;
2641 case ICmpInst::ICMP_NE: Lower++; break;
2642 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2643 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2644 case ICmpInst::ICMP_UGT:
2645 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2646 break;
2647 case ICmpInst::ICMP_SGT:
2648 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2649 break;
2650 case ICmpInst::ICMP_ULE:
2651 Lower = APInt::getMinValue(BitWidth); Upper++;
2652 break;
2653 case ICmpInst::ICMP_SLE:
2654 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2655 break;
2656 case ICmpInst::ICMP_UGE:
2657 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2658 break;
2659 case ICmpInst::ICMP_SGE:
2660 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2661 break;
2662 }
2663 return ConstantRange(Lower, Upper);
2664}
2665
Dan Gohman4e724382008-05-31 02:47:54 +00002666CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002667 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002668 default: assert(!"Unknown cmp predicate!");
2669 case ICMP_EQ: case ICMP_NE:
2670 return pred;
2671 case ICMP_SGT: return ICMP_SLT;
2672 case ICMP_SLT: return ICMP_SGT;
2673 case ICMP_SGE: return ICMP_SLE;
2674 case ICMP_SLE: return ICMP_SGE;
2675 case ICMP_UGT: return ICMP_ULT;
2676 case ICMP_ULT: return ICMP_UGT;
2677 case ICMP_UGE: return ICMP_ULE;
2678 case ICMP_ULE: return ICMP_UGE;
2679
Reid Spencerd9436b62006-11-20 01:22:35 +00002680 case FCMP_FALSE: case FCMP_TRUE:
2681 case FCMP_OEQ: case FCMP_ONE:
2682 case FCMP_UEQ: case FCMP_UNE:
2683 case FCMP_ORD: case FCMP_UNO:
2684 return pred;
2685 case FCMP_OGT: return FCMP_OLT;
2686 case FCMP_OLT: return FCMP_OGT;
2687 case FCMP_OGE: return FCMP_OLE;
2688 case FCMP_OLE: return FCMP_OGE;
2689 case FCMP_UGT: return FCMP_ULT;
2690 case FCMP_ULT: return FCMP_UGT;
2691 case FCMP_UGE: return FCMP_ULE;
2692 case FCMP_ULE: return FCMP_UGE;
2693 }
2694}
2695
Reid Spencer266e42b2006-12-23 06:05:41 +00002696bool CmpInst::isUnsigned(unsigned short predicate) {
2697 switch (predicate) {
2698 default: return false;
2699 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2700 case ICmpInst::ICMP_UGE: return true;
2701 }
2702}
2703
2704bool CmpInst::isSigned(unsigned short predicate){
2705 switch (predicate) {
2706 default: return false;
2707 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2708 case ICmpInst::ICMP_SGE: return true;
2709 }
2710}
2711
2712bool CmpInst::isOrdered(unsigned short predicate) {
2713 switch (predicate) {
2714 default: return false;
2715 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2716 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2717 case FCmpInst::FCMP_ORD: return true;
2718 }
2719}
2720
2721bool CmpInst::isUnordered(unsigned short predicate) {
2722 switch (predicate) {
2723 default: return false;
2724 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2725 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2726 case FCmpInst::FCMP_UNO: return true;
2727 }
2728}
2729
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002730//===----------------------------------------------------------------------===//
2731// SwitchInst Implementation
2732//===----------------------------------------------------------------------===//
2733
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002734void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002735 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002736 ReservedSpace = 2+NumCases*2;
2737 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002738 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002739
Gabor Greif2d3024d2008-05-26 21:33:52 +00002740 OperandList[0] = Value;
2741 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002742}
2743
Chris Lattner2195fc42007-02-24 00:55:48 +00002744/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2745/// switch on and a default destination. The number of additional cases can
2746/// be specified here to make memory allocation more efficient. This
2747/// constructor can also autoinsert before another instruction.
2748SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2749 Instruction *InsertBefore)
2750 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2751 init(Value, Default, NumCases);
2752}
2753
2754/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2755/// switch on and a default destination. The number of additional cases can
2756/// be specified here to make memory allocation more efficient. This
2757/// constructor also autoinserts at the end of the specified BasicBlock.
2758SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2759 BasicBlock *InsertAtEnd)
2760 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2761 init(Value, Default, NumCases);
2762}
2763
Misha Brukmanb1c93172005-04-21 23:48:37 +00002764SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002765 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002766 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002767 Use *OL = OperandList, *InOL = SI.OperandList;
2768 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002769 OL[i] = InOL[i];
2770 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002771 }
2772}
2773
Gordon Henriksen14a55692007-12-10 02:14:30 +00002774SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002775 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002776}
2777
2778
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002779/// addCase - Add an entry to the switch instruction...
2780///
Chris Lattner47ac1872005-02-24 05:32:09 +00002781void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002782 unsigned OpNo = NumOperands;
2783 if (OpNo+2 > ReservedSpace)
2784 resizeOperands(0); // Get more space!
2785 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002786 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002787 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002788 OperandList[OpNo] = OnVal;
2789 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002790}
2791
2792/// removeCase - This method removes the specified successor from the switch
2793/// instruction. Note that this cannot be used to remove the default
2794/// destination (successor #0).
2795///
2796void SwitchInst::removeCase(unsigned idx) {
2797 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002798 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2799
2800 unsigned NumOps = getNumOperands();
2801 Use *OL = OperandList;
2802
2803 // Move everything after this operand down.
2804 //
2805 // FIXME: we could just swap with the end of the list, then erase. However,
2806 // client might not expect this to happen. The code as it is thrashes the
2807 // use/def lists, which is kinda lame.
2808 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2809 OL[i-2] = OL[i];
2810 OL[i-2+1] = OL[i+1];
2811 }
2812
2813 // Nuke the last value.
2814 OL[NumOps-2].set(0);
2815 OL[NumOps-2+1].set(0);
2816 NumOperands = NumOps-2;
2817}
2818
2819/// resizeOperands - resize operands - This adjusts the length of the operands
2820/// list according to the following behavior:
2821/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002822/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002823/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2824/// 3. If NumOps == NumOperands, trim the reserved space.
2825///
2826void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002827 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002828 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002829 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002830 } else if (NumOps*2 > NumOperands) {
2831 // No resize needed.
2832 if (ReservedSpace >= NumOps) return;
2833 } else if (NumOps == NumOperands) {
2834 if (ReservedSpace == NumOps) return;
2835 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002836 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002837 }
2838
2839 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002840 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002841 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002842 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002843 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002844 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002845 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002846 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002847}
2848
2849
2850BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2851 return getSuccessor(idx);
2852}
2853unsigned SwitchInst::getNumSuccessorsV() const {
2854 return getNumSuccessors();
2855}
2856void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2857 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002858}
Chris Lattnerf22be932004-10-15 23:52:53 +00002859
Devang Patel295711f2008-02-19 22:15:16 +00002860//===----------------------------------------------------------------------===//
2861// GetResultInst Implementation
2862//===----------------------------------------------------------------------===//
2863
Devang Patel666d4512008-02-20 19:10:47 +00002864GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002865 const std::string &Name,
2866 Instruction *InsertBef)
Gabor Greif0d42adf2008-05-13 07:09:08 +00002867 : UnaryInstruction(cast<StructType>(Aggregate->getType())
2868 ->getElementType(Index),
2869 GetResult, Aggregate, InsertBef),
2870 Idx(Index) {
2871 assert(isValidOperands(Aggregate, Index)
2872 && "Invalid GetResultInst operands!");
Devang Patel295711f2008-02-19 22:15:16 +00002873 setName(Name);
2874}
2875
Devang Patel666d4512008-02-20 19:10:47 +00002876bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2877 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002878 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002879
Devang Patelcf193b82008-02-20 19:39:41 +00002880 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2881 unsigned NumElements = STy->getNumElements();
Chris Lattnerb6917d22008-04-23 05:36:34 +00002882 if (Index >= NumElements || NumElements == 0)
Devang Patelcf193b82008-02-20 19:39:41 +00002883 return false;
2884
2885 // getresult aggregate value's element types are restricted to
2886 // avoid nested aggregates.
2887 for (unsigned i = 0; i < NumElements; ++i)
2888 if (!STy->getElementType(i)->isFirstClassType())
2889 return false;
2890
2891 // Otherwise, Aggregate is valid.
2892 return true;
2893 }
Devang Patel666d4512008-02-20 19:10:47 +00002894 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002895}
2896
Chris Lattnerf22be932004-10-15 23:52:53 +00002897// Define these methods here so vtables don't get emitted into every translation
2898// unit that uses these classes.
2899
2900GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002901 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002902}
2903
2904BinaryOperator *BinaryOperator::clone() const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002905 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002906}
2907
Chris Lattner0b490b02007-08-24 20:48:18 +00002908FCmpInst* FCmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002909 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002910}
2911ICmpInst* ICmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002912 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002913}
2914
Nate Begemand2195702008-05-12 19:01:56 +00002915VFCmpInst* VFCmpInst::clone() const {
2916 return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
2917}
2918VICmpInst* VICmpInst::clone() const {
2919 return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
2920}
2921
Dan Gohman0752bff2008-05-23 00:36:11 +00002922ExtractValueInst *ExtractValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002923 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002924}
2925InsertValueInst *InsertValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002926 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002927}
2928
2929
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002930MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2931AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2932FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2933LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2934StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2935CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2936CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2937CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2938CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2939CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2940CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2941CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2942CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2943CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2944CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2945CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2946CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002947CallInst *CallInst::clone() const {
2948 return new(getNumOperands()) CallInst(*this);
2949}
2950SelectInst *SelectInst::clone() const {
2951 return new(getNumOperands()) SelectInst(*this);
2952}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002953VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2954
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002955ExtractElementInst *ExtractElementInst::clone() const {
2956 return new ExtractElementInst(*this);
2957}
2958InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002959 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002960}
2961ShuffleVectorInst *ShuffleVectorInst::clone() const {
2962 return new ShuffleVectorInst(*this);
2963}
Chris Lattnerf22be932004-10-15 23:52:53 +00002964PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002965ReturnInst *ReturnInst::clone() const {
2966 return new(getNumOperands()) ReturnInst(*this);
2967}
2968BranchInst *BranchInst::clone() const {
2969 return new(getNumOperands()) BranchInst(*this);
2970}
Gabor Greiff6caff662008-05-10 08:32:32 +00002971SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002972InvokeInst *InvokeInst::clone() const {
2973 return new(getNumOperands()) InvokeInst(*this);
2974}
Chris Lattnerf22be932004-10-15 23:52:53 +00002975UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002976UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002977GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }