blob: 156eff169c1ece8e7a4cc97e9114f7ce34900cce [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/BasicBlock.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Reid Spencerce38beb2007-04-09 18:00:57 +000020#include "llvm/ParameterAttributes.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000023#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024using namespace llvm;
25
Chris Lattner3e13b8c2008-01-02 23:42:30 +000026//===----------------------------------------------------------------------===//
27// CallSite Class
28//===----------------------------------------------------------------------===//
29
Chris Lattnerf7b6d312005-05-06 20:26:43 +000030unsigned CallSite::getCallingConv() const {
31 if (CallInst *CI = dyn_cast<CallInst>(I))
32 return CI->getCallingConv();
33 else
34 return cast<InvokeInst>(I)->getCallingConv();
35}
36void CallSite::setCallingConv(unsigned CC) {
37 if (CallInst *CI = dyn_cast<CallInst>(I))
38 CI->setCallingConv(CC);
39 else
40 cast<InvokeInst>(I)->setCallingConv(CC);
41}
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000042const ParamAttrsList* CallSite::getParamAttrs() const {
43 if (CallInst *CI = dyn_cast<CallInst>(I))
44 return CI->getParamAttrs();
45 else
46 return cast<InvokeInst>(I)->getParamAttrs();
47}
48void CallSite::setParamAttrs(const ParamAttrsList *PAL) {
49 if (CallInst *CI = dyn_cast<CallInst>(I))
50 CI->setParamAttrs(PAL);
51 else
52 cast<InvokeInst>(I)->setParamAttrs(PAL);
53}
Chris Lattner3e13b8c2008-01-02 23:42:30 +000054bool CallSite::paramHasAttr(uint16_t i, unsigned attr) const {
Duncan Sands5208d1a2007-11-28 17:07:01 +000055 if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner3e13b8c2008-01-02 23:42:30 +000056 return CI->paramHasAttr(i, (ParameterAttributes)attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000057 else
Chris Lattner3e13b8c2008-01-02 23:42:30 +000058 return cast<InvokeInst>(I)->paramHasAttr(i, (ParameterAttributes)attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000059}
Duncan Sands38ef3a82007-12-03 20:06:50 +000060bool CallSite::doesNotAccessMemory() const {
61 if (CallInst *CI = dyn_cast<CallInst>(I))
62 return CI->doesNotAccessMemory();
63 else
64 return cast<InvokeInst>(I)->doesNotAccessMemory();
65}
66bool CallSite::onlyReadsMemory() const {
67 if (CallInst *CI = dyn_cast<CallInst>(I))
68 return CI->onlyReadsMemory();
69 else
70 return cast<InvokeInst>(I)->onlyReadsMemory();
71}
Duncan Sands3353ed02007-12-18 09:59:50 +000072bool CallSite::doesNotThrow() const {
Duncan Sands8e4847e2007-12-16 15:51:49 +000073 if (CallInst *CI = dyn_cast<CallInst>(I))
Duncan Sands3353ed02007-12-18 09:59:50 +000074 return CI->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000075 else
Duncan Sands3353ed02007-12-18 09:59:50 +000076 return cast<InvokeInst>(I)->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000077}
Duncan Sandsaa31b922007-12-19 21:13:37 +000078void CallSite::setDoesNotThrow(bool doesNotThrow) {
79 if (CallInst *CI = dyn_cast<CallInst>(I))
80 CI->setDoesNotThrow(doesNotThrow);
81 else
82 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
83}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000084
Gordon Henriksen14a55692007-12-10 02:14:30 +000085//===----------------------------------------------------------------------===//
86// TerminatorInst Class
87//===----------------------------------------------------------------------===//
88
89// Out of line virtual method, so the vtable, etc has a home.
90TerminatorInst::~TerminatorInst() {
91}
92
93// Out of line virtual method, so the vtable, etc has a home.
94UnaryInstruction::~UnaryInstruction() {
95}
96
97
Chris Lattnerafdb3de2005-01-29 00:35:16 +000098//===----------------------------------------------------------------------===//
99// PHINode Class
100//===----------------------------------------------------------------------===//
101
102PHINode::PHINode(const PHINode &PN)
103 : Instruction(PN.getType(), Instruction::PHI,
104 new Use[PN.getNumOperands()], PN.getNumOperands()),
105 ReservedSpace(PN.getNumOperands()) {
106 Use *OL = OperandList;
107 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
108 OL[i].init(PN.getOperand(i), this);
109 OL[i+1].init(PN.getOperand(i+1), this);
110 }
111}
112
Gordon Henriksen14a55692007-12-10 02:14:30 +0000113PHINode::~PHINode() {
114 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000115}
116
117// removeIncomingValue - Remove an incoming value. This is useful if a
118// predecessor basic block is deleted.
119Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
120 unsigned NumOps = getNumOperands();
121 Use *OL = OperandList;
122 assert(Idx*2 < NumOps && "BB not in PHI node!");
123 Value *Removed = OL[Idx*2];
124
125 // Move everything after this operand down.
126 //
127 // FIXME: we could just swap with the end of the list, then erase. However,
128 // client might not expect this to happen. The code as it is thrashes the
129 // use/def lists, which is kinda lame.
130 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
131 OL[i-2] = OL[i];
132 OL[i-2+1] = OL[i+1];
133 }
134
135 // Nuke the last value.
136 OL[NumOps-2].set(0);
137 OL[NumOps-2+1].set(0);
138 NumOperands = NumOps-2;
139
140 // If the PHI node is dead, because it has zero entries, nuke it now.
141 if (NumOps == 2 && DeletePHIIfEmpty) {
142 // If anyone is using this PHI, make them use a dummy value instead...
143 replaceAllUsesWith(UndefValue::get(getType()));
144 eraseFromParent();
145 }
146 return Removed;
147}
148
149/// resizeOperands - resize operands - This adjusts the length of the operands
150/// list according to the following behavior:
151/// 1. If NumOps == 0, grow the operand list in response to a push_back style
152/// of operation. This grows the number of ops by 1.5 times.
153/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
154/// 3. If NumOps == NumOperands, trim the reserved space.
155///
156void PHINode::resizeOperands(unsigned NumOps) {
157 if (NumOps == 0) {
158 NumOps = (getNumOperands())*3/2;
159 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
160 } else if (NumOps*2 > NumOperands) {
161 // No resize needed.
162 if (ReservedSpace >= NumOps) return;
163 } else if (NumOps == NumOperands) {
164 if (ReservedSpace == NumOps) return;
165 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000166 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000167 }
168
169 ReservedSpace = NumOps;
170 Use *NewOps = new Use[NumOps];
171 Use *OldOps = OperandList;
172 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
173 NewOps[i].init(OldOps[i], this);
174 OldOps[i].set(0);
175 }
176 delete [] OldOps;
177 OperandList = NewOps;
178}
179
Nate Begemanb3923212005-08-04 23:24:19 +0000180/// hasConstantValue - If the specified PHI node always merges together the same
181/// value, return the value, otherwise return null.
182///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000183Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000184 // If the PHI node only has one incoming value, eliminate the PHI node...
185 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000186 if (getIncomingValue(0) != this) // not X = phi X
187 return getIncomingValue(0);
188 else
189 return UndefValue::get(getType()); // Self cycle is dead.
190
Nate Begemanb3923212005-08-04 23:24:19 +0000191 // Otherwise if all of the incoming values are the same for the PHI, replace
192 // the PHI node with the incoming value.
193 //
194 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000195 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000196 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000197 if (isa<UndefValue>(getIncomingValue(i)))
198 HasUndefInput = true;
199 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000200 if (InVal && getIncomingValue(i) != InVal)
201 return 0; // Not the same, bail out.
202 else
203 InVal = getIncomingValue(i);
204
205 // The only case that could cause InVal to be null is if we have a PHI node
206 // that only has entries for itself. In this case, there is no entry into the
207 // loop, so kill the PHI.
208 //
209 if (InVal == 0) InVal = UndefValue::get(getType());
210
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000211 // If we have a PHI node like phi(X, undef, X), where X is defined by some
212 // instruction, we cannot always return X as the result of the PHI node. Only
213 // do this if X is not an instruction (thus it must dominate the PHI block),
214 // or if the client is prepared to deal with this possibility.
215 if (HasUndefInput && !AllowNonDominatingInstruction)
216 if (Instruction *IV = dyn_cast<Instruction>(InVal))
217 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000218 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000219 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000220 return 0; // Cannot guarantee that InVal dominates this PHINode.
221
Nate Begemanb3923212005-08-04 23:24:19 +0000222 // All of the incoming values are the same, return the value now.
223 return InVal;
224}
225
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000226
227//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000228// CallInst Implementation
229//===----------------------------------------------------------------------===//
230
Gordon Henriksen14a55692007-12-10 02:14:30 +0000231CallInst::~CallInst() {
232 delete [] OperandList;
233 if (ParamAttrs)
234 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000235}
236
Chris Lattner054ba2c2007-02-13 00:58:44 +0000237void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000238 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000239 NumOperands = NumParams+1;
240 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000241 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000242
Misha Brukmanb1c93172005-04-21 23:48:37 +0000243 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000244 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000245 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000246
Chris Lattner054ba2c2007-02-13 00:58:44 +0000247 assert((NumParams == FTy->getNumParams() ||
248 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000249 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000250 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000251 assert((i >= FTy->getNumParams() ||
252 FTy->getParamType(i) == Params[i]->getType()) &&
253 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000254 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000255 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000256}
257
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000258void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000259 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000260 NumOperands = 3;
261 Use *OL = OperandList = new Use[3];
262 OL[0].init(Func, this);
263 OL[1].init(Actual1, this);
264 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000265
266 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000267 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000268 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000270 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000271 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000273 assert((0 >= FTy->getNumParams() ||
274 FTy->getParamType(0) == Actual1->getType()) &&
275 "Calling a function with a bad signature!");
276 assert((1 >= FTy->getNumParams() ||
277 FTy->getParamType(1) == Actual2->getType()) &&
278 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279}
280
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000281void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000282 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000283 NumOperands = 2;
284 Use *OL = OperandList = new Use[2];
285 OL[0].init(Func, this);
286 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000287
288 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000290 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000291
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000292 assert((FTy->getNumParams() == 1 ||
293 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000295 assert((0 == FTy->getNumParams() ||
296 FTy->getParamType(0) == Actual->getType()) &&
297 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298}
299
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000300void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000301 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000302 NumOperands = 1;
303 Use *OL = OperandList = new Use[1];
304 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000305
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000306 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000308 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000310 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311}
312
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000314 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
316 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000317 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000319 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320}
321
322CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
323 BasicBlock *InsertAtEnd)
324 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
325 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000326 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000328 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330CallInst::CallInst(Value *Func, const std::string &Name,
331 Instruction *InsertBefore)
332 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
333 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000334 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000336 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000337}
338
339CallInst::CallInst(Value *Func, const std::string &Name,
340 BasicBlock *InsertAtEnd)
341 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
342 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000343 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000344 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000345 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000346}
347
Misha Brukmanb1c93172005-04-21 23:48:37 +0000348CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000349 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000350 CI.getNumOperands()),
351 ParamAttrs(0) {
352 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000353 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000354 Use *OL = OperandList;
355 Use *InOL = CI.OperandList;
356 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
357 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000358}
359
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000360void CallInst::setParamAttrs(const ParamAttrsList *newAttrs) {
361 if (ParamAttrs == newAttrs)
362 return;
363
Reid Spencerc6a83842007-04-22 17:28:03 +0000364 if (ParamAttrs)
365 ParamAttrs->dropRef();
366
367 if (newAttrs)
368 newAttrs->addRef();
369
370 ParamAttrs = newAttrs;
371}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000372
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000373bool CallInst::paramHasAttr(uint16_t i, unsigned attr) const {
374 if (ParamAttrs && ParamAttrs->paramHasAttr(i, (ParameterAttributes)attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000375 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000376 if (const Function *F = getCalledFunction())
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000377 return F->paramHasAttr(i, (ParameterAttributes)attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000378 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000379}
380
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000381/// @brief Determine if the call does not access memory.
382bool CallInst::doesNotAccessMemory() const {
383 return paramHasAttr(0, ParamAttr::ReadNone);
384}
385
386/// @brief Determine if the call does not access or only reads memory.
387bool CallInst::onlyReadsMemory() const {
388 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
389}
390
391/// @brief Determine if the call cannot return.
392bool CallInst::doesNotReturn() const {
393 return paramHasAttr(0, ParamAttr::NoReturn);
394}
395
396/// @brief Determine if the call cannot unwind.
397bool CallInst::doesNotThrow() const {
398 return paramHasAttr(0, ParamAttr::NoUnwind);
399}
400
401/// @brief Determine if the call returns a structure.
402bool CallInst::isStructReturn() const {
403 // Be friendly and also check the callee.
404 return paramHasAttr(1, ParamAttr::StructRet);
405}
406
Evan Cheng09dde602008-01-12 18:57:32 +0000407/// @brief Determine if any call argument is an aggregate passed by value.
408bool CallInst::hasByValArgument() const {
Duncan Sandsa59f3962008-01-21 11:27:55 +0000409 if (ParamAttrs && ParamAttrs->hasAttrSomewhere(ParamAttr::ByVal))
410 return true;
411 // Be consistent with other methods and check the callee too.
412 if (const Function *F = getCalledFunction())
413 if (const ParamAttrsList *PAL = F->getParamAttrs())
414 return PAL->hasAttrSomewhere(ParamAttr::ByVal);
415 return false;
Evan Cheng09dde602008-01-12 18:57:32 +0000416}
417
Duncan Sandsaa31b922007-12-19 21:13:37 +0000418void CallInst::setDoesNotThrow(bool doesNotThrow) {
419 const ParamAttrsList *PAL = getParamAttrs();
420 if (doesNotThrow)
421 PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind);
422 else
423 PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind);
424 setParamAttrs(PAL);
425}
426
Duncan Sands5208d1a2007-11-28 17:07:01 +0000427
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000428//===----------------------------------------------------------------------===//
429// InvokeInst Implementation
430//===----------------------------------------------------------------------===//
431
Gordon Henriksen14a55692007-12-10 02:14:30 +0000432InvokeInst::~InvokeInst() {
433 delete [] OperandList;
434 if (ParamAttrs)
435 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000436}
437
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000438void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000439 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000440 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000441 NumOperands = 3+NumArgs;
442 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000443 OL[0].init(Fn, this);
444 OL[1].init(IfNormal, this);
445 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000446 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000447 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000448 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000449
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000450 assert((NumArgs == FTy->getNumParams()) ||
451 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000452 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000453
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000454 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000455 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000456 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000457 "Invoking a function with a bad signature!");
458
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000459 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000460 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000461}
462
Misha Brukmanb1c93172005-04-21 23:48:37 +0000463InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000464 : TerminatorInst(II.getType(), Instruction::Invoke,
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000465 new Use[II.getNumOperands()], II.getNumOperands()),
466 ParamAttrs(0) {
467 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000468 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000469 Use *OL = OperandList, *InOL = II.OperandList;
470 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
471 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000472}
473
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000474BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
475 return getSuccessor(idx);
476}
477unsigned InvokeInst::getNumSuccessorsV() const {
478 return getNumSuccessors();
479}
480void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
481 return setSuccessor(idx, B);
482}
483
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000484void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) {
485 if (ParamAttrs == newAttrs)
486 return;
487
Reid Spencerc6a83842007-04-22 17:28:03 +0000488 if (ParamAttrs)
489 ParamAttrs->dropRef();
490
491 if (newAttrs)
492 newAttrs->addRef();
493
494 ParamAttrs = newAttrs;
495}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000496
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000497bool InvokeInst::paramHasAttr(uint16_t i, unsigned attr) const {
498 if (ParamAttrs && ParamAttrs->paramHasAttr(i, (ParameterAttributes)attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000499 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000500 if (const Function *F = getCalledFunction())
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000501 return F->paramHasAttr(i, (ParameterAttributes)attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000502 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000503}
504
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000505
506/// @brief Determine if the call does not access memory.
507bool InvokeInst::doesNotAccessMemory() const {
508 return paramHasAttr(0, ParamAttr::ReadNone);
509}
510
511/// @brief Determine if the call does not access or only reads memory.
512bool InvokeInst::onlyReadsMemory() const {
513 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
514}
515
516/// @brief Determine if the call cannot return.
517bool InvokeInst::doesNotReturn() const {
518 return paramHasAttr(0, ParamAttr::NoReturn);
519}
520
521/// @brief Determine if the call cannot unwind.
522bool InvokeInst::doesNotThrow() const {
523 return paramHasAttr(0, ParamAttr::NoUnwind);
524}
525
Duncan Sandsaa31b922007-12-19 21:13:37 +0000526void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
527 const ParamAttrsList *PAL = getParamAttrs();
528 if (doesNotThrow)
529 PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind);
530 else
531 PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind);
532 setParamAttrs(PAL);
533}
534
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000535/// @brief Determine if the call returns a structure.
536bool InvokeInst::isStructReturn() const {
537 // Be friendly and also check the callee.
538 return paramHasAttr(1, ParamAttr::StructRet);
539}
540
Duncan Sands5208d1a2007-11-28 17:07:01 +0000541
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000542//===----------------------------------------------------------------------===//
543// ReturnInst Implementation
544//===----------------------------------------------------------------------===//
545
Chris Lattner2195fc42007-02-24 00:55:48 +0000546ReturnInst::ReturnInst(const ReturnInst &RI)
547 : TerminatorInst(Type::VoidTy, Instruction::Ret,
548 &RetVal, RI.getNumOperands()) {
549 if (RI.getNumOperands())
550 RetVal.init(RI.RetVal, this);
551}
552
553ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
554 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
555 init(retVal);
556}
557ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
558 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
559 init(retVal);
560}
561ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
562 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
563}
564
565
566
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000567void ReturnInst::init(Value *retVal) {
568 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000569 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000570 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000571 NumOperands = 1;
572 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000573 }
574}
575
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000576unsigned ReturnInst::getNumSuccessorsV() const {
577 return getNumSuccessors();
578}
579
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000580// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
581// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000582void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000583 assert(0 && "ReturnInst has no successors!");
584}
585
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000586BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
587 assert(0 && "ReturnInst has no successors!");
588 abort();
589 return 0;
590}
591
592
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000593//===----------------------------------------------------------------------===//
594// UnwindInst Implementation
595//===----------------------------------------------------------------------===//
596
Chris Lattner2195fc42007-02-24 00:55:48 +0000597UnwindInst::UnwindInst(Instruction *InsertBefore)
598 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
599}
600UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
601 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
602}
603
604
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000605unsigned UnwindInst::getNumSuccessorsV() const {
606 return getNumSuccessors();
607}
608
609void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000610 assert(0 && "UnwindInst has no successors!");
611}
612
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000613BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
614 assert(0 && "UnwindInst has no successors!");
615 abort();
616 return 0;
617}
618
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000619//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000620// UnreachableInst Implementation
621//===----------------------------------------------------------------------===//
622
Chris Lattner2195fc42007-02-24 00:55:48 +0000623UnreachableInst::UnreachableInst(Instruction *InsertBefore)
624 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
625}
626UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
627 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
628}
629
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000630unsigned UnreachableInst::getNumSuccessorsV() const {
631 return getNumSuccessors();
632}
633
634void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
635 assert(0 && "UnwindInst has no successors!");
636}
637
638BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
639 assert(0 && "UnwindInst has no successors!");
640 abort();
641 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000642}
643
644//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000645// BranchInst Implementation
646//===----------------------------------------------------------------------===//
647
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648void BranchInst::AssertOK() {
649 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000650 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000651 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000652}
653
Chris Lattner2195fc42007-02-24 00:55:48 +0000654BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
655 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
656 assert(IfTrue != 0 && "Branch destination may not be null!");
657 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
658}
659BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
660 Instruction *InsertBefore)
661: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
662 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
663 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
664 Ops[2].init(Cond, this);
665#ifndef NDEBUG
666 AssertOK();
667#endif
668}
669
670BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
671 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
672 assert(IfTrue != 0 && "Branch destination may not be null!");
673 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
674}
675
676BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
677 BasicBlock *InsertAtEnd)
678 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
679 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
680 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
681 Ops[2].init(Cond, this);
682#ifndef NDEBUG
683 AssertOK();
684#endif
685}
686
687
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000688BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000689 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000690 OperandList[0].init(BI.getOperand(0), this);
691 if (BI.getNumOperands() != 1) {
692 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
693 OperandList[1].init(BI.getOperand(1), this);
694 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000695 }
696}
697
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000698BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
699 return getSuccessor(idx);
700}
701unsigned BranchInst::getNumSuccessorsV() const {
702 return getNumSuccessors();
703}
704void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
705 setSuccessor(idx, B);
706}
707
708
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000709//===----------------------------------------------------------------------===//
710// AllocationInst Implementation
711//===----------------------------------------------------------------------===//
712
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000713static Value *getAISize(Value *Amt) {
714 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000715 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000716 else {
717 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000718 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000719 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000720 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000721 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000722 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000723}
724
Misha Brukmanb1c93172005-04-21 23:48:37 +0000725AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000726 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000727 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000728 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000729 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000730 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000731 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000732 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733}
734
Misha Brukmanb1c93172005-04-21 23:48:37 +0000735AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000736 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000737 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000738 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000739 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000740 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000741 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000742 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000743}
744
Gordon Henriksen14a55692007-12-10 02:14:30 +0000745// Out of line virtual method, so the vtable, etc has a home.
746AllocationInst::~AllocationInst() {
747}
748
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000749bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000750 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
751 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000752 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000753}
754
755const Type *AllocationInst::getAllocatedType() const {
756 return getType()->getElementType();
757}
758
759AllocaInst::AllocaInst(const AllocaInst &AI)
760 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000761 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000762}
763
764MallocInst::MallocInst(const MallocInst &MI)
765 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000766 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000767}
768
769//===----------------------------------------------------------------------===//
770// FreeInst Implementation
771//===----------------------------------------------------------------------===//
772
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000773void FreeInst::AssertOK() {
774 assert(isa<PointerType>(getOperand(0)->getType()) &&
775 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000776}
777
778FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000779 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000780 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000781}
782
783FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000784 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000785 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000786}
787
788
789//===----------------------------------------------------------------------===//
790// LoadInst Implementation
791//===----------------------------------------------------------------------===//
792
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000793void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000794 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000795 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000796}
797
798LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000799 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000800 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000801 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000802 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000803 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000804 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000805}
806
807LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000808 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000809 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000810 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000811 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000812 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000813 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000814}
815
816LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
817 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000818 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000819 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000820 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000821 setAlignment(0);
822 AssertOK();
823 setName(Name);
824}
825
826LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
827 unsigned Align, Instruction *InsertBef)
828 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
829 Load, Ptr, InsertBef) {
830 setVolatile(isVolatile);
831 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000832 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000833 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000834}
835
Dan Gohman68659282007-07-18 20:51:11 +0000836LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
837 unsigned Align, BasicBlock *InsertAE)
838 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
839 Load, Ptr, InsertAE) {
840 setVolatile(isVolatile);
841 setAlignment(Align);
842 AssertOK();
843 setName(Name);
844}
845
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000846LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
847 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000848 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000849 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000850 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000851 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000852 AssertOK();
853 setName(Name);
854}
855
856
857
858LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000859 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
860 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000861 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000862 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000863 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000864 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000865}
866
867LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000868 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
869 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000870 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000871 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000872 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000873 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000874}
875
876LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
877 Instruction *InsertBef)
878: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000879 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000880 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000881 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000882 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000883 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000884}
885
886LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
887 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000888 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
889 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000890 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000891 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000892 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000893 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000894}
895
Christopher Lamb84485702007-04-22 19:24:39 +0000896void LoadInst::setAlignment(unsigned Align) {
897 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
898 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
899}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000900
901//===----------------------------------------------------------------------===//
902// StoreInst Implementation
903//===----------------------------------------------------------------------===//
904
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000905void StoreInst::AssertOK() {
906 assert(isa<PointerType>(getOperand(1)->getType()) &&
907 "Ptr must have pointer type!");
908 assert(getOperand(0)->getType() ==
909 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000910 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000911}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000912
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000913
914StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000915 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000916 Ops[0].init(val, this);
917 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000918 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000919 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000920 AssertOK();
921}
922
923StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000924 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000925 Ops[0].init(val, this);
926 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000927 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000928 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000929 AssertOK();
930}
931
Misha Brukmanb1c93172005-04-21 23:48:37 +0000932StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000933 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000934 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000935 Ops[0].init(val, this);
936 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000937 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000938 setAlignment(0);
939 AssertOK();
940}
941
942StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
943 unsigned Align, Instruction *InsertBefore)
944 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
945 Ops[0].init(val, this);
946 Ops[1].init(addr, this);
947 setVolatile(isVolatile);
948 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000949 AssertOK();
950}
951
Misha Brukmanb1c93172005-04-21 23:48:37 +0000952StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000953 unsigned Align, BasicBlock *InsertAtEnd)
954 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
955 Ops[0].init(val, this);
956 Ops[1].init(addr, this);
957 setVolatile(isVolatile);
958 setAlignment(Align);
959 AssertOK();
960}
961
962StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000963 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000964 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000965 Ops[0].init(val, this);
966 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000967 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000968 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000969 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000970}
971
Christopher Lamb84485702007-04-22 19:24:39 +0000972void StoreInst::setAlignment(unsigned Align) {
973 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
974 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
975}
976
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000977//===----------------------------------------------------------------------===//
978// GetElementPtrInst Implementation
979//===----------------------------------------------------------------------===//
980
Christopher Lambedf07882007-12-17 01:12:55 +0000981static unsigned retrieveAddrSpace(const Value *Val) {
982 return cast<PointerType>(Val->getType())->getAddressSpace();
983}
984
Chris Lattner79807c3d2007-01-31 19:47:18 +0000985void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
986 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000987 Use *OL = OperandList = new Use[NumOperands];
988 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000989
Chris Lattner79807c3d2007-01-31 19:47:18 +0000990 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000991 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000992}
993
Chris Lattner82981202005-05-03 05:43:30 +0000994void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
995 NumOperands = 2;
996 Use *OL = OperandList = new Use[2];
997 OL[0].init(Ptr, this);
998 OL[1].init(Idx, this);
999}
1000
Chris Lattner82981202005-05-03 05:43:30 +00001001GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1002 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001003 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001004 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001005 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +00001006 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001007 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001008}
1009
1010GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1011 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001012 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001013 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001014 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +00001015 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001016 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001017}
1018
Gordon Henriksen14a55692007-12-10 02:14:30 +00001019GetElementPtrInst::~GetElementPtrInst() {
1020 delete[] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001021}
1022
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001023// getIndexedType - Returns the type of the element that would be loaded with
1024// a load instruction with the specified parameters.
1025//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001026// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001027// pointer type.
1028//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001029const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001030 Value* const *Idxs,
1031 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001032 bool AllowCompositeLeaf) {
1033 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
1034
1035 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +00001036 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001037 if (AllowCompositeLeaf ||
1038 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
1039 return cast<PointerType>(Ptr)->getElementType();
1040 else
1041 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001042
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001043 unsigned CurIdx = 0;
1044 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001045 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001046 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
1047 return 0; // Can't load a whole structure or array!?!?
1048 }
1049
Chris Lattner302116a2007-01-31 04:40:28 +00001050 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001051 if (isa<PointerType>(CT) && CurIdx != 1)
1052 return 0; // Can only index into pointer types at the first index!
1053 if (!CT->indexValid(Index)) return 0;
1054 Ptr = CT->getTypeAtIndex(Index);
1055
1056 // If the new type forwards to another type, then it is in the middle
1057 // of being refined to another type (and hence, may have dropped all
1058 // references to what it was using before). So, use the new forwarded
1059 // type.
1060 if (const Type * Ty = Ptr->getForwardedType()) {
1061 Ptr = Ty;
1062 }
1063 }
Chris Lattner302116a2007-01-31 04:40:28 +00001064 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001065}
1066
Chris Lattner82981202005-05-03 05:43:30 +00001067const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1068 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1069 if (!PTy) return 0; // Type isn't a pointer type!
1070
1071 // Check the pointer index.
1072 if (!PTy->indexValid(Idx)) return 0;
1073
Chris Lattnerc2233332005-05-03 16:44:45 +00001074 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001075}
1076
Chris Lattner45f15572007-04-14 00:12:57 +00001077
1078/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1079/// zeros. If so, the result pointer and the first operand have the same
1080/// value, just potentially different types.
1081bool GetElementPtrInst::hasAllZeroIndices() const {
1082 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1083 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1084 if (!CI->isZero()) return false;
1085 } else {
1086 return false;
1087 }
1088 }
1089 return true;
1090}
1091
Chris Lattner27058292007-04-27 20:35:56 +00001092/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1093/// constant integers. If so, the result pointer and the first operand have
1094/// a constant offset between them.
1095bool GetElementPtrInst::hasAllConstantIndices() const {
1096 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1097 if (!isa<ConstantInt>(getOperand(i)))
1098 return false;
1099 }
1100 return true;
1101}
1102
Chris Lattner45f15572007-04-14 00:12:57 +00001103
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001104//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001105// ExtractElementInst Implementation
1106//===----------------------------------------------------------------------===//
1107
1108ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001109 const std::string &Name,
1110 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001111 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001112 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001113 assert(isValidOperands(Val, Index) &&
1114 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001115 Ops[0].init(Val, this);
1116 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001117 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001118}
1119
Chris Lattner65511ff2006-10-05 06:24:58 +00001120ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1121 const std::string &Name,
1122 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001123 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001124 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001125 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001126 assert(isValidOperands(Val, Index) &&
1127 "Invalid extractelement instruction operands!");
1128 Ops[0].init(Val, this);
1129 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001130 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001131}
1132
1133
Robert Bocchino23004482006-01-10 19:05:34 +00001134ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001135 const std::string &Name,
1136 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001137 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001138 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001139 assert(isValidOperands(Val, Index) &&
1140 "Invalid extractelement instruction operands!");
1141
Robert Bocchino23004482006-01-10 19:05:34 +00001142 Ops[0].init(Val, this);
1143 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001144 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001145}
1146
Chris Lattner65511ff2006-10-05 06:24:58 +00001147ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1148 const std::string &Name,
1149 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001150 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001151 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001152 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001153 assert(isValidOperands(Val, Index) &&
1154 "Invalid extractelement instruction operands!");
1155
1156 Ops[0].init(Val, this);
1157 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001158 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001159}
1160
1161
Chris Lattner54865b32006-04-08 04:05:48 +00001162bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001163 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001164 return false;
1165 return true;
1166}
1167
1168
Robert Bocchino23004482006-01-10 19:05:34 +00001169//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001170// InsertElementInst Implementation
1171//===----------------------------------------------------------------------===//
1172
Chris Lattner0875d942006-04-14 22:20:32 +00001173InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1174 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1175 Ops[0].init(IE.Ops[0], this);
1176 Ops[1].init(IE.Ops[1], this);
1177 Ops[2].init(IE.Ops[2], this);
1178}
Chris Lattner54865b32006-04-08 04:05:48 +00001179InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001180 const std::string &Name,
1181 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001182 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001183 assert(isValidOperands(Vec, Elt, Index) &&
1184 "Invalid insertelement instruction operands!");
1185 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001186 Ops[1].init(Elt, this);
1187 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001188 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001189}
1190
Chris Lattner65511ff2006-10-05 06:24:58 +00001191InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1192 const std::string &Name,
1193 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001194 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001195 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001196 assert(isValidOperands(Vec, Elt, Index) &&
1197 "Invalid insertelement instruction operands!");
1198 Ops[0].init(Vec, this);
1199 Ops[1].init(Elt, this);
1200 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001201 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001202}
1203
1204
Chris Lattner54865b32006-04-08 04:05:48 +00001205InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001206 const std::string &Name,
1207 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001208 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001209 assert(isValidOperands(Vec, Elt, Index) &&
1210 "Invalid insertelement instruction operands!");
1211
1212 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001213 Ops[1].init(Elt, this);
1214 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001215 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001216}
1217
Chris Lattner65511ff2006-10-05 06:24:58 +00001218InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1219 const std::string &Name,
1220 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001221: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001222 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001223 assert(isValidOperands(Vec, Elt, Index) &&
1224 "Invalid insertelement instruction operands!");
1225
1226 Ops[0].init(Vec, this);
1227 Ops[1].init(Elt, this);
1228 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001229 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001230}
1231
Chris Lattner54865b32006-04-08 04:05:48 +00001232bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1233 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001234 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001235 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001236
Reid Spencerd84d35b2007-02-15 02:26:10 +00001237 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001238 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001239
Reid Spencer8d9336d2006-12-31 05:26:44 +00001240 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001241 return false; // Third operand of insertelement must be uint.
1242 return true;
1243}
1244
1245
Robert Bocchinoca27f032006-01-17 20:07:22 +00001246//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001247// ShuffleVectorInst Implementation
1248//===----------------------------------------------------------------------===//
1249
Chris Lattner0875d942006-04-14 22:20:32 +00001250ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1251 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1252 Ops[0].init(SV.Ops[0], this);
1253 Ops[1].init(SV.Ops[1], this);
1254 Ops[2].init(SV.Ops[2], this);
1255}
1256
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001257ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1258 const std::string &Name,
1259 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001260 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001261 assert(isValidOperands(V1, V2, Mask) &&
1262 "Invalid shuffle vector instruction operands!");
1263 Ops[0].init(V1, this);
1264 Ops[1].init(V2, this);
1265 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001266 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001267}
1268
1269ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1270 const std::string &Name,
1271 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001272 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001273 assert(isValidOperands(V1, V2, Mask) &&
1274 "Invalid shuffle vector instruction operands!");
1275
1276 Ops[0].init(V1, this);
1277 Ops[1].init(V2, this);
1278 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001279 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001280}
1281
1282bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1283 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001284 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001285 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001286 if (!isa<VectorType>(Mask->getType()) ||
1287 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1288 cast<VectorType>(Mask->getType())->getNumElements() !=
1289 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001290 return false;
1291 return true;
1292}
1293
1294
1295//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001296// BinaryOperator Class
1297//===----------------------------------------------------------------------===//
1298
Chris Lattner2195fc42007-02-24 00:55:48 +00001299BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1300 const Type *Ty, const std::string &Name,
1301 Instruction *InsertBefore)
1302 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1303 Ops[0].init(S1, this);
1304 Ops[1].init(S2, this);
1305 init(iType);
1306 setName(Name);
1307}
1308
1309BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1310 const Type *Ty, const std::string &Name,
1311 BasicBlock *InsertAtEnd)
1312 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1313 Ops[0].init(S1, this);
1314 Ops[1].init(S2, this);
1315 init(iType);
1316 setName(Name);
1317}
1318
1319
1320void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001321 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001322 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001323 assert(LHS->getType() == RHS->getType() &&
1324 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001325#ifndef NDEBUG
1326 switch (iType) {
1327 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001328 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001329 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001330 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001331 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001332 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001333 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001334 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001335 case UDiv:
1336 case SDiv:
1337 assert(getType() == LHS->getType() &&
1338 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001339 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1340 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001341 "Incorrect operand type (not integer) for S/UDIV");
1342 break;
1343 case FDiv:
1344 assert(getType() == LHS->getType() &&
1345 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001346 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1347 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001348 && "Incorrect operand type (not floating point) for FDIV");
1349 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001350 case URem:
1351 case SRem:
1352 assert(getType() == LHS->getType() &&
1353 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001354 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1355 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001356 "Incorrect operand type (not integer) for S/UREM");
1357 break;
1358 case FRem:
1359 assert(getType() == LHS->getType() &&
1360 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001361 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1362 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001363 && "Incorrect operand type (not floating point) for FREM");
1364 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001365 case Shl:
1366 case LShr:
1367 case AShr:
1368 assert(getType() == LHS->getType() &&
1369 "Shift operation should return same type as operands!");
1370 assert(getType()->isInteger() &&
1371 "Shift operation requires integer operands");
1372 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001373 case And: case Or:
1374 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001375 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001376 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001377 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001378 (isa<VectorType>(getType()) &&
1379 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001380 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001381 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001382 default:
1383 break;
1384 }
1385#endif
1386}
1387
1388BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001389 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001390 Instruction *InsertBefore) {
1391 assert(S1->getType() == S2->getType() &&
1392 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001393 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001394}
1395
1396BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001397 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001398 BasicBlock *InsertAtEnd) {
1399 BinaryOperator *Res = create(Op, S1, S2, Name);
1400 InsertAtEnd->getInstList().push_back(Res);
1401 return Res;
1402}
1403
1404BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1405 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001406 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1407 return new BinaryOperator(Instruction::Sub,
1408 zero, Op,
1409 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001410}
1411
1412BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1413 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001414 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1415 return new BinaryOperator(Instruction::Sub,
1416 zero, Op,
1417 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001418}
1419
1420BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1421 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001422 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001423 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001424 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001425 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001426 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001427 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001428 }
1429
1430 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001431 Op->getType(), Name, InsertBefore);
1432}
1433
1434BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1435 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001436 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001437 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001438 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001439 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001440 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001441 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001442 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001443 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001444 }
1445
1446 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001447 Op->getType(), Name, InsertAtEnd);
1448}
1449
1450
1451// isConstantAllOnes - Helper function for several functions below
1452static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001453 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1454 return CI->isAllOnesValue();
1455 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1456 return CV->isAllOnesValue();
1457 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001458}
1459
1460bool BinaryOperator::isNeg(const Value *V) {
1461 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1462 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001463 return Bop->getOperand(0) ==
1464 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001465 return false;
1466}
1467
1468bool BinaryOperator::isNot(const Value *V) {
1469 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1470 return (Bop->getOpcode() == Instruction::Xor &&
1471 (isConstantAllOnes(Bop->getOperand(1)) ||
1472 isConstantAllOnes(Bop->getOperand(0))));
1473 return false;
1474}
1475
Chris Lattner2c7d1772005-04-24 07:28:37 +00001476Value *BinaryOperator::getNegArgument(Value *BinOp) {
1477 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1478 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001479}
1480
Chris Lattner2c7d1772005-04-24 07:28:37 +00001481const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1482 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001483}
1484
Chris Lattner2c7d1772005-04-24 07:28:37 +00001485Value *BinaryOperator::getNotArgument(Value *BinOp) {
1486 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1487 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1488 Value *Op0 = BO->getOperand(0);
1489 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001490 if (isConstantAllOnes(Op0)) return Op1;
1491
1492 assert(isConstantAllOnes(Op1));
1493 return Op0;
1494}
1495
Chris Lattner2c7d1772005-04-24 07:28:37 +00001496const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1497 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001498}
1499
1500
1501// swapOperands - Exchange the two operands to this instruction. This
1502// instruction is safe to use on any binary instruction and does not
1503// modify the semantics of the instruction. If the instruction is
1504// order dependent (SetLT f.e.) the opcode is changed.
1505//
1506bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001507 if (!isCommutative())
1508 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001509 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001510 return false;
1511}
1512
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001513//===----------------------------------------------------------------------===//
1514// CastInst Class
1515//===----------------------------------------------------------------------===//
1516
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001517// Just determine if this cast only deals with integral->integral conversion.
1518bool CastInst::isIntegerCast() const {
1519 switch (getOpcode()) {
1520 default: return false;
1521 case Instruction::ZExt:
1522 case Instruction::SExt:
1523 case Instruction::Trunc:
1524 return true;
1525 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001526 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001527 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001528}
1529
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001530bool CastInst::isLosslessCast() const {
1531 // Only BitCast can be lossless, exit fast if we're not BitCast
1532 if (getOpcode() != Instruction::BitCast)
1533 return false;
1534
1535 // Identity cast is always lossless
1536 const Type* SrcTy = getOperand(0)->getType();
1537 const Type* DstTy = getType();
1538 if (SrcTy == DstTy)
1539 return true;
1540
Reid Spencer8d9336d2006-12-31 05:26:44 +00001541 // Pointer to pointer is always lossless.
1542 if (isa<PointerType>(SrcTy))
1543 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001544 return false; // Other types have no identity values
1545}
1546
1547/// This function determines if the CastInst does not require any bits to be
1548/// changed in order to effect the cast. Essentially, it identifies cases where
1549/// no code gen is necessary for the cast, hence the name no-op cast. For
1550/// example, the following are all no-op casts:
1551/// # bitcast uint %X, int
1552/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001553/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001554/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1555/// @brief Determine if a cast is a no-op.
1556bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1557 switch (getOpcode()) {
1558 default:
1559 assert(!"Invalid CastOp");
1560 case Instruction::Trunc:
1561 case Instruction::ZExt:
1562 case Instruction::SExt:
1563 case Instruction::FPTrunc:
1564 case Instruction::FPExt:
1565 case Instruction::UIToFP:
1566 case Instruction::SIToFP:
1567 case Instruction::FPToUI:
1568 case Instruction::FPToSI:
1569 return false; // These always modify bits
1570 case Instruction::BitCast:
1571 return true; // BitCast never modifies bits.
1572 case Instruction::PtrToInt:
1573 return IntPtrTy->getPrimitiveSizeInBits() ==
1574 getType()->getPrimitiveSizeInBits();
1575 case Instruction::IntToPtr:
1576 return IntPtrTy->getPrimitiveSizeInBits() ==
1577 getOperand(0)->getType()->getPrimitiveSizeInBits();
1578 }
1579}
1580
1581/// This function determines if a pair of casts can be eliminated and what
1582/// opcode should be used in the elimination. This assumes that there are two
1583/// instructions like this:
1584/// * %F = firstOpcode SrcTy %x to MidTy
1585/// * %S = secondOpcode MidTy %F to DstTy
1586/// The function returns a resultOpcode so these two casts can be replaced with:
1587/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1588/// If no such cast is permited, the function returns 0.
1589unsigned CastInst::isEliminableCastPair(
1590 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1591 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1592{
1593 // Define the 144 possibilities for these two cast instructions. The values
1594 // in this matrix determine what to do in a given situation and select the
1595 // case in the switch below. The rows correspond to firstOp, the columns
1596 // correspond to secondOp. In looking at the table below, keep in mind
1597 // the following cast properties:
1598 //
1599 // Size Compare Source Destination
1600 // Operator Src ? Size Type Sign Type Sign
1601 // -------- ------------ ------------------- ---------------------
1602 // TRUNC > Integer Any Integral Any
1603 // ZEXT < Integral Unsigned Integer Any
1604 // SEXT < Integral Signed Integer Any
1605 // FPTOUI n/a FloatPt n/a Integral Unsigned
1606 // FPTOSI n/a FloatPt n/a Integral Signed
1607 // UITOFP n/a Integral Unsigned FloatPt n/a
1608 // SITOFP n/a Integral Signed FloatPt n/a
1609 // FPTRUNC > FloatPt n/a FloatPt n/a
1610 // FPEXT < FloatPt n/a FloatPt n/a
1611 // PTRTOINT n/a Pointer n/a Integral Unsigned
1612 // INTTOPTR n/a Integral Unsigned Pointer n/a
1613 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001614 //
1615 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1616 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1617 // into "fptoui double to ulong", but this loses information about the range
1618 // of the produced value (we no longer know the top-part is all zeros).
1619 // Further this conversion is often much more expensive for typical hardware,
1620 // and causes issues when building libgcc. We disallow fptosi+sext for the
1621 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001622 const unsigned numCastOps =
1623 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1624 static const uint8_t CastResults[numCastOps][numCastOps] = {
1625 // T F F U S F F P I B -+
1626 // R Z S P P I I T P 2 N T |
1627 // U E E 2 2 2 2 R E I T C +- secondOp
1628 // N X X U S F F N X N 2 V |
1629 // C T T I I P P C T T P T -+
1630 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1631 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1632 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001633 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1634 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001635 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1636 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1637 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1638 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1639 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1640 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1641 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1642 };
1643
1644 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1645 [secondOp-Instruction::CastOpsBegin];
1646 switch (ElimCase) {
1647 case 0:
1648 // categorically disallowed
1649 return 0;
1650 case 1:
1651 // allowed, use first cast's opcode
1652 return firstOp;
1653 case 2:
1654 // allowed, use second cast's opcode
1655 return secondOp;
1656 case 3:
1657 // no-op cast in second op implies firstOp as long as the DestTy
1658 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001659 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001660 return firstOp;
1661 return 0;
1662 case 4:
1663 // no-op cast in second op implies firstOp as long as the DestTy
1664 // is floating point
1665 if (DstTy->isFloatingPoint())
1666 return firstOp;
1667 return 0;
1668 case 5:
1669 // no-op cast in first op implies secondOp as long as the SrcTy
1670 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001671 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001672 return secondOp;
1673 return 0;
1674 case 6:
1675 // no-op cast in first op implies secondOp as long as the SrcTy
1676 // is a floating point
1677 if (SrcTy->isFloatingPoint())
1678 return secondOp;
1679 return 0;
1680 case 7: {
1681 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1682 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1683 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1684 if (MidSize >= PtrSize)
1685 return Instruction::BitCast;
1686 return 0;
1687 }
1688 case 8: {
1689 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1690 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1691 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1692 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1693 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1694 if (SrcSize == DstSize)
1695 return Instruction::BitCast;
1696 else if (SrcSize < DstSize)
1697 return firstOp;
1698 return secondOp;
1699 }
1700 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1701 return Instruction::ZExt;
1702 case 10:
1703 // fpext followed by ftrunc is allowed if the bit size returned to is
1704 // the same as the original, in which case its just a bitcast
1705 if (SrcTy == DstTy)
1706 return Instruction::BitCast;
1707 return 0; // If the types are not the same we can't eliminate it.
1708 case 11:
1709 // bitcast followed by ptrtoint is allowed as long as the bitcast
1710 // is a pointer to pointer cast.
1711 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1712 return secondOp;
1713 return 0;
1714 case 12:
1715 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1716 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1717 return firstOp;
1718 return 0;
1719 case 13: {
1720 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1721 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1722 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1723 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1724 if (SrcSize <= PtrSize && SrcSize == DstSize)
1725 return Instruction::BitCast;
1726 return 0;
1727 }
1728 case 99:
1729 // cast combination can't happen (error in input). This is for all cases
1730 // where the MidTy is not the same for the two cast instructions.
1731 assert(!"Invalid Cast Combination");
1732 return 0;
1733 default:
1734 assert(!"Error in CastResults table!!!");
1735 return 0;
1736 }
1737 return 0;
1738}
1739
1740CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1741 const std::string &Name, Instruction *InsertBefore) {
1742 // Construct and return the appropriate CastInst subclass
1743 switch (op) {
1744 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1745 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1746 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1747 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1748 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1749 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1750 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1751 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1752 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1753 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1754 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1755 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1756 default:
1757 assert(!"Invalid opcode provided");
1758 }
1759 return 0;
1760}
1761
1762CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1763 const std::string &Name, BasicBlock *InsertAtEnd) {
1764 // Construct and return the appropriate CastInst subclass
1765 switch (op) {
1766 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1767 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1768 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1769 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1770 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1771 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1772 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1773 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1774 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1775 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1776 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1777 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1778 default:
1779 assert(!"Invalid opcode provided");
1780 }
1781 return 0;
1782}
1783
Reid Spencer5c140882006-12-04 20:17:56 +00001784CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1785 const std::string &Name,
1786 Instruction *InsertBefore) {
1787 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1788 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1789 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1790}
1791
1792CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1793 const std::string &Name,
1794 BasicBlock *InsertAtEnd) {
1795 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1796 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1797 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1798}
1799
1800CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1801 const std::string &Name,
1802 Instruction *InsertBefore) {
1803 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1804 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1805 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1806}
1807
1808CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1809 const std::string &Name,
1810 BasicBlock *InsertAtEnd) {
1811 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1812 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1813 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1814}
1815
1816CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1817 const std::string &Name,
1818 Instruction *InsertBefore) {
1819 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1820 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1821 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1822}
1823
1824CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1825 const std::string &Name,
1826 BasicBlock *InsertAtEnd) {
1827 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1828 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1829 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1830}
1831
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001832CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1833 const std::string &Name,
1834 BasicBlock *InsertAtEnd) {
1835 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001836 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001837 "Invalid cast");
1838
Chris Lattner03c49532007-01-15 02:27:26 +00001839 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001840 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1841 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1842}
1843
1844/// @brief Create a BitCast or a PtrToInt cast instruction
1845CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1846 const std::string &Name,
1847 Instruction *InsertBefore) {
1848 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001849 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001850 "Invalid cast");
1851
Chris Lattner03c49532007-01-15 02:27:26 +00001852 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001853 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1854 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1855}
1856
Reid Spencer7e933472006-12-12 00:49:44 +00001857CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1858 bool isSigned, const std::string &Name,
1859 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001860 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001861 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1862 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1863 Instruction::CastOps opcode =
1864 (SrcBits == DstBits ? Instruction::BitCast :
1865 (SrcBits > DstBits ? Instruction::Trunc :
1866 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1867 return create(opcode, C, Ty, Name, InsertBefore);
1868}
1869
1870CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1871 bool isSigned, const std::string &Name,
1872 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001873 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001874 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1875 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1876 Instruction::CastOps opcode =
1877 (SrcBits == DstBits ? Instruction::BitCast :
1878 (SrcBits > DstBits ? Instruction::Trunc :
1879 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1880 return create(opcode, C, Ty, Name, InsertAtEnd);
1881}
1882
1883CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1884 const std::string &Name,
1885 Instruction *InsertBefore) {
1886 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1887 "Invalid cast");
1888 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1889 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1890 Instruction::CastOps opcode =
1891 (SrcBits == DstBits ? Instruction::BitCast :
1892 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1893 return create(opcode, C, Ty, Name, InsertBefore);
1894}
1895
1896CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1897 const std::string &Name,
1898 BasicBlock *InsertAtEnd) {
1899 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1900 "Invalid cast");
1901 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1902 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1903 Instruction::CastOps opcode =
1904 (SrcBits == DstBits ? Instruction::BitCast :
1905 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1906 return create(opcode, C, Ty, Name, InsertAtEnd);
1907}
1908
Duncan Sands55e50902008-01-06 10:12:28 +00001909// Check whether it is valid to call getCastOpcode for these types.
1910// This routine must be kept in sync with getCastOpcode.
1911bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
1912 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
1913 return false;
1914
1915 if (SrcTy == DestTy)
1916 return true;
1917
1918 // Get the bit sizes, we'll need these
1919 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1920 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1921
1922 // Run through the possibilities ...
1923 if (DestTy->isInteger()) { // Casting to integral
1924 if (SrcTy->isInteger()) { // Casting from integral
1925 return true;
1926 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1927 return true;
1928 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1929 // Casting from vector
1930 return DestBits == PTy->getBitWidth();
1931 } else { // Casting from something else
1932 return isa<PointerType>(SrcTy);
1933 }
1934 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
1935 if (SrcTy->isInteger()) { // Casting from integral
1936 return true;
1937 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1938 return true;
1939 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1940 // Casting from vector
1941 return DestBits == PTy->getBitWidth();
1942 } else { // Casting from something else
1943 return false;
1944 }
1945 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1946 // Casting to vector
1947 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
1948 // Casting from vector
1949 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
1950 } else { // Casting from something else
1951 return DestPTy->getBitWidth() == SrcBits;
1952 }
1953 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
1954 if (isa<PointerType>(SrcTy)) { // Casting from pointer
1955 return true;
1956 } else if (SrcTy->isInteger()) { // Casting from integral
1957 return true;
1958 } else { // Casting from something else
1959 return false;
1960 }
1961 } else { // Casting to something else
1962 return false;
1963 }
1964}
1965
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001966// Provide a way to get a "cast" where the cast opcode is inferred from the
1967// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001968// logic in the castIsValid function below. This axiom should hold:
1969// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1970// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001971// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00001972// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001973Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001974CastInst::getCastOpcode(
1975 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001976 // Get the bit sizes, we'll need these
1977 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00001978 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1979 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001980
Duncan Sands55e50902008-01-06 10:12:28 +00001981 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
1982 "Only first class types are castable!");
1983
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001984 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001985 if (DestTy->isInteger()) { // Casting to integral
1986 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001987 if (DestBits < SrcBits)
1988 return Trunc; // int -> smaller int
1989 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001990 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001991 return SExt; // signed -> SEXT
1992 else
1993 return ZExt; // unsigned -> ZEXT
1994 } else {
1995 return BitCast; // Same size, No-op cast
1996 }
1997 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001998 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001999 return FPToSI; // FP -> sint
2000 else
2001 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002002 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002003 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002004 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002005 return BitCast; // Same size, no-op cast
2006 } else {
2007 assert(isa<PointerType>(SrcTy) &&
2008 "Casting from a value that is not first-class type");
2009 return PtrToInt; // ptr -> int
2010 }
2011 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002012 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002013 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002014 return SIToFP; // sint -> FP
2015 else
2016 return UIToFP; // uint -> FP
2017 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2018 if (DestBits < SrcBits) {
2019 return FPTrunc; // FP -> smaller FP
2020 } else if (DestBits > SrcBits) {
2021 return FPExt; // FP -> larger FP
2022 } else {
2023 return BitCast; // same size, no-op cast
2024 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002025 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002026 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002027 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002028 return BitCast; // same size, no-op cast
2029 } else {
2030 assert(0 && "Casting pointer or non-first class to float");
2031 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002032 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2033 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002034 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002035 "Casting vector to vector of different widths");
2036 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002037 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002038 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002039 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002040 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002041 }
2042 } else if (isa<PointerType>(DestTy)) {
2043 if (isa<PointerType>(SrcTy)) {
2044 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002045 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002046 return IntToPtr; // int -> ptr
2047 } else {
2048 assert(!"Casting pointer to other than pointer or int");
2049 }
2050 } else {
2051 assert(!"Casting to type that is not first-class");
2052 }
2053
2054 // If we fall through to here we probably hit an assertion cast above
2055 // and assertions are not turned on. Anything we return is an error, so
2056 // BitCast is as good a choice as any.
2057 return BitCast;
2058}
2059
2060//===----------------------------------------------------------------------===//
2061// CastInst SubClass Constructors
2062//===----------------------------------------------------------------------===//
2063
2064/// Check that the construction parameters for a CastInst are correct. This
2065/// could be broken out into the separate constructors but it is useful to have
2066/// it in one place and to eliminate the redundant code for getting the sizes
2067/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002068bool
2069CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002070
2071 // Check for type sanity on the arguments
2072 const Type *SrcTy = S->getType();
2073 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2074 return false;
2075
2076 // Get the size of the types in bits, we'll need this later
2077 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2078 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2079
2080 // Switch on the opcode provided
2081 switch (op) {
2082 default: return false; // This is an input error
2083 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002084 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002085 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002086 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002087 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002088 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002089 case Instruction::FPTrunc:
2090 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2091 SrcBitSize > DstBitSize;
2092 case Instruction::FPExt:
2093 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2094 SrcBitSize < DstBitSize;
2095 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002096 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002097 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2098 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2099 return SVTy->getElementType()->isInteger() &&
2100 DVTy->getElementType()->isFloatingPoint() &&
2101 SVTy->getNumElements() == DVTy->getNumElements();
2102 }
2103 }
Chris Lattner03c49532007-01-15 02:27:26 +00002104 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002105 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002106 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002107 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2108 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2109 return SVTy->getElementType()->isFloatingPoint() &&
2110 DVTy->getElementType()->isInteger() &&
2111 SVTy->getNumElements() == DVTy->getNumElements();
2112 }
2113 }
Chris Lattner03c49532007-01-15 02:27:26 +00002114 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002115 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002116 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002117 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002118 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002119 case Instruction::BitCast:
2120 // BitCast implies a no-op cast of type only. No bits change.
2121 // However, you can't cast pointers to anything but pointers.
2122 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2123 return false;
2124
Duncan Sands55e50902008-01-06 10:12:28 +00002125 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002126 // these cases, the cast is okay if the source and destination bit widths
2127 // are identical.
2128 return SrcBitSize == DstBitSize;
2129 }
2130}
2131
2132TruncInst::TruncInst(
2133 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2134) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002135 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002136}
2137
2138TruncInst::TruncInst(
2139 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2140) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002141 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002142}
2143
2144ZExtInst::ZExtInst(
2145 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2146) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002147 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002148}
2149
2150ZExtInst::ZExtInst(
2151 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2152) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002153 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002154}
2155SExtInst::SExtInst(
2156 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2157) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002158 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002159}
2160
Jeff Cohencc08c832006-12-02 02:22:01 +00002161SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002162 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2163) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002164 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002165}
2166
2167FPTruncInst::FPTruncInst(
2168 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2169) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002170 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002171}
2172
2173FPTruncInst::FPTruncInst(
2174 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2175) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002176 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002177}
2178
2179FPExtInst::FPExtInst(
2180 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2181) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002182 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002183}
2184
2185FPExtInst::FPExtInst(
2186 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2187) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002188 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002189}
2190
2191UIToFPInst::UIToFPInst(
2192 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2193) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002194 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002195}
2196
2197UIToFPInst::UIToFPInst(
2198 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2199) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002200 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002201}
2202
2203SIToFPInst::SIToFPInst(
2204 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2205) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002206 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002207}
2208
2209SIToFPInst::SIToFPInst(
2210 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2211) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002212 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002213}
2214
2215FPToUIInst::FPToUIInst(
2216 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2217) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002218 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002219}
2220
2221FPToUIInst::FPToUIInst(
2222 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2223) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002224 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002225}
2226
2227FPToSIInst::FPToSIInst(
2228 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2229) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002230 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002231}
2232
2233FPToSIInst::FPToSIInst(
2234 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2235) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002236 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237}
2238
2239PtrToIntInst::PtrToIntInst(
2240 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2241) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002242 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002243}
2244
2245PtrToIntInst::PtrToIntInst(
2246 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2247) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002248 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002249}
2250
2251IntToPtrInst::IntToPtrInst(
2252 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2253) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002254 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002255}
2256
2257IntToPtrInst::IntToPtrInst(
2258 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2259) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002260 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002261}
2262
2263BitCastInst::BitCastInst(
2264 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2265) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002266 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002267}
2268
2269BitCastInst::BitCastInst(
2270 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2271) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002272 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002273}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002274
2275//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002276// CmpInst Classes
2277//===----------------------------------------------------------------------===//
2278
2279CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2280 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002281 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002282 Ops[0].init(LHS, this);
2283 Ops[1].init(RHS, this);
2284 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002285 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002286 if (op == Instruction::ICmp) {
2287 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2288 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2289 "Invalid ICmp predicate value");
2290 const Type* Op0Ty = getOperand(0)->getType();
2291 const Type* Op1Ty = getOperand(1)->getType();
2292 assert(Op0Ty == Op1Ty &&
2293 "Both operands to ICmp instruction are not of the same type!");
2294 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002295 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002296 "Invalid operand types for ICmp instruction");
2297 return;
2298 }
2299 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2300 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2301 "Invalid FCmp predicate value");
2302 const Type* Op0Ty = getOperand(0)->getType();
2303 const Type* Op1Ty = getOperand(1)->getType();
2304 assert(Op0Ty == Op1Ty &&
2305 "Both operands to FCmp instruction are not of the same type!");
2306 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002307 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002308 "Invalid operand types for FCmp instruction");
2309}
2310
2311CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2312 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002313 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002314 Ops[0].init(LHS, this);
2315 Ops[1].init(RHS, this);
2316 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002317 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002318 if (op == Instruction::ICmp) {
2319 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2320 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2321 "Invalid ICmp predicate value");
2322
2323 const Type* Op0Ty = getOperand(0)->getType();
2324 const Type* Op1Ty = getOperand(1)->getType();
2325 assert(Op0Ty == Op1Ty &&
2326 "Both operands to ICmp instruction are not of the same type!");
2327 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002328 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002329 "Invalid operand types for ICmp instruction");
2330 return;
2331 }
2332 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2333 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2334 "Invalid FCmp predicate value");
2335 const Type* Op0Ty = getOperand(0)->getType();
2336 const Type* Op1Ty = getOperand(1)->getType();
2337 assert(Op0Ty == Op1Ty &&
2338 "Both operands to FCmp instruction are not of the same type!");
2339 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002340 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002341 "Invalid operand types for FCmp instruction");
2342}
2343
2344CmpInst *
2345CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2346 const std::string &Name, Instruction *InsertBefore) {
2347 if (Op == Instruction::ICmp) {
2348 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2349 InsertBefore);
2350 }
2351 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2352 InsertBefore);
2353}
2354
2355CmpInst *
2356CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2357 const std::string &Name, BasicBlock *InsertAtEnd) {
2358 if (Op == Instruction::ICmp) {
2359 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2360 InsertAtEnd);
2361 }
2362 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2363 InsertAtEnd);
2364}
2365
2366void CmpInst::swapOperands() {
2367 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2368 IC->swapOperands();
2369 else
2370 cast<FCmpInst>(this)->swapOperands();
2371}
2372
2373bool CmpInst::isCommutative() {
2374 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2375 return IC->isCommutative();
2376 return cast<FCmpInst>(this)->isCommutative();
2377}
2378
2379bool CmpInst::isEquality() {
2380 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2381 return IC->isEquality();
2382 return cast<FCmpInst>(this)->isEquality();
2383}
2384
2385
2386ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2387 switch (pred) {
2388 default:
2389 assert(!"Unknown icmp predicate!");
2390 case ICMP_EQ: return ICMP_NE;
2391 case ICMP_NE: return ICMP_EQ;
2392 case ICMP_UGT: return ICMP_ULE;
2393 case ICMP_ULT: return ICMP_UGE;
2394 case ICMP_UGE: return ICMP_ULT;
2395 case ICMP_ULE: return ICMP_UGT;
2396 case ICMP_SGT: return ICMP_SLE;
2397 case ICMP_SLT: return ICMP_SGE;
2398 case ICMP_SGE: return ICMP_SLT;
2399 case ICMP_SLE: return ICMP_SGT;
2400 }
2401}
2402
2403ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2404 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002405 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002406 case ICMP_EQ: case ICMP_NE:
2407 return pred;
2408 case ICMP_SGT: return ICMP_SLT;
2409 case ICMP_SLT: return ICMP_SGT;
2410 case ICMP_SGE: return ICMP_SLE;
2411 case ICMP_SLE: return ICMP_SGE;
2412 case ICMP_UGT: return ICMP_ULT;
2413 case ICMP_ULT: return ICMP_UGT;
2414 case ICMP_UGE: return ICMP_ULE;
2415 case ICMP_ULE: return ICMP_UGE;
2416 }
2417}
2418
Reid Spencer266e42b2006-12-23 06:05:41 +00002419ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2420 switch (pred) {
2421 default: assert(! "Unknown icmp predicate!");
2422 case ICMP_EQ: case ICMP_NE:
2423 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2424 return pred;
2425 case ICMP_UGT: return ICMP_SGT;
2426 case ICMP_ULT: return ICMP_SLT;
2427 case ICMP_UGE: return ICMP_SGE;
2428 case ICMP_ULE: return ICMP_SLE;
2429 }
2430}
2431
2432bool ICmpInst::isSignedPredicate(Predicate pred) {
2433 switch (pred) {
2434 default: assert(! "Unknown icmp predicate!");
2435 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2436 return true;
2437 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2438 case ICMP_UGE: case ICMP_ULE:
2439 return false;
2440 }
2441}
2442
Reid Spencer0286bc12007-02-28 22:00:54 +00002443/// Initialize a set of values that all satisfy the condition with C.
2444///
2445ConstantRange
2446ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2447 APInt Lower(C);
2448 APInt Upper(C);
2449 uint32_t BitWidth = C.getBitWidth();
2450 switch (pred) {
2451 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2452 case ICmpInst::ICMP_EQ: Upper++; break;
2453 case ICmpInst::ICMP_NE: Lower++; break;
2454 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2455 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2456 case ICmpInst::ICMP_UGT:
2457 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2458 break;
2459 case ICmpInst::ICMP_SGT:
2460 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2461 break;
2462 case ICmpInst::ICMP_ULE:
2463 Lower = APInt::getMinValue(BitWidth); Upper++;
2464 break;
2465 case ICmpInst::ICMP_SLE:
2466 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2467 break;
2468 case ICmpInst::ICMP_UGE:
2469 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2470 break;
2471 case ICmpInst::ICMP_SGE:
2472 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2473 break;
2474 }
2475 return ConstantRange(Lower, Upper);
2476}
2477
Reid Spencerd9436b62006-11-20 01:22:35 +00002478FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2479 switch (pred) {
2480 default:
2481 assert(!"Unknown icmp predicate!");
2482 case FCMP_OEQ: return FCMP_UNE;
2483 case FCMP_ONE: return FCMP_UEQ;
2484 case FCMP_OGT: return FCMP_ULE;
2485 case FCMP_OLT: return FCMP_UGE;
2486 case FCMP_OGE: return FCMP_ULT;
2487 case FCMP_OLE: return FCMP_UGT;
2488 case FCMP_UEQ: return FCMP_ONE;
2489 case FCMP_UNE: return FCMP_OEQ;
2490 case FCMP_UGT: return FCMP_OLE;
2491 case FCMP_ULT: return FCMP_OGE;
2492 case FCMP_UGE: return FCMP_OLT;
2493 case FCMP_ULE: return FCMP_OGT;
2494 case FCMP_ORD: return FCMP_UNO;
2495 case FCMP_UNO: return FCMP_ORD;
2496 case FCMP_TRUE: return FCMP_FALSE;
2497 case FCMP_FALSE: return FCMP_TRUE;
2498 }
2499}
2500
2501FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2502 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002503 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002504 case FCMP_FALSE: case FCMP_TRUE:
2505 case FCMP_OEQ: case FCMP_ONE:
2506 case FCMP_UEQ: case FCMP_UNE:
2507 case FCMP_ORD: case FCMP_UNO:
2508 return pred;
2509 case FCMP_OGT: return FCMP_OLT;
2510 case FCMP_OLT: return FCMP_OGT;
2511 case FCMP_OGE: return FCMP_OLE;
2512 case FCMP_OLE: return FCMP_OGE;
2513 case FCMP_UGT: return FCMP_ULT;
2514 case FCMP_ULT: return FCMP_UGT;
2515 case FCMP_UGE: return FCMP_ULE;
2516 case FCMP_ULE: return FCMP_UGE;
2517 }
2518}
2519
Reid Spencer266e42b2006-12-23 06:05:41 +00002520bool CmpInst::isUnsigned(unsigned short predicate) {
2521 switch (predicate) {
2522 default: return false;
2523 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2524 case ICmpInst::ICMP_UGE: return true;
2525 }
2526}
2527
2528bool CmpInst::isSigned(unsigned short predicate){
2529 switch (predicate) {
2530 default: return false;
2531 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2532 case ICmpInst::ICMP_SGE: return true;
2533 }
2534}
2535
2536bool CmpInst::isOrdered(unsigned short predicate) {
2537 switch (predicate) {
2538 default: return false;
2539 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2540 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2541 case FCmpInst::FCMP_ORD: return true;
2542 }
2543}
2544
2545bool CmpInst::isUnordered(unsigned short predicate) {
2546 switch (predicate) {
2547 default: return false;
2548 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2549 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2550 case FCmpInst::FCMP_UNO: return true;
2551 }
2552}
2553
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002554//===----------------------------------------------------------------------===//
2555// SwitchInst Implementation
2556//===----------------------------------------------------------------------===//
2557
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002558void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002559 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002560 ReservedSpace = 2+NumCases*2;
2561 NumOperands = 2;
2562 OperandList = new Use[ReservedSpace];
2563
2564 OperandList[0].init(Value, this);
2565 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002566}
2567
Chris Lattner2195fc42007-02-24 00:55:48 +00002568/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2569/// switch on and a default destination. The number of additional cases can
2570/// be specified here to make memory allocation more efficient. This
2571/// constructor can also autoinsert before another instruction.
2572SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2573 Instruction *InsertBefore)
2574 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2575 init(Value, Default, NumCases);
2576}
2577
2578/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2579/// switch on and a default destination. The number of additional cases can
2580/// be specified here to make memory allocation more efficient. This
2581/// constructor also autoinserts at the end of the specified BasicBlock.
2582SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2583 BasicBlock *InsertAtEnd)
2584 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2585 init(Value, Default, NumCases);
2586}
2587
Misha Brukmanb1c93172005-04-21 23:48:37 +00002588SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002589 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2590 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002591 Use *OL = OperandList, *InOL = SI.OperandList;
2592 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2593 OL[i].init(InOL[i], this);
2594 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002595 }
2596}
2597
Gordon Henriksen14a55692007-12-10 02:14:30 +00002598SwitchInst::~SwitchInst() {
2599 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002600}
2601
2602
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002603/// addCase - Add an entry to the switch instruction...
2604///
Chris Lattner47ac1872005-02-24 05:32:09 +00002605void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002606 unsigned OpNo = NumOperands;
2607 if (OpNo+2 > ReservedSpace)
2608 resizeOperands(0); // Get more space!
2609 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002610 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002611 NumOperands = OpNo+2;
2612 OperandList[OpNo].init(OnVal, this);
2613 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002614}
2615
2616/// removeCase - This method removes the specified successor from the switch
2617/// instruction. Note that this cannot be used to remove the default
2618/// destination (successor #0).
2619///
2620void SwitchInst::removeCase(unsigned idx) {
2621 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002622 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2623
2624 unsigned NumOps = getNumOperands();
2625 Use *OL = OperandList;
2626
2627 // Move everything after this operand down.
2628 //
2629 // FIXME: we could just swap with the end of the list, then erase. However,
2630 // client might not expect this to happen. The code as it is thrashes the
2631 // use/def lists, which is kinda lame.
2632 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2633 OL[i-2] = OL[i];
2634 OL[i-2+1] = OL[i+1];
2635 }
2636
2637 // Nuke the last value.
2638 OL[NumOps-2].set(0);
2639 OL[NumOps-2+1].set(0);
2640 NumOperands = NumOps-2;
2641}
2642
2643/// resizeOperands - resize operands - This adjusts the length of the operands
2644/// list according to the following behavior:
2645/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2646/// of operation. This grows the number of ops by 1.5 times.
2647/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2648/// 3. If NumOps == NumOperands, trim the reserved space.
2649///
2650void SwitchInst::resizeOperands(unsigned NumOps) {
2651 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002652 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002653 } else if (NumOps*2 > NumOperands) {
2654 // No resize needed.
2655 if (ReservedSpace >= NumOps) return;
2656 } else if (NumOps == NumOperands) {
2657 if (ReservedSpace == NumOps) return;
2658 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002659 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002660 }
2661
2662 ReservedSpace = NumOps;
2663 Use *NewOps = new Use[NumOps];
2664 Use *OldOps = OperandList;
2665 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2666 NewOps[i].init(OldOps[i], this);
2667 OldOps[i].set(0);
2668 }
2669 delete [] OldOps;
2670 OperandList = NewOps;
2671}
2672
2673
2674BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2675 return getSuccessor(idx);
2676}
2677unsigned SwitchInst::getNumSuccessorsV() const {
2678 return getNumSuccessors();
2679}
2680void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2681 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002682}
Chris Lattnerf22be932004-10-15 23:52:53 +00002683
2684
2685// Define these methods here so vtables don't get emitted into every translation
2686// unit that uses these classes.
2687
2688GetElementPtrInst *GetElementPtrInst::clone() const {
2689 return new GetElementPtrInst(*this);
2690}
2691
2692BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002693 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002694}
2695
Chris Lattner0b490b02007-08-24 20:48:18 +00002696FCmpInst* FCmpInst::clone() const {
2697 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2698}
2699ICmpInst* ICmpInst::clone() const {
2700 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002701}
2702
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002703MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2704AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2705FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2706LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2707StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2708CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2709CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2710CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2711CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2712CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2713CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2714CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2715CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2716CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2717CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2718CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2719CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2720CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002721SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2722VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2723
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002724ExtractElementInst *ExtractElementInst::clone() const {
2725 return new ExtractElementInst(*this);
2726}
2727InsertElementInst *InsertElementInst::clone() const {
2728 return new InsertElementInst(*this);
2729}
2730ShuffleVectorInst *ShuffleVectorInst::clone() const {
2731 return new ShuffleVectorInst(*this);
2732}
Chris Lattnerf22be932004-10-15 23:52:53 +00002733PHINode *PHINode::clone() const { return new PHINode(*this); }
2734ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2735BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2736SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2737InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2738UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002739UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}