blob: dfd3b830caaced46159274ce467e4d9764323d94 [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 Patelae682fb2008-02-26 17:56:20 +0000576 &RetVal, RI.getNumOperands()) {
Devang Patel59643e52008-02-23 00:35:18 +0000577 unsigned N = RI.getNumOperands();
Devang Patelae682fb2008-02-26 17:56:20 +0000578 if (N == 1)
579 RetVal.init(RI.RetVal, this);
580 else if (N) {
581 Use *OL = OperandList = new Use[N];
582 for (unsigned i = 0; i < N; ++i)
583 OL[i].init(RI.getOperand(i), this);
584 }
Chris Lattner2195fc42007-02-24 00:55:48 +0000585}
586
587ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Devang Patelae682fb2008-02-26 17:56:20 +0000588 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000589 if (retVal)
590 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000591}
592ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Devang Patelae682fb2008-02-26 17:56:20 +0000593 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000594 if (retVal)
595 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000596}
597ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Devang Patelae682fb2008-02-26 17:56:20 +0000598 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000599}
600
Devang Patela736c002008-02-26 19:38:17 +0000601ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
602 Instruction *InsertBefore)
603 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N, InsertBefore) {
604 if (N != 0)
605 init(retVals, N);
606}
607ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
608 BasicBlock *InsertAtEnd)
609 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N, InsertAtEnd) {
610 if (N != 0)
611 init(retVals, N);
612}
613ReturnInst::ReturnInst(Value * const* retVals, unsigned N)
614 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N) {
615 if (N != 0)
616 init(retVals, N);
617}
618
Devang Patel060e79c2008-02-26 19:15:26 +0000619void ReturnInst::init(Value * const* retVals, unsigned N) {
Devang Patelc38eb522008-02-26 18:49:29 +0000620 assert (N > 0 && "Invalid operands numbers in ReturnInst init");
Devang Patel59643e52008-02-23 00:35:18 +0000621
Devang Patelc38eb522008-02-26 18:49:29 +0000622 NumOperands = N;
Devang Patel59643e52008-02-23 00:35:18 +0000623 if (NumOperands == 1) {
Devang Patel060e79c2008-02-26 19:15:26 +0000624 Value *V = *retVals;
Devang Patel59643e52008-02-23 00:35:18 +0000625 if (V->getType() == Type::VoidTy)
626 return;
Devang Patel060e79c2008-02-26 19:15:26 +0000627 RetVal.init(V, this);
Devang Patelae682fb2008-02-26 17:56:20 +0000628 return;
Devang Patel59643e52008-02-23 00:35:18 +0000629 }
630
631 Use *OL = OperandList = new Use[NumOperands];
632 for (unsigned i = 0; i < NumOperands; ++i) {
Devang Patel060e79c2008-02-26 19:15:26 +0000633 Value *V = *retVals++;
Devang Patel59643e52008-02-23 00:35:18 +0000634 assert(!isa<BasicBlock>(V) &&
635 "Cannot return basic block. Probably using the incorrect ctor");
Devang Patel060e79c2008-02-26 19:15:26 +0000636 OL[i].init(V, this);
Devang Patel59643e52008-02-23 00:35:18 +0000637 }
638}
639
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000640unsigned ReturnInst::getNumSuccessorsV() const {
641 return getNumSuccessors();
642}
643
Devang Patelae682fb2008-02-26 17:56:20 +0000644/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
645/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000646void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000647 assert(0 && "ReturnInst has no successors!");
648}
649
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000650BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
651 assert(0 && "ReturnInst has no successors!");
652 abort();
653 return 0;
654}
655
Devang Patel59643e52008-02-23 00:35:18 +0000656ReturnInst::~ReturnInst() {
Devang Patelae682fb2008-02-26 17:56:20 +0000657 if (NumOperands > 1)
Devang Patel59643e52008-02-23 00:35:18 +0000658 delete [] OperandList;
659}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000660
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000661//===----------------------------------------------------------------------===//
662// UnwindInst Implementation
663//===----------------------------------------------------------------------===//
664
Chris Lattner2195fc42007-02-24 00:55:48 +0000665UnwindInst::UnwindInst(Instruction *InsertBefore)
666 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
667}
668UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
669 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
670}
671
672
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000673unsigned UnwindInst::getNumSuccessorsV() const {
674 return getNumSuccessors();
675}
676
677void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000678 assert(0 && "UnwindInst has no successors!");
679}
680
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000681BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
682 assert(0 && "UnwindInst has no successors!");
683 abort();
684 return 0;
685}
686
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000687//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000688// UnreachableInst Implementation
689//===----------------------------------------------------------------------===//
690
Chris Lattner2195fc42007-02-24 00:55:48 +0000691UnreachableInst::UnreachableInst(Instruction *InsertBefore)
692 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
693}
694UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
695 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
696}
697
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000698unsigned UnreachableInst::getNumSuccessorsV() const {
699 return getNumSuccessors();
700}
701
702void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
703 assert(0 && "UnwindInst has no successors!");
704}
705
706BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
707 assert(0 && "UnwindInst has no successors!");
708 abort();
709 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000710}
711
712//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000713// BranchInst Implementation
714//===----------------------------------------------------------------------===//
715
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000716void BranchInst::AssertOK() {
717 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000718 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000719 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000720}
721
Chris Lattner2195fc42007-02-24 00:55:48 +0000722BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
723 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
724 assert(IfTrue != 0 && "Branch destination may not be null!");
725 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
726}
727BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
728 Instruction *InsertBefore)
729: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
730 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
731 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
732 Ops[2].init(Cond, this);
733#ifndef NDEBUG
734 AssertOK();
735#endif
736}
737
738BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
739 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
740 assert(IfTrue != 0 && "Branch destination may not be null!");
741 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
742}
743
744BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
745 BasicBlock *InsertAtEnd)
746 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
747 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
748 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
749 Ops[2].init(Cond, this);
750#ifndef NDEBUG
751 AssertOK();
752#endif
753}
754
755
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000756BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000757 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000758 OperandList[0].init(BI.getOperand(0), this);
759 if (BI.getNumOperands() != 1) {
760 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
761 OperandList[1].init(BI.getOperand(1), this);
762 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000763 }
764}
765
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000766BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
767 return getSuccessor(idx);
768}
769unsigned BranchInst::getNumSuccessorsV() const {
770 return getNumSuccessors();
771}
772void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
773 setSuccessor(idx, B);
774}
775
776
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000777//===----------------------------------------------------------------------===//
778// AllocationInst Implementation
779//===----------------------------------------------------------------------===//
780
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000781static Value *getAISize(Value *Amt) {
782 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000783 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000784 else {
785 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000786 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000787 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000788 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000789 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000790 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000791}
792
Misha Brukmanb1c93172005-04-21 23:48:37 +0000793AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000794 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000795 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000796 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000797 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000798 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000799 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000800 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000801}
802
Misha Brukmanb1c93172005-04-21 23:48:37 +0000803AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000804 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000805 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000806 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000807 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000808 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000809 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000810 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000811}
812
Gordon Henriksen14a55692007-12-10 02:14:30 +0000813// Out of line virtual method, so the vtable, etc has a home.
814AllocationInst::~AllocationInst() {
815}
816
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000817bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000818 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
819 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000820 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000821}
822
823const Type *AllocationInst::getAllocatedType() const {
824 return getType()->getElementType();
825}
826
827AllocaInst::AllocaInst(const AllocaInst &AI)
828 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000829 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000830}
831
832MallocInst::MallocInst(const MallocInst &MI)
833 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000834 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000835}
836
837//===----------------------------------------------------------------------===//
838// FreeInst Implementation
839//===----------------------------------------------------------------------===//
840
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000841void FreeInst::AssertOK() {
842 assert(isa<PointerType>(getOperand(0)->getType()) &&
843 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000844}
845
846FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000847 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000848 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000849}
850
851FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000852 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000853 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000854}
855
856
857//===----------------------------------------------------------------------===//
858// LoadInst Implementation
859//===----------------------------------------------------------------------===//
860
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000861void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000862 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000863 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000864}
865
866LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000867 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000868 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000869 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000870 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000871 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000872 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000873}
874
875LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000876 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000877 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000878 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000879 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000880 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000881 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000882}
883
884LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
885 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000886 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000887 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000888 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000889 setAlignment(0);
890 AssertOK();
891 setName(Name);
892}
893
894LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
895 unsigned Align, Instruction *InsertBef)
896 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
897 Load, Ptr, InsertBef) {
898 setVolatile(isVolatile);
899 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000900 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000901 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000902}
903
Dan Gohman68659282007-07-18 20:51:11 +0000904LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
905 unsigned Align, BasicBlock *InsertAE)
906 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
907 Load, Ptr, InsertAE) {
908 setVolatile(isVolatile);
909 setAlignment(Align);
910 AssertOK();
911 setName(Name);
912}
913
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000914LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
915 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000916 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000917 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000918 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000919 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000920 AssertOK();
921 setName(Name);
922}
923
924
925
926LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000927 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
928 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000929 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000930 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000931 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000932 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000933}
934
935LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000936 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
937 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000938 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000939 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000940 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000941 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000942}
943
944LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
945 Instruction *InsertBef)
946: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000947 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000948 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000949 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000950 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000951 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000952}
953
954LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
955 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000956 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
957 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000958 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000959 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000960 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000961 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000962}
963
Christopher Lamb84485702007-04-22 19:24:39 +0000964void LoadInst::setAlignment(unsigned Align) {
965 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
966 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
967}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000968
969//===----------------------------------------------------------------------===//
970// StoreInst Implementation
971//===----------------------------------------------------------------------===//
972
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000973void StoreInst::AssertOK() {
974 assert(isa<PointerType>(getOperand(1)->getType()) &&
975 "Ptr must have pointer type!");
976 assert(getOperand(0)->getType() ==
977 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000978 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000979}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000980
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000981
982StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000983 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000984 Ops[0].init(val, this);
985 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000986 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000987 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000988 AssertOK();
989}
990
991StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000992 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000993 Ops[0].init(val, this);
994 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000995 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000996 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000997 AssertOK();
998}
999
Misha Brukmanb1c93172005-04-21 23:48:37 +00001000StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001001 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001002 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001003 Ops[0].init(val, this);
1004 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +00001005 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001006 setAlignment(0);
1007 AssertOK();
1008}
1009
1010StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1011 unsigned Align, Instruction *InsertBefore)
1012 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
1013 Ops[0].init(val, this);
1014 Ops[1].init(addr, this);
1015 setVolatile(isVolatile);
1016 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001017 AssertOK();
1018}
1019
Misha Brukmanb1c93172005-04-21 23:48:37 +00001020StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001021 unsigned Align, BasicBlock *InsertAtEnd)
1022 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
1023 Ops[0].init(val, this);
1024 Ops[1].init(addr, this);
1025 setVolatile(isVolatile);
1026 setAlignment(Align);
1027 AssertOK();
1028}
1029
1030StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001031 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001032 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001033 Ops[0].init(val, this);
1034 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +00001035 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001036 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001037 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001038}
1039
Christopher Lamb84485702007-04-22 19:24:39 +00001040void StoreInst::setAlignment(unsigned Align) {
1041 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1042 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1043}
1044
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001045//===----------------------------------------------------------------------===//
1046// GetElementPtrInst Implementation
1047//===----------------------------------------------------------------------===//
1048
Christopher Lambedf07882007-12-17 01:12:55 +00001049static unsigned retrieveAddrSpace(const Value *Val) {
1050 return cast<PointerType>(Val->getType())->getAddressSpace();
1051}
1052
Chris Lattner79807c3d2007-01-31 19:47:18 +00001053void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
1054 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001055 Use *OL = OperandList = new Use[NumOperands];
1056 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001057
Chris Lattner79807c3d2007-01-31 19:47:18 +00001058 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001059 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001060}
1061
Chris Lattner82981202005-05-03 05:43:30 +00001062void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
1063 NumOperands = 2;
1064 Use *OL = OperandList = new Use[2];
1065 OL[0].init(Ptr, this);
1066 OL[1].init(Idx, this);
1067}
1068
Chris Lattner82981202005-05-03 05:43:30 +00001069GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1070 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001071 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001072 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001073 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +00001074 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001075 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001076}
1077
1078GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1079 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001080 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001081 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001082 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +00001083 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001084 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001085}
1086
Gordon Henriksen14a55692007-12-10 02:14:30 +00001087GetElementPtrInst::~GetElementPtrInst() {
1088 delete[] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001089}
1090
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001091// getIndexedType - Returns the type of the element that would be loaded with
1092// a load instruction with the specified parameters.
1093//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001094// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001095// pointer type.
1096//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001097const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001098 Value* const *Idxs,
1099 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001100 bool AllowCompositeLeaf) {
1101 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
1102
1103 // Handle the special case of the empty set index set...
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001104 if (NumIdx == 0) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001105 if (AllowCompositeLeaf ||
1106 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
1107 return cast<PointerType>(Ptr)->getElementType();
1108 else
1109 return 0;
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001110 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001111
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001112 unsigned CurIdx = 0;
1113 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001114 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001115 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
1116 return 0; // Can't load a whole structure or array!?!?
1117 }
1118
Chris Lattner302116a2007-01-31 04:40:28 +00001119 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001120 if (isa<PointerType>(CT) && CurIdx != 1)
1121 return 0; // Can only index into pointer types at the first index!
1122 if (!CT->indexValid(Index)) return 0;
1123 Ptr = CT->getTypeAtIndex(Index);
1124
1125 // If the new type forwards to another type, then it is in the middle
1126 // of being refined to another type (and hence, may have dropped all
1127 // references to what it was using before). So, use the new forwarded
1128 // type.
1129 if (const Type * Ty = Ptr->getForwardedType()) {
1130 Ptr = Ty;
1131 }
1132 }
Chris Lattner302116a2007-01-31 04:40:28 +00001133 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001134}
1135
Chris Lattner82981202005-05-03 05:43:30 +00001136const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1137 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1138 if (!PTy) return 0; // Type isn't a pointer type!
1139
1140 // Check the pointer index.
1141 if (!PTy->indexValid(Idx)) return 0;
1142
Chris Lattnerc2233332005-05-03 16:44:45 +00001143 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001144}
1145
Chris Lattner45f15572007-04-14 00:12:57 +00001146
1147/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1148/// zeros. If so, the result pointer and the first operand have the same
1149/// value, just potentially different types.
1150bool GetElementPtrInst::hasAllZeroIndices() const {
1151 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1152 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1153 if (!CI->isZero()) return false;
1154 } else {
1155 return false;
1156 }
1157 }
1158 return true;
1159}
1160
Chris Lattner27058292007-04-27 20:35:56 +00001161/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1162/// constant integers. If so, the result pointer and the first operand have
1163/// a constant offset between them.
1164bool GetElementPtrInst::hasAllConstantIndices() const {
1165 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1166 if (!isa<ConstantInt>(getOperand(i)))
1167 return false;
1168 }
1169 return true;
1170}
1171
Chris Lattner45f15572007-04-14 00:12:57 +00001172
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001173//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001174// ExtractElementInst Implementation
1175//===----------------------------------------------------------------------===//
1176
1177ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001178 const std::string &Name,
1179 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001180 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001181 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001182 assert(isValidOperands(Val, Index) &&
1183 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001184 Ops[0].init(Val, this);
1185 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001186 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001187}
1188
Chris Lattner65511ff2006-10-05 06:24:58 +00001189ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1190 const std::string &Name,
1191 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001192 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001193 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001194 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001195 assert(isValidOperands(Val, Index) &&
1196 "Invalid extractelement instruction operands!");
1197 Ops[0].init(Val, this);
1198 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001199 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001200}
1201
1202
Robert Bocchino23004482006-01-10 19:05:34 +00001203ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001204 const std::string &Name,
1205 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001206 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001207 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001208 assert(isValidOperands(Val, Index) &&
1209 "Invalid extractelement instruction operands!");
1210
Robert Bocchino23004482006-01-10 19:05:34 +00001211 Ops[0].init(Val, this);
1212 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001213 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001214}
1215
Chris Lattner65511ff2006-10-05 06:24:58 +00001216ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1217 const std::string &Name,
1218 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001219 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001220 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001221 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001222 assert(isValidOperands(Val, Index) &&
1223 "Invalid extractelement instruction operands!");
1224
1225 Ops[0].init(Val, this);
1226 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001227 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001228}
1229
1230
Chris Lattner54865b32006-04-08 04:05:48 +00001231bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001232 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001233 return false;
1234 return true;
1235}
1236
1237
Robert Bocchino23004482006-01-10 19:05:34 +00001238//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001239// InsertElementInst Implementation
1240//===----------------------------------------------------------------------===//
1241
Chris Lattner0875d942006-04-14 22:20:32 +00001242InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1243 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1244 Ops[0].init(IE.Ops[0], this);
1245 Ops[1].init(IE.Ops[1], this);
1246 Ops[2].init(IE.Ops[2], this);
1247}
Chris Lattner54865b32006-04-08 04:05:48 +00001248InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001249 const std::string &Name,
1250 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001251 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001252 assert(isValidOperands(Vec, Elt, Index) &&
1253 "Invalid insertelement instruction operands!");
1254 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001255 Ops[1].init(Elt, this);
1256 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001257 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001258}
1259
Chris Lattner65511ff2006-10-05 06:24:58 +00001260InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1261 const std::string &Name,
1262 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001263 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001264 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001265 assert(isValidOperands(Vec, Elt, Index) &&
1266 "Invalid insertelement instruction operands!");
1267 Ops[0].init(Vec, this);
1268 Ops[1].init(Elt, this);
1269 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001270 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001271}
1272
1273
Chris Lattner54865b32006-04-08 04:05:48 +00001274InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001275 const std::string &Name,
1276 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001277 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001278 assert(isValidOperands(Vec, Elt, Index) &&
1279 "Invalid insertelement instruction operands!");
1280
1281 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001282 Ops[1].init(Elt, this);
1283 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001284 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001285}
1286
Chris Lattner65511ff2006-10-05 06:24:58 +00001287InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1288 const std::string &Name,
1289 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001290: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001291 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001292 assert(isValidOperands(Vec, Elt, Index) &&
1293 "Invalid insertelement instruction operands!");
1294
1295 Ops[0].init(Vec, this);
1296 Ops[1].init(Elt, this);
1297 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001298 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001299}
1300
Chris Lattner54865b32006-04-08 04:05:48 +00001301bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1302 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001303 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001304 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001305
Reid Spencerd84d35b2007-02-15 02:26:10 +00001306 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001307 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001308
Reid Spencer8d9336d2006-12-31 05:26:44 +00001309 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001310 return false; // Third operand of insertelement must be uint.
1311 return true;
1312}
1313
1314
Robert Bocchinoca27f032006-01-17 20:07:22 +00001315//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001316// ShuffleVectorInst Implementation
1317//===----------------------------------------------------------------------===//
1318
Chris Lattner0875d942006-04-14 22:20:32 +00001319ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1320 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1321 Ops[0].init(SV.Ops[0], this);
1322 Ops[1].init(SV.Ops[1], this);
1323 Ops[2].init(SV.Ops[2], this);
1324}
1325
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001326ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1327 const std::string &Name,
1328 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001329 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001330 assert(isValidOperands(V1, V2, Mask) &&
1331 "Invalid shuffle vector instruction operands!");
1332 Ops[0].init(V1, this);
1333 Ops[1].init(V2, this);
1334 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001335 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001336}
1337
1338ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1339 const std::string &Name,
1340 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001341 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001342 assert(isValidOperands(V1, V2, Mask) &&
1343 "Invalid shuffle vector instruction operands!");
1344
1345 Ops[0].init(V1, this);
1346 Ops[1].init(V2, this);
1347 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001348 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001349}
1350
1351bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1352 const Value *Mask) {
Chris Lattnerf724e342008-03-02 05:28:33 +00001353 if (!isa<VectorType>(V1->getType()) ||
1354 V1->getType() != V2->getType())
1355 return false;
1356
1357 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1358 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1359 MaskTy->getElementType() != Type::Int32Ty ||
1360 MaskTy->getNumElements() !=
1361 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001362 return false;
1363 return true;
1364}
1365
Chris Lattnerf724e342008-03-02 05:28:33 +00001366/// getMaskValue - Return the index from the shuffle mask for the specified
1367/// output result. This is either -1 if the element is undef or a number less
1368/// than 2*numelements.
1369int ShuffleVectorInst::getMaskValue(unsigned i) const {
1370 const Constant *Mask = cast<Constant>(getOperand(2));
1371 if (isa<UndefValue>(Mask)) return -1;
1372 if (isa<ConstantAggregateZero>(Mask)) return 0;
1373 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1374 assert(i < MaskCV->getNumOperands() && "Index out of range");
1375
1376 if (isa<UndefValue>(MaskCV->getOperand(i)))
1377 return -1;
1378 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1379}
1380
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001381
1382//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001383// BinaryOperator Class
1384//===----------------------------------------------------------------------===//
1385
Chris Lattner2195fc42007-02-24 00:55:48 +00001386BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1387 const Type *Ty, const std::string &Name,
1388 Instruction *InsertBefore)
1389 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1390 Ops[0].init(S1, this);
1391 Ops[1].init(S2, this);
1392 init(iType);
1393 setName(Name);
1394}
1395
1396BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1397 const Type *Ty, const std::string &Name,
1398 BasicBlock *InsertAtEnd)
1399 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1400 Ops[0].init(S1, this);
1401 Ops[1].init(S2, this);
1402 init(iType);
1403 setName(Name);
1404}
1405
1406
1407void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001408 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001409 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001410 assert(LHS->getType() == RHS->getType() &&
1411 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001412#ifndef NDEBUG
1413 switch (iType) {
1414 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001415 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001416 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001417 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001418 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001419 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001420 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001421 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001422 case UDiv:
1423 case SDiv:
1424 assert(getType() == LHS->getType() &&
1425 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001426 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1427 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001428 "Incorrect operand type (not integer) for S/UDIV");
1429 break;
1430 case FDiv:
1431 assert(getType() == LHS->getType() &&
1432 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001433 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1434 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001435 && "Incorrect operand type (not floating point) for FDIV");
1436 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001437 case URem:
1438 case SRem:
1439 assert(getType() == LHS->getType() &&
1440 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001441 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1442 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001443 "Incorrect operand type (not integer) for S/UREM");
1444 break;
1445 case FRem:
1446 assert(getType() == LHS->getType() &&
1447 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001448 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1449 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001450 && "Incorrect operand type (not floating point) for FREM");
1451 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001452 case Shl:
1453 case LShr:
1454 case AShr:
1455 assert(getType() == LHS->getType() &&
1456 "Shift operation should return same type as operands!");
1457 assert(getType()->isInteger() &&
1458 "Shift operation requires integer operands");
1459 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001460 case And: case Or:
1461 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001462 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001463 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001464 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001465 (isa<VectorType>(getType()) &&
1466 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001467 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001468 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001469 default:
1470 break;
1471 }
1472#endif
1473}
1474
1475BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001476 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001477 Instruction *InsertBefore) {
1478 assert(S1->getType() == S2->getType() &&
1479 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001480 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001481}
1482
1483BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001484 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001485 BasicBlock *InsertAtEnd) {
1486 BinaryOperator *Res = create(Op, S1, S2, Name);
1487 InsertAtEnd->getInstList().push_back(Res);
1488 return Res;
1489}
1490
1491BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1492 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001493 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1494 return new BinaryOperator(Instruction::Sub,
1495 zero, Op,
1496 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001497}
1498
1499BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1500 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001501 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1502 return new BinaryOperator(Instruction::Sub,
1503 zero, Op,
1504 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001505}
1506
1507BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1508 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001509 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001510 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001511 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001512 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001513 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001514 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001515 }
1516
1517 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001518 Op->getType(), Name, InsertBefore);
1519}
1520
1521BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1522 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001523 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001524 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001525 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001526 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001527 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001528 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001529 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001530 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001531 }
1532
1533 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001534 Op->getType(), Name, InsertAtEnd);
1535}
1536
1537
1538// isConstantAllOnes - Helper function for several functions below
1539static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001540 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1541 return CI->isAllOnesValue();
1542 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1543 return CV->isAllOnesValue();
1544 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001545}
1546
1547bool BinaryOperator::isNeg(const Value *V) {
1548 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1549 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001550 return Bop->getOperand(0) ==
1551 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001552 return false;
1553}
1554
1555bool BinaryOperator::isNot(const Value *V) {
1556 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1557 return (Bop->getOpcode() == Instruction::Xor &&
1558 (isConstantAllOnes(Bop->getOperand(1)) ||
1559 isConstantAllOnes(Bop->getOperand(0))));
1560 return false;
1561}
1562
Chris Lattner2c7d1772005-04-24 07:28:37 +00001563Value *BinaryOperator::getNegArgument(Value *BinOp) {
1564 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1565 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001566}
1567
Chris Lattner2c7d1772005-04-24 07:28:37 +00001568const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1569 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001570}
1571
Chris Lattner2c7d1772005-04-24 07:28:37 +00001572Value *BinaryOperator::getNotArgument(Value *BinOp) {
1573 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1574 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1575 Value *Op0 = BO->getOperand(0);
1576 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001577 if (isConstantAllOnes(Op0)) return Op1;
1578
1579 assert(isConstantAllOnes(Op1));
1580 return Op0;
1581}
1582
Chris Lattner2c7d1772005-04-24 07:28:37 +00001583const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1584 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001585}
1586
1587
1588// swapOperands - Exchange the two operands to this instruction. This
1589// instruction is safe to use on any binary instruction and does not
1590// modify the semantics of the instruction. If the instruction is
1591// order dependent (SetLT f.e.) the opcode is changed.
1592//
1593bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001594 if (!isCommutative())
1595 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001596 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001597 return false;
1598}
1599
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001600//===----------------------------------------------------------------------===//
1601// CastInst Class
1602//===----------------------------------------------------------------------===//
1603
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001604// Just determine if this cast only deals with integral->integral conversion.
1605bool CastInst::isIntegerCast() const {
1606 switch (getOpcode()) {
1607 default: return false;
1608 case Instruction::ZExt:
1609 case Instruction::SExt:
1610 case Instruction::Trunc:
1611 return true;
1612 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001613 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001614 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001615}
1616
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001617bool CastInst::isLosslessCast() const {
1618 // Only BitCast can be lossless, exit fast if we're not BitCast
1619 if (getOpcode() != Instruction::BitCast)
1620 return false;
1621
1622 // Identity cast is always lossless
1623 const Type* SrcTy = getOperand(0)->getType();
1624 const Type* DstTy = getType();
1625 if (SrcTy == DstTy)
1626 return true;
1627
Reid Spencer8d9336d2006-12-31 05:26:44 +00001628 // Pointer to pointer is always lossless.
1629 if (isa<PointerType>(SrcTy))
1630 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001631 return false; // Other types have no identity values
1632}
1633
1634/// This function determines if the CastInst does not require any bits to be
1635/// changed in order to effect the cast. Essentially, it identifies cases where
1636/// no code gen is necessary for the cast, hence the name no-op cast. For
1637/// example, the following are all no-op casts:
1638/// # bitcast uint %X, int
1639/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001640/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001641/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1642/// @brief Determine if a cast is a no-op.
1643bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1644 switch (getOpcode()) {
1645 default:
1646 assert(!"Invalid CastOp");
1647 case Instruction::Trunc:
1648 case Instruction::ZExt:
1649 case Instruction::SExt:
1650 case Instruction::FPTrunc:
1651 case Instruction::FPExt:
1652 case Instruction::UIToFP:
1653 case Instruction::SIToFP:
1654 case Instruction::FPToUI:
1655 case Instruction::FPToSI:
1656 return false; // These always modify bits
1657 case Instruction::BitCast:
1658 return true; // BitCast never modifies bits.
1659 case Instruction::PtrToInt:
1660 return IntPtrTy->getPrimitiveSizeInBits() ==
1661 getType()->getPrimitiveSizeInBits();
1662 case Instruction::IntToPtr:
1663 return IntPtrTy->getPrimitiveSizeInBits() ==
1664 getOperand(0)->getType()->getPrimitiveSizeInBits();
1665 }
1666}
1667
1668/// This function determines if a pair of casts can be eliminated and what
1669/// opcode should be used in the elimination. This assumes that there are two
1670/// instructions like this:
1671/// * %F = firstOpcode SrcTy %x to MidTy
1672/// * %S = secondOpcode MidTy %F to DstTy
1673/// The function returns a resultOpcode so these two casts can be replaced with:
1674/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1675/// If no such cast is permited, the function returns 0.
1676unsigned CastInst::isEliminableCastPair(
1677 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1678 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1679{
1680 // Define the 144 possibilities for these two cast instructions. The values
1681 // in this matrix determine what to do in a given situation and select the
1682 // case in the switch below. The rows correspond to firstOp, the columns
1683 // correspond to secondOp. In looking at the table below, keep in mind
1684 // the following cast properties:
1685 //
1686 // Size Compare Source Destination
1687 // Operator Src ? Size Type Sign Type Sign
1688 // -------- ------------ ------------------- ---------------------
1689 // TRUNC > Integer Any Integral Any
1690 // ZEXT < Integral Unsigned Integer Any
1691 // SEXT < Integral Signed Integer Any
1692 // FPTOUI n/a FloatPt n/a Integral Unsigned
1693 // FPTOSI n/a FloatPt n/a Integral Signed
1694 // UITOFP n/a Integral Unsigned FloatPt n/a
1695 // SITOFP n/a Integral Signed FloatPt n/a
1696 // FPTRUNC > FloatPt n/a FloatPt n/a
1697 // FPEXT < FloatPt n/a FloatPt n/a
1698 // PTRTOINT n/a Pointer n/a Integral Unsigned
1699 // INTTOPTR n/a Integral Unsigned Pointer n/a
1700 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001701 //
1702 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1703 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1704 // into "fptoui double to ulong", but this loses information about the range
1705 // of the produced value (we no longer know the top-part is all zeros).
1706 // Further this conversion is often much more expensive for typical hardware,
1707 // and causes issues when building libgcc. We disallow fptosi+sext for the
1708 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001709 const unsigned numCastOps =
1710 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1711 static const uint8_t CastResults[numCastOps][numCastOps] = {
1712 // T F F U S F F P I B -+
1713 // R Z S P P I I T P 2 N T |
1714 // U E E 2 2 2 2 R E I T C +- secondOp
1715 // N X X U S F F N X N 2 V |
1716 // C T T I I P P C T T P T -+
1717 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1718 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1719 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001720 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1721 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001722 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1723 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1724 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1725 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1726 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1727 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1728 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1729 };
1730
1731 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1732 [secondOp-Instruction::CastOpsBegin];
1733 switch (ElimCase) {
1734 case 0:
1735 // categorically disallowed
1736 return 0;
1737 case 1:
1738 // allowed, use first cast's opcode
1739 return firstOp;
1740 case 2:
1741 // allowed, use second cast's opcode
1742 return secondOp;
1743 case 3:
1744 // no-op cast in second op implies firstOp as long as the DestTy
1745 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001746 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001747 return firstOp;
1748 return 0;
1749 case 4:
1750 // no-op cast in second op implies firstOp as long as the DestTy
1751 // is floating point
1752 if (DstTy->isFloatingPoint())
1753 return firstOp;
1754 return 0;
1755 case 5:
1756 // no-op cast in first op implies secondOp as long as the SrcTy
1757 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001758 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001759 return secondOp;
1760 return 0;
1761 case 6:
1762 // no-op cast in first op implies secondOp as long as the SrcTy
1763 // is a floating point
1764 if (SrcTy->isFloatingPoint())
1765 return secondOp;
1766 return 0;
1767 case 7: {
1768 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1769 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1770 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1771 if (MidSize >= PtrSize)
1772 return Instruction::BitCast;
1773 return 0;
1774 }
1775 case 8: {
1776 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1777 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1778 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1779 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1780 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1781 if (SrcSize == DstSize)
1782 return Instruction::BitCast;
1783 else if (SrcSize < DstSize)
1784 return firstOp;
1785 return secondOp;
1786 }
1787 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1788 return Instruction::ZExt;
1789 case 10:
1790 // fpext followed by ftrunc is allowed if the bit size returned to is
1791 // the same as the original, in which case its just a bitcast
1792 if (SrcTy == DstTy)
1793 return Instruction::BitCast;
1794 return 0; // If the types are not the same we can't eliminate it.
1795 case 11:
1796 // bitcast followed by ptrtoint is allowed as long as the bitcast
1797 // is a pointer to pointer cast.
1798 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1799 return secondOp;
1800 return 0;
1801 case 12:
1802 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1803 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1804 return firstOp;
1805 return 0;
1806 case 13: {
1807 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1808 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1809 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1810 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1811 if (SrcSize <= PtrSize && SrcSize == DstSize)
1812 return Instruction::BitCast;
1813 return 0;
1814 }
1815 case 99:
1816 // cast combination can't happen (error in input). This is for all cases
1817 // where the MidTy is not the same for the two cast instructions.
1818 assert(!"Invalid Cast Combination");
1819 return 0;
1820 default:
1821 assert(!"Error in CastResults table!!!");
1822 return 0;
1823 }
1824 return 0;
1825}
1826
1827CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1828 const std::string &Name, Instruction *InsertBefore) {
1829 // Construct and return the appropriate CastInst subclass
1830 switch (op) {
1831 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1832 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1833 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1834 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1835 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1836 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1837 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1838 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1839 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1840 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1841 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1842 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1843 default:
1844 assert(!"Invalid opcode provided");
1845 }
1846 return 0;
1847}
1848
1849CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1850 const std::string &Name, BasicBlock *InsertAtEnd) {
1851 // Construct and return the appropriate CastInst subclass
1852 switch (op) {
1853 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1854 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1855 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1856 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1857 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1858 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1859 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1860 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1861 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1862 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1863 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1864 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1865 default:
1866 assert(!"Invalid opcode provided");
1867 }
1868 return 0;
1869}
1870
Reid Spencer5c140882006-12-04 20:17:56 +00001871CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1872 const std::string &Name,
1873 Instruction *InsertBefore) {
1874 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1875 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1876 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1877}
1878
1879CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1880 const std::string &Name,
1881 BasicBlock *InsertAtEnd) {
1882 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1883 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1884 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1885}
1886
1887CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1888 const std::string &Name,
1889 Instruction *InsertBefore) {
1890 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1891 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1892 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1893}
1894
1895CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1896 const std::string &Name,
1897 BasicBlock *InsertAtEnd) {
1898 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1899 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1900 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1901}
1902
1903CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1904 const std::string &Name,
1905 Instruction *InsertBefore) {
1906 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1907 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1908 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1909}
1910
1911CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1912 const std::string &Name,
1913 BasicBlock *InsertAtEnd) {
1914 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1915 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1916 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1917}
1918
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001919CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1920 const std::string &Name,
1921 BasicBlock *InsertAtEnd) {
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, InsertAtEnd);
1928 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1929}
1930
1931/// @brief Create a BitCast or a PtrToInt cast instruction
1932CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1933 const std::string &Name,
1934 Instruction *InsertBefore) {
1935 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001936 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001937 "Invalid cast");
1938
Chris Lattner03c49532007-01-15 02:27:26 +00001939 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001940 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1941 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1942}
1943
Reid Spencer7e933472006-12-12 00:49:44 +00001944CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1945 bool isSigned, const std::string &Name,
1946 Instruction *InsertBefore) {
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, InsertBefore);
1955}
1956
1957CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1958 bool isSigned, const std::string &Name,
1959 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001960 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001961 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1962 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1963 Instruction::CastOps opcode =
1964 (SrcBits == DstBits ? Instruction::BitCast :
1965 (SrcBits > DstBits ? Instruction::Trunc :
1966 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1967 return create(opcode, C, Ty, Name, InsertAtEnd);
1968}
1969
1970CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1971 const std::string &Name,
1972 Instruction *InsertBefore) {
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, InsertBefore);
1981}
1982
1983CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1984 const std::string &Name,
1985 BasicBlock *InsertAtEnd) {
1986 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1987 "Invalid cast");
1988 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1989 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1990 Instruction::CastOps opcode =
1991 (SrcBits == DstBits ? Instruction::BitCast :
1992 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1993 return create(opcode, C, Ty, Name, InsertAtEnd);
1994}
1995
Duncan Sands55e50902008-01-06 10:12:28 +00001996// Check whether it is valid to call getCastOpcode for these types.
1997// This routine must be kept in sync with getCastOpcode.
1998bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
1999 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2000 return false;
2001
2002 if (SrcTy == DestTy)
2003 return true;
2004
2005 // Get the bit sizes, we'll need these
2006 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2007 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2008
2009 // Run through the possibilities ...
2010 if (DestTy->isInteger()) { // Casting to integral
2011 if (SrcTy->isInteger()) { // Casting from integral
2012 return true;
2013 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2014 return true;
2015 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2016 // Casting from vector
2017 return DestBits == PTy->getBitWidth();
2018 } else { // Casting from something else
2019 return isa<PointerType>(SrcTy);
2020 }
2021 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2022 if (SrcTy->isInteger()) { // Casting from integral
2023 return true;
2024 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2025 return true;
2026 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
2027 // Casting from vector
2028 return DestBits == PTy->getBitWidth();
2029 } else { // Casting from something else
2030 return false;
2031 }
2032 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2033 // Casting to vector
2034 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2035 // Casting from vector
2036 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
2037 } else { // Casting from something else
2038 return DestPTy->getBitWidth() == SrcBits;
2039 }
2040 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2041 if (isa<PointerType>(SrcTy)) { // Casting from pointer
2042 return true;
2043 } else if (SrcTy->isInteger()) { // Casting from integral
2044 return true;
2045 } else { // Casting from something else
2046 return false;
2047 }
2048 } else { // Casting to something else
2049 return false;
2050 }
2051}
2052
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002053// Provide a way to get a "cast" where the cast opcode is inferred from the
2054// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002055// logic in the castIsValid function below. This axiom should hold:
2056// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2057// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002058// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002059// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002060Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002061CastInst::getCastOpcode(
2062 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002063 // Get the bit sizes, we'll need these
2064 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002065 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2066 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002067
Duncan Sands55e50902008-01-06 10:12:28 +00002068 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2069 "Only first class types are castable!");
2070
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002071 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002072 if (DestTy->isInteger()) { // Casting to integral
2073 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002074 if (DestBits < SrcBits)
2075 return Trunc; // int -> smaller int
2076 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002077 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002078 return SExt; // signed -> SEXT
2079 else
2080 return ZExt; // unsigned -> ZEXT
2081 } else {
2082 return BitCast; // Same size, No-op cast
2083 }
2084 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002085 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002086 return FPToSI; // FP -> sint
2087 else
2088 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002089 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002090 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002091 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002092 return BitCast; // Same size, no-op cast
2093 } else {
2094 assert(isa<PointerType>(SrcTy) &&
2095 "Casting from a value that is not first-class type");
2096 return PtrToInt; // ptr -> int
2097 }
2098 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002099 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002100 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002101 return SIToFP; // sint -> FP
2102 else
2103 return UIToFP; // uint -> FP
2104 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2105 if (DestBits < SrcBits) {
2106 return FPTrunc; // FP -> smaller FP
2107 } else if (DestBits > SrcBits) {
2108 return FPExt; // FP -> larger FP
2109 } else {
2110 return BitCast; // same size, no-op cast
2111 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002112 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002113 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002114 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002115 return BitCast; // same size, no-op cast
2116 } else {
2117 assert(0 && "Casting pointer or non-first class to float");
2118 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002119 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2120 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002121 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002122 "Casting vector to vector of different widths");
2123 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002124 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002125 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002126 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002127 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002128 }
2129 } else if (isa<PointerType>(DestTy)) {
2130 if (isa<PointerType>(SrcTy)) {
2131 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002132 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002133 return IntToPtr; // int -> ptr
2134 } else {
2135 assert(!"Casting pointer to other than pointer or int");
2136 }
2137 } else {
2138 assert(!"Casting to type that is not first-class");
2139 }
2140
2141 // If we fall through to here we probably hit an assertion cast above
2142 // and assertions are not turned on. Anything we return is an error, so
2143 // BitCast is as good a choice as any.
2144 return BitCast;
2145}
2146
2147//===----------------------------------------------------------------------===//
2148// CastInst SubClass Constructors
2149//===----------------------------------------------------------------------===//
2150
2151/// Check that the construction parameters for a CastInst are correct. This
2152/// could be broken out into the separate constructors but it is useful to have
2153/// it in one place and to eliminate the redundant code for getting the sizes
2154/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002155bool
2156CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002157
2158 // Check for type sanity on the arguments
2159 const Type *SrcTy = S->getType();
2160 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2161 return false;
2162
2163 // Get the size of the types in bits, we'll need this later
2164 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2165 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2166
2167 // Switch on the opcode provided
2168 switch (op) {
2169 default: return false; // This is an input error
2170 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002171 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002172 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002173 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002174 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002175 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002176 case Instruction::FPTrunc:
2177 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2178 SrcBitSize > DstBitSize;
2179 case Instruction::FPExt:
2180 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2181 SrcBitSize < DstBitSize;
2182 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002183 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002184 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2185 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2186 return SVTy->getElementType()->isInteger() &&
2187 DVTy->getElementType()->isFloatingPoint() &&
2188 SVTy->getNumElements() == DVTy->getNumElements();
2189 }
2190 }
Chris Lattner03c49532007-01-15 02:27:26 +00002191 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002192 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002193 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002194 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2195 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2196 return SVTy->getElementType()->isFloatingPoint() &&
2197 DVTy->getElementType()->isInteger() &&
2198 SVTy->getNumElements() == DVTy->getNumElements();
2199 }
2200 }
Chris Lattner03c49532007-01-15 02:27:26 +00002201 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002202 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002203 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002204 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002205 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002206 case Instruction::BitCast:
2207 // BitCast implies a no-op cast of type only. No bits change.
2208 // However, you can't cast pointers to anything but pointers.
2209 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2210 return false;
2211
Duncan Sands55e50902008-01-06 10:12:28 +00002212 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002213 // these cases, the cast is okay if the source and destination bit widths
2214 // are identical.
2215 return SrcBitSize == DstBitSize;
2216 }
2217}
2218
2219TruncInst::TruncInst(
2220 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2221) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002222 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002223}
2224
2225TruncInst::TruncInst(
2226 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2227) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002228 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002229}
2230
2231ZExtInst::ZExtInst(
2232 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2233) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002234 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002235}
2236
2237ZExtInst::ZExtInst(
2238 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2239) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002240 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002241}
2242SExtInst::SExtInst(
2243 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2244) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002245 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002246}
2247
Jeff Cohencc08c832006-12-02 02:22:01 +00002248SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002249 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2250) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002251 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002252}
2253
2254FPTruncInst::FPTruncInst(
2255 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2256) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002257 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002258}
2259
2260FPTruncInst::FPTruncInst(
2261 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2262) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002263 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002264}
2265
2266FPExtInst::FPExtInst(
2267 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2268) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002269 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002270}
2271
2272FPExtInst::FPExtInst(
2273 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2274) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002275 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002276}
2277
2278UIToFPInst::UIToFPInst(
2279 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2280) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002281 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002282}
2283
2284UIToFPInst::UIToFPInst(
2285 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2286) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002287 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002288}
2289
2290SIToFPInst::SIToFPInst(
2291 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2292) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002293 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002294}
2295
2296SIToFPInst::SIToFPInst(
2297 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2298) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002299 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002300}
2301
2302FPToUIInst::FPToUIInst(
2303 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2304) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002305 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306}
2307
2308FPToUIInst::FPToUIInst(
2309 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2310) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002311 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002312}
2313
2314FPToSIInst::FPToSIInst(
2315 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2316) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002317 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002318}
2319
2320FPToSIInst::FPToSIInst(
2321 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2322) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002323 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324}
2325
2326PtrToIntInst::PtrToIntInst(
2327 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2328) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002329 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002330}
2331
2332PtrToIntInst::PtrToIntInst(
2333 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2334) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002335 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002336}
2337
2338IntToPtrInst::IntToPtrInst(
2339 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2340) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002341 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002342}
2343
2344IntToPtrInst::IntToPtrInst(
2345 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2346) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002347 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002348}
2349
2350BitCastInst::BitCastInst(
2351 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2352) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002353 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002354}
2355
2356BitCastInst::BitCastInst(
2357 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2358) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002359 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002360}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002361
2362//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002363// CmpInst Classes
2364//===----------------------------------------------------------------------===//
2365
2366CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2367 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002368 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002369 Ops[0].init(LHS, this);
2370 Ops[1].init(RHS, this);
2371 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002372 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002373 if (op == Instruction::ICmp) {
2374 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2375 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2376 "Invalid ICmp predicate value");
2377 const Type* Op0Ty = getOperand(0)->getType();
2378 const Type* Op1Ty = getOperand(1)->getType();
2379 assert(Op0Ty == Op1Ty &&
2380 "Both operands to ICmp instruction are not of the same type!");
2381 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002382 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002383 "Invalid operand types for ICmp instruction");
2384 return;
2385 }
2386 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2387 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2388 "Invalid FCmp predicate value");
2389 const Type* Op0Ty = getOperand(0)->getType();
2390 const Type* Op1Ty = getOperand(1)->getType();
2391 assert(Op0Ty == Op1Ty &&
2392 "Both operands to FCmp instruction are not of the same type!");
2393 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002394 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002395 "Invalid operand types for FCmp instruction");
2396}
2397
2398CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2399 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002400 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002401 Ops[0].init(LHS, this);
2402 Ops[1].init(RHS, this);
2403 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002404 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002405 if (op == Instruction::ICmp) {
2406 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2407 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2408 "Invalid ICmp predicate value");
2409
2410 const Type* Op0Ty = getOperand(0)->getType();
2411 const Type* Op1Ty = getOperand(1)->getType();
2412 assert(Op0Ty == Op1Ty &&
2413 "Both operands to ICmp instruction are not of the same type!");
2414 // Check that the operands are the right type
Anton Korobeynikov579f0712008-02-20 11:08:44 +00002415 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002416 "Invalid operand types for ICmp instruction");
2417 return;
2418 }
2419 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2420 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2421 "Invalid FCmp predicate value");
2422 const Type* Op0Ty = getOperand(0)->getType();
2423 const Type* Op1Ty = getOperand(1)->getType();
2424 assert(Op0Ty == Op1Ty &&
2425 "Both operands to FCmp instruction are not of the same type!");
2426 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002427 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002428 "Invalid operand types for FCmp instruction");
2429}
2430
2431CmpInst *
2432CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2433 const std::string &Name, Instruction *InsertBefore) {
2434 if (Op == Instruction::ICmp) {
2435 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2436 InsertBefore);
2437 }
2438 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2439 InsertBefore);
2440}
2441
2442CmpInst *
2443CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2444 const std::string &Name, BasicBlock *InsertAtEnd) {
2445 if (Op == Instruction::ICmp) {
2446 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2447 InsertAtEnd);
2448 }
2449 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2450 InsertAtEnd);
2451}
2452
2453void CmpInst::swapOperands() {
2454 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2455 IC->swapOperands();
2456 else
2457 cast<FCmpInst>(this)->swapOperands();
2458}
2459
2460bool CmpInst::isCommutative() {
2461 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2462 return IC->isCommutative();
2463 return cast<FCmpInst>(this)->isCommutative();
2464}
2465
2466bool CmpInst::isEquality() {
2467 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2468 return IC->isEquality();
2469 return cast<FCmpInst>(this)->isEquality();
2470}
2471
2472
2473ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2474 switch (pred) {
2475 default:
2476 assert(!"Unknown icmp predicate!");
2477 case ICMP_EQ: return ICMP_NE;
2478 case ICMP_NE: return ICMP_EQ;
2479 case ICMP_UGT: return ICMP_ULE;
2480 case ICMP_ULT: return ICMP_UGE;
2481 case ICMP_UGE: return ICMP_ULT;
2482 case ICMP_ULE: return ICMP_UGT;
2483 case ICMP_SGT: return ICMP_SLE;
2484 case ICMP_SLT: return ICMP_SGE;
2485 case ICMP_SGE: return ICMP_SLT;
2486 case ICMP_SLE: return ICMP_SGT;
2487 }
2488}
2489
2490ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2491 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002492 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002493 case ICMP_EQ: case ICMP_NE:
2494 return pred;
2495 case ICMP_SGT: return ICMP_SLT;
2496 case ICMP_SLT: return ICMP_SGT;
2497 case ICMP_SGE: return ICMP_SLE;
2498 case ICMP_SLE: return ICMP_SGE;
2499 case ICMP_UGT: return ICMP_ULT;
2500 case ICMP_ULT: return ICMP_UGT;
2501 case ICMP_UGE: return ICMP_ULE;
2502 case ICMP_ULE: return ICMP_UGE;
2503 }
2504}
2505
Reid Spencer266e42b2006-12-23 06:05:41 +00002506ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2507 switch (pred) {
2508 default: assert(! "Unknown icmp predicate!");
2509 case ICMP_EQ: case ICMP_NE:
2510 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2511 return pred;
2512 case ICMP_UGT: return ICMP_SGT;
2513 case ICMP_ULT: return ICMP_SLT;
2514 case ICMP_UGE: return ICMP_SGE;
2515 case ICMP_ULE: return ICMP_SLE;
2516 }
2517}
2518
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002519ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2520 switch (pred) {
2521 default: assert(! "Unknown icmp predicate!");
2522 case ICMP_EQ: case ICMP_NE:
2523 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2524 return pred;
2525 case ICMP_SGT: return ICMP_UGT;
2526 case ICMP_SLT: return ICMP_ULT;
2527 case ICMP_SGE: return ICMP_UGE;
2528 case ICMP_SLE: return ICMP_ULE;
2529 }
2530}
2531
Reid Spencer266e42b2006-12-23 06:05:41 +00002532bool ICmpInst::isSignedPredicate(Predicate pred) {
2533 switch (pred) {
2534 default: assert(! "Unknown icmp predicate!");
2535 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2536 return true;
2537 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2538 case ICMP_UGE: case ICMP_ULE:
2539 return false;
2540 }
2541}
2542
Reid Spencer0286bc12007-02-28 22:00:54 +00002543/// Initialize a set of values that all satisfy the condition with C.
2544///
2545ConstantRange
2546ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2547 APInt Lower(C);
2548 APInt Upper(C);
2549 uint32_t BitWidth = C.getBitWidth();
2550 switch (pred) {
2551 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2552 case ICmpInst::ICMP_EQ: Upper++; break;
2553 case ICmpInst::ICMP_NE: Lower++; break;
2554 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2555 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2556 case ICmpInst::ICMP_UGT:
2557 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2558 break;
2559 case ICmpInst::ICMP_SGT:
2560 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2561 break;
2562 case ICmpInst::ICMP_ULE:
2563 Lower = APInt::getMinValue(BitWidth); Upper++;
2564 break;
2565 case ICmpInst::ICMP_SLE:
2566 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2567 break;
2568 case ICmpInst::ICMP_UGE:
2569 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2570 break;
2571 case ICmpInst::ICMP_SGE:
2572 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2573 break;
2574 }
2575 return ConstantRange(Lower, Upper);
2576}
2577
Reid Spencerd9436b62006-11-20 01:22:35 +00002578FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2579 switch (pred) {
2580 default:
2581 assert(!"Unknown icmp predicate!");
2582 case FCMP_OEQ: return FCMP_UNE;
2583 case FCMP_ONE: return FCMP_UEQ;
2584 case FCMP_OGT: return FCMP_ULE;
2585 case FCMP_OLT: return FCMP_UGE;
2586 case FCMP_OGE: return FCMP_ULT;
2587 case FCMP_OLE: return FCMP_UGT;
2588 case FCMP_UEQ: return FCMP_ONE;
2589 case FCMP_UNE: return FCMP_OEQ;
2590 case FCMP_UGT: return FCMP_OLE;
2591 case FCMP_ULT: return FCMP_OGE;
2592 case FCMP_UGE: return FCMP_OLT;
2593 case FCMP_ULE: return FCMP_OGT;
2594 case FCMP_ORD: return FCMP_UNO;
2595 case FCMP_UNO: return FCMP_ORD;
2596 case FCMP_TRUE: return FCMP_FALSE;
2597 case FCMP_FALSE: return FCMP_TRUE;
2598 }
2599}
2600
2601FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2602 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002603 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002604 case FCMP_FALSE: case FCMP_TRUE:
2605 case FCMP_OEQ: case FCMP_ONE:
2606 case FCMP_UEQ: case FCMP_UNE:
2607 case FCMP_ORD: case FCMP_UNO:
2608 return pred;
2609 case FCMP_OGT: return FCMP_OLT;
2610 case FCMP_OLT: return FCMP_OGT;
2611 case FCMP_OGE: return FCMP_OLE;
2612 case FCMP_OLE: return FCMP_OGE;
2613 case FCMP_UGT: return FCMP_ULT;
2614 case FCMP_ULT: return FCMP_UGT;
2615 case FCMP_UGE: return FCMP_ULE;
2616 case FCMP_ULE: return FCMP_UGE;
2617 }
2618}
2619
Reid Spencer266e42b2006-12-23 06:05:41 +00002620bool CmpInst::isUnsigned(unsigned short predicate) {
2621 switch (predicate) {
2622 default: return false;
2623 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2624 case ICmpInst::ICMP_UGE: return true;
2625 }
2626}
2627
2628bool CmpInst::isSigned(unsigned short predicate){
2629 switch (predicate) {
2630 default: return false;
2631 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2632 case ICmpInst::ICMP_SGE: return true;
2633 }
2634}
2635
2636bool CmpInst::isOrdered(unsigned short predicate) {
2637 switch (predicate) {
2638 default: return false;
2639 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2640 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2641 case FCmpInst::FCMP_ORD: return true;
2642 }
2643}
2644
2645bool CmpInst::isUnordered(unsigned short predicate) {
2646 switch (predicate) {
2647 default: return false;
2648 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2649 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2650 case FCmpInst::FCMP_UNO: return true;
2651 }
2652}
2653
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002654//===----------------------------------------------------------------------===//
2655// SwitchInst Implementation
2656//===----------------------------------------------------------------------===//
2657
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002658void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002659 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002660 ReservedSpace = 2+NumCases*2;
2661 NumOperands = 2;
2662 OperandList = new Use[ReservedSpace];
2663
2664 OperandList[0].init(Value, this);
2665 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002666}
2667
Chris Lattner2195fc42007-02-24 00:55:48 +00002668/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2669/// switch on and a default destination. The number of additional cases can
2670/// be specified here to make memory allocation more efficient. This
2671/// constructor can also autoinsert before another instruction.
2672SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2673 Instruction *InsertBefore)
2674 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2675 init(Value, Default, NumCases);
2676}
2677
2678/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2679/// switch on and a default destination. The number of additional cases can
2680/// be specified here to make memory allocation more efficient. This
2681/// constructor also autoinserts at the end of the specified BasicBlock.
2682SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2683 BasicBlock *InsertAtEnd)
2684 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2685 init(Value, Default, NumCases);
2686}
2687
Misha Brukmanb1c93172005-04-21 23:48:37 +00002688SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002689 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2690 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002691 Use *OL = OperandList, *InOL = SI.OperandList;
2692 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2693 OL[i].init(InOL[i], this);
2694 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002695 }
2696}
2697
Gordon Henriksen14a55692007-12-10 02:14:30 +00002698SwitchInst::~SwitchInst() {
2699 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002700}
2701
2702
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002703/// addCase - Add an entry to the switch instruction...
2704///
Chris Lattner47ac1872005-02-24 05:32:09 +00002705void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002706 unsigned OpNo = NumOperands;
2707 if (OpNo+2 > ReservedSpace)
2708 resizeOperands(0); // Get more space!
2709 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002710 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002711 NumOperands = OpNo+2;
2712 OperandList[OpNo].init(OnVal, this);
2713 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002714}
2715
2716/// removeCase - This method removes the specified successor from the switch
2717/// instruction. Note that this cannot be used to remove the default
2718/// destination (successor #0).
2719///
2720void SwitchInst::removeCase(unsigned idx) {
2721 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002722 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2723
2724 unsigned NumOps = getNumOperands();
2725 Use *OL = OperandList;
2726
2727 // Move everything after this operand down.
2728 //
2729 // FIXME: we could just swap with the end of the list, then erase. However,
2730 // client might not expect this to happen. The code as it is thrashes the
2731 // use/def lists, which is kinda lame.
2732 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2733 OL[i-2] = OL[i];
2734 OL[i-2+1] = OL[i+1];
2735 }
2736
2737 // Nuke the last value.
2738 OL[NumOps-2].set(0);
2739 OL[NumOps-2+1].set(0);
2740 NumOperands = NumOps-2;
2741}
2742
2743/// resizeOperands - resize operands - This adjusts the length of the operands
2744/// list according to the following behavior:
2745/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2746/// of operation. This grows the number of ops by 1.5 times.
2747/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2748/// 3. If NumOps == NumOperands, trim the reserved space.
2749///
2750void SwitchInst::resizeOperands(unsigned NumOps) {
2751 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002752 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002753 } else if (NumOps*2 > NumOperands) {
2754 // No resize needed.
2755 if (ReservedSpace >= NumOps) return;
2756 } else if (NumOps == NumOperands) {
2757 if (ReservedSpace == NumOps) return;
2758 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002759 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002760 }
2761
2762 ReservedSpace = NumOps;
2763 Use *NewOps = new Use[NumOps];
2764 Use *OldOps = OperandList;
2765 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2766 NewOps[i].init(OldOps[i], this);
2767 OldOps[i].set(0);
2768 }
2769 delete [] OldOps;
2770 OperandList = NewOps;
2771}
2772
2773
2774BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2775 return getSuccessor(idx);
2776}
2777unsigned SwitchInst::getNumSuccessorsV() const {
2778 return getNumSuccessors();
2779}
2780void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2781 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002782}
Chris Lattnerf22be932004-10-15 23:52:53 +00002783
Devang Patel295711f2008-02-19 22:15:16 +00002784//===----------------------------------------------------------------------===//
2785// GetResultInst Implementation
2786//===----------------------------------------------------------------------===//
2787
Devang Patel666d4512008-02-20 19:10:47 +00002788GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002789 const std::string &Name,
2790 Instruction *InsertBef)
Devang Pateldcad9da2008-02-20 19:26:55 +00002791 : Instruction(cast<StructType>(Aggregate->getType())->getElementType(Index),
Devang Patel666d4512008-02-20 19:10:47 +00002792 GetResult, &Aggr, 1, InsertBef) {
2793 assert(isValidOperands(Aggregate, Index) && "Invalid GetResultInst operands!");
2794 Aggr.init(Aggregate, this);
2795 Idx = Index;
Devang Patel295711f2008-02-19 22:15:16 +00002796 setName(Name);
2797}
2798
Devang Patel666d4512008-02-20 19:10:47 +00002799bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2800 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002801 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002802
Devang Patelcf193b82008-02-20 19:39:41 +00002803 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2804 unsigned NumElements = STy->getNumElements();
2805 if (Index >= NumElements)
2806 return false;
2807
2808 // getresult aggregate value's element types are restricted to
2809 // avoid nested aggregates.
2810 for (unsigned i = 0; i < NumElements; ++i)
2811 if (!STy->getElementType(i)->isFirstClassType())
2812 return false;
2813
2814 // Otherwise, Aggregate is valid.
2815 return true;
2816 }
Devang Patel666d4512008-02-20 19:10:47 +00002817 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002818}
2819
Chris Lattnerf22be932004-10-15 23:52:53 +00002820// Define these methods here so vtables don't get emitted into every translation
2821// unit that uses these classes.
2822
2823GetElementPtrInst *GetElementPtrInst::clone() const {
2824 return new GetElementPtrInst(*this);
2825}
2826
2827BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002828 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002829}
2830
Chris Lattner0b490b02007-08-24 20:48:18 +00002831FCmpInst* FCmpInst::clone() const {
2832 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2833}
2834ICmpInst* ICmpInst::clone() const {
2835 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002836}
2837
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002838MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2839AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2840FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2841LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2842StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2843CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2844CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2845CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2846CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2847CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2848CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2849CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2850CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2851CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2852CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2853CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2854CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2855CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002856SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2857VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2858
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002859ExtractElementInst *ExtractElementInst::clone() const {
2860 return new ExtractElementInst(*this);
2861}
2862InsertElementInst *InsertElementInst::clone() const {
2863 return new InsertElementInst(*this);
2864}
2865ShuffleVectorInst *ShuffleVectorInst::clone() const {
2866 return new ShuffleVectorInst(*this);
2867}
Chris Lattnerf22be932004-10-15 23:52:53 +00002868PHINode *PHINode::clone() const { return new PHINode(*this); }
2869ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2870BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2871SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2872InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2873UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002874UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002875GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }