blob: 36c3de72cebb82e678b3237e2cb0209f751d9ab7 [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}
Duncan Sands78c88722008-07-08 08:38:44 +000075void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
76 if (CallInst *CI = dyn_cast<CallInst>(I))
77 CI->setDoesNotAccessMemory(doesNotAccessMemory);
78 else
79 cast<InvokeInst>(I)->setDoesNotAccessMemory(doesNotAccessMemory);
80}
Duncan Sands38ef3a82007-12-03 20:06:50 +000081bool CallSite::onlyReadsMemory() const {
82 if (CallInst *CI = dyn_cast<CallInst>(I))
83 return CI->onlyReadsMemory();
84 else
85 return cast<InvokeInst>(I)->onlyReadsMemory();
86}
Duncan Sands78c88722008-07-08 08:38:44 +000087void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
88 if (CallInst *CI = dyn_cast<CallInst>(I))
89 CI->setOnlyReadsMemory(onlyReadsMemory);
90 else
91 cast<InvokeInst>(I)->setOnlyReadsMemory(onlyReadsMemory);
92}
93bool CallSite::doesNotReturn() const {
94 if (CallInst *CI = dyn_cast<CallInst>(I))
95 return CI->doesNotReturn();
96 else
97 return cast<InvokeInst>(I)->doesNotReturn();
98}
99void CallSite::setDoesNotReturn(bool doesNotReturn) {
100 if (CallInst *CI = dyn_cast<CallInst>(I))
101 CI->setDoesNotReturn(doesNotReturn);
102 else
103 cast<InvokeInst>(I)->setDoesNotReturn(doesNotReturn);
104}
Duncan Sands3353ed02007-12-18 09:59:50 +0000105bool CallSite::doesNotThrow() const {
Duncan Sands8e4847e2007-12-16 15:51:49 +0000106 if (CallInst *CI = dyn_cast<CallInst>(I))
Duncan Sands3353ed02007-12-18 09:59:50 +0000107 return CI->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +0000108 else
Duncan Sands3353ed02007-12-18 09:59:50 +0000109 return cast<InvokeInst>(I)->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +0000110}
Duncan Sandsaa31b922007-12-19 21:13:37 +0000111void CallSite::setDoesNotThrow(bool doesNotThrow) {
112 if (CallInst *CI = dyn_cast<CallInst>(I))
113 CI->setDoesNotThrow(doesNotThrow);
114 else
115 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
116}
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000117
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +0000118bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +0000119 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
120 if (AI->get() == Arg)
121 return true;
122 return false;
123}
124
Gordon Henriksen14a55692007-12-10 02:14:30 +0000125//===----------------------------------------------------------------------===//
126// TerminatorInst Class
127//===----------------------------------------------------------------------===//
128
129// Out of line virtual method, so the vtable, etc has a home.
130TerminatorInst::~TerminatorInst() {
131}
132
Gabor Greiff6caff662008-05-10 08:32:32 +0000133//===----------------------------------------------------------------------===//
134// UnaryInstruction Class
135//===----------------------------------------------------------------------===//
136
Gordon Henriksen14a55692007-12-10 02:14:30 +0000137// Out of line virtual method, so the vtable, etc has a home.
138UnaryInstruction::~UnaryInstruction() {
139}
140
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000141//===----------------------------------------------------------------------===//
142// PHINode Class
143//===----------------------------------------------------------------------===//
144
145PHINode::PHINode(const PHINode &PN)
146 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000147 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000148 ReservedSpace(PN.getNumOperands()) {
149 Use *OL = OperandList;
150 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000151 OL[i] = PN.getOperand(i);
152 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000153 }
154}
155
Gordon Henriksen14a55692007-12-10 02:14:30 +0000156PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000157 if (OperandList)
158 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000159}
160
161// removeIncomingValue - Remove an incoming value. This is useful if a
162// predecessor basic block is deleted.
163Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
164 unsigned NumOps = getNumOperands();
165 Use *OL = OperandList;
166 assert(Idx*2 < NumOps && "BB not in PHI node!");
167 Value *Removed = OL[Idx*2];
168
169 // Move everything after this operand down.
170 //
171 // FIXME: we could just swap with the end of the list, then erase. However,
172 // client might not expect this to happen. The code as it is thrashes the
173 // use/def lists, which is kinda lame.
174 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
175 OL[i-2] = OL[i];
176 OL[i-2+1] = OL[i+1];
177 }
178
179 // Nuke the last value.
180 OL[NumOps-2].set(0);
181 OL[NumOps-2+1].set(0);
182 NumOperands = NumOps-2;
183
184 // If the PHI node is dead, because it has zero entries, nuke it now.
185 if (NumOps == 2 && DeletePHIIfEmpty) {
186 // If anyone is using this PHI, make them use a dummy value instead...
187 replaceAllUsesWith(UndefValue::get(getType()));
188 eraseFromParent();
189 }
190 return Removed;
191}
192
193/// resizeOperands - resize operands - This adjusts the length of the operands
194/// list according to the following behavior:
195/// 1. If NumOps == 0, grow the operand list in response to a push_back style
196/// of operation. This grows the number of ops by 1.5 times.
197/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
198/// 3. If NumOps == NumOperands, trim the reserved space.
199///
200void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000201 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000202 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000203 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000204 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
205 } else if (NumOps*2 > NumOperands) {
206 // No resize needed.
207 if (ReservedSpace >= NumOps) return;
208 } else if (NumOps == NumOperands) {
209 if (ReservedSpace == NumOps) return;
210 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000211 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000212 }
213
214 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000216 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000217 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000218 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000219 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000220}
221
Nate Begemanb3923212005-08-04 23:24:19 +0000222/// hasConstantValue - If the specified PHI node always merges together the same
223/// value, return the value, otherwise return null.
224///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000225Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000226 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000227 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000228 if (getIncomingValue(0) != this) // not X = phi X
229 return getIncomingValue(0);
230 else
231 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000232 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000233
Nate Begemanb3923212005-08-04 23:24:19 +0000234 // Otherwise if all of the incoming values are the same for the PHI, replace
235 // the PHI node with the incoming value.
236 //
237 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000238 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000239 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000240 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000241 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000242 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000243 if (InVal && getIncomingValue(i) != InVal)
244 return 0; // Not the same, bail out.
245 else
246 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000247 }
Nate Begemanb3923212005-08-04 23:24:19 +0000248
249 // The only case that could cause InVal to be null is if we have a PHI node
250 // that only has entries for itself. In this case, there is no entry into the
251 // loop, so kill the PHI.
252 //
253 if (InVal == 0) InVal = UndefValue::get(getType());
254
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000255 // If we have a PHI node like phi(X, undef, X), where X is defined by some
256 // instruction, we cannot always return X as the result of the PHI node. Only
257 // do this if X is not an instruction (thus it must dominate the PHI block),
258 // or if the client is prepared to deal with this possibility.
259 if (HasUndefInput && !AllowNonDominatingInstruction)
260 if (Instruction *IV = dyn_cast<Instruction>(InVal))
261 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000262 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000263 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000264 return 0; // Cannot guarantee that InVal dominates this PHINode.
265
Nate Begemanb3923212005-08-04 23:24:19 +0000266 // All of the incoming values are the same, return the value now.
267 return InVal;
268}
269
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000270
271//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272// CallInst Implementation
273//===----------------------------------------------------------------------===//
274
Gordon Henriksen14a55692007-12-10 02:14:30 +0000275CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000276}
277
Chris Lattner054ba2c2007-02-13 00:58:44 +0000278void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000279 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
280 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000281 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000282
Misha Brukmanb1c93172005-04-21 23:48:37 +0000283 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000285 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286
Chris Lattner054ba2c2007-02-13 00:58:44 +0000287 assert((NumParams == FTy->getNumParams() ||
288 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000289 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000290 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000291 assert((i >= FTy->getNumParams() ||
292 FTy->getParamType(i) == Params[i]->getType()) &&
293 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000294 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000295 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296}
297
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000298void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000299 assert(NumOperands == 3 && "NumOperands not set up?");
300 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000301 OL[0] = Func;
302 OL[1] = Actual1;
303 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000304
305 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000307 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000308
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000309 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000310 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000312 assert((0 >= FTy->getNumParams() ||
313 FTy->getParamType(0) == Actual1->getType()) &&
314 "Calling a function with a bad signature!");
315 assert((1 >= FTy->getNumParams() ||
316 FTy->getParamType(1) == Actual2->getType()) &&
317 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318}
319
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000320void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000321 assert(NumOperands == 2 && "NumOperands not set up?");
322 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000323 OL[0] = Func;
324 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000325
326 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000328 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000330 assert((FTy->getNumParams() == 1 ||
331 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000333 assert((0 == FTy->getNumParams() ||
334 FTy->getParamType(0) == Actual->getType()) &&
335 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336}
337
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000338void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000339 assert(NumOperands == 1 && "NumOperands not set up?");
340 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000341 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000342
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000343 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000344 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000345 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000346
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000347 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348}
349
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000351 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000352 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
353 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000354 Instruction::Call,
355 OperandTraits<CallInst>::op_end(this) - 2,
356 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000357 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000358 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000359}
360
361CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
362 BasicBlock *InsertAtEnd)
363 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
364 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000365 Instruction::Call,
366 OperandTraits<CallInst>::op_end(this) - 2,
367 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000368 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000369 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000370}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371CallInst::CallInst(Value *Func, const std::string &Name,
372 Instruction *InsertBefore)
373 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
374 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000375 Instruction::Call,
376 OperandTraits<CallInst>::op_end(this) - 1,
377 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000378 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000379 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000380}
381
382CallInst::CallInst(Value *Func, const std::string &Name,
383 BasicBlock *InsertAtEnd)
384 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
385 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000386 Instruction::Call,
387 OperandTraits<CallInst>::op_end(this) - 1,
388 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000389 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000390 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000391}
392
Misha Brukmanb1c93172005-04-21 23:48:37 +0000393CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000394 : Instruction(CI.getType(), Instruction::Call,
395 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000396 CI.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000397 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000398 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000399 Use *OL = OperandList;
400 Use *InOL = CI.OperandList;
401 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000402 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000403}
404
Eric Christopher901b1a72008-05-16 20:39:43 +0000405void CallInst::addParamAttr(unsigned i, ParameterAttributes attr) {
406 PAListPtr PAL = getParamAttrs();
407 PAL = PAL.addAttr(i, attr);
408 setParamAttrs(PAL);
409}
410
Duncan Sands78c88722008-07-08 08:38:44 +0000411void CallInst::removeParamAttr(unsigned i, ParameterAttributes attr) {
412 PAListPtr PAL = getParamAttrs();
413 PAL = PAL.removeAttr(i, attr);
414 setParamAttrs(PAL);
415}
416
Chris Lattnerc994c022008-03-13 05:00:21 +0000417bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000418 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000419 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000420 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000421 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000422 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000423}
424
Duncan Sands5208d1a2007-11-28 17:07:01 +0000425
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000426//===----------------------------------------------------------------------===//
427// InvokeInst Implementation
428//===----------------------------------------------------------------------===//
429
430void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000431 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000432 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
433 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000434 OL[0] = Fn;
435 OL[1] = IfNormal;
436 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000437 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000438 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000439 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000440
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000441 assert(((NumArgs == FTy->getNumParams()) ||
442 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000443 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000444
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000445 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000446 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000447 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000448 "Invoking a function with a bad signature!");
449
Gabor Greif2d3024d2008-05-26 21:33:52 +0000450 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000451 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000452}
453
Misha Brukmanb1c93172005-04-21 23:48:37 +0000454InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000455 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000456 OperandTraits<InvokeInst>::op_end(this)
457 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000458 II.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000459 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000460 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000461 Use *OL = OperandList, *InOL = II.OperandList;
462 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000463 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000464}
465
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000466BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
467 return getSuccessor(idx);
468}
469unsigned InvokeInst::getNumSuccessorsV() const {
470 return getNumSuccessors();
471}
472void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
473 return setSuccessor(idx, B);
474}
475
Chris Lattnerc994c022008-03-13 05:00:21 +0000476bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000477 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000478 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000479 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000480 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000481 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000482}
483
Eric Christopher901b1a72008-05-16 20:39:43 +0000484void InvokeInst::addParamAttr(unsigned i, ParameterAttributes attr) {
485 PAListPtr PAL = getParamAttrs();
486 PAL = PAL.addAttr(i, attr);
487 setParamAttrs(PAL);
488}
489
Duncan Sands78c88722008-07-08 08:38:44 +0000490void InvokeInst::removeParamAttr(unsigned i, ParameterAttributes attr) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000491 PAListPtr PAL = getParamAttrs();
Duncan Sands78c88722008-07-08 08:38:44 +0000492 PAL = PAL.removeAttr(i, attr);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000493 setParamAttrs(PAL);
494}
495
Duncan Sands5208d1a2007-11-28 17:07:01 +0000496
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000497//===----------------------------------------------------------------------===//
498// ReturnInst Implementation
499//===----------------------------------------------------------------------===//
500
Chris Lattner2195fc42007-02-24 00:55:48 +0000501ReturnInst::ReturnInst(const ReturnInst &RI)
502 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Gabor Greif697e94c2008-05-15 10:04:30 +0000503 OperandTraits<ReturnInst>::op_end(this)
504 - RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000505 RI.getNumOperands()) {
Devang Patel59643e52008-02-23 00:35:18 +0000506 unsigned N = RI.getNumOperands();
Gabor Greiff6caff662008-05-10 08:32:32 +0000507 if (N == 1)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000508 Op<0>() = RI.Op<0>();
Devang Patelae682fb2008-02-26 17:56:20 +0000509 else if (N) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000510 Use *OL = OperandList;
Devang Patelae682fb2008-02-26 17:56:20 +0000511 for (unsigned i = 0; i < N; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000512 OL[i] = RI.getOperand(i);
Devang Patelae682fb2008-02-26 17:56:20 +0000513 }
Chris Lattner2195fc42007-02-24 00:55:48 +0000514}
515
516ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000517 : TerminatorInst(Type::VoidTy, Instruction::Ret,
518 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
519 retVal != 0, InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000520 if (retVal)
521 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000522}
523ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000524 : TerminatorInst(Type::VoidTy, Instruction::Ret,
525 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
526 retVal != 0, InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000527 if (retVal)
528 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000529}
530ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000531 : TerminatorInst(Type::VoidTy, Instruction::Ret,
532 OperandTraits<ReturnInst>::op_end(this),
533 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000534}
535
Devang Patela736c002008-02-26 19:38:17 +0000536ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
537 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000538 : TerminatorInst(Type::VoidTy, Instruction::Ret,
539 OperandTraits<ReturnInst>::op_end(this) - N,
540 N, InsertBefore) {
Devang Patela736c002008-02-26 19:38:17 +0000541 if (N != 0)
542 init(retVals, N);
543}
544ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
545 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000546 : TerminatorInst(Type::VoidTy, Instruction::Ret,
547 OperandTraits<ReturnInst>::op_end(this) - N,
548 N, InsertAtEnd) {
Devang Patela736c002008-02-26 19:38:17 +0000549 if (N != 0)
550 init(retVals, N);
551}
552
Devang Patel060e79c2008-02-26 19:15:26 +0000553void ReturnInst::init(Value * const* retVals, unsigned N) {
Devang Patelc38eb522008-02-26 18:49:29 +0000554 assert (N > 0 && "Invalid operands numbers in ReturnInst init");
Devang Patel59643e52008-02-23 00:35:18 +0000555
Devang Patelc38eb522008-02-26 18:49:29 +0000556 NumOperands = N;
Devang Patel59643e52008-02-23 00:35:18 +0000557 if (NumOperands == 1) {
Devang Patel060e79c2008-02-26 19:15:26 +0000558 Value *V = *retVals;
Devang Patel59643e52008-02-23 00:35:18 +0000559 if (V->getType() == Type::VoidTy)
560 return;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000561 Op<0>() = V;
Devang Patelae682fb2008-02-26 17:56:20 +0000562 return;
Devang Patel59643e52008-02-23 00:35:18 +0000563 }
564
Gabor Greiff6caff662008-05-10 08:32:32 +0000565 Use *OL = OperandList;
Devang Patel59643e52008-02-23 00:35:18 +0000566 for (unsigned i = 0; i < NumOperands; ++i) {
Devang Patel060e79c2008-02-26 19:15:26 +0000567 Value *V = *retVals++;
Devang Patel59643e52008-02-23 00:35:18 +0000568 assert(!isa<BasicBlock>(V) &&
569 "Cannot return basic block. Probably using the incorrect ctor");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000570 OL[i] = V;
Devang Patel59643e52008-02-23 00:35:18 +0000571 }
572}
573
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000574unsigned ReturnInst::getNumSuccessorsV() const {
575 return getNumSuccessors();
576}
577
Devang Patelae682fb2008-02-26 17:56:20 +0000578/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
579/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000580void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000581 assert(0 && "ReturnInst has no successors!");
582}
583
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000584BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
585 assert(0 && "ReturnInst has no successors!");
586 abort();
587 return 0;
588}
589
Devang Patel59643e52008-02-23 00:35:18 +0000590ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000591}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000592
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000593//===----------------------------------------------------------------------===//
594// UnwindInst Implementation
595//===----------------------------------------------------------------------===//
596
Chris Lattner2195fc42007-02-24 00:55:48 +0000597UnwindInst::UnwindInst(Instruction *InsertBefore)
598 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
599}
600UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
601 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
602}
603
604
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000605unsigned UnwindInst::getNumSuccessorsV() const {
606 return getNumSuccessors();
607}
608
609void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000610 assert(0 && "UnwindInst has no successors!");
611}
612
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000613BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
614 assert(0 && "UnwindInst has no successors!");
615 abort();
616 return 0;
617}
618
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000619//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000620// UnreachableInst Implementation
621//===----------------------------------------------------------------------===//
622
Chris Lattner2195fc42007-02-24 00:55:48 +0000623UnreachableInst::UnreachableInst(Instruction *InsertBefore)
624 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
625}
626UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
627 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
628}
629
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000630unsigned UnreachableInst::getNumSuccessorsV() const {
631 return getNumSuccessors();
632}
633
634void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
635 assert(0 && "UnwindInst has no successors!");
636}
637
638BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
639 assert(0 && "UnwindInst has no successors!");
640 abort();
641 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000642}
643
644//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000645// BranchInst Implementation
646//===----------------------------------------------------------------------===//
647
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648void BranchInst::AssertOK() {
649 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000650 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000651 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000652}
653
Chris Lattner2195fc42007-02-24 00:55:48 +0000654BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000655 : TerminatorInst(Type::VoidTy, Instruction::Br,
656 OperandTraits<BranchInst>::op_end(this) - 1,
657 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000658 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000659 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000660}
661BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
662 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000663 : TerminatorInst(Type::VoidTy, Instruction::Br,
664 OperandTraits<BranchInst>::op_end(this) - 3,
665 3, InsertBefore) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000666 Op<0>() = IfTrue;
667 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000668 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000669#ifndef NDEBUG
670 AssertOK();
671#endif
672}
673
674BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000675 : TerminatorInst(Type::VoidTy, Instruction::Br,
676 OperandTraits<BranchInst>::op_end(this) - 1,
677 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000678 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000679 Op<0>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000680}
681
682BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
683 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000684 : TerminatorInst(Type::VoidTy, Instruction::Br,
685 OperandTraits<BranchInst>::op_end(this) - 3,
686 3, InsertAtEnd) {
Gabor Greif0a0f2c12008-05-27 10:48:39 +0000687 Op<0>() = IfTrue;
688 Op<1>() = IfFalse;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000689 Op<2>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000690#ifndef NDEBUG
691 AssertOK();
692#endif
693}
694
695
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000696BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000697 TerminatorInst(Type::VoidTy, Instruction::Br,
698 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
699 BI.getNumOperands()) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000700 OperandList[0] = BI.getOperand(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000701 if (BI.getNumOperands() != 1) {
702 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000703 OperandList[1] = BI.getOperand(1);
704 OperandList[2] = BI.getOperand(2);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000705 }
706}
707
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000708BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
709 return getSuccessor(idx);
710}
711unsigned BranchInst::getNumSuccessorsV() const {
712 return getNumSuccessors();
713}
714void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
715 setSuccessor(idx, B);
716}
717
718
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000719//===----------------------------------------------------------------------===//
720// AllocationInst Implementation
721//===----------------------------------------------------------------------===//
722
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000723static Value *getAISize(Value *Amt) {
724 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000725 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000726 else {
727 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000728 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000729 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000730 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000731 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000732 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733}
734
Misha Brukmanb1c93172005-04-21 23:48:37 +0000735AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000736 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000737 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000738 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000739 InsertBefore) {
740 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000741 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000742 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000743}
744
Misha Brukmanb1c93172005-04-21 23:48:37 +0000745AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000746 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000747 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000748 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000749 InsertAtEnd) {
750 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000751 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000752 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000753}
754
Gordon Henriksen14a55692007-12-10 02:14:30 +0000755// Out of line virtual method, so the vtable, etc has a home.
756AllocationInst::~AllocationInst() {
757}
758
Dan Gohmanaa583d72008-03-24 16:55:58 +0000759void AllocationInst::setAlignment(unsigned Align) {
760 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
761 SubclassData = Log2_32(Align) + 1;
762 assert(getAlignment() == Align && "Alignment representation error!");
763}
764
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000765bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000766 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
767 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000768 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000769}
770
771const Type *AllocationInst::getAllocatedType() const {
772 return getType()->getElementType();
773}
774
775AllocaInst::AllocaInst(const AllocaInst &AI)
776 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000777 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000778}
779
780MallocInst::MallocInst(const MallocInst &MI)
781 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000782 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000783}
784
785//===----------------------------------------------------------------------===//
786// FreeInst Implementation
787//===----------------------------------------------------------------------===//
788
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000789void FreeInst::AssertOK() {
790 assert(isa<PointerType>(getOperand(0)->getType()) &&
791 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000792}
793
794FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000795 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000796 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000797}
798
799FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000800 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000801 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000802}
803
804
805//===----------------------------------------------------------------------===//
806// LoadInst Implementation
807//===----------------------------------------------------------------------===//
808
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000809void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000810 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000811 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000812}
813
814LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000815 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000816 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000817 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000818 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000819 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000820 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000821}
822
823LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000824 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000825 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000826 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000827 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000828 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000829 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000830}
831
832LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
833 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000834 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000835 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000836 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000837 setAlignment(0);
838 AssertOK();
839 setName(Name);
840}
841
842LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
843 unsigned Align, Instruction *InsertBef)
844 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
845 Load, Ptr, InsertBef) {
846 setVolatile(isVolatile);
847 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000848 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000849 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000850}
851
Dan Gohman68659282007-07-18 20:51:11 +0000852LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
853 unsigned Align, BasicBlock *InsertAE)
854 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
855 Load, Ptr, InsertAE) {
856 setVolatile(isVolatile);
857 setAlignment(Align);
858 AssertOK();
859 setName(Name);
860}
861
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000862LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
863 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000864 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000865 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000866 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000867 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000868 AssertOK();
869 setName(Name);
870}
871
872
873
874LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000875 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
876 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000877 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000878 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000879 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000880 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000881}
882
883LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000884 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
885 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000886 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000887 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000888 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000889 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000890}
891
892LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
893 Instruction *InsertBef)
894: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000895 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000896 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000897 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000898 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000899 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000900}
901
902LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
903 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000904 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
905 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000906 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000907 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000908 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000909 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000910}
911
Christopher Lamb84485702007-04-22 19:24:39 +0000912void LoadInst::setAlignment(unsigned Align) {
913 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
914 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
915}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000916
917//===----------------------------------------------------------------------===//
918// StoreInst Implementation
919//===----------------------------------------------------------------------===//
920
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000921void StoreInst::AssertOK() {
922 assert(isa<PointerType>(getOperand(1)->getType()) &&
923 "Ptr must have pointer type!");
924 assert(getOperand(0)->getType() ==
925 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000926 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000927}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000928
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000929
930StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000931 : Instruction(Type::VoidTy, Store,
932 OperandTraits<StoreInst>::op_begin(this),
933 OperandTraits<StoreInst>::operands(this),
934 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000935 Op<0>() = val;
936 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000937 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000938 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000939 AssertOK();
940}
941
942StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000943 : Instruction(Type::VoidTy, Store,
944 OperandTraits<StoreInst>::op_begin(this),
945 OperandTraits<StoreInst>::operands(this),
946 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000947 Op<0>() = val;
948 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000949 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000950 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000951 AssertOK();
952}
953
Misha Brukmanb1c93172005-04-21 23:48:37 +0000954StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000955 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000956 : Instruction(Type::VoidTy, Store,
957 OperandTraits<StoreInst>::op_begin(this),
958 OperandTraits<StoreInst>::operands(this),
959 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000960 Op<0>() = val;
961 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000962 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000963 setAlignment(0);
964 AssertOK();
965}
966
967StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
968 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000969 : Instruction(Type::VoidTy, Store,
970 OperandTraits<StoreInst>::op_begin(this),
971 OperandTraits<StoreInst>::operands(this),
972 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000973 Op<0>() = val;
974 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000975 setVolatile(isVolatile);
976 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000977 AssertOK();
978}
979
Misha Brukmanb1c93172005-04-21 23:48:37 +0000980StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000981 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000982 : Instruction(Type::VoidTy, Store,
983 OperandTraits<StoreInst>::op_begin(this),
984 OperandTraits<StoreInst>::operands(this),
985 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000986 Op<0>() = val;
987 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000988 setVolatile(isVolatile);
989 setAlignment(Align);
990 AssertOK();
991}
992
993StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000994 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000995 : Instruction(Type::VoidTy, Store,
996 OperandTraits<StoreInst>::op_begin(this),
997 OperandTraits<StoreInst>::operands(this),
998 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000999 Op<0>() = val;
1000 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001001 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001002 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001003 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001004}
1005
Christopher Lamb84485702007-04-22 19:24:39 +00001006void StoreInst::setAlignment(unsigned Align) {
1007 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1008 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1009}
1010
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001011//===----------------------------------------------------------------------===//
1012// GetElementPtrInst Implementation
1013//===----------------------------------------------------------------------===//
1014
Christopher Lambedf07882007-12-17 01:12:55 +00001015static unsigned retrieveAddrSpace(const Value *Val) {
1016 return cast<PointerType>(Val->getType())->getAddressSpace();
1017}
1018
Gabor Greif21ba1842008-06-06 20:28:12 +00001019void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
1020 const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001021 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1022 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001023 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001024
Chris Lattner79807c3d2007-01-31 19:47:18 +00001025 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001026 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001027
1028 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001029}
1030
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001031void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001032 assert(NumOperands == 2 && "NumOperands not initialized?");
1033 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001034 OL[0] = Ptr;
1035 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001036
1037 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001038}
1039
Gabor Greiff6caff662008-05-10 08:32:32 +00001040GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001041 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001042 OperandTraits<GetElementPtrInst>::op_end(this)
1043 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001044 GEPI.getNumOperands()) {
1045 Use *OL = OperandList;
1046 Use *GEPIOL = GEPI.OperandList;
1047 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001048 OL[i] = GEPIOL[i];
Gabor Greiff6caff662008-05-10 08:32:32 +00001049}
1050
Chris Lattner82981202005-05-03 05:43:30 +00001051GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1052 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001053 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001054 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001055 GetElementPtr,
1056 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1057 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001058 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001059}
1060
1061GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1062 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001063 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001064 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001065 GetElementPtr,
1066 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1067 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001068 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001069}
1070
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001071// getIndexedType - Returns the type of the element that would be loaded with
1072// a load instruction with the specified parameters.
1073//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001074// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001075// pointer type.
1076//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001077const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001078 Value* const *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001079 unsigned NumIdx) {
1080 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1081 if (!PTy) return 0; // Type isn't a pointer type!
1082 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001083
1084 // Handle the special case of the empty set index set...
Dan Gohman12fce772008-05-15 19:50:34 +00001085 if (NumIdx == 0)
1086 return Agg;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001087
Dan Gohman1ecaf452008-05-31 00:58:22 +00001088 unsigned CurIdx = 1;
1089 for (; CurIdx != NumIdx; ++CurIdx) {
1090 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1091 if (!CT || isa<PointerType>(CT)) return 0;
1092 Value *Index = Idxs[CurIdx];
1093 if (!CT->indexValid(Index)) return 0;
1094 Agg = CT->getTypeAtIndex(Index);
1095
1096 // If the new type forwards to another type, then it is in the middle
1097 // of being refined to another type (and hence, may have dropped all
1098 // references to what it was using before). So, use the new forwarded
1099 // type.
1100 if (const Type *Ty = Agg->getForwardedType())
1101 Agg = Ty;
1102 }
1103 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001104}
1105
Chris Lattner82981202005-05-03 05:43:30 +00001106const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1107 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1108 if (!PTy) return 0; // Type isn't a pointer type!
1109
1110 // Check the pointer index.
1111 if (!PTy->indexValid(Idx)) return 0;
1112
Chris Lattnerc2233332005-05-03 16:44:45 +00001113 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001114}
1115
Chris Lattner45f15572007-04-14 00:12:57 +00001116
1117/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1118/// zeros. If so, the result pointer and the first operand have the same
1119/// value, just potentially different types.
1120bool GetElementPtrInst::hasAllZeroIndices() const {
1121 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1122 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1123 if (!CI->isZero()) return false;
1124 } else {
1125 return false;
1126 }
1127 }
1128 return true;
1129}
1130
Chris Lattner27058292007-04-27 20:35:56 +00001131/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1132/// constant integers. If so, the result pointer and the first operand have
1133/// a constant offset between them.
1134bool GetElementPtrInst::hasAllConstantIndices() const {
1135 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1136 if (!isa<ConstantInt>(getOperand(i)))
1137 return false;
1138 }
1139 return true;
1140}
1141
Chris Lattner45f15572007-04-14 00:12:57 +00001142
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001143//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001144// ExtractElementInst Implementation
1145//===----------------------------------------------------------------------===//
1146
1147ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001148 const std::string &Name,
1149 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001150 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001151 ExtractElement,
1152 OperandTraits<ExtractElementInst>::op_begin(this),
1153 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001154 assert(isValidOperands(Val, Index) &&
1155 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001156 Op<0>() = Val;
1157 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001158 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001159}
1160
Chris Lattner65511ff2006-10-05 06:24:58 +00001161ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1162 const std::string &Name,
1163 Instruction *InsertBef)
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, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001168 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001169 assert(isValidOperands(Val, Index) &&
1170 "Invalid extractelement instruction operands!");
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);
Chris Lattner65511ff2006-10-05 06:24:58 +00001174}
1175
1176
Robert Bocchino23004482006-01-10 19:05:34 +00001177ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001178 const std::string &Name,
1179 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001180 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001181 ExtractElement,
1182 OperandTraits<ExtractElementInst>::op_begin(this),
1183 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +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);
Robert Bocchino23004482006-01-10 19:05:34 +00001190}
1191
Chris Lattner65511ff2006-10-05 06:24:58 +00001192ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1193 const std::string &Name,
1194 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001195 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001196 ExtractElement,
1197 OperandTraits<ExtractElementInst>::op_begin(this),
1198 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001199 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001200 assert(isValidOperands(Val, Index) &&
1201 "Invalid extractelement instruction operands!");
1202
Gabor Greif2d3024d2008-05-26 21:33:52 +00001203 Op<0>() = Val;
1204 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001205 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001206}
1207
1208
Chris Lattner54865b32006-04-08 04:05:48 +00001209bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001210 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001211 return false;
1212 return true;
1213}
1214
1215
Robert Bocchino23004482006-01-10 19:05:34 +00001216//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001217// InsertElementInst Implementation
1218//===----------------------------------------------------------------------===//
1219
Chris Lattner0875d942006-04-14 22:20:32 +00001220InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001221 : Instruction(IE.getType(), InsertElement,
1222 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001223 Op<0>() = IE.Op<0>();
1224 Op<1>() = IE.Op<1>();
1225 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001226}
Chris Lattner54865b32006-04-08 04:05:48 +00001227InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001228 const std::string &Name,
1229 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001230 : Instruction(Vec->getType(), InsertElement,
1231 OperandTraits<InsertElementInst>::op_begin(this),
1232 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001233 assert(isValidOperands(Vec, Elt, Index) &&
1234 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001235 Op<0>() = Vec;
1236 Op<1>() = Elt;
1237 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001238 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001239}
1240
Chris Lattner65511ff2006-10-05 06:24:58 +00001241InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1242 const std::string &Name,
1243 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001244 : Instruction(Vec->getType(), InsertElement,
1245 OperandTraits<InsertElementInst>::op_begin(this),
1246 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001247 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001248 assert(isValidOperands(Vec, Elt, Index) &&
1249 "Invalid insertelement instruction operands!");
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);
Chris Lattner65511ff2006-10-05 06:24:58 +00001254}
1255
1256
Chris Lattner54865b32006-04-08 04:05:48 +00001257InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001258 const std::string &Name,
1259 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001260 : Instruction(Vec->getType(), InsertElement,
1261 OperandTraits<InsertElementInst>::op_begin(this),
1262 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +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);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001270}
1271
Chris Lattner65511ff2006-10-05 06:24:58 +00001272InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1273 const std::string &Name,
1274 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001275: Instruction(Vec->getType(), InsertElement,
1276 OperandTraits<InsertElementInst>::op_begin(this),
1277 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001278 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001279 assert(isValidOperands(Vec, Elt, Index) &&
1280 "Invalid insertelement instruction operands!");
1281
Gabor Greif2d3024d2008-05-26 21:33:52 +00001282 Op<0>() = Vec;
1283 Op<1>() = Elt;
1284 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001285 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001286}
1287
Chris Lattner54865b32006-04-08 04:05:48 +00001288bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1289 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001290 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001291 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001292
Reid Spencerd84d35b2007-02-15 02:26:10 +00001293 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001294 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001295
Reid Spencer8d9336d2006-12-31 05:26:44 +00001296 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001297 return false; // Third operand of insertelement must be uint.
1298 return true;
1299}
1300
1301
Robert Bocchinoca27f032006-01-17 20:07:22 +00001302//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001303// ShuffleVectorInst Implementation
1304//===----------------------------------------------------------------------===//
1305
Chris Lattner0875d942006-04-14 22:20:32 +00001306ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001307 : Instruction(SV.getType(), ShuffleVector,
1308 OperandTraits<ShuffleVectorInst>::op_begin(this),
1309 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001310 Op<0>() = SV.Op<0>();
1311 Op<1>() = SV.Op<1>();
1312 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001313}
1314
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001315ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1316 const std::string &Name,
1317 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001318 : Instruction(V1->getType(), ShuffleVector,
1319 OperandTraits<ShuffleVectorInst>::op_begin(this),
1320 OperandTraits<ShuffleVectorInst>::operands(this),
1321 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001322 assert(isValidOperands(V1, V2, Mask) &&
1323 "Invalid shuffle vector instruction operands!");
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
1330ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1331 const std::string &Name,
1332 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001333 : Instruction(V1->getType(), ShuffleVector,
1334 OperandTraits<ShuffleVectorInst>::op_begin(this),
1335 OperandTraits<ShuffleVectorInst>::operands(this),
1336 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001337 assert(isValidOperands(V1, V2, Mask) &&
1338 "Invalid shuffle vector instruction operands!");
1339
Gabor Greif2d3024d2008-05-26 21:33:52 +00001340 Op<0>() = V1;
1341 Op<1>() = V2;
1342 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001343 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001344}
1345
1346bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1347 const Value *Mask) {
Chris Lattnerf724e342008-03-02 05:28:33 +00001348 if (!isa<VectorType>(V1->getType()) ||
1349 V1->getType() != V2->getType())
1350 return false;
1351
1352 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1353 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1354 MaskTy->getElementType() != Type::Int32Ty ||
1355 MaskTy->getNumElements() !=
1356 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001357 return false;
1358 return true;
1359}
1360
Chris Lattnerf724e342008-03-02 05:28:33 +00001361/// getMaskValue - Return the index from the shuffle mask for the specified
1362/// output result. This is either -1 if the element is undef or a number less
1363/// than 2*numelements.
1364int ShuffleVectorInst::getMaskValue(unsigned i) const {
1365 const Constant *Mask = cast<Constant>(getOperand(2));
1366 if (isa<UndefValue>(Mask)) return -1;
1367 if (isa<ConstantAggregateZero>(Mask)) return 0;
1368 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1369 assert(i < MaskCV->getNumOperands() && "Index out of range");
1370
1371 if (isa<UndefValue>(MaskCV->getOperand(i)))
1372 return -1;
1373 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1374}
1375
Dan Gohman12fce772008-05-15 19:50:34 +00001376//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001377// InsertValueInst Class
1378//===----------------------------------------------------------------------===//
1379
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001380void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1381 unsigned NumIdx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001382 assert(NumOperands == 2 && "NumOperands not initialized?");
1383 Op<0>() = Agg;
1384 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001385
Dan Gohman1ecaf452008-05-31 00:58:22 +00001386 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001387 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001388}
1389
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001390void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1391 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001392 assert(NumOperands == 2 && "NumOperands not initialized?");
1393 Op<0>() = Agg;
1394 Op<1>() = Val;
1395
1396 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001397 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001398}
1399
1400InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001401 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001402 OperandTraits<InsertValueInst>::op_begin(this), 2),
1403 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001404 Op<0>() = IVI.getOperand(0);
1405 Op<1>() = IVI.getOperand(1);
Dan Gohman0752bff2008-05-23 00:36:11 +00001406}
1407
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001408InsertValueInst::InsertValueInst(Value *Agg,
1409 Value *Val,
1410 unsigned Idx,
1411 const std::string &Name,
1412 Instruction *InsertBefore)
1413 : Instruction(Agg->getType(), InsertValue,
1414 OperandTraits<InsertValueInst>::op_begin(this),
1415 2, InsertBefore) {
1416 init(Agg, Val, Idx, Name);
1417}
1418
1419InsertValueInst::InsertValueInst(Value *Agg,
1420 Value *Val,
1421 unsigned Idx,
1422 const std::string &Name,
1423 BasicBlock *InsertAtEnd)
1424 : Instruction(Agg->getType(), InsertValue,
1425 OperandTraits<InsertValueInst>::op_begin(this),
1426 2, InsertAtEnd) {
1427 init(Agg, Val, Idx, Name);
1428}
1429
Dan Gohman0752bff2008-05-23 00:36:11 +00001430//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001431// ExtractValueInst Class
1432//===----------------------------------------------------------------------===//
1433
Gabor Greifcbcc4952008-06-06 21:06:32 +00001434void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Gabor Greif21ba1842008-06-06 20:28:12 +00001435 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001436 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001437
Dan Gohman1ecaf452008-05-31 00:58:22 +00001438 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001439 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001440}
1441
Gabor Greifcbcc4952008-06-06 21:06:32 +00001442void ExtractValueInst::init(unsigned Idx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001443 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001444
1445 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001446 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001447}
1448
1449ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001450 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001451 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001452}
1453
Dan Gohman12fce772008-05-15 19:50:34 +00001454// getIndexedType - Returns the type of the element that would be extracted
1455// with an extractvalue instruction with the specified parameters.
1456//
1457// A null type is returned if the indices are invalid for the specified
1458// pointer type.
1459//
1460const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001461 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001462 unsigned NumIdx) {
1463 unsigned CurIdx = 0;
1464 for (; CurIdx != NumIdx; ++CurIdx) {
1465 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001466 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1467 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001468 if (!CT->indexValid(Index)) return 0;
1469 Agg = CT->getTypeAtIndex(Index);
1470
1471 // If the new type forwards to another type, then it is in the middle
1472 // of being refined to another type (and hence, may have dropped all
1473 // references to what it was using before). So, use the new forwarded
1474 // type.
1475 if (const Type *Ty = Agg->getForwardedType())
1476 Agg = Ty;
1477 }
1478 return CurIdx == NumIdx ? Agg : 0;
1479}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001480
Dan Gohman4a3125b2008-06-17 21:07:55 +00001481const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001482 unsigned Idx) {
1483 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001484}
1485
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001486//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001487// BinaryOperator Class
1488//===----------------------------------------------------------------------===//
1489
Chris Lattner2195fc42007-02-24 00:55:48 +00001490BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1491 const Type *Ty, const std::string &Name,
1492 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001493 : Instruction(Ty, iType,
1494 OperandTraits<BinaryOperator>::op_begin(this),
1495 OperandTraits<BinaryOperator>::operands(this),
1496 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001497 Op<0>() = S1;
1498 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001499 init(iType);
1500 setName(Name);
1501}
1502
1503BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1504 const Type *Ty, const std::string &Name,
1505 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001506 : Instruction(Ty, iType,
1507 OperandTraits<BinaryOperator>::op_begin(this),
1508 OperandTraits<BinaryOperator>::operands(this),
1509 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001510 Op<0>() = S1;
1511 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001512 init(iType);
1513 setName(Name);
1514}
1515
1516
1517void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001518 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001519 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001520 assert(LHS->getType() == RHS->getType() &&
1521 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001522#ifndef NDEBUG
1523 switch (iType) {
1524 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001525 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001526 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001527 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001528 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001529 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001530 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001531 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001532 case UDiv:
1533 case SDiv:
1534 assert(getType() == LHS->getType() &&
1535 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001536 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1537 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001538 "Incorrect operand type (not integer) for S/UDIV");
1539 break;
1540 case FDiv:
1541 assert(getType() == LHS->getType() &&
1542 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001543 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1544 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001545 && "Incorrect operand type (not floating point) for FDIV");
1546 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001547 case URem:
1548 case SRem:
1549 assert(getType() == LHS->getType() &&
1550 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001551 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1552 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001553 "Incorrect operand type (not integer) for S/UREM");
1554 break;
1555 case FRem:
1556 assert(getType() == LHS->getType() &&
1557 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001558 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1559 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001560 && "Incorrect operand type (not floating point) for FREM");
1561 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001562 case Shl:
1563 case LShr:
1564 case AShr:
1565 assert(getType() == LHS->getType() &&
1566 "Shift operation should return same type as operands!");
1567 assert(getType()->isInteger() &&
1568 "Shift operation requires integer operands");
1569 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001570 case And: case Or:
1571 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001572 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001573 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001574 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001575 (isa<VectorType>(getType()) &&
1576 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001577 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001578 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001579 default:
1580 break;
1581 }
1582#endif
1583}
1584
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001585BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001586 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001587 Instruction *InsertBefore) {
1588 assert(S1->getType() == S2->getType() &&
1589 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001590 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001591}
1592
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001593BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001594 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001595 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001596 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001597 InsertAtEnd->getInstList().push_back(Res);
1598 return Res;
1599}
1600
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001601BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001602 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001603 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1604 return new BinaryOperator(Instruction::Sub,
1605 zero, Op,
1606 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001607}
1608
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001609BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001610 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001611 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1612 return new BinaryOperator(Instruction::Sub,
1613 zero, Op,
1614 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001615}
1616
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001617BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001618 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001619 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001620 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001621 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001622 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001623 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001624 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001625 }
1626
1627 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001628 Op->getType(), Name, InsertBefore);
1629}
1630
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001631BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001632 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001633 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001634 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001635 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001636 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001637 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001638 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001639 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001640 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001641 }
1642
1643 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001644 Op->getType(), Name, InsertAtEnd);
1645}
1646
1647
1648// isConstantAllOnes - Helper function for several functions below
1649static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001650 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1651 return CI->isAllOnesValue();
1652 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1653 return CV->isAllOnesValue();
1654 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001655}
1656
1657bool BinaryOperator::isNeg(const Value *V) {
1658 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1659 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001660 return Bop->getOperand(0) ==
1661 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001662 return false;
1663}
1664
1665bool BinaryOperator::isNot(const Value *V) {
1666 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1667 return (Bop->getOpcode() == Instruction::Xor &&
1668 (isConstantAllOnes(Bop->getOperand(1)) ||
1669 isConstantAllOnes(Bop->getOperand(0))));
1670 return false;
1671}
1672
Chris Lattner2c7d1772005-04-24 07:28:37 +00001673Value *BinaryOperator::getNegArgument(Value *BinOp) {
1674 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1675 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001676}
1677
Chris Lattner2c7d1772005-04-24 07:28:37 +00001678const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1679 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001680}
1681
Chris Lattner2c7d1772005-04-24 07:28:37 +00001682Value *BinaryOperator::getNotArgument(Value *BinOp) {
1683 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1684 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1685 Value *Op0 = BO->getOperand(0);
1686 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001687 if (isConstantAllOnes(Op0)) return Op1;
1688
1689 assert(isConstantAllOnes(Op1));
1690 return Op0;
1691}
1692
Chris Lattner2c7d1772005-04-24 07:28:37 +00001693const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1694 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001695}
1696
1697
1698// swapOperands - Exchange the two operands to this instruction. This
1699// instruction is safe to use on any binary instruction and does not
1700// modify the semantics of the instruction. If the instruction is
1701// order dependent (SetLT f.e.) the opcode is changed.
1702//
1703bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001704 if (!isCommutative())
1705 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001706 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001707 return false;
1708}
1709
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001710//===----------------------------------------------------------------------===//
1711// CastInst Class
1712//===----------------------------------------------------------------------===//
1713
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001714// Just determine if this cast only deals with integral->integral conversion.
1715bool CastInst::isIntegerCast() const {
1716 switch (getOpcode()) {
1717 default: return false;
1718 case Instruction::ZExt:
1719 case Instruction::SExt:
1720 case Instruction::Trunc:
1721 return true;
1722 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001723 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001724 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001725}
1726
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001727bool CastInst::isLosslessCast() const {
1728 // Only BitCast can be lossless, exit fast if we're not BitCast
1729 if (getOpcode() != Instruction::BitCast)
1730 return false;
1731
1732 // Identity cast is always lossless
1733 const Type* SrcTy = getOperand(0)->getType();
1734 const Type* DstTy = getType();
1735 if (SrcTy == DstTy)
1736 return true;
1737
Reid Spencer8d9336d2006-12-31 05:26:44 +00001738 // Pointer to pointer is always lossless.
1739 if (isa<PointerType>(SrcTy))
1740 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001741 return false; // Other types have no identity values
1742}
1743
1744/// This function determines if the CastInst does not require any bits to be
1745/// changed in order to effect the cast. Essentially, it identifies cases where
1746/// no code gen is necessary for the cast, hence the name no-op cast. For
1747/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001748/// # bitcast i32* %x to i8*
1749/// # bitcast <2 x i32> %x to <4 x i16>
1750/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001751/// @brief Determine if a cast is a no-op.
1752bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1753 switch (getOpcode()) {
1754 default:
1755 assert(!"Invalid CastOp");
1756 case Instruction::Trunc:
1757 case Instruction::ZExt:
1758 case Instruction::SExt:
1759 case Instruction::FPTrunc:
1760 case Instruction::FPExt:
1761 case Instruction::UIToFP:
1762 case Instruction::SIToFP:
1763 case Instruction::FPToUI:
1764 case Instruction::FPToSI:
1765 return false; // These always modify bits
1766 case Instruction::BitCast:
1767 return true; // BitCast never modifies bits.
1768 case Instruction::PtrToInt:
1769 return IntPtrTy->getPrimitiveSizeInBits() ==
1770 getType()->getPrimitiveSizeInBits();
1771 case Instruction::IntToPtr:
1772 return IntPtrTy->getPrimitiveSizeInBits() ==
1773 getOperand(0)->getType()->getPrimitiveSizeInBits();
1774 }
1775}
1776
1777/// This function determines if a pair of casts can be eliminated and what
1778/// opcode should be used in the elimination. This assumes that there are two
1779/// instructions like this:
1780/// * %F = firstOpcode SrcTy %x to MidTy
1781/// * %S = secondOpcode MidTy %F to DstTy
1782/// The function returns a resultOpcode so these two casts can be replaced with:
1783/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1784/// If no such cast is permited, the function returns 0.
1785unsigned CastInst::isEliminableCastPair(
1786 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1787 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1788{
1789 // Define the 144 possibilities for these two cast instructions. The values
1790 // in this matrix determine what to do in a given situation and select the
1791 // case in the switch below. The rows correspond to firstOp, the columns
1792 // correspond to secondOp. In looking at the table below, keep in mind
1793 // the following cast properties:
1794 //
1795 // Size Compare Source Destination
1796 // Operator Src ? Size Type Sign Type Sign
1797 // -------- ------------ ------------------- ---------------------
1798 // TRUNC > Integer Any Integral Any
1799 // ZEXT < Integral Unsigned Integer Any
1800 // SEXT < Integral Signed Integer Any
1801 // FPTOUI n/a FloatPt n/a Integral Unsigned
1802 // FPTOSI n/a FloatPt n/a Integral Signed
1803 // UITOFP n/a Integral Unsigned FloatPt n/a
1804 // SITOFP n/a Integral Signed FloatPt n/a
1805 // FPTRUNC > FloatPt n/a FloatPt n/a
1806 // FPEXT < FloatPt n/a FloatPt n/a
1807 // PTRTOINT n/a Pointer n/a Integral Unsigned
1808 // INTTOPTR n/a Integral Unsigned Pointer n/a
1809 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001810 //
1811 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1812 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1813 // into "fptoui double to ulong", but this loses information about the range
1814 // of the produced value (we no longer know the top-part is all zeros).
1815 // Further this conversion is often much more expensive for typical hardware,
1816 // and causes issues when building libgcc. We disallow fptosi+sext for the
1817 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001818 const unsigned numCastOps =
1819 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1820 static const uint8_t CastResults[numCastOps][numCastOps] = {
1821 // T F F U S F F P I B -+
1822 // R Z S P P I I T P 2 N T |
1823 // U E E 2 2 2 2 R E I T C +- secondOp
1824 // N X X U S F F N X N 2 V |
1825 // C T T I I P P C T T P T -+
1826 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1827 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1828 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001829 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1830 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001831 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1832 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1833 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1834 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1835 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1836 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1837 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1838 };
1839
1840 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1841 [secondOp-Instruction::CastOpsBegin];
1842 switch (ElimCase) {
1843 case 0:
1844 // categorically disallowed
1845 return 0;
1846 case 1:
1847 // allowed, use first cast's opcode
1848 return firstOp;
1849 case 2:
1850 // allowed, use second cast's opcode
1851 return secondOp;
1852 case 3:
1853 // no-op cast in second op implies firstOp as long as the DestTy
1854 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001855 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001856 return firstOp;
1857 return 0;
1858 case 4:
1859 // no-op cast in second op implies firstOp as long as the DestTy
1860 // is floating point
1861 if (DstTy->isFloatingPoint())
1862 return firstOp;
1863 return 0;
1864 case 5:
1865 // no-op cast in first op implies secondOp as long as the SrcTy
1866 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001867 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001868 return secondOp;
1869 return 0;
1870 case 6:
1871 // no-op cast in first op implies secondOp as long as the SrcTy
1872 // is a floating point
1873 if (SrcTy->isFloatingPoint())
1874 return secondOp;
1875 return 0;
1876 case 7: {
1877 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1878 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1879 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1880 if (MidSize >= PtrSize)
1881 return Instruction::BitCast;
1882 return 0;
1883 }
1884 case 8: {
1885 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1886 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1887 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1888 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1889 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1890 if (SrcSize == DstSize)
1891 return Instruction::BitCast;
1892 else if (SrcSize < DstSize)
1893 return firstOp;
1894 return secondOp;
1895 }
1896 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1897 return Instruction::ZExt;
1898 case 10:
1899 // fpext followed by ftrunc is allowed if the bit size returned to is
1900 // the same as the original, in which case its just a bitcast
1901 if (SrcTy == DstTy)
1902 return Instruction::BitCast;
1903 return 0; // If the types are not the same we can't eliminate it.
1904 case 11:
1905 // bitcast followed by ptrtoint is allowed as long as the bitcast
1906 // is a pointer to pointer cast.
1907 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1908 return secondOp;
1909 return 0;
1910 case 12:
1911 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1912 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1913 return firstOp;
1914 return 0;
1915 case 13: {
1916 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1917 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1918 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1919 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1920 if (SrcSize <= PtrSize && SrcSize == DstSize)
1921 return Instruction::BitCast;
1922 return 0;
1923 }
1924 case 99:
1925 // cast combination can't happen (error in input). This is for all cases
1926 // where the MidTy is not the same for the two cast instructions.
1927 assert(!"Invalid Cast Combination");
1928 return 0;
1929 default:
1930 assert(!"Error in CastResults table!!!");
1931 return 0;
1932 }
1933 return 0;
1934}
1935
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001936CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001937 const std::string &Name, Instruction *InsertBefore) {
1938 // Construct and return the appropriate CastInst subclass
1939 switch (op) {
1940 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1941 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1942 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1943 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1944 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1945 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1946 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1947 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1948 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1949 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1950 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1951 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1952 default:
1953 assert(!"Invalid opcode provided");
1954 }
1955 return 0;
1956}
1957
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001958CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001959 const std::string &Name, BasicBlock *InsertAtEnd) {
1960 // Construct and return the appropriate CastInst subclass
1961 switch (op) {
1962 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1963 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1964 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1965 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1966 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1967 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1968 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1969 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1970 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1971 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1972 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1973 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1974 default:
1975 assert(!"Invalid opcode provided");
1976 }
1977 return 0;
1978}
1979
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001980CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001981 const std::string &Name,
1982 Instruction *InsertBefore) {
1983 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001984 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1985 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001986}
1987
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001988CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001989 const std::string &Name,
1990 BasicBlock *InsertAtEnd) {
1991 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001992 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1993 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00001994}
1995
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001996CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001997 const std::string &Name,
1998 Instruction *InsertBefore) {
1999 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002000 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2001 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002002}
2003
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002004CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002005 const std::string &Name,
2006 BasicBlock *InsertAtEnd) {
2007 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002008 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2009 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002010}
2011
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002012CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002013 const std::string &Name,
2014 Instruction *InsertBefore) {
2015 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002016 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2017 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002018}
2019
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002020CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002021 const std::string &Name,
2022 BasicBlock *InsertAtEnd) {
2023 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002024 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2025 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002026}
2027
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002028CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002029 const std::string &Name,
2030 BasicBlock *InsertAtEnd) {
2031 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002032 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002033 "Invalid cast");
2034
Chris Lattner03c49532007-01-15 02:27:26 +00002035 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002036 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2037 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002038}
2039
2040/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002041CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002042 const std::string &Name,
2043 Instruction *InsertBefore) {
2044 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002045 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002046 "Invalid cast");
2047
Chris Lattner03c49532007-01-15 02:27:26 +00002048 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002049 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2050 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002051}
2052
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002053CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002054 bool isSigned, const std::string &Name,
2055 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002056 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002057 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2058 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2059 Instruction::CastOps opcode =
2060 (SrcBits == DstBits ? Instruction::BitCast :
2061 (SrcBits > DstBits ? Instruction::Trunc :
2062 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002063 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002064}
2065
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002066CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002067 bool isSigned, const std::string &Name,
2068 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00002069 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00002070 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2071 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2072 Instruction::CastOps opcode =
2073 (SrcBits == DstBits ? Instruction::BitCast :
2074 (SrcBits > DstBits ? Instruction::Trunc :
2075 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002076 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002077}
2078
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002079CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002080 const std::string &Name,
2081 Instruction *InsertBefore) {
2082 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2083 "Invalid cast");
2084 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2085 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2086 Instruction::CastOps opcode =
2087 (SrcBits == DstBits ? Instruction::BitCast :
2088 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002089 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002090}
2091
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002092CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002093 const std::string &Name,
2094 BasicBlock *InsertAtEnd) {
2095 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
2096 "Invalid cast");
2097 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
2098 unsigned DstBits = Ty->getPrimitiveSizeInBits();
2099 Instruction::CastOps opcode =
2100 (SrcBits == DstBits ? Instruction::BitCast :
2101 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002102 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002103}
2104
Duncan Sands55e50902008-01-06 10:12:28 +00002105// Check whether it is valid to call getCastOpcode for these types.
2106// This routine must be kept in sync with getCastOpcode.
2107bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2108 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2109 return false;
2110
2111 if (SrcTy == DestTy)
2112 return true;
2113
2114 // Get the bit sizes, we'll need these
2115 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2116 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2117
2118 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002119 if (DestTy->isInteger()) { // Casting to integral
2120 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002121 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002122 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002123 return true;
2124 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002125 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002126 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002127 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002128 return isa<PointerType>(SrcTy);
2129 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002130 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2131 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002132 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002133 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002134 return true;
2135 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002136 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002137 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002138 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002139 return false;
2140 }
2141 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002142 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002143 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002144 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002145 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002146 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002147 return DestPTy->getBitWidth() == SrcBits;
2148 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002149 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2150 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002151 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002152 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002153 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002154 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002155 return false;
2156 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002157 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002158 return false;
2159 }
2160}
2161
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002162// Provide a way to get a "cast" where the cast opcode is inferred from the
2163// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002164// logic in the castIsValid function below. This axiom should hold:
2165// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2166// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002167// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002168// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002169Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002170CastInst::getCastOpcode(
2171 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002172 // Get the bit sizes, we'll need these
2173 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002174 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2175 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002176
Duncan Sands55e50902008-01-06 10:12:28 +00002177 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2178 "Only first class types are castable!");
2179
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002180 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002181 if (DestTy->isInteger()) { // Casting to integral
2182 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002183 if (DestBits < SrcBits)
2184 return Trunc; // int -> smaller int
2185 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002186 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002187 return SExt; // signed -> SEXT
2188 else
2189 return ZExt; // unsigned -> ZEXT
2190 } else {
2191 return BitCast; // Same size, No-op cast
2192 }
2193 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002194 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002195 return FPToSI; // FP -> sint
2196 else
2197 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002198 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002199 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002200 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002201 return BitCast; // Same size, no-op cast
2202 } else {
2203 assert(isa<PointerType>(SrcTy) &&
2204 "Casting from a value that is not first-class type");
2205 return PtrToInt; // ptr -> int
2206 }
2207 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002208 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002209 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002210 return SIToFP; // sint -> FP
2211 else
2212 return UIToFP; // uint -> FP
2213 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2214 if (DestBits < SrcBits) {
2215 return FPTrunc; // FP -> smaller FP
2216 } else if (DestBits > SrcBits) {
2217 return FPExt; // FP -> larger FP
2218 } else {
2219 return BitCast; // same size, no-op cast
2220 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002221 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002222 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002223 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002224 return BitCast; // same size, no-op cast
2225 } else {
2226 assert(0 && "Casting pointer or non-first class to float");
2227 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002228 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2229 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002230 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002231 "Casting vector to vector of different widths");
2232 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002233 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002234 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002235 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002236 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237 }
2238 } else if (isa<PointerType>(DestTy)) {
2239 if (isa<PointerType>(SrcTy)) {
2240 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002241 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002242 return IntToPtr; // int -> ptr
2243 } else {
2244 assert(!"Casting pointer to other than pointer or int");
2245 }
2246 } else {
2247 assert(!"Casting to type that is not first-class");
2248 }
2249
2250 // If we fall through to here we probably hit an assertion cast above
2251 // and assertions are not turned on. Anything we return is an error, so
2252 // BitCast is as good a choice as any.
2253 return BitCast;
2254}
2255
2256//===----------------------------------------------------------------------===//
2257// CastInst SubClass Constructors
2258//===----------------------------------------------------------------------===//
2259
2260/// Check that the construction parameters for a CastInst are correct. This
2261/// could be broken out into the separate constructors but it is useful to have
2262/// it in one place and to eliminate the redundant code for getting the sizes
2263/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002264bool
2265CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002266
2267 // Check for type sanity on the arguments
2268 const Type *SrcTy = S->getType();
2269 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2270 return false;
2271
2272 // Get the size of the types in bits, we'll need this later
2273 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2274 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2275
2276 // Switch on the opcode provided
2277 switch (op) {
2278 default: return false; // This is an input error
2279 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002280 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002281 case Instruction::ZExt:
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::SExt:
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::FPTrunc:
2286 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2287 SrcBitSize > DstBitSize;
2288 case Instruction::FPExt:
2289 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2290 SrcBitSize < DstBitSize;
2291 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002292 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002293 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2294 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2295 return SVTy->getElementType()->isInteger() &&
2296 DVTy->getElementType()->isFloatingPoint() &&
2297 SVTy->getNumElements() == DVTy->getNumElements();
2298 }
2299 }
Chris Lattner03c49532007-01-15 02:27:26 +00002300 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002302 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002303 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2304 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2305 return SVTy->getElementType()->isFloatingPoint() &&
2306 DVTy->getElementType()->isInteger() &&
2307 SVTy->getNumElements() == DVTy->getNumElements();
2308 }
2309 }
Chris Lattner03c49532007-01-15 02:27:26 +00002310 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002311 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002312 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002313 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002314 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002315 case Instruction::BitCast:
2316 // BitCast implies a no-op cast of type only. No bits change.
2317 // However, you can't cast pointers to anything but pointers.
2318 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2319 return false;
2320
Duncan Sands55e50902008-01-06 10:12:28 +00002321 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002322 // these cases, the cast is okay if the source and destination bit widths
2323 // are identical.
2324 return SrcBitSize == DstBitSize;
2325 }
2326}
2327
2328TruncInst::TruncInst(
2329 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2330) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002331 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002332}
2333
2334TruncInst::TruncInst(
2335 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2336) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002337 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002338}
2339
2340ZExtInst::ZExtInst(
2341 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2342) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002343 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344}
2345
2346ZExtInst::ZExtInst(
2347 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2348) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002349 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002350}
2351SExtInst::SExtInst(
2352 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2353) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002354 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002355}
2356
Jeff Cohencc08c832006-12-02 02:22:01 +00002357SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002358 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2359) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002360 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002361}
2362
2363FPTruncInst::FPTruncInst(
2364 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2365) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002366 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002367}
2368
2369FPTruncInst::FPTruncInst(
2370 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2371) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002372 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002373}
2374
2375FPExtInst::FPExtInst(
2376 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2377) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002378 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379}
2380
2381FPExtInst::FPExtInst(
2382 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2383) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002384 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002385}
2386
2387UIToFPInst::UIToFPInst(
2388 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2389) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002390 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391}
2392
2393UIToFPInst::UIToFPInst(
2394 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2395) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002396 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002397}
2398
2399SIToFPInst::SIToFPInst(
2400 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2401) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002402 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002403}
2404
2405SIToFPInst::SIToFPInst(
2406 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2407) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002408 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002409}
2410
2411FPToUIInst::FPToUIInst(
2412 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2413) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002414 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002415}
2416
2417FPToUIInst::FPToUIInst(
2418 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2419) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002420 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002421}
2422
2423FPToSIInst::FPToSIInst(
2424 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2425) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002426 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002427}
2428
2429FPToSIInst::FPToSIInst(
2430 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2431) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002432 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002433}
2434
2435PtrToIntInst::PtrToIntInst(
2436 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2437) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002438 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002439}
2440
2441PtrToIntInst::PtrToIntInst(
2442 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2443) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002444 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002445}
2446
2447IntToPtrInst::IntToPtrInst(
2448 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2449) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002450 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002451}
2452
2453IntToPtrInst::IntToPtrInst(
2454 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2455) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002456 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002457}
2458
2459BitCastInst::BitCastInst(
2460 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2461) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002462 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463}
2464
2465BitCastInst::BitCastInst(
2466 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2467) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002468 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002470
2471//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002472// CmpInst Classes
2473//===----------------------------------------------------------------------===//
2474
Nate Begemand2195702008-05-12 19:01:56 +00002475CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2476 Value *LHS, Value *RHS, const std::string &Name,
2477 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002478 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002479 OperandTraits<CmpInst>::op_begin(this),
2480 OperandTraits<CmpInst>::operands(this),
2481 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002482 Op<0>() = LHS;
2483 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002484 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002485 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002486}
Gabor Greiff6caff662008-05-10 08:32:32 +00002487
Nate Begemand2195702008-05-12 19:01:56 +00002488CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2489 Value *LHS, Value *RHS, const std::string &Name,
2490 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002491 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002492 OperandTraits<CmpInst>::op_begin(this),
2493 OperandTraits<CmpInst>::operands(this),
2494 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002495 Op<0>() = LHS;
2496 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002497 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002498 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002499}
2500
2501CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002502CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002503 const std::string &Name, Instruction *InsertBefore) {
2504 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002505 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002506 InsertBefore);
2507 }
Nate Begemand2195702008-05-12 19:01:56 +00002508 if (Op == Instruction::FCmp) {
2509 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2510 InsertBefore);
2511 }
2512 if (Op == Instruction::VICmp) {
2513 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2514 InsertBefore);
2515 }
2516 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2517 InsertBefore);
Reid Spencerd9436b62006-11-20 01:22:35 +00002518}
2519
2520CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002521CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002522 const std::string &Name, BasicBlock *InsertAtEnd) {
2523 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002524 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002525 InsertAtEnd);
2526 }
Nate Begemand2195702008-05-12 19:01:56 +00002527 if (Op == Instruction::FCmp) {
2528 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2529 InsertAtEnd);
2530 }
2531 if (Op == Instruction::VICmp) {
2532 return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2533 InsertAtEnd);
2534 }
2535 return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2536 InsertAtEnd);
Reid Spencerd9436b62006-11-20 01:22:35 +00002537}
2538
2539void CmpInst::swapOperands() {
2540 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2541 IC->swapOperands();
2542 else
2543 cast<FCmpInst>(this)->swapOperands();
2544}
2545
2546bool CmpInst::isCommutative() {
2547 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2548 return IC->isCommutative();
2549 return cast<FCmpInst>(this)->isCommutative();
2550}
2551
2552bool CmpInst::isEquality() {
2553 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2554 return IC->isEquality();
2555 return cast<FCmpInst>(this)->isEquality();
2556}
2557
2558
Dan Gohman4e724382008-05-31 02:47:54 +00002559CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002560 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002561 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002562 case ICMP_EQ: return ICMP_NE;
2563 case ICMP_NE: return ICMP_EQ;
2564 case ICMP_UGT: return ICMP_ULE;
2565 case ICMP_ULT: return ICMP_UGE;
2566 case ICMP_UGE: return ICMP_ULT;
2567 case ICMP_ULE: return ICMP_UGT;
2568 case ICMP_SGT: return ICMP_SLE;
2569 case ICMP_SLT: return ICMP_SGE;
2570 case ICMP_SGE: return ICMP_SLT;
2571 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002572
Dan Gohman4e724382008-05-31 02:47:54 +00002573 case FCMP_OEQ: return FCMP_UNE;
2574 case FCMP_ONE: return FCMP_UEQ;
2575 case FCMP_OGT: return FCMP_ULE;
2576 case FCMP_OLT: return FCMP_UGE;
2577 case FCMP_OGE: return FCMP_ULT;
2578 case FCMP_OLE: return FCMP_UGT;
2579 case FCMP_UEQ: return FCMP_ONE;
2580 case FCMP_UNE: return FCMP_OEQ;
2581 case FCMP_UGT: return FCMP_OLE;
2582 case FCMP_ULT: return FCMP_OGE;
2583 case FCMP_UGE: return FCMP_OLT;
2584 case FCMP_ULE: return FCMP_OGT;
2585 case FCMP_ORD: return FCMP_UNO;
2586 case FCMP_UNO: return FCMP_ORD;
2587 case FCMP_TRUE: return FCMP_FALSE;
2588 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002589 }
2590}
2591
Reid Spencer266e42b2006-12-23 06:05:41 +00002592ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2593 switch (pred) {
2594 default: assert(! "Unknown icmp predicate!");
2595 case ICMP_EQ: case ICMP_NE:
2596 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2597 return pred;
2598 case ICMP_UGT: return ICMP_SGT;
2599 case ICMP_ULT: return ICMP_SLT;
2600 case ICMP_UGE: return ICMP_SGE;
2601 case ICMP_ULE: return ICMP_SLE;
2602 }
2603}
2604
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002605ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2606 switch (pred) {
2607 default: assert(! "Unknown icmp predicate!");
2608 case ICMP_EQ: case ICMP_NE:
2609 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2610 return pred;
2611 case ICMP_SGT: return ICMP_UGT;
2612 case ICMP_SLT: return ICMP_ULT;
2613 case ICMP_SGE: return ICMP_UGE;
2614 case ICMP_SLE: return ICMP_ULE;
2615 }
2616}
2617
Reid Spencer266e42b2006-12-23 06:05:41 +00002618bool ICmpInst::isSignedPredicate(Predicate pred) {
2619 switch (pred) {
2620 default: assert(! "Unknown icmp predicate!");
2621 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2622 return true;
2623 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2624 case ICMP_UGE: case ICMP_ULE:
2625 return false;
2626 }
2627}
2628
Reid Spencer0286bc12007-02-28 22:00:54 +00002629/// Initialize a set of values that all satisfy the condition with C.
2630///
2631ConstantRange
2632ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2633 APInt Lower(C);
2634 APInt Upper(C);
2635 uint32_t BitWidth = C.getBitWidth();
2636 switch (pred) {
2637 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2638 case ICmpInst::ICMP_EQ: Upper++; break;
2639 case ICmpInst::ICMP_NE: Lower++; break;
2640 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2641 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2642 case ICmpInst::ICMP_UGT:
2643 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2644 break;
2645 case ICmpInst::ICMP_SGT:
2646 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2647 break;
2648 case ICmpInst::ICMP_ULE:
2649 Lower = APInt::getMinValue(BitWidth); Upper++;
2650 break;
2651 case ICmpInst::ICMP_SLE:
2652 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2653 break;
2654 case ICmpInst::ICMP_UGE:
2655 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2656 break;
2657 case ICmpInst::ICMP_SGE:
2658 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2659 break;
2660 }
2661 return ConstantRange(Lower, Upper);
2662}
2663
Dan Gohman4e724382008-05-31 02:47:54 +00002664CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002665 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002666 default: assert(!"Unknown cmp predicate!");
2667 case ICMP_EQ: case ICMP_NE:
2668 return pred;
2669 case ICMP_SGT: return ICMP_SLT;
2670 case ICMP_SLT: return ICMP_SGT;
2671 case ICMP_SGE: return ICMP_SLE;
2672 case ICMP_SLE: return ICMP_SGE;
2673 case ICMP_UGT: return ICMP_ULT;
2674 case ICMP_ULT: return ICMP_UGT;
2675 case ICMP_UGE: return ICMP_ULE;
2676 case ICMP_ULE: return ICMP_UGE;
2677
Reid Spencerd9436b62006-11-20 01:22:35 +00002678 case FCMP_FALSE: case FCMP_TRUE:
2679 case FCMP_OEQ: case FCMP_ONE:
2680 case FCMP_UEQ: case FCMP_UNE:
2681 case FCMP_ORD: case FCMP_UNO:
2682 return pred;
2683 case FCMP_OGT: return FCMP_OLT;
2684 case FCMP_OLT: return FCMP_OGT;
2685 case FCMP_OGE: return FCMP_OLE;
2686 case FCMP_OLE: return FCMP_OGE;
2687 case FCMP_UGT: return FCMP_ULT;
2688 case FCMP_ULT: return FCMP_UGT;
2689 case FCMP_UGE: return FCMP_ULE;
2690 case FCMP_ULE: return FCMP_UGE;
2691 }
2692}
2693
Reid Spencer266e42b2006-12-23 06:05:41 +00002694bool CmpInst::isUnsigned(unsigned short predicate) {
2695 switch (predicate) {
2696 default: return false;
2697 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2698 case ICmpInst::ICMP_UGE: return true;
2699 }
2700}
2701
2702bool CmpInst::isSigned(unsigned short predicate){
2703 switch (predicate) {
2704 default: return false;
2705 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2706 case ICmpInst::ICMP_SGE: return true;
2707 }
2708}
2709
2710bool CmpInst::isOrdered(unsigned short predicate) {
2711 switch (predicate) {
2712 default: return false;
2713 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2714 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2715 case FCmpInst::FCMP_ORD: return true;
2716 }
2717}
2718
2719bool CmpInst::isUnordered(unsigned short predicate) {
2720 switch (predicate) {
2721 default: return false;
2722 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2723 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2724 case FCmpInst::FCMP_UNO: return true;
2725 }
2726}
2727
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002728//===----------------------------------------------------------------------===//
2729// SwitchInst Implementation
2730//===----------------------------------------------------------------------===//
2731
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002732void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002733 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002734 ReservedSpace = 2+NumCases*2;
2735 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002736 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002737
Gabor Greif2d3024d2008-05-26 21:33:52 +00002738 OperandList[0] = Value;
2739 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002740}
2741
Chris Lattner2195fc42007-02-24 00:55:48 +00002742/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2743/// switch on and a default destination. The number of additional cases can
2744/// be specified here to make memory allocation more efficient. This
2745/// constructor can also autoinsert before another instruction.
2746SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2747 Instruction *InsertBefore)
2748 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2749 init(Value, Default, NumCases);
2750}
2751
2752/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2753/// switch on and a default destination. The number of additional cases can
2754/// be specified here to make memory allocation more efficient. This
2755/// constructor also autoinserts at the end of the specified BasicBlock.
2756SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2757 BasicBlock *InsertAtEnd)
2758 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2759 init(Value, Default, NumCases);
2760}
2761
Misha Brukmanb1c93172005-04-21 23:48:37 +00002762SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002763 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002764 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002765 Use *OL = OperandList, *InOL = SI.OperandList;
2766 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002767 OL[i] = InOL[i];
2768 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002769 }
2770}
2771
Gordon Henriksen14a55692007-12-10 02:14:30 +00002772SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002773 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002774}
2775
2776
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002777/// addCase - Add an entry to the switch instruction...
2778///
Chris Lattner47ac1872005-02-24 05:32:09 +00002779void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002780 unsigned OpNo = NumOperands;
2781 if (OpNo+2 > ReservedSpace)
2782 resizeOperands(0); // Get more space!
2783 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002784 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002785 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002786 OperandList[OpNo] = OnVal;
2787 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002788}
2789
2790/// removeCase - This method removes the specified successor from the switch
2791/// instruction. Note that this cannot be used to remove the default
2792/// destination (successor #0).
2793///
2794void SwitchInst::removeCase(unsigned idx) {
2795 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002796 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2797
2798 unsigned NumOps = getNumOperands();
2799 Use *OL = OperandList;
2800
2801 // Move everything after this operand down.
2802 //
2803 // FIXME: we could just swap with the end of the list, then erase. However,
2804 // client might not expect this to happen. The code as it is thrashes the
2805 // use/def lists, which is kinda lame.
2806 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2807 OL[i-2] = OL[i];
2808 OL[i-2+1] = OL[i+1];
2809 }
2810
2811 // Nuke the last value.
2812 OL[NumOps-2].set(0);
2813 OL[NumOps-2+1].set(0);
2814 NumOperands = NumOps-2;
2815}
2816
2817/// resizeOperands - resize operands - This adjusts the length of the operands
2818/// list according to the following behavior:
2819/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002820/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002821/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2822/// 3. If NumOps == NumOperands, trim the reserved space.
2823///
2824void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002825 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002826 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002827 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002828 } else if (NumOps*2 > NumOperands) {
2829 // No resize needed.
2830 if (ReservedSpace >= NumOps) return;
2831 } else if (NumOps == NumOperands) {
2832 if (ReservedSpace == NumOps) return;
2833 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002834 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002835 }
2836
2837 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002838 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002839 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002840 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002841 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002842 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002843 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002844 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002845}
2846
2847
2848BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2849 return getSuccessor(idx);
2850}
2851unsigned SwitchInst::getNumSuccessorsV() const {
2852 return getNumSuccessors();
2853}
2854void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2855 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002856}
Chris Lattnerf22be932004-10-15 23:52:53 +00002857
Devang Patel295711f2008-02-19 22:15:16 +00002858//===----------------------------------------------------------------------===//
2859// GetResultInst Implementation
2860//===----------------------------------------------------------------------===//
2861
Devang Patel666d4512008-02-20 19:10:47 +00002862GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002863 const std::string &Name,
2864 Instruction *InsertBef)
Gabor Greif0d42adf2008-05-13 07:09:08 +00002865 : UnaryInstruction(cast<StructType>(Aggregate->getType())
2866 ->getElementType(Index),
2867 GetResult, Aggregate, InsertBef),
2868 Idx(Index) {
2869 assert(isValidOperands(Aggregate, Index)
2870 && "Invalid GetResultInst operands!");
Devang Patel295711f2008-02-19 22:15:16 +00002871 setName(Name);
2872}
2873
Devang Patel666d4512008-02-20 19:10:47 +00002874bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2875 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002876 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002877
Devang Patelcf193b82008-02-20 19:39:41 +00002878 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2879 unsigned NumElements = STy->getNumElements();
Chris Lattnerb6917d22008-04-23 05:36:34 +00002880 if (Index >= NumElements || NumElements == 0)
Devang Patelcf193b82008-02-20 19:39:41 +00002881 return false;
2882
2883 // getresult aggregate value's element types are restricted to
2884 // avoid nested aggregates.
2885 for (unsigned i = 0; i < NumElements; ++i)
2886 if (!STy->getElementType(i)->isFirstClassType())
2887 return false;
2888
2889 // Otherwise, Aggregate is valid.
2890 return true;
2891 }
Devang Patel666d4512008-02-20 19:10:47 +00002892 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002893}
2894
Chris Lattnerf22be932004-10-15 23:52:53 +00002895// Define these methods here so vtables don't get emitted into every translation
2896// unit that uses these classes.
2897
2898GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002899 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002900}
2901
2902BinaryOperator *BinaryOperator::clone() const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002903 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002904}
2905
Chris Lattner0b490b02007-08-24 20:48:18 +00002906FCmpInst* FCmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002907 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002908}
2909ICmpInst* ICmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002910 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002911}
2912
Nate Begemand2195702008-05-12 19:01:56 +00002913VFCmpInst* VFCmpInst::clone() const {
2914 return new VFCmpInst(getPredicate(), Op<0>(), Op<1>());
2915}
2916VICmpInst* VICmpInst::clone() const {
2917 return new VICmpInst(getPredicate(), Op<0>(), Op<1>());
2918}
2919
Dan Gohman0752bff2008-05-23 00:36:11 +00002920ExtractValueInst *ExtractValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002921 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002922}
2923InsertValueInst *InsertValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002924 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002925}
2926
2927
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002928MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2929AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2930FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2931LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2932StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2933CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2934CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2935CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2936CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2937CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2938CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2939CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2940CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2941CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2942CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2943CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2944CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002945CallInst *CallInst::clone() const {
2946 return new(getNumOperands()) CallInst(*this);
2947}
2948SelectInst *SelectInst::clone() const {
2949 return new(getNumOperands()) SelectInst(*this);
2950}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002951VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2952
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002953ExtractElementInst *ExtractElementInst::clone() const {
2954 return new ExtractElementInst(*this);
2955}
2956InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002957 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002958}
2959ShuffleVectorInst *ShuffleVectorInst::clone() const {
2960 return new ShuffleVectorInst(*this);
2961}
Chris Lattnerf22be932004-10-15 23:52:53 +00002962PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002963ReturnInst *ReturnInst::clone() const {
2964 return new(getNumOperands()) ReturnInst(*this);
2965}
2966BranchInst *BranchInst::clone() const {
2967 return new(getNumOperands()) BranchInst(*this);
2968}
Gabor Greiff6caff662008-05-10 08:32:32 +00002969SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002970InvokeInst *InvokeInst::clone() const {
2971 return new(getNumOperands()) InvokeInst(*this);
2972}
Chris Lattnerf22be932004-10-15 23:52:53 +00002973UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002974UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002975GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }