blob: 863f011cd07b9e65de76bd938cca3657d9d7128e [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
15#include "llvm/BasicBlock.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Reid Spencerce38beb2007-04-09 18:00:57 +000020#include "llvm/ParameterAttributes.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000023#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024using namespace llvm;
25
Chris Lattner3e13b8c2008-01-02 23:42:30 +000026//===----------------------------------------------------------------------===//
27// CallSite Class
28//===----------------------------------------------------------------------===//
29
Duncan Sands85fab3a2008-02-18 17:32:13 +000030CallSite::CallSite(Instruction *C) {
31 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
32 I = C;
33}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000034unsigned CallSite::getCallingConv() const {
35 if (CallInst *CI = dyn_cast<CallInst>(I))
36 return CI->getCallingConv();
37 else
38 return cast<InvokeInst>(I)->getCallingConv();
39}
40void CallSite::setCallingConv(unsigned CC) {
41 if (CallInst *CI = dyn_cast<CallInst>(I))
42 CI->setCallingConv(CC);
43 else
44 cast<InvokeInst>(I)->setCallingConv(CC);
45}
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000046const ParamAttrsList* CallSite::getParamAttrs() const {
47 if (CallInst *CI = dyn_cast<CallInst>(I))
48 return CI->getParamAttrs();
49 else
50 return cast<InvokeInst>(I)->getParamAttrs();
51}
52void CallSite::setParamAttrs(const ParamAttrsList *PAL) {
53 if (CallInst *CI = dyn_cast<CallInst>(I))
54 CI->setParamAttrs(PAL);
55 else
56 cast<InvokeInst>(I)->setParamAttrs(PAL);
57}
Dale Johannesen89268bc2008-02-19 21:38:47 +000058bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
Duncan Sands5208d1a2007-11-28 17:07:01 +000059 if (CallInst *CI = dyn_cast<CallInst>(I))
Dale Johannesen89268bc2008-02-19 21:38:47 +000060 return CI->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000061 else
Dale Johannesen89268bc2008-02-19 21:38:47 +000062 return cast<InvokeInst>(I)->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000063}
Duncan Sands38ef3a82007-12-03 20:06:50 +000064bool CallSite::doesNotAccessMemory() const {
65 if (CallInst *CI = dyn_cast<CallInst>(I))
66 return CI->doesNotAccessMemory();
67 else
68 return cast<InvokeInst>(I)->doesNotAccessMemory();
69}
70bool CallSite::onlyReadsMemory() const {
71 if (CallInst *CI = dyn_cast<CallInst>(I))
72 return CI->onlyReadsMemory();
73 else
74 return cast<InvokeInst>(I)->onlyReadsMemory();
75}
Duncan Sands3353ed02007-12-18 09:59:50 +000076bool CallSite::doesNotThrow() const {
Duncan Sands8e4847e2007-12-16 15:51:49 +000077 if (CallInst *CI = dyn_cast<CallInst>(I))
Duncan Sands3353ed02007-12-18 09:59:50 +000078 return CI->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000079 else
Duncan Sands3353ed02007-12-18 09:59:50 +000080 return cast<InvokeInst>(I)->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000081}
Duncan Sandsaa31b922007-12-19 21:13:37 +000082void CallSite::setDoesNotThrow(bool doesNotThrow) {
83 if (CallInst *CI = dyn_cast<CallInst>(I))
84 CI->setDoesNotThrow(doesNotThrow);
85 else
86 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
87}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000088
Gordon Henriksen14a55692007-12-10 02:14:30 +000089//===----------------------------------------------------------------------===//
90// TerminatorInst Class
91//===----------------------------------------------------------------------===//
92
93// Out of line virtual method, so the vtable, etc has a home.
94TerminatorInst::~TerminatorInst() {
95}
96
97// Out of line virtual method, so the vtable, etc has a home.
98UnaryInstruction::~UnaryInstruction() {
99}
100
101
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000102//===----------------------------------------------------------------------===//
103// PHINode Class
104//===----------------------------------------------------------------------===//
105
106PHINode::PHINode(const PHINode &PN)
107 : Instruction(PN.getType(), Instruction::PHI,
108 new Use[PN.getNumOperands()], PN.getNumOperands()),
109 ReservedSpace(PN.getNumOperands()) {
110 Use *OL = OperandList;
111 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
112 OL[i].init(PN.getOperand(i), this);
113 OL[i+1].init(PN.getOperand(i+1), this);
114 }
115}
116
Gordon Henriksen14a55692007-12-10 02:14:30 +0000117PHINode::~PHINode() {
118 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000119}
120
121// removeIncomingValue - Remove an incoming value. This is useful if a
122// predecessor basic block is deleted.
123Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
124 unsigned NumOps = getNumOperands();
125 Use *OL = OperandList;
126 assert(Idx*2 < NumOps && "BB not in PHI node!");
127 Value *Removed = OL[Idx*2];
128
129 // Move everything after this operand down.
130 //
131 // FIXME: we could just swap with the end of the list, then erase. However,
132 // client might not expect this to happen. The code as it is thrashes the
133 // use/def lists, which is kinda lame.
134 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
135 OL[i-2] = OL[i];
136 OL[i-2+1] = OL[i+1];
137 }
138
139 // Nuke the last value.
140 OL[NumOps-2].set(0);
141 OL[NumOps-2+1].set(0);
142 NumOperands = NumOps-2;
143
144 // If the PHI node is dead, because it has zero entries, nuke it now.
145 if (NumOps == 2 && DeletePHIIfEmpty) {
146 // If anyone is using this PHI, make them use a dummy value instead...
147 replaceAllUsesWith(UndefValue::get(getType()));
148 eraseFromParent();
149 }
150 return Removed;
151}
152
153/// resizeOperands - resize operands - This adjusts the length of the operands
154/// list according to the following behavior:
155/// 1. If NumOps == 0, grow the operand list in response to a push_back style
156/// of operation. This grows the number of ops by 1.5 times.
157/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
158/// 3. If NumOps == NumOperands, trim the reserved space.
159///
160void PHINode::resizeOperands(unsigned NumOps) {
161 if (NumOps == 0) {
162 NumOps = (getNumOperands())*3/2;
163 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
164 } else if (NumOps*2 > NumOperands) {
165 // No resize needed.
166 if (ReservedSpace >= NumOps) return;
167 } else if (NumOps == NumOperands) {
168 if (ReservedSpace == NumOps) return;
169 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000170 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000171 }
172
173 ReservedSpace = NumOps;
174 Use *NewOps = new Use[NumOps];
175 Use *OldOps = OperandList;
176 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
177 NewOps[i].init(OldOps[i], this);
178 OldOps[i].set(0);
179 }
180 delete [] OldOps;
181 OperandList = NewOps;
182}
183
Nate Begemanb3923212005-08-04 23:24:19 +0000184/// hasConstantValue - If the specified PHI node always merges together the same
185/// value, return the value, otherwise return null.
186///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000187Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000188 // If the PHI node only has one incoming value, eliminate the PHI node...
189 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000190 if (getIncomingValue(0) != this) // not X = phi X
191 return getIncomingValue(0);
192 else
193 return UndefValue::get(getType()); // Self cycle is dead.
194
Nate Begemanb3923212005-08-04 23:24:19 +0000195 // Otherwise if all of the incoming values are the same for the PHI, replace
196 // the PHI node with the incoming value.
197 //
198 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000199 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000200 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000201 if (isa<UndefValue>(getIncomingValue(i)))
202 HasUndefInput = true;
203 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000204 if (InVal && getIncomingValue(i) != InVal)
205 return 0; // Not the same, bail out.
206 else
207 InVal = getIncomingValue(i);
208
209 // The only case that could cause InVal to be null is if we have a PHI node
210 // that only has entries for itself. In this case, there is no entry into the
211 // loop, so kill the PHI.
212 //
213 if (InVal == 0) InVal = UndefValue::get(getType());
214
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000215 // If we have a PHI node like phi(X, undef, X), where X is defined by some
216 // instruction, we cannot always return X as the result of the PHI node. Only
217 // do this if X is not an instruction (thus it must dominate the PHI block),
218 // or if the client is prepared to deal with this possibility.
219 if (HasUndefInput && !AllowNonDominatingInstruction)
220 if (Instruction *IV = dyn_cast<Instruction>(InVal))
221 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000222 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000223 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000224 return 0; // Cannot guarantee that InVal dominates this PHINode.
225
Nate Begemanb3923212005-08-04 23:24:19 +0000226 // All of the incoming values are the same, return the value now.
227 return InVal;
228}
229
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000230
231//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000232// CallInst Implementation
233//===----------------------------------------------------------------------===//
234
Gordon Henriksen14a55692007-12-10 02:14:30 +0000235CallInst::~CallInst() {
236 delete [] OperandList;
237 if (ParamAttrs)
238 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000239}
240
Chris Lattner054ba2c2007-02-13 00:58:44 +0000241void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000242 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000243 NumOperands = NumParams+1;
244 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000245 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000246
Misha Brukmanb1c93172005-04-21 23:48:37 +0000247 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000248 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000249 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000250
Chris Lattner054ba2c2007-02-13 00:58:44 +0000251 assert((NumParams == FTy->getNumParams() ||
252 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000253 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000254 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000255 assert((i >= FTy->getNumParams() ||
256 FTy->getParamType(i) == Params[i]->getType()) &&
257 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000258 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000259 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260}
261
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000262void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000263 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000264 NumOperands = 3;
265 Use *OL = OperandList = new Use[3];
266 OL[0].init(Func, this);
267 OL[1].init(Actual1, this);
268 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000269
270 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000271 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000272 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000274 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000275 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000276 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000277 assert((0 >= FTy->getNumParams() ||
278 FTy->getParamType(0) == Actual1->getType()) &&
279 "Calling a function with a bad signature!");
280 assert((1 >= FTy->getNumParams() ||
281 FTy->getParamType(1) == Actual2->getType()) &&
282 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000283}
284
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000285void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000286 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000287 NumOperands = 2;
288 Use *OL = OperandList = new Use[2];
289 OL[0].init(Func, this);
290 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000291
292 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000293 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000294 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000295
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000296 assert((FTy->getNumParams() == 1 ||
297 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000299 assert((0 == FTy->getNumParams() ||
300 FTy->getParamType(0) == Actual->getType()) &&
301 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302}
303
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000304void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000305 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000306 NumOperands = 1;
307 Use *OL = OperandList = new Use[1];
308 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000309
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000310 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000312 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000314 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315}
316
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000317CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000318 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000319 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
320 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000321 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000323 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324}
325
326CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
327 BasicBlock *InsertAtEnd)
328 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
329 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000330 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000332 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000333}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000334CallInst::CallInst(Value *Func, const std::string &Name,
335 Instruction *InsertBefore)
336 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
337 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000338 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000339 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000340 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341}
342
343CallInst::CallInst(Value *Func, const std::string &Name,
344 BasicBlock *InsertAtEnd)
345 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
346 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000347 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000349 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350}
351
Misha Brukmanb1c93172005-04-21 23:48:37 +0000352CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000353 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000354 CI.getNumOperands()),
355 ParamAttrs(0) {
356 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000357 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000358 Use *OL = OperandList;
359 Use *InOL = CI.OperandList;
360 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
361 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362}
363
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000364void CallInst::setParamAttrs(const ParamAttrsList *newAttrs) {
365 if (ParamAttrs == newAttrs)
366 return;
367
Reid Spencerc6a83842007-04-22 17:28:03 +0000368 if (ParamAttrs)
369 ParamAttrs->dropRef();
370
371 if (newAttrs)
372 newAttrs->addRef();
373
374 ParamAttrs = newAttrs;
375}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000376
Dale Johannesen89268bc2008-02-19 21:38:47 +0000377bool CallInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
378 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000379 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000380 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000381 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000382 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000383}
384
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000385/// @brief Determine if the call does not access memory.
386bool CallInst::doesNotAccessMemory() const {
387 return paramHasAttr(0, ParamAttr::ReadNone);
388}
389
390/// @brief Determine if the call does not access or only reads memory.
391bool CallInst::onlyReadsMemory() const {
392 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
393}
394
395/// @brief Determine if the call cannot return.
396bool CallInst::doesNotReturn() const {
397 return paramHasAttr(0, ParamAttr::NoReturn);
398}
399
400/// @brief Determine if the call cannot unwind.
401bool CallInst::doesNotThrow() const {
402 return paramHasAttr(0, ParamAttr::NoUnwind);
403}
404
405/// @brief Determine if the call returns a structure.
406bool CallInst::isStructReturn() const {
407 // Be friendly and also check the callee.
408 return paramHasAttr(1, ParamAttr::StructRet);
409}
410
Evan Cheng09dde602008-01-12 18:57:32 +0000411/// @brief Determine if any call argument is an aggregate passed by value.
412bool CallInst::hasByValArgument() const {
Duncan Sandsa59f3962008-01-21 11:27:55 +0000413 if (ParamAttrs && ParamAttrs->hasAttrSomewhere(ParamAttr::ByVal))
414 return true;
415 // Be consistent with other methods and check the callee too.
416 if (const Function *F = getCalledFunction())
417 if (const ParamAttrsList *PAL = F->getParamAttrs())
418 return PAL->hasAttrSomewhere(ParamAttr::ByVal);
419 return false;
Evan Cheng09dde602008-01-12 18:57:32 +0000420}
421
Duncan Sandsaa31b922007-12-19 21:13:37 +0000422void CallInst::setDoesNotThrow(bool doesNotThrow) {
423 const ParamAttrsList *PAL = getParamAttrs();
424 if (doesNotThrow)
425 PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind);
426 else
427 PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind);
428 setParamAttrs(PAL);
429}
430
Duncan Sands5208d1a2007-11-28 17:07:01 +0000431
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000432//===----------------------------------------------------------------------===//
433// InvokeInst Implementation
434//===----------------------------------------------------------------------===//
435
Gordon Henriksen14a55692007-12-10 02:14:30 +0000436InvokeInst::~InvokeInst() {
437 delete [] OperandList;
438 if (ParamAttrs)
439 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000440}
441
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000442void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000443 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000444 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000445 NumOperands = 3+NumArgs;
446 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000447 OL[0].init(Fn, this);
448 OL[1].init(IfNormal, this);
449 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000450 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000451 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000452 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000453
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000454 assert((NumArgs == FTy->getNumParams()) ||
455 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000456 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000457
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000458 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000459 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000460 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000461 "Invoking a function with a bad signature!");
462
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000463 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000464 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000465}
466
Misha Brukmanb1c93172005-04-21 23:48:37 +0000467InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000468 : TerminatorInst(II.getType(), Instruction::Invoke,
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000469 new Use[II.getNumOperands()], II.getNumOperands()),
470 ParamAttrs(0) {
471 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000472 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000473 Use *OL = OperandList, *InOL = II.OperandList;
474 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
475 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000476}
477
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000478BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
479 return getSuccessor(idx);
480}
481unsigned InvokeInst::getNumSuccessorsV() const {
482 return getNumSuccessors();
483}
484void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
485 return setSuccessor(idx, B);
486}
487
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000488void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) {
489 if (ParamAttrs == newAttrs)
490 return;
491
Reid Spencerc6a83842007-04-22 17:28:03 +0000492 if (ParamAttrs)
493 ParamAttrs->dropRef();
494
495 if (newAttrs)
496 newAttrs->addRef();
497
498 ParamAttrs = newAttrs;
499}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000500
Dale Johannesen89268bc2008-02-19 21:38:47 +0000501bool InvokeInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
502 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000503 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000504 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000505 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000506 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000507}
508
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000509
510/// @brief Determine if the call does not access memory.
511bool InvokeInst::doesNotAccessMemory() const {
512 return paramHasAttr(0, ParamAttr::ReadNone);
513}
514
515/// @brief Determine if the call does not access or only reads memory.
516bool InvokeInst::onlyReadsMemory() const {
517 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
518}
519
520/// @brief Determine if the call cannot return.
521bool InvokeInst::doesNotReturn() const {
522 return paramHasAttr(0, ParamAttr::NoReturn);
523}
524
525/// @brief Determine if the call cannot unwind.
526bool InvokeInst::doesNotThrow() const {
527 return paramHasAttr(0, ParamAttr::NoUnwind);
528}
529
Duncan Sandsaa31b922007-12-19 21:13:37 +0000530void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
531 const ParamAttrsList *PAL = getParamAttrs();
532 if (doesNotThrow)
533 PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind);
534 else
535 PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind);
536 setParamAttrs(PAL);
537}
538
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000539/// @brief Determine if the call returns a structure.
540bool InvokeInst::isStructReturn() const {
541 // Be friendly and also check the callee.
542 return paramHasAttr(1, ParamAttr::StructRet);
543}
544
Duncan Sands5208d1a2007-11-28 17:07:01 +0000545
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000546//===----------------------------------------------------------------------===//
547// ReturnInst Implementation
548//===----------------------------------------------------------------------===//
549
Chris Lattner2195fc42007-02-24 00:55:48 +0000550ReturnInst::ReturnInst(const ReturnInst &RI)
551 : TerminatorInst(Type::VoidTy, Instruction::Ret,
552 &RetVal, RI.getNumOperands()) {
553 if (RI.getNumOperands())
554 RetVal.init(RI.RetVal, this);
555}
556
557ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
558 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
559 init(retVal);
560}
561ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
562 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
563 init(retVal);
564}
565ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
566 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
567}
568
569
570
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000571void ReturnInst::init(Value *retVal) {
572 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000573 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000574 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000575 NumOperands = 1;
576 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000577 }
578}
579
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000580unsigned ReturnInst::getNumSuccessorsV() const {
581 return getNumSuccessors();
582}
583
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000584// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
585// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000586void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000587 assert(0 && "ReturnInst has no successors!");
588}
589
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000590BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
591 assert(0 && "ReturnInst has no successors!");
592 abort();
593 return 0;
594}
595
596
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000597//===----------------------------------------------------------------------===//
598// UnwindInst Implementation
599//===----------------------------------------------------------------------===//
600
Chris Lattner2195fc42007-02-24 00:55:48 +0000601UnwindInst::UnwindInst(Instruction *InsertBefore)
602 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
603}
604UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
605 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
606}
607
608
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000609unsigned UnwindInst::getNumSuccessorsV() const {
610 return getNumSuccessors();
611}
612
613void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000614 assert(0 && "UnwindInst has no successors!");
615}
616
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000617BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
618 assert(0 && "UnwindInst has no successors!");
619 abort();
620 return 0;
621}
622
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000623//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000624// UnreachableInst Implementation
625//===----------------------------------------------------------------------===//
626
Chris Lattner2195fc42007-02-24 00:55:48 +0000627UnreachableInst::UnreachableInst(Instruction *InsertBefore)
628 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
629}
630UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
631 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
632}
633
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000634unsigned UnreachableInst::getNumSuccessorsV() const {
635 return getNumSuccessors();
636}
637
638void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
639 assert(0 && "UnwindInst has no successors!");
640}
641
642BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
643 assert(0 && "UnwindInst has no successors!");
644 abort();
645 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000646}
647
648//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000649// BranchInst Implementation
650//===----------------------------------------------------------------------===//
651
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000652void BranchInst::AssertOK() {
653 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000654 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000656}
657
Chris Lattner2195fc42007-02-24 00:55:48 +0000658BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
659 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
660 assert(IfTrue != 0 && "Branch destination may not be null!");
661 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
662}
663BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
664 Instruction *InsertBefore)
665: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
666 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
667 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
668 Ops[2].init(Cond, this);
669#ifndef NDEBUG
670 AssertOK();
671#endif
672}
673
674BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
675 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
676 assert(IfTrue != 0 && "Branch destination may not be null!");
677 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
678}
679
680BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
681 BasicBlock *InsertAtEnd)
682 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
683 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
684 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
685 Ops[2].init(Cond, this);
686#ifndef NDEBUG
687 AssertOK();
688#endif
689}
690
691
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000692BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000693 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000694 OperandList[0].init(BI.getOperand(0), this);
695 if (BI.getNumOperands() != 1) {
696 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
697 OperandList[1].init(BI.getOperand(1), this);
698 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000699 }
700}
701
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000702BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
703 return getSuccessor(idx);
704}
705unsigned BranchInst::getNumSuccessorsV() const {
706 return getNumSuccessors();
707}
708void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
709 setSuccessor(idx, B);
710}
711
712
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000713//===----------------------------------------------------------------------===//
714// AllocationInst Implementation
715//===----------------------------------------------------------------------===//
716
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000717static Value *getAISize(Value *Amt) {
718 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000719 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000720 else {
721 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000722 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000723 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000724 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000725 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000726 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000727}
728
Misha Brukmanb1c93172005-04-21 23:48:37 +0000729AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000730 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000731 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000732 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000733 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000734 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000735 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000736 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000737}
738
Misha Brukmanb1c93172005-04-21 23:48:37 +0000739AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000740 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000741 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000742 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000743 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000744 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000745 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000746 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000747}
748
Gordon Henriksen14a55692007-12-10 02:14:30 +0000749// Out of line virtual method, so the vtable, etc has a home.
750AllocationInst::~AllocationInst() {
751}
752
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000753bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000754 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
755 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000756 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000757}
758
759const Type *AllocationInst::getAllocatedType() const {
760 return getType()->getElementType();
761}
762
763AllocaInst::AllocaInst(const AllocaInst &AI)
764 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000765 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000766}
767
768MallocInst::MallocInst(const MallocInst &MI)
769 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000770 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000771}
772
773//===----------------------------------------------------------------------===//
774// FreeInst Implementation
775//===----------------------------------------------------------------------===//
776
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000777void FreeInst::AssertOK() {
778 assert(isa<PointerType>(getOperand(0)->getType()) &&
779 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000780}
781
782FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000783 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000784 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000785}
786
787FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000788 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000789 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000790}
791
792
793//===----------------------------------------------------------------------===//
794// LoadInst Implementation
795//===----------------------------------------------------------------------===//
796
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000797void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000798 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000799 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000800}
801
802LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000803 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000804 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000805 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000806 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000807 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000808 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000809}
810
811LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000812 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000813 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000814 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000815 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000816 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000817 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000818}
819
820LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
821 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000822 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000823 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000824 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000825 setAlignment(0);
826 AssertOK();
827 setName(Name);
828}
829
830LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
831 unsigned Align, Instruction *InsertBef)
832 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
833 Load, Ptr, InsertBef) {
834 setVolatile(isVolatile);
835 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000836 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000837 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000838}
839
Dan Gohman68659282007-07-18 20:51:11 +0000840LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
841 unsigned Align, BasicBlock *InsertAE)
842 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
843 Load, Ptr, InsertAE) {
844 setVolatile(isVolatile);
845 setAlignment(Align);
846 AssertOK();
847 setName(Name);
848}
849
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000850LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
851 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000852 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000853 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000854 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000855 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000856 AssertOK();
857 setName(Name);
858}
859
860
861
862LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000863 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
864 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000865 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000866 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000867 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000868 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000869}
870
871LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000872 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
873 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000874 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000875 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000876 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000877 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000878}
879
880LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
881 Instruction *InsertBef)
882: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000883 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000884 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000885 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000886 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000887 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000888}
889
890LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
891 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000892 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
893 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000894 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000895 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000896 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000897 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000898}
899
Christopher Lamb84485702007-04-22 19:24:39 +0000900void LoadInst::setAlignment(unsigned Align) {
901 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
902 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
903}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000904
905//===----------------------------------------------------------------------===//
906// StoreInst Implementation
907//===----------------------------------------------------------------------===//
908
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000909void StoreInst::AssertOK() {
910 assert(isa<PointerType>(getOperand(1)->getType()) &&
911 "Ptr must have pointer type!");
912 assert(getOperand(0)->getType() ==
913 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000914 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000915}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000916
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000917
918StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000919 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000920 Ops[0].init(val, this);
921 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000922 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000923 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000924 AssertOK();
925}
926
927StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000928 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000929 Ops[0].init(val, this);
930 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000931 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000932 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000933 AssertOK();
934}
935
Misha Brukmanb1c93172005-04-21 23:48:37 +0000936StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000937 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000938 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000939 Ops[0].init(val, this);
940 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000941 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000942 setAlignment(0);
943 AssertOK();
944}
945
946StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
947 unsigned Align, Instruction *InsertBefore)
948 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
949 Ops[0].init(val, this);
950 Ops[1].init(addr, this);
951 setVolatile(isVolatile);
952 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000953 AssertOK();
954}
955
Misha Brukmanb1c93172005-04-21 23:48:37 +0000956StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000957 unsigned Align, BasicBlock *InsertAtEnd)
958 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
959 Ops[0].init(val, this);
960 Ops[1].init(addr, this);
961 setVolatile(isVolatile);
962 setAlignment(Align);
963 AssertOK();
964}
965
966StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000967 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000968 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000969 Ops[0].init(val, this);
970 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000971 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000972 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000973 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000974}
975
Christopher Lamb84485702007-04-22 19:24:39 +0000976void StoreInst::setAlignment(unsigned Align) {
977 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
978 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
979}
980
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000981//===----------------------------------------------------------------------===//
982// GetElementPtrInst Implementation
983//===----------------------------------------------------------------------===//
984
Christopher Lambedf07882007-12-17 01:12:55 +0000985static unsigned retrieveAddrSpace(const Value *Val) {
986 return cast<PointerType>(Val->getType())->getAddressSpace();
987}
988
Chris Lattner79807c3d2007-01-31 19:47:18 +0000989void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
990 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000991 Use *OL = OperandList = new Use[NumOperands];
992 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000993
Chris Lattner79807c3d2007-01-31 19:47:18 +0000994 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000995 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000996}
997
Chris Lattner82981202005-05-03 05:43:30 +0000998void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
999 NumOperands = 2;
1000 Use *OL = OperandList = new Use[2];
1001 OL[0].init(Ptr, this);
1002 OL[1].init(Idx, this);
1003}
1004
Chris Lattner82981202005-05-03 05:43:30 +00001005GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1006 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001007 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001008 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001009 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +00001010 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001011 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001012}
1013
1014GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1015 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001016 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001017 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001018 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +00001019 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001020 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001021}
1022
Gordon Henriksen14a55692007-12-10 02:14:30 +00001023GetElementPtrInst::~GetElementPtrInst() {
1024 delete[] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001025}
1026
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001027// getIndexedType - Returns the type of the element that would be loaded with
1028// a load instruction with the specified parameters.
1029//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001030// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001031// pointer type.
1032//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001033const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001034 Value* const *Idxs,
1035 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001036 bool AllowCompositeLeaf) {
1037 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
1038
1039 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +00001040 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001041 if (AllowCompositeLeaf ||
1042 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
1043 return cast<PointerType>(Ptr)->getElementType();
1044 else
1045 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001046
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001047 unsigned CurIdx = 0;
1048 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001049 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001050 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
1051 return 0; // Can't load a whole structure or array!?!?
1052 }
1053
Chris Lattner302116a2007-01-31 04:40:28 +00001054 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001055 if (isa<PointerType>(CT) && CurIdx != 1)
1056 return 0; // Can only index into pointer types at the first index!
1057 if (!CT->indexValid(Index)) return 0;
1058 Ptr = CT->getTypeAtIndex(Index);
1059
1060 // If the new type forwards to another type, then it is in the middle
1061 // of being refined to another type (and hence, may have dropped all
1062 // references to what it was using before). So, use the new forwarded
1063 // type.
1064 if (const Type * Ty = Ptr->getForwardedType()) {
1065 Ptr = Ty;
1066 }
1067 }
Chris Lattner302116a2007-01-31 04:40:28 +00001068 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001069}
1070
Chris Lattner82981202005-05-03 05:43:30 +00001071const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1072 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1073 if (!PTy) return 0; // Type isn't a pointer type!
1074
1075 // Check the pointer index.
1076 if (!PTy->indexValid(Idx)) return 0;
1077
Chris Lattnerc2233332005-05-03 16:44:45 +00001078 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001079}
1080
Chris Lattner45f15572007-04-14 00:12:57 +00001081
1082/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1083/// zeros. If so, the result pointer and the first operand have the same
1084/// value, just potentially different types.
1085bool GetElementPtrInst::hasAllZeroIndices() const {
1086 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1087 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1088 if (!CI->isZero()) return false;
1089 } else {
1090 return false;
1091 }
1092 }
1093 return true;
1094}
1095
Chris Lattner27058292007-04-27 20:35:56 +00001096/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1097/// constant integers. If so, the result pointer and the first operand have
1098/// a constant offset between them.
1099bool GetElementPtrInst::hasAllConstantIndices() const {
1100 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1101 if (!isa<ConstantInt>(getOperand(i)))
1102 return false;
1103 }
1104 return true;
1105}
1106
Chris Lattner45f15572007-04-14 00:12:57 +00001107
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001108//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001109// ExtractElementInst Implementation
1110//===----------------------------------------------------------------------===//
1111
1112ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001113 const std::string &Name,
1114 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001115 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001116 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001117 assert(isValidOperands(Val, Index) &&
1118 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001119 Ops[0].init(Val, this);
1120 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001121 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001122}
1123
Chris Lattner65511ff2006-10-05 06:24:58 +00001124ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1125 const std::string &Name,
1126 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001127 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001128 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001129 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001130 assert(isValidOperands(Val, Index) &&
1131 "Invalid extractelement instruction operands!");
1132 Ops[0].init(Val, this);
1133 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001134 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001135}
1136
1137
Robert Bocchino23004482006-01-10 19:05:34 +00001138ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001139 const std::string &Name,
1140 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001141 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001142 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001143 assert(isValidOperands(Val, Index) &&
1144 "Invalid extractelement instruction operands!");
1145
Robert Bocchino23004482006-01-10 19:05:34 +00001146 Ops[0].init(Val, this);
1147 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001148 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001149}
1150
Chris Lattner65511ff2006-10-05 06:24:58 +00001151ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1152 const std::string &Name,
1153 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001154 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001155 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001156 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001157 assert(isValidOperands(Val, Index) &&
1158 "Invalid extractelement instruction operands!");
1159
1160 Ops[0].init(Val, this);
1161 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001162 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001163}
1164
1165
Chris Lattner54865b32006-04-08 04:05:48 +00001166bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001167 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001168 return false;
1169 return true;
1170}
1171
1172
Robert Bocchino23004482006-01-10 19:05:34 +00001173//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001174// InsertElementInst Implementation
1175//===----------------------------------------------------------------------===//
1176
Chris Lattner0875d942006-04-14 22:20:32 +00001177InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1178 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1179 Ops[0].init(IE.Ops[0], this);
1180 Ops[1].init(IE.Ops[1], this);
1181 Ops[2].init(IE.Ops[2], this);
1182}
Chris Lattner54865b32006-04-08 04:05:48 +00001183InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001184 const std::string &Name,
1185 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001186 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001187 assert(isValidOperands(Vec, Elt, Index) &&
1188 "Invalid insertelement instruction operands!");
1189 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001190 Ops[1].init(Elt, this);
1191 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001192 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001193}
1194
Chris Lattner65511ff2006-10-05 06:24:58 +00001195InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1196 const std::string &Name,
1197 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001198 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
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(Vec, Elt, Index) &&
1201 "Invalid insertelement instruction operands!");
1202 Ops[0].init(Vec, this);
1203 Ops[1].init(Elt, this);
1204 Ops[2].init(Index, this);
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 +00001209InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001210 const std::string &Name,
1211 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001212 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001213 assert(isValidOperands(Vec, Elt, Index) &&
1214 "Invalid insertelement instruction operands!");
1215
1216 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001217 Ops[1].init(Elt, this);
1218 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001219 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001220}
1221
Chris Lattner65511ff2006-10-05 06:24:58 +00001222InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1223 const std::string &Name,
1224 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001225: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001226 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001227 assert(isValidOperands(Vec, Elt, Index) &&
1228 "Invalid insertelement instruction operands!");
1229
1230 Ops[0].init(Vec, this);
1231 Ops[1].init(Elt, this);
1232 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001233 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001234}
1235
Chris Lattner54865b32006-04-08 04:05:48 +00001236bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1237 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001238 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001239 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001240
Reid Spencerd84d35b2007-02-15 02:26:10 +00001241 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001242 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001243
Reid Spencer8d9336d2006-12-31 05:26:44 +00001244 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001245 return false; // Third operand of insertelement must be uint.
1246 return true;
1247}
1248
1249
Robert Bocchinoca27f032006-01-17 20:07:22 +00001250//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001251// ShuffleVectorInst Implementation
1252//===----------------------------------------------------------------------===//
1253
Chris Lattner0875d942006-04-14 22:20:32 +00001254ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1255 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1256 Ops[0].init(SV.Ops[0], this);
1257 Ops[1].init(SV.Ops[1], this);
1258 Ops[2].init(SV.Ops[2], this);
1259}
1260
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001261ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1262 const std::string &Name,
1263 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001264 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001265 assert(isValidOperands(V1, V2, Mask) &&
1266 "Invalid shuffle vector instruction operands!");
1267 Ops[0].init(V1, this);
1268 Ops[1].init(V2, this);
1269 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001270 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001271}
1272
1273ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1274 const std::string &Name,
1275 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001276 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001277 assert(isValidOperands(V1, V2, Mask) &&
1278 "Invalid shuffle vector instruction operands!");
1279
1280 Ops[0].init(V1, this);
1281 Ops[1].init(V2, this);
1282 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001283 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001284}
1285
1286bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1287 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001288 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001289 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001290 if (!isa<VectorType>(Mask->getType()) ||
1291 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1292 cast<VectorType>(Mask->getType())->getNumElements() !=
1293 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001294 return false;
1295 return true;
1296}
1297
1298
1299//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001300// BinaryOperator Class
1301//===----------------------------------------------------------------------===//
1302
Chris Lattner2195fc42007-02-24 00:55:48 +00001303BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1304 const Type *Ty, const std::string &Name,
1305 Instruction *InsertBefore)
1306 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1307 Ops[0].init(S1, this);
1308 Ops[1].init(S2, this);
1309 init(iType);
1310 setName(Name);
1311}
1312
1313BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1314 const Type *Ty, const std::string &Name,
1315 BasicBlock *InsertAtEnd)
1316 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1317 Ops[0].init(S1, this);
1318 Ops[1].init(S2, this);
1319 init(iType);
1320 setName(Name);
1321}
1322
1323
1324void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001325 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001326 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001327 assert(LHS->getType() == RHS->getType() &&
1328 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001329#ifndef NDEBUG
1330 switch (iType) {
1331 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001332 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001333 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001334 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001335 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001336 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001337 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001338 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001339 case UDiv:
1340 case SDiv:
1341 assert(getType() == LHS->getType() &&
1342 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001343 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1344 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001345 "Incorrect operand type (not integer) for S/UDIV");
1346 break;
1347 case FDiv:
1348 assert(getType() == LHS->getType() &&
1349 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001350 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1351 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001352 && "Incorrect operand type (not floating point) for FDIV");
1353 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001354 case URem:
1355 case SRem:
1356 assert(getType() == LHS->getType() &&
1357 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001358 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1359 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001360 "Incorrect operand type (not integer) for S/UREM");
1361 break;
1362 case FRem:
1363 assert(getType() == LHS->getType() &&
1364 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001365 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1366 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001367 && "Incorrect operand type (not floating point) for FREM");
1368 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001369 case Shl:
1370 case LShr:
1371 case AShr:
1372 assert(getType() == LHS->getType() &&
1373 "Shift operation should return same type as operands!");
1374 assert(getType()->isInteger() &&
1375 "Shift operation requires integer operands");
1376 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001377 case And: case Or:
1378 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001379 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001380 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001381 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001382 (isa<VectorType>(getType()) &&
1383 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001384 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001385 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001386 default:
1387 break;
1388 }
1389#endif
1390}
1391
1392BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001393 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001394 Instruction *InsertBefore) {
1395 assert(S1->getType() == S2->getType() &&
1396 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001397 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001398}
1399
1400BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001401 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001402 BasicBlock *InsertAtEnd) {
1403 BinaryOperator *Res = create(Op, S1, S2, Name);
1404 InsertAtEnd->getInstList().push_back(Res);
1405 return Res;
1406}
1407
1408BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1409 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001410 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1411 return new BinaryOperator(Instruction::Sub,
1412 zero, Op,
1413 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001414}
1415
1416BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1417 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001418 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1419 return new BinaryOperator(Instruction::Sub,
1420 zero, Op,
1421 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001422}
1423
1424BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1425 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001426 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001427 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001428 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001429 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001430 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001431 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001432 }
1433
1434 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001435 Op->getType(), Name, InsertBefore);
1436}
1437
1438BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1439 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001440 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001441 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001442 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001443 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001444 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001445 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001446 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001447 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001448 }
1449
1450 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001451 Op->getType(), Name, InsertAtEnd);
1452}
1453
1454
1455// isConstantAllOnes - Helper function for several functions below
1456static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001457 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1458 return CI->isAllOnesValue();
1459 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1460 return CV->isAllOnesValue();
1461 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001462}
1463
1464bool BinaryOperator::isNeg(const Value *V) {
1465 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1466 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001467 return Bop->getOperand(0) ==
1468 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001469 return false;
1470}
1471
1472bool BinaryOperator::isNot(const Value *V) {
1473 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1474 return (Bop->getOpcode() == Instruction::Xor &&
1475 (isConstantAllOnes(Bop->getOperand(1)) ||
1476 isConstantAllOnes(Bop->getOperand(0))));
1477 return false;
1478}
1479
Chris Lattner2c7d1772005-04-24 07:28:37 +00001480Value *BinaryOperator::getNegArgument(Value *BinOp) {
1481 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1482 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001483}
1484
Chris Lattner2c7d1772005-04-24 07:28:37 +00001485const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1486 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001487}
1488
Chris Lattner2c7d1772005-04-24 07:28:37 +00001489Value *BinaryOperator::getNotArgument(Value *BinOp) {
1490 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1491 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1492 Value *Op0 = BO->getOperand(0);
1493 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001494 if (isConstantAllOnes(Op0)) return Op1;
1495
1496 assert(isConstantAllOnes(Op1));
1497 return Op0;
1498}
1499
Chris Lattner2c7d1772005-04-24 07:28:37 +00001500const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1501 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001502}
1503
1504
1505// swapOperands - Exchange the two operands to this instruction. This
1506// instruction is safe to use on any binary instruction and does not
1507// modify the semantics of the instruction. If the instruction is
1508// order dependent (SetLT f.e.) the opcode is changed.
1509//
1510bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001511 if (!isCommutative())
1512 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001513 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001514 return false;
1515}
1516
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001517//===----------------------------------------------------------------------===//
1518// CastInst Class
1519//===----------------------------------------------------------------------===//
1520
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001521// Just determine if this cast only deals with integral->integral conversion.
1522bool CastInst::isIntegerCast() const {
1523 switch (getOpcode()) {
1524 default: return false;
1525 case Instruction::ZExt:
1526 case Instruction::SExt:
1527 case Instruction::Trunc:
1528 return true;
1529 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001530 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001531 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001532}
1533
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001534bool CastInst::isLosslessCast() const {
1535 // Only BitCast can be lossless, exit fast if we're not BitCast
1536 if (getOpcode() != Instruction::BitCast)
1537 return false;
1538
1539 // Identity cast is always lossless
1540 const Type* SrcTy = getOperand(0)->getType();
1541 const Type* DstTy = getType();
1542 if (SrcTy == DstTy)
1543 return true;
1544
Reid Spencer8d9336d2006-12-31 05:26:44 +00001545 // Pointer to pointer is always lossless.
1546 if (isa<PointerType>(SrcTy))
1547 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001548 return false; // Other types have no identity values
1549}
1550
1551/// This function determines if the CastInst does not require any bits to be
1552/// changed in order to effect the cast. Essentially, it identifies cases where
1553/// no code gen is necessary for the cast, hence the name no-op cast. For
1554/// example, the following are all no-op casts:
1555/// # bitcast uint %X, int
1556/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001557/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001558/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1559/// @brief Determine if a cast is a no-op.
1560bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1561 switch (getOpcode()) {
1562 default:
1563 assert(!"Invalid CastOp");
1564 case Instruction::Trunc:
1565 case Instruction::ZExt:
1566 case Instruction::SExt:
1567 case Instruction::FPTrunc:
1568 case Instruction::FPExt:
1569 case Instruction::UIToFP:
1570 case Instruction::SIToFP:
1571 case Instruction::FPToUI:
1572 case Instruction::FPToSI:
1573 return false; // These always modify bits
1574 case Instruction::BitCast:
1575 return true; // BitCast never modifies bits.
1576 case Instruction::PtrToInt:
1577 return IntPtrTy->getPrimitiveSizeInBits() ==
1578 getType()->getPrimitiveSizeInBits();
1579 case Instruction::IntToPtr:
1580 return IntPtrTy->getPrimitiveSizeInBits() ==
1581 getOperand(0)->getType()->getPrimitiveSizeInBits();
1582 }
1583}
1584
1585/// This function determines if a pair of casts can be eliminated and what
1586/// opcode should be used in the elimination. This assumes that there are two
1587/// instructions like this:
1588/// * %F = firstOpcode SrcTy %x to MidTy
1589/// * %S = secondOpcode MidTy %F to DstTy
1590/// The function returns a resultOpcode so these two casts can be replaced with:
1591/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1592/// If no such cast is permited, the function returns 0.
1593unsigned CastInst::isEliminableCastPair(
1594 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1595 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1596{
1597 // Define the 144 possibilities for these two cast instructions. The values
1598 // in this matrix determine what to do in a given situation and select the
1599 // case in the switch below. The rows correspond to firstOp, the columns
1600 // correspond to secondOp. In looking at the table below, keep in mind
1601 // the following cast properties:
1602 //
1603 // Size Compare Source Destination
1604 // Operator Src ? Size Type Sign Type Sign
1605 // -------- ------------ ------------------- ---------------------
1606 // TRUNC > Integer Any Integral Any
1607 // ZEXT < Integral Unsigned Integer Any
1608 // SEXT < Integral Signed Integer Any
1609 // FPTOUI n/a FloatPt n/a Integral Unsigned
1610 // FPTOSI n/a FloatPt n/a Integral Signed
1611 // UITOFP n/a Integral Unsigned FloatPt n/a
1612 // SITOFP n/a Integral Signed FloatPt n/a
1613 // FPTRUNC > FloatPt n/a FloatPt n/a
1614 // FPEXT < FloatPt n/a FloatPt n/a
1615 // PTRTOINT n/a Pointer n/a Integral Unsigned
1616 // INTTOPTR n/a Integral Unsigned Pointer n/a
1617 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001618 //
1619 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1620 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1621 // into "fptoui double to ulong", but this loses information about the range
1622 // of the produced value (we no longer know the top-part is all zeros).
1623 // Further this conversion is often much more expensive for typical hardware,
1624 // and causes issues when building libgcc. We disallow fptosi+sext for the
1625 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001626 const unsigned numCastOps =
1627 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1628 static const uint8_t CastResults[numCastOps][numCastOps] = {
1629 // T F F U S F F P I B -+
1630 // R Z S P P I I T P 2 N T |
1631 // U E E 2 2 2 2 R E I T C +- secondOp
1632 // N X X U S F F N X N 2 V |
1633 // C T T I I P P C T T P T -+
1634 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1635 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1636 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001637 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1638 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001639 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1640 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1641 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1642 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1643 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1644 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1645 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1646 };
1647
1648 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1649 [secondOp-Instruction::CastOpsBegin];
1650 switch (ElimCase) {
1651 case 0:
1652 // categorically disallowed
1653 return 0;
1654 case 1:
1655 // allowed, use first cast's opcode
1656 return firstOp;
1657 case 2:
1658 // allowed, use second cast's opcode
1659 return secondOp;
1660 case 3:
1661 // no-op cast in second op implies firstOp as long as the DestTy
1662 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001663 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001664 return firstOp;
1665 return 0;
1666 case 4:
1667 // no-op cast in second op implies firstOp as long as the DestTy
1668 // is floating point
1669 if (DstTy->isFloatingPoint())
1670 return firstOp;
1671 return 0;
1672 case 5:
1673 // no-op cast in first op implies secondOp as long as the SrcTy
1674 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001675 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001676 return secondOp;
1677 return 0;
1678 case 6:
1679 // no-op cast in first op implies secondOp as long as the SrcTy
1680 // is a floating point
1681 if (SrcTy->isFloatingPoint())
1682 return secondOp;
1683 return 0;
1684 case 7: {
1685 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1686 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1687 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1688 if (MidSize >= PtrSize)
1689 return Instruction::BitCast;
1690 return 0;
1691 }
1692 case 8: {
1693 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1694 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1695 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1696 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1697 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1698 if (SrcSize == DstSize)
1699 return Instruction::BitCast;
1700 else if (SrcSize < DstSize)
1701 return firstOp;
1702 return secondOp;
1703 }
1704 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1705 return Instruction::ZExt;
1706 case 10:
1707 // fpext followed by ftrunc is allowed if the bit size returned to is
1708 // the same as the original, in which case its just a bitcast
1709 if (SrcTy == DstTy)
1710 return Instruction::BitCast;
1711 return 0; // If the types are not the same we can't eliminate it.
1712 case 11:
1713 // bitcast followed by ptrtoint is allowed as long as the bitcast
1714 // is a pointer to pointer cast.
1715 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1716 return secondOp;
1717 return 0;
1718 case 12:
1719 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1720 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1721 return firstOp;
1722 return 0;
1723 case 13: {
1724 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1725 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1726 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1727 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1728 if (SrcSize <= PtrSize && SrcSize == DstSize)
1729 return Instruction::BitCast;
1730 return 0;
1731 }
1732 case 99:
1733 // cast combination can't happen (error in input). This is for all cases
1734 // where the MidTy is not the same for the two cast instructions.
1735 assert(!"Invalid Cast Combination");
1736 return 0;
1737 default:
1738 assert(!"Error in CastResults table!!!");
1739 return 0;
1740 }
1741 return 0;
1742}
1743
1744CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1745 const std::string &Name, Instruction *InsertBefore) {
1746 // Construct and return the appropriate CastInst subclass
1747 switch (op) {
1748 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1749 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1750 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1751 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1752 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1753 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1754 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1755 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1756 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1757 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1758 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1759 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1760 default:
1761 assert(!"Invalid opcode provided");
1762 }
1763 return 0;
1764}
1765
1766CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1767 const std::string &Name, BasicBlock *InsertAtEnd) {
1768 // Construct and return the appropriate CastInst subclass
1769 switch (op) {
1770 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1771 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1772 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1773 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1774 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1775 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1776 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1777 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1778 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1779 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1780 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1781 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1782 default:
1783 assert(!"Invalid opcode provided");
1784 }
1785 return 0;
1786}
1787
Reid Spencer5c140882006-12-04 20:17:56 +00001788CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1789 const std::string &Name,
1790 Instruction *InsertBefore) {
1791 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1792 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1793 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1794}
1795
1796CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1797 const std::string &Name,
1798 BasicBlock *InsertAtEnd) {
1799 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1800 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1801 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1802}
1803
1804CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1805 const std::string &Name,
1806 Instruction *InsertBefore) {
1807 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1808 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1809 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1810}
1811
1812CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1813 const std::string &Name,
1814 BasicBlock *InsertAtEnd) {
1815 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1816 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1817 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1818}
1819
1820CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1821 const std::string &Name,
1822 Instruction *InsertBefore) {
1823 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1824 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1825 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1826}
1827
1828CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1829 const std::string &Name,
1830 BasicBlock *InsertAtEnd) {
1831 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1832 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1833 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1834}
1835
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001836CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1837 const std::string &Name,
1838 BasicBlock *InsertAtEnd) {
1839 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001840 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001841 "Invalid cast");
1842
Chris Lattner03c49532007-01-15 02:27:26 +00001843 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001844 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1845 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1846}
1847
1848/// @brief Create a BitCast or a PtrToInt cast instruction
1849CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1850 const std::string &Name,
1851 Instruction *InsertBefore) {
1852 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001853 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001854 "Invalid cast");
1855
Chris Lattner03c49532007-01-15 02:27:26 +00001856 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001857 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1858 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1859}
1860
Reid Spencer7e933472006-12-12 00:49:44 +00001861CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1862 bool isSigned, const std::string &Name,
1863 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001864 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001865 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1866 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1867 Instruction::CastOps opcode =
1868 (SrcBits == DstBits ? Instruction::BitCast :
1869 (SrcBits > DstBits ? Instruction::Trunc :
1870 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1871 return create(opcode, C, Ty, Name, InsertBefore);
1872}
1873
1874CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1875 bool isSigned, const std::string &Name,
1876 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001877 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001878 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1879 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1880 Instruction::CastOps opcode =
1881 (SrcBits == DstBits ? Instruction::BitCast :
1882 (SrcBits > DstBits ? Instruction::Trunc :
1883 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1884 return create(opcode, C, Ty, Name, InsertAtEnd);
1885}
1886
1887CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1888 const std::string &Name,
1889 Instruction *InsertBefore) {
1890 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1891 "Invalid cast");
1892 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1893 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1894 Instruction::CastOps opcode =
1895 (SrcBits == DstBits ? Instruction::BitCast :
1896 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1897 return create(opcode, C, Ty, Name, InsertBefore);
1898}
1899
1900CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1901 const std::string &Name,
1902 BasicBlock *InsertAtEnd) {
1903 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1904 "Invalid cast");
1905 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1906 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1907 Instruction::CastOps opcode =
1908 (SrcBits == DstBits ? Instruction::BitCast :
1909 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1910 return create(opcode, C, Ty, Name, InsertAtEnd);
1911}
1912
Duncan Sands55e50902008-01-06 10:12:28 +00001913// Check whether it is valid to call getCastOpcode for these types.
1914// This routine must be kept in sync with getCastOpcode.
1915bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
1916 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
1917 return false;
1918
1919 if (SrcTy == DestTy)
1920 return true;
1921
1922 // Get the bit sizes, we'll need these
1923 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1924 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1925
1926 // Run through the possibilities ...
1927 if (DestTy->isInteger()) { // Casting to integral
1928 if (SrcTy->isInteger()) { // Casting from integral
1929 return true;
1930 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1931 return true;
1932 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1933 // Casting from vector
1934 return DestBits == PTy->getBitWidth();
1935 } else { // Casting from something else
1936 return isa<PointerType>(SrcTy);
1937 }
1938 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
1939 if (SrcTy->isInteger()) { // Casting from integral
1940 return true;
1941 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1942 return true;
1943 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1944 // Casting from vector
1945 return DestBits == PTy->getBitWidth();
1946 } else { // Casting from something else
1947 return false;
1948 }
1949 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1950 // Casting to vector
1951 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
1952 // Casting from vector
1953 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
1954 } else { // Casting from something else
1955 return DestPTy->getBitWidth() == SrcBits;
1956 }
1957 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
1958 if (isa<PointerType>(SrcTy)) { // Casting from pointer
1959 return true;
1960 } else if (SrcTy->isInteger()) { // Casting from integral
1961 return true;
1962 } else { // Casting from something else
1963 return false;
1964 }
1965 } else { // Casting to something else
1966 return false;
1967 }
1968}
1969
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001970// Provide a way to get a "cast" where the cast opcode is inferred from the
1971// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001972// logic in the castIsValid function below. This axiom should hold:
1973// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1974// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001975// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00001976// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001977Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001978CastInst::getCastOpcode(
1979 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001980 // Get the bit sizes, we'll need these
1981 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00001982 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1983 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001984
Duncan Sands55e50902008-01-06 10:12:28 +00001985 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
1986 "Only first class types are castable!");
1987
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001988 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001989 if (DestTy->isInteger()) { // Casting to integral
1990 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001991 if (DestBits < SrcBits)
1992 return Trunc; // int -> smaller int
1993 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001994 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001995 return SExt; // signed -> SEXT
1996 else
1997 return ZExt; // unsigned -> ZEXT
1998 } else {
1999 return BitCast; // Same size, No-op cast
2000 }
2001 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002002 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002003 return FPToSI; // FP -> sint
2004 else
2005 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002006 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002007 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002008 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002009 return BitCast; // Same size, no-op cast
2010 } else {
2011 assert(isa<PointerType>(SrcTy) &&
2012 "Casting from a value that is not first-class type");
2013 return PtrToInt; // ptr -> int
2014 }
2015 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002016 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002017 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002018 return SIToFP; // sint -> FP
2019 else
2020 return UIToFP; // uint -> FP
2021 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2022 if (DestBits < SrcBits) {
2023 return FPTrunc; // FP -> smaller FP
2024 } else if (DestBits > SrcBits) {
2025 return FPExt; // FP -> larger FP
2026 } else {
2027 return BitCast; // same size, no-op cast
2028 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002029 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002030 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002031 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002032 return BitCast; // same size, no-op cast
2033 } else {
2034 assert(0 && "Casting pointer or non-first class to float");
2035 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002036 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2037 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002038 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002039 "Casting vector to vector of different widths");
2040 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002041 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002042 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002043 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002044 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002045 }
2046 } else if (isa<PointerType>(DestTy)) {
2047 if (isa<PointerType>(SrcTy)) {
2048 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002049 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002050 return IntToPtr; // int -> ptr
2051 } else {
2052 assert(!"Casting pointer to other than pointer or int");
2053 }
2054 } else {
2055 assert(!"Casting to type that is not first-class");
2056 }
2057
2058 // If we fall through to here we probably hit an assertion cast above
2059 // and assertions are not turned on. Anything we return is an error, so
2060 // BitCast is as good a choice as any.
2061 return BitCast;
2062}
2063
2064//===----------------------------------------------------------------------===//
2065// CastInst SubClass Constructors
2066//===----------------------------------------------------------------------===//
2067
2068/// Check that the construction parameters for a CastInst are correct. This
2069/// could be broken out into the separate constructors but it is useful to have
2070/// it in one place and to eliminate the redundant code for getting the sizes
2071/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002072bool
2073CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002074
2075 // Check for type sanity on the arguments
2076 const Type *SrcTy = S->getType();
2077 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2078 return false;
2079
2080 // Get the size of the types in bits, we'll need this later
2081 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2082 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2083
2084 // Switch on the opcode provided
2085 switch (op) {
2086 default: return false; // This is an input error
2087 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002088 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002089 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002090 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002091 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002092 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002093 case Instruction::FPTrunc:
2094 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2095 SrcBitSize > DstBitSize;
2096 case Instruction::FPExt:
2097 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2098 SrcBitSize < DstBitSize;
2099 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002100 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002101 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2102 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2103 return SVTy->getElementType()->isInteger() &&
2104 DVTy->getElementType()->isFloatingPoint() &&
2105 SVTy->getNumElements() == DVTy->getNumElements();
2106 }
2107 }
Chris Lattner03c49532007-01-15 02:27:26 +00002108 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002109 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002110 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002111 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2112 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2113 return SVTy->getElementType()->isFloatingPoint() &&
2114 DVTy->getElementType()->isInteger() &&
2115 SVTy->getNumElements() == DVTy->getNumElements();
2116 }
2117 }
Chris Lattner03c49532007-01-15 02:27:26 +00002118 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002119 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002120 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002121 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002122 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002123 case Instruction::BitCast:
2124 // BitCast implies a no-op cast of type only. No bits change.
2125 // However, you can't cast pointers to anything but pointers.
2126 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2127 return false;
2128
Duncan Sands55e50902008-01-06 10:12:28 +00002129 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002130 // these cases, the cast is okay if the source and destination bit widths
2131 // are identical.
2132 return SrcBitSize == DstBitSize;
2133 }
2134}
2135
2136TruncInst::TruncInst(
2137 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2138) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002139 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002140}
2141
2142TruncInst::TruncInst(
2143 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2144) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002145 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002146}
2147
2148ZExtInst::ZExtInst(
2149 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2150) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002151 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002152}
2153
2154ZExtInst::ZExtInst(
2155 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2156) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002157 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002158}
2159SExtInst::SExtInst(
2160 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2161) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002162 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002163}
2164
Jeff Cohencc08c832006-12-02 02:22:01 +00002165SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002166 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2167) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002168 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002169}
2170
2171FPTruncInst::FPTruncInst(
2172 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2173) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002174 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002175}
2176
2177FPTruncInst::FPTruncInst(
2178 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2179) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002180 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002181}
2182
2183FPExtInst::FPExtInst(
2184 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2185) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002186 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002187}
2188
2189FPExtInst::FPExtInst(
2190 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2191) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002192 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002193}
2194
2195UIToFPInst::UIToFPInst(
2196 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2197) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002198 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002199}
2200
2201UIToFPInst::UIToFPInst(
2202 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2203) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002204 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002205}
2206
2207SIToFPInst::SIToFPInst(
2208 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2209) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002210 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002211}
2212
2213SIToFPInst::SIToFPInst(
2214 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2215) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002216 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002217}
2218
2219FPToUIInst::FPToUIInst(
2220 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2221) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002222 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002223}
2224
2225FPToUIInst::FPToUIInst(
2226 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2227) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002228 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002229}
2230
2231FPToSIInst::FPToSIInst(
2232 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2233) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002234 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002235}
2236
2237FPToSIInst::FPToSIInst(
2238 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2239) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002240 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002241}
2242
2243PtrToIntInst::PtrToIntInst(
2244 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2245) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002246 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002247}
2248
2249PtrToIntInst::PtrToIntInst(
2250 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2251) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002252 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002253}
2254
2255IntToPtrInst::IntToPtrInst(
2256 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2257) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002258 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002259}
2260
2261IntToPtrInst::IntToPtrInst(
2262 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2263) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002264 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002265}
2266
2267BitCastInst::BitCastInst(
2268 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2269) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002270 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002271}
2272
2273BitCastInst::BitCastInst(
2274 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2275) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002276 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002277}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002278
2279//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002280// CmpInst Classes
2281//===----------------------------------------------------------------------===//
2282
2283CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2284 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002285 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002286 Ops[0].init(LHS, this);
2287 Ops[1].init(RHS, this);
2288 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002289 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002290 if (op == Instruction::ICmp) {
2291 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2292 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2293 "Invalid ICmp predicate value");
2294 const Type* Op0Ty = getOperand(0)->getType();
2295 const Type* Op1Ty = getOperand(1)->getType();
2296 assert(Op0Ty == Op1Ty &&
2297 "Both operands to ICmp instruction are not of the same type!");
2298 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002299 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002300 "Invalid operand types for ICmp instruction");
2301 return;
2302 }
2303 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2304 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2305 "Invalid FCmp predicate value");
2306 const Type* Op0Ty = getOperand(0)->getType();
2307 const Type* Op1Ty = getOperand(1)->getType();
2308 assert(Op0Ty == Op1Ty &&
2309 "Both operands to FCmp instruction are not of the same type!");
2310 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002311 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002312 "Invalid operand types for FCmp instruction");
2313}
2314
2315CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2316 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002317 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002318 Ops[0].init(LHS, this);
2319 Ops[1].init(RHS, this);
2320 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002321 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002322 if (op == Instruction::ICmp) {
2323 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2324 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2325 "Invalid ICmp predicate value");
2326
2327 const Type* Op0Ty = getOperand(0)->getType();
2328 const Type* Op1Ty = getOperand(1)->getType();
2329 assert(Op0Ty == Op1Ty &&
2330 "Both operands to ICmp instruction are not of the same type!");
2331 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002332 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002333 "Invalid operand types for ICmp instruction");
2334 return;
2335 }
2336 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2337 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2338 "Invalid FCmp predicate value");
2339 const Type* Op0Ty = getOperand(0)->getType();
2340 const Type* Op1Ty = getOperand(1)->getType();
2341 assert(Op0Ty == Op1Ty &&
2342 "Both operands to FCmp instruction are not of the same type!");
2343 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002344 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002345 "Invalid operand types for FCmp instruction");
2346}
2347
2348CmpInst *
2349CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2350 const std::string &Name, Instruction *InsertBefore) {
2351 if (Op == Instruction::ICmp) {
2352 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2353 InsertBefore);
2354 }
2355 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2356 InsertBefore);
2357}
2358
2359CmpInst *
2360CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2361 const std::string &Name, BasicBlock *InsertAtEnd) {
2362 if (Op == Instruction::ICmp) {
2363 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2364 InsertAtEnd);
2365 }
2366 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2367 InsertAtEnd);
2368}
2369
2370void CmpInst::swapOperands() {
2371 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2372 IC->swapOperands();
2373 else
2374 cast<FCmpInst>(this)->swapOperands();
2375}
2376
2377bool CmpInst::isCommutative() {
2378 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2379 return IC->isCommutative();
2380 return cast<FCmpInst>(this)->isCommutative();
2381}
2382
2383bool CmpInst::isEquality() {
2384 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2385 return IC->isEquality();
2386 return cast<FCmpInst>(this)->isEquality();
2387}
2388
2389
2390ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2391 switch (pred) {
2392 default:
2393 assert(!"Unknown icmp predicate!");
2394 case ICMP_EQ: return ICMP_NE;
2395 case ICMP_NE: return ICMP_EQ;
2396 case ICMP_UGT: return ICMP_ULE;
2397 case ICMP_ULT: return ICMP_UGE;
2398 case ICMP_UGE: return ICMP_ULT;
2399 case ICMP_ULE: return ICMP_UGT;
2400 case ICMP_SGT: return ICMP_SLE;
2401 case ICMP_SLT: return ICMP_SGE;
2402 case ICMP_SGE: return ICMP_SLT;
2403 case ICMP_SLE: return ICMP_SGT;
2404 }
2405}
2406
2407ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2408 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002409 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002410 case ICMP_EQ: case ICMP_NE:
2411 return pred;
2412 case ICMP_SGT: return ICMP_SLT;
2413 case ICMP_SLT: return ICMP_SGT;
2414 case ICMP_SGE: return ICMP_SLE;
2415 case ICMP_SLE: return ICMP_SGE;
2416 case ICMP_UGT: return ICMP_ULT;
2417 case ICMP_ULT: return ICMP_UGT;
2418 case ICMP_UGE: return ICMP_ULE;
2419 case ICMP_ULE: return ICMP_UGE;
2420 }
2421}
2422
Reid Spencer266e42b2006-12-23 06:05:41 +00002423ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2424 switch (pred) {
2425 default: assert(! "Unknown icmp predicate!");
2426 case ICMP_EQ: case ICMP_NE:
2427 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2428 return pred;
2429 case ICMP_UGT: return ICMP_SGT;
2430 case ICMP_ULT: return ICMP_SLT;
2431 case ICMP_UGE: return ICMP_SGE;
2432 case ICMP_ULE: return ICMP_SLE;
2433 }
2434}
2435
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002436ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2437 switch (pred) {
2438 default: assert(! "Unknown icmp predicate!");
2439 case ICMP_EQ: case ICMP_NE:
2440 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2441 return pred;
2442 case ICMP_SGT: return ICMP_UGT;
2443 case ICMP_SLT: return ICMP_ULT;
2444 case ICMP_SGE: return ICMP_UGE;
2445 case ICMP_SLE: return ICMP_ULE;
2446 }
2447}
2448
Reid Spencer266e42b2006-12-23 06:05:41 +00002449bool ICmpInst::isSignedPredicate(Predicate pred) {
2450 switch (pred) {
2451 default: assert(! "Unknown icmp predicate!");
2452 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2453 return true;
2454 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2455 case ICMP_UGE: case ICMP_ULE:
2456 return false;
2457 }
2458}
2459
Reid Spencer0286bc12007-02-28 22:00:54 +00002460/// Initialize a set of values that all satisfy the condition with C.
2461///
2462ConstantRange
2463ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2464 APInt Lower(C);
2465 APInt Upper(C);
2466 uint32_t BitWidth = C.getBitWidth();
2467 switch (pred) {
2468 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2469 case ICmpInst::ICMP_EQ: Upper++; break;
2470 case ICmpInst::ICMP_NE: Lower++; break;
2471 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2472 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2473 case ICmpInst::ICMP_UGT:
2474 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2475 break;
2476 case ICmpInst::ICMP_SGT:
2477 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2478 break;
2479 case ICmpInst::ICMP_ULE:
2480 Lower = APInt::getMinValue(BitWidth); Upper++;
2481 break;
2482 case ICmpInst::ICMP_SLE:
2483 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2484 break;
2485 case ICmpInst::ICMP_UGE:
2486 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2487 break;
2488 case ICmpInst::ICMP_SGE:
2489 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2490 break;
2491 }
2492 return ConstantRange(Lower, Upper);
2493}
2494
Reid Spencerd9436b62006-11-20 01:22:35 +00002495FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2496 switch (pred) {
2497 default:
2498 assert(!"Unknown icmp predicate!");
2499 case FCMP_OEQ: return FCMP_UNE;
2500 case FCMP_ONE: return FCMP_UEQ;
2501 case FCMP_OGT: return FCMP_ULE;
2502 case FCMP_OLT: return FCMP_UGE;
2503 case FCMP_OGE: return FCMP_ULT;
2504 case FCMP_OLE: return FCMP_UGT;
2505 case FCMP_UEQ: return FCMP_ONE;
2506 case FCMP_UNE: return FCMP_OEQ;
2507 case FCMP_UGT: return FCMP_OLE;
2508 case FCMP_ULT: return FCMP_OGE;
2509 case FCMP_UGE: return FCMP_OLT;
2510 case FCMP_ULE: return FCMP_OGT;
2511 case FCMP_ORD: return FCMP_UNO;
2512 case FCMP_UNO: return FCMP_ORD;
2513 case FCMP_TRUE: return FCMP_FALSE;
2514 case FCMP_FALSE: return FCMP_TRUE;
2515 }
2516}
2517
2518FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2519 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002520 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002521 case FCMP_FALSE: case FCMP_TRUE:
2522 case FCMP_OEQ: case FCMP_ONE:
2523 case FCMP_UEQ: case FCMP_UNE:
2524 case FCMP_ORD: case FCMP_UNO:
2525 return pred;
2526 case FCMP_OGT: return FCMP_OLT;
2527 case FCMP_OLT: return FCMP_OGT;
2528 case FCMP_OGE: return FCMP_OLE;
2529 case FCMP_OLE: return FCMP_OGE;
2530 case FCMP_UGT: return FCMP_ULT;
2531 case FCMP_ULT: return FCMP_UGT;
2532 case FCMP_UGE: return FCMP_ULE;
2533 case FCMP_ULE: return FCMP_UGE;
2534 }
2535}
2536
Reid Spencer266e42b2006-12-23 06:05:41 +00002537bool CmpInst::isUnsigned(unsigned short predicate) {
2538 switch (predicate) {
2539 default: return false;
2540 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2541 case ICmpInst::ICMP_UGE: return true;
2542 }
2543}
2544
2545bool CmpInst::isSigned(unsigned short predicate){
2546 switch (predicate) {
2547 default: return false;
2548 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2549 case ICmpInst::ICMP_SGE: return true;
2550 }
2551}
2552
2553bool CmpInst::isOrdered(unsigned short predicate) {
2554 switch (predicate) {
2555 default: return false;
2556 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2557 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2558 case FCmpInst::FCMP_ORD: return true;
2559 }
2560}
2561
2562bool CmpInst::isUnordered(unsigned short predicate) {
2563 switch (predicate) {
2564 default: return false;
2565 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2566 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2567 case FCmpInst::FCMP_UNO: return true;
2568 }
2569}
2570
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002571//===----------------------------------------------------------------------===//
2572// SwitchInst Implementation
2573//===----------------------------------------------------------------------===//
2574
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002575void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002576 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002577 ReservedSpace = 2+NumCases*2;
2578 NumOperands = 2;
2579 OperandList = new Use[ReservedSpace];
2580
2581 OperandList[0].init(Value, this);
2582 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002583}
2584
Chris Lattner2195fc42007-02-24 00:55:48 +00002585/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2586/// switch on and a default destination. The number of additional cases can
2587/// be specified here to make memory allocation more efficient. This
2588/// constructor can also autoinsert before another instruction.
2589SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2590 Instruction *InsertBefore)
2591 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2592 init(Value, Default, NumCases);
2593}
2594
2595/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2596/// switch on and a default destination. The number of additional cases can
2597/// be specified here to make memory allocation more efficient. This
2598/// constructor also autoinserts at the end of the specified BasicBlock.
2599SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2600 BasicBlock *InsertAtEnd)
2601 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2602 init(Value, Default, NumCases);
2603}
2604
Misha Brukmanb1c93172005-04-21 23:48:37 +00002605SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002606 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2607 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002608 Use *OL = OperandList, *InOL = SI.OperandList;
2609 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2610 OL[i].init(InOL[i], this);
2611 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002612 }
2613}
2614
Gordon Henriksen14a55692007-12-10 02:14:30 +00002615SwitchInst::~SwitchInst() {
2616 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002617}
2618
2619
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002620/// addCase - Add an entry to the switch instruction...
2621///
Chris Lattner47ac1872005-02-24 05:32:09 +00002622void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002623 unsigned OpNo = NumOperands;
2624 if (OpNo+2 > ReservedSpace)
2625 resizeOperands(0); // Get more space!
2626 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002627 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002628 NumOperands = OpNo+2;
2629 OperandList[OpNo].init(OnVal, this);
2630 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002631}
2632
2633/// removeCase - This method removes the specified successor from the switch
2634/// instruction. Note that this cannot be used to remove the default
2635/// destination (successor #0).
2636///
2637void SwitchInst::removeCase(unsigned idx) {
2638 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002639 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2640
2641 unsigned NumOps = getNumOperands();
2642 Use *OL = OperandList;
2643
2644 // Move everything after this operand down.
2645 //
2646 // FIXME: we could just swap with the end of the list, then erase. However,
2647 // client might not expect this to happen. The code as it is thrashes the
2648 // use/def lists, which is kinda lame.
2649 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2650 OL[i-2] = OL[i];
2651 OL[i-2+1] = OL[i+1];
2652 }
2653
2654 // Nuke the last value.
2655 OL[NumOps-2].set(0);
2656 OL[NumOps-2+1].set(0);
2657 NumOperands = NumOps-2;
2658}
2659
2660/// resizeOperands - resize operands - This adjusts the length of the operands
2661/// list according to the following behavior:
2662/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2663/// of operation. This grows the number of ops by 1.5 times.
2664/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2665/// 3. If NumOps == NumOperands, trim the reserved space.
2666///
2667void SwitchInst::resizeOperands(unsigned NumOps) {
2668 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002669 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002670 } else if (NumOps*2 > NumOperands) {
2671 // No resize needed.
2672 if (ReservedSpace >= NumOps) return;
2673 } else if (NumOps == NumOperands) {
2674 if (ReservedSpace == NumOps) return;
2675 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002676 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002677 }
2678
2679 ReservedSpace = NumOps;
2680 Use *NewOps = new Use[NumOps];
2681 Use *OldOps = OperandList;
2682 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2683 NewOps[i].init(OldOps[i], this);
2684 OldOps[i].set(0);
2685 }
2686 delete [] OldOps;
2687 OperandList = NewOps;
2688}
2689
2690
2691BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2692 return getSuccessor(idx);
2693}
2694unsigned SwitchInst::getNumSuccessorsV() const {
2695 return getNumSuccessors();
2696}
2697void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2698 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002699}
Chris Lattnerf22be932004-10-15 23:52:53 +00002700
2701
2702// Define these methods here so vtables don't get emitted into every translation
2703// unit that uses these classes.
2704
2705GetElementPtrInst *GetElementPtrInst::clone() const {
2706 return new GetElementPtrInst(*this);
2707}
2708
2709BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002710 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002711}
2712
Chris Lattner0b490b02007-08-24 20:48:18 +00002713FCmpInst* FCmpInst::clone() const {
2714 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2715}
2716ICmpInst* ICmpInst::clone() const {
2717 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002718}
2719
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002720MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2721AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2722FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2723LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2724StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2725CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2726CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2727CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2728CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2729CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2730CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2731CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2732CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2733CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2734CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2735CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2736CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2737CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002738SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2739VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2740
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002741ExtractElementInst *ExtractElementInst::clone() const {
2742 return new ExtractElementInst(*this);
2743}
2744InsertElementInst *InsertElementInst::clone() const {
2745 return new InsertElementInst(*this);
2746}
2747ShuffleVectorInst *ShuffleVectorInst::clone() const {
2748 return new ShuffleVectorInst(*this);
2749}
Chris Lattnerf22be932004-10-15 23:52:53 +00002750PHINode *PHINode::clone() const { return new PHINode(*this); }
2751ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2752BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2753SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2754InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2755UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002756UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}