blob: 9a84b5400458d41c2c61e089ce74f15a21144462 [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
422/// @brief Determine if the call returns a structure.
423bool CallInst::isStructReturn() const {
424 // Be friendly and also check the callee.
425 return paramHasAttr(1, ParamAttr::StructRet);
426}
427
Evan Cheng09dde602008-01-12 18:57:32 +0000428/// @brief Determine if any call argument is an aggregate passed by value.
429bool CallInst::hasByValArgument() const {
Duncan Sandsa59f3962008-01-21 11:27:55 +0000430 if (ParamAttrs && ParamAttrs->hasAttrSomewhere(ParamAttr::ByVal))
431 return true;
432 // Be consistent with other methods and check the callee too.
433 if (const Function *F = getCalledFunction())
434 if (const ParamAttrsList *PAL = F->getParamAttrs())
435 return PAL->hasAttrSomewhere(ParamAttr::ByVal);
436 return false;
Evan Cheng09dde602008-01-12 18:57:32 +0000437}
438
Duncan Sandsaa31b922007-12-19 21:13:37 +0000439void CallInst::setDoesNotThrow(bool doesNotThrow) {
440 const ParamAttrsList *PAL = getParamAttrs();
441 if (doesNotThrow)
442 PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind);
443 else
444 PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind);
445 setParamAttrs(PAL);
446}
447
Duncan Sands5208d1a2007-11-28 17:07:01 +0000448
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000449//===----------------------------------------------------------------------===//
450// InvokeInst Implementation
451//===----------------------------------------------------------------------===//
452
Gordon Henriksen14a55692007-12-10 02:14:30 +0000453InvokeInst::~InvokeInst() {
454 delete [] OperandList;
455 if (ParamAttrs)
456 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000457}
458
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000459void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000460 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000461 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000462 NumOperands = 3+NumArgs;
463 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000464 OL[0].init(Fn, this);
465 OL[1].init(IfNormal, this);
466 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000467 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000468 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000469 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000470
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000471 assert(((NumArgs == FTy->getNumParams()) ||
472 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000473 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000474
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000475 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000476 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000477 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000478 "Invoking a function with a bad signature!");
479
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000480 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000481 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000482}
483
Misha Brukmanb1c93172005-04-21 23:48:37 +0000484InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000485 : TerminatorInst(II.getType(), Instruction::Invoke,
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000486 new Use[II.getNumOperands()], II.getNumOperands()),
487 ParamAttrs(0) {
488 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000489 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000490 Use *OL = OperandList, *InOL = II.OperandList;
491 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
492 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000493}
494
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000495BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
496 return getSuccessor(idx);
497}
498unsigned InvokeInst::getNumSuccessorsV() const {
499 return getNumSuccessors();
500}
501void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
502 return setSuccessor(idx, B);
503}
504
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000505void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) {
506 if (ParamAttrs == newAttrs)
507 return;
508
Reid Spencerc6a83842007-04-22 17:28:03 +0000509 if (ParamAttrs)
510 ParamAttrs->dropRef();
511
512 if (newAttrs)
513 newAttrs->addRef();
514
515 ParamAttrs = newAttrs;
516}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000517
Dale Johannesen89268bc2008-02-19 21:38:47 +0000518bool InvokeInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
519 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000520 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000521 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000522 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000523 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000524}
525
Dale Johanneseneabc5f32008-02-22 17:49:45 +0000526uint16_t InvokeInst::getParamAlignment(uint16_t i) const {
527 if (ParamAttrs && ParamAttrs->getParamAlignment(i))
528 return ParamAttrs->getParamAlignment(i);
529 if (const Function *F = getCalledFunction())
530 return F->getParamAlignment(i);
531 return 0;
532}
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000533
534/// @brief Determine if the call does not access memory.
535bool InvokeInst::doesNotAccessMemory() const {
536 return paramHasAttr(0, ParamAttr::ReadNone);
537}
538
539/// @brief Determine if the call does not access or only reads memory.
540bool InvokeInst::onlyReadsMemory() const {
541 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
542}
543
544/// @brief Determine if the call cannot return.
545bool InvokeInst::doesNotReturn() const {
546 return paramHasAttr(0, ParamAttr::NoReturn);
547}
548
549/// @brief Determine if the call cannot unwind.
550bool InvokeInst::doesNotThrow() const {
551 return paramHasAttr(0, ParamAttr::NoUnwind);
552}
553
Duncan Sandsaa31b922007-12-19 21:13:37 +0000554void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
555 const ParamAttrsList *PAL = getParamAttrs();
556 if (doesNotThrow)
557 PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind);
558 else
559 PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind);
560 setParamAttrs(PAL);
561}
562
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000563/// @brief Determine if the call returns a structure.
564bool InvokeInst::isStructReturn() const {
565 // Be friendly and also check the callee.
566 return paramHasAttr(1, ParamAttr::StructRet);
567}
568
Duncan Sands5208d1a2007-11-28 17:07:01 +0000569
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000570//===----------------------------------------------------------------------===//
571// ReturnInst Implementation
572//===----------------------------------------------------------------------===//
573
Chris Lattner2195fc42007-02-24 00:55:48 +0000574ReturnInst::ReturnInst(const ReturnInst &RI)
575 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Devang Patel59643e52008-02-23 00:35:18 +0000576 OperandList, RI.getNumOperands()) {
577 unsigned N = RI.getNumOperands();
578 Use *OL = OperandList = new Use[N];
579 for (unsigned i = 0; i < N; ++i)
580 OL[i].init(RI.getOperand(i), this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000581}
582
583ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Devang Patel59643e52008-02-23 00:35:18 +0000584 : TerminatorInst(Type::VoidTy, Instruction::Ret, OperandList, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000585 init(retVal);
586}
587ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Devang Patel59643e52008-02-23 00:35:18 +0000588 : TerminatorInst(Type::VoidTy, Instruction::Ret, OperandList, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000589 init(retVal);
590}
591ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Devang Patel59643e52008-02-23 00:35:18 +0000592 : TerminatorInst(Type::VoidTy, Instruction::Ret, OperandList, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000593}
594
Devang Patel767338c2008-02-26 00:12:13 +0000595ReturnInst::ReturnInst(const std::vector<Value *> &retVals, Instruction *InsertBefore)
Devang Patel59643e52008-02-23 00:35:18 +0000596 : TerminatorInst(Type::VoidTy, Instruction::Ret, OperandList, retVals.size(), InsertBefore) {
597 init(retVals);
598}
Devang Patel767338c2008-02-26 00:12:13 +0000599ReturnInst::ReturnInst(const std::vector<Value *> &retVals, BasicBlock *InsertAtEnd)
Devang Patel59643e52008-02-23 00:35:18 +0000600 : TerminatorInst(Type::VoidTy, Instruction::Ret, OperandList, retVals.size(), InsertAtEnd) {
601 init(retVals);
602}
Devang Patel767338c2008-02-26 00:12:13 +0000603ReturnInst::ReturnInst(const std::vector<Value *> &retVals)
Devang Patel59643e52008-02-23 00:35:18 +0000604 : TerminatorInst(Type::VoidTy, Instruction::Ret, OperandList, retVals.size()) {
605 init(retVals);
606}
Chris Lattner2195fc42007-02-24 00:55:48 +0000607
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000608void ReturnInst::init(Value *retVal) {
609 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000610 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000611 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000612 NumOperands = 1;
Devang Patel59643e52008-02-23 00:35:18 +0000613 Use *OL = OperandList = new Use[1];
614 OL[0].init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000615 }
616}
617
Devang Patel767338c2008-02-26 00:12:13 +0000618void ReturnInst::init(const std::vector<Value *> &retVals) {
Devang Patel59643e52008-02-23 00:35:18 +0000619 if (retVals.empty())
620 return;
621
622 NumOperands = retVals.size();
623 if (NumOperands == 1) {
624 Value *V = retVals[0];
625 if (V->getType() == Type::VoidTy)
626 return;
627 }
628
629 Use *OL = OperandList = new Use[NumOperands];
630 for (unsigned i = 0; i < NumOperands; ++i) {
631 Value *V = retVals[i];
632 assert(!isa<BasicBlock>(V) &&
633 "Cannot return basic block. Probably using the incorrect ctor");
634 OL[i].init(V, this);
635 }
636}
637
638Value *ReturnInst::getReturnValue(unsigned n) const {
639 if (NumOperands)
640 return OperandList[n];
641 else
642 return 0;
643}
644
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000645unsigned ReturnInst::getNumSuccessorsV() const {
646 return getNumSuccessors();
647}
648
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000649// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
650// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000651void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000652 assert(0 && "ReturnInst has no successors!");
653}
654
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
656 assert(0 && "ReturnInst has no successors!");
657 abort();
658 return 0;
659}
660
Devang Patel59643e52008-02-23 00:35:18 +0000661ReturnInst::~ReturnInst() {
662 if (NumOperands)
663 delete [] OperandList;
664}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000665
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000666//===----------------------------------------------------------------------===//
667// UnwindInst Implementation
668//===----------------------------------------------------------------------===//
669
Chris Lattner2195fc42007-02-24 00:55:48 +0000670UnwindInst::UnwindInst(Instruction *InsertBefore)
671 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
672}
673UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
674 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
675}
676
677
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000678unsigned UnwindInst::getNumSuccessorsV() const {
679 return getNumSuccessors();
680}
681
682void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000683 assert(0 && "UnwindInst has no successors!");
684}
685
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000686BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
687 assert(0 && "UnwindInst has no successors!");
688 abort();
689 return 0;
690}
691
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000692//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000693// UnreachableInst Implementation
694//===----------------------------------------------------------------------===//
695
Chris Lattner2195fc42007-02-24 00:55:48 +0000696UnreachableInst::UnreachableInst(Instruction *InsertBefore)
697 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
698}
699UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
700 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
701}
702
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000703unsigned UnreachableInst::getNumSuccessorsV() const {
704 return getNumSuccessors();
705}
706
707void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
708 assert(0 && "UnwindInst has no successors!");
709}
710
711BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
712 assert(0 && "UnwindInst has no successors!");
713 abort();
714 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000715}
716
717//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000718// BranchInst Implementation
719//===----------------------------------------------------------------------===//
720
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721void BranchInst::AssertOK() {
722 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000723 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000724 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725}
726
Chris Lattner2195fc42007-02-24 00:55:48 +0000727BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
728 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
729 assert(IfTrue != 0 && "Branch destination may not be null!");
730 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
731}
732BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
733 Instruction *InsertBefore)
734: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
735 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
736 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
737 Ops[2].init(Cond, this);
738#ifndef NDEBUG
739 AssertOK();
740#endif
741}
742
743BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
744 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
745 assert(IfTrue != 0 && "Branch destination may not be null!");
746 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
747}
748
749BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
750 BasicBlock *InsertAtEnd)
751 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
752 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
753 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
754 Ops[2].init(Cond, this);
755#ifndef NDEBUG
756 AssertOK();
757#endif
758}
759
760
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000761BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000762 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000763 OperandList[0].init(BI.getOperand(0), this);
764 if (BI.getNumOperands() != 1) {
765 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
766 OperandList[1].init(BI.getOperand(1), this);
767 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000768 }
769}
770
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000771BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
772 return getSuccessor(idx);
773}
774unsigned BranchInst::getNumSuccessorsV() const {
775 return getNumSuccessors();
776}
777void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
778 setSuccessor(idx, B);
779}
780
781
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000782//===----------------------------------------------------------------------===//
783// AllocationInst Implementation
784//===----------------------------------------------------------------------===//
785
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000786static Value *getAISize(Value *Amt) {
787 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000788 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000789 else {
790 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000791 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000792 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000793 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000794 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000795 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000796}
797
Misha Brukmanb1c93172005-04-21 23:48:37 +0000798AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000799 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000800 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000801 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000802 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000803 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000804 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000805 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000806}
807
Misha Brukmanb1c93172005-04-21 23:48:37 +0000808AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000809 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000810 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000811 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000812 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000813 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000814 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000815 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000816}
817
Gordon Henriksen14a55692007-12-10 02:14:30 +0000818// Out of line virtual method, so the vtable, etc has a home.
819AllocationInst::~AllocationInst() {
820}
821
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000822bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000823 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
824 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000825 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000826}
827
828const Type *AllocationInst::getAllocatedType() const {
829 return getType()->getElementType();
830}
831
832AllocaInst::AllocaInst(const AllocaInst &AI)
833 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000834 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000835}
836
837MallocInst::MallocInst(const MallocInst &MI)
838 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000839 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000840}
841
842//===----------------------------------------------------------------------===//
843// FreeInst Implementation
844//===----------------------------------------------------------------------===//
845
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000846void FreeInst::AssertOK() {
847 assert(isa<PointerType>(getOperand(0)->getType()) &&
848 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000849}
850
851FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000852 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000853 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000854}
855
856FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000857 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000858 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000859}
860
861
862//===----------------------------------------------------------------------===//
863// LoadInst Implementation
864//===----------------------------------------------------------------------===//
865
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000866void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000867 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000868 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000869}
870
871LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000872 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000873 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000874 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000875 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000876 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000877 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000878}
879
880LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000881 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000882 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000883 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000884 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000885 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000886 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000887}
888
889LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
890 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000891 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000892 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000893 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000894 setAlignment(0);
895 AssertOK();
896 setName(Name);
897}
898
899LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
900 unsigned Align, Instruction *InsertBef)
901 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
902 Load, Ptr, InsertBef) {
903 setVolatile(isVolatile);
904 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000905 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000906 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000907}
908
Dan Gohman68659282007-07-18 20:51:11 +0000909LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
910 unsigned Align, BasicBlock *InsertAE)
911 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
912 Load, Ptr, InsertAE) {
913 setVolatile(isVolatile);
914 setAlignment(Align);
915 AssertOK();
916 setName(Name);
917}
918
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000919LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
920 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000921 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000922 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000923 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000924 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000925 AssertOK();
926 setName(Name);
927}
928
929
930
931LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000932 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
933 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000934 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000935 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000936 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000937 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000938}
939
940LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000941 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
942 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000943 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000944 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000945 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000946 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000947}
948
949LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
950 Instruction *InsertBef)
951: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000952 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000953 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000954 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000955 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000956 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000957}
958
959LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
960 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000961 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
962 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000963 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000964 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000965 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000966 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000967}
968
Christopher Lamb84485702007-04-22 19:24:39 +0000969void LoadInst::setAlignment(unsigned Align) {
970 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
971 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
972}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000973
974//===----------------------------------------------------------------------===//
975// StoreInst Implementation
976//===----------------------------------------------------------------------===//
977
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000978void StoreInst::AssertOK() {
979 assert(isa<PointerType>(getOperand(1)->getType()) &&
980 "Ptr must have pointer type!");
981 assert(getOperand(0)->getType() ==
982 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000983 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000984}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000985
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000986
987StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000988 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000989 Ops[0].init(val, this);
990 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000991 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000992 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000993 AssertOK();
994}
995
996StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000997 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000998 Ops[0].init(val, this);
999 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +00001000 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001001 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001002 AssertOK();
1003}
1004
Misha Brukmanb1c93172005-04-21 23:48:37 +00001005StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001006 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001007 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001008 Ops[0].init(val, this);
1009 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +00001010 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001011 setAlignment(0);
1012 AssertOK();
1013}
1014
1015StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1016 unsigned Align, Instruction *InsertBefore)
1017 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
1018 Ops[0].init(val, this);
1019 Ops[1].init(addr, this);
1020 setVolatile(isVolatile);
1021 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001022 AssertOK();
1023}
1024
Misha Brukmanb1c93172005-04-21 23:48:37 +00001025StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001026 unsigned Align, BasicBlock *InsertAtEnd)
1027 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
1028 Ops[0].init(val, this);
1029 Ops[1].init(addr, this);
1030 setVolatile(isVolatile);
1031 setAlignment(Align);
1032 AssertOK();
1033}
1034
1035StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001036 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001037 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001038 Ops[0].init(val, this);
1039 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +00001040 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001041 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001042 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001043}
1044
Christopher Lamb84485702007-04-22 19:24:39 +00001045void StoreInst::setAlignment(unsigned Align) {
1046 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1047 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1048}
1049
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001050//===----------------------------------------------------------------------===//
1051// GetElementPtrInst Implementation
1052//===----------------------------------------------------------------------===//
1053
Christopher Lambedf07882007-12-17 01:12:55 +00001054static unsigned retrieveAddrSpace(const Value *Val) {
1055 return cast<PointerType>(Val->getType())->getAddressSpace();
1056}
1057
Chris Lattner79807c3d2007-01-31 19:47:18 +00001058void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
1059 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001060 Use *OL = OperandList = new Use[NumOperands];
1061 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001062
Chris Lattner79807c3d2007-01-31 19:47:18 +00001063 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001064 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001065}
1066
Chris Lattner82981202005-05-03 05:43:30 +00001067void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
1068 NumOperands = 2;
1069 Use *OL = OperandList = new Use[2];
1070 OL[0].init(Ptr, this);
1071 OL[1].init(Idx, this);
1072}
1073
Chris Lattner82981202005-05-03 05:43:30 +00001074GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1075 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001076 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001077 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001078 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +00001079 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001080 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001081}
1082
1083GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1084 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001085 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001086 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001087 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +00001088 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001089 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001090}
1091
Gordon Henriksen14a55692007-12-10 02:14:30 +00001092GetElementPtrInst::~GetElementPtrInst() {
1093 delete[] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001094}
1095
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001096// getIndexedType - Returns the type of the element that would be loaded with
1097// a load instruction with the specified parameters.
1098//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001099// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001100// pointer type.
1101//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001102const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001103 Value* const *Idxs,
1104 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001105 bool AllowCompositeLeaf) {
1106 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
1107
1108 // Handle the special case of the empty set index set...
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001109 if (NumIdx == 0) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001110 if (AllowCompositeLeaf ||
1111 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
1112 return cast<PointerType>(Ptr)->getElementType();
1113 else
1114 return 0;
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001115 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001116
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001117 unsigned CurIdx = 0;
1118 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001119 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001120 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
1121 return 0; // Can't load a whole structure or array!?!?
1122 }
1123
Chris Lattner302116a2007-01-31 04:40:28 +00001124 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001125 if (isa<PointerType>(CT) && CurIdx != 1)
1126 return 0; // Can only index into pointer types at the first index!
1127 if (!CT->indexValid(Index)) return 0;
1128 Ptr = CT->getTypeAtIndex(Index);
1129
1130 // If the new type forwards to another type, then it is in the middle
1131 // of being refined to another type (and hence, may have dropped all
1132 // references to what it was using before). So, use the new forwarded
1133 // type.
1134 if (const Type * Ty = Ptr->getForwardedType()) {
1135 Ptr = Ty;
1136 }
1137 }
Chris Lattner302116a2007-01-31 04:40:28 +00001138 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001139}
1140
Chris Lattner82981202005-05-03 05:43:30 +00001141const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1142 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1143 if (!PTy) return 0; // Type isn't a pointer type!
1144
1145 // Check the pointer index.
1146 if (!PTy->indexValid(Idx)) return 0;
1147
Chris Lattnerc2233332005-05-03 16:44:45 +00001148 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001149}
1150
Chris Lattner45f15572007-04-14 00:12:57 +00001151
1152/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1153/// zeros. If so, the result pointer and the first operand have the same
1154/// value, just potentially different types.
1155bool GetElementPtrInst::hasAllZeroIndices() const {
1156 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1157 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1158 if (!CI->isZero()) return false;
1159 } else {
1160 return false;
1161 }
1162 }
1163 return true;
1164}
1165
Chris Lattner27058292007-04-27 20:35:56 +00001166/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1167/// constant integers. If so, the result pointer and the first operand have
1168/// a constant offset between them.
1169bool GetElementPtrInst::hasAllConstantIndices() const {
1170 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1171 if (!isa<ConstantInt>(getOperand(i)))
1172 return false;
1173 }
1174 return true;
1175}
1176
Chris Lattner45f15572007-04-14 00:12:57 +00001177
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001178//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001179// ExtractElementInst Implementation
1180//===----------------------------------------------------------------------===//
1181
1182ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001183 const std::string &Name,
1184 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001185 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001186 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001187 assert(isValidOperands(Val, Index) &&
1188 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001189 Ops[0].init(Val, this);
1190 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001191 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001192}
1193
Chris Lattner65511ff2006-10-05 06:24:58 +00001194ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1195 const std::string &Name,
1196 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001197 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001198 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001199 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001200 assert(isValidOperands(Val, Index) &&
1201 "Invalid extractelement instruction operands!");
1202 Ops[0].init(Val, this);
1203 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001204 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001205}
1206
1207
Robert Bocchino23004482006-01-10 19:05:34 +00001208ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001209 const std::string &Name,
1210 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001211 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001212 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001213 assert(isValidOperands(Val, Index) &&
1214 "Invalid extractelement instruction operands!");
1215
Robert Bocchino23004482006-01-10 19:05:34 +00001216 Ops[0].init(Val, this);
1217 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001218 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001219}
1220
Chris Lattner65511ff2006-10-05 06:24:58 +00001221ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1222 const std::string &Name,
1223 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001224 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001225 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001226 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001227 assert(isValidOperands(Val, Index) &&
1228 "Invalid extractelement instruction operands!");
1229
1230 Ops[0].init(Val, this);
1231 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001232 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001233}
1234
1235
Chris Lattner54865b32006-04-08 04:05:48 +00001236bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001237 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001238 return false;
1239 return true;
1240}
1241
1242
Robert Bocchino23004482006-01-10 19:05:34 +00001243//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001244// InsertElementInst Implementation
1245//===----------------------------------------------------------------------===//
1246
Chris Lattner0875d942006-04-14 22:20:32 +00001247InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1248 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1249 Ops[0].init(IE.Ops[0], this);
1250 Ops[1].init(IE.Ops[1], this);
1251 Ops[2].init(IE.Ops[2], this);
1252}
Chris Lattner54865b32006-04-08 04:05:48 +00001253InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001254 const std::string &Name,
1255 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001256 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001257 assert(isValidOperands(Vec, Elt, Index) &&
1258 "Invalid insertelement instruction operands!");
1259 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001260 Ops[1].init(Elt, this);
1261 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001262 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001263}
1264
Chris Lattner65511ff2006-10-05 06:24:58 +00001265InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1266 const std::string &Name,
1267 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001268 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001269 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001270 assert(isValidOperands(Vec, Elt, Index) &&
1271 "Invalid insertelement instruction operands!");
1272 Ops[0].init(Vec, this);
1273 Ops[1].init(Elt, this);
1274 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001275 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001276}
1277
1278
Chris Lattner54865b32006-04-08 04:05:48 +00001279InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001280 const std::string &Name,
1281 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001282 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001283 assert(isValidOperands(Vec, Elt, Index) &&
1284 "Invalid insertelement instruction operands!");
1285
1286 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001287 Ops[1].init(Elt, this);
1288 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001289 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001290}
1291
Chris Lattner65511ff2006-10-05 06:24:58 +00001292InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1293 const std::string &Name,
1294 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001295: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001296 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001297 assert(isValidOperands(Vec, Elt, Index) &&
1298 "Invalid insertelement instruction operands!");
1299
1300 Ops[0].init(Vec, this);
1301 Ops[1].init(Elt, this);
1302 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001303 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001304}
1305
Chris Lattner54865b32006-04-08 04:05:48 +00001306bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1307 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001308 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001309 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001310
Reid Spencerd84d35b2007-02-15 02:26:10 +00001311 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001312 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001313
Reid Spencer8d9336d2006-12-31 05:26:44 +00001314 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001315 return false; // Third operand of insertelement must be uint.
1316 return true;
1317}
1318
1319
Robert Bocchinoca27f032006-01-17 20:07:22 +00001320//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001321// ShuffleVectorInst Implementation
1322//===----------------------------------------------------------------------===//
1323
Chris Lattner0875d942006-04-14 22:20:32 +00001324ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1325 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1326 Ops[0].init(SV.Ops[0], this);
1327 Ops[1].init(SV.Ops[1], this);
1328 Ops[2].init(SV.Ops[2], this);
1329}
1330
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001331ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1332 const std::string &Name,
1333 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001334 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001335 assert(isValidOperands(V1, V2, Mask) &&
1336 "Invalid shuffle vector instruction operands!");
1337 Ops[0].init(V1, this);
1338 Ops[1].init(V2, this);
1339 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001340 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001341}
1342
1343ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1344 const std::string &Name,
1345 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001346 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001347 assert(isValidOperands(V1, V2, Mask) &&
1348 "Invalid shuffle vector instruction operands!");
1349
1350 Ops[0].init(V1, this);
1351 Ops[1].init(V2, this);
1352 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001353 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001354}
1355
1356bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1357 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001358 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001359 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001360 if (!isa<VectorType>(Mask->getType()) ||
1361 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1362 cast<VectorType>(Mask->getType())->getNumElements() !=
1363 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001364 return false;
1365 return true;
1366}
1367
1368
1369//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001370// BinaryOperator Class
1371//===----------------------------------------------------------------------===//
1372
Chris Lattner2195fc42007-02-24 00:55:48 +00001373BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1374 const Type *Ty, const std::string &Name,
1375 Instruction *InsertBefore)
1376 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1377 Ops[0].init(S1, this);
1378 Ops[1].init(S2, this);
1379 init(iType);
1380 setName(Name);
1381}
1382
1383BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1384 const Type *Ty, const std::string &Name,
1385 BasicBlock *InsertAtEnd)
1386 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1387 Ops[0].init(S1, this);
1388 Ops[1].init(S2, this);
1389 init(iType);
1390 setName(Name);
1391}
1392
1393
1394void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001395 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001396 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001397 assert(LHS->getType() == RHS->getType() &&
1398 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001399#ifndef NDEBUG
1400 switch (iType) {
1401 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001402 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001403 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001404 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001405 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001406 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001407 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001408 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001409 case UDiv:
1410 case SDiv:
1411 assert(getType() == LHS->getType() &&
1412 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001413 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1414 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001415 "Incorrect operand type (not integer) for S/UDIV");
1416 break;
1417 case FDiv:
1418 assert(getType() == LHS->getType() &&
1419 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001420 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1421 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001422 && "Incorrect operand type (not floating point) for FDIV");
1423 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001424 case URem:
1425 case SRem:
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 Spencer7eb55b32006-11-02 01:53:59 +00001430 "Incorrect operand type (not integer) for S/UREM");
1431 break;
1432 case FRem:
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 Spencer7eb55b32006-11-02 01:53:59 +00001437 && "Incorrect operand type (not floating point) for FREM");
1438 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001439 case Shl:
1440 case LShr:
1441 case AShr:
1442 assert(getType() == LHS->getType() &&
1443 "Shift operation should return same type as operands!");
1444 assert(getType()->isInteger() &&
1445 "Shift operation requires integer operands");
1446 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001447 case And: case Or:
1448 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001449 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001450 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001451 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001452 (isa<VectorType>(getType()) &&
1453 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001454 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001455 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001456 default:
1457 break;
1458 }
1459#endif
1460}
1461
1462BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001463 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001464 Instruction *InsertBefore) {
1465 assert(S1->getType() == S2->getType() &&
1466 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001467 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001468}
1469
1470BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001471 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001472 BasicBlock *InsertAtEnd) {
1473 BinaryOperator *Res = create(Op, S1, S2, Name);
1474 InsertAtEnd->getInstList().push_back(Res);
1475 return Res;
1476}
1477
1478BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1479 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001480 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1481 return new BinaryOperator(Instruction::Sub,
1482 zero, Op,
1483 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001484}
1485
1486BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1487 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001488 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1489 return new BinaryOperator(Instruction::Sub,
1490 zero, Op,
1491 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001492}
1493
1494BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1495 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001496 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001497 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001498 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001499 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001500 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001501 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001502 }
1503
1504 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001505 Op->getType(), Name, InsertBefore);
1506}
1507
1508BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1509 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001510 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001511 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001512 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001513 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001514 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001515 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001516 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001517 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001518 }
1519
1520 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001521 Op->getType(), Name, InsertAtEnd);
1522}
1523
1524
1525// isConstantAllOnes - Helper function for several functions below
1526static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001527 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1528 return CI->isAllOnesValue();
1529 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1530 return CV->isAllOnesValue();
1531 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001532}
1533
1534bool BinaryOperator::isNeg(const Value *V) {
1535 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1536 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001537 return Bop->getOperand(0) ==
1538 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001539 return false;
1540}
1541
1542bool BinaryOperator::isNot(const Value *V) {
1543 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1544 return (Bop->getOpcode() == Instruction::Xor &&
1545 (isConstantAllOnes(Bop->getOperand(1)) ||
1546 isConstantAllOnes(Bop->getOperand(0))));
1547 return false;
1548}
1549
Chris Lattner2c7d1772005-04-24 07:28:37 +00001550Value *BinaryOperator::getNegArgument(Value *BinOp) {
1551 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1552 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001553}
1554
Chris Lattner2c7d1772005-04-24 07:28:37 +00001555const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1556 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001557}
1558
Chris Lattner2c7d1772005-04-24 07:28:37 +00001559Value *BinaryOperator::getNotArgument(Value *BinOp) {
1560 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1561 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1562 Value *Op0 = BO->getOperand(0);
1563 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001564 if (isConstantAllOnes(Op0)) return Op1;
1565
1566 assert(isConstantAllOnes(Op1));
1567 return Op0;
1568}
1569
Chris Lattner2c7d1772005-04-24 07:28:37 +00001570const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1571 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001572}
1573
1574
1575// swapOperands - Exchange the two operands to this instruction. This
1576// instruction is safe to use on any binary instruction and does not
1577// modify the semantics of the instruction. If the instruction is
1578// order dependent (SetLT f.e.) the opcode is changed.
1579//
1580bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001581 if (!isCommutative())
1582 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001583 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001584 return false;
1585}
1586
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001587//===----------------------------------------------------------------------===//
1588// CastInst Class
1589//===----------------------------------------------------------------------===//
1590
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001591// Just determine if this cast only deals with integral->integral conversion.
1592bool CastInst::isIntegerCast() const {
1593 switch (getOpcode()) {
1594 default: return false;
1595 case Instruction::ZExt:
1596 case Instruction::SExt:
1597 case Instruction::Trunc:
1598 return true;
1599 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001600 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001601 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001602}
1603
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001604bool CastInst::isLosslessCast() const {
1605 // Only BitCast can be lossless, exit fast if we're not BitCast
1606 if (getOpcode() != Instruction::BitCast)
1607 return false;
1608
1609 // Identity cast is always lossless
1610 const Type* SrcTy = getOperand(0)->getType();
1611 const Type* DstTy = getType();
1612 if (SrcTy == DstTy)
1613 return true;
1614
Reid Spencer8d9336d2006-12-31 05:26:44 +00001615 // Pointer to pointer is always lossless.
1616 if (isa<PointerType>(SrcTy))
1617 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001618 return false; // Other types have no identity values
1619}
1620
1621/// This function determines if the CastInst does not require any bits to be
1622/// changed in order to effect the cast. Essentially, it identifies cases where
1623/// no code gen is necessary for the cast, hence the name no-op cast. For
1624/// example, the following are all no-op casts:
1625/// # bitcast uint %X, int
1626/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001627/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001628/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1629/// @brief Determine if a cast is a no-op.
1630bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1631 switch (getOpcode()) {
1632 default:
1633 assert(!"Invalid CastOp");
1634 case Instruction::Trunc:
1635 case Instruction::ZExt:
1636 case Instruction::SExt:
1637 case Instruction::FPTrunc:
1638 case Instruction::FPExt:
1639 case Instruction::UIToFP:
1640 case Instruction::SIToFP:
1641 case Instruction::FPToUI:
1642 case Instruction::FPToSI:
1643 return false; // These always modify bits
1644 case Instruction::BitCast:
1645 return true; // BitCast never modifies bits.
1646 case Instruction::PtrToInt:
1647 return IntPtrTy->getPrimitiveSizeInBits() ==
1648 getType()->getPrimitiveSizeInBits();
1649 case Instruction::IntToPtr:
1650 return IntPtrTy->getPrimitiveSizeInBits() ==
1651 getOperand(0)->getType()->getPrimitiveSizeInBits();
1652 }
1653}
1654
1655/// This function determines if a pair of casts can be eliminated and what
1656/// opcode should be used in the elimination. This assumes that there are two
1657/// instructions like this:
1658/// * %F = firstOpcode SrcTy %x to MidTy
1659/// * %S = secondOpcode MidTy %F to DstTy
1660/// The function returns a resultOpcode so these two casts can be replaced with:
1661/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1662/// If no such cast is permited, the function returns 0.
1663unsigned CastInst::isEliminableCastPair(
1664 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1665 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1666{
1667 // Define the 144 possibilities for these two cast instructions. The values
1668 // in this matrix determine what to do in a given situation and select the
1669 // case in the switch below. The rows correspond to firstOp, the columns
1670 // correspond to secondOp. In looking at the table below, keep in mind
1671 // the following cast properties:
1672 //
1673 // Size Compare Source Destination
1674 // Operator Src ? Size Type Sign Type Sign
1675 // -------- ------------ ------------------- ---------------------
1676 // TRUNC > Integer Any Integral Any
1677 // ZEXT < Integral Unsigned Integer Any
1678 // SEXT < Integral Signed Integer Any
1679 // FPTOUI n/a FloatPt n/a Integral Unsigned
1680 // FPTOSI n/a FloatPt n/a Integral Signed
1681 // UITOFP n/a Integral Unsigned FloatPt n/a
1682 // SITOFP n/a Integral Signed FloatPt n/a
1683 // FPTRUNC > FloatPt n/a FloatPt n/a
1684 // FPEXT < FloatPt n/a FloatPt n/a
1685 // PTRTOINT n/a Pointer n/a Integral Unsigned
1686 // INTTOPTR n/a Integral Unsigned Pointer n/a
1687 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001688 //
1689 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1690 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1691 // into "fptoui double to ulong", but this loses information about the range
1692 // of the produced value (we no longer know the top-part is all zeros).
1693 // Further this conversion is often much more expensive for typical hardware,
1694 // and causes issues when building libgcc. We disallow fptosi+sext for the
1695 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001696 const unsigned numCastOps =
1697 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1698 static const uint8_t CastResults[numCastOps][numCastOps] = {
1699 // T F F U S F F P I B -+
1700 // R Z S P P I I T P 2 N T |
1701 // U E E 2 2 2 2 R E I T C +- secondOp
1702 // N X X U S F F N X N 2 V |
1703 // C T T I I P P C T T P T -+
1704 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1705 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1706 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001707 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1708 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001709 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1710 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1711 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1712 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1713 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1714 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1715 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1716 };
1717
1718 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1719 [secondOp-Instruction::CastOpsBegin];
1720 switch (ElimCase) {
1721 case 0:
1722 // categorically disallowed
1723 return 0;
1724 case 1:
1725 // allowed, use first cast's opcode
1726 return firstOp;
1727 case 2:
1728 // allowed, use second cast's opcode
1729 return secondOp;
1730 case 3:
1731 // no-op cast in second op implies firstOp as long as the DestTy
1732 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001733 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001734 return firstOp;
1735 return 0;
1736 case 4:
1737 // no-op cast in second op implies firstOp as long as the DestTy
1738 // is floating point
1739 if (DstTy->isFloatingPoint())
1740 return firstOp;
1741 return 0;
1742 case 5:
1743 // no-op cast in first op implies secondOp as long as the SrcTy
1744 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001745 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001746 return secondOp;
1747 return 0;
1748 case 6:
1749 // no-op cast in first op implies secondOp as long as the SrcTy
1750 // is a floating point
1751 if (SrcTy->isFloatingPoint())
1752 return secondOp;
1753 return 0;
1754 case 7: {
1755 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1756 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1757 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1758 if (MidSize >= PtrSize)
1759 return Instruction::BitCast;
1760 return 0;
1761 }
1762 case 8: {
1763 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1764 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1765 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1766 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1767 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1768 if (SrcSize == DstSize)
1769 return Instruction::BitCast;
1770 else if (SrcSize < DstSize)
1771 return firstOp;
1772 return secondOp;
1773 }
1774 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1775 return Instruction::ZExt;
1776 case 10:
1777 // fpext followed by ftrunc is allowed if the bit size returned to is
1778 // the same as the original, in which case its just a bitcast
1779 if (SrcTy == DstTy)
1780 return Instruction::BitCast;
1781 return 0; // If the types are not the same we can't eliminate it.
1782 case 11:
1783 // bitcast followed by ptrtoint is allowed as long as the bitcast
1784 // is a pointer to pointer cast.
1785 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1786 return secondOp;
1787 return 0;
1788 case 12:
1789 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1790 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1791 return firstOp;
1792 return 0;
1793 case 13: {
1794 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1795 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1796 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1797 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1798 if (SrcSize <= PtrSize && SrcSize == DstSize)
1799 return Instruction::BitCast;
1800 return 0;
1801 }
1802 case 99:
1803 // cast combination can't happen (error in input). This is for all cases
1804 // where the MidTy is not the same for the two cast instructions.
1805 assert(!"Invalid Cast Combination");
1806 return 0;
1807 default:
1808 assert(!"Error in CastResults table!!!");
1809 return 0;
1810 }
1811 return 0;
1812}
1813
1814CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1815 const std::string &Name, Instruction *InsertBefore) {
1816 // Construct and return the appropriate CastInst subclass
1817 switch (op) {
1818 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1819 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1820 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1821 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1822 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1823 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1824 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1825 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1826 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1827 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1828 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1829 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1830 default:
1831 assert(!"Invalid opcode provided");
1832 }
1833 return 0;
1834}
1835
1836CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1837 const std::string &Name, BasicBlock *InsertAtEnd) {
1838 // Construct and return the appropriate CastInst subclass
1839 switch (op) {
1840 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1841 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1842 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1843 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1844 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1845 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1846 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1847 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1848 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1849 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1850 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1851 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1852 default:
1853 assert(!"Invalid opcode provided");
1854 }
1855 return 0;
1856}
1857
Reid Spencer5c140882006-12-04 20:17:56 +00001858CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1859 const std::string &Name,
1860 Instruction *InsertBefore) {
1861 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1862 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1863 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1864}
1865
1866CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1867 const std::string &Name,
1868 BasicBlock *InsertAtEnd) {
1869 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1870 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1871 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1872}
1873
1874CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1875 const std::string &Name,
1876 Instruction *InsertBefore) {
1877 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1878 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1879 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1880}
1881
1882CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1883 const std::string &Name,
1884 BasicBlock *InsertAtEnd) {
1885 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1886 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1887 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1888}
1889
1890CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1891 const std::string &Name,
1892 Instruction *InsertBefore) {
1893 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1894 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1895 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1896}
1897
1898CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1899 const std::string &Name,
1900 BasicBlock *InsertAtEnd) {
1901 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1902 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1903 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1904}
1905
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001906CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1907 const std::string &Name,
1908 BasicBlock *InsertAtEnd) {
1909 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001910 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001911 "Invalid cast");
1912
Chris Lattner03c49532007-01-15 02:27:26 +00001913 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001914 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1915 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1916}
1917
1918/// @brief Create a BitCast or a PtrToInt cast instruction
1919CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1920 const std::string &Name,
1921 Instruction *InsertBefore) {
1922 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001923 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001924 "Invalid cast");
1925
Chris Lattner03c49532007-01-15 02:27:26 +00001926 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001927 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1928 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1929}
1930
Reid Spencer7e933472006-12-12 00:49:44 +00001931CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1932 bool isSigned, const std::string &Name,
1933 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001934 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001935 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1936 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1937 Instruction::CastOps opcode =
1938 (SrcBits == DstBits ? Instruction::BitCast :
1939 (SrcBits > DstBits ? Instruction::Trunc :
1940 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1941 return create(opcode, C, Ty, Name, InsertBefore);
1942}
1943
1944CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1945 bool isSigned, const std::string &Name,
1946 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001947 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001948 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1949 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1950 Instruction::CastOps opcode =
1951 (SrcBits == DstBits ? Instruction::BitCast :
1952 (SrcBits > DstBits ? Instruction::Trunc :
1953 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1954 return create(opcode, C, Ty, Name, InsertAtEnd);
1955}
1956
1957CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1958 const std::string &Name,
1959 Instruction *InsertBefore) {
1960 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1961 "Invalid cast");
1962 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1963 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1964 Instruction::CastOps opcode =
1965 (SrcBits == DstBits ? Instruction::BitCast :
1966 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1967 return create(opcode, C, Ty, Name, InsertBefore);
1968}
1969
1970CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1971 const std::string &Name,
1972 BasicBlock *InsertAtEnd) {
1973 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1974 "Invalid cast");
1975 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1976 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1977 Instruction::CastOps opcode =
1978 (SrcBits == DstBits ? Instruction::BitCast :
1979 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1980 return create(opcode, C, Ty, Name, InsertAtEnd);
1981}
1982
Duncan Sands55e50902008-01-06 10:12:28 +00001983// Check whether it is valid to call getCastOpcode for these types.
1984// This routine must be kept in sync with getCastOpcode.
1985bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
1986 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
1987 return false;
1988
1989 if (SrcTy == DestTy)
1990 return true;
1991
1992 // Get the bit sizes, we'll need these
1993 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1994 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1995
1996 // Run through the possibilities ...
1997 if (DestTy->isInteger()) { // Casting to integral
1998 if (SrcTy->isInteger()) { // Casting from integral
1999 return true;
2000 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2001 return true;
2002 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2003 // Casting from vector
2004 return DestBits == PTy->getBitWidth();
2005 } else { // Casting from something else
2006 return isa<PointerType>(SrcTy);
2007 }
2008 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2009 if (SrcTy->isInteger()) { // Casting from integral
2010 return true;
2011 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2012 return true;
2013 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2014 // Casting from vector
2015 return DestBits == PTy->getBitWidth();
2016 } else { // Casting from something else
2017 return false;
2018 }
2019 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2020 // Casting to vector
2021 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2022 // Casting from vector
2023 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
2024 } else { // Casting from something else
2025 return DestPTy->getBitWidth() == SrcBits;
2026 }
2027 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2028 if (isa<PointerType>(SrcTy)) { // Casting from pointer
2029 return true;
2030 } else if (SrcTy->isInteger()) { // Casting from integral
2031 return true;
2032 } else { // Casting from something else
2033 return false;
2034 }
2035 } else { // Casting to something else
2036 return false;
2037 }
2038}
2039
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002040// Provide a way to get a "cast" where the cast opcode is inferred from the
2041// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002042// logic in the castIsValid function below. This axiom should hold:
2043// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2044// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002045// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002046// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002047Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002048CastInst::getCastOpcode(
2049 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002050 // Get the bit sizes, we'll need these
2051 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002052 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2053 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002054
Duncan Sands55e50902008-01-06 10:12:28 +00002055 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2056 "Only first class types are castable!");
2057
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002058 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002059 if (DestTy->isInteger()) { // Casting to integral
2060 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002061 if (DestBits < SrcBits)
2062 return Trunc; // int -> smaller int
2063 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002064 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002065 return SExt; // signed -> SEXT
2066 else
2067 return ZExt; // unsigned -> ZEXT
2068 } else {
2069 return BitCast; // Same size, No-op cast
2070 }
2071 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002072 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002073 return FPToSI; // FP -> sint
2074 else
2075 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002076 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002077 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002078 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002079 return BitCast; // Same size, no-op cast
2080 } else {
2081 assert(isa<PointerType>(SrcTy) &&
2082 "Casting from a value that is not first-class type");
2083 return PtrToInt; // ptr -> int
2084 }
2085 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002086 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002087 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002088 return SIToFP; // sint -> FP
2089 else
2090 return UIToFP; // uint -> FP
2091 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2092 if (DestBits < SrcBits) {
2093 return FPTrunc; // FP -> smaller FP
2094 } else if (DestBits > SrcBits) {
2095 return FPExt; // FP -> larger FP
2096 } else {
2097 return BitCast; // same size, no-op cast
2098 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002099 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002100 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002101 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002102 return BitCast; // same size, no-op cast
2103 } else {
2104 assert(0 && "Casting pointer or non-first class to float");
2105 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002106 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2107 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002108 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002109 "Casting vector to vector of different widths");
2110 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002111 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002112 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002113 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002114 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002115 }
2116 } else if (isa<PointerType>(DestTy)) {
2117 if (isa<PointerType>(SrcTy)) {
2118 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002119 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002120 return IntToPtr; // int -> ptr
2121 } else {
2122 assert(!"Casting pointer to other than pointer or int");
2123 }
2124 } else {
2125 assert(!"Casting to type that is not first-class");
2126 }
2127
2128 // If we fall through to here we probably hit an assertion cast above
2129 // and assertions are not turned on. Anything we return is an error, so
2130 // BitCast is as good a choice as any.
2131 return BitCast;
2132}
2133
2134//===----------------------------------------------------------------------===//
2135// CastInst SubClass Constructors
2136//===----------------------------------------------------------------------===//
2137
2138/// Check that the construction parameters for a CastInst are correct. This
2139/// could be broken out into the separate constructors but it is useful to have
2140/// it in one place and to eliminate the redundant code for getting the sizes
2141/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002142bool
2143CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002144
2145 // Check for type sanity on the arguments
2146 const Type *SrcTy = S->getType();
2147 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2148 return false;
2149
2150 // Get the size of the types in bits, we'll need this later
2151 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2152 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2153
2154 // Switch on the opcode provided
2155 switch (op) {
2156 default: return false; // This is an input error
2157 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002158 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002159 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002160 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002161 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002162 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002163 case Instruction::FPTrunc:
2164 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2165 SrcBitSize > DstBitSize;
2166 case Instruction::FPExt:
2167 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2168 SrcBitSize < DstBitSize;
2169 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002170 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002171 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2172 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2173 return SVTy->getElementType()->isInteger() &&
2174 DVTy->getElementType()->isFloatingPoint() &&
2175 SVTy->getNumElements() == DVTy->getNumElements();
2176 }
2177 }
Chris Lattner03c49532007-01-15 02:27:26 +00002178 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002179 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002180 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002181 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2182 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2183 return SVTy->getElementType()->isFloatingPoint() &&
2184 DVTy->getElementType()->isInteger() &&
2185 SVTy->getNumElements() == DVTy->getNumElements();
2186 }
2187 }
Chris Lattner03c49532007-01-15 02:27:26 +00002188 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002189 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002190 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002191 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002192 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002193 case Instruction::BitCast:
2194 // BitCast implies a no-op cast of type only. No bits change.
2195 // However, you can't cast pointers to anything but pointers.
2196 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2197 return false;
2198
Duncan Sands55e50902008-01-06 10:12:28 +00002199 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002200 // these cases, the cast is okay if the source and destination bit widths
2201 // are identical.
2202 return SrcBitSize == DstBitSize;
2203 }
2204}
2205
2206TruncInst::TruncInst(
2207 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2208) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002209 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002210}
2211
2212TruncInst::TruncInst(
2213 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2214) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002215 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002216}
2217
2218ZExtInst::ZExtInst(
2219 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2220) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002221 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002222}
2223
2224ZExtInst::ZExtInst(
2225 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2226) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002227 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002228}
2229SExtInst::SExtInst(
2230 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2231) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002232 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002233}
2234
Jeff Cohencc08c832006-12-02 02:22:01 +00002235SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002236 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2237) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002238 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002239}
2240
2241FPTruncInst::FPTruncInst(
2242 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2243) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002244 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002245}
2246
2247FPTruncInst::FPTruncInst(
2248 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2249) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002250 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002251}
2252
2253FPExtInst::FPExtInst(
2254 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2255) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002256 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002257}
2258
2259FPExtInst::FPExtInst(
2260 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2261) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002262 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002263}
2264
2265UIToFPInst::UIToFPInst(
2266 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2267) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002268 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002269}
2270
2271UIToFPInst::UIToFPInst(
2272 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2273) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002274 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002275}
2276
2277SIToFPInst::SIToFPInst(
2278 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2279) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002280 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002281}
2282
2283SIToFPInst::SIToFPInst(
2284 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2285) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002286 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002287}
2288
2289FPToUIInst::FPToUIInst(
2290 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2291) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002292 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002293}
2294
2295FPToUIInst::FPToUIInst(
2296 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2297) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002298 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299}
2300
2301FPToSIInst::FPToSIInst(
2302 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2303) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002304 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002305}
2306
2307FPToSIInst::FPToSIInst(
2308 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2309) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002310 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002311}
2312
2313PtrToIntInst::PtrToIntInst(
2314 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2315) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002316 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002317}
2318
2319PtrToIntInst::PtrToIntInst(
2320 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2321) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002322 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002323}
2324
2325IntToPtrInst::IntToPtrInst(
2326 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2327) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002328 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002329}
2330
2331IntToPtrInst::IntToPtrInst(
2332 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2333) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002334 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002335}
2336
2337BitCastInst::BitCastInst(
2338 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2339) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002340 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002341}
2342
2343BitCastInst::BitCastInst(
2344 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2345) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002346 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002347}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002348
2349//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002350// CmpInst Classes
2351//===----------------------------------------------------------------------===//
2352
2353CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2354 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002355 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002356 Ops[0].init(LHS, this);
2357 Ops[1].init(RHS, this);
2358 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002359 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002360 if (op == Instruction::ICmp) {
2361 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2362 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2363 "Invalid ICmp predicate value");
2364 const Type* Op0Ty = getOperand(0)->getType();
2365 const Type* Op1Ty = getOperand(1)->getType();
2366 assert(Op0Ty == Op1Ty &&
2367 "Both operands to ICmp instruction are not of the same type!");
2368 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002369 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002370 "Invalid operand types for ICmp instruction");
2371 return;
2372 }
2373 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2374 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2375 "Invalid FCmp predicate value");
2376 const Type* Op0Ty = getOperand(0)->getType();
2377 const Type* Op1Ty = getOperand(1)->getType();
2378 assert(Op0Ty == Op1Ty &&
2379 "Both operands to FCmp instruction are not of the same type!");
2380 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002381 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002382 "Invalid operand types for FCmp instruction");
2383}
2384
2385CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2386 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002387 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002388 Ops[0].init(LHS, this);
2389 Ops[1].init(RHS, this);
2390 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002391 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002392 if (op == Instruction::ICmp) {
2393 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2394 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2395 "Invalid ICmp predicate value");
2396
2397 const Type* Op0Ty = getOperand(0)->getType();
2398 const Type* Op1Ty = getOperand(1)->getType();
2399 assert(Op0Ty == Op1Ty &&
2400 "Both operands to ICmp instruction are not of the same type!");
2401 // Check that the operands are the right type
Anton Korobeynikov579f0712008-02-20 11:08:44 +00002402 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002403 "Invalid operand types for ICmp instruction");
2404 return;
2405 }
2406 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2407 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2408 "Invalid FCmp predicate value");
2409 const Type* Op0Ty = getOperand(0)->getType();
2410 const Type* Op1Ty = getOperand(1)->getType();
2411 assert(Op0Ty == Op1Ty &&
2412 "Both operands to FCmp instruction are not of the same type!");
2413 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002414 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002415 "Invalid operand types for FCmp instruction");
2416}
2417
2418CmpInst *
2419CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2420 const std::string &Name, Instruction *InsertBefore) {
2421 if (Op == Instruction::ICmp) {
2422 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2423 InsertBefore);
2424 }
2425 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2426 InsertBefore);
2427}
2428
2429CmpInst *
2430CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2431 const std::string &Name, BasicBlock *InsertAtEnd) {
2432 if (Op == Instruction::ICmp) {
2433 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2434 InsertAtEnd);
2435 }
2436 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2437 InsertAtEnd);
2438}
2439
2440void CmpInst::swapOperands() {
2441 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2442 IC->swapOperands();
2443 else
2444 cast<FCmpInst>(this)->swapOperands();
2445}
2446
2447bool CmpInst::isCommutative() {
2448 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2449 return IC->isCommutative();
2450 return cast<FCmpInst>(this)->isCommutative();
2451}
2452
2453bool CmpInst::isEquality() {
2454 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2455 return IC->isEquality();
2456 return cast<FCmpInst>(this)->isEquality();
2457}
2458
2459
2460ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2461 switch (pred) {
2462 default:
2463 assert(!"Unknown icmp predicate!");
2464 case ICMP_EQ: return ICMP_NE;
2465 case ICMP_NE: return ICMP_EQ;
2466 case ICMP_UGT: return ICMP_ULE;
2467 case ICMP_ULT: return ICMP_UGE;
2468 case ICMP_UGE: return ICMP_ULT;
2469 case ICMP_ULE: return ICMP_UGT;
2470 case ICMP_SGT: return ICMP_SLE;
2471 case ICMP_SLT: return ICMP_SGE;
2472 case ICMP_SGE: return ICMP_SLT;
2473 case ICMP_SLE: return ICMP_SGT;
2474 }
2475}
2476
2477ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2478 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002479 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002480 case ICMP_EQ: case ICMP_NE:
2481 return pred;
2482 case ICMP_SGT: return ICMP_SLT;
2483 case ICMP_SLT: return ICMP_SGT;
2484 case ICMP_SGE: return ICMP_SLE;
2485 case ICMP_SLE: return ICMP_SGE;
2486 case ICMP_UGT: return ICMP_ULT;
2487 case ICMP_ULT: return ICMP_UGT;
2488 case ICMP_UGE: return ICMP_ULE;
2489 case ICMP_ULE: return ICMP_UGE;
2490 }
2491}
2492
Reid Spencer266e42b2006-12-23 06:05:41 +00002493ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2494 switch (pred) {
2495 default: assert(! "Unknown icmp predicate!");
2496 case ICMP_EQ: case ICMP_NE:
2497 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2498 return pred;
2499 case ICMP_UGT: return ICMP_SGT;
2500 case ICMP_ULT: return ICMP_SLT;
2501 case ICMP_UGE: return ICMP_SGE;
2502 case ICMP_ULE: return ICMP_SLE;
2503 }
2504}
2505
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002506ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2507 switch (pred) {
2508 default: assert(! "Unknown icmp predicate!");
2509 case ICMP_EQ: case ICMP_NE:
2510 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2511 return pred;
2512 case ICMP_SGT: return ICMP_UGT;
2513 case ICMP_SLT: return ICMP_ULT;
2514 case ICMP_SGE: return ICMP_UGE;
2515 case ICMP_SLE: return ICMP_ULE;
2516 }
2517}
2518
Reid Spencer266e42b2006-12-23 06:05:41 +00002519bool ICmpInst::isSignedPredicate(Predicate pred) {
2520 switch (pred) {
2521 default: assert(! "Unknown icmp predicate!");
2522 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2523 return true;
2524 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2525 case ICMP_UGE: case ICMP_ULE:
2526 return false;
2527 }
2528}
2529
Reid Spencer0286bc12007-02-28 22:00:54 +00002530/// Initialize a set of values that all satisfy the condition with C.
2531///
2532ConstantRange
2533ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2534 APInt Lower(C);
2535 APInt Upper(C);
2536 uint32_t BitWidth = C.getBitWidth();
2537 switch (pred) {
2538 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2539 case ICmpInst::ICMP_EQ: Upper++; break;
2540 case ICmpInst::ICMP_NE: Lower++; break;
2541 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2542 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2543 case ICmpInst::ICMP_UGT:
2544 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2545 break;
2546 case ICmpInst::ICMP_SGT:
2547 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2548 break;
2549 case ICmpInst::ICMP_ULE:
2550 Lower = APInt::getMinValue(BitWidth); Upper++;
2551 break;
2552 case ICmpInst::ICMP_SLE:
2553 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2554 break;
2555 case ICmpInst::ICMP_UGE:
2556 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2557 break;
2558 case ICmpInst::ICMP_SGE:
2559 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2560 break;
2561 }
2562 return ConstantRange(Lower, Upper);
2563}
2564
Reid Spencerd9436b62006-11-20 01:22:35 +00002565FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2566 switch (pred) {
2567 default:
2568 assert(!"Unknown icmp predicate!");
2569 case FCMP_OEQ: return FCMP_UNE;
2570 case FCMP_ONE: return FCMP_UEQ;
2571 case FCMP_OGT: return FCMP_ULE;
2572 case FCMP_OLT: return FCMP_UGE;
2573 case FCMP_OGE: return FCMP_ULT;
2574 case FCMP_OLE: return FCMP_UGT;
2575 case FCMP_UEQ: return FCMP_ONE;
2576 case FCMP_UNE: return FCMP_OEQ;
2577 case FCMP_UGT: return FCMP_OLE;
2578 case FCMP_ULT: return FCMP_OGE;
2579 case FCMP_UGE: return FCMP_OLT;
2580 case FCMP_ULE: return FCMP_OGT;
2581 case FCMP_ORD: return FCMP_UNO;
2582 case FCMP_UNO: return FCMP_ORD;
2583 case FCMP_TRUE: return FCMP_FALSE;
2584 case FCMP_FALSE: return FCMP_TRUE;
2585 }
2586}
2587
2588FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2589 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002590 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002591 case FCMP_FALSE: case FCMP_TRUE:
2592 case FCMP_OEQ: case FCMP_ONE:
2593 case FCMP_UEQ: case FCMP_UNE:
2594 case FCMP_ORD: case FCMP_UNO:
2595 return pred;
2596 case FCMP_OGT: return FCMP_OLT;
2597 case FCMP_OLT: return FCMP_OGT;
2598 case FCMP_OGE: return FCMP_OLE;
2599 case FCMP_OLE: return FCMP_OGE;
2600 case FCMP_UGT: return FCMP_ULT;
2601 case FCMP_ULT: return FCMP_UGT;
2602 case FCMP_UGE: return FCMP_ULE;
2603 case FCMP_ULE: return FCMP_UGE;
2604 }
2605}
2606
Reid Spencer266e42b2006-12-23 06:05:41 +00002607bool CmpInst::isUnsigned(unsigned short predicate) {
2608 switch (predicate) {
2609 default: return false;
2610 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2611 case ICmpInst::ICMP_UGE: return true;
2612 }
2613}
2614
2615bool CmpInst::isSigned(unsigned short predicate){
2616 switch (predicate) {
2617 default: return false;
2618 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2619 case ICmpInst::ICMP_SGE: return true;
2620 }
2621}
2622
2623bool CmpInst::isOrdered(unsigned short predicate) {
2624 switch (predicate) {
2625 default: return false;
2626 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2627 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2628 case FCmpInst::FCMP_ORD: return true;
2629 }
2630}
2631
2632bool CmpInst::isUnordered(unsigned short predicate) {
2633 switch (predicate) {
2634 default: return false;
2635 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2636 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2637 case FCmpInst::FCMP_UNO: return true;
2638 }
2639}
2640
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002641//===----------------------------------------------------------------------===//
2642// SwitchInst Implementation
2643//===----------------------------------------------------------------------===//
2644
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002645void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002646 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002647 ReservedSpace = 2+NumCases*2;
2648 NumOperands = 2;
2649 OperandList = new Use[ReservedSpace];
2650
2651 OperandList[0].init(Value, this);
2652 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002653}
2654
Chris Lattner2195fc42007-02-24 00:55:48 +00002655/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2656/// switch on and a default destination. The number of additional cases can
2657/// be specified here to make memory allocation more efficient. This
2658/// constructor can also autoinsert before another instruction.
2659SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2660 Instruction *InsertBefore)
2661 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2662 init(Value, Default, NumCases);
2663}
2664
2665/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2666/// switch on and a default destination. The number of additional cases can
2667/// be specified here to make memory allocation more efficient. This
2668/// constructor also autoinserts at the end of the specified BasicBlock.
2669SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2670 BasicBlock *InsertAtEnd)
2671 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2672 init(Value, Default, NumCases);
2673}
2674
Misha Brukmanb1c93172005-04-21 23:48:37 +00002675SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002676 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2677 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002678 Use *OL = OperandList, *InOL = SI.OperandList;
2679 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2680 OL[i].init(InOL[i], this);
2681 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002682 }
2683}
2684
Gordon Henriksen14a55692007-12-10 02:14:30 +00002685SwitchInst::~SwitchInst() {
2686 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002687}
2688
2689
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002690/// addCase - Add an entry to the switch instruction...
2691///
Chris Lattner47ac1872005-02-24 05:32:09 +00002692void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002693 unsigned OpNo = NumOperands;
2694 if (OpNo+2 > ReservedSpace)
2695 resizeOperands(0); // Get more space!
2696 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002697 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002698 NumOperands = OpNo+2;
2699 OperandList[OpNo].init(OnVal, this);
2700 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002701}
2702
2703/// removeCase - This method removes the specified successor from the switch
2704/// instruction. Note that this cannot be used to remove the default
2705/// destination (successor #0).
2706///
2707void SwitchInst::removeCase(unsigned idx) {
2708 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002709 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2710
2711 unsigned NumOps = getNumOperands();
2712 Use *OL = OperandList;
2713
2714 // Move everything after this operand down.
2715 //
2716 // FIXME: we could just swap with the end of the list, then erase. However,
2717 // client might not expect this to happen. The code as it is thrashes the
2718 // use/def lists, which is kinda lame.
2719 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2720 OL[i-2] = OL[i];
2721 OL[i-2+1] = OL[i+1];
2722 }
2723
2724 // Nuke the last value.
2725 OL[NumOps-2].set(0);
2726 OL[NumOps-2+1].set(0);
2727 NumOperands = NumOps-2;
2728}
2729
2730/// resizeOperands - resize operands - This adjusts the length of the operands
2731/// list according to the following behavior:
2732/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2733/// of operation. This grows the number of ops by 1.5 times.
2734/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2735/// 3. If NumOps == NumOperands, trim the reserved space.
2736///
2737void SwitchInst::resizeOperands(unsigned NumOps) {
2738 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002739 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002740 } else if (NumOps*2 > NumOperands) {
2741 // No resize needed.
2742 if (ReservedSpace >= NumOps) return;
2743 } else if (NumOps == NumOperands) {
2744 if (ReservedSpace == NumOps) return;
2745 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002746 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002747 }
2748
2749 ReservedSpace = NumOps;
2750 Use *NewOps = new Use[NumOps];
2751 Use *OldOps = OperandList;
2752 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2753 NewOps[i].init(OldOps[i], this);
2754 OldOps[i].set(0);
2755 }
2756 delete [] OldOps;
2757 OperandList = NewOps;
2758}
2759
2760
2761BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2762 return getSuccessor(idx);
2763}
2764unsigned SwitchInst::getNumSuccessorsV() const {
2765 return getNumSuccessors();
2766}
2767void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2768 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002769}
Chris Lattnerf22be932004-10-15 23:52:53 +00002770
Devang Patel295711f2008-02-19 22:15:16 +00002771//===----------------------------------------------------------------------===//
2772// GetResultInst Implementation
2773//===----------------------------------------------------------------------===//
2774
Devang Patel666d4512008-02-20 19:10:47 +00002775GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002776 const std::string &Name,
2777 Instruction *InsertBef)
Devang Pateldcad9da2008-02-20 19:26:55 +00002778 : Instruction(cast<StructType>(Aggregate->getType())->getElementType(Index),
Devang Patel666d4512008-02-20 19:10:47 +00002779 GetResult, &Aggr, 1, InsertBef) {
2780 assert(isValidOperands(Aggregate, Index) && "Invalid GetResultInst operands!");
2781 Aggr.init(Aggregate, this);
2782 Idx = Index;
Devang Patel295711f2008-02-19 22:15:16 +00002783 setName(Name);
2784}
2785
Devang Patel666d4512008-02-20 19:10:47 +00002786bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2787 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002788 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002789
Devang Patelcf193b82008-02-20 19:39:41 +00002790 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2791 unsigned NumElements = STy->getNumElements();
2792 if (Index >= NumElements)
2793 return false;
2794
2795 // getresult aggregate value's element types are restricted to
2796 // avoid nested aggregates.
2797 for (unsigned i = 0; i < NumElements; ++i)
2798 if (!STy->getElementType(i)->isFirstClassType())
2799 return false;
2800
2801 // Otherwise, Aggregate is valid.
2802 return true;
2803 }
Devang Patel666d4512008-02-20 19:10:47 +00002804 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002805}
2806
Chris Lattnerf22be932004-10-15 23:52:53 +00002807// Define these methods here so vtables don't get emitted into every translation
2808// unit that uses these classes.
2809
2810GetElementPtrInst *GetElementPtrInst::clone() const {
2811 return new GetElementPtrInst(*this);
2812}
2813
2814BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002815 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002816}
2817
Chris Lattner0b490b02007-08-24 20:48:18 +00002818FCmpInst* FCmpInst::clone() const {
2819 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2820}
2821ICmpInst* ICmpInst::clone() const {
2822 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002823}
2824
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002825MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2826AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2827FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2828LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2829StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2830CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2831CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2832CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2833CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2834CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2835CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2836CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2837CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2838CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2839CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2840CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2841CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2842CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002843SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2844VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2845
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002846ExtractElementInst *ExtractElementInst::clone() const {
2847 return new ExtractElementInst(*this);
2848}
2849InsertElementInst *InsertElementInst::clone() const {
2850 return new InsertElementInst(*this);
2851}
2852ShuffleVectorInst *ShuffleVectorInst::clone() const {
2853 return new ShuffleVectorInst(*this);
2854}
Chris Lattnerf22be932004-10-15 23:52:53 +00002855PHINode *PHINode::clone() const { return new PHINode(*this); }
2856ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2857BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2858SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2859InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2860UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002861UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002862GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }