blob: ad99c78c1ab97c904be5b65197af0a6466f43325 [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"
20#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000021#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000022#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023using namespace llvm;
24
Chris Lattner3e13b8c2008-01-02 23:42:30 +000025//===----------------------------------------------------------------------===//
26// CallSite Class
27//===----------------------------------------------------------------------===//
28
Duncan Sands85fab3a2008-02-18 17:32:13 +000029CallSite::CallSite(Instruction *C) {
30 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
31 I = C;
32}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000033unsigned CallSite::getCallingConv() const {
34 if (CallInst *CI = dyn_cast<CallInst>(I))
35 return CI->getCallingConv();
36 else
37 return cast<InvokeInst>(I)->getCallingConv();
38}
39void CallSite::setCallingConv(unsigned CC) {
40 if (CallInst *CI = dyn_cast<CallInst>(I))
41 CI->setCallingConv(CC);
42 else
43 cast<InvokeInst>(I)->setCallingConv(CC);
44}
Chris Lattner8a923e72008-03-12 17:45:29 +000045const PAListPtr &CallSite::getParamAttrs() const {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000046 if (CallInst *CI = dyn_cast<CallInst>(I))
47 return CI->getParamAttrs();
48 else
49 return cast<InvokeInst>(I)->getParamAttrs();
50}
Chris Lattner8a923e72008-03-12 17:45:29 +000051void CallSite::setParamAttrs(const PAListPtr &PAL) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000052 if (CallInst *CI = dyn_cast<CallInst>(I))
53 CI->setParamAttrs(PAL);
54 else
55 cast<InvokeInst>(I)->setParamAttrs(PAL);
56}
Dale Johannesen89268bc2008-02-19 21:38:47 +000057bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
Duncan Sands5208d1a2007-11-28 17:07:01 +000058 if (CallInst *CI = dyn_cast<CallInst>(I))
Dale Johannesen89268bc2008-02-19 21:38:47 +000059 return CI->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000060 else
Dale Johannesen89268bc2008-02-19 21:38:47 +000061 return cast<InvokeInst>(I)->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000062}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000063uint16_t CallSite::getParamAlignment(uint16_t i) const {
64 if (CallInst *CI = dyn_cast<CallInst>(I))
65 return CI->getParamAlignment(i);
66 else
67 return cast<InvokeInst>(I)->getParamAlignment(i);
68}
69
Duncan Sands38ef3a82007-12-03 20:06:50 +000070bool CallSite::doesNotAccessMemory() const {
71 if (CallInst *CI = dyn_cast<CallInst>(I))
72 return CI->doesNotAccessMemory();
73 else
74 return cast<InvokeInst>(I)->doesNotAccessMemory();
75}
76bool CallSite::onlyReadsMemory() const {
77 if (CallInst *CI = dyn_cast<CallInst>(I))
78 return CI->onlyReadsMemory();
79 else
80 return cast<InvokeInst>(I)->onlyReadsMemory();
81}
Duncan Sands3353ed02007-12-18 09:59:50 +000082bool CallSite::doesNotThrow() const {
Duncan Sands8e4847e2007-12-16 15:51:49 +000083 if (CallInst *CI = dyn_cast<CallInst>(I))
Duncan Sands3353ed02007-12-18 09:59:50 +000084 return CI->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000085 else
Duncan Sands3353ed02007-12-18 09:59:50 +000086 return cast<InvokeInst>(I)->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000087}
Duncan Sandsaa31b922007-12-19 21:13:37 +000088void CallSite::setDoesNotThrow(bool doesNotThrow) {
89 if (CallInst *CI = dyn_cast<CallInst>(I))
90 CI->setDoesNotThrow(doesNotThrow);
91 else
92 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
93}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000094
Gordon Henriksen14a55692007-12-10 02:14:30 +000095//===----------------------------------------------------------------------===//
96// TerminatorInst Class
97//===----------------------------------------------------------------------===//
98
99// Out of line virtual method, so the vtable, etc has a home.
100TerminatorInst::~TerminatorInst() {
101}
102
103// Out of line virtual method, so the vtable, etc has a home.
104UnaryInstruction::~UnaryInstruction() {
105}
106
107
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000108//===----------------------------------------------------------------------===//
109// PHINode Class
110//===----------------------------------------------------------------------===//
111
112PHINode::PHINode(const PHINode &PN)
113 : Instruction(PN.getType(), Instruction::PHI,
114 new Use[PN.getNumOperands()], PN.getNumOperands()),
115 ReservedSpace(PN.getNumOperands()) {
116 Use *OL = OperandList;
117 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
118 OL[i].init(PN.getOperand(i), this);
119 OL[i+1].init(PN.getOperand(i+1), this);
120 }
121}
122
Gordon Henriksen14a55692007-12-10 02:14:30 +0000123PHINode::~PHINode() {
124 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000125}
126
127// removeIncomingValue - Remove an incoming value. This is useful if a
128// predecessor basic block is deleted.
129Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
130 unsigned NumOps = getNumOperands();
131 Use *OL = OperandList;
132 assert(Idx*2 < NumOps && "BB not in PHI node!");
133 Value *Removed = OL[Idx*2];
134
135 // Move everything after this operand down.
136 //
137 // FIXME: we could just swap with the end of the list, then erase. However,
138 // client might not expect this to happen. The code as it is thrashes the
139 // use/def lists, which is kinda lame.
140 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
141 OL[i-2] = OL[i];
142 OL[i-2+1] = OL[i+1];
143 }
144
145 // Nuke the last value.
146 OL[NumOps-2].set(0);
147 OL[NumOps-2+1].set(0);
148 NumOperands = NumOps-2;
149
150 // If the PHI node is dead, because it has zero entries, nuke it now.
151 if (NumOps == 2 && DeletePHIIfEmpty) {
152 // If anyone is using this PHI, make them use a dummy value instead...
153 replaceAllUsesWith(UndefValue::get(getType()));
154 eraseFromParent();
155 }
156 return Removed;
157}
158
159/// resizeOperands - resize operands - This adjusts the length of the operands
160/// list according to the following behavior:
161/// 1. If NumOps == 0, grow the operand list in response to a push_back style
162/// of operation. This grows the number of ops by 1.5 times.
163/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
164/// 3. If NumOps == NumOperands, trim the reserved space.
165///
166void PHINode::resizeOperands(unsigned NumOps) {
167 if (NumOps == 0) {
168 NumOps = (getNumOperands())*3/2;
169 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
170 } else if (NumOps*2 > NumOperands) {
171 // No resize needed.
172 if (ReservedSpace >= NumOps) return;
173 } else if (NumOps == NumOperands) {
174 if (ReservedSpace == NumOps) return;
175 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000176 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000177 }
178
179 ReservedSpace = NumOps;
180 Use *NewOps = new Use[NumOps];
181 Use *OldOps = OperandList;
182 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
183 NewOps[i].init(OldOps[i], this);
184 OldOps[i].set(0);
185 }
186 delete [] OldOps;
187 OperandList = NewOps;
188}
189
Nate Begemanb3923212005-08-04 23:24:19 +0000190/// hasConstantValue - If the specified PHI node always merges together the same
191/// value, return the value, otherwise return null.
192///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000193Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000194 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000195 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000196 if (getIncomingValue(0) != this) // not X = phi X
197 return getIncomingValue(0);
198 else
199 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000200 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000201
Nate Begemanb3923212005-08-04 23:24:19 +0000202 // Otherwise if all of the incoming values are the same for the PHI, replace
203 // the PHI node with the incoming value.
204 //
205 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000206 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000207 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000208 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000209 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000210 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000211 if (InVal && getIncomingValue(i) != InVal)
212 return 0; // Not the same, bail out.
213 else
214 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000215 }
Nate Begemanb3923212005-08-04 23:24:19 +0000216
217 // The only case that could cause InVal to be null is if we have a PHI node
218 // that only has entries for itself. In this case, there is no entry into the
219 // loop, so kill the PHI.
220 //
221 if (InVal == 0) InVal = UndefValue::get(getType());
222
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000223 // If we have a PHI node like phi(X, undef, X), where X is defined by some
224 // instruction, we cannot always return X as the result of the PHI node. Only
225 // do this if X is not an instruction (thus it must dominate the PHI block),
226 // or if the client is prepared to deal with this possibility.
227 if (HasUndefInput && !AllowNonDominatingInstruction)
228 if (Instruction *IV = dyn_cast<Instruction>(InVal))
229 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000230 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000231 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000232 return 0; // Cannot guarantee that InVal dominates this PHINode.
233
Nate Begemanb3923212005-08-04 23:24:19 +0000234 // All of the incoming values are the same, return the value now.
235 return InVal;
236}
237
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000238
239//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000240// CallInst Implementation
241//===----------------------------------------------------------------------===//
242
Gordon Henriksen14a55692007-12-10 02:14:30 +0000243CallInst::~CallInst() {
244 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000245}
246
Chris Lattner054ba2c2007-02-13 00:58:44 +0000247void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
248 NumOperands = NumParams+1;
249 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000250 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000251
Misha Brukmanb1c93172005-04-21 23:48:37 +0000252 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000254 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000255
Chris Lattner054ba2c2007-02-13 00:58:44 +0000256 assert((NumParams == FTy->getNumParams() ||
257 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000258 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000259 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000260 assert((i >= FTy->getNumParams() ||
261 FTy->getParamType(i) == Params[i]->getType()) &&
262 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000263 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000264 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265}
266
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000267void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
268 NumOperands = 3;
269 Use *OL = OperandList = new Use[3];
270 OL[0].init(Func, this);
271 OL[1].init(Actual1, this);
272 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000273
274 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000275 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000276 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000277
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000278 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000279 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000280 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000281 assert((0 >= FTy->getNumParams() ||
282 FTy->getParamType(0) == Actual1->getType()) &&
283 "Calling a function with a bad signature!");
284 assert((1 >= FTy->getNumParams() ||
285 FTy->getParamType(1) == Actual2->getType()) &&
286 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287}
288
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000289void CallInst::init(Value *Func, Value *Actual) {
290 NumOperands = 2;
291 Use *OL = OperandList = new Use[2];
292 OL[0].init(Func, this);
293 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000294
295 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000297 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000299 assert((FTy->getNumParams() == 1 ||
300 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000301 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000302 assert((0 == FTy->getNumParams() ||
303 FTy->getParamType(0) == Actual->getType()) &&
304 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000305}
306
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000307void CallInst::init(Value *Func) {
308 NumOperands = 1;
309 Use *OL = OperandList = new Use[1];
310 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000311
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000312 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000314 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000316 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000317}
318
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000319CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000320 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000321 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
322 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000323 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000325 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000326}
327
328CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
329 BasicBlock *InsertAtEnd)
330 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
331 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000332 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000333 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000334 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336CallInst::CallInst(Value *Func, const std::string &Name,
337 Instruction *InsertBefore)
338 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
339 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000340 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000342 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000343}
344
345CallInst::CallInst(Value *Func, const std::string &Name,
346 BasicBlock *InsertAtEnd)
347 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
348 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000349 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000351 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000352}
353
Misha Brukmanb1c93172005-04-21 23:48:37 +0000354CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000355 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
Chris Lattner8a923e72008-03-12 17:45:29 +0000356 CI.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000357 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000358 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000359 Use *OL = OperandList;
360 Use *InOL = CI.OperandList;
361 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
362 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000363}
364
Chris Lattnerc994c022008-03-13 05:00:21 +0000365bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000366 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000367 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000368 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000369 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000370 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000371}
372
Duncan Sandsaa31b922007-12-19 21:13:37 +0000373void CallInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000374 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000375 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000376 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000377 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000378 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000379 setParamAttrs(PAL);
380}
381
Duncan Sands5208d1a2007-11-28 17:07:01 +0000382
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000383//===----------------------------------------------------------------------===//
384// InvokeInst Implementation
385//===----------------------------------------------------------------------===//
386
Gordon Henriksen14a55692007-12-10 02:14:30 +0000387InvokeInst::~InvokeInst() {
388 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000389}
390
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000391void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000392 Value* const *Args, unsigned NumArgs) {
393 NumOperands = 3+NumArgs;
394 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000395 OL[0].init(Fn, this);
396 OL[1].init(IfNormal, this);
397 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000398 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000399 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000400 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000401
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000402 assert(((NumArgs == FTy->getNumParams()) ||
403 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000404 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000405
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000406 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000407 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000408 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000409 "Invoking a function with a bad signature!");
410
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000411 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000412 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000413}
414
Misha Brukmanb1c93172005-04-21 23:48:37 +0000415InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000416 : TerminatorInst(II.getType(), Instruction::Invoke,
Chris Lattner8a923e72008-03-12 17:45:29 +0000417 new Use[II.getNumOperands()], II.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000418 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000419 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000420 Use *OL = OperandList, *InOL = II.OperandList;
421 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
422 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000423}
424
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000425BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
426 return getSuccessor(idx);
427}
428unsigned InvokeInst::getNumSuccessorsV() const {
429 return getNumSuccessors();
430}
431void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
432 return setSuccessor(idx, B);
433}
434
Chris Lattnerc994c022008-03-13 05:00:21 +0000435bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000436 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000437 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000438 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000439 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000440 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000441}
442
Duncan Sandsaa31b922007-12-19 21:13:37 +0000443void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000444 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000445 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000446 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000447 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000448 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000449 setParamAttrs(PAL);
450}
451
Duncan Sands5208d1a2007-11-28 17:07:01 +0000452
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000453//===----------------------------------------------------------------------===//
454// ReturnInst Implementation
455//===----------------------------------------------------------------------===//
456
Chris Lattner2195fc42007-02-24 00:55:48 +0000457ReturnInst::ReturnInst(const ReturnInst &RI)
458 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Devang Patelae682fb2008-02-26 17:56:20 +0000459 &RetVal, RI.getNumOperands()) {
Devang Patel59643e52008-02-23 00:35:18 +0000460 unsigned N = RI.getNumOperands();
Devang Patelae682fb2008-02-26 17:56:20 +0000461 if (N == 1)
462 RetVal.init(RI.RetVal, this);
463 else if (N) {
464 Use *OL = OperandList = new Use[N];
465 for (unsigned i = 0; i < N; ++i)
466 OL[i].init(RI.getOperand(i), this);
467 }
Chris Lattner2195fc42007-02-24 00:55:48 +0000468}
469
470ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Devang Patelae682fb2008-02-26 17:56:20 +0000471 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000472 if (retVal)
473 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000474}
475ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Devang Patelae682fb2008-02-26 17:56:20 +0000476 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000477 if (retVal)
478 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000479}
480ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Devang Patelae682fb2008-02-26 17:56:20 +0000481 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000482}
483
Devang Patela736c002008-02-26 19:38:17 +0000484ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
485 Instruction *InsertBefore)
486 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N, InsertBefore) {
487 if (N != 0)
488 init(retVals, N);
489}
490ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
491 BasicBlock *InsertAtEnd)
492 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N, InsertAtEnd) {
493 if (N != 0)
494 init(retVals, N);
495}
496ReturnInst::ReturnInst(Value * const* retVals, unsigned N)
497 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N) {
498 if (N != 0)
499 init(retVals, N);
500}
501
Devang Patel060e79c2008-02-26 19:15:26 +0000502void ReturnInst::init(Value * const* retVals, unsigned N) {
Devang Patelc38eb522008-02-26 18:49:29 +0000503 assert (N > 0 && "Invalid operands numbers in ReturnInst init");
Devang Patel59643e52008-02-23 00:35:18 +0000504
Devang Patelc38eb522008-02-26 18:49:29 +0000505 NumOperands = N;
Devang Patel59643e52008-02-23 00:35:18 +0000506 if (NumOperands == 1) {
Devang Patel060e79c2008-02-26 19:15:26 +0000507 Value *V = *retVals;
Devang Patel59643e52008-02-23 00:35:18 +0000508 if (V->getType() == Type::VoidTy)
509 return;
Devang Patel060e79c2008-02-26 19:15:26 +0000510 RetVal.init(V, this);
Devang Patelae682fb2008-02-26 17:56:20 +0000511 return;
Devang Patel59643e52008-02-23 00:35:18 +0000512 }
513
514 Use *OL = OperandList = new Use[NumOperands];
515 for (unsigned i = 0; i < NumOperands; ++i) {
Devang Patel060e79c2008-02-26 19:15:26 +0000516 Value *V = *retVals++;
Devang Patel59643e52008-02-23 00:35:18 +0000517 assert(!isa<BasicBlock>(V) &&
518 "Cannot return basic block. Probably using the incorrect ctor");
Devang Patel060e79c2008-02-26 19:15:26 +0000519 OL[i].init(V, this);
Devang Patel59643e52008-02-23 00:35:18 +0000520 }
521}
522
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000523unsigned ReturnInst::getNumSuccessorsV() const {
524 return getNumSuccessors();
525}
526
Devang Patelae682fb2008-02-26 17:56:20 +0000527/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
528/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000529void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000530 assert(0 && "ReturnInst has no successors!");
531}
532
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000533BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
534 assert(0 && "ReturnInst has no successors!");
535 abort();
536 return 0;
537}
538
Devang Patel59643e52008-02-23 00:35:18 +0000539ReturnInst::~ReturnInst() {
Devang Patelae682fb2008-02-26 17:56:20 +0000540 if (NumOperands > 1)
Devang Patel59643e52008-02-23 00:35:18 +0000541 delete [] OperandList;
542}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000543
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000544//===----------------------------------------------------------------------===//
545// UnwindInst Implementation
546//===----------------------------------------------------------------------===//
547
Chris Lattner2195fc42007-02-24 00:55:48 +0000548UnwindInst::UnwindInst(Instruction *InsertBefore)
549 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
550}
551UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
552 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
553}
554
555
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000556unsigned UnwindInst::getNumSuccessorsV() const {
557 return getNumSuccessors();
558}
559
560void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000561 assert(0 && "UnwindInst has no successors!");
562}
563
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000564BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
565 assert(0 && "UnwindInst has no successors!");
566 abort();
567 return 0;
568}
569
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000570//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000571// UnreachableInst Implementation
572//===----------------------------------------------------------------------===//
573
Chris Lattner2195fc42007-02-24 00:55:48 +0000574UnreachableInst::UnreachableInst(Instruction *InsertBefore)
575 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
576}
577UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
578 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
579}
580
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000581unsigned UnreachableInst::getNumSuccessorsV() const {
582 return getNumSuccessors();
583}
584
585void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
586 assert(0 && "UnwindInst has no successors!");
587}
588
589BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
590 assert(0 && "UnwindInst has no successors!");
591 abort();
592 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000593}
594
595//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000596// BranchInst Implementation
597//===----------------------------------------------------------------------===//
598
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000599void BranchInst::AssertOK() {
600 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000601 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000602 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000603}
604
Chris Lattner2195fc42007-02-24 00:55:48 +0000605BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
606 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
607 assert(IfTrue != 0 && "Branch destination may not be null!");
608 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
609}
610BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
611 Instruction *InsertBefore)
612: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
613 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
614 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
615 Ops[2].init(Cond, this);
616#ifndef NDEBUG
617 AssertOK();
618#endif
619}
620
621BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
622 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
623 assert(IfTrue != 0 && "Branch destination may not be null!");
624 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
625}
626
627BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
628 BasicBlock *InsertAtEnd)
629 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
630 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
631 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
632 Ops[2].init(Cond, this);
633#ifndef NDEBUG
634 AssertOK();
635#endif
636}
637
638
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000639BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000640 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000641 OperandList[0].init(BI.getOperand(0), this);
642 if (BI.getNumOperands() != 1) {
643 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
644 OperandList[1].init(BI.getOperand(1), this);
645 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000646 }
647}
648
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000649BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
650 return getSuccessor(idx);
651}
652unsigned BranchInst::getNumSuccessorsV() const {
653 return getNumSuccessors();
654}
655void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
656 setSuccessor(idx, B);
657}
658
659
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000660//===----------------------------------------------------------------------===//
661// AllocationInst Implementation
662//===----------------------------------------------------------------------===//
663
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000664static Value *getAISize(Value *Amt) {
665 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000666 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000667 else {
668 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000669 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000670 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000671 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000672 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000673 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000674}
675
Misha Brukmanb1c93172005-04-21 23:48:37 +0000676AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000677 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000678 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000679 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000680 InsertBefore) {
681 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000682 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000683 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684}
685
Misha Brukmanb1c93172005-04-21 23:48:37 +0000686AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000687 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000688 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000689 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000690 InsertAtEnd) {
691 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000692 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000693 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000694}
695
Gordon Henriksen14a55692007-12-10 02:14:30 +0000696// Out of line virtual method, so the vtable, etc has a home.
697AllocationInst::~AllocationInst() {
698}
699
Dan Gohmanaa583d72008-03-24 16:55:58 +0000700void AllocationInst::setAlignment(unsigned Align) {
701 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
702 SubclassData = Log2_32(Align) + 1;
703 assert(getAlignment() == Align && "Alignment representation error!");
704}
705
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000706bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000707 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
708 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000709 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000710}
711
712const Type *AllocationInst::getAllocatedType() const {
713 return getType()->getElementType();
714}
715
716AllocaInst::AllocaInst(const AllocaInst &AI)
717 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000718 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000719}
720
721MallocInst::MallocInst(const MallocInst &MI)
722 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000723 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000724}
725
726//===----------------------------------------------------------------------===//
727// FreeInst Implementation
728//===----------------------------------------------------------------------===//
729
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000730void FreeInst::AssertOK() {
731 assert(isa<PointerType>(getOperand(0)->getType()) &&
732 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733}
734
735FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000736 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000737 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000738}
739
740FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000741 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000742 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000743}
744
745
746//===----------------------------------------------------------------------===//
747// LoadInst Implementation
748//===----------------------------------------------------------------------===//
749
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000750void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000751 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000752 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000753}
754
755LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000756 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000757 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000758 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000759 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000760 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000761 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000762}
763
764LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000765 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000766 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000767 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000768 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000769 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000770 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000771}
772
773LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
774 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000775 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000776 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000777 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000778 setAlignment(0);
779 AssertOK();
780 setName(Name);
781}
782
783LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
784 unsigned Align, Instruction *InsertBef)
785 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
786 Load, Ptr, InsertBef) {
787 setVolatile(isVolatile);
788 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000789 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000790 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000791}
792
Dan Gohman68659282007-07-18 20:51:11 +0000793LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
794 unsigned Align, BasicBlock *InsertAE)
795 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
796 Load, Ptr, InsertAE) {
797 setVolatile(isVolatile);
798 setAlignment(Align);
799 AssertOK();
800 setName(Name);
801}
802
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
804 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000805 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000806 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000807 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000808 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000809 AssertOK();
810 setName(Name);
811}
812
813
814
815LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000816 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
817 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000818 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000819 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000820 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000821 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000822}
823
824LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000825 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
826 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000827 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000828 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000829 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000830 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000831}
832
833LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
834 Instruction *InsertBef)
835: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000836 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000837 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000838 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000839 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000840 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000841}
842
843LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
844 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000845 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
846 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000847 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000848 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000849 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000850 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000851}
852
Christopher Lamb84485702007-04-22 19:24:39 +0000853void LoadInst::setAlignment(unsigned Align) {
854 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
855 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
856}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000857
858//===----------------------------------------------------------------------===//
859// StoreInst Implementation
860//===----------------------------------------------------------------------===//
861
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000862void StoreInst::AssertOK() {
863 assert(isa<PointerType>(getOperand(1)->getType()) &&
864 "Ptr must have pointer type!");
865 assert(getOperand(0)->getType() ==
866 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000867 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000868}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000869
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000870
871StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000872 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000873 Ops[0].init(val, this);
874 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000875 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000876 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000877 AssertOK();
878}
879
880StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000881 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000882 Ops[0].init(val, this);
883 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000884 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000885 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000886 AssertOK();
887}
888
Misha Brukmanb1c93172005-04-21 23:48:37 +0000889StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000890 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000891 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000892 Ops[0].init(val, this);
893 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000894 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000895 setAlignment(0);
896 AssertOK();
897}
898
899StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
900 unsigned Align, Instruction *InsertBefore)
901 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
902 Ops[0].init(val, this);
903 Ops[1].init(addr, this);
904 setVolatile(isVolatile);
905 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000906 AssertOK();
907}
908
Misha Brukmanb1c93172005-04-21 23:48:37 +0000909StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000910 unsigned Align, BasicBlock *InsertAtEnd)
911 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
912 Ops[0].init(val, this);
913 Ops[1].init(addr, this);
914 setVolatile(isVolatile);
915 setAlignment(Align);
916 AssertOK();
917}
918
919StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000920 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000921 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000922 Ops[0].init(val, this);
923 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000924 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000925 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000926 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000927}
928
Christopher Lamb84485702007-04-22 19:24:39 +0000929void StoreInst::setAlignment(unsigned Align) {
930 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
931 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
932}
933
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000934//===----------------------------------------------------------------------===//
935// GetElementPtrInst Implementation
936//===----------------------------------------------------------------------===//
937
Christopher Lambedf07882007-12-17 01:12:55 +0000938static unsigned retrieveAddrSpace(const Value *Val) {
939 return cast<PointerType>(Val->getType())->getAddressSpace();
940}
941
Chris Lattner79807c3d2007-01-31 19:47:18 +0000942void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
943 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000944 Use *OL = OperandList = new Use[NumOperands];
945 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000946
Chris Lattner79807c3d2007-01-31 19:47:18 +0000947 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000948 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000949}
950
Chris Lattner82981202005-05-03 05:43:30 +0000951void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
952 NumOperands = 2;
953 Use *OL = OperandList = new Use[2];
954 OL[0].init(Ptr, this);
955 OL[1].init(Idx, this);
956}
957
Chris Lattner82981202005-05-03 05:43:30 +0000958GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
959 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000960 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +0000961 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +0000962 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +0000963 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000964 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000965}
966
967GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
968 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000969 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +0000970 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +0000971 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +0000972 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000973 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000974}
975
Gordon Henriksen14a55692007-12-10 02:14:30 +0000976GetElementPtrInst::~GetElementPtrInst() {
977 delete[] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000978}
979
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000980// getIndexedType - Returns the type of the element that would be loaded with
981// a load instruction with the specified parameters.
982//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000983// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000984// pointer type.
985//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000986const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000987 Value* const *Idxs,
988 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000989 bool AllowCompositeLeaf) {
990 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
991
992 // Handle the special case of the empty set index set...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000993 if (NumIdx == 0) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000994 if (AllowCompositeLeaf ||
995 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
996 return cast<PointerType>(Ptr)->getElementType();
997 else
998 return 0;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000999 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001000
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001001 unsigned CurIdx = 0;
1002 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001003 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001004 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
1005 return 0; // Can't load a whole structure or array!?!?
1006 }
1007
Chris Lattner302116a2007-01-31 04:40:28 +00001008 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001009 if (isa<PointerType>(CT) && CurIdx != 1)
1010 return 0; // Can only index into pointer types at the first index!
1011 if (!CT->indexValid(Index)) return 0;
1012 Ptr = CT->getTypeAtIndex(Index);
1013
1014 // If the new type forwards to another type, then it is in the middle
1015 // of being refined to another type (and hence, may have dropped all
1016 // references to what it was using before). So, use the new forwarded
1017 // type.
1018 if (const Type * Ty = Ptr->getForwardedType()) {
1019 Ptr = Ty;
1020 }
1021 }
Chris Lattner302116a2007-01-31 04:40:28 +00001022 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001023}
1024
Chris Lattner82981202005-05-03 05:43:30 +00001025const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1026 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1027 if (!PTy) return 0; // Type isn't a pointer type!
1028
1029 // Check the pointer index.
1030 if (!PTy->indexValid(Idx)) return 0;
1031
Chris Lattnerc2233332005-05-03 16:44:45 +00001032 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001033}
1034
Chris Lattner45f15572007-04-14 00:12:57 +00001035
1036/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1037/// zeros. If so, the result pointer and the first operand have the same
1038/// value, just potentially different types.
1039bool GetElementPtrInst::hasAllZeroIndices() const {
1040 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1041 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1042 if (!CI->isZero()) return false;
1043 } else {
1044 return false;
1045 }
1046 }
1047 return true;
1048}
1049
Chris Lattner27058292007-04-27 20:35:56 +00001050/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1051/// constant integers. If so, the result pointer and the first operand have
1052/// a constant offset between them.
1053bool GetElementPtrInst::hasAllConstantIndices() const {
1054 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1055 if (!isa<ConstantInt>(getOperand(i)))
1056 return false;
1057 }
1058 return true;
1059}
1060
Chris Lattner45f15572007-04-14 00:12:57 +00001061
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001062//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001063// ExtractElementInst Implementation
1064//===----------------------------------------------------------------------===//
1065
1066ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001067 const std::string &Name,
1068 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001069 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001070 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001071 assert(isValidOperands(Val, Index) &&
1072 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001073 Ops[0].init(Val, this);
1074 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001075 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001076}
1077
Chris Lattner65511ff2006-10-05 06:24:58 +00001078ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1079 const std::string &Name,
1080 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001081 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001082 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001083 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001084 assert(isValidOperands(Val, Index) &&
1085 "Invalid extractelement instruction operands!");
1086 Ops[0].init(Val, this);
1087 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001088 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001089}
1090
1091
Robert Bocchino23004482006-01-10 19:05:34 +00001092ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001093 const std::string &Name,
1094 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001095 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001096 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001097 assert(isValidOperands(Val, Index) &&
1098 "Invalid extractelement instruction operands!");
1099
Robert Bocchino23004482006-01-10 19:05:34 +00001100 Ops[0].init(Val, this);
1101 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001102 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001103}
1104
Chris Lattner65511ff2006-10-05 06:24:58 +00001105ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1106 const std::string &Name,
1107 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001108 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001109 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001110 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001111 assert(isValidOperands(Val, Index) &&
1112 "Invalid extractelement instruction operands!");
1113
1114 Ops[0].init(Val, this);
1115 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001116 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001117}
1118
1119
Chris Lattner54865b32006-04-08 04:05:48 +00001120bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001121 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001122 return false;
1123 return true;
1124}
1125
1126
Robert Bocchino23004482006-01-10 19:05:34 +00001127//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001128// InsertElementInst Implementation
1129//===----------------------------------------------------------------------===//
1130
Chris Lattner0875d942006-04-14 22:20:32 +00001131InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1132 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1133 Ops[0].init(IE.Ops[0], this);
1134 Ops[1].init(IE.Ops[1], this);
1135 Ops[2].init(IE.Ops[2], this);
1136}
Chris Lattner54865b32006-04-08 04:05:48 +00001137InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001138 const std::string &Name,
1139 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001140 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001141 assert(isValidOperands(Vec, Elt, Index) &&
1142 "Invalid insertelement instruction operands!");
1143 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001144 Ops[1].init(Elt, this);
1145 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001146 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001147}
1148
Chris Lattner65511ff2006-10-05 06:24:58 +00001149InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1150 const std::string &Name,
1151 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001152 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001153 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001154 assert(isValidOperands(Vec, Elt, Index) &&
1155 "Invalid insertelement instruction operands!");
1156 Ops[0].init(Vec, this);
1157 Ops[1].init(Elt, this);
1158 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001159 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001160}
1161
1162
Chris Lattner54865b32006-04-08 04:05:48 +00001163InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001164 const std::string &Name,
1165 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001166 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001167 assert(isValidOperands(Vec, Elt, Index) &&
1168 "Invalid insertelement instruction operands!");
1169
1170 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001171 Ops[1].init(Elt, this);
1172 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001173 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001174}
1175
Chris Lattner65511ff2006-10-05 06:24:58 +00001176InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1177 const std::string &Name,
1178 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001179: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001180 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001181 assert(isValidOperands(Vec, Elt, Index) &&
1182 "Invalid insertelement instruction operands!");
1183
1184 Ops[0].init(Vec, this);
1185 Ops[1].init(Elt, this);
1186 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001187 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001188}
1189
Chris Lattner54865b32006-04-08 04:05:48 +00001190bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1191 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001192 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001193 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001194
Reid Spencerd84d35b2007-02-15 02:26:10 +00001195 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001196 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001197
Reid Spencer8d9336d2006-12-31 05:26:44 +00001198 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001199 return false; // Third operand of insertelement must be uint.
1200 return true;
1201}
1202
1203
Robert Bocchinoca27f032006-01-17 20:07:22 +00001204//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001205// ShuffleVectorInst Implementation
1206//===----------------------------------------------------------------------===//
1207
Chris Lattner0875d942006-04-14 22:20:32 +00001208ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1209 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1210 Ops[0].init(SV.Ops[0], this);
1211 Ops[1].init(SV.Ops[1], this);
1212 Ops[2].init(SV.Ops[2], this);
1213}
1214
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001215ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1216 const std::string &Name,
1217 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001218 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001219 assert(isValidOperands(V1, V2, Mask) &&
1220 "Invalid shuffle vector instruction operands!");
1221 Ops[0].init(V1, this);
1222 Ops[1].init(V2, this);
1223 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001224 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001225}
1226
1227ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1228 const std::string &Name,
1229 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001230 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001231 assert(isValidOperands(V1, V2, Mask) &&
1232 "Invalid shuffle vector instruction operands!");
1233
1234 Ops[0].init(V1, this);
1235 Ops[1].init(V2, this);
1236 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001237 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001238}
1239
1240bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1241 const Value *Mask) {
Chris Lattnerf724e342008-03-02 05:28:33 +00001242 if (!isa<VectorType>(V1->getType()) ||
1243 V1->getType() != V2->getType())
1244 return false;
1245
1246 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1247 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1248 MaskTy->getElementType() != Type::Int32Ty ||
1249 MaskTy->getNumElements() !=
1250 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001251 return false;
1252 return true;
1253}
1254
Chris Lattnerf724e342008-03-02 05:28:33 +00001255/// getMaskValue - Return the index from the shuffle mask for the specified
1256/// output result. This is either -1 if the element is undef or a number less
1257/// than 2*numelements.
1258int ShuffleVectorInst::getMaskValue(unsigned i) const {
1259 const Constant *Mask = cast<Constant>(getOperand(2));
1260 if (isa<UndefValue>(Mask)) return -1;
1261 if (isa<ConstantAggregateZero>(Mask)) return 0;
1262 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1263 assert(i < MaskCV->getNumOperands() && "Index out of range");
1264
1265 if (isa<UndefValue>(MaskCV->getOperand(i)))
1266 return -1;
1267 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1268}
1269
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001270
1271//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001272// BinaryOperator Class
1273//===----------------------------------------------------------------------===//
1274
Chris Lattner2195fc42007-02-24 00:55:48 +00001275BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1276 const Type *Ty, const std::string &Name,
1277 Instruction *InsertBefore)
1278 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1279 Ops[0].init(S1, this);
1280 Ops[1].init(S2, this);
1281 init(iType);
1282 setName(Name);
1283}
1284
1285BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1286 const Type *Ty, const std::string &Name,
1287 BasicBlock *InsertAtEnd)
1288 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1289 Ops[0].init(S1, this);
1290 Ops[1].init(S2, this);
1291 init(iType);
1292 setName(Name);
1293}
1294
1295
1296void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001297 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001298 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001299 assert(LHS->getType() == RHS->getType() &&
1300 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001301#ifndef NDEBUG
1302 switch (iType) {
1303 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001304 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001305 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001306 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001307 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001308 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001309 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001310 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001311 case UDiv:
1312 case SDiv:
1313 assert(getType() == LHS->getType() &&
1314 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001315 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1316 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001317 "Incorrect operand type (not integer) for S/UDIV");
1318 break;
1319 case FDiv:
1320 assert(getType() == LHS->getType() &&
1321 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001322 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1323 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001324 && "Incorrect operand type (not floating point) for FDIV");
1325 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001326 case URem:
1327 case SRem:
1328 assert(getType() == LHS->getType() &&
1329 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001330 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1331 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001332 "Incorrect operand type (not integer) for S/UREM");
1333 break;
1334 case FRem:
1335 assert(getType() == LHS->getType() &&
1336 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001337 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1338 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001339 && "Incorrect operand type (not floating point) for FREM");
1340 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001341 case Shl:
1342 case LShr:
1343 case AShr:
1344 assert(getType() == LHS->getType() &&
1345 "Shift operation should return same type as operands!");
1346 assert(getType()->isInteger() &&
1347 "Shift operation requires integer operands");
1348 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001349 case And: case Or:
1350 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001351 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001352 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001353 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001354 (isa<VectorType>(getType()) &&
1355 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001356 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001357 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001358 default:
1359 break;
1360 }
1361#endif
1362}
1363
1364BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001365 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001366 Instruction *InsertBefore) {
1367 assert(S1->getType() == S2->getType() &&
1368 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001369 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001370}
1371
1372BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001373 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001374 BasicBlock *InsertAtEnd) {
1375 BinaryOperator *Res = create(Op, S1, S2, Name);
1376 InsertAtEnd->getInstList().push_back(Res);
1377 return Res;
1378}
1379
1380BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1381 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001382 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1383 return new BinaryOperator(Instruction::Sub,
1384 zero, Op,
1385 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001386}
1387
1388BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1389 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001390 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1391 return new BinaryOperator(Instruction::Sub,
1392 zero, Op,
1393 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001394}
1395
1396BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1397 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001398 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001399 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001400 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001401 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001402 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001403 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001404 }
1405
1406 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001407 Op->getType(), Name, InsertBefore);
1408}
1409
1410BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1411 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001412 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001413 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001414 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001415 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001416 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001417 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001418 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001419 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001420 }
1421
1422 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001423 Op->getType(), Name, InsertAtEnd);
1424}
1425
1426
1427// isConstantAllOnes - Helper function for several functions below
1428static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001429 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1430 return CI->isAllOnesValue();
1431 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1432 return CV->isAllOnesValue();
1433 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001434}
1435
1436bool BinaryOperator::isNeg(const Value *V) {
1437 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1438 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001439 return Bop->getOperand(0) ==
1440 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001441 return false;
1442}
1443
1444bool BinaryOperator::isNot(const Value *V) {
1445 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1446 return (Bop->getOpcode() == Instruction::Xor &&
1447 (isConstantAllOnes(Bop->getOperand(1)) ||
1448 isConstantAllOnes(Bop->getOperand(0))));
1449 return false;
1450}
1451
Chris Lattner2c7d1772005-04-24 07:28:37 +00001452Value *BinaryOperator::getNegArgument(Value *BinOp) {
1453 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1454 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001455}
1456
Chris Lattner2c7d1772005-04-24 07:28:37 +00001457const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1458 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001459}
1460
Chris Lattner2c7d1772005-04-24 07:28:37 +00001461Value *BinaryOperator::getNotArgument(Value *BinOp) {
1462 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1463 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1464 Value *Op0 = BO->getOperand(0);
1465 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001466 if (isConstantAllOnes(Op0)) return Op1;
1467
1468 assert(isConstantAllOnes(Op1));
1469 return Op0;
1470}
1471
Chris Lattner2c7d1772005-04-24 07:28:37 +00001472const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1473 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001474}
1475
1476
1477// swapOperands - Exchange the two operands to this instruction. This
1478// instruction is safe to use on any binary instruction and does not
1479// modify the semantics of the instruction. If the instruction is
1480// order dependent (SetLT f.e.) the opcode is changed.
1481//
1482bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001483 if (!isCommutative())
1484 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001485 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001486 return false;
1487}
1488
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001489//===----------------------------------------------------------------------===//
1490// CastInst Class
1491//===----------------------------------------------------------------------===//
1492
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001493// Just determine if this cast only deals with integral->integral conversion.
1494bool CastInst::isIntegerCast() const {
1495 switch (getOpcode()) {
1496 default: return false;
1497 case Instruction::ZExt:
1498 case Instruction::SExt:
1499 case Instruction::Trunc:
1500 return true;
1501 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001502 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001503 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001504}
1505
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001506bool CastInst::isLosslessCast() const {
1507 // Only BitCast can be lossless, exit fast if we're not BitCast
1508 if (getOpcode() != Instruction::BitCast)
1509 return false;
1510
1511 // Identity cast is always lossless
1512 const Type* SrcTy = getOperand(0)->getType();
1513 const Type* DstTy = getType();
1514 if (SrcTy == DstTy)
1515 return true;
1516
Reid Spencer8d9336d2006-12-31 05:26:44 +00001517 // Pointer to pointer is always lossless.
1518 if (isa<PointerType>(SrcTy))
1519 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001520 return false; // Other types have no identity values
1521}
1522
1523/// This function determines if the CastInst does not require any bits to be
1524/// changed in order to effect the cast. Essentially, it identifies cases where
1525/// no code gen is necessary for the cast, hence the name no-op cast. For
1526/// example, the following are all no-op casts:
1527/// # bitcast uint %X, int
1528/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001529/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001530/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1531/// @brief Determine if a cast is a no-op.
1532bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1533 switch (getOpcode()) {
1534 default:
1535 assert(!"Invalid CastOp");
1536 case Instruction::Trunc:
1537 case Instruction::ZExt:
1538 case Instruction::SExt:
1539 case Instruction::FPTrunc:
1540 case Instruction::FPExt:
1541 case Instruction::UIToFP:
1542 case Instruction::SIToFP:
1543 case Instruction::FPToUI:
1544 case Instruction::FPToSI:
1545 return false; // These always modify bits
1546 case Instruction::BitCast:
1547 return true; // BitCast never modifies bits.
1548 case Instruction::PtrToInt:
1549 return IntPtrTy->getPrimitiveSizeInBits() ==
1550 getType()->getPrimitiveSizeInBits();
1551 case Instruction::IntToPtr:
1552 return IntPtrTy->getPrimitiveSizeInBits() ==
1553 getOperand(0)->getType()->getPrimitiveSizeInBits();
1554 }
1555}
1556
1557/// This function determines if a pair of casts can be eliminated and what
1558/// opcode should be used in the elimination. This assumes that there are two
1559/// instructions like this:
1560/// * %F = firstOpcode SrcTy %x to MidTy
1561/// * %S = secondOpcode MidTy %F to DstTy
1562/// The function returns a resultOpcode so these two casts can be replaced with:
1563/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1564/// If no such cast is permited, the function returns 0.
1565unsigned CastInst::isEliminableCastPair(
1566 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1567 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1568{
1569 // Define the 144 possibilities for these two cast instructions. The values
1570 // in this matrix determine what to do in a given situation and select the
1571 // case in the switch below. The rows correspond to firstOp, the columns
1572 // correspond to secondOp. In looking at the table below, keep in mind
1573 // the following cast properties:
1574 //
1575 // Size Compare Source Destination
1576 // Operator Src ? Size Type Sign Type Sign
1577 // -------- ------------ ------------------- ---------------------
1578 // TRUNC > Integer Any Integral Any
1579 // ZEXT < Integral Unsigned Integer Any
1580 // SEXT < Integral Signed Integer Any
1581 // FPTOUI n/a FloatPt n/a Integral Unsigned
1582 // FPTOSI n/a FloatPt n/a Integral Signed
1583 // UITOFP n/a Integral Unsigned FloatPt n/a
1584 // SITOFP n/a Integral Signed FloatPt n/a
1585 // FPTRUNC > FloatPt n/a FloatPt n/a
1586 // FPEXT < FloatPt n/a FloatPt n/a
1587 // PTRTOINT n/a Pointer n/a Integral Unsigned
1588 // INTTOPTR n/a Integral Unsigned Pointer n/a
1589 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001590 //
1591 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1592 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1593 // into "fptoui double to ulong", but this loses information about the range
1594 // of the produced value (we no longer know the top-part is all zeros).
1595 // Further this conversion is often much more expensive for typical hardware,
1596 // and causes issues when building libgcc. We disallow fptosi+sext for the
1597 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001598 const unsigned numCastOps =
1599 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1600 static const uint8_t CastResults[numCastOps][numCastOps] = {
1601 // T F F U S F F P I B -+
1602 // R Z S P P I I T P 2 N T |
1603 // U E E 2 2 2 2 R E I T C +- secondOp
1604 // N X X U S F F N X N 2 V |
1605 // C T T I I P P C T T P T -+
1606 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1607 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1608 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001609 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1610 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001611 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1612 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1613 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1614 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1615 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1616 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1617 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1618 };
1619
1620 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1621 [secondOp-Instruction::CastOpsBegin];
1622 switch (ElimCase) {
1623 case 0:
1624 // categorically disallowed
1625 return 0;
1626 case 1:
1627 // allowed, use first cast's opcode
1628 return firstOp;
1629 case 2:
1630 // allowed, use second cast's opcode
1631 return secondOp;
1632 case 3:
1633 // no-op cast in second op implies firstOp as long as the DestTy
1634 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001635 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001636 return firstOp;
1637 return 0;
1638 case 4:
1639 // no-op cast in second op implies firstOp as long as the DestTy
1640 // is floating point
1641 if (DstTy->isFloatingPoint())
1642 return firstOp;
1643 return 0;
1644 case 5:
1645 // no-op cast in first op implies secondOp as long as the SrcTy
1646 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001647 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001648 return secondOp;
1649 return 0;
1650 case 6:
1651 // no-op cast in first op implies secondOp as long as the SrcTy
1652 // is a floating point
1653 if (SrcTy->isFloatingPoint())
1654 return secondOp;
1655 return 0;
1656 case 7: {
1657 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1658 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1659 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1660 if (MidSize >= PtrSize)
1661 return Instruction::BitCast;
1662 return 0;
1663 }
1664 case 8: {
1665 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1666 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1667 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1668 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1669 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1670 if (SrcSize == DstSize)
1671 return Instruction::BitCast;
1672 else if (SrcSize < DstSize)
1673 return firstOp;
1674 return secondOp;
1675 }
1676 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1677 return Instruction::ZExt;
1678 case 10:
1679 // fpext followed by ftrunc is allowed if the bit size returned to is
1680 // the same as the original, in which case its just a bitcast
1681 if (SrcTy == DstTy)
1682 return Instruction::BitCast;
1683 return 0; // If the types are not the same we can't eliminate it.
1684 case 11:
1685 // bitcast followed by ptrtoint is allowed as long as the bitcast
1686 // is a pointer to pointer cast.
1687 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1688 return secondOp;
1689 return 0;
1690 case 12:
1691 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1692 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1693 return firstOp;
1694 return 0;
1695 case 13: {
1696 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1697 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1698 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1699 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1700 if (SrcSize <= PtrSize && SrcSize == DstSize)
1701 return Instruction::BitCast;
1702 return 0;
1703 }
1704 case 99:
1705 // cast combination can't happen (error in input). This is for all cases
1706 // where the MidTy is not the same for the two cast instructions.
1707 assert(!"Invalid Cast Combination");
1708 return 0;
1709 default:
1710 assert(!"Error in CastResults table!!!");
1711 return 0;
1712 }
1713 return 0;
1714}
1715
1716CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1717 const std::string &Name, Instruction *InsertBefore) {
1718 // Construct and return the appropriate CastInst subclass
1719 switch (op) {
1720 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1721 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1722 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1723 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1724 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1725 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1726 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1727 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1728 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1729 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1730 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1731 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1732 default:
1733 assert(!"Invalid opcode provided");
1734 }
1735 return 0;
1736}
1737
1738CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1739 const std::string &Name, BasicBlock *InsertAtEnd) {
1740 // Construct and return the appropriate CastInst subclass
1741 switch (op) {
1742 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1743 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1744 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1745 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1746 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1747 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1748 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1749 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1750 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1751 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1752 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1753 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1754 default:
1755 assert(!"Invalid opcode provided");
1756 }
1757 return 0;
1758}
1759
Reid Spencer5c140882006-12-04 20:17:56 +00001760CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1761 const std::string &Name,
1762 Instruction *InsertBefore) {
1763 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1764 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1765 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1766}
1767
1768CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1769 const std::string &Name,
1770 BasicBlock *InsertAtEnd) {
1771 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1772 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1773 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1774}
1775
1776CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1777 const std::string &Name,
1778 Instruction *InsertBefore) {
1779 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1780 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1781 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1782}
1783
1784CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1785 const std::string &Name,
1786 BasicBlock *InsertAtEnd) {
1787 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1788 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1789 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1790}
1791
1792CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1793 const std::string &Name,
1794 Instruction *InsertBefore) {
1795 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1796 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1797 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1798}
1799
1800CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1801 const std::string &Name,
1802 BasicBlock *InsertAtEnd) {
1803 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1804 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1805 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1806}
1807
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001808CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1809 const std::string &Name,
1810 BasicBlock *InsertAtEnd) {
1811 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001812 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001813 "Invalid cast");
1814
Chris Lattner03c49532007-01-15 02:27:26 +00001815 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001816 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1817 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1818}
1819
1820/// @brief Create a BitCast or a PtrToInt cast instruction
1821CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1822 const std::string &Name,
1823 Instruction *InsertBefore) {
1824 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001825 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001826 "Invalid cast");
1827
Chris Lattner03c49532007-01-15 02:27:26 +00001828 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001829 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1830 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1831}
1832
Reid Spencer7e933472006-12-12 00:49:44 +00001833CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1834 bool isSigned, const std::string &Name,
1835 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001836 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001837 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1838 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1839 Instruction::CastOps opcode =
1840 (SrcBits == DstBits ? Instruction::BitCast :
1841 (SrcBits > DstBits ? Instruction::Trunc :
1842 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1843 return create(opcode, C, Ty, Name, InsertBefore);
1844}
1845
1846CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1847 bool isSigned, const std::string &Name,
1848 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001849 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001850 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1851 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1852 Instruction::CastOps opcode =
1853 (SrcBits == DstBits ? Instruction::BitCast :
1854 (SrcBits > DstBits ? Instruction::Trunc :
1855 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1856 return create(opcode, C, Ty, Name, InsertAtEnd);
1857}
1858
1859CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1860 const std::string &Name,
1861 Instruction *InsertBefore) {
1862 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1863 "Invalid cast");
1864 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1865 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1866 Instruction::CastOps opcode =
1867 (SrcBits == DstBits ? Instruction::BitCast :
1868 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1869 return create(opcode, C, Ty, Name, InsertBefore);
1870}
1871
1872CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1873 const std::string &Name,
1874 BasicBlock *InsertAtEnd) {
1875 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1876 "Invalid cast");
1877 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1878 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1879 Instruction::CastOps opcode =
1880 (SrcBits == DstBits ? Instruction::BitCast :
1881 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1882 return create(opcode, C, Ty, Name, InsertAtEnd);
1883}
1884
Duncan Sands55e50902008-01-06 10:12:28 +00001885// Check whether it is valid to call getCastOpcode for these types.
1886// This routine must be kept in sync with getCastOpcode.
1887bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
1888 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
1889 return false;
1890
1891 if (SrcTy == DestTy)
1892 return true;
1893
1894 // Get the bit sizes, we'll need these
1895 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1896 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1897
1898 // Run through the possibilities ...
1899 if (DestTy->isInteger()) { // Casting to integral
1900 if (SrcTy->isInteger()) { // Casting from integral
1901 return true;
1902 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1903 return true;
1904 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1905 // Casting from vector
1906 return DestBits == PTy->getBitWidth();
1907 } else { // Casting from something else
1908 return isa<PointerType>(SrcTy);
1909 }
1910 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
1911 if (SrcTy->isInteger()) { // Casting from integral
1912 return true;
1913 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1914 return true;
1915 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1916 // Casting from vector
1917 return DestBits == PTy->getBitWidth();
1918 } else { // Casting from something else
1919 return false;
1920 }
1921 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1922 // Casting to vector
1923 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
1924 // Casting from vector
1925 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
1926 } else { // Casting from something else
1927 return DestPTy->getBitWidth() == SrcBits;
1928 }
1929 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
1930 if (isa<PointerType>(SrcTy)) { // Casting from pointer
1931 return true;
1932 } else if (SrcTy->isInteger()) { // Casting from integral
1933 return true;
1934 } else { // Casting from something else
1935 return false;
1936 }
1937 } else { // Casting to something else
1938 return false;
1939 }
1940}
1941
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001942// Provide a way to get a "cast" where the cast opcode is inferred from the
1943// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001944// logic in the castIsValid function below. This axiom should hold:
1945// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1946// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001947// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00001948// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001949Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001950CastInst::getCastOpcode(
1951 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001952 // Get the bit sizes, we'll need these
1953 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00001954 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1955 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001956
Duncan Sands55e50902008-01-06 10:12:28 +00001957 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
1958 "Only first class types are castable!");
1959
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001960 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001961 if (DestTy->isInteger()) { // Casting to integral
1962 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001963 if (DestBits < SrcBits)
1964 return Trunc; // int -> smaller int
1965 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001966 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001967 return SExt; // signed -> SEXT
1968 else
1969 return ZExt; // unsigned -> ZEXT
1970 } else {
1971 return BitCast; // Same size, No-op cast
1972 }
1973 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001974 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001975 return FPToSI; // FP -> sint
1976 else
1977 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001978 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001979 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001980 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001981 return BitCast; // Same size, no-op cast
1982 } else {
1983 assert(isa<PointerType>(SrcTy) &&
1984 "Casting from a value that is not first-class type");
1985 return PtrToInt; // ptr -> int
1986 }
1987 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001988 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001989 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001990 return SIToFP; // sint -> FP
1991 else
1992 return UIToFP; // uint -> FP
1993 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1994 if (DestBits < SrcBits) {
1995 return FPTrunc; // FP -> smaller FP
1996 } else if (DestBits > SrcBits) {
1997 return FPExt; // FP -> larger FP
1998 } else {
1999 return BitCast; // same size, no-op cast
2000 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002001 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002002 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002003 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002004 return BitCast; // same size, no-op cast
2005 } else {
2006 assert(0 && "Casting pointer or non-first class to float");
2007 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002008 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2009 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002010 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002011 "Casting vector to vector of different widths");
2012 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002013 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002014 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002015 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002016 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002017 }
2018 } else if (isa<PointerType>(DestTy)) {
2019 if (isa<PointerType>(SrcTy)) {
2020 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002021 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002022 return IntToPtr; // int -> ptr
2023 } else {
2024 assert(!"Casting pointer to other than pointer or int");
2025 }
2026 } else {
2027 assert(!"Casting to type that is not first-class");
2028 }
2029
2030 // If we fall through to here we probably hit an assertion cast above
2031 // and assertions are not turned on. Anything we return is an error, so
2032 // BitCast is as good a choice as any.
2033 return BitCast;
2034}
2035
2036//===----------------------------------------------------------------------===//
2037// CastInst SubClass Constructors
2038//===----------------------------------------------------------------------===//
2039
2040/// Check that the construction parameters for a CastInst are correct. This
2041/// could be broken out into the separate constructors but it is useful to have
2042/// it in one place and to eliminate the redundant code for getting the sizes
2043/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002044bool
2045CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002046
2047 // Check for type sanity on the arguments
2048 const Type *SrcTy = S->getType();
2049 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2050 return false;
2051
2052 // Get the size of the types in bits, we'll need this later
2053 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2054 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2055
2056 // Switch on the opcode provided
2057 switch (op) {
2058 default: return false; // This is an input error
2059 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002060 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002061 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002062 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002063 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002064 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002065 case Instruction::FPTrunc:
2066 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2067 SrcBitSize > DstBitSize;
2068 case Instruction::FPExt:
2069 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2070 SrcBitSize < DstBitSize;
2071 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002072 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002073 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2074 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2075 return SVTy->getElementType()->isInteger() &&
2076 DVTy->getElementType()->isFloatingPoint() &&
2077 SVTy->getNumElements() == DVTy->getNumElements();
2078 }
2079 }
Chris Lattner03c49532007-01-15 02:27:26 +00002080 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002081 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002082 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002083 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2084 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2085 return SVTy->getElementType()->isFloatingPoint() &&
2086 DVTy->getElementType()->isInteger() &&
2087 SVTy->getNumElements() == DVTy->getNumElements();
2088 }
2089 }
Chris Lattner03c49532007-01-15 02:27:26 +00002090 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002091 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002092 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002093 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002094 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002095 case Instruction::BitCast:
2096 // BitCast implies a no-op cast of type only. No bits change.
2097 // However, you can't cast pointers to anything but pointers.
2098 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2099 return false;
2100
Duncan Sands55e50902008-01-06 10:12:28 +00002101 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002102 // these cases, the cast is okay if the source and destination bit widths
2103 // are identical.
2104 return SrcBitSize == DstBitSize;
2105 }
2106}
2107
2108TruncInst::TruncInst(
2109 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2110) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002111 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002112}
2113
2114TruncInst::TruncInst(
2115 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2116) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002117 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002118}
2119
2120ZExtInst::ZExtInst(
2121 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2122) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002123 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002124}
2125
2126ZExtInst::ZExtInst(
2127 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2128) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002129 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002130}
2131SExtInst::SExtInst(
2132 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2133) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002134 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002135}
2136
Jeff Cohencc08c832006-12-02 02:22:01 +00002137SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002138 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2139) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002140 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002141}
2142
2143FPTruncInst::FPTruncInst(
2144 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2145) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002146 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002147}
2148
2149FPTruncInst::FPTruncInst(
2150 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2151) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002152 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002153}
2154
2155FPExtInst::FPExtInst(
2156 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2157) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002158 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002159}
2160
2161FPExtInst::FPExtInst(
2162 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2163) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002164 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002165}
2166
2167UIToFPInst::UIToFPInst(
2168 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2169) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002170 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002171}
2172
2173UIToFPInst::UIToFPInst(
2174 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2175) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002176 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002177}
2178
2179SIToFPInst::SIToFPInst(
2180 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2181) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002182 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002183}
2184
2185SIToFPInst::SIToFPInst(
2186 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2187) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002188 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002189}
2190
2191FPToUIInst::FPToUIInst(
2192 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2193) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002194 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002195}
2196
2197FPToUIInst::FPToUIInst(
2198 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2199) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002200 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002201}
2202
2203FPToSIInst::FPToSIInst(
2204 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2205) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002206 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002207}
2208
2209FPToSIInst::FPToSIInst(
2210 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2211) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002212 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002213}
2214
2215PtrToIntInst::PtrToIntInst(
2216 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2217) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002218 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002219}
2220
2221PtrToIntInst::PtrToIntInst(
2222 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2223) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002224 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002225}
2226
2227IntToPtrInst::IntToPtrInst(
2228 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2229) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002230 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002231}
2232
2233IntToPtrInst::IntToPtrInst(
2234 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2235) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002236 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237}
2238
2239BitCastInst::BitCastInst(
2240 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2241) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002242 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002243}
2244
2245BitCastInst::BitCastInst(
2246 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2247) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002248 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002249}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002250
2251//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002252// CmpInst Classes
2253//===----------------------------------------------------------------------===//
2254
2255CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2256 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002257 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002258 Ops[0].init(LHS, this);
2259 Ops[1].init(RHS, this);
2260 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002261 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002262 if (op == Instruction::ICmp) {
2263 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2264 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2265 "Invalid ICmp predicate value");
2266 const Type* Op0Ty = getOperand(0)->getType();
2267 const Type* Op1Ty = getOperand(1)->getType();
2268 assert(Op0Ty == Op1Ty &&
2269 "Both operands to ICmp instruction are not of the same type!");
2270 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002271 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002272 "Invalid operand types for ICmp instruction");
2273 return;
2274 }
2275 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2276 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2277 "Invalid FCmp predicate value");
2278 const Type* Op0Ty = getOperand(0)->getType();
2279 const Type* Op1Ty = getOperand(1)->getType();
2280 assert(Op0Ty == Op1Ty &&
2281 "Both operands to FCmp instruction are not of the same type!");
2282 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002283 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002284 "Invalid operand types for FCmp instruction");
2285}
2286
2287CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2288 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002289 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002290 Ops[0].init(LHS, this);
2291 Ops[1].init(RHS, this);
2292 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002293 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002294 if (op == Instruction::ICmp) {
2295 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2296 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2297 "Invalid ICmp predicate value");
2298
2299 const Type* Op0Ty = getOperand(0)->getType();
2300 const Type* Op1Ty = getOperand(1)->getType();
2301 assert(Op0Ty == Op1Ty &&
2302 "Both operands to ICmp instruction are not of the same type!");
2303 // Check that the operands are the right type
Anton Korobeynikov579f0712008-02-20 11:08:44 +00002304 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002305 "Invalid operand types for ICmp instruction");
2306 return;
2307 }
2308 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2309 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2310 "Invalid FCmp predicate value");
2311 const Type* Op0Ty = getOperand(0)->getType();
2312 const Type* Op1Ty = getOperand(1)->getType();
2313 assert(Op0Ty == Op1Ty &&
2314 "Both operands to FCmp instruction are not of the same type!");
2315 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002316 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002317 "Invalid operand types for FCmp instruction");
2318}
2319
2320CmpInst *
2321CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2322 const std::string &Name, Instruction *InsertBefore) {
2323 if (Op == Instruction::ICmp) {
2324 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2325 InsertBefore);
2326 }
2327 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2328 InsertBefore);
2329}
2330
2331CmpInst *
2332CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2333 const std::string &Name, BasicBlock *InsertAtEnd) {
2334 if (Op == Instruction::ICmp) {
2335 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2336 InsertAtEnd);
2337 }
2338 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2339 InsertAtEnd);
2340}
2341
2342void CmpInst::swapOperands() {
2343 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2344 IC->swapOperands();
2345 else
2346 cast<FCmpInst>(this)->swapOperands();
2347}
2348
2349bool CmpInst::isCommutative() {
2350 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2351 return IC->isCommutative();
2352 return cast<FCmpInst>(this)->isCommutative();
2353}
2354
2355bool CmpInst::isEquality() {
2356 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2357 return IC->isEquality();
2358 return cast<FCmpInst>(this)->isEquality();
2359}
2360
2361
2362ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2363 switch (pred) {
2364 default:
2365 assert(!"Unknown icmp predicate!");
2366 case ICMP_EQ: return ICMP_NE;
2367 case ICMP_NE: return ICMP_EQ;
2368 case ICMP_UGT: return ICMP_ULE;
2369 case ICMP_ULT: return ICMP_UGE;
2370 case ICMP_UGE: return ICMP_ULT;
2371 case ICMP_ULE: return ICMP_UGT;
2372 case ICMP_SGT: return ICMP_SLE;
2373 case ICMP_SLT: return ICMP_SGE;
2374 case ICMP_SGE: return ICMP_SLT;
2375 case ICMP_SLE: return ICMP_SGT;
2376 }
2377}
2378
2379ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2380 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002381 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002382 case ICMP_EQ: case ICMP_NE:
2383 return pred;
2384 case ICMP_SGT: return ICMP_SLT;
2385 case ICMP_SLT: return ICMP_SGT;
2386 case ICMP_SGE: return ICMP_SLE;
2387 case ICMP_SLE: return ICMP_SGE;
2388 case ICMP_UGT: return ICMP_ULT;
2389 case ICMP_ULT: return ICMP_UGT;
2390 case ICMP_UGE: return ICMP_ULE;
2391 case ICMP_ULE: return ICMP_UGE;
2392 }
2393}
2394
Reid Spencer266e42b2006-12-23 06:05:41 +00002395ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2396 switch (pred) {
2397 default: assert(! "Unknown icmp predicate!");
2398 case ICMP_EQ: case ICMP_NE:
2399 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2400 return pred;
2401 case ICMP_UGT: return ICMP_SGT;
2402 case ICMP_ULT: return ICMP_SLT;
2403 case ICMP_UGE: return ICMP_SGE;
2404 case ICMP_ULE: return ICMP_SLE;
2405 }
2406}
2407
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002408ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2409 switch (pred) {
2410 default: assert(! "Unknown icmp predicate!");
2411 case ICMP_EQ: case ICMP_NE:
2412 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2413 return pred;
2414 case ICMP_SGT: return ICMP_UGT;
2415 case ICMP_SLT: return ICMP_ULT;
2416 case ICMP_SGE: return ICMP_UGE;
2417 case ICMP_SLE: return ICMP_ULE;
2418 }
2419}
2420
Reid Spencer266e42b2006-12-23 06:05:41 +00002421bool ICmpInst::isSignedPredicate(Predicate pred) {
2422 switch (pred) {
2423 default: assert(! "Unknown icmp predicate!");
2424 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2425 return true;
2426 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2427 case ICMP_UGE: case ICMP_ULE:
2428 return false;
2429 }
2430}
2431
Reid Spencer0286bc12007-02-28 22:00:54 +00002432/// Initialize a set of values that all satisfy the condition with C.
2433///
2434ConstantRange
2435ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2436 APInt Lower(C);
2437 APInt Upper(C);
2438 uint32_t BitWidth = C.getBitWidth();
2439 switch (pred) {
2440 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2441 case ICmpInst::ICMP_EQ: Upper++; break;
2442 case ICmpInst::ICMP_NE: Lower++; break;
2443 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2444 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2445 case ICmpInst::ICMP_UGT:
2446 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2447 break;
2448 case ICmpInst::ICMP_SGT:
2449 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2450 break;
2451 case ICmpInst::ICMP_ULE:
2452 Lower = APInt::getMinValue(BitWidth); Upper++;
2453 break;
2454 case ICmpInst::ICMP_SLE:
2455 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2456 break;
2457 case ICmpInst::ICMP_UGE:
2458 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2459 break;
2460 case ICmpInst::ICMP_SGE:
2461 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2462 break;
2463 }
2464 return ConstantRange(Lower, Upper);
2465}
2466
Reid Spencerd9436b62006-11-20 01:22:35 +00002467FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2468 switch (pred) {
2469 default:
2470 assert(!"Unknown icmp predicate!");
2471 case FCMP_OEQ: return FCMP_UNE;
2472 case FCMP_ONE: return FCMP_UEQ;
2473 case FCMP_OGT: return FCMP_ULE;
2474 case FCMP_OLT: return FCMP_UGE;
2475 case FCMP_OGE: return FCMP_ULT;
2476 case FCMP_OLE: return FCMP_UGT;
2477 case FCMP_UEQ: return FCMP_ONE;
2478 case FCMP_UNE: return FCMP_OEQ;
2479 case FCMP_UGT: return FCMP_OLE;
2480 case FCMP_ULT: return FCMP_OGE;
2481 case FCMP_UGE: return FCMP_OLT;
2482 case FCMP_ULE: return FCMP_OGT;
2483 case FCMP_ORD: return FCMP_UNO;
2484 case FCMP_UNO: return FCMP_ORD;
2485 case FCMP_TRUE: return FCMP_FALSE;
2486 case FCMP_FALSE: return FCMP_TRUE;
2487 }
2488}
2489
2490FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2491 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002492 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002493 case FCMP_FALSE: case FCMP_TRUE:
2494 case FCMP_OEQ: case FCMP_ONE:
2495 case FCMP_UEQ: case FCMP_UNE:
2496 case FCMP_ORD: case FCMP_UNO:
2497 return pred;
2498 case FCMP_OGT: return FCMP_OLT;
2499 case FCMP_OLT: return FCMP_OGT;
2500 case FCMP_OGE: return FCMP_OLE;
2501 case FCMP_OLE: return FCMP_OGE;
2502 case FCMP_UGT: return FCMP_ULT;
2503 case FCMP_ULT: return FCMP_UGT;
2504 case FCMP_UGE: return FCMP_ULE;
2505 case FCMP_ULE: return FCMP_UGE;
2506 }
2507}
2508
Reid Spencer266e42b2006-12-23 06:05:41 +00002509bool CmpInst::isUnsigned(unsigned short predicate) {
2510 switch (predicate) {
2511 default: return false;
2512 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2513 case ICmpInst::ICMP_UGE: return true;
2514 }
2515}
2516
2517bool CmpInst::isSigned(unsigned short predicate){
2518 switch (predicate) {
2519 default: return false;
2520 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2521 case ICmpInst::ICMP_SGE: return true;
2522 }
2523}
2524
2525bool CmpInst::isOrdered(unsigned short predicate) {
2526 switch (predicate) {
2527 default: return false;
2528 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2529 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2530 case FCmpInst::FCMP_ORD: return true;
2531 }
2532}
2533
2534bool CmpInst::isUnordered(unsigned short predicate) {
2535 switch (predicate) {
2536 default: return false;
2537 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2538 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2539 case FCmpInst::FCMP_UNO: return true;
2540 }
2541}
2542
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002543//===----------------------------------------------------------------------===//
2544// SwitchInst Implementation
2545//===----------------------------------------------------------------------===//
2546
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002547void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002548 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002549 ReservedSpace = 2+NumCases*2;
2550 NumOperands = 2;
2551 OperandList = new Use[ReservedSpace];
2552
2553 OperandList[0].init(Value, this);
2554 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002555}
2556
Chris Lattner2195fc42007-02-24 00:55:48 +00002557/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2558/// switch on and a default destination. The number of additional cases can
2559/// be specified here to make memory allocation more efficient. This
2560/// constructor can also autoinsert before another instruction.
2561SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2562 Instruction *InsertBefore)
2563 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2564 init(Value, Default, NumCases);
2565}
2566
2567/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2568/// switch on and a default destination. The number of additional cases can
2569/// be specified here to make memory allocation more efficient. This
2570/// constructor also autoinserts at the end of the specified BasicBlock.
2571SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2572 BasicBlock *InsertAtEnd)
2573 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2574 init(Value, Default, NumCases);
2575}
2576
Misha Brukmanb1c93172005-04-21 23:48:37 +00002577SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002578 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2579 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002580 Use *OL = OperandList, *InOL = SI.OperandList;
2581 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2582 OL[i].init(InOL[i], this);
2583 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002584 }
2585}
2586
Gordon Henriksen14a55692007-12-10 02:14:30 +00002587SwitchInst::~SwitchInst() {
2588 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002589}
2590
2591
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002592/// addCase - Add an entry to the switch instruction...
2593///
Chris Lattner47ac1872005-02-24 05:32:09 +00002594void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002595 unsigned OpNo = NumOperands;
2596 if (OpNo+2 > ReservedSpace)
2597 resizeOperands(0); // Get more space!
2598 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002599 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002600 NumOperands = OpNo+2;
2601 OperandList[OpNo].init(OnVal, this);
2602 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002603}
2604
2605/// removeCase - This method removes the specified successor from the switch
2606/// instruction. Note that this cannot be used to remove the default
2607/// destination (successor #0).
2608///
2609void SwitchInst::removeCase(unsigned idx) {
2610 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002611 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2612
2613 unsigned NumOps = getNumOperands();
2614 Use *OL = OperandList;
2615
2616 // Move everything after this operand down.
2617 //
2618 // FIXME: we could just swap with the end of the list, then erase. However,
2619 // client might not expect this to happen. The code as it is thrashes the
2620 // use/def lists, which is kinda lame.
2621 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2622 OL[i-2] = OL[i];
2623 OL[i-2+1] = OL[i+1];
2624 }
2625
2626 // Nuke the last value.
2627 OL[NumOps-2].set(0);
2628 OL[NumOps-2+1].set(0);
2629 NumOperands = NumOps-2;
2630}
2631
2632/// resizeOperands - resize operands - This adjusts the length of the operands
2633/// list according to the following behavior:
2634/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2635/// of operation. This grows the number of ops by 1.5 times.
2636/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2637/// 3. If NumOps == NumOperands, trim the reserved space.
2638///
2639void SwitchInst::resizeOperands(unsigned NumOps) {
2640 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002641 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002642 } else if (NumOps*2 > NumOperands) {
2643 // No resize needed.
2644 if (ReservedSpace >= NumOps) return;
2645 } else if (NumOps == NumOperands) {
2646 if (ReservedSpace == NumOps) return;
2647 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002648 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002649 }
2650
2651 ReservedSpace = NumOps;
2652 Use *NewOps = new Use[NumOps];
2653 Use *OldOps = OperandList;
2654 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2655 NewOps[i].init(OldOps[i], this);
2656 OldOps[i].set(0);
2657 }
2658 delete [] OldOps;
2659 OperandList = NewOps;
2660}
2661
2662
2663BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2664 return getSuccessor(idx);
2665}
2666unsigned SwitchInst::getNumSuccessorsV() const {
2667 return getNumSuccessors();
2668}
2669void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2670 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002671}
Chris Lattnerf22be932004-10-15 23:52:53 +00002672
Devang Patel295711f2008-02-19 22:15:16 +00002673//===----------------------------------------------------------------------===//
2674// GetResultInst Implementation
2675//===----------------------------------------------------------------------===//
2676
Devang Patel666d4512008-02-20 19:10:47 +00002677GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002678 const std::string &Name,
2679 Instruction *InsertBef)
Devang Pateldcad9da2008-02-20 19:26:55 +00002680 : Instruction(cast<StructType>(Aggregate->getType())->getElementType(Index),
Devang Patel666d4512008-02-20 19:10:47 +00002681 GetResult, &Aggr, 1, InsertBef) {
2682 assert(isValidOperands(Aggregate, Index) && "Invalid GetResultInst operands!");
2683 Aggr.init(Aggregate, this);
2684 Idx = Index;
Devang Patel295711f2008-02-19 22:15:16 +00002685 setName(Name);
2686}
2687
Devang Patel666d4512008-02-20 19:10:47 +00002688bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2689 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002690 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002691
Devang Patelcf193b82008-02-20 19:39:41 +00002692 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2693 unsigned NumElements = STy->getNumElements();
2694 if (Index >= NumElements)
2695 return false;
2696
2697 // getresult aggregate value's element types are restricted to
2698 // avoid nested aggregates.
2699 for (unsigned i = 0; i < NumElements; ++i)
2700 if (!STy->getElementType(i)->isFirstClassType())
2701 return false;
2702
2703 // Otherwise, Aggregate is valid.
2704 return true;
2705 }
Devang Patel666d4512008-02-20 19:10:47 +00002706 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002707}
2708
Chris Lattnerf22be932004-10-15 23:52:53 +00002709// Define these methods here so vtables don't get emitted into every translation
2710// unit that uses these classes.
2711
2712GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002713 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002714}
2715
2716BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002717 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002718}
2719
Chris Lattner0b490b02007-08-24 20:48:18 +00002720FCmpInst* FCmpInst::clone() const {
2721 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2722}
2723ICmpInst* ICmpInst::clone() const {
2724 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002725}
2726
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002727MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2728AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2729FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2730LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2731StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2732CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2733CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2734CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2735CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2736CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2737CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2738CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2739CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2740CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2741CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2742CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2743CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greife9ecc682008-04-06 20:25:17 +00002744CallInst *CallInst::clone() const { return new(getNumOperands()) CallInst(*this); }
2745SelectInst *SelectInst::clone() const { return new(getNumOperands()) SelectInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002746VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2747
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002748ExtractElementInst *ExtractElementInst::clone() const {
2749 return new ExtractElementInst(*this);
2750}
2751InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002752 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002753}
2754ShuffleVectorInst *ShuffleVectorInst::clone() const {
2755 return new ShuffleVectorInst(*this);
2756}
Chris Lattnerf22be932004-10-15 23:52:53 +00002757PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greife9ecc682008-04-06 20:25:17 +00002758ReturnInst *ReturnInst::clone() const { return new(getNumOperands()) ReturnInst(*this); }
2759BranchInst *BranchInst::clone() const { return new(getNumOperands()) BranchInst(*this); }
2760SwitchInst *SwitchInst::clone() const { return new(getNumOperands()) SwitchInst(*this); }
2761InvokeInst *InvokeInst::clone() const { return new(getNumOperands()) InvokeInst(*this); }
Chris Lattnerf22be932004-10-15 23:52:53 +00002762UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002763UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002764GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }