blob: f7401ec979789ade514462acba668b4b2b9480a4 [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"
Dale Johannesen09f410b2008-02-22 22:17:59 +000020#include "llvm/ParamAttrsList.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}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000064uint16_t CallSite::getParamAlignment(uint16_t i) const {
65 if (CallInst *CI = dyn_cast<CallInst>(I))
66 return CI->getParamAlignment(i);
67 else
68 return cast<InvokeInst>(I)->getParamAlignment(i);
69}
70
Duncan Sands38ef3a82007-12-03 20:06:50 +000071bool CallSite::doesNotAccessMemory() const {
72 if (CallInst *CI = dyn_cast<CallInst>(I))
73 return CI->doesNotAccessMemory();
74 else
75 return cast<InvokeInst>(I)->doesNotAccessMemory();
76}
77bool CallSite::onlyReadsMemory() const {
78 if (CallInst *CI = dyn_cast<CallInst>(I))
79 return CI->onlyReadsMemory();
80 else
81 return cast<InvokeInst>(I)->onlyReadsMemory();
82}
Duncan Sands3353ed02007-12-18 09:59:50 +000083bool CallSite::doesNotThrow() const {
Duncan Sands8e4847e2007-12-16 15:51:49 +000084 if (CallInst *CI = dyn_cast<CallInst>(I))
Duncan Sands3353ed02007-12-18 09:59:50 +000085 return CI->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000086 else
Duncan Sands3353ed02007-12-18 09:59:50 +000087 return cast<InvokeInst>(I)->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000088}
Duncan Sandsaa31b922007-12-19 21:13:37 +000089void CallSite::setDoesNotThrow(bool doesNotThrow) {
90 if (CallInst *CI = dyn_cast<CallInst>(I))
91 CI->setDoesNotThrow(doesNotThrow);
92 else
93 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
94}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000095
Gordon Henriksen14a55692007-12-10 02:14:30 +000096//===----------------------------------------------------------------------===//
97// TerminatorInst Class
98//===----------------------------------------------------------------------===//
99
100// Out of line virtual method, so the vtable, etc has a home.
101TerminatorInst::~TerminatorInst() {
102}
103
104// Out of line virtual method, so the vtable, etc has a home.
105UnaryInstruction::~UnaryInstruction() {
106}
107
108
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000109//===----------------------------------------------------------------------===//
110// PHINode Class
111//===----------------------------------------------------------------------===//
112
113PHINode::PHINode(const PHINode &PN)
114 : Instruction(PN.getType(), Instruction::PHI,
115 new Use[PN.getNumOperands()], PN.getNumOperands()),
116 ReservedSpace(PN.getNumOperands()) {
117 Use *OL = OperandList;
118 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
119 OL[i].init(PN.getOperand(i), this);
120 OL[i+1].init(PN.getOperand(i+1), this);
121 }
122}
123
Gordon Henriksen14a55692007-12-10 02:14:30 +0000124PHINode::~PHINode() {
125 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000126}
127
128// removeIncomingValue - Remove an incoming value. This is useful if a
129// predecessor basic block is deleted.
130Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
131 unsigned NumOps = getNumOperands();
132 Use *OL = OperandList;
133 assert(Idx*2 < NumOps && "BB not in PHI node!");
134 Value *Removed = OL[Idx*2];
135
136 // Move everything after this operand down.
137 //
138 // FIXME: we could just swap with the end of the list, then erase. However,
139 // client might not expect this to happen. The code as it is thrashes the
140 // use/def lists, which is kinda lame.
141 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
142 OL[i-2] = OL[i];
143 OL[i-2+1] = OL[i+1];
144 }
145
146 // Nuke the last value.
147 OL[NumOps-2].set(0);
148 OL[NumOps-2+1].set(0);
149 NumOperands = NumOps-2;
150
151 // If the PHI node is dead, because it has zero entries, nuke it now.
152 if (NumOps == 2 && DeletePHIIfEmpty) {
153 // If anyone is using this PHI, make them use a dummy value instead...
154 replaceAllUsesWith(UndefValue::get(getType()));
155 eraseFromParent();
156 }
157 return Removed;
158}
159
160/// resizeOperands - resize operands - This adjusts the length of the operands
161/// list according to the following behavior:
162/// 1. If NumOps == 0, grow the operand list in response to a push_back style
163/// of operation. This grows the number of ops by 1.5 times.
164/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
165/// 3. If NumOps == NumOperands, trim the reserved space.
166///
167void PHINode::resizeOperands(unsigned NumOps) {
168 if (NumOps == 0) {
169 NumOps = (getNumOperands())*3/2;
170 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
171 } else if (NumOps*2 > NumOperands) {
172 // No resize needed.
173 if (ReservedSpace >= NumOps) return;
174 } else if (NumOps == NumOperands) {
175 if (ReservedSpace == NumOps) return;
176 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000177 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000178 }
179
180 ReservedSpace = NumOps;
181 Use *NewOps = new Use[NumOps];
182 Use *OldOps = OperandList;
183 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
184 NewOps[i].init(OldOps[i], this);
185 OldOps[i].set(0);
186 }
187 delete [] OldOps;
188 OperandList = NewOps;
189}
190
Nate Begemanb3923212005-08-04 23:24:19 +0000191/// hasConstantValue - If the specified PHI node always merges together the same
192/// value, return the value, otherwise return null.
193///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000194Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000195 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000196 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000197 if (getIncomingValue(0) != this) // not X = phi X
198 return getIncomingValue(0);
199 else
200 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000201 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000202
Nate Begemanb3923212005-08-04 23:24:19 +0000203 // Otherwise if all of the incoming values are the same for the PHI, replace
204 // the PHI node with the incoming value.
205 //
206 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000207 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000208 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000209 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000210 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000211 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000212 if (InVal && getIncomingValue(i) != InVal)
213 return 0; // Not the same, bail out.
214 else
215 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000216 }
Nate Begemanb3923212005-08-04 23:24:19 +0000217
218 // The only case that could cause InVal to be null is if we have a PHI node
219 // that only has entries for itself. In this case, there is no entry into the
220 // loop, so kill the PHI.
221 //
222 if (InVal == 0) InVal = UndefValue::get(getType());
223
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000224 // If we have a PHI node like phi(X, undef, X), where X is defined by some
225 // instruction, we cannot always return X as the result of the PHI node. Only
226 // do this if X is not an instruction (thus it must dominate the PHI block),
227 // or if the client is prepared to deal with this possibility.
228 if (HasUndefInput && !AllowNonDominatingInstruction)
229 if (Instruction *IV = dyn_cast<Instruction>(InVal))
230 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000231 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000232 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000233 return 0; // Cannot guarantee that InVal dominates this PHINode.
234
Nate Begemanb3923212005-08-04 23:24:19 +0000235 // All of the incoming values are the same, return the value now.
236 return InVal;
237}
238
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000239
240//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000241// CallInst Implementation
242//===----------------------------------------------------------------------===//
243
Gordon Henriksen14a55692007-12-10 02:14:30 +0000244CallInst::~CallInst() {
245 delete [] OperandList;
246 if (ParamAttrs)
247 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000248}
249
Chris Lattner054ba2c2007-02-13 00:58:44 +0000250void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000251 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000252 NumOperands = NumParams+1;
253 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000254 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000255
Misha Brukmanb1c93172005-04-21 23:48:37 +0000256 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000257 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000258 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000259
Chris Lattner054ba2c2007-02-13 00:58:44 +0000260 assert((NumParams == FTy->getNumParams() ||
261 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000262 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000263 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000264 assert((i >= FTy->getNumParams() ||
265 FTy->getParamType(i) == Params[i]->getType()) &&
266 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000267 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000268 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269}
270
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000271void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000272 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000273 NumOperands = 3;
274 Use *OL = OperandList = new Use[3];
275 OL[0].init(Func, this);
276 OL[1].init(Actual1, this);
277 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000278
279 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000280 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000281 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000282
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000283 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000284 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000286 assert((0 >= FTy->getNumParams() ||
287 FTy->getParamType(0) == Actual1->getType()) &&
288 "Calling a function with a bad signature!");
289 assert((1 >= FTy->getNumParams() ||
290 FTy->getParamType(1) == Actual2->getType()) &&
291 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000292}
293
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000294void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000295 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000296 NumOperands = 2;
297 Use *OL = OperandList = new Use[2];
298 OL[0].init(Func, this);
299 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000300
301 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000303 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000305 assert((FTy->getNumParams() == 1 ||
306 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000308 assert((0 == FTy->getNumParams() ||
309 FTy->getParamType(0) == Actual->getType()) &&
310 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311}
312
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000313void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000314 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000315 NumOperands = 1;
316 Use *OL = OperandList = new Use[1];
317 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000318
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000319 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000321 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000323 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324}
325
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000326CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000327 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000328 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
329 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000330 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000332 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000333}
334
335CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
336 BasicBlock *InsertAtEnd)
337 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
338 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000339 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000340 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000341 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000342}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000343CallInst::CallInst(Value *Func, const std::string &Name,
344 Instruction *InsertBefore)
345 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
346 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000347 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000349 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350}
351
352CallInst::CallInst(Value *Func, const std::string &Name,
353 BasicBlock *InsertAtEnd)
354 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
355 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000356 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000357 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000358 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000359}
360
Misha Brukmanb1c93172005-04-21 23:48:37 +0000361CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000362 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000363 CI.getNumOperands()),
364 ParamAttrs(0) {
365 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000366 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000367 Use *OL = OperandList;
368 Use *InOL = CI.OperandList;
369 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
370 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371}
372
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000373void CallInst::setParamAttrs(const ParamAttrsList *newAttrs) {
374 if (ParamAttrs == newAttrs)
375 return;
376
Reid Spencerc6a83842007-04-22 17:28:03 +0000377 if (ParamAttrs)
378 ParamAttrs->dropRef();
379
380 if (newAttrs)
381 newAttrs->addRef();
382
383 ParamAttrs = newAttrs;
384}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000385
Dale Johannesen89268bc2008-02-19 21:38:47 +0000386bool CallInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
387 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000388 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000389 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000390 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000391 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000392}
393
Dale Johanneseneabc5f32008-02-22 17:49:45 +0000394uint16_t CallInst::getParamAlignment(uint16_t i) const {
395 if (ParamAttrs && ParamAttrs->getParamAlignment(i))
396 return ParamAttrs->getParamAlignment(i);
397 if (const Function *F = getCalledFunction())
398 return F->getParamAlignment(i);
399 return 0;
400}
401
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000402/// @brief Determine if the call does not access memory.
403bool CallInst::doesNotAccessMemory() const {
404 return paramHasAttr(0, ParamAttr::ReadNone);
405}
406
407/// @brief Determine if the call does not access or only reads memory.
408bool CallInst::onlyReadsMemory() const {
409 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
410}
411
412/// @brief Determine if the call cannot return.
413bool CallInst::doesNotReturn() const {
414 return paramHasAttr(0, ParamAttr::NoReturn);
415}
416
417/// @brief Determine if the call cannot unwind.
418bool CallInst::doesNotThrow() const {
419 return paramHasAttr(0, ParamAttr::NoUnwind);
420}
421
Devang Patel9d9178592008-03-03 21:46:28 +0000422/// @brief Determine if the call returns a structure through first
423/// pointer argument.
424bool CallInst::hasStructRetAttr() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000425 // Be friendly and also check the callee.
426 return paramHasAttr(1, ParamAttr::StructRet);
427}
428
Evan Cheng09dde602008-01-12 18:57:32 +0000429/// @brief Determine if any call argument is an aggregate passed by value.
430bool CallInst::hasByValArgument() const {
Duncan Sandsa59f3962008-01-21 11:27:55 +0000431 if (ParamAttrs && ParamAttrs->hasAttrSomewhere(ParamAttr::ByVal))
432 return true;
433 // Be consistent with other methods and check the callee too.
434 if (const Function *F = getCalledFunction())
435 if (const ParamAttrsList *PAL = F->getParamAttrs())
436 return PAL->hasAttrSomewhere(ParamAttr::ByVal);
437 return false;
Evan Cheng09dde602008-01-12 18:57:32 +0000438}
439
Duncan Sandsaa31b922007-12-19 21:13:37 +0000440void CallInst::setDoesNotThrow(bool doesNotThrow) {
441 const ParamAttrsList *PAL = getParamAttrs();
442 if (doesNotThrow)
443 PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind);
444 else
445 PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind);
446 setParamAttrs(PAL);
447}
448
Duncan Sands5208d1a2007-11-28 17:07:01 +0000449
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000450//===----------------------------------------------------------------------===//
451// InvokeInst Implementation
452//===----------------------------------------------------------------------===//
453
Gordon Henriksen14a55692007-12-10 02:14:30 +0000454InvokeInst::~InvokeInst() {
455 delete [] OperandList;
456 if (ParamAttrs)
457 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000458}
459
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000460void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000461 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000462 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000463 NumOperands = 3+NumArgs;
464 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000465 OL[0].init(Fn, this);
466 OL[1].init(IfNormal, this);
467 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000468 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000469 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000470 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000471
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000472 assert(((NumArgs == FTy->getNumParams()) ||
473 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000474 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000475
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000476 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000477 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000478 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000479 "Invoking a function with a bad signature!");
480
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000481 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000482 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000483}
484
Misha Brukmanb1c93172005-04-21 23:48:37 +0000485InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000486 : TerminatorInst(II.getType(), Instruction::Invoke,
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000487 new Use[II.getNumOperands()], II.getNumOperands()),
488 ParamAttrs(0) {
489 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000490 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000491 Use *OL = OperandList, *InOL = II.OperandList;
492 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
493 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000494}
495
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000496BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
497 return getSuccessor(idx);
498}
499unsigned InvokeInst::getNumSuccessorsV() const {
500 return getNumSuccessors();
501}
502void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
503 return setSuccessor(idx, B);
504}
505
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000506void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) {
507 if (ParamAttrs == newAttrs)
508 return;
509
Reid Spencerc6a83842007-04-22 17:28:03 +0000510 if (ParamAttrs)
511 ParamAttrs->dropRef();
512
513 if (newAttrs)
514 newAttrs->addRef();
515
516 ParamAttrs = newAttrs;
517}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000518
Dale Johannesen89268bc2008-02-19 21:38:47 +0000519bool InvokeInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
520 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000521 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000522 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000523 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000524 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000525}
526
Dale Johanneseneabc5f32008-02-22 17:49:45 +0000527uint16_t InvokeInst::getParamAlignment(uint16_t i) const {
528 if (ParamAttrs && ParamAttrs->getParamAlignment(i))
529 return ParamAttrs->getParamAlignment(i);
530 if (const Function *F = getCalledFunction())
531 return F->getParamAlignment(i);
532 return 0;
533}
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000534
535/// @brief Determine if the call does not access memory.
536bool InvokeInst::doesNotAccessMemory() const {
537 return paramHasAttr(0, ParamAttr::ReadNone);
538}
539
540/// @brief Determine if the call does not access or only reads memory.
541bool InvokeInst::onlyReadsMemory() const {
542 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
543}
544
545/// @brief Determine if the call cannot return.
546bool InvokeInst::doesNotReturn() const {
547 return paramHasAttr(0, ParamAttr::NoReturn);
548}
549
550/// @brief Determine if the call cannot unwind.
551bool InvokeInst::doesNotThrow() const {
552 return paramHasAttr(0, ParamAttr::NoUnwind);
553}
554
Duncan Sandsaa31b922007-12-19 21:13:37 +0000555void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
556 const ParamAttrsList *PAL = getParamAttrs();
557 if (doesNotThrow)
558 PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind);
559 else
560 PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind);
561 setParamAttrs(PAL);
562}
563
Devang Patel9d9178592008-03-03 21:46:28 +0000564/// @brief Determine if the invoke returns a structure through first
565/// pointer argument.
566bool InvokeInst::hasStructRetAttr() const {
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000567 // Be friendly and also check the callee.
568 return paramHasAttr(1, ParamAttr::StructRet);
569}
570
Duncan Sands5208d1a2007-11-28 17:07:01 +0000571
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000572//===----------------------------------------------------------------------===//
573// ReturnInst Implementation
574//===----------------------------------------------------------------------===//
575
Chris Lattner2195fc42007-02-24 00:55:48 +0000576ReturnInst::ReturnInst(const ReturnInst &RI)
577 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Devang Patelae682fb2008-02-26 17:56:20 +0000578 &RetVal, RI.getNumOperands()) {
Devang Patel59643e52008-02-23 00:35:18 +0000579 unsigned N = RI.getNumOperands();
Devang Patelae682fb2008-02-26 17:56:20 +0000580 if (N == 1)
581 RetVal.init(RI.RetVal, this);
582 else if (N) {
583 Use *OL = OperandList = new Use[N];
584 for (unsigned i = 0; i < N; ++i)
585 OL[i].init(RI.getOperand(i), this);
586 }
Chris Lattner2195fc42007-02-24 00:55:48 +0000587}
588
589ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Devang Patelae682fb2008-02-26 17:56:20 +0000590 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000591 if (retVal)
592 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000593}
594ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Devang Patelae682fb2008-02-26 17:56:20 +0000595 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000596 if (retVal)
597 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000598}
599ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Devang Patelae682fb2008-02-26 17:56:20 +0000600 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000601}
602
Devang Patela736c002008-02-26 19:38:17 +0000603ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
604 Instruction *InsertBefore)
605 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N, InsertBefore) {
606 if (N != 0)
607 init(retVals, N);
608}
609ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
610 BasicBlock *InsertAtEnd)
611 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N, InsertAtEnd) {
612 if (N != 0)
613 init(retVals, N);
614}
615ReturnInst::ReturnInst(Value * const* retVals, unsigned N)
616 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N) {
617 if (N != 0)
618 init(retVals, N);
619}
620
Devang Patel060e79c2008-02-26 19:15:26 +0000621void ReturnInst::init(Value * const* retVals, unsigned N) {
Devang Patelc38eb522008-02-26 18:49:29 +0000622 assert (N > 0 && "Invalid operands numbers in ReturnInst init");
Devang Patel59643e52008-02-23 00:35:18 +0000623
Devang Patelc38eb522008-02-26 18:49:29 +0000624 NumOperands = N;
Devang Patel59643e52008-02-23 00:35:18 +0000625 if (NumOperands == 1) {
Devang Patel060e79c2008-02-26 19:15:26 +0000626 Value *V = *retVals;
Devang Patel59643e52008-02-23 00:35:18 +0000627 if (V->getType() == Type::VoidTy)
628 return;
Devang Patel060e79c2008-02-26 19:15:26 +0000629 RetVal.init(V, this);
Devang Patelae682fb2008-02-26 17:56:20 +0000630 return;
Devang Patel59643e52008-02-23 00:35:18 +0000631 }
632
633 Use *OL = OperandList = new Use[NumOperands];
634 for (unsigned i = 0; i < NumOperands; ++i) {
Devang Patel060e79c2008-02-26 19:15:26 +0000635 Value *V = *retVals++;
Devang Patel59643e52008-02-23 00:35:18 +0000636 assert(!isa<BasicBlock>(V) &&
637 "Cannot return basic block. Probably using the incorrect ctor");
Devang Patel060e79c2008-02-26 19:15:26 +0000638 OL[i].init(V, this);
Devang Patel59643e52008-02-23 00:35:18 +0000639 }
640}
641
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000642unsigned ReturnInst::getNumSuccessorsV() const {
643 return getNumSuccessors();
644}
645
Devang Patelae682fb2008-02-26 17:56:20 +0000646/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
647/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000649 assert(0 && "ReturnInst has no successors!");
650}
651
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000652BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
653 assert(0 && "ReturnInst has no successors!");
654 abort();
655 return 0;
656}
657
Devang Patel59643e52008-02-23 00:35:18 +0000658ReturnInst::~ReturnInst() {
Devang Patelae682fb2008-02-26 17:56:20 +0000659 if (NumOperands > 1)
Devang Patel59643e52008-02-23 00:35:18 +0000660 delete [] OperandList;
661}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000662
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000663//===----------------------------------------------------------------------===//
664// UnwindInst Implementation
665//===----------------------------------------------------------------------===//
666
Chris Lattner2195fc42007-02-24 00:55:48 +0000667UnwindInst::UnwindInst(Instruction *InsertBefore)
668 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
669}
670UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
671 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
672}
673
674
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000675unsigned UnwindInst::getNumSuccessorsV() const {
676 return getNumSuccessors();
677}
678
679void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000680 assert(0 && "UnwindInst has no successors!");
681}
682
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000683BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
684 assert(0 && "UnwindInst has no successors!");
685 abort();
686 return 0;
687}
688
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000689//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000690// UnreachableInst Implementation
691//===----------------------------------------------------------------------===//
692
Chris Lattner2195fc42007-02-24 00:55:48 +0000693UnreachableInst::UnreachableInst(Instruction *InsertBefore)
694 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
695}
696UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
697 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
698}
699
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000700unsigned UnreachableInst::getNumSuccessorsV() const {
701 return getNumSuccessors();
702}
703
704void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
705 assert(0 && "UnwindInst has no successors!");
706}
707
708BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
709 assert(0 && "UnwindInst has no successors!");
710 abort();
711 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000712}
713
714//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000715// BranchInst Implementation
716//===----------------------------------------------------------------------===//
717
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000718void BranchInst::AssertOK() {
719 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000720 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000722}
723
Chris Lattner2195fc42007-02-24 00:55:48 +0000724BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
725 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
726 assert(IfTrue != 0 && "Branch destination may not be null!");
727 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
728}
729BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
730 Instruction *InsertBefore)
731: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
732 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
733 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
734 Ops[2].init(Cond, this);
735#ifndef NDEBUG
736 AssertOK();
737#endif
738}
739
740BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
741 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
742 assert(IfTrue != 0 && "Branch destination may not be null!");
743 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
744}
745
746BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
747 BasicBlock *InsertAtEnd)
748 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
749 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
750 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
751 Ops[2].init(Cond, this);
752#ifndef NDEBUG
753 AssertOK();
754#endif
755}
756
757
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000758BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000759 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000760 OperandList[0].init(BI.getOperand(0), this);
761 if (BI.getNumOperands() != 1) {
762 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
763 OperandList[1].init(BI.getOperand(1), this);
764 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000765 }
766}
767
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000768BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
769 return getSuccessor(idx);
770}
771unsigned BranchInst::getNumSuccessorsV() const {
772 return getNumSuccessors();
773}
774void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
775 setSuccessor(idx, B);
776}
777
778
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000779//===----------------------------------------------------------------------===//
780// AllocationInst Implementation
781//===----------------------------------------------------------------------===//
782
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000783static Value *getAISize(Value *Amt) {
784 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000785 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000786 else {
787 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000788 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000789 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000790 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000791 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000792 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000793}
794
Misha Brukmanb1c93172005-04-21 23:48:37 +0000795AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000796 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000797 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000798 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000799 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000800 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000801 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000802 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803}
804
Misha Brukmanb1c93172005-04-21 23:48:37 +0000805AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000806 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000807 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000808 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000809 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000810 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000811 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000812 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000813}
814
Gordon Henriksen14a55692007-12-10 02:14:30 +0000815// Out of line virtual method, so the vtable, etc has a home.
816AllocationInst::~AllocationInst() {
817}
818
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000819bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000820 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
821 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000822 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000823}
824
825const Type *AllocationInst::getAllocatedType() const {
826 return getType()->getElementType();
827}
828
829AllocaInst::AllocaInst(const AllocaInst &AI)
830 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000831 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000832}
833
834MallocInst::MallocInst(const MallocInst &MI)
835 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000836 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000837}
838
839//===----------------------------------------------------------------------===//
840// FreeInst Implementation
841//===----------------------------------------------------------------------===//
842
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000843void FreeInst::AssertOK() {
844 assert(isa<PointerType>(getOperand(0)->getType()) &&
845 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000846}
847
848FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000849 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000850 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000851}
852
853FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000854 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000855 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000856}
857
858
859//===----------------------------------------------------------------------===//
860// LoadInst Implementation
861//===----------------------------------------------------------------------===//
862
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000863void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000864 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000865 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000866}
867
868LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000869 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000870 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000871 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000872 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000873 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000874 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000875}
876
877LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000878 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000879 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000880 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000881 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000882 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000883 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000884}
885
886LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
887 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000888 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000889 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000890 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000891 setAlignment(0);
892 AssertOK();
893 setName(Name);
894}
895
896LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
897 unsigned Align, Instruction *InsertBef)
898 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
899 Load, Ptr, InsertBef) {
900 setVolatile(isVolatile);
901 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000902 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000903 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000904}
905
Dan Gohman68659282007-07-18 20:51:11 +0000906LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
907 unsigned Align, BasicBlock *InsertAE)
908 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
909 Load, Ptr, InsertAE) {
910 setVolatile(isVolatile);
911 setAlignment(Align);
912 AssertOK();
913 setName(Name);
914}
915
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000916LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
917 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000918 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000919 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000920 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000921 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000922 AssertOK();
923 setName(Name);
924}
925
926
927
928LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000929 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
930 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000931 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000932 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000933 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000934 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000935}
936
937LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000938 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
939 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000940 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000941 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000942 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000943 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000944}
945
946LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
947 Instruction *InsertBef)
948: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000949 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000950 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000951 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000952 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000953 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000954}
955
956LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
957 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000958 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
959 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000960 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000961 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000962 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000963 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000964}
965
Christopher Lamb84485702007-04-22 19:24:39 +0000966void LoadInst::setAlignment(unsigned Align) {
967 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
968 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
969}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000970
971//===----------------------------------------------------------------------===//
972// StoreInst Implementation
973//===----------------------------------------------------------------------===//
974
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000975void StoreInst::AssertOK() {
976 assert(isa<PointerType>(getOperand(1)->getType()) &&
977 "Ptr must have pointer type!");
978 assert(getOperand(0)->getType() ==
979 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000980 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000981}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000982
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000983
984StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000985 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000986 Ops[0].init(val, this);
987 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000988 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000989 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000990 AssertOK();
991}
992
993StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000994 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000995 Ops[0].init(val, this);
996 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000997 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000998 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000999 AssertOK();
1000}
1001
Misha Brukmanb1c93172005-04-21 23:48:37 +00001002StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001003 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001004 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001005 Ops[0].init(val, this);
1006 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +00001007 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001008 setAlignment(0);
1009 AssertOK();
1010}
1011
1012StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1013 unsigned Align, Instruction *InsertBefore)
1014 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
1015 Ops[0].init(val, this);
1016 Ops[1].init(addr, this);
1017 setVolatile(isVolatile);
1018 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001019 AssertOK();
1020}
1021
Misha Brukmanb1c93172005-04-21 23:48:37 +00001022StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001023 unsigned Align, BasicBlock *InsertAtEnd)
1024 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
1025 Ops[0].init(val, this);
1026 Ops[1].init(addr, this);
1027 setVolatile(isVolatile);
1028 setAlignment(Align);
1029 AssertOK();
1030}
1031
1032StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001033 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001034 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001035 Ops[0].init(val, this);
1036 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +00001037 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001038 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001039 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001040}
1041
Christopher Lamb84485702007-04-22 19:24:39 +00001042void StoreInst::setAlignment(unsigned Align) {
1043 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1044 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1045}
1046
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001047//===----------------------------------------------------------------------===//
1048// GetElementPtrInst Implementation
1049//===----------------------------------------------------------------------===//
1050
Christopher Lambedf07882007-12-17 01:12:55 +00001051static unsigned retrieveAddrSpace(const Value *Val) {
1052 return cast<PointerType>(Val->getType())->getAddressSpace();
1053}
1054
Chris Lattner79807c3d2007-01-31 19:47:18 +00001055void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
1056 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001057 Use *OL = OperandList = new Use[NumOperands];
1058 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001059
Chris Lattner79807c3d2007-01-31 19:47:18 +00001060 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001061 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001062}
1063
Chris Lattner82981202005-05-03 05:43:30 +00001064void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
1065 NumOperands = 2;
1066 Use *OL = OperandList = new Use[2];
1067 OL[0].init(Ptr, this);
1068 OL[1].init(Idx, this);
1069}
1070
Chris Lattner82981202005-05-03 05:43:30 +00001071GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1072 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001073 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001074 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001075 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +00001076 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001077 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001078}
1079
1080GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1081 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001082 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001083 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001084 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +00001085 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001086 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001087}
1088
Gordon Henriksen14a55692007-12-10 02:14:30 +00001089GetElementPtrInst::~GetElementPtrInst() {
1090 delete[] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001091}
1092
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001093// getIndexedType - Returns the type of the element that would be loaded with
1094// a load instruction with the specified parameters.
1095//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001096// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001097// pointer type.
1098//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001099const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001100 Value* const *Idxs,
1101 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001102 bool AllowCompositeLeaf) {
1103 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
1104
1105 // Handle the special case of the empty set index set...
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001106 if (NumIdx == 0) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001107 if (AllowCompositeLeaf ||
1108 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
1109 return cast<PointerType>(Ptr)->getElementType();
1110 else
1111 return 0;
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001112 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001113
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001114 unsigned CurIdx = 0;
1115 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001116 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001117 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
1118 return 0; // Can't load a whole structure or array!?!?
1119 }
1120
Chris Lattner302116a2007-01-31 04:40:28 +00001121 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001122 if (isa<PointerType>(CT) && CurIdx != 1)
1123 return 0; // Can only index into pointer types at the first index!
1124 if (!CT->indexValid(Index)) return 0;
1125 Ptr = CT->getTypeAtIndex(Index);
1126
1127 // If the new type forwards to another type, then it is in the middle
1128 // of being refined to another type (and hence, may have dropped all
1129 // references to what it was using before). So, use the new forwarded
1130 // type.
1131 if (const Type * Ty = Ptr->getForwardedType()) {
1132 Ptr = Ty;
1133 }
1134 }
Chris Lattner302116a2007-01-31 04:40:28 +00001135 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001136}
1137
Chris Lattner82981202005-05-03 05:43:30 +00001138const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1139 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1140 if (!PTy) return 0; // Type isn't a pointer type!
1141
1142 // Check the pointer index.
1143 if (!PTy->indexValid(Idx)) return 0;
1144
Chris Lattnerc2233332005-05-03 16:44:45 +00001145 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001146}
1147
Chris Lattner45f15572007-04-14 00:12:57 +00001148
1149/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1150/// zeros. If so, the result pointer and the first operand have the same
1151/// value, just potentially different types.
1152bool GetElementPtrInst::hasAllZeroIndices() const {
1153 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1154 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1155 if (!CI->isZero()) return false;
1156 } else {
1157 return false;
1158 }
1159 }
1160 return true;
1161}
1162
Chris Lattner27058292007-04-27 20:35:56 +00001163/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1164/// constant integers. If so, the result pointer and the first operand have
1165/// a constant offset between them.
1166bool GetElementPtrInst::hasAllConstantIndices() const {
1167 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1168 if (!isa<ConstantInt>(getOperand(i)))
1169 return false;
1170 }
1171 return true;
1172}
1173
Chris Lattner45f15572007-04-14 00:12:57 +00001174
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001175//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001176// ExtractElementInst Implementation
1177//===----------------------------------------------------------------------===//
1178
1179ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001180 const std::string &Name,
1181 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001182 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001183 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001184 assert(isValidOperands(Val, Index) &&
1185 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001186 Ops[0].init(Val, this);
1187 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001188 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001189}
1190
Chris Lattner65511ff2006-10-05 06:24:58 +00001191ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1192 const std::string &Name,
1193 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001194 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001195 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001196 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001197 assert(isValidOperands(Val, Index) &&
1198 "Invalid extractelement instruction operands!");
1199 Ops[0].init(Val, this);
1200 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001201 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001202}
1203
1204
Robert Bocchino23004482006-01-10 19:05:34 +00001205ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001206 const std::string &Name,
1207 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001208 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001209 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001210 assert(isValidOperands(Val, Index) &&
1211 "Invalid extractelement instruction operands!");
1212
Robert Bocchino23004482006-01-10 19:05:34 +00001213 Ops[0].init(Val, this);
1214 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001215 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001216}
1217
Chris Lattner65511ff2006-10-05 06:24:58 +00001218ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1219 const std::string &Name,
1220 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001221 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001222 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001223 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001224 assert(isValidOperands(Val, Index) &&
1225 "Invalid extractelement instruction operands!");
1226
1227 Ops[0].init(Val, this);
1228 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001229 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001230}
1231
1232
Chris Lattner54865b32006-04-08 04:05:48 +00001233bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001234 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001235 return false;
1236 return true;
1237}
1238
1239
Robert Bocchino23004482006-01-10 19:05:34 +00001240//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001241// InsertElementInst Implementation
1242//===----------------------------------------------------------------------===//
1243
Chris Lattner0875d942006-04-14 22:20:32 +00001244InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1245 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1246 Ops[0].init(IE.Ops[0], this);
1247 Ops[1].init(IE.Ops[1], this);
1248 Ops[2].init(IE.Ops[2], this);
1249}
Chris Lattner54865b32006-04-08 04:05:48 +00001250InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001251 const std::string &Name,
1252 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001253 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001254 assert(isValidOperands(Vec, Elt, Index) &&
1255 "Invalid insertelement instruction operands!");
1256 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001257 Ops[1].init(Elt, this);
1258 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001259 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001260}
1261
Chris Lattner65511ff2006-10-05 06:24:58 +00001262InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1263 const std::string &Name,
1264 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001265 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001266 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001267 assert(isValidOperands(Vec, Elt, Index) &&
1268 "Invalid insertelement instruction operands!");
1269 Ops[0].init(Vec, this);
1270 Ops[1].init(Elt, this);
1271 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001272 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001273}
1274
1275
Chris Lattner54865b32006-04-08 04:05:48 +00001276InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001277 const std::string &Name,
1278 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001279 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001280 assert(isValidOperands(Vec, Elt, Index) &&
1281 "Invalid insertelement instruction operands!");
1282
1283 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001284 Ops[1].init(Elt, this);
1285 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001286 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001287}
1288
Chris Lattner65511ff2006-10-05 06:24:58 +00001289InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1290 const std::string &Name,
1291 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001292: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001293 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001294 assert(isValidOperands(Vec, Elt, Index) &&
1295 "Invalid insertelement instruction operands!");
1296
1297 Ops[0].init(Vec, this);
1298 Ops[1].init(Elt, this);
1299 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001300 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001301}
1302
Chris Lattner54865b32006-04-08 04:05:48 +00001303bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1304 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001305 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001306 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001307
Reid Spencerd84d35b2007-02-15 02:26:10 +00001308 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001309 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001310
Reid Spencer8d9336d2006-12-31 05:26:44 +00001311 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001312 return false; // Third operand of insertelement must be uint.
1313 return true;
1314}
1315
1316
Robert Bocchinoca27f032006-01-17 20:07:22 +00001317//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001318// ShuffleVectorInst Implementation
1319//===----------------------------------------------------------------------===//
1320
Chris Lattner0875d942006-04-14 22:20:32 +00001321ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1322 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1323 Ops[0].init(SV.Ops[0], this);
1324 Ops[1].init(SV.Ops[1], this);
1325 Ops[2].init(SV.Ops[2], this);
1326}
1327
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001328ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1329 const std::string &Name,
1330 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001331 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001332 assert(isValidOperands(V1, V2, Mask) &&
1333 "Invalid shuffle vector instruction operands!");
1334 Ops[0].init(V1, this);
1335 Ops[1].init(V2, this);
1336 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001337 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001338}
1339
1340ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1341 const std::string &Name,
1342 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001343 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001344 assert(isValidOperands(V1, V2, Mask) &&
1345 "Invalid shuffle vector instruction operands!");
1346
1347 Ops[0].init(V1, this);
1348 Ops[1].init(V2, this);
1349 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001350 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001351}
1352
1353bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1354 const Value *Mask) {
Chris Lattnerf724e342008-03-02 05:28:33 +00001355 if (!isa<VectorType>(V1->getType()) ||
1356 V1->getType() != V2->getType())
1357 return false;
1358
1359 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1360 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1361 MaskTy->getElementType() != Type::Int32Ty ||
1362 MaskTy->getNumElements() !=
1363 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001364 return false;
1365 return true;
1366}
1367
Chris Lattnerf724e342008-03-02 05:28:33 +00001368/// getMaskValue - Return the index from the shuffle mask for the specified
1369/// output result. This is either -1 if the element is undef or a number less
1370/// than 2*numelements.
1371int ShuffleVectorInst::getMaskValue(unsigned i) const {
1372 const Constant *Mask = cast<Constant>(getOperand(2));
1373 if (isa<UndefValue>(Mask)) return -1;
1374 if (isa<ConstantAggregateZero>(Mask)) return 0;
1375 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1376 assert(i < MaskCV->getNumOperands() && "Index out of range");
1377
1378 if (isa<UndefValue>(MaskCV->getOperand(i)))
1379 return -1;
1380 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1381}
1382
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001383
1384//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001385// BinaryOperator Class
1386//===----------------------------------------------------------------------===//
1387
Chris Lattner2195fc42007-02-24 00:55:48 +00001388BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1389 const Type *Ty, const std::string &Name,
1390 Instruction *InsertBefore)
1391 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1392 Ops[0].init(S1, this);
1393 Ops[1].init(S2, this);
1394 init(iType);
1395 setName(Name);
1396}
1397
1398BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1399 const Type *Ty, const std::string &Name,
1400 BasicBlock *InsertAtEnd)
1401 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1402 Ops[0].init(S1, this);
1403 Ops[1].init(S2, this);
1404 init(iType);
1405 setName(Name);
1406}
1407
1408
1409void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001410 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001411 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001412 assert(LHS->getType() == RHS->getType() &&
1413 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001414#ifndef NDEBUG
1415 switch (iType) {
1416 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001417 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001418 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001419 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001420 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001421 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001422 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001423 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001424 case UDiv:
1425 case SDiv:
1426 assert(getType() == LHS->getType() &&
1427 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001428 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1429 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001430 "Incorrect operand type (not integer) for S/UDIV");
1431 break;
1432 case FDiv:
1433 assert(getType() == LHS->getType() &&
1434 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001435 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1436 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001437 && "Incorrect operand type (not floating point) for FDIV");
1438 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001439 case URem:
1440 case SRem:
1441 assert(getType() == LHS->getType() &&
1442 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001443 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1444 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001445 "Incorrect operand type (not integer) for S/UREM");
1446 break;
1447 case FRem:
1448 assert(getType() == LHS->getType() &&
1449 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001450 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1451 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001452 && "Incorrect operand type (not floating point) for FREM");
1453 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001454 case Shl:
1455 case LShr:
1456 case AShr:
1457 assert(getType() == LHS->getType() &&
1458 "Shift operation should return same type as operands!");
1459 assert(getType()->isInteger() &&
1460 "Shift operation requires integer operands");
1461 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001462 case And: case Or:
1463 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001464 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001465 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001466 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001467 (isa<VectorType>(getType()) &&
1468 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001469 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001470 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001471 default:
1472 break;
1473 }
1474#endif
1475}
1476
1477BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001478 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001479 Instruction *InsertBefore) {
1480 assert(S1->getType() == S2->getType() &&
1481 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001482 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001483}
1484
1485BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001486 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001487 BasicBlock *InsertAtEnd) {
1488 BinaryOperator *Res = create(Op, S1, S2, Name);
1489 InsertAtEnd->getInstList().push_back(Res);
1490 return Res;
1491}
1492
1493BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1494 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001495 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1496 return new BinaryOperator(Instruction::Sub,
1497 zero, Op,
1498 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001499}
1500
1501BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1502 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001503 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1504 return new BinaryOperator(Instruction::Sub,
1505 zero, Op,
1506 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001507}
1508
1509BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1510 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001511 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001512 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001513 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001514 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001515 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001516 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001517 }
1518
1519 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001520 Op->getType(), Name, InsertBefore);
1521}
1522
1523BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1524 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001525 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001526 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001527 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001528 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001529 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001530 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001531 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001532 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001533 }
1534
1535 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001536 Op->getType(), Name, InsertAtEnd);
1537}
1538
1539
1540// isConstantAllOnes - Helper function for several functions below
1541static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001542 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1543 return CI->isAllOnesValue();
1544 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1545 return CV->isAllOnesValue();
1546 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001547}
1548
1549bool BinaryOperator::isNeg(const Value *V) {
1550 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1551 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001552 return Bop->getOperand(0) ==
1553 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001554 return false;
1555}
1556
1557bool BinaryOperator::isNot(const Value *V) {
1558 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1559 return (Bop->getOpcode() == Instruction::Xor &&
1560 (isConstantAllOnes(Bop->getOperand(1)) ||
1561 isConstantAllOnes(Bop->getOperand(0))));
1562 return false;
1563}
1564
Chris Lattner2c7d1772005-04-24 07:28:37 +00001565Value *BinaryOperator::getNegArgument(Value *BinOp) {
1566 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1567 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001568}
1569
Chris Lattner2c7d1772005-04-24 07:28:37 +00001570const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1571 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001572}
1573
Chris Lattner2c7d1772005-04-24 07:28:37 +00001574Value *BinaryOperator::getNotArgument(Value *BinOp) {
1575 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1576 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1577 Value *Op0 = BO->getOperand(0);
1578 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001579 if (isConstantAllOnes(Op0)) return Op1;
1580
1581 assert(isConstantAllOnes(Op1));
1582 return Op0;
1583}
1584
Chris Lattner2c7d1772005-04-24 07:28:37 +00001585const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1586 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001587}
1588
1589
1590// swapOperands - Exchange the two operands to this instruction. This
1591// instruction is safe to use on any binary instruction and does not
1592// modify the semantics of the instruction. If the instruction is
1593// order dependent (SetLT f.e.) the opcode is changed.
1594//
1595bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001596 if (!isCommutative())
1597 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001598 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001599 return false;
1600}
1601
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001602//===----------------------------------------------------------------------===//
1603// CastInst Class
1604//===----------------------------------------------------------------------===//
1605
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001606// Just determine if this cast only deals with integral->integral conversion.
1607bool CastInst::isIntegerCast() const {
1608 switch (getOpcode()) {
1609 default: return false;
1610 case Instruction::ZExt:
1611 case Instruction::SExt:
1612 case Instruction::Trunc:
1613 return true;
1614 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001615 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001616 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001617}
1618
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001619bool CastInst::isLosslessCast() const {
1620 // Only BitCast can be lossless, exit fast if we're not BitCast
1621 if (getOpcode() != Instruction::BitCast)
1622 return false;
1623
1624 // Identity cast is always lossless
1625 const Type* SrcTy = getOperand(0)->getType();
1626 const Type* DstTy = getType();
1627 if (SrcTy == DstTy)
1628 return true;
1629
Reid Spencer8d9336d2006-12-31 05:26:44 +00001630 // Pointer to pointer is always lossless.
1631 if (isa<PointerType>(SrcTy))
1632 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001633 return false; // Other types have no identity values
1634}
1635
1636/// This function determines if the CastInst does not require any bits to be
1637/// changed in order to effect the cast. Essentially, it identifies cases where
1638/// no code gen is necessary for the cast, hence the name no-op cast. For
1639/// example, the following are all no-op casts:
1640/// # bitcast uint %X, int
1641/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001642/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001643/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1644/// @brief Determine if a cast is a no-op.
1645bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1646 switch (getOpcode()) {
1647 default:
1648 assert(!"Invalid CastOp");
1649 case Instruction::Trunc:
1650 case Instruction::ZExt:
1651 case Instruction::SExt:
1652 case Instruction::FPTrunc:
1653 case Instruction::FPExt:
1654 case Instruction::UIToFP:
1655 case Instruction::SIToFP:
1656 case Instruction::FPToUI:
1657 case Instruction::FPToSI:
1658 return false; // These always modify bits
1659 case Instruction::BitCast:
1660 return true; // BitCast never modifies bits.
1661 case Instruction::PtrToInt:
1662 return IntPtrTy->getPrimitiveSizeInBits() ==
1663 getType()->getPrimitiveSizeInBits();
1664 case Instruction::IntToPtr:
1665 return IntPtrTy->getPrimitiveSizeInBits() ==
1666 getOperand(0)->getType()->getPrimitiveSizeInBits();
1667 }
1668}
1669
1670/// This function determines if a pair of casts can be eliminated and what
1671/// opcode should be used in the elimination. This assumes that there are two
1672/// instructions like this:
1673/// * %F = firstOpcode SrcTy %x to MidTy
1674/// * %S = secondOpcode MidTy %F to DstTy
1675/// The function returns a resultOpcode so these two casts can be replaced with:
1676/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1677/// If no such cast is permited, the function returns 0.
1678unsigned CastInst::isEliminableCastPair(
1679 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1680 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1681{
1682 // Define the 144 possibilities for these two cast instructions. The values
1683 // in this matrix determine what to do in a given situation and select the
1684 // case in the switch below. The rows correspond to firstOp, the columns
1685 // correspond to secondOp. In looking at the table below, keep in mind
1686 // the following cast properties:
1687 //
1688 // Size Compare Source Destination
1689 // Operator Src ? Size Type Sign Type Sign
1690 // -------- ------------ ------------------- ---------------------
1691 // TRUNC > Integer Any Integral Any
1692 // ZEXT < Integral Unsigned Integer Any
1693 // SEXT < Integral Signed Integer Any
1694 // FPTOUI n/a FloatPt n/a Integral Unsigned
1695 // FPTOSI n/a FloatPt n/a Integral Signed
1696 // UITOFP n/a Integral Unsigned FloatPt n/a
1697 // SITOFP n/a Integral Signed FloatPt n/a
1698 // FPTRUNC > FloatPt n/a FloatPt n/a
1699 // FPEXT < FloatPt n/a FloatPt n/a
1700 // PTRTOINT n/a Pointer n/a Integral Unsigned
1701 // INTTOPTR n/a Integral Unsigned Pointer n/a
1702 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001703 //
1704 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1705 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1706 // into "fptoui double to ulong", but this loses information about the range
1707 // of the produced value (we no longer know the top-part is all zeros).
1708 // Further this conversion is often much more expensive for typical hardware,
1709 // and causes issues when building libgcc. We disallow fptosi+sext for the
1710 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001711 const unsigned numCastOps =
1712 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1713 static const uint8_t CastResults[numCastOps][numCastOps] = {
1714 // T F F U S F F P I B -+
1715 // R Z S P P I I T P 2 N T |
1716 // U E E 2 2 2 2 R E I T C +- secondOp
1717 // N X X U S F F N X N 2 V |
1718 // C T T I I P P C T T P T -+
1719 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1720 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1721 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001722 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1723 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001724 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1725 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1726 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1727 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1728 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1729 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1730 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1731 };
1732
1733 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1734 [secondOp-Instruction::CastOpsBegin];
1735 switch (ElimCase) {
1736 case 0:
1737 // categorically disallowed
1738 return 0;
1739 case 1:
1740 // allowed, use first cast's opcode
1741 return firstOp;
1742 case 2:
1743 // allowed, use second cast's opcode
1744 return secondOp;
1745 case 3:
1746 // no-op cast in second op implies firstOp as long as the DestTy
1747 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001748 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001749 return firstOp;
1750 return 0;
1751 case 4:
1752 // no-op cast in second op implies firstOp as long as the DestTy
1753 // is floating point
1754 if (DstTy->isFloatingPoint())
1755 return firstOp;
1756 return 0;
1757 case 5:
1758 // no-op cast in first op implies secondOp as long as the SrcTy
1759 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001760 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001761 return secondOp;
1762 return 0;
1763 case 6:
1764 // no-op cast in first op implies secondOp as long as the SrcTy
1765 // is a floating point
1766 if (SrcTy->isFloatingPoint())
1767 return secondOp;
1768 return 0;
1769 case 7: {
1770 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1771 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1772 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1773 if (MidSize >= PtrSize)
1774 return Instruction::BitCast;
1775 return 0;
1776 }
1777 case 8: {
1778 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1779 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1780 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1781 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1782 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1783 if (SrcSize == DstSize)
1784 return Instruction::BitCast;
1785 else if (SrcSize < DstSize)
1786 return firstOp;
1787 return secondOp;
1788 }
1789 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1790 return Instruction::ZExt;
1791 case 10:
1792 // fpext followed by ftrunc is allowed if the bit size returned to is
1793 // the same as the original, in which case its just a bitcast
1794 if (SrcTy == DstTy)
1795 return Instruction::BitCast;
1796 return 0; // If the types are not the same we can't eliminate it.
1797 case 11:
1798 // bitcast followed by ptrtoint is allowed as long as the bitcast
1799 // is a pointer to pointer cast.
1800 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1801 return secondOp;
1802 return 0;
1803 case 12:
1804 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1805 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1806 return firstOp;
1807 return 0;
1808 case 13: {
1809 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1810 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1811 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1812 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1813 if (SrcSize <= PtrSize && SrcSize == DstSize)
1814 return Instruction::BitCast;
1815 return 0;
1816 }
1817 case 99:
1818 // cast combination can't happen (error in input). This is for all cases
1819 // where the MidTy is not the same for the two cast instructions.
1820 assert(!"Invalid Cast Combination");
1821 return 0;
1822 default:
1823 assert(!"Error in CastResults table!!!");
1824 return 0;
1825 }
1826 return 0;
1827}
1828
1829CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1830 const std::string &Name, Instruction *InsertBefore) {
1831 // Construct and return the appropriate CastInst subclass
1832 switch (op) {
1833 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1834 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1835 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1836 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1837 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1838 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1839 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1840 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1841 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1842 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1843 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1844 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1845 default:
1846 assert(!"Invalid opcode provided");
1847 }
1848 return 0;
1849}
1850
1851CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1852 const std::string &Name, BasicBlock *InsertAtEnd) {
1853 // Construct and return the appropriate CastInst subclass
1854 switch (op) {
1855 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1856 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1857 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1858 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1859 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1860 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1861 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1862 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1863 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1864 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1865 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1866 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1867 default:
1868 assert(!"Invalid opcode provided");
1869 }
1870 return 0;
1871}
1872
Reid Spencer5c140882006-12-04 20:17:56 +00001873CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1874 const std::string &Name,
1875 Instruction *InsertBefore) {
1876 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1877 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1878 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1879}
1880
1881CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1882 const std::string &Name,
1883 BasicBlock *InsertAtEnd) {
1884 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1885 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1886 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1887}
1888
1889CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1890 const std::string &Name,
1891 Instruction *InsertBefore) {
1892 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1893 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1894 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1895}
1896
1897CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1898 const std::string &Name,
1899 BasicBlock *InsertAtEnd) {
1900 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1901 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1902 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1903}
1904
1905CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1906 const std::string &Name,
1907 Instruction *InsertBefore) {
1908 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1909 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1910 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1911}
1912
1913CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1914 const std::string &Name,
1915 BasicBlock *InsertAtEnd) {
1916 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1917 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1918 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1919}
1920
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001921CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1922 const std::string &Name,
1923 BasicBlock *InsertAtEnd) {
1924 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001925 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001926 "Invalid cast");
1927
Chris Lattner03c49532007-01-15 02:27:26 +00001928 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001929 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1930 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1931}
1932
1933/// @brief Create a BitCast or a PtrToInt cast instruction
1934CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1935 const std::string &Name,
1936 Instruction *InsertBefore) {
1937 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001938 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001939 "Invalid cast");
1940
Chris Lattner03c49532007-01-15 02:27:26 +00001941 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001942 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1943 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1944}
1945
Reid Spencer7e933472006-12-12 00:49:44 +00001946CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1947 bool isSigned, const std::string &Name,
1948 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001949 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001950 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1951 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1952 Instruction::CastOps opcode =
1953 (SrcBits == DstBits ? Instruction::BitCast :
1954 (SrcBits > DstBits ? Instruction::Trunc :
1955 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1956 return create(opcode, C, Ty, Name, InsertBefore);
1957}
1958
1959CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1960 bool isSigned, const std::string &Name,
1961 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001962 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001963 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1964 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1965 Instruction::CastOps opcode =
1966 (SrcBits == DstBits ? Instruction::BitCast :
1967 (SrcBits > DstBits ? Instruction::Trunc :
1968 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1969 return create(opcode, C, Ty, Name, InsertAtEnd);
1970}
1971
1972CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1973 const std::string &Name,
1974 Instruction *InsertBefore) {
1975 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1976 "Invalid cast");
1977 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1978 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1979 Instruction::CastOps opcode =
1980 (SrcBits == DstBits ? Instruction::BitCast :
1981 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1982 return create(opcode, C, Ty, Name, InsertBefore);
1983}
1984
1985CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1986 const std::string &Name,
1987 BasicBlock *InsertAtEnd) {
1988 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1989 "Invalid cast");
1990 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1991 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1992 Instruction::CastOps opcode =
1993 (SrcBits == DstBits ? Instruction::BitCast :
1994 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1995 return create(opcode, C, Ty, Name, InsertAtEnd);
1996}
1997
Duncan Sands55e50902008-01-06 10:12:28 +00001998// Check whether it is valid to call getCastOpcode for these types.
1999// This routine must be kept in sync with getCastOpcode.
2000bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2001 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2002 return false;
2003
2004 if (SrcTy == DestTy)
2005 return true;
2006
2007 // Get the bit sizes, we'll need these
2008 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2009 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2010
2011 // Run through the possibilities ...
2012 if (DestTy->isInteger()) { // Casting to integral
2013 if (SrcTy->isInteger()) { // Casting from integral
2014 return true;
2015 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2016 return true;
2017 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2018 // Casting from vector
2019 return DestBits == PTy->getBitWidth();
2020 } else { // Casting from something else
2021 return isa<PointerType>(SrcTy);
2022 }
2023 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2024 if (SrcTy->isInteger()) { // Casting from integral
2025 return true;
2026 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2027 return true;
2028 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2029 // Casting from vector
2030 return DestBits == PTy->getBitWidth();
2031 } else { // Casting from something else
2032 return false;
2033 }
2034 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2035 // Casting to vector
2036 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2037 // Casting from vector
2038 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
2039 } else { // Casting from something else
2040 return DestPTy->getBitWidth() == SrcBits;
2041 }
2042 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2043 if (isa<PointerType>(SrcTy)) { // Casting from pointer
2044 return true;
2045 } else if (SrcTy->isInteger()) { // Casting from integral
2046 return true;
2047 } else { // Casting from something else
2048 return false;
2049 }
2050 } else { // Casting to something else
2051 return false;
2052 }
2053}
2054
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002055// Provide a way to get a "cast" where the cast opcode is inferred from the
2056// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002057// logic in the castIsValid function below. This axiom should hold:
2058// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2059// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002060// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002061// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002062Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002063CastInst::getCastOpcode(
2064 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002065 // Get the bit sizes, we'll need these
2066 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002067 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2068 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002069
Duncan Sands55e50902008-01-06 10:12:28 +00002070 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2071 "Only first class types are castable!");
2072
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002073 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002074 if (DestTy->isInteger()) { // Casting to integral
2075 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002076 if (DestBits < SrcBits)
2077 return Trunc; // int -> smaller int
2078 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002079 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002080 return SExt; // signed -> SEXT
2081 else
2082 return ZExt; // unsigned -> ZEXT
2083 } else {
2084 return BitCast; // Same size, No-op cast
2085 }
2086 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002087 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002088 return FPToSI; // FP -> sint
2089 else
2090 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002091 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002092 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002093 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002094 return BitCast; // Same size, no-op cast
2095 } else {
2096 assert(isa<PointerType>(SrcTy) &&
2097 "Casting from a value that is not first-class type");
2098 return PtrToInt; // ptr -> int
2099 }
2100 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002101 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002102 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002103 return SIToFP; // sint -> FP
2104 else
2105 return UIToFP; // uint -> FP
2106 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2107 if (DestBits < SrcBits) {
2108 return FPTrunc; // FP -> smaller FP
2109 } else if (DestBits > SrcBits) {
2110 return FPExt; // FP -> larger FP
2111 } else {
2112 return BitCast; // same size, no-op cast
2113 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002114 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002115 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002116 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002117 return BitCast; // same size, no-op cast
2118 } else {
2119 assert(0 && "Casting pointer or non-first class to float");
2120 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002121 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2122 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002123 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002124 "Casting vector to vector of different widths");
2125 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002126 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002127 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002128 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002129 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002130 }
2131 } else if (isa<PointerType>(DestTy)) {
2132 if (isa<PointerType>(SrcTy)) {
2133 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002134 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002135 return IntToPtr; // int -> ptr
2136 } else {
2137 assert(!"Casting pointer to other than pointer or int");
2138 }
2139 } else {
2140 assert(!"Casting to type that is not first-class");
2141 }
2142
2143 // If we fall through to here we probably hit an assertion cast above
2144 // and assertions are not turned on. Anything we return is an error, so
2145 // BitCast is as good a choice as any.
2146 return BitCast;
2147}
2148
2149//===----------------------------------------------------------------------===//
2150// CastInst SubClass Constructors
2151//===----------------------------------------------------------------------===//
2152
2153/// Check that the construction parameters for a CastInst are correct. This
2154/// could be broken out into the separate constructors but it is useful to have
2155/// it in one place and to eliminate the redundant code for getting the sizes
2156/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002157bool
2158CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002159
2160 // Check for type sanity on the arguments
2161 const Type *SrcTy = S->getType();
2162 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2163 return false;
2164
2165 // Get the size of the types in bits, we'll need this later
2166 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2167 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2168
2169 // Switch on the opcode provided
2170 switch (op) {
2171 default: return false; // This is an input error
2172 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002173 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002174 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002175 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002176 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002177 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002178 case Instruction::FPTrunc:
2179 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2180 SrcBitSize > DstBitSize;
2181 case Instruction::FPExt:
2182 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2183 SrcBitSize < DstBitSize;
2184 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002185 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002186 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2187 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2188 return SVTy->getElementType()->isInteger() &&
2189 DVTy->getElementType()->isFloatingPoint() &&
2190 SVTy->getNumElements() == DVTy->getNumElements();
2191 }
2192 }
Chris Lattner03c49532007-01-15 02:27:26 +00002193 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002194 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002195 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002196 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2197 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2198 return SVTy->getElementType()->isFloatingPoint() &&
2199 DVTy->getElementType()->isInteger() &&
2200 SVTy->getNumElements() == DVTy->getNumElements();
2201 }
2202 }
Chris Lattner03c49532007-01-15 02:27:26 +00002203 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002204 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002205 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002206 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002207 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002208 case Instruction::BitCast:
2209 // BitCast implies a no-op cast of type only. No bits change.
2210 // However, you can't cast pointers to anything but pointers.
2211 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2212 return false;
2213
Duncan Sands55e50902008-01-06 10:12:28 +00002214 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002215 // these cases, the cast is okay if the source and destination bit widths
2216 // are identical.
2217 return SrcBitSize == DstBitSize;
2218 }
2219}
2220
2221TruncInst::TruncInst(
2222 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2223) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002224 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002225}
2226
2227TruncInst::TruncInst(
2228 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2229) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002230 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002231}
2232
2233ZExtInst::ZExtInst(
2234 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2235) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002236 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237}
2238
2239ZExtInst::ZExtInst(
2240 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2241) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002242 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002243}
2244SExtInst::SExtInst(
2245 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2246) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002247 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002248}
2249
Jeff Cohencc08c832006-12-02 02:22:01 +00002250SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002251 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2252) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002253 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002254}
2255
2256FPTruncInst::FPTruncInst(
2257 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2258) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002259 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002260}
2261
2262FPTruncInst::FPTruncInst(
2263 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2264) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002265 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002266}
2267
2268FPExtInst::FPExtInst(
2269 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2270) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002271 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002272}
2273
2274FPExtInst::FPExtInst(
2275 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2276) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002277 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002278}
2279
2280UIToFPInst::UIToFPInst(
2281 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2282) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002283 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002284}
2285
2286UIToFPInst::UIToFPInst(
2287 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2288) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002289 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002290}
2291
2292SIToFPInst::SIToFPInst(
2293 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2294) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002295 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002296}
2297
2298SIToFPInst::SIToFPInst(
2299 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2300) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002301 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002302}
2303
2304FPToUIInst::FPToUIInst(
2305 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2306) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002307 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002308}
2309
2310FPToUIInst::FPToUIInst(
2311 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2312) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002313 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002314}
2315
2316FPToSIInst::FPToSIInst(
2317 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2318) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002319 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002320}
2321
2322FPToSIInst::FPToSIInst(
2323 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2324) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002325 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002326}
2327
2328PtrToIntInst::PtrToIntInst(
2329 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2330) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002331 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002332}
2333
2334PtrToIntInst::PtrToIntInst(
2335 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2336) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002337 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002338}
2339
2340IntToPtrInst::IntToPtrInst(
2341 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2342) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002343 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344}
2345
2346IntToPtrInst::IntToPtrInst(
2347 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2348) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002349 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002350}
2351
2352BitCastInst::BitCastInst(
2353 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2354) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002355 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002356}
2357
2358BitCastInst::BitCastInst(
2359 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2360) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002361 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002363
2364//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002365// CmpInst Classes
2366//===----------------------------------------------------------------------===//
2367
2368CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2369 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002370 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002371 Ops[0].init(LHS, this);
2372 Ops[1].init(RHS, this);
2373 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002374 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002375 if (op == Instruction::ICmp) {
2376 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2377 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2378 "Invalid ICmp predicate value");
2379 const Type* Op0Ty = getOperand(0)->getType();
2380 const Type* Op1Ty = getOperand(1)->getType();
2381 assert(Op0Ty == Op1Ty &&
2382 "Both operands to ICmp instruction are not of the same type!");
2383 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002384 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002385 "Invalid operand types for ICmp instruction");
2386 return;
2387 }
2388 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2389 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2390 "Invalid FCmp predicate value");
2391 const Type* Op0Ty = getOperand(0)->getType();
2392 const Type* Op1Ty = getOperand(1)->getType();
2393 assert(Op0Ty == Op1Ty &&
2394 "Both operands to FCmp instruction are not of the same type!");
2395 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002396 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002397 "Invalid operand types for FCmp instruction");
2398}
2399
2400CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2401 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002402 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002403 Ops[0].init(LHS, this);
2404 Ops[1].init(RHS, this);
2405 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002406 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002407 if (op == Instruction::ICmp) {
2408 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2409 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2410 "Invalid ICmp predicate value");
2411
2412 const Type* Op0Ty = getOperand(0)->getType();
2413 const Type* Op1Ty = getOperand(1)->getType();
2414 assert(Op0Ty == Op1Ty &&
2415 "Both operands to ICmp instruction are not of the same type!");
2416 // Check that the operands are the right type
Anton Korobeynikov579f0712008-02-20 11:08:44 +00002417 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002418 "Invalid operand types for ICmp instruction");
2419 return;
2420 }
2421 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2422 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2423 "Invalid FCmp predicate value");
2424 const Type* Op0Ty = getOperand(0)->getType();
2425 const Type* Op1Ty = getOperand(1)->getType();
2426 assert(Op0Ty == Op1Ty &&
2427 "Both operands to FCmp instruction are not of the same type!");
2428 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002429 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002430 "Invalid operand types for FCmp instruction");
2431}
2432
2433CmpInst *
2434CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2435 const std::string &Name, Instruction *InsertBefore) {
2436 if (Op == Instruction::ICmp) {
2437 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2438 InsertBefore);
2439 }
2440 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2441 InsertBefore);
2442}
2443
2444CmpInst *
2445CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2446 const std::string &Name, BasicBlock *InsertAtEnd) {
2447 if (Op == Instruction::ICmp) {
2448 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2449 InsertAtEnd);
2450 }
2451 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2452 InsertAtEnd);
2453}
2454
2455void CmpInst::swapOperands() {
2456 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2457 IC->swapOperands();
2458 else
2459 cast<FCmpInst>(this)->swapOperands();
2460}
2461
2462bool CmpInst::isCommutative() {
2463 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2464 return IC->isCommutative();
2465 return cast<FCmpInst>(this)->isCommutative();
2466}
2467
2468bool CmpInst::isEquality() {
2469 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2470 return IC->isEquality();
2471 return cast<FCmpInst>(this)->isEquality();
2472}
2473
2474
2475ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2476 switch (pred) {
2477 default:
2478 assert(!"Unknown icmp predicate!");
2479 case ICMP_EQ: return ICMP_NE;
2480 case ICMP_NE: return ICMP_EQ;
2481 case ICMP_UGT: return ICMP_ULE;
2482 case ICMP_ULT: return ICMP_UGE;
2483 case ICMP_UGE: return ICMP_ULT;
2484 case ICMP_ULE: return ICMP_UGT;
2485 case ICMP_SGT: return ICMP_SLE;
2486 case ICMP_SLT: return ICMP_SGE;
2487 case ICMP_SGE: return ICMP_SLT;
2488 case ICMP_SLE: return ICMP_SGT;
2489 }
2490}
2491
2492ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2493 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002494 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002495 case ICMP_EQ: case ICMP_NE:
2496 return pred;
2497 case ICMP_SGT: return ICMP_SLT;
2498 case ICMP_SLT: return ICMP_SGT;
2499 case ICMP_SGE: return ICMP_SLE;
2500 case ICMP_SLE: return ICMP_SGE;
2501 case ICMP_UGT: return ICMP_ULT;
2502 case ICMP_ULT: return ICMP_UGT;
2503 case ICMP_UGE: return ICMP_ULE;
2504 case ICMP_ULE: return ICMP_UGE;
2505 }
2506}
2507
Reid Spencer266e42b2006-12-23 06:05:41 +00002508ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2509 switch (pred) {
2510 default: assert(! "Unknown icmp predicate!");
2511 case ICMP_EQ: case ICMP_NE:
2512 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2513 return pred;
2514 case ICMP_UGT: return ICMP_SGT;
2515 case ICMP_ULT: return ICMP_SLT;
2516 case ICMP_UGE: return ICMP_SGE;
2517 case ICMP_ULE: return ICMP_SLE;
2518 }
2519}
2520
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002521ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2522 switch (pred) {
2523 default: assert(! "Unknown icmp predicate!");
2524 case ICMP_EQ: case ICMP_NE:
2525 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2526 return pred;
2527 case ICMP_SGT: return ICMP_UGT;
2528 case ICMP_SLT: return ICMP_ULT;
2529 case ICMP_SGE: return ICMP_UGE;
2530 case ICMP_SLE: return ICMP_ULE;
2531 }
2532}
2533
Reid Spencer266e42b2006-12-23 06:05:41 +00002534bool ICmpInst::isSignedPredicate(Predicate pred) {
2535 switch (pred) {
2536 default: assert(! "Unknown icmp predicate!");
2537 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2538 return true;
2539 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2540 case ICMP_UGE: case ICMP_ULE:
2541 return false;
2542 }
2543}
2544
Reid Spencer0286bc12007-02-28 22:00:54 +00002545/// Initialize a set of values that all satisfy the condition with C.
2546///
2547ConstantRange
2548ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2549 APInt Lower(C);
2550 APInt Upper(C);
2551 uint32_t BitWidth = C.getBitWidth();
2552 switch (pred) {
2553 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2554 case ICmpInst::ICMP_EQ: Upper++; break;
2555 case ICmpInst::ICMP_NE: Lower++; break;
2556 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2557 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2558 case ICmpInst::ICMP_UGT:
2559 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2560 break;
2561 case ICmpInst::ICMP_SGT:
2562 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2563 break;
2564 case ICmpInst::ICMP_ULE:
2565 Lower = APInt::getMinValue(BitWidth); Upper++;
2566 break;
2567 case ICmpInst::ICMP_SLE:
2568 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2569 break;
2570 case ICmpInst::ICMP_UGE:
2571 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2572 break;
2573 case ICmpInst::ICMP_SGE:
2574 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2575 break;
2576 }
2577 return ConstantRange(Lower, Upper);
2578}
2579
Reid Spencerd9436b62006-11-20 01:22:35 +00002580FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2581 switch (pred) {
2582 default:
2583 assert(!"Unknown icmp predicate!");
2584 case FCMP_OEQ: return FCMP_UNE;
2585 case FCMP_ONE: return FCMP_UEQ;
2586 case FCMP_OGT: return FCMP_ULE;
2587 case FCMP_OLT: return FCMP_UGE;
2588 case FCMP_OGE: return FCMP_ULT;
2589 case FCMP_OLE: return FCMP_UGT;
2590 case FCMP_UEQ: return FCMP_ONE;
2591 case FCMP_UNE: return FCMP_OEQ;
2592 case FCMP_UGT: return FCMP_OLE;
2593 case FCMP_ULT: return FCMP_OGE;
2594 case FCMP_UGE: return FCMP_OLT;
2595 case FCMP_ULE: return FCMP_OGT;
2596 case FCMP_ORD: return FCMP_UNO;
2597 case FCMP_UNO: return FCMP_ORD;
2598 case FCMP_TRUE: return FCMP_FALSE;
2599 case FCMP_FALSE: return FCMP_TRUE;
2600 }
2601}
2602
2603FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2604 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002605 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002606 case FCMP_FALSE: case FCMP_TRUE:
2607 case FCMP_OEQ: case FCMP_ONE:
2608 case FCMP_UEQ: case FCMP_UNE:
2609 case FCMP_ORD: case FCMP_UNO:
2610 return pred;
2611 case FCMP_OGT: return FCMP_OLT;
2612 case FCMP_OLT: return FCMP_OGT;
2613 case FCMP_OGE: return FCMP_OLE;
2614 case FCMP_OLE: return FCMP_OGE;
2615 case FCMP_UGT: return FCMP_ULT;
2616 case FCMP_ULT: return FCMP_UGT;
2617 case FCMP_UGE: return FCMP_ULE;
2618 case FCMP_ULE: return FCMP_UGE;
2619 }
2620}
2621
Reid Spencer266e42b2006-12-23 06:05:41 +00002622bool CmpInst::isUnsigned(unsigned short predicate) {
2623 switch (predicate) {
2624 default: return false;
2625 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2626 case ICmpInst::ICMP_UGE: return true;
2627 }
2628}
2629
2630bool CmpInst::isSigned(unsigned short predicate){
2631 switch (predicate) {
2632 default: return false;
2633 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2634 case ICmpInst::ICMP_SGE: return true;
2635 }
2636}
2637
2638bool CmpInst::isOrdered(unsigned short predicate) {
2639 switch (predicate) {
2640 default: return false;
2641 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2642 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2643 case FCmpInst::FCMP_ORD: return true;
2644 }
2645}
2646
2647bool CmpInst::isUnordered(unsigned short predicate) {
2648 switch (predicate) {
2649 default: return false;
2650 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2651 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2652 case FCmpInst::FCMP_UNO: return true;
2653 }
2654}
2655
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002656//===----------------------------------------------------------------------===//
2657// SwitchInst Implementation
2658//===----------------------------------------------------------------------===//
2659
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002660void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002661 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002662 ReservedSpace = 2+NumCases*2;
2663 NumOperands = 2;
2664 OperandList = new Use[ReservedSpace];
2665
2666 OperandList[0].init(Value, this);
2667 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002668}
2669
Chris Lattner2195fc42007-02-24 00:55:48 +00002670/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2671/// switch on and a default destination. The number of additional cases can
2672/// be specified here to make memory allocation more efficient. This
2673/// constructor can also autoinsert before another instruction.
2674SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2675 Instruction *InsertBefore)
2676 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2677 init(Value, Default, NumCases);
2678}
2679
2680/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2681/// switch on and a default destination. The number of additional cases can
2682/// be specified here to make memory allocation more efficient. This
2683/// constructor also autoinserts at the end of the specified BasicBlock.
2684SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2685 BasicBlock *InsertAtEnd)
2686 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2687 init(Value, Default, NumCases);
2688}
2689
Misha Brukmanb1c93172005-04-21 23:48:37 +00002690SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002691 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2692 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002693 Use *OL = OperandList, *InOL = SI.OperandList;
2694 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2695 OL[i].init(InOL[i], this);
2696 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002697 }
2698}
2699
Gordon Henriksen14a55692007-12-10 02:14:30 +00002700SwitchInst::~SwitchInst() {
2701 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002702}
2703
2704
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002705/// addCase - Add an entry to the switch instruction...
2706///
Chris Lattner47ac1872005-02-24 05:32:09 +00002707void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002708 unsigned OpNo = NumOperands;
2709 if (OpNo+2 > ReservedSpace)
2710 resizeOperands(0); // Get more space!
2711 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002712 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002713 NumOperands = OpNo+2;
2714 OperandList[OpNo].init(OnVal, this);
2715 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002716}
2717
2718/// removeCase - This method removes the specified successor from the switch
2719/// instruction. Note that this cannot be used to remove the default
2720/// destination (successor #0).
2721///
2722void SwitchInst::removeCase(unsigned idx) {
2723 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002724 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2725
2726 unsigned NumOps = getNumOperands();
2727 Use *OL = OperandList;
2728
2729 // Move everything after this operand down.
2730 //
2731 // FIXME: we could just swap with the end of the list, then erase. However,
2732 // client might not expect this to happen. The code as it is thrashes the
2733 // use/def lists, which is kinda lame.
2734 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2735 OL[i-2] = OL[i];
2736 OL[i-2+1] = OL[i+1];
2737 }
2738
2739 // Nuke the last value.
2740 OL[NumOps-2].set(0);
2741 OL[NumOps-2+1].set(0);
2742 NumOperands = NumOps-2;
2743}
2744
2745/// resizeOperands - resize operands - This adjusts the length of the operands
2746/// list according to the following behavior:
2747/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2748/// of operation. This grows the number of ops by 1.5 times.
2749/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2750/// 3. If NumOps == NumOperands, trim the reserved space.
2751///
2752void SwitchInst::resizeOperands(unsigned NumOps) {
2753 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002754 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002755 } else if (NumOps*2 > NumOperands) {
2756 // No resize needed.
2757 if (ReservedSpace >= NumOps) return;
2758 } else if (NumOps == NumOperands) {
2759 if (ReservedSpace == NumOps) return;
2760 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002761 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002762 }
2763
2764 ReservedSpace = NumOps;
2765 Use *NewOps = new Use[NumOps];
2766 Use *OldOps = OperandList;
2767 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2768 NewOps[i].init(OldOps[i], this);
2769 OldOps[i].set(0);
2770 }
2771 delete [] OldOps;
2772 OperandList = NewOps;
2773}
2774
2775
2776BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2777 return getSuccessor(idx);
2778}
2779unsigned SwitchInst::getNumSuccessorsV() const {
2780 return getNumSuccessors();
2781}
2782void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2783 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002784}
Chris Lattnerf22be932004-10-15 23:52:53 +00002785
Devang Patel295711f2008-02-19 22:15:16 +00002786//===----------------------------------------------------------------------===//
2787// GetResultInst Implementation
2788//===----------------------------------------------------------------------===//
2789
Devang Patel666d4512008-02-20 19:10:47 +00002790GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002791 const std::string &Name,
2792 Instruction *InsertBef)
Devang Pateldcad9da2008-02-20 19:26:55 +00002793 : Instruction(cast<StructType>(Aggregate->getType())->getElementType(Index),
Devang Patel666d4512008-02-20 19:10:47 +00002794 GetResult, &Aggr, 1, InsertBef) {
2795 assert(isValidOperands(Aggregate, Index) && "Invalid GetResultInst operands!");
2796 Aggr.init(Aggregate, this);
2797 Idx = Index;
Devang Patel295711f2008-02-19 22:15:16 +00002798 setName(Name);
2799}
2800
Devang Patel666d4512008-02-20 19:10:47 +00002801bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2802 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002803 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002804
Devang Patelcf193b82008-02-20 19:39:41 +00002805 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2806 unsigned NumElements = STy->getNumElements();
2807 if (Index >= NumElements)
2808 return false;
2809
2810 // getresult aggregate value's element types are restricted to
2811 // avoid nested aggregates.
2812 for (unsigned i = 0; i < NumElements; ++i)
2813 if (!STy->getElementType(i)->isFirstClassType())
2814 return false;
2815
2816 // Otherwise, Aggregate is valid.
2817 return true;
2818 }
Devang Patel666d4512008-02-20 19:10:47 +00002819 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002820}
2821
Chris Lattnerf22be932004-10-15 23:52:53 +00002822// Define these methods here so vtables don't get emitted into every translation
2823// unit that uses these classes.
2824
2825GetElementPtrInst *GetElementPtrInst::clone() const {
2826 return new GetElementPtrInst(*this);
2827}
2828
2829BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002830 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002831}
2832
Chris Lattner0b490b02007-08-24 20:48:18 +00002833FCmpInst* FCmpInst::clone() const {
2834 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2835}
2836ICmpInst* ICmpInst::clone() const {
2837 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002838}
2839
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002840MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2841AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2842FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2843LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2844StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2845CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2846CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2847CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2848CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2849CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2850CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2851CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2852CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2853CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2854CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2855CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2856CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2857CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002858SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2859VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2860
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002861ExtractElementInst *ExtractElementInst::clone() const {
2862 return new ExtractElementInst(*this);
2863}
2864InsertElementInst *InsertElementInst::clone() const {
2865 return new InsertElementInst(*this);
2866}
2867ShuffleVectorInst *ShuffleVectorInst::clone() const {
2868 return new ShuffleVectorInst(*this);
2869}
Chris Lattnerf22be932004-10-15 23:52:53 +00002870PHINode *PHINode::clone() const { return new PHINode(*this); }
2871ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2872BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2873SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2874InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2875UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002876UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002877GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }