blob: df5a2fc0cfcb49050dd4e488ef3739e95490f8d2 [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),
Chris Lattner2195fc42007-02-24 00:55:48 +0000680 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000681 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
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),
Chris Lattner2195fc42007-02-24 00:55:48 +0000690 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000691 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
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
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000700bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000701 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
702 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000703 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000704}
705
706const Type *AllocationInst::getAllocatedType() const {
707 return getType()->getElementType();
708}
709
710AllocaInst::AllocaInst(const AllocaInst &AI)
711 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000712 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000713}
714
715MallocInst::MallocInst(const MallocInst &MI)
716 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000717 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000718}
719
720//===----------------------------------------------------------------------===//
721// FreeInst Implementation
722//===----------------------------------------------------------------------===//
723
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000724void FreeInst::AssertOK() {
725 assert(isa<PointerType>(getOperand(0)->getType()) &&
726 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000727}
728
729FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000730 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000731 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732}
733
734FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000735 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000736 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000737}
738
739
740//===----------------------------------------------------------------------===//
741// LoadInst Implementation
742//===----------------------------------------------------------------------===//
743
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000744void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000745 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000746 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000747}
748
749LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000750 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000751 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000752 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000753 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000754 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000755 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000756}
757
758LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000759 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000760 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000761 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000762 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000763 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000764 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000765}
766
767LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
768 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000769 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000770 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000771 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000772 setAlignment(0);
773 AssertOK();
774 setName(Name);
775}
776
777LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
778 unsigned Align, Instruction *InsertBef)
779 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
780 Load, Ptr, InsertBef) {
781 setVolatile(isVolatile);
782 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000783 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000784 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000785}
786
Dan Gohman68659282007-07-18 20:51:11 +0000787LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
788 unsigned Align, BasicBlock *InsertAE)
789 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
790 Load, Ptr, InsertAE) {
791 setVolatile(isVolatile);
792 setAlignment(Align);
793 AssertOK();
794 setName(Name);
795}
796
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000797LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
798 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000799 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000800 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000801 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000802 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000803 AssertOK();
804 setName(Name);
805}
806
807
808
809LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000810 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
811 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000812 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000813 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000814 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000815 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000816}
817
818LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000819 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
820 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000821 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000822 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000823 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000824 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000825}
826
827LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
828 Instruction *InsertBef)
829: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000830 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000831 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000832 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000833 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000834 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000835}
836
837LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
838 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000839 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
840 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000841 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000842 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000843 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000844 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000845}
846
Christopher Lamb84485702007-04-22 19:24:39 +0000847void LoadInst::setAlignment(unsigned Align) {
848 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
849 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
850}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000851
852//===----------------------------------------------------------------------===//
853// StoreInst Implementation
854//===----------------------------------------------------------------------===//
855
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000856void StoreInst::AssertOK() {
857 assert(isa<PointerType>(getOperand(1)->getType()) &&
858 "Ptr must have pointer type!");
859 assert(getOperand(0)->getType() ==
860 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000861 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000862}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000863
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000864
865StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000866 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000867 Ops[0].init(val, this);
868 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000869 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000870 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000871 AssertOK();
872}
873
874StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000875 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000876 Ops[0].init(val, this);
877 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000878 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000879 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000880 AssertOK();
881}
882
Misha Brukmanb1c93172005-04-21 23:48:37 +0000883StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000884 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000885 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000886 Ops[0].init(val, this);
887 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000888 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000889 setAlignment(0);
890 AssertOK();
891}
892
893StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
894 unsigned Align, Instruction *InsertBefore)
895 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
896 Ops[0].init(val, this);
897 Ops[1].init(addr, this);
898 setVolatile(isVolatile);
899 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000900 AssertOK();
901}
902
Misha Brukmanb1c93172005-04-21 23:48:37 +0000903StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000904 unsigned Align, BasicBlock *InsertAtEnd)
905 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
906 Ops[0].init(val, this);
907 Ops[1].init(addr, this);
908 setVolatile(isVolatile);
909 setAlignment(Align);
910 AssertOK();
911}
912
913StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000914 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000915 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000916 Ops[0].init(val, this);
917 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000918 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000919 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000920 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000921}
922
Christopher Lamb84485702007-04-22 19:24:39 +0000923void StoreInst::setAlignment(unsigned Align) {
924 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
925 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
926}
927
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000928//===----------------------------------------------------------------------===//
929// GetElementPtrInst Implementation
930//===----------------------------------------------------------------------===//
931
Christopher Lambedf07882007-12-17 01:12:55 +0000932static unsigned retrieveAddrSpace(const Value *Val) {
933 return cast<PointerType>(Val->getType())->getAddressSpace();
934}
935
Chris Lattner79807c3d2007-01-31 19:47:18 +0000936void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
937 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000938 Use *OL = OperandList = new Use[NumOperands];
939 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000940
Chris Lattner79807c3d2007-01-31 19:47:18 +0000941 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000942 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000943}
944
Chris Lattner82981202005-05-03 05:43:30 +0000945void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
946 NumOperands = 2;
947 Use *OL = OperandList = new Use[2];
948 OL[0].init(Ptr, this);
949 OL[1].init(Idx, this);
950}
951
Chris Lattner82981202005-05-03 05:43:30 +0000952GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
953 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000954 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +0000955 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +0000956 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +0000957 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000958 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000959}
960
961GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
962 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000963 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +0000964 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +0000965 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +0000966 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000967 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000968}
969
Gordon Henriksen14a55692007-12-10 02:14:30 +0000970GetElementPtrInst::~GetElementPtrInst() {
971 delete[] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000972}
973
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000974// getIndexedType - Returns the type of the element that would be loaded with
975// a load instruction with the specified parameters.
976//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000977// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000978// pointer type.
979//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000980const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000981 Value* const *Idxs,
982 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000983 bool AllowCompositeLeaf) {
984 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
985
986 // Handle the special case of the empty set index set...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000987 if (NumIdx == 0) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000988 if (AllowCompositeLeaf ||
989 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
990 return cast<PointerType>(Ptr)->getElementType();
991 else
992 return 0;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000993 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000994
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000995 unsigned CurIdx = 0;
996 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000997 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000998 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
999 return 0; // Can't load a whole structure or array!?!?
1000 }
1001
Chris Lattner302116a2007-01-31 04:40:28 +00001002 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001003 if (isa<PointerType>(CT) && CurIdx != 1)
1004 return 0; // Can only index into pointer types at the first index!
1005 if (!CT->indexValid(Index)) return 0;
1006 Ptr = CT->getTypeAtIndex(Index);
1007
1008 // If the new type forwards to another type, then it is in the middle
1009 // of being refined to another type (and hence, may have dropped all
1010 // references to what it was using before). So, use the new forwarded
1011 // type.
1012 if (const Type * Ty = Ptr->getForwardedType()) {
1013 Ptr = Ty;
1014 }
1015 }
Chris Lattner302116a2007-01-31 04:40:28 +00001016 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001017}
1018
Chris Lattner82981202005-05-03 05:43:30 +00001019const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1020 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1021 if (!PTy) return 0; // Type isn't a pointer type!
1022
1023 // Check the pointer index.
1024 if (!PTy->indexValid(Idx)) return 0;
1025
Chris Lattnerc2233332005-05-03 16:44:45 +00001026 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001027}
1028
Chris Lattner45f15572007-04-14 00:12:57 +00001029
1030/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1031/// zeros. If so, the result pointer and the first operand have the same
1032/// value, just potentially different types.
1033bool GetElementPtrInst::hasAllZeroIndices() const {
1034 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1035 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1036 if (!CI->isZero()) return false;
1037 } else {
1038 return false;
1039 }
1040 }
1041 return true;
1042}
1043
Chris Lattner27058292007-04-27 20:35:56 +00001044/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1045/// constant integers. If so, the result pointer and the first operand have
1046/// a constant offset between them.
1047bool GetElementPtrInst::hasAllConstantIndices() const {
1048 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1049 if (!isa<ConstantInt>(getOperand(i)))
1050 return false;
1051 }
1052 return true;
1053}
1054
Chris Lattner45f15572007-04-14 00:12:57 +00001055
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001056//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001057// ExtractElementInst Implementation
1058//===----------------------------------------------------------------------===//
1059
1060ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001061 const std::string &Name,
1062 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001063 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001064 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001065 assert(isValidOperands(Val, Index) &&
1066 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001067 Ops[0].init(Val, this);
1068 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001069 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001070}
1071
Chris Lattner65511ff2006-10-05 06:24:58 +00001072ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1073 const std::string &Name,
1074 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001075 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001076 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001077 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001078 assert(isValidOperands(Val, Index) &&
1079 "Invalid extractelement instruction operands!");
1080 Ops[0].init(Val, this);
1081 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001082 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001083}
1084
1085
Robert Bocchino23004482006-01-10 19:05:34 +00001086ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001087 const std::string &Name,
1088 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001089 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001090 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001091 assert(isValidOperands(Val, Index) &&
1092 "Invalid extractelement instruction operands!");
1093
Robert Bocchino23004482006-01-10 19:05:34 +00001094 Ops[0].init(Val, this);
1095 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001096 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001097}
1098
Chris Lattner65511ff2006-10-05 06:24:58 +00001099ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1100 const std::string &Name,
1101 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001102 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001103 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001104 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001105 assert(isValidOperands(Val, Index) &&
1106 "Invalid extractelement instruction operands!");
1107
1108 Ops[0].init(Val, this);
1109 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001110 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001111}
1112
1113
Chris Lattner54865b32006-04-08 04:05:48 +00001114bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001115 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001116 return false;
1117 return true;
1118}
1119
1120
Robert Bocchino23004482006-01-10 19:05:34 +00001121//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001122// InsertElementInst Implementation
1123//===----------------------------------------------------------------------===//
1124
Chris Lattner0875d942006-04-14 22:20:32 +00001125InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1126 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1127 Ops[0].init(IE.Ops[0], this);
1128 Ops[1].init(IE.Ops[1], this);
1129 Ops[2].init(IE.Ops[2], this);
1130}
Chris Lattner54865b32006-04-08 04:05:48 +00001131InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001132 const std::string &Name,
1133 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001134 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001135 assert(isValidOperands(Vec, Elt, Index) &&
1136 "Invalid insertelement instruction operands!");
1137 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001138 Ops[1].init(Elt, this);
1139 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001140 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001141}
1142
Chris Lattner65511ff2006-10-05 06:24:58 +00001143InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1144 const std::string &Name,
1145 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001146 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001147 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001148 assert(isValidOperands(Vec, Elt, Index) &&
1149 "Invalid insertelement instruction operands!");
1150 Ops[0].init(Vec, this);
1151 Ops[1].init(Elt, this);
1152 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001153 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001154}
1155
1156
Chris Lattner54865b32006-04-08 04:05:48 +00001157InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001158 const std::string &Name,
1159 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001160 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001161 assert(isValidOperands(Vec, Elt, Index) &&
1162 "Invalid insertelement instruction operands!");
1163
1164 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001165 Ops[1].init(Elt, this);
1166 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001167 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001168}
1169
Chris Lattner65511ff2006-10-05 06:24:58 +00001170InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1171 const std::string &Name,
1172 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001173: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001174 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001175 assert(isValidOperands(Vec, Elt, Index) &&
1176 "Invalid insertelement instruction operands!");
1177
1178 Ops[0].init(Vec, this);
1179 Ops[1].init(Elt, this);
1180 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001181 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001182}
1183
Chris Lattner54865b32006-04-08 04:05:48 +00001184bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1185 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001186 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001187 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001188
Reid Spencerd84d35b2007-02-15 02:26:10 +00001189 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001190 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001191
Reid Spencer8d9336d2006-12-31 05:26:44 +00001192 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001193 return false; // Third operand of insertelement must be uint.
1194 return true;
1195}
1196
1197
Robert Bocchinoca27f032006-01-17 20:07:22 +00001198//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001199// ShuffleVectorInst Implementation
1200//===----------------------------------------------------------------------===//
1201
Chris Lattner0875d942006-04-14 22:20:32 +00001202ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1203 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1204 Ops[0].init(SV.Ops[0], this);
1205 Ops[1].init(SV.Ops[1], this);
1206 Ops[2].init(SV.Ops[2], this);
1207}
1208
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001209ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1210 const std::string &Name,
1211 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001212 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001213 assert(isValidOperands(V1, V2, Mask) &&
1214 "Invalid shuffle vector instruction operands!");
1215 Ops[0].init(V1, this);
1216 Ops[1].init(V2, this);
1217 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001218 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001219}
1220
1221ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1222 const std::string &Name,
1223 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001224 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001225 assert(isValidOperands(V1, V2, Mask) &&
1226 "Invalid shuffle vector instruction operands!");
1227
1228 Ops[0].init(V1, this);
1229 Ops[1].init(V2, this);
1230 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001231 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001232}
1233
1234bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1235 const Value *Mask) {
Chris Lattnerf724e342008-03-02 05:28:33 +00001236 if (!isa<VectorType>(V1->getType()) ||
1237 V1->getType() != V2->getType())
1238 return false;
1239
1240 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1241 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1242 MaskTy->getElementType() != Type::Int32Ty ||
1243 MaskTy->getNumElements() !=
1244 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001245 return false;
1246 return true;
1247}
1248
Chris Lattnerf724e342008-03-02 05:28:33 +00001249/// getMaskValue - Return the index from the shuffle mask for the specified
1250/// output result. This is either -1 if the element is undef or a number less
1251/// than 2*numelements.
1252int ShuffleVectorInst::getMaskValue(unsigned i) const {
1253 const Constant *Mask = cast<Constant>(getOperand(2));
1254 if (isa<UndefValue>(Mask)) return -1;
1255 if (isa<ConstantAggregateZero>(Mask)) return 0;
1256 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1257 assert(i < MaskCV->getNumOperands() && "Index out of range");
1258
1259 if (isa<UndefValue>(MaskCV->getOperand(i)))
1260 return -1;
1261 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1262}
1263
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001264
1265//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001266// BinaryOperator Class
1267//===----------------------------------------------------------------------===//
1268
Chris Lattner2195fc42007-02-24 00:55:48 +00001269BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1270 const Type *Ty, const std::string &Name,
1271 Instruction *InsertBefore)
1272 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1273 Ops[0].init(S1, this);
1274 Ops[1].init(S2, this);
1275 init(iType);
1276 setName(Name);
1277}
1278
1279BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1280 const Type *Ty, const std::string &Name,
1281 BasicBlock *InsertAtEnd)
1282 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1283 Ops[0].init(S1, this);
1284 Ops[1].init(S2, this);
1285 init(iType);
1286 setName(Name);
1287}
1288
1289
1290void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001291 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001292 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001293 assert(LHS->getType() == RHS->getType() &&
1294 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001295#ifndef NDEBUG
1296 switch (iType) {
1297 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001298 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001299 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001300 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001301 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001302 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001303 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001304 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001305 case UDiv:
1306 case SDiv:
1307 assert(getType() == LHS->getType() &&
1308 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001309 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1310 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001311 "Incorrect operand type (not integer) for S/UDIV");
1312 break;
1313 case FDiv:
1314 assert(getType() == LHS->getType() &&
1315 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001316 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1317 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001318 && "Incorrect operand type (not floating point) for FDIV");
1319 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001320 case URem:
1321 case SRem:
1322 assert(getType() == LHS->getType() &&
1323 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001324 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1325 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001326 "Incorrect operand type (not integer) for S/UREM");
1327 break;
1328 case FRem:
1329 assert(getType() == LHS->getType() &&
1330 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001331 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1332 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001333 && "Incorrect operand type (not floating point) for FREM");
1334 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001335 case Shl:
1336 case LShr:
1337 case AShr:
1338 assert(getType() == LHS->getType() &&
1339 "Shift operation should return same type as operands!");
1340 assert(getType()->isInteger() &&
1341 "Shift operation requires integer operands");
1342 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001343 case And: case Or:
1344 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001345 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001346 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001347 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001348 (isa<VectorType>(getType()) &&
1349 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001350 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001351 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001352 default:
1353 break;
1354 }
1355#endif
1356}
1357
1358BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001359 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001360 Instruction *InsertBefore) {
1361 assert(S1->getType() == S2->getType() &&
1362 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001363 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001364}
1365
1366BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001367 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001368 BasicBlock *InsertAtEnd) {
1369 BinaryOperator *Res = create(Op, S1, S2, Name);
1370 InsertAtEnd->getInstList().push_back(Res);
1371 return Res;
1372}
1373
1374BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1375 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001376 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1377 return new BinaryOperator(Instruction::Sub,
1378 zero, Op,
1379 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001380}
1381
1382BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1383 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001384 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1385 return new BinaryOperator(Instruction::Sub,
1386 zero, Op,
1387 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001388}
1389
1390BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1391 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001392 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001393 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001394 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001395 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001396 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001397 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001398 }
1399
1400 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001401 Op->getType(), Name, InsertBefore);
1402}
1403
1404BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1405 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001406 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001407 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001408 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001409 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001410 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001411 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001412 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001413 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001414 }
1415
1416 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001417 Op->getType(), Name, InsertAtEnd);
1418}
1419
1420
1421// isConstantAllOnes - Helper function for several functions below
1422static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001423 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1424 return CI->isAllOnesValue();
1425 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1426 return CV->isAllOnesValue();
1427 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001428}
1429
1430bool BinaryOperator::isNeg(const Value *V) {
1431 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1432 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001433 return Bop->getOperand(0) ==
1434 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001435 return false;
1436}
1437
1438bool BinaryOperator::isNot(const Value *V) {
1439 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1440 return (Bop->getOpcode() == Instruction::Xor &&
1441 (isConstantAllOnes(Bop->getOperand(1)) ||
1442 isConstantAllOnes(Bop->getOperand(0))));
1443 return false;
1444}
1445
Chris Lattner2c7d1772005-04-24 07:28:37 +00001446Value *BinaryOperator::getNegArgument(Value *BinOp) {
1447 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1448 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001449}
1450
Chris Lattner2c7d1772005-04-24 07:28:37 +00001451const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1452 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001453}
1454
Chris Lattner2c7d1772005-04-24 07:28:37 +00001455Value *BinaryOperator::getNotArgument(Value *BinOp) {
1456 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1457 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1458 Value *Op0 = BO->getOperand(0);
1459 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001460 if (isConstantAllOnes(Op0)) return Op1;
1461
1462 assert(isConstantAllOnes(Op1));
1463 return Op0;
1464}
1465
Chris Lattner2c7d1772005-04-24 07:28:37 +00001466const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1467 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001468}
1469
1470
1471// swapOperands - Exchange the two operands to this instruction. This
1472// instruction is safe to use on any binary instruction and does not
1473// modify the semantics of the instruction. If the instruction is
1474// order dependent (SetLT f.e.) the opcode is changed.
1475//
1476bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001477 if (!isCommutative())
1478 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001479 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001480 return false;
1481}
1482
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001483//===----------------------------------------------------------------------===//
1484// CastInst Class
1485//===----------------------------------------------------------------------===//
1486
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001487// Just determine if this cast only deals with integral->integral conversion.
1488bool CastInst::isIntegerCast() const {
1489 switch (getOpcode()) {
1490 default: return false;
1491 case Instruction::ZExt:
1492 case Instruction::SExt:
1493 case Instruction::Trunc:
1494 return true;
1495 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001496 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001497 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001498}
1499
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001500bool CastInst::isLosslessCast() const {
1501 // Only BitCast can be lossless, exit fast if we're not BitCast
1502 if (getOpcode() != Instruction::BitCast)
1503 return false;
1504
1505 // Identity cast is always lossless
1506 const Type* SrcTy = getOperand(0)->getType();
1507 const Type* DstTy = getType();
1508 if (SrcTy == DstTy)
1509 return true;
1510
Reid Spencer8d9336d2006-12-31 05:26:44 +00001511 // Pointer to pointer is always lossless.
1512 if (isa<PointerType>(SrcTy))
1513 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001514 return false; // Other types have no identity values
1515}
1516
1517/// This function determines if the CastInst does not require any bits to be
1518/// changed in order to effect the cast. Essentially, it identifies cases where
1519/// no code gen is necessary for the cast, hence the name no-op cast. For
1520/// example, the following are all no-op casts:
1521/// # bitcast uint %X, int
1522/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001523/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001524/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1525/// @brief Determine if a cast is a no-op.
1526bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1527 switch (getOpcode()) {
1528 default:
1529 assert(!"Invalid CastOp");
1530 case Instruction::Trunc:
1531 case Instruction::ZExt:
1532 case Instruction::SExt:
1533 case Instruction::FPTrunc:
1534 case Instruction::FPExt:
1535 case Instruction::UIToFP:
1536 case Instruction::SIToFP:
1537 case Instruction::FPToUI:
1538 case Instruction::FPToSI:
1539 return false; // These always modify bits
1540 case Instruction::BitCast:
1541 return true; // BitCast never modifies bits.
1542 case Instruction::PtrToInt:
1543 return IntPtrTy->getPrimitiveSizeInBits() ==
1544 getType()->getPrimitiveSizeInBits();
1545 case Instruction::IntToPtr:
1546 return IntPtrTy->getPrimitiveSizeInBits() ==
1547 getOperand(0)->getType()->getPrimitiveSizeInBits();
1548 }
1549}
1550
1551/// This function determines if a pair of casts can be eliminated and what
1552/// opcode should be used in the elimination. This assumes that there are two
1553/// instructions like this:
1554/// * %F = firstOpcode SrcTy %x to MidTy
1555/// * %S = secondOpcode MidTy %F to DstTy
1556/// The function returns a resultOpcode so these two casts can be replaced with:
1557/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1558/// If no such cast is permited, the function returns 0.
1559unsigned CastInst::isEliminableCastPair(
1560 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1561 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1562{
1563 // Define the 144 possibilities for these two cast instructions. The values
1564 // in this matrix determine what to do in a given situation and select the
1565 // case in the switch below. The rows correspond to firstOp, the columns
1566 // correspond to secondOp. In looking at the table below, keep in mind
1567 // the following cast properties:
1568 //
1569 // Size Compare Source Destination
1570 // Operator Src ? Size Type Sign Type Sign
1571 // -------- ------------ ------------------- ---------------------
1572 // TRUNC > Integer Any Integral Any
1573 // ZEXT < Integral Unsigned Integer Any
1574 // SEXT < Integral Signed Integer Any
1575 // FPTOUI n/a FloatPt n/a Integral Unsigned
1576 // FPTOSI n/a FloatPt n/a Integral Signed
1577 // UITOFP n/a Integral Unsigned FloatPt n/a
1578 // SITOFP n/a Integral Signed FloatPt n/a
1579 // FPTRUNC > FloatPt n/a FloatPt n/a
1580 // FPEXT < FloatPt n/a FloatPt n/a
1581 // PTRTOINT n/a Pointer n/a Integral Unsigned
1582 // INTTOPTR n/a Integral Unsigned Pointer n/a
1583 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001584 //
1585 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1586 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1587 // into "fptoui double to ulong", but this loses information about the range
1588 // of the produced value (we no longer know the top-part is all zeros).
1589 // Further this conversion is often much more expensive for typical hardware,
1590 // and causes issues when building libgcc. We disallow fptosi+sext for the
1591 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001592 const unsigned numCastOps =
1593 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1594 static const uint8_t CastResults[numCastOps][numCastOps] = {
1595 // T F F U S F F P I B -+
1596 // R Z S P P I I T P 2 N T |
1597 // U E E 2 2 2 2 R E I T C +- secondOp
1598 // N X X U S F F N X N 2 V |
1599 // C T T I I P P C T T P T -+
1600 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1601 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1602 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001603 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1604 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001605 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1606 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1607 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1608 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1609 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1610 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1611 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1612 };
1613
1614 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1615 [secondOp-Instruction::CastOpsBegin];
1616 switch (ElimCase) {
1617 case 0:
1618 // categorically disallowed
1619 return 0;
1620 case 1:
1621 // allowed, use first cast's opcode
1622 return firstOp;
1623 case 2:
1624 // allowed, use second cast's opcode
1625 return secondOp;
1626 case 3:
1627 // no-op cast in second op implies firstOp as long as the DestTy
1628 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001629 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001630 return firstOp;
1631 return 0;
1632 case 4:
1633 // no-op cast in second op implies firstOp as long as the DestTy
1634 // is floating point
1635 if (DstTy->isFloatingPoint())
1636 return firstOp;
1637 return 0;
1638 case 5:
1639 // no-op cast in first op implies secondOp as long as the SrcTy
1640 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001641 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001642 return secondOp;
1643 return 0;
1644 case 6:
1645 // no-op cast in first op implies secondOp as long as the SrcTy
1646 // is a floating point
1647 if (SrcTy->isFloatingPoint())
1648 return secondOp;
1649 return 0;
1650 case 7: {
1651 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1652 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1653 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1654 if (MidSize >= PtrSize)
1655 return Instruction::BitCast;
1656 return 0;
1657 }
1658 case 8: {
1659 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1660 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1661 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1662 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1663 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1664 if (SrcSize == DstSize)
1665 return Instruction::BitCast;
1666 else if (SrcSize < DstSize)
1667 return firstOp;
1668 return secondOp;
1669 }
1670 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1671 return Instruction::ZExt;
1672 case 10:
1673 // fpext followed by ftrunc is allowed if the bit size returned to is
1674 // the same as the original, in which case its just a bitcast
1675 if (SrcTy == DstTy)
1676 return Instruction::BitCast;
1677 return 0; // If the types are not the same we can't eliminate it.
1678 case 11:
1679 // bitcast followed by ptrtoint is allowed as long as the bitcast
1680 // is a pointer to pointer cast.
1681 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1682 return secondOp;
1683 return 0;
1684 case 12:
1685 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1686 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1687 return firstOp;
1688 return 0;
1689 case 13: {
1690 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1691 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1692 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1693 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1694 if (SrcSize <= PtrSize && SrcSize == DstSize)
1695 return Instruction::BitCast;
1696 return 0;
1697 }
1698 case 99:
1699 // cast combination can't happen (error in input). This is for all cases
1700 // where the MidTy is not the same for the two cast instructions.
1701 assert(!"Invalid Cast Combination");
1702 return 0;
1703 default:
1704 assert(!"Error in CastResults table!!!");
1705 return 0;
1706 }
1707 return 0;
1708}
1709
1710CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1711 const std::string &Name, Instruction *InsertBefore) {
1712 // Construct and return the appropriate CastInst subclass
1713 switch (op) {
1714 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1715 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1716 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1717 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1718 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1719 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1720 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1721 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1722 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1723 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1724 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1725 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1726 default:
1727 assert(!"Invalid opcode provided");
1728 }
1729 return 0;
1730}
1731
1732CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1733 const std::string &Name, BasicBlock *InsertAtEnd) {
1734 // Construct and return the appropriate CastInst subclass
1735 switch (op) {
1736 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1737 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1738 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1739 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1740 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1741 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1742 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1743 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1744 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1745 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1746 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1747 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1748 default:
1749 assert(!"Invalid opcode provided");
1750 }
1751 return 0;
1752}
1753
Reid Spencer5c140882006-12-04 20:17:56 +00001754CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1755 const std::string &Name,
1756 Instruction *InsertBefore) {
1757 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1758 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1759 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1760}
1761
1762CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1763 const std::string &Name,
1764 BasicBlock *InsertAtEnd) {
1765 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1766 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1767 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1768}
1769
1770CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1771 const std::string &Name,
1772 Instruction *InsertBefore) {
1773 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1774 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1775 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1776}
1777
1778CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1779 const std::string &Name,
1780 BasicBlock *InsertAtEnd) {
1781 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1782 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1783 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1784}
1785
1786CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1787 const std::string &Name,
1788 Instruction *InsertBefore) {
1789 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1790 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1791 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1792}
1793
1794CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1795 const std::string &Name,
1796 BasicBlock *InsertAtEnd) {
1797 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1798 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1799 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1800}
1801
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001802CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1803 const std::string &Name,
1804 BasicBlock *InsertAtEnd) {
1805 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001806 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001807 "Invalid cast");
1808
Chris Lattner03c49532007-01-15 02:27:26 +00001809 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001810 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1811 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1812}
1813
1814/// @brief Create a BitCast or a PtrToInt cast instruction
1815CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1816 const std::string &Name,
1817 Instruction *InsertBefore) {
1818 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001819 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001820 "Invalid cast");
1821
Chris Lattner03c49532007-01-15 02:27:26 +00001822 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001823 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1824 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1825}
1826
Reid Spencer7e933472006-12-12 00:49:44 +00001827CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1828 bool isSigned, const std::string &Name,
1829 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001830 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001831 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1832 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1833 Instruction::CastOps opcode =
1834 (SrcBits == DstBits ? Instruction::BitCast :
1835 (SrcBits > DstBits ? Instruction::Trunc :
1836 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1837 return create(opcode, C, Ty, Name, InsertBefore);
1838}
1839
1840CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1841 bool isSigned, const std::string &Name,
1842 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001843 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001844 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1845 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1846 Instruction::CastOps opcode =
1847 (SrcBits == DstBits ? Instruction::BitCast :
1848 (SrcBits > DstBits ? Instruction::Trunc :
1849 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1850 return create(opcode, C, Ty, Name, InsertAtEnd);
1851}
1852
1853CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1854 const std::string &Name,
1855 Instruction *InsertBefore) {
1856 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1857 "Invalid cast");
1858 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1859 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1860 Instruction::CastOps opcode =
1861 (SrcBits == DstBits ? Instruction::BitCast :
1862 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1863 return create(opcode, C, Ty, Name, InsertBefore);
1864}
1865
1866CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1867 const std::string &Name,
1868 BasicBlock *InsertAtEnd) {
1869 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1870 "Invalid cast");
1871 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1872 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1873 Instruction::CastOps opcode =
1874 (SrcBits == DstBits ? Instruction::BitCast :
1875 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1876 return create(opcode, C, Ty, Name, InsertAtEnd);
1877}
1878
Duncan Sands55e50902008-01-06 10:12:28 +00001879// Check whether it is valid to call getCastOpcode for these types.
1880// This routine must be kept in sync with getCastOpcode.
1881bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
1882 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
1883 return false;
1884
1885 if (SrcTy == DestTy)
1886 return true;
1887
1888 // Get the bit sizes, we'll need these
1889 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1890 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1891
1892 // Run through the possibilities ...
1893 if (DestTy->isInteger()) { // Casting to integral
1894 if (SrcTy->isInteger()) { // Casting from integral
1895 return true;
1896 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1897 return true;
1898 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1899 // Casting from vector
1900 return DestBits == PTy->getBitWidth();
1901 } else { // Casting from something else
1902 return isa<PointerType>(SrcTy);
1903 }
1904 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
1905 if (SrcTy->isInteger()) { // Casting from integral
1906 return true;
1907 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1908 return true;
1909 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1910 // Casting from vector
1911 return DestBits == PTy->getBitWidth();
1912 } else { // Casting from something else
1913 return false;
1914 }
1915 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1916 // Casting to vector
1917 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
1918 // Casting from vector
1919 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
1920 } else { // Casting from something else
1921 return DestPTy->getBitWidth() == SrcBits;
1922 }
1923 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
1924 if (isa<PointerType>(SrcTy)) { // Casting from pointer
1925 return true;
1926 } else if (SrcTy->isInteger()) { // Casting from integral
1927 return true;
1928 } else { // Casting from something else
1929 return false;
1930 }
1931 } else { // Casting to something else
1932 return false;
1933 }
1934}
1935
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001936// Provide a way to get a "cast" where the cast opcode is inferred from the
1937// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001938// logic in the castIsValid function below. This axiom should hold:
1939// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1940// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001941// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00001942// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001943Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001944CastInst::getCastOpcode(
1945 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001946 // Get the bit sizes, we'll need these
1947 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00001948 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1949 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001950
Duncan Sands55e50902008-01-06 10:12:28 +00001951 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
1952 "Only first class types are castable!");
1953
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001954 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001955 if (DestTy->isInteger()) { // Casting to integral
1956 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001957 if (DestBits < SrcBits)
1958 return Trunc; // int -> smaller int
1959 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001960 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001961 return SExt; // signed -> SEXT
1962 else
1963 return ZExt; // unsigned -> ZEXT
1964 } else {
1965 return BitCast; // Same size, No-op cast
1966 }
1967 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001968 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001969 return FPToSI; // FP -> sint
1970 else
1971 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001972 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001973 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001974 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001975 return BitCast; // Same size, no-op cast
1976 } else {
1977 assert(isa<PointerType>(SrcTy) &&
1978 "Casting from a value that is not first-class type");
1979 return PtrToInt; // ptr -> int
1980 }
1981 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001982 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001983 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001984 return SIToFP; // sint -> FP
1985 else
1986 return UIToFP; // uint -> FP
1987 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1988 if (DestBits < SrcBits) {
1989 return FPTrunc; // FP -> smaller FP
1990 } else if (DestBits > SrcBits) {
1991 return FPExt; // FP -> larger FP
1992 } else {
1993 return BitCast; // same size, no-op cast
1994 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001995 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001996 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001997 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001998 return BitCast; // same size, no-op cast
1999 } else {
2000 assert(0 && "Casting pointer or non-first class to float");
2001 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002002 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2003 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002004 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002005 "Casting vector to vector of different widths");
2006 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002007 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002008 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002009 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002010 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002011 }
2012 } else if (isa<PointerType>(DestTy)) {
2013 if (isa<PointerType>(SrcTy)) {
2014 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002015 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002016 return IntToPtr; // int -> ptr
2017 } else {
2018 assert(!"Casting pointer to other than pointer or int");
2019 }
2020 } else {
2021 assert(!"Casting to type that is not first-class");
2022 }
2023
2024 // If we fall through to here we probably hit an assertion cast above
2025 // and assertions are not turned on. Anything we return is an error, so
2026 // BitCast is as good a choice as any.
2027 return BitCast;
2028}
2029
2030//===----------------------------------------------------------------------===//
2031// CastInst SubClass Constructors
2032//===----------------------------------------------------------------------===//
2033
2034/// Check that the construction parameters for a CastInst are correct. This
2035/// could be broken out into the separate constructors but it is useful to have
2036/// it in one place and to eliminate the redundant code for getting the sizes
2037/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002038bool
2039CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002040
2041 // Check for type sanity on the arguments
2042 const Type *SrcTy = S->getType();
2043 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2044 return false;
2045
2046 // Get the size of the types in bits, we'll need this later
2047 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2048 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2049
2050 // Switch on the opcode provided
2051 switch (op) {
2052 default: return false; // This is an input error
2053 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002054 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002055 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002056 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002057 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002058 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002059 case Instruction::FPTrunc:
2060 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2061 SrcBitSize > DstBitSize;
2062 case Instruction::FPExt:
2063 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2064 SrcBitSize < DstBitSize;
2065 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002066 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002067 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2068 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2069 return SVTy->getElementType()->isInteger() &&
2070 DVTy->getElementType()->isFloatingPoint() &&
2071 SVTy->getNumElements() == DVTy->getNumElements();
2072 }
2073 }
Chris Lattner03c49532007-01-15 02:27:26 +00002074 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002075 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002076 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002077 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2078 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2079 return SVTy->getElementType()->isFloatingPoint() &&
2080 DVTy->getElementType()->isInteger() &&
2081 SVTy->getNumElements() == DVTy->getNumElements();
2082 }
2083 }
Chris Lattner03c49532007-01-15 02:27:26 +00002084 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002085 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002086 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002087 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002088 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002089 case Instruction::BitCast:
2090 // BitCast implies a no-op cast of type only. No bits change.
2091 // However, you can't cast pointers to anything but pointers.
2092 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2093 return false;
2094
Duncan Sands55e50902008-01-06 10:12:28 +00002095 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002096 // these cases, the cast is okay if the source and destination bit widths
2097 // are identical.
2098 return SrcBitSize == DstBitSize;
2099 }
2100}
2101
2102TruncInst::TruncInst(
2103 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2104) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002105 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002106}
2107
2108TruncInst::TruncInst(
2109 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2110) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002111 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002112}
2113
2114ZExtInst::ZExtInst(
2115 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2116) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002117 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002118}
2119
2120ZExtInst::ZExtInst(
2121 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2122) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002123 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002124}
2125SExtInst::SExtInst(
2126 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2127) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002128 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002129}
2130
Jeff Cohencc08c832006-12-02 02:22:01 +00002131SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002132 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2133) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002134 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002135}
2136
2137FPTruncInst::FPTruncInst(
2138 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2139) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002140 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002141}
2142
2143FPTruncInst::FPTruncInst(
2144 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2145) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002146 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002147}
2148
2149FPExtInst::FPExtInst(
2150 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2151) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002152 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002153}
2154
2155FPExtInst::FPExtInst(
2156 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2157) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002158 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002159}
2160
2161UIToFPInst::UIToFPInst(
2162 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2163) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002164 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002165}
2166
2167UIToFPInst::UIToFPInst(
2168 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2169) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002170 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002171}
2172
2173SIToFPInst::SIToFPInst(
2174 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2175) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002176 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002177}
2178
2179SIToFPInst::SIToFPInst(
2180 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2181) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002182 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002183}
2184
2185FPToUIInst::FPToUIInst(
2186 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2187) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002188 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002189}
2190
2191FPToUIInst::FPToUIInst(
2192 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2193) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002194 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002195}
2196
2197FPToSIInst::FPToSIInst(
2198 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2199) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002200 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002201}
2202
2203FPToSIInst::FPToSIInst(
2204 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2205) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002206 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002207}
2208
2209PtrToIntInst::PtrToIntInst(
2210 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2211) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002212 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002213}
2214
2215PtrToIntInst::PtrToIntInst(
2216 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2217) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002218 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002219}
2220
2221IntToPtrInst::IntToPtrInst(
2222 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2223) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002224 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002225}
2226
2227IntToPtrInst::IntToPtrInst(
2228 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2229) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002230 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002231}
2232
2233BitCastInst::BitCastInst(
2234 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2235) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002236 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237}
2238
2239BitCastInst::BitCastInst(
2240 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2241) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002242 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002243}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002244
2245//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002246// CmpInst Classes
2247//===----------------------------------------------------------------------===//
2248
2249CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2250 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002251 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002252 Ops[0].init(LHS, this);
2253 Ops[1].init(RHS, this);
2254 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002255 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002256 if (op == Instruction::ICmp) {
2257 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2258 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2259 "Invalid ICmp predicate value");
2260 const Type* Op0Ty = getOperand(0)->getType();
2261 const Type* Op1Ty = getOperand(1)->getType();
2262 assert(Op0Ty == Op1Ty &&
2263 "Both operands to ICmp instruction are not of the same type!");
2264 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002265 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002266 "Invalid operand types for ICmp instruction");
2267 return;
2268 }
2269 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2270 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2271 "Invalid FCmp predicate value");
2272 const Type* Op0Ty = getOperand(0)->getType();
2273 const Type* Op1Ty = getOperand(1)->getType();
2274 assert(Op0Ty == Op1Ty &&
2275 "Both operands to FCmp instruction are not of the same type!");
2276 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002277 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002278 "Invalid operand types for FCmp instruction");
2279}
2280
2281CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2282 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002283 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002284 Ops[0].init(LHS, this);
2285 Ops[1].init(RHS, this);
2286 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002287 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002288 if (op == Instruction::ICmp) {
2289 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2290 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2291 "Invalid ICmp predicate value");
2292
2293 const Type* Op0Ty = getOperand(0)->getType();
2294 const Type* Op1Ty = getOperand(1)->getType();
2295 assert(Op0Ty == Op1Ty &&
2296 "Both operands to ICmp instruction are not of the same type!");
2297 // Check that the operands are the right type
Anton Korobeynikov579f0712008-02-20 11:08:44 +00002298 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002299 "Invalid operand types for ICmp instruction");
2300 return;
2301 }
2302 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2303 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2304 "Invalid FCmp predicate value");
2305 const Type* Op0Ty = getOperand(0)->getType();
2306 const Type* Op1Ty = getOperand(1)->getType();
2307 assert(Op0Ty == Op1Ty &&
2308 "Both operands to FCmp instruction are not of the same type!");
2309 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002310 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002311 "Invalid operand types for FCmp instruction");
2312}
2313
2314CmpInst *
2315CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2316 const std::string &Name, Instruction *InsertBefore) {
2317 if (Op == Instruction::ICmp) {
2318 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2319 InsertBefore);
2320 }
2321 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2322 InsertBefore);
2323}
2324
2325CmpInst *
2326CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2327 const std::string &Name, BasicBlock *InsertAtEnd) {
2328 if (Op == Instruction::ICmp) {
2329 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2330 InsertAtEnd);
2331 }
2332 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2333 InsertAtEnd);
2334}
2335
2336void CmpInst::swapOperands() {
2337 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2338 IC->swapOperands();
2339 else
2340 cast<FCmpInst>(this)->swapOperands();
2341}
2342
2343bool CmpInst::isCommutative() {
2344 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2345 return IC->isCommutative();
2346 return cast<FCmpInst>(this)->isCommutative();
2347}
2348
2349bool CmpInst::isEquality() {
2350 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2351 return IC->isEquality();
2352 return cast<FCmpInst>(this)->isEquality();
2353}
2354
2355
2356ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2357 switch (pred) {
2358 default:
2359 assert(!"Unknown icmp predicate!");
2360 case ICMP_EQ: return ICMP_NE;
2361 case ICMP_NE: return ICMP_EQ;
2362 case ICMP_UGT: return ICMP_ULE;
2363 case ICMP_ULT: return ICMP_UGE;
2364 case ICMP_UGE: return ICMP_ULT;
2365 case ICMP_ULE: return ICMP_UGT;
2366 case ICMP_SGT: return ICMP_SLE;
2367 case ICMP_SLT: return ICMP_SGE;
2368 case ICMP_SGE: return ICMP_SLT;
2369 case ICMP_SLE: return ICMP_SGT;
2370 }
2371}
2372
2373ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2374 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002375 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002376 case ICMP_EQ: case ICMP_NE:
2377 return pred;
2378 case ICMP_SGT: return ICMP_SLT;
2379 case ICMP_SLT: return ICMP_SGT;
2380 case ICMP_SGE: return ICMP_SLE;
2381 case ICMP_SLE: return ICMP_SGE;
2382 case ICMP_UGT: return ICMP_ULT;
2383 case ICMP_ULT: return ICMP_UGT;
2384 case ICMP_UGE: return ICMP_ULE;
2385 case ICMP_ULE: return ICMP_UGE;
2386 }
2387}
2388
Reid Spencer266e42b2006-12-23 06:05:41 +00002389ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2390 switch (pred) {
2391 default: assert(! "Unknown icmp predicate!");
2392 case ICMP_EQ: case ICMP_NE:
2393 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2394 return pred;
2395 case ICMP_UGT: return ICMP_SGT;
2396 case ICMP_ULT: return ICMP_SLT;
2397 case ICMP_UGE: return ICMP_SGE;
2398 case ICMP_ULE: return ICMP_SLE;
2399 }
2400}
2401
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002402ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2403 switch (pred) {
2404 default: assert(! "Unknown icmp predicate!");
2405 case ICMP_EQ: case ICMP_NE:
2406 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2407 return pred;
2408 case ICMP_SGT: return ICMP_UGT;
2409 case ICMP_SLT: return ICMP_ULT;
2410 case ICMP_SGE: return ICMP_UGE;
2411 case ICMP_SLE: return ICMP_ULE;
2412 }
2413}
2414
Reid Spencer266e42b2006-12-23 06:05:41 +00002415bool ICmpInst::isSignedPredicate(Predicate pred) {
2416 switch (pred) {
2417 default: assert(! "Unknown icmp predicate!");
2418 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2419 return true;
2420 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2421 case ICMP_UGE: case ICMP_ULE:
2422 return false;
2423 }
2424}
2425
Reid Spencer0286bc12007-02-28 22:00:54 +00002426/// Initialize a set of values that all satisfy the condition with C.
2427///
2428ConstantRange
2429ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2430 APInt Lower(C);
2431 APInt Upper(C);
2432 uint32_t BitWidth = C.getBitWidth();
2433 switch (pred) {
2434 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2435 case ICmpInst::ICMP_EQ: Upper++; break;
2436 case ICmpInst::ICMP_NE: Lower++; break;
2437 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2438 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2439 case ICmpInst::ICMP_UGT:
2440 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2441 break;
2442 case ICmpInst::ICMP_SGT:
2443 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2444 break;
2445 case ICmpInst::ICMP_ULE:
2446 Lower = APInt::getMinValue(BitWidth); Upper++;
2447 break;
2448 case ICmpInst::ICMP_SLE:
2449 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2450 break;
2451 case ICmpInst::ICMP_UGE:
2452 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2453 break;
2454 case ICmpInst::ICMP_SGE:
2455 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2456 break;
2457 }
2458 return ConstantRange(Lower, Upper);
2459}
2460
Reid Spencerd9436b62006-11-20 01:22:35 +00002461FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2462 switch (pred) {
2463 default:
2464 assert(!"Unknown icmp predicate!");
2465 case FCMP_OEQ: return FCMP_UNE;
2466 case FCMP_ONE: return FCMP_UEQ;
2467 case FCMP_OGT: return FCMP_ULE;
2468 case FCMP_OLT: return FCMP_UGE;
2469 case FCMP_OGE: return FCMP_ULT;
2470 case FCMP_OLE: return FCMP_UGT;
2471 case FCMP_UEQ: return FCMP_ONE;
2472 case FCMP_UNE: return FCMP_OEQ;
2473 case FCMP_UGT: return FCMP_OLE;
2474 case FCMP_ULT: return FCMP_OGE;
2475 case FCMP_UGE: return FCMP_OLT;
2476 case FCMP_ULE: return FCMP_OGT;
2477 case FCMP_ORD: return FCMP_UNO;
2478 case FCMP_UNO: return FCMP_ORD;
2479 case FCMP_TRUE: return FCMP_FALSE;
2480 case FCMP_FALSE: return FCMP_TRUE;
2481 }
2482}
2483
2484FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2485 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002486 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002487 case FCMP_FALSE: case FCMP_TRUE:
2488 case FCMP_OEQ: case FCMP_ONE:
2489 case FCMP_UEQ: case FCMP_UNE:
2490 case FCMP_ORD: case FCMP_UNO:
2491 return pred;
2492 case FCMP_OGT: return FCMP_OLT;
2493 case FCMP_OLT: return FCMP_OGT;
2494 case FCMP_OGE: return FCMP_OLE;
2495 case FCMP_OLE: return FCMP_OGE;
2496 case FCMP_UGT: return FCMP_ULT;
2497 case FCMP_ULT: return FCMP_UGT;
2498 case FCMP_UGE: return FCMP_ULE;
2499 case FCMP_ULE: return FCMP_UGE;
2500 }
2501}
2502
Reid Spencer266e42b2006-12-23 06:05:41 +00002503bool CmpInst::isUnsigned(unsigned short predicate) {
2504 switch (predicate) {
2505 default: return false;
2506 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2507 case ICmpInst::ICMP_UGE: return true;
2508 }
2509}
2510
2511bool CmpInst::isSigned(unsigned short predicate){
2512 switch (predicate) {
2513 default: return false;
2514 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2515 case ICmpInst::ICMP_SGE: return true;
2516 }
2517}
2518
2519bool CmpInst::isOrdered(unsigned short predicate) {
2520 switch (predicate) {
2521 default: return false;
2522 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2523 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2524 case FCmpInst::FCMP_ORD: return true;
2525 }
2526}
2527
2528bool CmpInst::isUnordered(unsigned short predicate) {
2529 switch (predicate) {
2530 default: return false;
2531 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2532 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2533 case FCmpInst::FCMP_UNO: return true;
2534 }
2535}
2536
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002537//===----------------------------------------------------------------------===//
2538// SwitchInst Implementation
2539//===----------------------------------------------------------------------===//
2540
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002541void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002542 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002543 ReservedSpace = 2+NumCases*2;
2544 NumOperands = 2;
2545 OperandList = new Use[ReservedSpace];
2546
2547 OperandList[0].init(Value, this);
2548 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002549}
2550
Chris Lattner2195fc42007-02-24 00:55:48 +00002551/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2552/// switch on and a default destination. The number of additional cases can
2553/// be specified here to make memory allocation more efficient. This
2554/// constructor can also autoinsert before another instruction.
2555SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2556 Instruction *InsertBefore)
2557 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2558 init(Value, Default, NumCases);
2559}
2560
2561/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2562/// switch on and a default destination. The number of additional cases can
2563/// be specified here to make memory allocation more efficient. This
2564/// constructor also autoinserts at the end of the specified BasicBlock.
2565SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2566 BasicBlock *InsertAtEnd)
2567 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2568 init(Value, Default, NumCases);
2569}
2570
Misha Brukmanb1c93172005-04-21 23:48:37 +00002571SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002572 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2573 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002574 Use *OL = OperandList, *InOL = SI.OperandList;
2575 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2576 OL[i].init(InOL[i], this);
2577 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002578 }
2579}
2580
Gordon Henriksen14a55692007-12-10 02:14:30 +00002581SwitchInst::~SwitchInst() {
2582 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002583}
2584
2585
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002586/// addCase - Add an entry to the switch instruction...
2587///
Chris Lattner47ac1872005-02-24 05:32:09 +00002588void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002589 unsigned OpNo = NumOperands;
2590 if (OpNo+2 > ReservedSpace)
2591 resizeOperands(0); // Get more space!
2592 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002593 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002594 NumOperands = OpNo+2;
2595 OperandList[OpNo].init(OnVal, this);
2596 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002597}
2598
2599/// removeCase - This method removes the specified successor from the switch
2600/// instruction. Note that this cannot be used to remove the default
2601/// destination (successor #0).
2602///
2603void SwitchInst::removeCase(unsigned idx) {
2604 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002605 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2606
2607 unsigned NumOps = getNumOperands();
2608 Use *OL = OperandList;
2609
2610 // Move everything after this operand down.
2611 //
2612 // FIXME: we could just swap with the end of the list, then erase. However,
2613 // client might not expect this to happen. The code as it is thrashes the
2614 // use/def lists, which is kinda lame.
2615 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2616 OL[i-2] = OL[i];
2617 OL[i-2+1] = OL[i+1];
2618 }
2619
2620 // Nuke the last value.
2621 OL[NumOps-2].set(0);
2622 OL[NumOps-2+1].set(0);
2623 NumOperands = NumOps-2;
2624}
2625
2626/// resizeOperands - resize operands - This adjusts the length of the operands
2627/// list according to the following behavior:
2628/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2629/// of operation. This grows the number of ops by 1.5 times.
2630/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2631/// 3. If NumOps == NumOperands, trim the reserved space.
2632///
2633void SwitchInst::resizeOperands(unsigned NumOps) {
2634 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002635 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002636 } else if (NumOps*2 > NumOperands) {
2637 // No resize needed.
2638 if (ReservedSpace >= NumOps) return;
2639 } else if (NumOps == NumOperands) {
2640 if (ReservedSpace == NumOps) return;
2641 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002642 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002643 }
2644
2645 ReservedSpace = NumOps;
2646 Use *NewOps = new Use[NumOps];
2647 Use *OldOps = OperandList;
2648 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2649 NewOps[i].init(OldOps[i], this);
2650 OldOps[i].set(0);
2651 }
2652 delete [] OldOps;
2653 OperandList = NewOps;
2654}
2655
2656
2657BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2658 return getSuccessor(idx);
2659}
2660unsigned SwitchInst::getNumSuccessorsV() const {
2661 return getNumSuccessors();
2662}
2663void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2664 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002665}
Chris Lattnerf22be932004-10-15 23:52:53 +00002666
Devang Patel295711f2008-02-19 22:15:16 +00002667//===----------------------------------------------------------------------===//
2668// GetResultInst Implementation
2669//===----------------------------------------------------------------------===//
2670
Devang Patel666d4512008-02-20 19:10:47 +00002671GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002672 const std::string &Name,
2673 Instruction *InsertBef)
Devang Pateldcad9da2008-02-20 19:26:55 +00002674 : Instruction(cast<StructType>(Aggregate->getType())->getElementType(Index),
Devang Patel666d4512008-02-20 19:10:47 +00002675 GetResult, &Aggr, 1, InsertBef) {
2676 assert(isValidOperands(Aggregate, Index) && "Invalid GetResultInst operands!");
2677 Aggr.init(Aggregate, this);
2678 Idx = Index;
Devang Patel295711f2008-02-19 22:15:16 +00002679 setName(Name);
2680}
2681
Devang Patel666d4512008-02-20 19:10:47 +00002682bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2683 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002684 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002685
Devang Patelcf193b82008-02-20 19:39:41 +00002686 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2687 unsigned NumElements = STy->getNumElements();
2688 if (Index >= NumElements)
2689 return false;
2690
2691 // getresult aggregate value's element types are restricted to
2692 // avoid nested aggregates.
2693 for (unsigned i = 0; i < NumElements; ++i)
2694 if (!STy->getElementType(i)->isFirstClassType())
2695 return false;
2696
2697 // Otherwise, Aggregate is valid.
2698 return true;
2699 }
Devang Patel666d4512008-02-20 19:10:47 +00002700 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002701}
2702
Chris Lattnerf22be932004-10-15 23:52:53 +00002703// Define these methods here so vtables don't get emitted into every translation
2704// unit that uses these classes.
2705
2706GetElementPtrInst *GetElementPtrInst::clone() const {
2707 return new GetElementPtrInst(*this);
2708}
2709
2710BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002711 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002712}
2713
Chris Lattner0b490b02007-08-24 20:48:18 +00002714FCmpInst* FCmpInst::clone() const {
2715 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2716}
2717ICmpInst* ICmpInst::clone() const {
2718 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002719}
2720
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002721MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2722AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2723FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2724LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2725StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2726CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2727CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2728CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2729CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2730CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2731CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2732CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2733CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2734CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2735CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2736CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2737CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2738CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002739SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2740VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2741
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002742ExtractElementInst *ExtractElementInst::clone() const {
2743 return new ExtractElementInst(*this);
2744}
2745InsertElementInst *InsertElementInst::clone() const {
2746 return new InsertElementInst(*this);
2747}
2748ShuffleVectorInst *ShuffleVectorInst::clone() const {
2749 return new ShuffleVectorInst(*this);
2750}
Chris Lattnerf22be932004-10-15 23:52:53 +00002751PHINode *PHINode::clone() const { return new PHINode(*this); }
2752ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2753BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2754SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2755InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2756UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002757UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002758GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }