blob: ee9a02e91acc8de86a8b1043e8646343897a4f53 [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
Dale Johannesen89268bc2008-02-19 21:38:47 +0000365bool CallInst::paramHasAttr(uint16_t 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
Dale Johanneseneabc5f32008-02-22 17:49:45 +0000373uint16_t CallInst::getParamAlignment(uint16_t i) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000374 return ParamAttrs.getParamAlignment(i);
Dale Johanneseneabc5f32008-02-22 17:49:45 +0000375}
376
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000377/// @brief Determine if the call does not access memory.
378bool CallInst::doesNotAccessMemory() const {
379 return paramHasAttr(0, ParamAttr::ReadNone);
380}
381
382/// @brief Determine if the call does not access or only reads memory.
383bool CallInst::onlyReadsMemory() const {
384 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
385}
386
387/// @brief Determine if the call cannot return.
388bool CallInst::doesNotReturn() const {
389 return paramHasAttr(0, ParamAttr::NoReturn);
390}
391
392/// @brief Determine if the call cannot unwind.
393bool CallInst::doesNotThrow() const {
394 return paramHasAttr(0, ParamAttr::NoUnwind);
395}
396
Devang Patel9d9178592008-03-03 21:46:28 +0000397/// @brief Determine if the call returns a structure through first
398/// pointer argument.
399bool CallInst::hasStructRetAttr() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000400 // Be friendly and also check the callee.
401 return paramHasAttr(1, ParamAttr::StructRet);
402}
403
Evan Cheng09dde602008-01-12 18:57:32 +0000404/// @brief Determine if any call argument is an aggregate passed by value.
405bool CallInst::hasByValArgument() const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000406 if (ParamAttrs.hasAttrSomewhere(ParamAttr::ByVal))
Duncan Sandsa59f3962008-01-21 11:27:55 +0000407 return true;
408 // Be consistent with other methods and check the callee too.
409 if (const Function *F = getCalledFunction())
Chris Lattner8a923e72008-03-12 17:45:29 +0000410 return F->getParamAttrs().hasAttrSomewhere(ParamAttr::ByVal);
Duncan Sandsa59f3962008-01-21 11:27:55 +0000411 return false;
Evan Cheng09dde602008-01-12 18:57:32 +0000412}
413
Duncan Sandsaa31b922007-12-19 21:13:37 +0000414void CallInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000415 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000416 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000417 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000418 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000419 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000420 setParamAttrs(PAL);
421}
422
Duncan Sands5208d1a2007-11-28 17:07:01 +0000423
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000424//===----------------------------------------------------------------------===//
425// InvokeInst Implementation
426//===----------------------------------------------------------------------===//
427
Gordon Henriksen14a55692007-12-10 02:14:30 +0000428InvokeInst::~InvokeInst() {
429 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000430}
431
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000432void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000433 Value* const *Args, unsigned NumArgs) {
434 NumOperands = 3+NumArgs;
435 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000436 OL[0].init(Fn, this);
437 OL[1].init(IfNormal, this);
438 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000439 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000440 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000441 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000442
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000443 assert(((NumArgs == FTy->getNumParams()) ||
444 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000445 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000446
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000447 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000448 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000449 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000450 "Invoking a function with a bad signature!");
451
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000452 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000453 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000454}
455
Misha Brukmanb1c93172005-04-21 23:48:37 +0000456InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000457 : TerminatorInst(II.getType(), Instruction::Invoke,
Chris Lattner8a923e72008-03-12 17:45:29 +0000458 new Use[II.getNumOperands()], II.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000459 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000460 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000461 Use *OL = OperandList, *InOL = II.OperandList;
462 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
463 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000464}
465
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000466BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
467 return getSuccessor(idx);
468}
469unsigned InvokeInst::getNumSuccessorsV() const {
470 return getNumSuccessors();
471}
472void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
473 return setSuccessor(idx, B);
474}
475
Dale Johannesen89268bc2008-02-19 21:38:47 +0000476bool InvokeInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000477 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000478 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000479 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000480 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000481 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000482}
483
Dale Johanneseneabc5f32008-02-22 17:49:45 +0000484uint16_t InvokeInst::getParamAlignment(uint16_t i) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000485 return ParamAttrs.getParamAlignment(i);
Dale Johanneseneabc5f32008-02-22 17:49:45 +0000486}
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000487
488/// @brief Determine if the call does not access memory.
489bool InvokeInst::doesNotAccessMemory() const {
490 return paramHasAttr(0, ParamAttr::ReadNone);
491}
492
493/// @brief Determine if the call does not access or only reads memory.
494bool InvokeInst::onlyReadsMemory() const {
495 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
496}
497
498/// @brief Determine if the call cannot return.
499bool InvokeInst::doesNotReturn() const {
500 return paramHasAttr(0, ParamAttr::NoReturn);
501}
502
503/// @brief Determine if the call cannot unwind.
504bool InvokeInst::doesNotThrow() const {
505 return paramHasAttr(0, ParamAttr::NoUnwind);
506}
507
Duncan Sandsaa31b922007-12-19 21:13:37 +0000508void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000509 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000510 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000511 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000512 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000513 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000514 setParamAttrs(PAL);
515}
516
Devang Patel9d9178592008-03-03 21:46:28 +0000517/// @brief Determine if the invoke returns a structure through first
518/// pointer argument.
519bool InvokeInst::hasStructRetAttr() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000520 // Be friendly and also check the callee.
521 return paramHasAttr(1, ParamAttr::StructRet);
522}
523
Duncan Sands5208d1a2007-11-28 17:07:01 +0000524
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000525//===----------------------------------------------------------------------===//
526// ReturnInst Implementation
527//===----------------------------------------------------------------------===//
528
Chris Lattner2195fc42007-02-24 00:55:48 +0000529ReturnInst::ReturnInst(const ReturnInst &RI)
530 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Devang Patelae682fb2008-02-26 17:56:20 +0000531 &RetVal, RI.getNumOperands()) {
Devang Patel59643e52008-02-23 00:35:18 +0000532 unsigned N = RI.getNumOperands();
Devang Patelae682fb2008-02-26 17:56:20 +0000533 if (N == 1)
534 RetVal.init(RI.RetVal, this);
535 else if (N) {
536 Use *OL = OperandList = new Use[N];
537 for (unsigned i = 0; i < N; ++i)
538 OL[i].init(RI.getOperand(i), this);
539 }
Chris Lattner2195fc42007-02-24 00:55:48 +0000540}
541
542ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Devang Patelae682fb2008-02-26 17:56:20 +0000543 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000544 if (retVal)
545 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000546}
547ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Devang Patelae682fb2008-02-26 17:56:20 +0000548 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000549 if (retVal)
550 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000551}
552ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Devang Patelae682fb2008-02-26 17:56:20 +0000553 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000554}
555
Devang Patela736c002008-02-26 19:38:17 +0000556ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
557 Instruction *InsertBefore)
558 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N, InsertBefore) {
559 if (N != 0)
560 init(retVals, N);
561}
562ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
563 BasicBlock *InsertAtEnd)
564 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N, InsertAtEnd) {
565 if (N != 0)
566 init(retVals, N);
567}
568ReturnInst::ReturnInst(Value * const* retVals, unsigned N)
569 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N) {
570 if (N != 0)
571 init(retVals, N);
572}
573
Devang Patel060e79c2008-02-26 19:15:26 +0000574void ReturnInst::init(Value * const* retVals, unsigned N) {
Devang Patelc38eb522008-02-26 18:49:29 +0000575 assert (N > 0 && "Invalid operands numbers in ReturnInst init");
Devang Patel59643e52008-02-23 00:35:18 +0000576
Devang Patelc38eb522008-02-26 18:49:29 +0000577 NumOperands = N;
Devang Patel59643e52008-02-23 00:35:18 +0000578 if (NumOperands == 1) {
Devang Patel060e79c2008-02-26 19:15:26 +0000579 Value *V = *retVals;
Devang Patel59643e52008-02-23 00:35:18 +0000580 if (V->getType() == Type::VoidTy)
581 return;
Devang Patel060e79c2008-02-26 19:15:26 +0000582 RetVal.init(V, this);
Devang Patelae682fb2008-02-26 17:56:20 +0000583 return;
Devang Patel59643e52008-02-23 00:35:18 +0000584 }
585
586 Use *OL = OperandList = new Use[NumOperands];
587 for (unsigned i = 0; i < NumOperands; ++i) {
Devang Patel060e79c2008-02-26 19:15:26 +0000588 Value *V = *retVals++;
Devang Patel59643e52008-02-23 00:35:18 +0000589 assert(!isa<BasicBlock>(V) &&
590 "Cannot return basic block. Probably using the incorrect ctor");
Devang Patel060e79c2008-02-26 19:15:26 +0000591 OL[i].init(V, this);
Devang Patel59643e52008-02-23 00:35:18 +0000592 }
593}
594
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000595unsigned ReturnInst::getNumSuccessorsV() const {
596 return getNumSuccessors();
597}
598
Devang Patelae682fb2008-02-26 17:56:20 +0000599/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
600/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000601void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000602 assert(0 && "ReturnInst has no successors!");
603}
604
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000605BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
606 assert(0 && "ReturnInst has no successors!");
607 abort();
608 return 0;
609}
610
Devang Patel59643e52008-02-23 00:35:18 +0000611ReturnInst::~ReturnInst() {
Devang Patelae682fb2008-02-26 17:56:20 +0000612 if (NumOperands > 1)
Devang Patel59643e52008-02-23 00:35:18 +0000613 delete [] OperandList;
614}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000615
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000616//===----------------------------------------------------------------------===//
617// UnwindInst Implementation
618//===----------------------------------------------------------------------===//
619
Chris Lattner2195fc42007-02-24 00:55:48 +0000620UnwindInst::UnwindInst(Instruction *InsertBefore)
621 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
622}
623UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
624 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
625}
626
627
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000628unsigned UnwindInst::getNumSuccessorsV() const {
629 return getNumSuccessors();
630}
631
632void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000633 assert(0 && "UnwindInst has no successors!");
634}
635
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000636BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
637 assert(0 && "UnwindInst has no successors!");
638 abort();
639 return 0;
640}
641
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000642//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000643// UnreachableInst Implementation
644//===----------------------------------------------------------------------===//
645
Chris Lattner2195fc42007-02-24 00:55:48 +0000646UnreachableInst::UnreachableInst(Instruction *InsertBefore)
647 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
648}
649UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
650 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
651}
652
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000653unsigned UnreachableInst::getNumSuccessorsV() const {
654 return getNumSuccessors();
655}
656
657void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
658 assert(0 && "UnwindInst has no successors!");
659}
660
661BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
662 assert(0 && "UnwindInst has no successors!");
663 abort();
664 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000665}
666
667//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000668// BranchInst Implementation
669//===----------------------------------------------------------------------===//
670
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000671void BranchInst::AssertOK() {
672 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000673 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000674 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000675}
676
Chris Lattner2195fc42007-02-24 00:55:48 +0000677BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
678 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
679 assert(IfTrue != 0 && "Branch destination may not be null!");
680 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
681}
682BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
683 Instruction *InsertBefore)
684: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
685 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
686 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
687 Ops[2].init(Cond, this);
688#ifndef NDEBUG
689 AssertOK();
690#endif
691}
692
693BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
694 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
695 assert(IfTrue != 0 && "Branch destination may not be null!");
696 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
697}
698
699BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
700 BasicBlock *InsertAtEnd)
701 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
702 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
703 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
704 Ops[2].init(Cond, this);
705#ifndef NDEBUG
706 AssertOK();
707#endif
708}
709
710
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000711BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000712 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000713 OperandList[0].init(BI.getOperand(0), this);
714 if (BI.getNumOperands() != 1) {
715 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
716 OperandList[1].init(BI.getOperand(1), this);
717 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000718 }
719}
720
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
722 return getSuccessor(idx);
723}
724unsigned BranchInst::getNumSuccessorsV() const {
725 return getNumSuccessors();
726}
727void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
728 setSuccessor(idx, B);
729}
730
731
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732//===----------------------------------------------------------------------===//
733// AllocationInst Implementation
734//===----------------------------------------------------------------------===//
735
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000736static Value *getAISize(Value *Amt) {
737 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000738 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000739 else {
740 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000741 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000742 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000743 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000744 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000745 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000746}
747
Misha Brukmanb1c93172005-04-21 23:48:37 +0000748AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000749 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000750 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000751 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000752 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000753 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000754 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000755 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000756}
757
Misha Brukmanb1c93172005-04-21 23:48:37 +0000758AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000759 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000760 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000761 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000762 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000763 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000764 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000765 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000766}
767
Gordon Henriksen14a55692007-12-10 02:14:30 +0000768// Out of line virtual method, so the vtable, etc has a home.
769AllocationInst::~AllocationInst() {
770}
771
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000772bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000773 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
774 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000775 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000776}
777
778const Type *AllocationInst::getAllocatedType() const {
779 return getType()->getElementType();
780}
781
782AllocaInst::AllocaInst(const AllocaInst &AI)
783 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000784 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000785}
786
787MallocInst::MallocInst(const MallocInst &MI)
788 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000789 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000790}
791
792//===----------------------------------------------------------------------===//
793// FreeInst Implementation
794//===----------------------------------------------------------------------===//
795
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000796void FreeInst::AssertOK() {
797 assert(isa<PointerType>(getOperand(0)->getType()) &&
798 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000799}
800
801FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000802 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000803 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000804}
805
806FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000807 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000808 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000809}
810
811
812//===----------------------------------------------------------------------===//
813// LoadInst Implementation
814//===----------------------------------------------------------------------===//
815
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000816void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000817 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000818 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000819}
820
821LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000822 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000823 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000824 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000825 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000826 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000827 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000828}
829
830LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000831 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000832 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000833 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000834 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000835 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000836 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000837}
838
839LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
840 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000841 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000842 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000843 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000844 setAlignment(0);
845 AssertOK();
846 setName(Name);
847}
848
849LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
850 unsigned Align, Instruction *InsertBef)
851 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
852 Load, Ptr, InsertBef) {
853 setVolatile(isVolatile);
854 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000855 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000856 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000857}
858
Dan Gohman68659282007-07-18 20:51:11 +0000859LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
860 unsigned Align, BasicBlock *InsertAE)
861 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
862 Load, Ptr, InsertAE) {
863 setVolatile(isVolatile);
864 setAlignment(Align);
865 AssertOK();
866 setName(Name);
867}
868
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000869LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
870 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000871 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000872 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000873 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000874 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000875 AssertOK();
876 setName(Name);
877}
878
879
880
881LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000882 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
883 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000884 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000885 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000886 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000887 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000888}
889
890LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000891 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
892 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000893 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000894 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000895 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000896 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000897}
898
899LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
900 Instruction *InsertBef)
901: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000902 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000903 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000904 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000905 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000906 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000907}
908
909LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
910 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000911 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
912 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000913 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000914 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000915 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000916 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000917}
918
Christopher Lamb84485702007-04-22 19:24:39 +0000919void LoadInst::setAlignment(unsigned Align) {
920 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
921 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
922}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000923
924//===----------------------------------------------------------------------===//
925// StoreInst Implementation
926//===----------------------------------------------------------------------===//
927
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000928void StoreInst::AssertOK() {
929 assert(isa<PointerType>(getOperand(1)->getType()) &&
930 "Ptr must have pointer type!");
931 assert(getOperand(0)->getType() ==
932 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000933 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000934}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000935
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000936
937StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000938 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000939 Ops[0].init(val, this);
940 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000941 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000942 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000943 AssertOK();
944}
945
946StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000947 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000948 Ops[0].init(val, this);
949 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000950 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000951 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000952 AssertOK();
953}
954
Misha Brukmanb1c93172005-04-21 23:48:37 +0000955StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000956 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000957 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000958 Ops[0].init(val, this);
959 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000960 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000961 setAlignment(0);
962 AssertOK();
963}
964
965StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
966 unsigned Align, Instruction *InsertBefore)
967 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
968 Ops[0].init(val, this);
969 Ops[1].init(addr, this);
970 setVolatile(isVolatile);
971 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000972 AssertOK();
973}
974
Misha Brukmanb1c93172005-04-21 23:48:37 +0000975StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000976 unsigned Align, BasicBlock *InsertAtEnd)
977 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
978 Ops[0].init(val, this);
979 Ops[1].init(addr, this);
980 setVolatile(isVolatile);
981 setAlignment(Align);
982 AssertOK();
983}
984
985StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000986 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000987 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000988 Ops[0].init(val, this);
989 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000990 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000991 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000992 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000993}
994
Christopher Lamb84485702007-04-22 19:24:39 +0000995void StoreInst::setAlignment(unsigned Align) {
996 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
997 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
998}
999
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001000//===----------------------------------------------------------------------===//
1001// GetElementPtrInst Implementation
1002//===----------------------------------------------------------------------===//
1003
Christopher Lambedf07882007-12-17 01:12:55 +00001004static unsigned retrieveAddrSpace(const Value *Val) {
1005 return cast<PointerType>(Val->getType())->getAddressSpace();
1006}
1007
Chris Lattner79807c3d2007-01-31 19:47:18 +00001008void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
1009 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001010 Use *OL = OperandList = new Use[NumOperands];
1011 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001012
Chris Lattner79807c3d2007-01-31 19:47:18 +00001013 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001014 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001015}
1016
Chris Lattner82981202005-05-03 05:43:30 +00001017void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
1018 NumOperands = 2;
1019 Use *OL = OperandList = new Use[2];
1020 OL[0].init(Ptr, this);
1021 OL[1].init(Idx, this);
1022}
1023
Chris Lattner82981202005-05-03 05:43:30 +00001024GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1025 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001026 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001027 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001028 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +00001029 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001030 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001031}
1032
1033GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1034 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001035 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001036 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001037 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +00001038 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001039 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001040}
1041
Gordon Henriksen14a55692007-12-10 02:14:30 +00001042GetElementPtrInst::~GetElementPtrInst() {
1043 delete[] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001044}
1045
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001046// getIndexedType - Returns the type of the element that would be loaded with
1047// a load instruction with the specified parameters.
1048//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001049// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001050// pointer type.
1051//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001052const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001053 Value* const *Idxs,
1054 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001055 bool AllowCompositeLeaf) {
1056 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
1057
1058 // Handle the special case of the empty set index set...
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001059 if (NumIdx == 0) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001060 if (AllowCompositeLeaf ||
1061 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
1062 return cast<PointerType>(Ptr)->getElementType();
1063 else
1064 return 0;
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001065 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001066
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001067 unsigned CurIdx = 0;
1068 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001069 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001070 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
1071 return 0; // Can't load a whole structure or array!?!?
1072 }
1073
Chris Lattner302116a2007-01-31 04:40:28 +00001074 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001075 if (isa<PointerType>(CT) && CurIdx != 1)
1076 return 0; // Can only index into pointer types at the first index!
1077 if (!CT->indexValid(Index)) return 0;
1078 Ptr = CT->getTypeAtIndex(Index);
1079
1080 // If the new type forwards to another type, then it is in the middle
1081 // of being refined to another type (and hence, may have dropped all
1082 // references to what it was using before). So, use the new forwarded
1083 // type.
1084 if (const Type * Ty = Ptr->getForwardedType()) {
1085 Ptr = Ty;
1086 }
1087 }
Chris Lattner302116a2007-01-31 04:40:28 +00001088 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001089}
1090
Chris Lattner82981202005-05-03 05:43:30 +00001091const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1092 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1093 if (!PTy) return 0; // Type isn't a pointer type!
1094
1095 // Check the pointer index.
1096 if (!PTy->indexValid(Idx)) return 0;
1097
Chris Lattnerc2233332005-05-03 16:44:45 +00001098 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001099}
1100
Chris Lattner45f15572007-04-14 00:12:57 +00001101
1102/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1103/// zeros. If so, the result pointer and the first operand have the same
1104/// value, just potentially different types.
1105bool GetElementPtrInst::hasAllZeroIndices() const {
1106 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1107 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1108 if (!CI->isZero()) return false;
1109 } else {
1110 return false;
1111 }
1112 }
1113 return true;
1114}
1115
Chris Lattner27058292007-04-27 20:35:56 +00001116/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1117/// constant integers. If so, the result pointer and the first operand have
1118/// a constant offset between them.
1119bool GetElementPtrInst::hasAllConstantIndices() const {
1120 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1121 if (!isa<ConstantInt>(getOperand(i)))
1122 return false;
1123 }
1124 return true;
1125}
1126
Chris Lattner45f15572007-04-14 00:12:57 +00001127
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001128//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001129// ExtractElementInst Implementation
1130//===----------------------------------------------------------------------===//
1131
1132ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001133 const std::string &Name,
1134 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001135 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001136 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001137 assert(isValidOperands(Val, Index) &&
1138 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001139 Ops[0].init(Val, this);
1140 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001141 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001142}
1143
Chris Lattner65511ff2006-10-05 06:24:58 +00001144ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1145 const std::string &Name,
1146 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001147 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001148 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001149 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001150 assert(isValidOperands(Val, Index) &&
1151 "Invalid extractelement instruction operands!");
1152 Ops[0].init(Val, this);
1153 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001154 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001155}
1156
1157
Robert Bocchino23004482006-01-10 19:05:34 +00001158ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001159 const std::string &Name,
1160 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001161 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001162 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001163 assert(isValidOperands(Val, Index) &&
1164 "Invalid extractelement instruction operands!");
1165
Robert Bocchino23004482006-01-10 19:05:34 +00001166 Ops[0].init(Val, this);
1167 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001168 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001169}
1170
Chris Lattner65511ff2006-10-05 06:24:58 +00001171ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1172 const std::string &Name,
1173 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001174 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001175 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001176 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001177 assert(isValidOperands(Val, Index) &&
1178 "Invalid extractelement instruction operands!");
1179
1180 Ops[0].init(Val, this);
1181 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001182 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001183}
1184
1185
Chris Lattner54865b32006-04-08 04:05:48 +00001186bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001187 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001188 return false;
1189 return true;
1190}
1191
1192
Robert Bocchino23004482006-01-10 19:05:34 +00001193//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001194// InsertElementInst Implementation
1195//===----------------------------------------------------------------------===//
1196
Chris Lattner0875d942006-04-14 22:20:32 +00001197InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1198 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1199 Ops[0].init(IE.Ops[0], this);
1200 Ops[1].init(IE.Ops[1], this);
1201 Ops[2].init(IE.Ops[2], this);
1202}
Chris Lattner54865b32006-04-08 04:05:48 +00001203InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001204 const std::string &Name,
1205 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001206 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001207 assert(isValidOperands(Vec, Elt, Index) &&
1208 "Invalid insertelement instruction operands!");
1209 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001210 Ops[1].init(Elt, this);
1211 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001212 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001213}
1214
Chris Lattner65511ff2006-10-05 06:24:58 +00001215InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1216 const std::string &Name,
1217 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001218 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001219 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001220 assert(isValidOperands(Vec, Elt, Index) &&
1221 "Invalid insertelement instruction operands!");
1222 Ops[0].init(Vec, this);
1223 Ops[1].init(Elt, this);
1224 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001225 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001226}
1227
1228
Chris Lattner54865b32006-04-08 04:05:48 +00001229InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001230 const std::string &Name,
1231 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001232 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001233 assert(isValidOperands(Vec, Elt, Index) &&
1234 "Invalid insertelement instruction operands!");
1235
1236 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001237 Ops[1].init(Elt, this);
1238 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001239 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001240}
1241
Chris Lattner65511ff2006-10-05 06:24:58 +00001242InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1243 const std::string &Name,
1244 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001245: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001246 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001247 assert(isValidOperands(Vec, Elt, Index) &&
1248 "Invalid insertelement instruction operands!");
1249
1250 Ops[0].init(Vec, this);
1251 Ops[1].init(Elt, this);
1252 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001253 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001254}
1255
Chris Lattner54865b32006-04-08 04:05:48 +00001256bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1257 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001258 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001259 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001260
Reid Spencerd84d35b2007-02-15 02:26:10 +00001261 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001262 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001263
Reid Spencer8d9336d2006-12-31 05:26:44 +00001264 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001265 return false; // Third operand of insertelement must be uint.
1266 return true;
1267}
1268
1269
Robert Bocchinoca27f032006-01-17 20:07:22 +00001270//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001271// ShuffleVectorInst Implementation
1272//===----------------------------------------------------------------------===//
1273
Chris Lattner0875d942006-04-14 22:20:32 +00001274ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1275 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1276 Ops[0].init(SV.Ops[0], this);
1277 Ops[1].init(SV.Ops[1], this);
1278 Ops[2].init(SV.Ops[2], this);
1279}
1280
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001281ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1282 const std::string &Name,
1283 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001284 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001285 assert(isValidOperands(V1, V2, Mask) &&
1286 "Invalid shuffle vector instruction operands!");
1287 Ops[0].init(V1, this);
1288 Ops[1].init(V2, this);
1289 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001290 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001291}
1292
1293ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1294 const std::string &Name,
1295 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001296 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001297 assert(isValidOperands(V1, V2, Mask) &&
1298 "Invalid shuffle vector instruction operands!");
1299
1300 Ops[0].init(V1, this);
1301 Ops[1].init(V2, this);
1302 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001303 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001304}
1305
1306bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1307 const Value *Mask) {
Chris Lattnerf724e342008-03-02 05:28:33 +00001308 if (!isa<VectorType>(V1->getType()) ||
1309 V1->getType() != V2->getType())
1310 return false;
1311
1312 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1313 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1314 MaskTy->getElementType() != Type::Int32Ty ||
1315 MaskTy->getNumElements() !=
1316 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001317 return false;
1318 return true;
1319}
1320
Chris Lattnerf724e342008-03-02 05:28:33 +00001321/// getMaskValue - Return the index from the shuffle mask for the specified
1322/// output result. This is either -1 if the element is undef or a number less
1323/// than 2*numelements.
1324int ShuffleVectorInst::getMaskValue(unsigned i) const {
1325 const Constant *Mask = cast<Constant>(getOperand(2));
1326 if (isa<UndefValue>(Mask)) return -1;
1327 if (isa<ConstantAggregateZero>(Mask)) return 0;
1328 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1329 assert(i < MaskCV->getNumOperands() && "Index out of range");
1330
1331 if (isa<UndefValue>(MaskCV->getOperand(i)))
1332 return -1;
1333 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1334}
1335
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001336
1337//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001338// BinaryOperator Class
1339//===----------------------------------------------------------------------===//
1340
Chris Lattner2195fc42007-02-24 00:55:48 +00001341BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1342 const Type *Ty, const std::string &Name,
1343 Instruction *InsertBefore)
1344 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1345 Ops[0].init(S1, this);
1346 Ops[1].init(S2, this);
1347 init(iType);
1348 setName(Name);
1349}
1350
1351BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1352 const Type *Ty, const std::string &Name,
1353 BasicBlock *InsertAtEnd)
1354 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1355 Ops[0].init(S1, this);
1356 Ops[1].init(S2, this);
1357 init(iType);
1358 setName(Name);
1359}
1360
1361
1362void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001363 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001364 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001365 assert(LHS->getType() == RHS->getType() &&
1366 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001367#ifndef NDEBUG
1368 switch (iType) {
1369 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001370 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001371 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001372 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001373 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001374 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001375 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001376 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001377 case UDiv:
1378 case SDiv:
1379 assert(getType() == LHS->getType() &&
1380 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001381 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1382 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001383 "Incorrect operand type (not integer) for S/UDIV");
1384 break;
1385 case FDiv:
1386 assert(getType() == LHS->getType() &&
1387 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001388 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1389 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001390 && "Incorrect operand type (not floating point) for FDIV");
1391 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001392 case URem:
1393 case SRem:
1394 assert(getType() == LHS->getType() &&
1395 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001396 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1397 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001398 "Incorrect operand type (not integer) for S/UREM");
1399 break;
1400 case FRem:
1401 assert(getType() == LHS->getType() &&
1402 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001403 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1404 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001405 && "Incorrect operand type (not floating point) for FREM");
1406 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001407 case Shl:
1408 case LShr:
1409 case AShr:
1410 assert(getType() == LHS->getType() &&
1411 "Shift operation should return same type as operands!");
1412 assert(getType()->isInteger() &&
1413 "Shift operation requires integer operands");
1414 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001415 case And: case Or:
1416 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001417 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001418 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001419 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001420 (isa<VectorType>(getType()) &&
1421 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001422 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001423 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001424 default:
1425 break;
1426 }
1427#endif
1428}
1429
1430BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001431 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001432 Instruction *InsertBefore) {
1433 assert(S1->getType() == S2->getType() &&
1434 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001435 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001436}
1437
1438BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001439 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001440 BasicBlock *InsertAtEnd) {
1441 BinaryOperator *Res = create(Op, S1, S2, Name);
1442 InsertAtEnd->getInstList().push_back(Res);
1443 return Res;
1444}
1445
1446BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1447 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001448 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1449 return new BinaryOperator(Instruction::Sub,
1450 zero, Op,
1451 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001452}
1453
1454BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1455 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001456 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1457 return new BinaryOperator(Instruction::Sub,
1458 zero, Op,
1459 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001460}
1461
1462BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1463 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001464 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001465 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001466 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001467 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001468 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001469 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001470 }
1471
1472 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001473 Op->getType(), Name, InsertBefore);
1474}
1475
1476BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1477 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001478 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001479 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001480 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001481 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001482 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001483 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001484 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001485 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001486 }
1487
1488 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001489 Op->getType(), Name, InsertAtEnd);
1490}
1491
1492
1493// isConstantAllOnes - Helper function for several functions below
1494static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001495 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1496 return CI->isAllOnesValue();
1497 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1498 return CV->isAllOnesValue();
1499 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001500}
1501
1502bool BinaryOperator::isNeg(const Value *V) {
1503 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1504 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001505 return Bop->getOperand(0) ==
1506 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001507 return false;
1508}
1509
1510bool BinaryOperator::isNot(const Value *V) {
1511 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1512 return (Bop->getOpcode() == Instruction::Xor &&
1513 (isConstantAllOnes(Bop->getOperand(1)) ||
1514 isConstantAllOnes(Bop->getOperand(0))));
1515 return false;
1516}
1517
Chris Lattner2c7d1772005-04-24 07:28:37 +00001518Value *BinaryOperator::getNegArgument(Value *BinOp) {
1519 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1520 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001521}
1522
Chris Lattner2c7d1772005-04-24 07:28:37 +00001523const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1524 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001525}
1526
Chris Lattner2c7d1772005-04-24 07:28:37 +00001527Value *BinaryOperator::getNotArgument(Value *BinOp) {
1528 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1529 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1530 Value *Op0 = BO->getOperand(0);
1531 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001532 if (isConstantAllOnes(Op0)) return Op1;
1533
1534 assert(isConstantAllOnes(Op1));
1535 return Op0;
1536}
1537
Chris Lattner2c7d1772005-04-24 07:28:37 +00001538const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1539 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001540}
1541
1542
1543// swapOperands - Exchange the two operands to this instruction. This
1544// instruction is safe to use on any binary instruction and does not
1545// modify the semantics of the instruction. If the instruction is
1546// order dependent (SetLT f.e.) the opcode is changed.
1547//
1548bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001549 if (!isCommutative())
1550 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001551 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001552 return false;
1553}
1554
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001555//===----------------------------------------------------------------------===//
1556// CastInst Class
1557//===----------------------------------------------------------------------===//
1558
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001559// Just determine if this cast only deals with integral->integral conversion.
1560bool CastInst::isIntegerCast() const {
1561 switch (getOpcode()) {
1562 default: return false;
1563 case Instruction::ZExt:
1564 case Instruction::SExt:
1565 case Instruction::Trunc:
1566 return true;
1567 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001568 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001569 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001570}
1571
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001572bool CastInst::isLosslessCast() const {
1573 // Only BitCast can be lossless, exit fast if we're not BitCast
1574 if (getOpcode() != Instruction::BitCast)
1575 return false;
1576
1577 // Identity cast is always lossless
1578 const Type* SrcTy = getOperand(0)->getType();
1579 const Type* DstTy = getType();
1580 if (SrcTy == DstTy)
1581 return true;
1582
Reid Spencer8d9336d2006-12-31 05:26:44 +00001583 // Pointer to pointer is always lossless.
1584 if (isa<PointerType>(SrcTy))
1585 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001586 return false; // Other types have no identity values
1587}
1588
1589/// This function determines if the CastInst does not require any bits to be
1590/// changed in order to effect the cast. Essentially, it identifies cases where
1591/// no code gen is necessary for the cast, hence the name no-op cast. For
1592/// example, the following are all no-op casts:
1593/// # bitcast uint %X, int
1594/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001595/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001596/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1597/// @brief Determine if a cast is a no-op.
1598bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1599 switch (getOpcode()) {
1600 default:
1601 assert(!"Invalid CastOp");
1602 case Instruction::Trunc:
1603 case Instruction::ZExt:
1604 case Instruction::SExt:
1605 case Instruction::FPTrunc:
1606 case Instruction::FPExt:
1607 case Instruction::UIToFP:
1608 case Instruction::SIToFP:
1609 case Instruction::FPToUI:
1610 case Instruction::FPToSI:
1611 return false; // These always modify bits
1612 case Instruction::BitCast:
1613 return true; // BitCast never modifies bits.
1614 case Instruction::PtrToInt:
1615 return IntPtrTy->getPrimitiveSizeInBits() ==
1616 getType()->getPrimitiveSizeInBits();
1617 case Instruction::IntToPtr:
1618 return IntPtrTy->getPrimitiveSizeInBits() ==
1619 getOperand(0)->getType()->getPrimitiveSizeInBits();
1620 }
1621}
1622
1623/// This function determines if a pair of casts can be eliminated and what
1624/// opcode should be used in the elimination. This assumes that there are two
1625/// instructions like this:
1626/// * %F = firstOpcode SrcTy %x to MidTy
1627/// * %S = secondOpcode MidTy %F to DstTy
1628/// The function returns a resultOpcode so these two casts can be replaced with:
1629/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1630/// If no such cast is permited, the function returns 0.
1631unsigned CastInst::isEliminableCastPair(
1632 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1633 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1634{
1635 // Define the 144 possibilities for these two cast instructions. The values
1636 // in this matrix determine what to do in a given situation and select the
1637 // case in the switch below. The rows correspond to firstOp, the columns
1638 // correspond to secondOp. In looking at the table below, keep in mind
1639 // the following cast properties:
1640 //
1641 // Size Compare Source Destination
1642 // Operator Src ? Size Type Sign Type Sign
1643 // -------- ------------ ------------------- ---------------------
1644 // TRUNC > Integer Any Integral Any
1645 // ZEXT < Integral Unsigned Integer Any
1646 // SEXT < Integral Signed Integer Any
1647 // FPTOUI n/a FloatPt n/a Integral Unsigned
1648 // FPTOSI n/a FloatPt n/a Integral Signed
1649 // UITOFP n/a Integral Unsigned FloatPt n/a
1650 // SITOFP n/a Integral Signed FloatPt n/a
1651 // FPTRUNC > FloatPt n/a FloatPt n/a
1652 // FPEXT < FloatPt n/a FloatPt n/a
1653 // PTRTOINT n/a Pointer n/a Integral Unsigned
1654 // INTTOPTR n/a Integral Unsigned Pointer n/a
1655 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001656 //
1657 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1658 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1659 // into "fptoui double to ulong", but this loses information about the range
1660 // of the produced value (we no longer know the top-part is all zeros).
1661 // Further this conversion is often much more expensive for typical hardware,
1662 // and causes issues when building libgcc. We disallow fptosi+sext for the
1663 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001664 const unsigned numCastOps =
1665 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1666 static const uint8_t CastResults[numCastOps][numCastOps] = {
1667 // T F F U S F F P I B -+
1668 // R Z S P P I I T P 2 N T |
1669 // U E E 2 2 2 2 R E I T C +- secondOp
1670 // N X X U S F F N X N 2 V |
1671 // C T T I I P P C T T P T -+
1672 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1673 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1674 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001675 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1676 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001677 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1678 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1679 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1680 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1681 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1682 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1683 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1684 };
1685
1686 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1687 [secondOp-Instruction::CastOpsBegin];
1688 switch (ElimCase) {
1689 case 0:
1690 // categorically disallowed
1691 return 0;
1692 case 1:
1693 // allowed, use first cast's opcode
1694 return firstOp;
1695 case 2:
1696 // allowed, use second cast's opcode
1697 return secondOp;
1698 case 3:
1699 // no-op cast in second op implies firstOp as long as the DestTy
1700 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001701 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001702 return firstOp;
1703 return 0;
1704 case 4:
1705 // no-op cast in second op implies firstOp as long as the DestTy
1706 // is floating point
1707 if (DstTy->isFloatingPoint())
1708 return firstOp;
1709 return 0;
1710 case 5:
1711 // no-op cast in first op implies secondOp as long as the SrcTy
1712 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001713 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001714 return secondOp;
1715 return 0;
1716 case 6:
1717 // no-op cast in first op implies secondOp as long as the SrcTy
1718 // is a floating point
1719 if (SrcTy->isFloatingPoint())
1720 return secondOp;
1721 return 0;
1722 case 7: {
1723 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1724 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1725 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1726 if (MidSize >= PtrSize)
1727 return Instruction::BitCast;
1728 return 0;
1729 }
1730 case 8: {
1731 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1732 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1733 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1734 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1735 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1736 if (SrcSize == DstSize)
1737 return Instruction::BitCast;
1738 else if (SrcSize < DstSize)
1739 return firstOp;
1740 return secondOp;
1741 }
1742 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1743 return Instruction::ZExt;
1744 case 10:
1745 // fpext followed by ftrunc is allowed if the bit size returned to is
1746 // the same as the original, in which case its just a bitcast
1747 if (SrcTy == DstTy)
1748 return Instruction::BitCast;
1749 return 0; // If the types are not the same we can't eliminate it.
1750 case 11:
1751 // bitcast followed by ptrtoint is allowed as long as the bitcast
1752 // is a pointer to pointer cast.
1753 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1754 return secondOp;
1755 return 0;
1756 case 12:
1757 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1758 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1759 return firstOp;
1760 return 0;
1761 case 13: {
1762 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1763 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1764 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1765 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1766 if (SrcSize <= PtrSize && SrcSize == DstSize)
1767 return Instruction::BitCast;
1768 return 0;
1769 }
1770 case 99:
1771 // cast combination can't happen (error in input). This is for all cases
1772 // where the MidTy is not the same for the two cast instructions.
1773 assert(!"Invalid Cast Combination");
1774 return 0;
1775 default:
1776 assert(!"Error in CastResults table!!!");
1777 return 0;
1778 }
1779 return 0;
1780}
1781
1782CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1783 const std::string &Name, Instruction *InsertBefore) {
1784 // Construct and return the appropriate CastInst subclass
1785 switch (op) {
1786 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1787 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1788 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1789 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1790 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1791 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1792 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1793 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1794 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1795 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1796 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1797 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1798 default:
1799 assert(!"Invalid opcode provided");
1800 }
1801 return 0;
1802}
1803
1804CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1805 const std::string &Name, BasicBlock *InsertAtEnd) {
1806 // Construct and return the appropriate CastInst subclass
1807 switch (op) {
1808 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1809 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1810 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1811 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1812 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1813 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1814 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1815 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1816 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1817 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1818 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1819 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1820 default:
1821 assert(!"Invalid opcode provided");
1822 }
1823 return 0;
1824}
1825
Reid Spencer5c140882006-12-04 20:17:56 +00001826CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1827 const std::string &Name,
1828 Instruction *InsertBefore) {
1829 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1830 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1831 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1832}
1833
1834CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1835 const std::string &Name,
1836 BasicBlock *InsertAtEnd) {
1837 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1838 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1839 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1840}
1841
1842CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1843 const std::string &Name,
1844 Instruction *InsertBefore) {
1845 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1846 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1847 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1848}
1849
1850CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1851 const std::string &Name,
1852 BasicBlock *InsertAtEnd) {
1853 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1854 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1855 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1856}
1857
1858CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1859 const std::string &Name,
1860 Instruction *InsertBefore) {
1861 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1862 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1863 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1864}
1865
1866CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1867 const std::string &Name,
1868 BasicBlock *InsertAtEnd) {
1869 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1870 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1871 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1872}
1873
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001874CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1875 const std::string &Name,
1876 BasicBlock *InsertAtEnd) {
1877 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001878 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001879 "Invalid cast");
1880
Chris Lattner03c49532007-01-15 02:27:26 +00001881 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001882 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1883 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1884}
1885
1886/// @brief Create a BitCast or a PtrToInt cast instruction
1887CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1888 const std::string &Name,
1889 Instruction *InsertBefore) {
1890 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001891 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001892 "Invalid cast");
1893
Chris Lattner03c49532007-01-15 02:27:26 +00001894 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001895 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1896 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1897}
1898
Reid Spencer7e933472006-12-12 00:49:44 +00001899CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1900 bool isSigned, const std::string &Name,
1901 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001902 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001903 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1904 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1905 Instruction::CastOps opcode =
1906 (SrcBits == DstBits ? Instruction::BitCast :
1907 (SrcBits > DstBits ? Instruction::Trunc :
1908 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1909 return create(opcode, C, Ty, Name, InsertBefore);
1910}
1911
1912CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1913 bool isSigned, const std::string &Name,
1914 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001915 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001916 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1917 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1918 Instruction::CastOps opcode =
1919 (SrcBits == DstBits ? Instruction::BitCast :
1920 (SrcBits > DstBits ? Instruction::Trunc :
1921 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1922 return create(opcode, C, Ty, Name, InsertAtEnd);
1923}
1924
1925CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1926 const std::string &Name,
1927 Instruction *InsertBefore) {
1928 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1929 "Invalid cast");
1930 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1931 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1932 Instruction::CastOps opcode =
1933 (SrcBits == DstBits ? Instruction::BitCast :
1934 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1935 return create(opcode, C, Ty, Name, InsertBefore);
1936}
1937
1938CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1939 const std::string &Name,
1940 BasicBlock *InsertAtEnd) {
1941 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1942 "Invalid cast");
1943 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1944 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1945 Instruction::CastOps opcode =
1946 (SrcBits == DstBits ? Instruction::BitCast :
1947 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1948 return create(opcode, C, Ty, Name, InsertAtEnd);
1949}
1950
Duncan Sands55e50902008-01-06 10:12:28 +00001951// Check whether it is valid to call getCastOpcode for these types.
1952// This routine must be kept in sync with getCastOpcode.
1953bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
1954 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
1955 return false;
1956
1957 if (SrcTy == DestTy)
1958 return true;
1959
1960 // Get the bit sizes, we'll need these
1961 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1962 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1963
1964 // Run through the possibilities ...
1965 if (DestTy->isInteger()) { // Casting to integral
1966 if (SrcTy->isInteger()) { // Casting from integral
1967 return true;
1968 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1969 return true;
1970 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1971 // Casting from vector
1972 return DestBits == PTy->getBitWidth();
1973 } else { // Casting from something else
1974 return isa<PointerType>(SrcTy);
1975 }
1976 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
1977 if (SrcTy->isInteger()) { // Casting from integral
1978 return true;
1979 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1980 return true;
1981 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1982 // Casting from vector
1983 return DestBits == PTy->getBitWidth();
1984 } else { // Casting from something else
1985 return false;
1986 }
1987 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1988 // Casting to vector
1989 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
1990 // Casting from vector
1991 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
1992 } else { // Casting from something else
1993 return DestPTy->getBitWidth() == SrcBits;
1994 }
1995 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
1996 if (isa<PointerType>(SrcTy)) { // Casting from pointer
1997 return true;
1998 } else if (SrcTy->isInteger()) { // Casting from integral
1999 return true;
2000 } else { // Casting from something else
2001 return false;
2002 }
2003 } else { // Casting to something else
2004 return false;
2005 }
2006}
2007
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002008// Provide a way to get a "cast" where the cast opcode is inferred from the
2009// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002010// logic in the castIsValid function below. This axiom should hold:
2011// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2012// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002013// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002014// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002015Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002016CastInst::getCastOpcode(
2017 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002018 // Get the bit sizes, we'll need these
2019 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002020 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2021 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002022
Duncan Sands55e50902008-01-06 10:12:28 +00002023 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2024 "Only first class types are castable!");
2025
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002026 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002027 if (DestTy->isInteger()) { // Casting to integral
2028 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002029 if (DestBits < SrcBits)
2030 return Trunc; // int -> smaller int
2031 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002032 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002033 return SExt; // signed -> SEXT
2034 else
2035 return ZExt; // unsigned -> ZEXT
2036 } else {
2037 return BitCast; // Same size, No-op cast
2038 }
2039 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002040 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002041 return FPToSI; // FP -> sint
2042 else
2043 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002044 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002045 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002046 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002047 return BitCast; // Same size, no-op cast
2048 } else {
2049 assert(isa<PointerType>(SrcTy) &&
2050 "Casting from a value that is not first-class type");
2051 return PtrToInt; // ptr -> int
2052 }
2053 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002054 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002055 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002056 return SIToFP; // sint -> FP
2057 else
2058 return UIToFP; // uint -> FP
2059 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2060 if (DestBits < SrcBits) {
2061 return FPTrunc; // FP -> smaller FP
2062 } else if (DestBits > SrcBits) {
2063 return FPExt; // FP -> larger FP
2064 } else {
2065 return BitCast; // same size, no-op cast
2066 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002067 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002068 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002069 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002070 return BitCast; // same size, no-op cast
2071 } else {
2072 assert(0 && "Casting pointer or non-first class to float");
2073 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002074 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2075 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002076 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002077 "Casting vector to vector of different widths");
2078 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002079 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002080 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002081 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002082 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002083 }
2084 } else if (isa<PointerType>(DestTy)) {
2085 if (isa<PointerType>(SrcTy)) {
2086 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002087 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002088 return IntToPtr; // int -> ptr
2089 } else {
2090 assert(!"Casting pointer to other than pointer or int");
2091 }
2092 } else {
2093 assert(!"Casting to type that is not first-class");
2094 }
2095
2096 // If we fall through to here we probably hit an assertion cast above
2097 // and assertions are not turned on. Anything we return is an error, so
2098 // BitCast is as good a choice as any.
2099 return BitCast;
2100}
2101
2102//===----------------------------------------------------------------------===//
2103// CastInst SubClass Constructors
2104//===----------------------------------------------------------------------===//
2105
2106/// Check that the construction parameters for a CastInst are correct. This
2107/// could be broken out into the separate constructors but it is useful to have
2108/// it in one place and to eliminate the redundant code for getting the sizes
2109/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002110bool
2111CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002112
2113 // Check for type sanity on the arguments
2114 const Type *SrcTy = S->getType();
2115 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2116 return false;
2117
2118 // Get the size of the types in bits, we'll need this later
2119 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2120 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2121
2122 // Switch on the opcode provided
2123 switch (op) {
2124 default: return false; // This is an input error
2125 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002126 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002127 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002128 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002129 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002130 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002131 case Instruction::FPTrunc:
2132 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2133 SrcBitSize > DstBitSize;
2134 case Instruction::FPExt:
2135 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2136 SrcBitSize < DstBitSize;
2137 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002138 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002139 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2140 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2141 return SVTy->getElementType()->isInteger() &&
2142 DVTy->getElementType()->isFloatingPoint() &&
2143 SVTy->getNumElements() == DVTy->getNumElements();
2144 }
2145 }
Chris Lattner03c49532007-01-15 02:27:26 +00002146 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002147 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002148 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002149 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2150 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2151 return SVTy->getElementType()->isFloatingPoint() &&
2152 DVTy->getElementType()->isInteger() &&
2153 SVTy->getNumElements() == DVTy->getNumElements();
2154 }
2155 }
Chris Lattner03c49532007-01-15 02:27:26 +00002156 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002157 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002158 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002159 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002160 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002161 case Instruction::BitCast:
2162 // BitCast implies a no-op cast of type only. No bits change.
2163 // However, you can't cast pointers to anything but pointers.
2164 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2165 return false;
2166
Duncan Sands55e50902008-01-06 10:12:28 +00002167 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002168 // these cases, the cast is okay if the source and destination bit widths
2169 // are identical.
2170 return SrcBitSize == DstBitSize;
2171 }
2172}
2173
2174TruncInst::TruncInst(
2175 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2176) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002177 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002178}
2179
2180TruncInst::TruncInst(
2181 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2182) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002183 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002184}
2185
2186ZExtInst::ZExtInst(
2187 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2188) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002189 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002190}
2191
2192ZExtInst::ZExtInst(
2193 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2194) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002195 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002196}
2197SExtInst::SExtInst(
2198 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2199) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002200 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002201}
2202
Jeff Cohencc08c832006-12-02 02:22:01 +00002203SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002204 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2205) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002206 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002207}
2208
2209FPTruncInst::FPTruncInst(
2210 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2211) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002212 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002213}
2214
2215FPTruncInst::FPTruncInst(
2216 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2217) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002218 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002219}
2220
2221FPExtInst::FPExtInst(
2222 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2223) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002224 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002225}
2226
2227FPExtInst::FPExtInst(
2228 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2229) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002230 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002231}
2232
2233UIToFPInst::UIToFPInst(
2234 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2235) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002236 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237}
2238
2239UIToFPInst::UIToFPInst(
2240 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2241) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002242 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002243}
2244
2245SIToFPInst::SIToFPInst(
2246 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2247) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002248 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002249}
2250
2251SIToFPInst::SIToFPInst(
2252 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2253) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002254 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002255}
2256
2257FPToUIInst::FPToUIInst(
2258 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2259) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002260 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002261}
2262
2263FPToUIInst::FPToUIInst(
2264 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2265) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002266 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002267}
2268
2269FPToSIInst::FPToSIInst(
2270 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2271) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002272 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002273}
2274
2275FPToSIInst::FPToSIInst(
2276 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2277) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002278 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002279}
2280
2281PtrToIntInst::PtrToIntInst(
2282 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2283) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002284 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002285}
2286
2287PtrToIntInst::PtrToIntInst(
2288 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2289) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002290 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002291}
2292
2293IntToPtrInst::IntToPtrInst(
2294 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2295) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002296 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002297}
2298
2299IntToPtrInst::IntToPtrInst(
2300 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2301) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002302 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002303}
2304
2305BitCastInst::BitCastInst(
2306 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2307) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002308 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002309}
2310
2311BitCastInst::BitCastInst(
2312 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2313) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002314 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002315}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002316
2317//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002318// CmpInst Classes
2319//===----------------------------------------------------------------------===//
2320
2321CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2322 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002323 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002324 Ops[0].init(LHS, this);
2325 Ops[1].init(RHS, this);
2326 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002327 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002328 if (op == Instruction::ICmp) {
2329 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2330 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2331 "Invalid ICmp predicate value");
2332 const Type* Op0Ty = getOperand(0)->getType();
2333 const Type* Op1Ty = getOperand(1)->getType();
2334 assert(Op0Ty == Op1Ty &&
2335 "Both operands to ICmp instruction are not of the same type!");
2336 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002337 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002338 "Invalid operand types for ICmp instruction");
2339 return;
2340 }
2341 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2342 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2343 "Invalid FCmp predicate value");
2344 const Type* Op0Ty = getOperand(0)->getType();
2345 const Type* Op1Ty = getOperand(1)->getType();
2346 assert(Op0Ty == Op1Ty &&
2347 "Both operands to FCmp instruction are not of the same type!");
2348 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002349 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002350 "Invalid operand types for FCmp instruction");
2351}
2352
2353CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2354 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002355 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002356 Ops[0].init(LHS, this);
2357 Ops[1].init(RHS, this);
2358 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002359 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002360 if (op == Instruction::ICmp) {
2361 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2362 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2363 "Invalid ICmp predicate value");
2364
2365 const Type* Op0Ty = getOperand(0)->getType();
2366 const Type* Op1Ty = getOperand(1)->getType();
2367 assert(Op0Ty == Op1Ty &&
2368 "Both operands to ICmp instruction are not of the same type!");
2369 // Check that the operands are the right type
Anton Korobeynikov579f0712008-02-20 11:08:44 +00002370 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002371 "Invalid operand types for ICmp instruction");
2372 return;
2373 }
2374 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2375 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2376 "Invalid FCmp predicate value");
2377 const Type* Op0Ty = getOperand(0)->getType();
2378 const Type* Op1Ty = getOperand(1)->getType();
2379 assert(Op0Ty == Op1Ty &&
2380 "Both operands to FCmp instruction are not of the same type!");
2381 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002382 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002383 "Invalid operand types for FCmp instruction");
2384}
2385
2386CmpInst *
2387CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2388 const std::string &Name, Instruction *InsertBefore) {
2389 if (Op == Instruction::ICmp) {
2390 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2391 InsertBefore);
2392 }
2393 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2394 InsertBefore);
2395}
2396
2397CmpInst *
2398CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2399 const std::string &Name, BasicBlock *InsertAtEnd) {
2400 if (Op == Instruction::ICmp) {
2401 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2402 InsertAtEnd);
2403 }
2404 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2405 InsertAtEnd);
2406}
2407
2408void CmpInst::swapOperands() {
2409 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2410 IC->swapOperands();
2411 else
2412 cast<FCmpInst>(this)->swapOperands();
2413}
2414
2415bool CmpInst::isCommutative() {
2416 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2417 return IC->isCommutative();
2418 return cast<FCmpInst>(this)->isCommutative();
2419}
2420
2421bool CmpInst::isEquality() {
2422 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2423 return IC->isEquality();
2424 return cast<FCmpInst>(this)->isEquality();
2425}
2426
2427
2428ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2429 switch (pred) {
2430 default:
2431 assert(!"Unknown icmp predicate!");
2432 case ICMP_EQ: return ICMP_NE;
2433 case ICMP_NE: return ICMP_EQ;
2434 case ICMP_UGT: return ICMP_ULE;
2435 case ICMP_ULT: return ICMP_UGE;
2436 case ICMP_UGE: return ICMP_ULT;
2437 case ICMP_ULE: return ICMP_UGT;
2438 case ICMP_SGT: return ICMP_SLE;
2439 case ICMP_SLT: return ICMP_SGE;
2440 case ICMP_SGE: return ICMP_SLT;
2441 case ICMP_SLE: return ICMP_SGT;
2442 }
2443}
2444
2445ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2446 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002447 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002448 case ICMP_EQ: case ICMP_NE:
2449 return pred;
2450 case ICMP_SGT: return ICMP_SLT;
2451 case ICMP_SLT: return ICMP_SGT;
2452 case ICMP_SGE: return ICMP_SLE;
2453 case ICMP_SLE: return ICMP_SGE;
2454 case ICMP_UGT: return ICMP_ULT;
2455 case ICMP_ULT: return ICMP_UGT;
2456 case ICMP_UGE: return ICMP_ULE;
2457 case ICMP_ULE: return ICMP_UGE;
2458 }
2459}
2460
Reid Spencer266e42b2006-12-23 06:05:41 +00002461ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2462 switch (pred) {
2463 default: assert(! "Unknown icmp predicate!");
2464 case ICMP_EQ: case ICMP_NE:
2465 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2466 return pred;
2467 case ICMP_UGT: return ICMP_SGT;
2468 case ICMP_ULT: return ICMP_SLT;
2469 case ICMP_UGE: return ICMP_SGE;
2470 case ICMP_ULE: return ICMP_SLE;
2471 }
2472}
2473
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002474ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2475 switch (pred) {
2476 default: assert(! "Unknown icmp predicate!");
2477 case ICMP_EQ: case ICMP_NE:
2478 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2479 return pred;
2480 case ICMP_SGT: return ICMP_UGT;
2481 case ICMP_SLT: return ICMP_ULT;
2482 case ICMP_SGE: return ICMP_UGE;
2483 case ICMP_SLE: return ICMP_ULE;
2484 }
2485}
2486
Reid Spencer266e42b2006-12-23 06:05:41 +00002487bool ICmpInst::isSignedPredicate(Predicate pred) {
2488 switch (pred) {
2489 default: assert(! "Unknown icmp predicate!");
2490 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2491 return true;
2492 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2493 case ICMP_UGE: case ICMP_ULE:
2494 return false;
2495 }
2496}
2497
Reid Spencer0286bc12007-02-28 22:00:54 +00002498/// Initialize a set of values that all satisfy the condition with C.
2499///
2500ConstantRange
2501ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2502 APInt Lower(C);
2503 APInt Upper(C);
2504 uint32_t BitWidth = C.getBitWidth();
2505 switch (pred) {
2506 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2507 case ICmpInst::ICMP_EQ: Upper++; break;
2508 case ICmpInst::ICMP_NE: Lower++; break;
2509 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2510 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2511 case ICmpInst::ICMP_UGT:
2512 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2513 break;
2514 case ICmpInst::ICMP_SGT:
2515 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2516 break;
2517 case ICmpInst::ICMP_ULE:
2518 Lower = APInt::getMinValue(BitWidth); Upper++;
2519 break;
2520 case ICmpInst::ICMP_SLE:
2521 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2522 break;
2523 case ICmpInst::ICMP_UGE:
2524 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2525 break;
2526 case ICmpInst::ICMP_SGE:
2527 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2528 break;
2529 }
2530 return ConstantRange(Lower, Upper);
2531}
2532
Reid Spencerd9436b62006-11-20 01:22:35 +00002533FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2534 switch (pred) {
2535 default:
2536 assert(!"Unknown icmp predicate!");
2537 case FCMP_OEQ: return FCMP_UNE;
2538 case FCMP_ONE: return FCMP_UEQ;
2539 case FCMP_OGT: return FCMP_ULE;
2540 case FCMP_OLT: return FCMP_UGE;
2541 case FCMP_OGE: return FCMP_ULT;
2542 case FCMP_OLE: return FCMP_UGT;
2543 case FCMP_UEQ: return FCMP_ONE;
2544 case FCMP_UNE: return FCMP_OEQ;
2545 case FCMP_UGT: return FCMP_OLE;
2546 case FCMP_ULT: return FCMP_OGE;
2547 case FCMP_UGE: return FCMP_OLT;
2548 case FCMP_ULE: return FCMP_OGT;
2549 case FCMP_ORD: return FCMP_UNO;
2550 case FCMP_UNO: return FCMP_ORD;
2551 case FCMP_TRUE: return FCMP_FALSE;
2552 case FCMP_FALSE: return FCMP_TRUE;
2553 }
2554}
2555
2556FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2557 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002558 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002559 case FCMP_FALSE: case FCMP_TRUE:
2560 case FCMP_OEQ: case FCMP_ONE:
2561 case FCMP_UEQ: case FCMP_UNE:
2562 case FCMP_ORD: case FCMP_UNO:
2563 return pred;
2564 case FCMP_OGT: return FCMP_OLT;
2565 case FCMP_OLT: return FCMP_OGT;
2566 case FCMP_OGE: return FCMP_OLE;
2567 case FCMP_OLE: return FCMP_OGE;
2568 case FCMP_UGT: return FCMP_ULT;
2569 case FCMP_ULT: return FCMP_UGT;
2570 case FCMP_UGE: return FCMP_ULE;
2571 case FCMP_ULE: return FCMP_UGE;
2572 }
2573}
2574
Reid Spencer266e42b2006-12-23 06:05:41 +00002575bool CmpInst::isUnsigned(unsigned short predicate) {
2576 switch (predicate) {
2577 default: return false;
2578 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2579 case ICmpInst::ICMP_UGE: return true;
2580 }
2581}
2582
2583bool CmpInst::isSigned(unsigned short predicate){
2584 switch (predicate) {
2585 default: return false;
2586 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2587 case ICmpInst::ICMP_SGE: return true;
2588 }
2589}
2590
2591bool CmpInst::isOrdered(unsigned short predicate) {
2592 switch (predicate) {
2593 default: return false;
2594 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2595 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2596 case FCmpInst::FCMP_ORD: return true;
2597 }
2598}
2599
2600bool CmpInst::isUnordered(unsigned short predicate) {
2601 switch (predicate) {
2602 default: return false;
2603 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2604 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2605 case FCmpInst::FCMP_UNO: return true;
2606 }
2607}
2608
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002609//===----------------------------------------------------------------------===//
2610// SwitchInst Implementation
2611//===----------------------------------------------------------------------===//
2612
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002613void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002614 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002615 ReservedSpace = 2+NumCases*2;
2616 NumOperands = 2;
2617 OperandList = new Use[ReservedSpace];
2618
2619 OperandList[0].init(Value, this);
2620 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002621}
2622
Chris Lattner2195fc42007-02-24 00:55:48 +00002623/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2624/// switch on and a default destination. The number of additional cases can
2625/// be specified here to make memory allocation more efficient. This
2626/// constructor can also autoinsert before another instruction.
2627SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2628 Instruction *InsertBefore)
2629 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2630 init(Value, Default, NumCases);
2631}
2632
2633/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2634/// switch on and a default destination. The number of additional cases can
2635/// be specified here to make memory allocation more efficient. This
2636/// constructor also autoinserts at the end of the specified BasicBlock.
2637SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2638 BasicBlock *InsertAtEnd)
2639 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2640 init(Value, Default, NumCases);
2641}
2642
Misha Brukmanb1c93172005-04-21 23:48:37 +00002643SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002644 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2645 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002646 Use *OL = OperandList, *InOL = SI.OperandList;
2647 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2648 OL[i].init(InOL[i], this);
2649 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002650 }
2651}
2652
Gordon Henriksen14a55692007-12-10 02:14:30 +00002653SwitchInst::~SwitchInst() {
2654 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002655}
2656
2657
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002658/// addCase - Add an entry to the switch instruction...
2659///
Chris Lattner47ac1872005-02-24 05:32:09 +00002660void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002661 unsigned OpNo = NumOperands;
2662 if (OpNo+2 > ReservedSpace)
2663 resizeOperands(0); // Get more space!
2664 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002665 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002666 NumOperands = OpNo+2;
2667 OperandList[OpNo].init(OnVal, this);
2668 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002669}
2670
2671/// removeCase - This method removes the specified successor from the switch
2672/// instruction. Note that this cannot be used to remove the default
2673/// destination (successor #0).
2674///
2675void SwitchInst::removeCase(unsigned idx) {
2676 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002677 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2678
2679 unsigned NumOps = getNumOperands();
2680 Use *OL = OperandList;
2681
2682 // Move everything after this operand down.
2683 //
2684 // FIXME: we could just swap with the end of the list, then erase. However,
2685 // client might not expect this to happen. The code as it is thrashes the
2686 // use/def lists, which is kinda lame.
2687 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2688 OL[i-2] = OL[i];
2689 OL[i-2+1] = OL[i+1];
2690 }
2691
2692 // Nuke the last value.
2693 OL[NumOps-2].set(0);
2694 OL[NumOps-2+1].set(0);
2695 NumOperands = NumOps-2;
2696}
2697
2698/// resizeOperands - resize operands - This adjusts the length of the operands
2699/// list according to the following behavior:
2700/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2701/// of operation. This grows the number of ops by 1.5 times.
2702/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2703/// 3. If NumOps == NumOperands, trim the reserved space.
2704///
2705void SwitchInst::resizeOperands(unsigned NumOps) {
2706 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002707 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002708 } else if (NumOps*2 > NumOperands) {
2709 // No resize needed.
2710 if (ReservedSpace >= NumOps) return;
2711 } else if (NumOps == NumOperands) {
2712 if (ReservedSpace == NumOps) return;
2713 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002714 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002715 }
2716
2717 ReservedSpace = NumOps;
2718 Use *NewOps = new Use[NumOps];
2719 Use *OldOps = OperandList;
2720 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2721 NewOps[i].init(OldOps[i], this);
2722 OldOps[i].set(0);
2723 }
2724 delete [] OldOps;
2725 OperandList = NewOps;
2726}
2727
2728
2729BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2730 return getSuccessor(idx);
2731}
2732unsigned SwitchInst::getNumSuccessorsV() const {
2733 return getNumSuccessors();
2734}
2735void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2736 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002737}
Chris Lattnerf22be932004-10-15 23:52:53 +00002738
Devang Patel295711f2008-02-19 22:15:16 +00002739//===----------------------------------------------------------------------===//
2740// GetResultInst Implementation
2741//===----------------------------------------------------------------------===//
2742
Devang Patel666d4512008-02-20 19:10:47 +00002743GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002744 const std::string &Name,
2745 Instruction *InsertBef)
Devang Pateldcad9da2008-02-20 19:26:55 +00002746 : Instruction(cast<StructType>(Aggregate->getType())->getElementType(Index),
Devang Patel666d4512008-02-20 19:10:47 +00002747 GetResult, &Aggr, 1, InsertBef) {
2748 assert(isValidOperands(Aggregate, Index) && "Invalid GetResultInst operands!");
2749 Aggr.init(Aggregate, this);
2750 Idx = Index;
Devang Patel295711f2008-02-19 22:15:16 +00002751 setName(Name);
2752}
2753
Devang Patel666d4512008-02-20 19:10:47 +00002754bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2755 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002756 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002757
Devang Patelcf193b82008-02-20 19:39:41 +00002758 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2759 unsigned NumElements = STy->getNumElements();
2760 if (Index >= NumElements)
2761 return false;
2762
2763 // getresult aggregate value's element types are restricted to
2764 // avoid nested aggregates.
2765 for (unsigned i = 0; i < NumElements; ++i)
2766 if (!STy->getElementType(i)->isFirstClassType())
2767 return false;
2768
2769 // Otherwise, Aggregate is valid.
2770 return true;
2771 }
Devang Patel666d4512008-02-20 19:10:47 +00002772 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002773}
2774
Chris Lattnerf22be932004-10-15 23:52:53 +00002775// Define these methods here so vtables don't get emitted into every translation
2776// unit that uses these classes.
2777
2778GetElementPtrInst *GetElementPtrInst::clone() const {
2779 return new GetElementPtrInst(*this);
2780}
2781
2782BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002783 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002784}
2785
Chris Lattner0b490b02007-08-24 20:48:18 +00002786FCmpInst* FCmpInst::clone() const {
2787 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2788}
2789ICmpInst* ICmpInst::clone() const {
2790 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002791}
2792
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002793MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2794AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2795FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2796LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2797StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2798CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2799CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2800CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2801CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2802CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2803CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2804CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2805CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2806CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2807CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2808CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2809CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2810CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002811SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2812VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2813
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002814ExtractElementInst *ExtractElementInst::clone() const {
2815 return new ExtractElementInst(*this);
2816}
2817InsertElementInst *InsertElementInst::clone() const {
2818 return new InsertElementInst(*this);
2819}
2820ShuffleVectorInst *ShuffleVectorInst::clone() const {
2821 return new ShuffleVectorInst(*this);
2822}
Chris Lattnerf22be932004-10-15 23:52:53 +00002823PHINode *PHINode::clone() const { return new PHINode(*this); }
2824ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2825BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2826SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2827InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2828UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002829UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002830GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }