blob: c1e583375a2cc9a90856df9e20d08cd1cde0e4cf [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/BasicBlock.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Reid Spencerce38beb2007-04-09 18:00:57 +000020#include "llvm/ParameterAttributes.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000023#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024using namespace llvm;
25
Chris Lattner3e13b8c2008-01-02 23:42:30 +000026//===----------------------------------------------------------------------===//
27// CallSite Class
28//===----------------------------------------------------------------------===//
29
Duncan Sands85fab3a2008-02-18 17:32:13 +000030CallSite::CallSite(Instruction *C) {
31 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
32 I = C;
33}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000034unsigned CallSite::getCallingConv() const {
35 if (CallInst *CI = dyn_cast<CallInst>(I))
36 return CI->getCallingConv();
37 else
38 return cast<InvokeInst>(I)->getCallingConv();
39}
40void CallSite::setCallingConv(unsigned CC) {
41 if (CallInst *CI = dyn_cast<CallInst>(I))
42 CI->setCallingConv(CC);
43 else
44 cast<InvokeInst>(I)->setCallingConv(CC);
45}
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000046const ParamAttrsList* CallSite::getParamAttrs() const {
47 if (CallInst *CI = dyn_cast<CallInst>(I))
48 return CI->getParamAttrs();
49 else
50 return cast<InvokeInst>(I)->getParamAttrs();
51}
52void CallSite::setParamAttrs(const ParamAttrsList *PAL) {
53 if (CallInst *CI = dyn_cast<CallInst>(I))
54 CI->setParamAttrs(PAL);
55 else
56 cast<InvokeInst>(I)->setParamAttrs(PAL);
57}
Dale Johannesen89268bc2008-02-19 21:38:47 +000058bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
Duncan Sands5208d1a2007-11-28 17:07:01 +000059 if (CallInst *CI = dyn_cast<CallInst>(I))
Dale Johannesen89268bc2008-02-19 21:38:47 +000060 return CI->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000061 else
Dale Johannesen89268bc2008-02-19 21:38:47 +000062 return cast<InvokeInst>(I)->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000063}
Duncan Sands38ef3a82007-12-03 20:06:50 +000064bool CallSite::doesNotAccessMemory() const {
65 if (CallInst *CI = dyn_cast<CallInst>(I))
66 return CI->doesNotAccessMemory();
67 else
68 return cast<InvokeInst>(I)->doesNotAccessMemory();
69}
70bool CallSite::onlyReadsMemory() const {
71 if (CallInst *CI = dyn_cast<CallInst>(I))
72 return CI->onlyReadsMemory();
73 else
74 return cast<InvokeInst>(I)->onlyReadsMemory();
75}
Duncan Sands3353ed02007-12-18 09:59:50 +000076bool CallSite::doesNotThrow() const {
Duncan Sands8e4847e2007-12-16 15:51:49 +000077 if (CallInst *CI = dyn_cast<CallInst>(I))
Duncan Sands3353ed02007-12-18 09:59:50 +000078 return CI->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000079 else
Duncan Sands3353ed02007-12-18 09:59:50 +000080 return cast<InvokeInst>(I)->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000081}
Duncan Sandsaa31b922007-12-19 21:13:37 +000082void CallSite::setDoesNotThrow(bool doesNotThrow) {
83 if (CallInst *CI = dyn_cast<CallInst>(I))
84 CI->setDoesNotThrow(doesNotThrow);
85 else
86 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
87}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000088
Gordon Henriksen14a55692007-12-10 02:14:30 +000089//===----------------------------------------------------------------------===//
90// TerminatorInst Class
91//===----------------------------------------------------------------------===//
92
93// Out of line virtual method, so the vtable, etc has a home.
94TerminatorInst::~TerminatorInst() {
95}
96
97// Out of line virtual method, so the vtable, etc has a home.
98UnaryInstruction::~UnaryInstruction() {
99}
100
101
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000102//===----------------------------------------------------------------------===//
103// PHINode Class
104//===----------------------------------------------------------------------===//
105
106PHINode::PHINode(const PHINode &PN)
107 : Instruction(PN.getType(), Instruction::PHI,
108 new Use[PN.getNumOperands()], PN.getNumOperands()),
109 ReservedSpace(PN.getNumOperands()) {
110 Use *OL = OperandList;
111 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
112 OL[i].init(PN.getOperand(i), this);
113 OL[i+1].init(PN.getOperand(i+1), this);
114 }
115}
116
Gordon Henriksen14a55692007-12-10 02:14:30 +0000117PHINode::~PHINode() {
118 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000119}
120
121// removeIncomingValue - Remove an incoming value. This is useful if a
122// predecessor basic block is deleted.
123Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
124 unsigned NumOps = getNumOperands();
125 Use *OL = OperandList;
126 assert(Idx*2 < NumOps && "BB not in PHI node!");
127 Value *Removed = OL[Idx*2];
128
129 // Move everything after this operand down.
130 //
131 // FIXME: we could just swap with the end of the list, then erase. However,
132 // client might not expect this to happen. The code as it is thrashes the
133 // use/def lists, which is kinda lame.
134 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
135 OL[i-2] = OL[i];
136 OL[i-2+1] = OL[i+1];
137 }
138
139 // Nuke the last value.
140 OL[NumOps-2].set(0);
141 OL[NumOps-2+1].set(0);
142 NumOperands = NumOps-2;
143
144 // If the PHI node is dead, because it has zero entries, nuke it now.
145 if (NumOps == 2 && DeletePHIIfEmpty) {
146 // If anyone is using this PHI, make them use a dummy value instead...
147 replaceAllUsesWith(UndefValue::get(getType()));
148 eraseFromParent();
149 }
150 return Removed;
151}
152
153/// resizeOperands - resize operands - This adjusts the length of the operands
154/// list according to the following behavior:
155/// 1. If NumOps == 0, grow the operand list in response to a push_back style
156/// of operation. This grows the number of ops by 1.5 times.
157/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
158/// 3. If NumOps == NumOperands, trim the reserved space.
159///
160void PHINode::resizeOperands(unsigned NumOps) {
161 if (NumOps == 0) {
162 NumOps = (getNumOperands())*3/2;
163 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
164 } else if (NumOps*2 > NumOperands) {
165 // No resize needed.
166 if (ReservedSpace >= NumOps) return;
167 } else if (NumOps == NumOperands) {
168 if (ReservedSpace == NumOps) return;
169 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000170 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000171 }
172
173 ReservedSpace = NumOps;
174 Use *NewOps = new Use[NumOps];
175 Use *OldOps = OperandList;
176 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
177 NewOps[i].init(OldOps[i], this);
178 OldOps[i].set(0);
179 }
180 delete [] OldOps;
181 OperandList = NewOps;
182}
183
Nate Begemanb3923212005-08-04 23:24:19 +0000184/// hasConstantValue - If the specified PHI node always merges together the same
185/// value, return the value, otherwise return null.
186///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000187Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000188 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000189 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000190 if (getIncomingValue(0) != this) // not X = phi X
191 return getIncomingValue(0);
192 else
193 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000194 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000195
Nate Begemanb3923212005-08-04 23:24:19 +0000196 // Otherwise if all of the incoming values are the same for the PHI, replace
197 // the PHI node with the incoming value.
198 //
199 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000200 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000201 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000202 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000203 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000204 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000205 if (InVal && getIncomingValue(i) != InVal)
206 return 0; // Not the same, bail out.
207 else
208 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000209 }
Nate Begemanb3923212005-08-04 23:24:19 +0000210
211 // The only case that could cause InVal to be null is if we have a PHI node
212 // that only has entries for itself. In this case, there is no entry into the
213 // loop, so kill the PHI.
214 //
215 if (InVal == 0) InVal = UndefValue::get(getType());
216
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000217 // If we have a PHI node like phi(X, undef, X), where X is defined by some
218 // instruction, we cannot always return X as the result of the PHI node. Only
219 // do this if X is not an instruction (thus it must dominate the PHI block),
220 // or if the client is prepared to deal with this possibility.
221 if (HasUndefInput && !AllowNonDominatingInstruction)
222 if (Instruction *IV = dyn_cast<Instruction>(InVal))
223 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000224 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000225 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000226 return 0; // Cannot guarantee that InVal dominates this PHINode.
227
Nate Begemanb3923212005-08-04 23:24:19 +0000228 // All of the incoming values are the same, return the value now.
229 return InVal;
230}
231
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000232
233//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000234// CallInst Implementation
235//===----------------------------------------------------------------------===//
236
Gordon Henriksen14a55692007-12-10 02:14:30 +0000237CallInst::~CallInst() {
238 delete [] OperandList;
239 if (ParamAttrs)
240 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000241}
242
Chris Lattner054ba2c2007-02-13 00:58:44 +0000243void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000244 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000245 NumOperands = NumParams+1;
246 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000247 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000248
Misha Brukmanb1c93172005-04-21 23:48:37 +0000249 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000250 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000251 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000252
Chris Lattner054ba2c2007-02-13 00:58:44 +0000253 assert((NumParams == FTy->getNumParams() ||
254 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000255 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000256 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000257 assert((i >= FTy->getNumParams() ||
258 FTy->getParamType(i) == Params[i]->getType()) &&
259 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000260 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000261 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262}
263
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000264void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000265 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000266 NumOperands = 3;
267 Use *OL = OperandList = new Use[3];
268 OL[0].init(Func, this);
269 OL[1].init(Actual1, this);
270 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000271
272 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000274 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000275
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000276 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000277 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000278 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000279 assert((0 >= FTy->getNumParams() ||
280 FTy->getParamType(0) == Actual1->getType()) &&
281 "Calling a function with a bad signature!");
282 assert((1 >= FTy->getNumParams() ||
283 FTy->getParamType(1) == Actual2->getType()) &&
284 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285}
286
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000287void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000288 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000289 NumOperands = 2;
290 Use *OL = OperandList = new Use[2];
291 OL[0].init(Func, this);
292 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000293
294 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000295 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000296 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000297
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000298 assert((FTy->getNumParams() == 1 ||
299 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000301 assert((0 == FTy->getNumParams() ||
302 FTy->getParamType(0) == Actual->getType()) &&
303 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304}
305
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000306void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000307 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000308 NumOperands = 1;
309 Use *OL = OperandList = new Use[1];
310 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000311
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000312 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000314 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000316 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000317}
318
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000319CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000320 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000321 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
322 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000323 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000325 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000326}
327
328CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
329 BasicBlock *InsertAtEnd)
330 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
331 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000332 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000333 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000334 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336CallInst::CallInst(Value *Func, const std::string &Name,
337 Instruction *InsertBefore)
338 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
339 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000340 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000342 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000343}
344
345CallInst::CallInst(Value *Func, const std::string &Name,
346 BasicBlock *InsertAtEnd)
347 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
348 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000349 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000351 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000352}
353
Misha Brukmanb1c93172005-04-21 23:48:37 +0000354CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000355 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000356 CI.getNumOperands()),
357 ParamAttrs(0) {
358 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000359 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000360 Use *OL = OperandList;
361 Use *InOL = CI.OperandList;
362 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
363 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000364}
365
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000366void CallInst::setParamAttrs(const ParamAttrsList *newAttrs) {
367 if (ParamAttrs == newAttrs)
368 return;
369
Reid Spencerc6a83842007-04-22 17:28:03 +0000370 if (ParamAttrs)
371 ParamAttrs->dropRef();
372
373 if (newAttrs)
374 newAttrs->addRef();
375
376 ParamAttrs = newAttrs;
377}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000378
Dale Johannesen89268bc2008-02-19 21:38:47 +0000379bool CallInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
380 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000381 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000382 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000383 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000384 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000385}
386
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000387/// @brief Determine if the call does not access memory.
388bool CallInst::doesNotAccessMemory() const {
389 return paramHasAttr(0, ParamAttr::ReadNone);
390}
391
392/// @brief Determine if the call does not access or only reads memory.
393bool CallInst::onlyReadsMemory() const {
394 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
395}
396
397/// @brief Determine if the call cannot return.
398bool CallInst::doesNotReturn() const {
399 return paramHasAttr(0, ParamAttr::NoReturn);
400}
401
402/// @brief Determine if the call cannot unwind.
403bool CallInst::doesNotThrow() const {
404 return paramHasAttr(0, ParamAttr::NoUnwind);
405}
406
407/// @brief Determine if the call returns a structure.
408bool CallInst::isStructReturn() const {
409 // Be friendly and also check the callee.
410 return paramHasAttr(1, ParamAttr::StructRet);
411}
412
Evan Cheng09dde602008-01-12 18:57:32 +0000413/// @brief Determine if any call argument is an aggregate passed by value.
414bool CallInst::hasByValArgument() const {
Duncan Sandsa59f3962008-01-21 11:27:55 +0000415 if (ParamAttrs && ParamAttrs->hasAttrSomewhere(ParamAttr::ByVal))
416 return true;
417 // Be consistent with other methods and check the callee too.
418 if (const Function *F = getCalledFunction())
419 if (const ParamAttrsList *PAL = F->getParamAttrs())
420 return PAL->hasAttrSomewhere(ParamAttr::ByVal);
421 return false;
Evan Cheng09dde602008-01-12 18:57:32 +0000422}
423
Duncan Sandsaa31b922007-12-19 21:13:37 +0000424void CallInst::setDoesNotThrow(bool doesNotThrow) {
425 const ParamAttrsList *PAL = getParamAttrs();
426 if (doesNotThrow)
427 PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind);
428 else
429 PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind);
430 setParamAttrs(PAL);
431}
432
Duncan Sands5208d1a2007-11-28 17:07:01 +0000433
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000434//===----------------------------------------------------------------------===//
435// InvokeInst Implementation
436//===----------------------------------------------------------------------===//
437
Gordon Henriksen14a55692007-12-10 02:14:30 +0000438InvokeInst::~InvokeInst() {
439 delete [] OperandList;
440 if (ParamAttrs)
441 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000442}
443
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000444void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000445 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000446 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000447 NumOperands = 3+NumArgs;
448 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000449 OL[0].init(Fn, this);
450 OL[1].init(IfNormal, this);
451 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000452 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000453 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000454 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000455
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000456 assert(((NumArgs == FTy->getNumParams()) ||
457 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000458 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000459
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000460 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000461 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000462 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000463 "Invoking a function with a bad signature!");
464
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000465 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000466 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000467}
468
Misha Brukmanb1c93172005-04-21 23:48:37 +0000469InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000470 : TerminatorInst(II.getType(), Instruction::Invoke,
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000471 new Use[II.getNumOperands()], II.getNumOperands()),
472 ParamAttrs(0) {
473 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000474 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000475 Use *OL = OperandList, *InOL = II.OperandList;
476 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
477 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000478}
479
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000480BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
481 return getSuccessor(idx);
482}
483unsigned InvokeInst::getNumSuccessorsV() const {
484 return getNumSuccessors();
485}
486void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
487 return setSuccessor(idx, B);
488}
489
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000490void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) {
491 if (ParamAttrs == newAttrs)
492 return;
493
Reid Spencerc6a83842007-04-22 17:28:03 +0000494 if (ParamAttrs)
495 ParamAttrs->dropRef();
496
497 if (newAttrs)
498 newAttrs->addRef();
499
500 ParamAttrs = newAttrs;
501}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000502
Dale Johannesen89268bc2008-02-19 21:38:47 +0000503bool InvokeInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
504 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000505 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000506 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000507 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000508 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000509}
510
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000511
512/// @brief Determine if the call does not access memory.
513bool InvokeInst::doesNotAccessMemory() const {
514 return paramHasAttr(0, ParamAttr::ReadNone);
515}
516
517/// @brief Determine if the call does not access or only reads memory.
518bool InvokeInst::onlyReadsMemory() const {
519 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
520}
521
522/// @brief Determine if the call cannot return.
523bool InvokeInst::doesNotReturn() const {
524 return paramHasAttr(0, ParamAttr::NoReturn);
525}
526
527/// @brief Determine if the call cannot unwind.
528bool InvokeInst::doesNotThrow() const {
529 return paramHasAttr(0, ParamAttr::NoUnwind);
530}
531
Duncan Sandsaa31b922007-12-19 21:13:37 +0000532void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
533 const ParamAttrsList *PAL = getParamAttrs();
534 if (doesNotThrow)
535 PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind);
536 else
537 PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind);
538 setParamAttrs(PAL);
539}
540
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000541/// @brief Determine if the call returns a structure.
542bool InvokeInst::isStructReturn() const {
543 // Be friendly and also check the callee.
544 return paramHasAttr(1, ParamAttr::StructRet);
545}
546
Duncan Sands5208d1a2007-11-28 17:07:01 +0000547
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000548//===----------------------------------------------------------------------===//
549// ReturnInst Implementation
550//===----------------------------------------------------------------------===//
551
Chris Lattner2195fc42007-02-24 00:55:48 +0000552ReturnInst::ReturnInst(const ReturnInst &RI)
553 : TerminatorInst(Type::VoidTy, Instruction::Ret,
554 &RetVal, RI.getNumOperands()) {
555 if (RI.getNumOperands())
556 RetVal.init(RI.RetVal, this);
557}
558
559ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
560 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
561 init(retVal);
562}
563ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
564 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
565 init(retVal);
566}
567ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
568 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
569}
570
571
572
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000573void ReturnInst::init(Value *retVal) {
574 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000575 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000576 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000577 NumOperands = 1;
578 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000579 }
580}
581
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000582unsigned ReturnInst::getNumSuccessorsV() const {
583 return getNumSuccessors();
584}
585
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000586// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
587// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000588void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000589 assert(0 && "ReturnInst has no successors!");
590}
591
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000592BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
593 assert(0 && "ReturnInst has no successors!");
594 abort();
595 return 0;
596}
597
598
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000599//===----------------------------------------------------------------------===//
600// UnwindInst Implementation
601//===----------------------------------------------------------------------===//
602
Chris Lattner2195fc42007-02-24 00:55:48 +0000603UnwindInst::UnwindInst(Instruction *InsertBefore)
604 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
605}
606UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
607 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
608}
609
610
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000611unsigned UnwindInst::getNumSuccessorsV() const {
612 return getNumSuccessors();
613}
614
615void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000616 assert(0 && "UnwindInst has no successors!");
617}
618
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000619BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
620 assert(0 && "UnwindInst has no successors!");
621 abort();
622 return 0;
623}
624
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000625//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000626// UnreachableInst Implementation
627//===----------------------------------------------------------------------===//
628
Chris Lattner2195fc42007-02-24 00:55:48 +0000629UnreachableInst::UnreachableInst(Instruction *InsertBefore)
630 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
631}
632UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
633 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
634}
635
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000636unsigned UnreachableInst::getNumSuccessorsV() const {
637 return getNumSuccessors();
638}
639
640void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
641 assert(0 && "UnwindInst has no successors!");
642}
643
644BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
645 assert(0 && "UnwindInst has no successors!");
646 abort();
647 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000648}
649
650//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000651// BranchInst Implementation
652//===----------------------------------------------------------------------===//
653
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000654void BranchInst::AssertOK() {
655 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000656 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000657 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000658}
659
Chris Lattner2195fc42007-02-24 00:55:48 +0000660BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
661 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
662 assert(IfTrue != 0 && "Branch destination may not be null!");
663 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
664}
665BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
666 Instruction *InsertBefore)
667: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
668 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
669 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
670 Ops[2].init(Cond, this);
671#ifndef NDEBUG
672 AssertOK();
673#endif
674}
675
676BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
677 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
678 assert(IfTrue != 0 && "Branch destination may not be null!");
679 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
680}
681
682BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
683 BasicBlock *InsertAtEnd)
684 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
685 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
686 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
687 Ops[2].init(Cond, this);
688#ifndef NDEBUG
689 AssertOK();
690#endif
691}
692
693
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000694BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000695 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000696 OperandList[0].init(BI.getOperand(0), this);
697 if (BI.getNumOperands() != 1) {
698 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
699 OperandList[1].init(BI.getOperand(1), this);
700 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000701 }
702}
703
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000704BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
705 return getSuccessor(idx);
706}
707unsigned BranchInst::getNumSuccessorsV() const {
708 return getNumSuccessors();
709}
710void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
711 setSuccessor(idx, B);
712}
713
714
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000715//===----------------------------------------------------------------------===//
716// AllocationInst Implementation
717//===----------------------------------------------------------------------===//
718
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000719static Value *getAISize(Value *Amt) {
720 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000721 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000722 else {
723 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000724 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000725 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000726 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000727 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000728 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000729}
730
Misha Brukmanb1c93172005-04-21 23:48:37 +0000731AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000732 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000734 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000735 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000736 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000737 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000738 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000739}
740
Misha Brukmanb1c93172005-04-21 23:48:37 +0000741AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000742 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000743 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000744 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000745 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000746 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000747 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000748 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000749}
750
Gordon Henriksen14a55692007-12-10 02:14:30 +0000751// Out of line virtual method, so the vtable, etc has a home.
752AllocationInst::~AllocationInst() {
753}
754
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000755bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000756 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
757 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000758 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000759}
760
761const Type *AllocationInst::getAllocatedType() const {
762 return getType()->getElementType();
763}
764
765AllocaInst::AllocaInst(const AllocaInst &AI)
766 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000767 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000768}
769
770MallocInst::MallocInst(const MallocInst &MI)
771 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000772 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000773}
774
775//===----------------------------------------------------------------------===//
776// FreeInst Implementation
777//===----------------------------------------------------------------------===//
778
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000779void FreeInst::AssertOK() {
780 assert(isa<PointerType>(getOperand(0)->getType()) &&
781 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000782}
783
784FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000785 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000786 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000787}
788
789FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000790 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000791 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000792}
793
794
795//===----------------------------------------------------------------------===//
796// LoadInst Implementation
797//===----------------------------------------------------------------------===//
798
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000799void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000800 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000801 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000802}
803
804LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000805 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000806 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000807 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000808 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000809 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000810 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000811}
812
813LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000814 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000815 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000816 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000817 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000818 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000819 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000820}
821
822LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
823 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000824 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000825 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000826 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000827 setAlignment(0);
828 AssertOK();
829 setName(Name);
830}
831
832LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
833 unsigned Align, Instruction *InsertBef)
834 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
835 Load, Ptr, InsertBef) {
836 setVolatile(isVolatile);
837 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000838 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000839 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000840}
841
Dan Gohman68659282007-07-18 20:51:11 +0000842LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
843 unsigned Align, BasicBlock *InsertAE)
844 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
845 Load, Ptr, InsertAE) {
846 setVolatile(isVolatile);
847 setAlignment(Align);
848 AssertOK();
849 setName(Name);
850}
851
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000852LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
853 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000854 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000855 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000856 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000857 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000858 AssertOK();
859 setName(Name);
860}
861
862
863
864LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000865 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
866 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000867 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000868 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000869 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000870 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000871}
872
873LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000874 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
875 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000876 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000877 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000878 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000879 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000880}
881
882LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
883 Instruction *InsertBef)
884: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000885 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000886 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000887 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000888 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000889 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000890}
891
892LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
893 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000894 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
895 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000896 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000897 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000898 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000899 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000900}
901
Christopher Lamb84485702007-04-22 19:24:39 +0000902void LoadInst::setAlignment(unsigned Align) {
903 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
904 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
905}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000906
907//===----------------------------------------------------------------------===//
908// StoreInst Implementation
909//===----------------------------------------------------------------------===//
910
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000911void StoreInst::AssertOK() {
912 assert(isa<PointerType>(getOperand(1)->getType()) &&
913 "Ptr must have pointer type!");
914 assert(getOperand(0)->getType() ==
915 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000916 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000917}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000918
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000919
920StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000921 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000922 Ops[0].init(val, this);
923 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000924 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000925 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000926 AssertOK();
927}
928
929StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000930 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000931 Ops[0].init(val, this);
932 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000933 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000934 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000935 AssertOK();
936}
937
Misha Brukmanb1c93172005-04-21 23:48:37 +0000938StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000939 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000940 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000941 Ops[0].init(val, this);
942 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000943 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000944 setAlignment(0);
945 AssertOK();
946}
947
948StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
949 unsigned Align, Instruction *InsertBefore)
950 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
951 Ops[0].init(val, this);
952 Ops[1].init(addr, this);
953 setVolatile(isVolatile);
954 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000955 AssertOK();
956}
957
Misha Brukmanb1c93172005-04-21 23:48:37 +0000958StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000959 unsigned Align, BasicBlock *InsertAtEnd)
960 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
961 Ops[0].init(val, this);
962 Ops[1].init(addr, this);
963 setVolatile(isVolatile);
964 setAlignment(Align);
965 AssertOK();
966}
967
968StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000969 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000970 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000971 Ops[0].init(val, this);
972 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000973 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000974 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000975 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000976}
977
Christopher Lamb84485702007-04-22 19:24:39 +0000978void StoreInst::setAlignment(unsigned Align) {
979 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
980 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
981}
982
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000983//===----------------------------------------------------------------------===//
984// GetElementPtrInst Implementation
985//===----------------------------------------------------------------------===//
986
Christopher Lambedf07882007-12-17 01:12:55 +0000987static unsigned retrieveAddrSpace(const Value *Val) {
988 return cast<PointerType>(Val->getType())->getAddressSpace();
989}
990
Chris Lattner79807c3d2007-01-31 19:47:18 +0000991void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
992 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000993 Use *OL = OperandList = new Use[NumOperands];
994 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000995
Chris Lattner79807c3d2007-01-31 19:47:18 +0000996 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000997 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000998}
999
Chris Lattner82981202005-05-03 05:43:30 +00001000void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
1001 NumOperands = 2;
1002 Use *OL = OperandList = new Use[2];
1003 OL[0].init(Ptr, this);
1004 OL[1].init(Idx, this);
1005}
1006
Chris Lattner82981202005-05-03 05:43:30 +00001007GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1008 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001009 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001010 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001011 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +00001012 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001013 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001014}
1015
1016GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1017 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001018 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001019 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001020 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +00001021 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001022 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001023}
1024
Gordon Henriksen14a55692007-12-10 02:14:30 +00001025GetElementPtrInst::~GetElementPtrInst() {
1026 delete[] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001027}
1028
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001029// getIndexedType - Returns the type of the element that would be loaded with
1030// a load instruction with the specified parameters.
1031//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001032// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001033// pointer type.
1034//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001035const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001036 Value* const *Idxs,
1037 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001038 bool AllowCompositeLeaf) {
1039 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
1040
1041 // Handle the special case of the empty set index set...
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001042 if (NumIdx == 0) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001043 if (AllowCompositeLeaf ||
1044 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
1045 return cast<PointerType>(Ptr)->getElementType();
1046 else
1047 return 0;
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001048 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001049
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001050 unsigned CurIdx = 0;
1051 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001052 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001053 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
1054 return 0; // Can't load a whole structure or array!?!?
1055 }
1056
Chris Lattner302116a2007-01-31 04:40:28 +00001057 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001058 if (isa<PointerType>(CT) && CurIdx != 1)
1059 return 0; // Can only index into pointer types at the first index!
1060 if (!CT->indexValid(Index)) return 0;
1061 Ptr = CT->getTypeAtIndex(Index);
1062
1063 // If the new type forwards to another type, then it is in the middle
1064 // of being refined to another type (and hence, may have dropped all
1065 // references to what it was using before). So, use the new forwarded
1066 // type.
1067 if (const Type * Ty = Ptr->getForwardedType()) {
1068 Ptr = Ty;
1069 }
1070 }
Chris Lattner302116a2007-01-31 04:40:28 +00001071 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001072}
1073
Chris Lattner82981202005-05-03 05:43:30 +00001074const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1075 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1076 if (!PTy) return 0; // Type isn't a pointer type!
1077
1078 // Check the pointer index.
1079 if (!PTy->indexValid(Idx)) return 0;
1080
Chris Lattnerc2233332005-05-03 16:44:45 +00001081 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001082}
1083
Chris Lattner45f15572007-04-14 00:12:57 +00001084
1085/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1086/// zeros. If so, the result pointer and the first operand have the same
1087/// value, just potentially different types.
1088bool GetElementPtrInst::hasAllZeroIndices() const {
1089 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1090 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1091 if (!CI->isZero()) return false;
1092 } else {
1093 return false;
1094 }
1095 }
1096 return true;
1097}
1098
Chris Lattner27058292007-04-27 20:35:56 +00001099/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1100/// constant integers. If so, the result pointer and the first operand have
1101/// a constant offset between them.
1102bool GetElementPtrInst::hasAllConstantIndices() const {
1103 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1104 if (!isa<ConstantInt>(getOperand(i)))
1105 return false;
1106 }
1107 return true;
1108}
1109
Chris Lattner45f15572007-04-14 00:12:57 +00001110
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001111//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001112// ExtractElementInst Implementation
1113//===----------------------------------------------------------------------===//
1114
1115ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001116 const std::string &Name,
1117 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001118 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001119 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001120 assert(isValidOperands(Val, Index) &&
1121 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001122 Ops[0].init(Val, this);
1123 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001124 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001125}
1126
Chris Lattner65511ff2006-10-05 06:24:58 +00001127ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1128 const std::string &Name,
1129 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001130 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001131 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001132 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001133 assert(isValidOperands(Val, Index) &&
1134 "Invalid extractelement instruction operands!");
1135 Ops[0].init(Val, this);
1136 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001137 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001138}
1139
1140
Robert Bocchino23004482006-01-10 19:05:34 +00001141ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001142 const std::string &Name,
1143 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001144 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001145 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001146 assert(isValidOperands(Val, Index) &&
1147 "Invalid extractelement instruction operands!");
1148
Robert Bocchino23004482006-01-10 19:05:34 +00001149 Ops[0].init(Val, this);
1150 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001151 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001152}
1153
Chris Lattner65511ff2006-10-05 06:24:58 +00001154ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1155 const std::string &Name,
1156 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001157 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001158 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001159 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001160 assert(isValidOperands(Val, Index) &&
1161 "Invalid extractelement instruction operands!");
1162
1163 Ops[0].init(Val, this);
1164 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001165 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001166}
1167
1168
Chris Lattner54865b32006-04-08 04:05:48 +00001169bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001170 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001171 return false;
1172 return true;
1173}
1174
1175
Robert Bocchino23004482006-01-10 19:05:34 +00001176//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001177// InsertElementInst Implementation
1178//===----------------------------------------------------------------------===//
1179
Chris Lattner0875d942006-04-14 22:20:32 +00001180InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1181 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1182 Ops[0].init(IE.Ops[0], this);
1183 Ops[1].init(IE.Ops[1], this);
1184 Ops[2].init(IE.Ops[2], this);
1185}
Chris Lattner54865b32006-04-08 04:05:48 +00001186InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001187 const std::string &Name,
1188 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001189 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001190 assert(isValidOperands(Vec, Elt, Index) &&
1191 "Invalid insertelement instruction operands!");
1192 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001193 Ops[1].init(Elt, this);
1194 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001195 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001196}
1197
Chris Lattner65511ff2006-10-05 06:24:58 +00001198InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1199 const std::string &Name,
1200 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001201 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001202 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001203 assert(isValidOperands(Vec, Elt, Index) &&
1204 "Invalid insertelement instruction operands!");
1205 Ops[0].init(Vec, this);
1206 Ops[1].init(Elt, this);
1207 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001208 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001209}
1210
1211
Chris Lattner54865b32006-04-08 04:05:48 +00001212InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001213 const std::string &Name,
1214 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001215 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001216 assert(isValidOperands(Vec, Elt, Index) &&
1217 "Invalid insertelement instruction operands!");
1218
1219 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001220 Ops[1].init(Elt, this);
1221 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001222 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001223}
1224
Chris Lattner65511ff2006-10-05 06:24:58 +00001225InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1226 const std::string &Name,
1227 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001228: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001229 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001230 assert(isValidOperands(Vec, Elt, Index) &&
1231 "Invalid insertelement instruction operands!");
1232
1233 Ops[0].init(Vec, this);
1234 Ops[1].init(Elt, this);
1235 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001236 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001237}
1238
Chris Lattner54865b32006-04-08 04:05:48 +00001239bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1240 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001241 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001242 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001243
Reid Spencerd84d35b2007-02-15 02:26:10 +00001244 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001245 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001246
Reid Spencer8d9336d2006-12-31 05:26:44 +00001247 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001248 return false; // Third operand of insertelement must be uint.
1249 return true;
1250}
1251
1252
Robert Bocchinoca27f032006-01-17 20:07:22 +00001253//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001254// ShuffleVectorInst Implementation
1255//===----------------------------------------------------------------------===//
1256
Chris Lattner0875d942006-04-14 22:20:32 +00001257ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1258 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1259 Ops[0].init(SV.Ops[0], this);
1260 Ops[1].init(SV.Ops[1], this);
1261 Ops[2].init(SV.Ops[2], this);
1262}
1263
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001264ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1265 const std::string &Name,
1266 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001267 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001268 assert(isValidOperands(V1, V2, Mask) &&
1269 "Invalid shuffle vector instruction operands!");
1270 Ops[0].init(V1, this);
1271 Ops[1].init(V2, this);
1272 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001273 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001274}
1275
1276ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1277 const std::string &Name,
1278 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001279 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001280 assert(isValidOperands(V1, V2, Mask) &&
1281 "Invalid shuffle vector instruction operands!");
1282
1283 Ops[0].init(V1, this);
1284 Ops[1].init(V2, this);
1285 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001286 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001287}
1288
1289bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1290 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001291 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001292 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001293 if (!isa<VectorType>(Mask->getType()) ||
1294 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1295 cast<VectorType>(Mask->getType())->getNumElements() !=
1296 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001297 return false;
1298 return true;
1299}
1300
1301
1302//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001303// BinaryOperator Class
1304//===----------------------------------------------------------------------===//
1305
Chris Lattner2195fc42007-02-24 00:55:48 +00001306BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1307 const Type *Ty, const std::string &Name,
1308 Instruction *InsertBefore)
1309 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1310 Ops[0].init(S1, this);
1311 Ops[1].init(S2, this);
1312 init(iType);
1313 setName(Name);
1314}
1315
1316BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1317 const Type *Ty, const std::string &Name,
1318 BasicBlock *InsertAtEnd)
1319 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1320 Ops[0].init(S1, this);
1321 Ops[1].init(S2, this);
1322 init(iType);
1323 setName(Name);
1324}
1325
1326
1327void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001328 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001329 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001330 assert(LHS->getType() == RHS->getType() &&
1331 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001332#ifndef NDEBUG
1333 switch (iType) {
1334 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001335 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001336 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001337 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001338 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001339 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001340 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001341 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001342 case UDiv:
1343 case SDiv:
1344 assert(getType() == LHS->getType() &&
1345 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001346 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1347 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001348 "Incorrect operand type (not integer) for S/UDIV");
1349 break;
1350 case FDiv:
1351 assert(getType() == LHS->getType() &&
1352 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001353 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1354 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001355 && "Incorrect operand type (not floating point) for FDIV");
1356 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001357 case URem:
1358 case SRem:
1359 assert(getType() == LHS->getType() &&
1360 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001361 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1362 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001363 "Incorrect operand type (not integer) for S/UREM");
1364 break;
1365 case FRem:
1366 assert(getType() == LHS->getType() &&
1367 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001368 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1369 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001370 && "Incorrect operand type (not floating point) for FREM");
1371 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001372 case Shl:
1373 case LShr:
1374 case AShr:
1375 assert(getType() == LHS->getType() &&
1376 "Shift operation should return same type as operands!");
1377 assert(getType()->isInteger() &&
1378 "Shift operation requires integer operands");
1379 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001380 case And: case Or:
1381 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001382 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001383 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001384 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001385 (isa<VectorType>(getType()) &&
1386 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001387 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001388 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001389 default:
1390 break;
1391 }
1392#endif
1393}
1394
1395BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001396 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001397 Instruction *InsertBefore) {
1398 assert(S1->getType() == S2->getType() &&
1399 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001400 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001401}
1402
1403BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001404 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001405 BasicBlock *InsertAtEnd) {
1406 BinaryOperator *Res = create(Op, S1, S2, Name);
1407 InsertAtEnd->getInstList().push_back(Res);
1408 return Res;
1409}
1410
1411BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1412 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001413 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1414 return new BinaryOperator(Instruction::Sub,
1415 zero, Op,
1416 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001417}
1418
1419BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1420 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001421 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1422 return new BinaryOperator(Instruction::Sub,
1423 zero, Op,
1424 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001425}
1426
1427BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1428 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001429 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001430 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001431 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001432 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001433 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001434 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001435 }
1436
1437 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001438 Op->getType(), Name, InsertBefore);
1439}
1440
1441BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1442 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001443 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001444 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001445 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001446 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001447 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001448 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001449 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001450 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001451 }
1452
1453 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001454 Op->getType(), Name, InsertAtEnd);
1455}
1456
1457
1458// isConstantAllOnes - Helper function for several functions below
1459static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001460 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1461 return CI->isAllOnesValue();
1462 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1463 return CV->isAllOnesValue();
1464 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001465}
1466
1467bool BinaryOperator::isNeg(const Value *V) {
1468 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1469 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001470 return Bop->getOperand(0) ==
1471 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001472 return false;
1473}
1474
1475bool BinaryOperator::isNot(const Value *V) {
1476 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1477 return (Bop->getOpcode() == Instruction::Xor &&
1478 (isConstantAllOnes(Bop->getOperand(1)) ||
1479 isConstantAllOnes(Bop->getOperand(0))));
1480 return false;
1481}
1482
Chris Lattner2c7d1772005-04-24 07:28:37 +00001483Value *BinaryOperator::getNegArgument(Value *BinOp) {
1484 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1485 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001486}
1487
Chris Lattner2c7d1772005-04-24 07:28:37 +00001488const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1489 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001490}
1491
Chris Lattner2c7d1772005-04-24 07:28:37 +00001492Value *BinaryOperator::getNotArgument(Value *BinOp) {
1493 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1494 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1495 Value *Op0 = BO->getOperand(0);
1496 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001497 if (isConstantAllOnes(Op0)) return Op1;
1498
1499 assert(isConstantAllOnes(Op1));
1500 return Op0;
1501}
1502
Chris Lattner2c7d1772005-04-24 07:28:37 +00001503const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1504 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001505}
1506
1507
1508// swapOperands - Exchange the two operands to this instruction. This
1509// instruction is safe to use on any binary instruction and does not
1510// modify the semantics of the instruction. If the instruction is
1511// order dependent (SetLT f.e.) the opcode is changed.
1512//
1513bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001514 if (!isCommutative())
1515 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001516 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001517 return false;
1518}
1519
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001520//===----------------------------------------------------------------------===//
1521// CastInst Class
1522//===----------------------------------------------------------------------===//
1523
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001524// Just determine if this cast only deals with integral->integral conversion.
1525bool CastInst::isIntegerCast() const {
1526 switch (getOpcode()) {
1527 default: return false;
1528 case Instruction::ZExt:
1529 case Instruction::SExt:
1530 case Instruction::Trunc:
1531 return true;
1532 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001533 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001534 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001535}
1536
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001537bool CastInst::isLosslessCast() const {
1538 // Only BitCast can be lossless, exit fast if we're not BitCast
1539 if (getOpcode() != Instruction::BitCast)
1540 return false;
1541
1542 // Identity cast is always lossless
1543 const Type* SrcTy = getOperand(0)->getType();
1544 const Type* DstTy = getType();
1545 if (SrcTy == DstTy)
1546 return true;
1547
Reid Spencer8d9336d2006-12-31 05:26:44 +00001548 // Pointer to pointer is always lossless.
1549 if (isa<PointerType>(SrcTy))
1550 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001551 return false; // Other types have no identity values
1552}
1553
1554/// This function determines if the CastInst does not require any bits to be
1555/// changed in order to effect the cast. Essentially, it identifies cases where
1556/// no code gen is necessary for the cast, hence the name no-op cast. For
1557/// example, the following are all no-op casts:
1558/// # bitcast uint %X, int
1559/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001560/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001561/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1562/// @brief Determine if a cast is a no-op.
1563bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1564 switch (getOpcode()) {
1565 default:
1566 assert(!"Invalid CastOp");
1567 case Instruction::Trunc:
1568 case Instruction::ZExt:
1569 case Instruction::SExt:
1570 case Instruction::FPTrunc:
1571 case Instruction::FPExt:
1572 case Instruction::UIToFP:
1573 case Instruction::SIToFP:
1574 case Instruction::FPToUI:
1575 case Instruction::FPToSI:
1576 return false; // These always modify bits
1577 case Instruction::BitCast:
1578 return true; // BitCast never modifies bits.
1579 case Instruction::PtrToInt:
1580 return IntPtrTy->getPrimitiveSizeInBits() ==
1581 getType()->getPrimitiveSizeInBits();
1582 case Instruction::IntToPtr:
1583 return IntPtrTy->getPrimitiveSizeInBits() ==
1584 getOperand(0)->getType()->getPrimitiveSizeInBits();
1585 }
1586}
1587
1588/// This function determines if a pair of casts can be eliminated and what
1589/// opcode should be used in the elimination. This assumes that there are two
1590/// instructions like this:
1591/// * %F = firstOpcode SrcTy %x to MidTy
1592/// * %S = secondOpcode MidTy %F to DstTy
1593/// The function returns a resultOpcode so these two casts can be replaced with:
1594/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1595/// If no such cast is permited, the function returns 0.
1596unsigned CastInst::isEliminableCastPair(
1597 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1598 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1599{
1600 // Define the 144 possibilities for these two cast instructions. The values
1601 // in this matrix determine what to do in a given situation and select the
1602 // case in the switch below. The rows correspond to firstOp, the columns
1603 // correspond to secondOp. In looking at the table below, keep in mind
1604 // the following cast properties:
1605 //
1606 // Size Compare Source Destination
1607 // Operator Src ? Size Type Sign Type Sign
1608 // -------- ------------ ------------------- ---------------------
1609 // TRUNC > Integer Any Integral Any
1610 // ZEXT < Integral Unsigned Integer Any
1611 // SEXT < Integral Signed Integer Any
1612 // FPTOUI n/a FloatPt n/a Integral Unsigned
1613 // FPTOSI n/a FloatPt n/a Integral Signed
1614 // UITOFP n/a Integral Unsigned FloatPt n/a
1615 // SITOFP n/a Integral Signed FloatPt n/a
1616 // FPTRUNC > FloatPt n/a FloatPt n/a
1617 // FPEXT < FloatPt n/a FloatPt n/a
1618 // PTRTOINT n/a Pointer n/a Integral Unsigned
1619 // INTTOPTR n/a Integral Unsigned Pointer n/a
1620 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001621 //
1622 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1623 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1624 // into "fptoui double to ulong", but this loses information about the range
1625 // of the produced value (we no longer know the top-part is all zeros).
1626 // Further this conversion is often much more expensive for typical hardware,
1627 // and causes issues when building libgcc. We disallow fptosi+sext for the
1628 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001629 const unsigned numCastOps =
1630 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1631 static const uint8_t CastResults[numCastOps][numCastOps] = {
1632 // T F F U S F F P I B -+
1633 // R Z S P P I I T P 2 N T |
1634 // U E E 2 2 2 2 R E I T C +- secondOp
1635 // N X X U S F F N X N 2 V |
1636 // C T T I I P P C T T P T -+
1637 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1638 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1639 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001640 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1641 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001642 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1643 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1644 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1645 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1646 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1647 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1648 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1649 };
1650
1651 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1652 [secondOp-Instruction::CastOpsBegin];
1653 switch (ElimCase) {
1654 case 0:
1655 // categorically disallowed
1656 return 0;
1657 case 1:
1658 // allowed, use first cast's opcode
1659 return firstOp;
1660 case 2:
1661 // allowed, use second cast's opcode
1662 return secondOp;
1663 case 3:
1664 // no-op cast in second op implies firstOp as long as the DestTy
1665 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001666 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001667 return firstOp;
1668 return 0;
1669 case 4:
1670 // no-op cast in second op implies firstOp as long as the DestTy
1671 // is floating point
1672 if (DstTy->isFloatingPoint())
1673 return firstOp;
1674 return 0;
1675 case 5:
1676 // no-op cast in first op implies secondOp as long as the SrcTy
1677 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001678 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001679 return secondOp;
1680 return 0;
1681 case 6:
1682 // no-op cast in first op implies secondOp as long as the SrcTy
1683 // is a floating point
1684 if (SrcTy->isFloatingPoint())
1685 return secondOp;
1686 return 0;
1687 case 7: {
1688 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1689 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1690 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1691 if (MidSize >= PtrSize)
1692 return Instruction::BitCast;
1693 return 0;
1694 }
1695 case 8: {
1696 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1697 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1698 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1699 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1700 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1701 if (SrcSize == DstSize)
1702 return Instruction::BitCast;
1703 else if (SrcSize < DstSize)
1704 return firstOp;
1705 return secondOp;
1706 }
1707 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1708 return Instruction::ZExt;
1709 case 10:
1710 // fpext followed by ftrunc is allowed if the bit size returned to is
1711 // the same as the original, in which case its just a bitcast
1712 if (SrcTy == DstTy)
1713 return Instruction::BitCast;
1714 return 0; // If the types are not the same we can't eliminate it.
1715 case 11:
1716 // bitcast followed by ptrtoint is allowed as long as the bitcast
1717 // is a pointer to pointer cast.
1718 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1719 return secondOp;
1720 return 0;
1721 case 12:
1722 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1723 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1724 return firstOp;
1725 return 0;
1726 case 13: {
1727 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1728 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1729 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1730 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1731 if (SrcSize <= PtrSize && SrcSize == DstSize)
1732 return Instruction::BitCast;
1733 return 0;
1734 }
1735 case 99:
1736 // cast combination can't happen (error in input). This is for all cases
1737 // where the MidTy is not the same for the two cast instructions.
1738 assert(!"Invalid Cast Combination");
1739 return 0;
1740 default:
1741 assert(!"Error in CastResults table!!!");
1742 return 0;
1743 }
1744 return 0;
1745}
1746
1747CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1748 const std::string &Name, Instruction *InsertBefore) {
1749 // Construct and return the appropriate CastInst subclass
1750 switch (op) {
1751 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1752 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1753 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1754 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1755 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1756 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1757 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1758 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1759 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1760 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1761 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1762 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1763 default:
1764 assert(!"Invalid opcode provided");
1765 }
1766 return 0;
1767}
1768
1769CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1770 const std::string &Name, BasicBlock *InsertAtEnd) {
1771 // Construct and return the appropriate CastInst subclass
1772 switch (op) {
1773 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1774 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1775 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1776 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1777 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1778 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1779 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1780 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1781 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1782 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1783 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1784 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1785 default:
1786 assert(!"Invalid opcode provided");
1787 }
1788 return 0;
1789}
1790
Reid Spencer5c140882006-12-04 20:17:56 +00001791CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1792 const std::string &Name,
1793 Instruction *InsertBefore) {
1794 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1795 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1796 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1797}
1798
1799CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1800 const std::string &Name,
1801 BasicBlock *InsertAtEnd) {
1802 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1803 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1804 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1805}
1806
1807CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1808 const std::string &Name,
1809 Instruction *InsertBefore) {
1810 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1811 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1812 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1813}
1814
1815CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1816 const std::string &Name,
1817 BasicBlock *InsertAtEnd) {
1818 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1819 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1820 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1821}
1822
1823CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1824 const std::string &Name,
1825 Instruction *InsertBefore) {
1826 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1827 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1828 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1829}
1830
1831CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1832 const std::string &Name,
1833 BasicBlock *InsertAtEnd) {
1834 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1835 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1836 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1837}
1838
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001839CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1840 const std::string &Name,
1841 BasicBlock *InsertAtEnd) {
1842 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001843 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001844 "Invalid cast");
1845
Chris Lattner03c49532007-01-15 02:27:26 +00001846 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001847 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1848 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1849}
1850
1851/// @brief Create a BitCast or a PtrToInt cast instruction
1852CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1853 const std::string &Name,
1854 Instruction *InsertBefore) {
1855 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001856 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001857 "Invalid cast");
1858
Chris Lattner03c49532007-01-15 02:27:26 +00001859 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001860 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1861 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1862}
1863
Reid Spencer7e933472006-12-12 00:49:44 +00001864CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1865 bool isSigned, const std::string &Name,
1866 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001867 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001868 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1869 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1870 Instruction::CastOps opcode =
1871 (SrcBits == DstBits ? Instruction::BitCast :
1872 (SrcBits > DstBits ? Instruction::Trunc :
1873 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1874 return create(opcode, C, Ty, Name, InsertBefore);
1875}
1876
1877CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1878 bool isSigned, const std::string &Name,
1879 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001880 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001881 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1882 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1883 Instruction::CastOps opcode =
1884 (SrcBits == DstBits ? Instruction::BitCast :
1885 (SrcBits > DstBits ? Instruction::Trunc :
1886 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1887 return create(opcode, C, Ty, Name, InsertAtEnd);
1888}
1889
1890CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1891 const std::string &Name,
1892 Instruction *InsertBefore) {
1893 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1894 "Invalid cast");
1895 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1896 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1897 Instruction::CastOps opcode =
1898 (SrcBits == DstBits ? Instruction::BitCast :
1899 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1900 return create(opcode, C, Ty, Name, InsertBefore);
1901}
1902
1903CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1904 const std::string &Name,
1905 BasicBlock *InsertAtEnd) {
1906 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1907 "Invalid cast");
1908 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1909 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1910 Instruction::CastOps opcode =
1911 (SrcBits == DstBits ? Instruction::BitCast :
1912 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1913 return create(opcode, C, Ty, Name, InsertAtEnd);
1914}
1915
Duncan Sands55e50902008-01-06 10:12:28 +00001916// Check whether it is valid to call getCastOpcode for these types.
1917// This routine must be kept in sync with getCastOpcode.
1918bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
1919 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
1920 return false;
1921
1922 if (SrcTy == DestTy)
1923 return true;
1924
1925 // Get the bit sizes, we'll need these
1926 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1927 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1928
1929 // Run through the possibilities ...
1930 if (DestTy->isInteger()) { // Casting to integral
1931 if (SrcTy->isInteger()) { // Casting from integral
1932 return true;
1933 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1934 return true;
1935 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1936 // Casting from vector
1937 return DestBits == PTy->getBitWidth();
1938 } else { // Casting from something else
1939 return isa<PointerType>(SrcTy);
1940 }
1941 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
1942 if (SrcTy->isInteger()) { // Casting from integral
1943 return true;
1944 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1945 return true;
1946 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1947 // Casting from vector
1948 return DestBits == PTy->getBitWidth();
1949 } else { // Casting from something else
1950 return false;
1951 }
1952 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1953 // Casting to vector
1954 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
1955 // Casting from vector
1956 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
1957 } else { // Casting from something else
1958 return DestPTy->getBitWidth() == SrcBits;
1959 }
1960 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
1961 if (isa<PointerType>(SrcTy)) { // Casting from pointer
1962 return true;
1963 } else if (SrcTy->isInteger()) { // Casting from integral
1964 return true;
1965 } else { // Casting from something else
1966 return false;
1967 }
1968 } else { // Casting to something else
1969 return false;
1970 }
1971}
1972
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001973// Provide a way to get a "cast" where the cast opcode is inferred from the
1974// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001975// logic in the castIsValid function below. This axiom should hold:
1976// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1977// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001978// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00001979// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001980Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001981CastInst::getCastOpcode(
1982 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001983 // Get the bit sizes, we'll need these
1984 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00001985 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1986 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001987
Duncan Sands55e50902008-01-06 10:12:28 +00001988 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
1989 "Only first class types are castable!");
1990
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001991 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001992 if (DestTy->isInteger()) { // Casting to integral
1993 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001994 if (DestBits < SrcBits)
1995 return Trunc; // int -> smaller int
1996 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001997 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001998 return SExt; // signed -> SEXT
1999 else
2000 return ZExt; // unsigned -> ZEXT
2001 } else {
2002 return BitCast; // Same size, No-op cast
2003 }
2004 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002005 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002006 return FPToSI; // FP -> sint
2007 else
2008 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002009 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002010 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002011 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002012 return BitCast; // Same size, no-op cast
2013 } else {
2014 assert(isa<PointerType>(SrcTy) &&
2015 "Casting from a value that is not first-class type");
2016 return PtrToInt; // ptr -> int
2017 }
2018 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002019 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002020 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002021 return SIToFP; // sint -> FP
2022 else
2023 return UIToFP; // uint -> FP
2024 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2025 if (DestBits < SrcBits) {
2026 return FPTrunc; // FP -> smaller FP
2027 } else if (DestBits > SrcBits) {
2028 return FPExt; // FP -> larger FP
2029 } else {
2030 return BitCast; // same size, no-op cast
2031 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002032 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002033 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002034 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002035 return BitCast; // same size, no-op cast
2036 } else {
2037 assert(0 && "Casting pointer or non-first class to float");
2038 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002039 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2040 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002041 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002042 "Casting vector to vector of different widths");
2043 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002044 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002045 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002046 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002047 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002048 }
2049 } else if (isa<PointerType>(DestTy)) {
2050 if (isa<PointerType>(SrcTy)) {
2051 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002052 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002053 return IntToPtr; // int -> ptr
2054 } else {
2055 assert(!"Casting pointer to other than pointer or int");
2056 }
2057 } else {
2058 assert(!"Casting to type that is not first-class");
2059 }
2060
2061 // If we fall through to here we probably hit an assertion cast above
2062 // and assertions are not turned on. Anything we return is an error, so
2063 // BitCast is as good a choice as any.
2064 return BitCast;
2065}
2066
2067//===----------------------------------------------------------------------===//
2068// CastInst SubClass Constructors
2069//===----------------------------------------------------------------------===//
2070
2071/// Check that the construction parameters for a CastInst are correct. This
2072/// could be broken out into the separate constructors but it is useful to have
2073/// it in one place and to eliminate the redundant code for getting the sizes
2074/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002075bool
2076CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002077
2078 // Check for type sanity on the arguments
2079 const Type *SrcTy = S->getType();
2080 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2081 return false;
2082
2083 // Get the size of the types in bits, we'll need this later
2084 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2085 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2086
2087 // Switch on the opcode provided
2088 switch (op) {
2089 default: return false; // This is an input error
2090 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002091 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002092 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002093 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002094 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002095 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002096 case Instruction::FPTrunc:
2097 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2098 SrcBitSize > DstBitSize;
2099 case Instruction::FPExt:
2100 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2101 SrcBitSize < DstBitSize;
2102 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002103 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002104 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2105 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2106 return SVTy->getElementType()->isInteger() &&
2107 DVTy->getElementType()->isFloatingPoint() &&
2108 SVTy->getNumElements() == DVTy->getNumElements();
2109 }
2110 }
Chris Lattner03c49532007-01-15 02:27:26 +00002111 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002112 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002113 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002114 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2115 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2116 return SVTy->getElementType()->isFloatingPoint() &&
2117 DVTy->getElementType()->isInteger() &&
2118 SVTy->getNumElements() == DVTy->getNumElements();
2119 }
2120 }
Chris Lattner03c49532007-01-15 02:27:26 +00002121 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002122 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002123 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002124 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002125 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002126 case Instruction::BitCast:
2127 // BitCast implies a no-op cast of type only. No bits change.
2128 // However, you can't cast pointers to anything but pointers.
2129 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2130 return false;
2131
Duncan Sands55e50902008-01-06 10:12:28 +00002132 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002133 // these cases, the cast is okay if the source and destination bit widths
2134 // are identical.
2135 return SrcBitSize == DstBitSize;
2136 }
2137}
2138
2139TruncInst::TruncInst(
2140 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2141) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002142 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002143}
2144
2145TruncInst::TruncInst(
2146 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2147) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002148 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002149}
2150
2151ZExtInst::ZExtInst(
2152 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2153) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002154 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002155}
2156
2157ZExtInst::ZExtInst(
2158 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2159) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002160 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002161}
2162SExtInst::SExtInst(
2163 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2164) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002165 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002166}
2167
Jeff Cohencc08c832006-12-02 02:22:01 +00002168SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002169 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2170) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002171 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002172}
2173
2174FPTruncInst::FPTruncInst(
2175 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2176) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002177 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002178}
2179
2180FPTruncInst::FPTruncInst(
2181 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2182) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002183 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002184}
2185
2186FPExtInst::FPExtInst(
2187 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2188) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002189 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002190}
2191
2192FPExtInst::FPExtInst(
2193 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2194) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002195 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002196}
2197
2198UIToFPInst::UIToFPInst(
2199 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2200) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002201 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002202}
2203
2204UIToFPInst::UIToFPInst(
2205 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2206) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002207 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002208}
2209
2210SIToFPInst::SIToFPInst(
2211 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2212) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002213 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002214}
2215
2216SIToFPInst::SIToFPInst(
2217 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2218) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002219 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002220}
2221
2222FPToUIInst::FPToUIInst(
2223 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2224) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002225 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002226}
2227
2228FPToUIInst::FPToUIInst(
2229 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2230) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002231 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002232}
2233
2234FPToSIInst::FPToSIInst(
2235 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2236) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002237 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002238}
2239
2240FPToSIInst::FPToSIInst(
2241 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2242) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002243 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002244}
2245
2246PtrToIntInst::PtrToIntInst(
2247 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2248) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002249 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002250}
2251
2252PtrToIntInst::PtrToIntInst(
2253 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2254) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002255 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002256}
2257
2258IntToPtrInst::IntToPtrInst(
2259 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2260) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002261 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002262}
2263
2264IntToPtrInst::IntToPtrInst(
2265 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2266) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002267 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002268}
2269
2270BitCastInst::BitCastInst(
2271 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2272) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002273 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002274}
2275
2276BitCastInst::BitCastInst(
2277 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2278) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002279 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002280}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002281
2282//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002283// CmpInst Classes
2284//===----------------------------------------------------------------------===//
2285
2286CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2287 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002288 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002289 Ops[0].init(LHS, this);
2290 Ops[1].init(RHS, this);
2291 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002292 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002293 if (op == Instruction::ICmp) {
2294 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2295 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2296 "Invalid ICmp predicate value");
2297 const Type* Op0Ty = getOperand(0)->getType();
2298 const Type* Op1Ty = getOperand(1)->getType();
2299 assert(Op0Ty == Op1Ty &&
2300 "Both operands to ICmp instruction are not of the same type!");
2301 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002302 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002303 "Invalid operand types for ICmp instruction");
2304 return;
2305 }
2306 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2307 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2308 "Invalid FCmp predicate value");
2309 const Type* Op0Ty = getOperand(0)->getType();
2310 const Type* Op1Ty = getOperand(1)->getType();
2311 assert(Op0Ty == Op1Ty &&
2312 "Both operands to FCmp instruction are not of the same type!");
2313 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002314 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002315 "Invalid operand types for FCmp instruction");
2316}
2317
2318CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2319 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002320 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002321 Ops[0].init(LHS, this);
2322 Ops[1].init(RHS, this);
2323 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002324 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002325 if (op == Instruction::ICmp) {
2326 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2327 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2328 "Invalid ICmp predicate value");
2329
2330 const Type* Op0Ty = getOperand(0)->getType();
2331 const Type* Op1Ty = getOperand(1)->getType();
2332 assert(Op0Ty == Op1Ty &&
2333 "Both operands to ICmp instruction are not of the same type!");
2334 // Check that the operands are the right type
Anton Korobeynikov579f0712008-02-20 11:08:44 +00002335 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002336 "Invalid operand types for ICmp instruction");
2337 return;
2338 }
2339 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2340 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2341 "Invalid FCmp predicate value");
2342 const Type* Op0Ty = getOperand(0)->getType();
2343 const Type* Op1Ty = getOperand(1)->getType();
2344 assert(Op0Ty == Op1Ty &&
2345 "Both operands to FCmp instruction are not of the same type!");
2346 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002347 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002348 "Invalid operand types for FCmp instruction");
2349}
2350
2351CmpInst *
2352CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2353 const std::string &Name, Instruction *InsertBefore) {
2354 if (Op == Instruction::ICmp) {
2355 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2356 InsertBefore);
2357 }
2358 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2359 InsertBefore);
2360}
2361
2362CmpInst *
2363CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2364 const std::string &Name, BasicBlock *InsertAtEnd) {
2365 if (Op == Instruction::ICmp) {
2366 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2367 InsertAtEnd);
2368 }
2369 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2370 InsertAtEnd);
2371}
2372
2373void CmpInst::swapOperands() {
2374 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2375 IC->swapOperands();
2376 else
2377 cast<FCmpInst>(this)->swapOperands();
2378}
2379
2380bool CmpInst::isCommutative() {
2381 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2382 return IC->isCommutative();
2383 return cast<FCmpInst>(this)->isCommutative();
2384}
2385
2386bool CmpInst::isEquality() {
2387 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2388 return IC->isEquality();
2389 return cast<FCmpInst>(this)->isEquality();
2390}
2391
2392
2393ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2394 switch (pred) {
2395 default:
2396 assert(!"Unknown icmp predicate!");
2397 case ICMP_EQ: return ICMP_NE;
2398 case ICMP_NE: return ICMP_EQ;
2399 case ICMP_UGT: return ICMP_ULE;
2400 case ICMP_ULT: return ICMP_UGE;
2401 case ICMP_UGE: return ICMP_ULT;
2402 case ICMP_ULE: return ICMP_UGT;
2403 case ICMP_SGT: return ICMP_SLE;
2404 case ICMP_SLT: return ICMP_SGE;
2405 case ICMP_SGE: return ICMP_SLT;
2406 case ICMP_SLE: return ICMP_SGT;
2407 }
2408}
2409
2410ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2411 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002412 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002413 case ICMP_EQ: case ICMP_NE:
2414 return pred;
2415 case ICMP_SGT: return ICMP_SLT;
2416 case ICMP_SLT: return ICMP_SGT;
2417 case ICMP_SGE: return ICMP_SLE;
2418 case ICMP_SLE: return ICMP_SGE;
2419 case ICMP_UGT: return ICMP_ULT;
2420 case ICMP_ULT: return ICMP_UGT;
2421 case ICMP_UGE: return ICMP_ULE;
2422 case ICMP_ULE: return ICMP_UGE;
2423 }
2424}
2425
Reid Spencer266e42b2006-12-23 06:05:41 +00002426ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2427 switch (pred) {
2428 default: assert(! "Unknown icmp predicate!");
2429 case ICMP_EQ: case ICMP_NE:
2430 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2431 return pred;
2432 case ICMP_UGT: return ICMP_SGT;
2433 case ICMP_ULT: return ICMP_SLT;
2434 case ICMP_UGE: return ICMP_SGE;
2435 case ICMP_ULE: return ICMP_SLE;
2436 }
2437}
2438
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002439ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2440 switch (pred) {
2441 default: assert(! "Unknown icmp predicate!");
2442 case ICMP_EQ: case ICMP_NE:
2443 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2444 return pred;
2445 case ICMP_SGT: return ICMP_UGT;
2446 case ICMP_SLT: return ICMP_ULT;
2447 case ICMP_SGE: return ICMP_UGE;
2448 case ICMP_SLE: return ICMP_ULE;
2449 }
2450}
2451
Reid Spencer266e42b2006-12-23 06:05:41 +00002452bool ICmpInst::isSignedPredicate(Predicate pred) {
2453 switch (pred) {
2454 default: assert(! "Unknown icmp predicate!");
2455 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2456 return true;
2457 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2458 case ICMP_UGE: case ICMP_ULE:
2459 return false;
2460 }
2461}
2462
Reid Spencer0286bc12007-02-28 22:00:54 +00002463/// Initialize a set of values that all satisfy the condition with C.
2464///
2465ConstantRange
2466ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2467 APInt Lower(C);
2468 APInt Upper(C);
2469 uint32_t BitWidth = C.getBitWidth();
2470 switch (pred) {
2471 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2472 case ICmpInst::ICMP_EQ: Upper++; break;
2473 case ICmpInst::ICMP_NE: Lower++; break;
2474 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2475 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2476 case ICmpInst::ICMP_UGT:
2477 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2478 break;
2479 case ICmpInst::ICMP_SGT:
2480 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2481 break;
2482 case ICmpInst::ICMP_ULE:
2483 Lower = APInt::getMinValue(BitWidth); Upper++;
2484 break;
2485 case ICmpInst::ICMP_SLE:
2486 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2487 break;
2488 case ICmpInst::ICMP_UGE:
2489 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2490 break;
2491 case ICmpInst::ICMP_SGE:
2492 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2493 break;
2494 }
2495 return ConstantRange(Lower, Upper);
2496}
2497
Reid Spencerd9436b62006-11-20 01:22:35 +00002498FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2499 switch (pred) {
2500 default:
2501 assert(!"Unknown icmp predicate!");
2502 case FCMP_OEQ: return FCMP_UNE;
2503 case FCMP_ONE: return FCMP_UEQ;
2504 case FCMP_OGT: return FCMP_ULE;
2505 case FCMP_OLT: return FCMP_UGE;
2506 case FCMP_OGE: return FCMP_ULT;
2507 case FCMP_OLE: return FCMP_UGT;
2508 case FCMP_UEQ: return FCMP_ONE;
2509 case FCMP_UNE: return FCMP_OEQ;
2510 case FCMP_UGT: return FCMP_OLE;
2511 case FCMP_ULT: return FCMP_OGE;
2512 case FCMP_UGE: return FCMP_OLT;
2513 case FCMP_ULE: return FCMP_OGT;
2514 case FCMP_ORD: return FCMP_UNO;
2515 case FCMP_UNO: return FCMP_ORD;
2516 case FCMP_TRUE: return FCMP_FALSE;
2517 case FCMP_FALSE: return FCMP_TRUE;
2518 }
2519}
2520
2521FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2522 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002523 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002524 case FCMP_FALSE: case FCMP_TRUE:
2525 case FCMP_OEQ: case FCMP_ONE:
2526 case FCMP_UEQ: case FCMP_UNE:
2527 case FCMP_ORD: case FCMP_UNO:
2528 return pred;
2529 case FCMP_OGT: return FCMP_OLT;
2530 case FCMP_OLT: return FCMP_OGT;
2531 case FCMP_OGE: return FCMP_OLE;
2532 case FCMP_OLE: return FCMP_OGE;
2533 case FCMP_UGT: return FCMP_ULT;
2534 case FCMP_ULT: return FCMP_UGT;
2535 case FCMP_UGE: return FCMP_ULE;
2536 case FCMP_ULE: return FCMP_UGE;
2537 }
2538}
2539
Reid Spencer266e42b2006-12-23 06:05:41 +00002540bool CmpInst::isUnsigned(unsigned short predicate) {
2541 switch (predicate) {
2542 default: return false;
2543 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2544 case ICmpInst::ICMP_UGE: return true;
2545 }
2546}
2547
2548bool CmpInst::isSigned(unsigned short predicate){
2549 switch (predicate) {
2550 default: return false;
2551 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2552 case ICmpInst::ICMP_SGE: return true;
2553 }
2554}
2555
2556bool CmpInst::isOrdered(unsigned short predicate) {
2557 switch (predicate) {
2558 default: return false;
2559 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2560 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2561 case FCmpInst::FCMP_ORD: return true;
2562 }
2563}
2564
2565bool CmpInst::isUnordered(unsigned short predicate) {
2566 switch (predicate) {
2567 default: return false;
2568 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2569 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2570 case FCmpInst::FCMP_UNO: return true;
2571 }
2572}
2573
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002574//===----------------------------------------------------------------------===//
2575// SwitchInst Implementation
2576//===----------------------------------------------------------------------===//
2577
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002578void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002579 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002580 ReservedSpace = 2+NumCases*2;
2581 NumOperands = 2;
2582 OperandList = new Use[ReservedSpace];
2583
2584 OperandList[0].init(Value, this);
2585 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002586}
2587
Chris Lattner2195fc42007-02-24 00:55:48 +00002588/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2589/// switch on and a default destination. The number of additional cases can
2590/// be specified here to make memory allocation more efficient. This
2591/// constructor can also autoinsert before another instruction.
2592SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2593 Instruction *InsertBefore)
2594 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2595 init(Value, Default, NumCases);
2596}
2597
2598/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2599/// switch on and a default destination. The number of additional cases can
2600/// be specified here to make memory allocation more efficient. This
2601/// constructor also autoinserts at the end of the specified BasicBlock.
2602SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2603 BasicBlock *InsertAtEnd)
2604 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2605 init(Value, Default, NumCases);
2606}
2607
Misha Brukmanb1c93172005-04-21 23:48:37 +00002608SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002609 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2610 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002611 Use *OL = OperandList, *InOL = SI.OperandList;
2612 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2613 OL[i].init(InOL[i], this);
2614 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002615 }
2616}
2617
Gordon Henriksen14a55692007-12-10 02:14:30 +00002618SwitchInst::~SwitchInst() {
2619 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002620}
2621
2622
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002623/// addCase - Add an entry to the switch instruction...
2624///
Chris Lattner47ac1872005-02-24 05:32:09 +00002625void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002626 unsigned OpNo = NumOperands;
2627 if (OpNo+2 > ReservedSpace)
2628 resizeOperands(0); // Get more space!
2629 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002630 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002631 NumOperands = OpNo+2;
2632 OperandList[OpNo].init(OnVal, this);
2633 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002634}
2635
2636/// removeCase - This method removes the specified successor from the switch
2637/// instruction. Note that this cannot be used to remove the default
2638/// destination (successor #0).
2639///
2640void SwitchInst::removeCase(unsigned idx) {
2641 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002642 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2643
2644 unsigned NumOps = getNumOperands();
2645 Use *OL = OperandList;
2646
2647 // Move everything after this operand down.
2648 //
2649 // FIXME: we could just swap with the end of the list, then erase. However,
2650 // client might not expect this to happen. The code as it is thrashes the
2651 // use/def lists, which is kinda lame.
2652 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2653 OL[i-2] = OL[i];
2654 OL[i-2+1] = OL[i+1];
2655 }
2656
2657 // Nuke the last value.
2658 OL[NumOps-2].set(0);
2659 OL[NumOps-2+1].set(0);
2660 NumOperands = NumOps-2;
2661}
2662
2663/// resizeOperands - resize operands - This adjusts the length of the operands
2664/// list according to the following behavior:
2665/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2666/// of operation. This grows the number of ops by 1.5 times.
2667/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2668/// 3. If NumOps == NumOperands, trim the reserved space.
2669///
2670void SwitchInst::resizeOperands(unsigned NumOps) {
2671 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002672 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002673 } else if (NumOps*2 > NumOperands) {
2674 // No resize needed.
2675 if (ReservedSpace >= NumOps) return;
2676 } else if (NumOps == NumOperands) {
2677 if (ReservedSpace == NumOps) return;
2678 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002679 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002680 }
2681
2682 ReservedSpace = NumOps;
2683 Use *NewOps = new Use[NumOps];
2684 Use *OldOps = OperandList;
2685 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2686 NewOps[i].init(OldOps[i], this);
2687 OldOps[i].set(0);
2688 }
2689 delete [] OldOps;
2690 OperandList = NewOps;
2691}
2692
2693
2694BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2695 return getSuccessor(idx);
2696}
2697unsigned SwitchInst::getNumSuccessorsV() const {
2698 return getNumSuccessors();
2699}
2700void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2701 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002702}
Chris Lattnerf22be932004-10-15 23:52:53 +00002703
Devang Patel295711f2008-02-19 22:15:16 +00002704//===----------------------------------------------------------------------===//
2705// GetResultInst Implementation
2706//===----------------------------------------------------------------------===//
2707
Devang Patel666d4512008-02-20 19:10:47 +00002708GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002709 const std::string &Name,
2710 Instruction *InsertBef)
Devang Pateldcad9da2008-02-20 19:26:55 +00002711 : Instruction(cast<StructType>(Aggregate->getType())->getElementType(Index),
Devang Patel666d4512008-02-20 19:10:47 +00002712 GetResult, &Aggr, 1, InsertBef) {
2713 assert(isValidOperands(Aggregate, Index) && "Invalid GetResultInst operands!");
2714 Aggr.init(Aggregate, this);
2715 Idx = Index;
Devang Patel295711f2008-02-19 22:15:16 +00002716 setName(Name);
2717}
2718
Devang Patel666d4512008-02-20 19:10:47 +00002719bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2720 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002721 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002722 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType()))
2723 if (Index < STy->getNumElements())
2724 return true;
2725
2726 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002727}
2728
Chris Lattnerf22be932004-10-15 23:52:53 +00002729
2730// Define these methods here so vtables don't get emitted into every translation
2731// unit that uses these classes.
2732
2733GetElementPtrInst *GetElementPtrInst::clone() const {
2734 return new GetElementPtrInst(*this);
2735}
2736
2737BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002738 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002739}
2740
Chris Lattner0b490b02007-08-24 20:48:18 +00002741FCmpInst* FCmpInst::clone() const {
2742 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2743}
2744ICmpInst* ICmpInst::clone() const {
2745 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002746}
2747
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002748MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2749AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2750FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2751LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2752StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2753CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2754CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2755CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2756CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2757CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2758CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2759CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2760CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2761CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2762CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2763CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2764CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2765CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002766SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2767VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2768
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002769ExtractElementInst *ExtractElementInst::clone() const {
2770 return new ExtractElementInst(*this);
2771}
2772InsertElementInst *InsertElementInst::clone() const {
2773 return new InsertElementInst(*this);
2774}
2775ShuffleVectorInst *ShuffleVectorInst::clone() const {
2776 return new ShuffleVectorInst(*this);
2777}
Chris Lattnerf22be932004-10-15 23:52:53 +00002778PHINode *PHINode::clone() const { return new PHINode(*this); }
2779ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2780BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2781SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2782InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2783UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002784UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002785GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }