blob: 78e7b17a66930d72104b3161cc78162df3e9a4c4 [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"
20#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000021#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000022#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023using namespace llvm;
24
Chris Lattner3e13b8c2008-01-02 23:42:30 +000025//===----------------------------------------------------------------------===//
26// CallSite Class
27//===----------------------------------------------------------------------===//
28
Duncan Sands85fab3a2008-02-18 17:32:13 +000029CallSite::CallSite(Instruction *C) {
30 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
31 I = C;
32}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000033unsigned CallSite::getCallingConv() const {
34 if (CallInst *CI = dyn_cast<CallInst>(I))
35 return CI->getCallingConv();
36 else
37 return cast<InvokeInst>(I)->getCallingConv();
38}
39void CallSite::setCallingConv(unsigned CC) {
40 if (CallInst *CI = dyn_cast<CallInst>(I))
41 CI->setCallingConv(CC);
42 else
43 cast<InvokeInst>(I)->setCallingConv(CC);
44}
Chris Lattner8a923e72008-03-12 17:45:29 +000045const PAListPtr &CallSite::getParamAttrs() const {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000046 if (CallInst *CI = dyn_cast<CallInst>(I))
47 return CI->getParamAttrs();
48 else
49 return cast<InvokeInst>(I)->getParamAttrs();
50}
Chris Lattner8a923e72008-03-12 17:45:29 +000051void CallSite::setParamAttrs(const PAListPtr &PAL) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000052 if (CallInst *CI = dyn_cast<CallInst>(I))
53 CI->setParamAttrs(PAL);
54 else
55 cast<InvokeInst>(I)->setParamAttrs(PAL);
56}
Dale Johannesen89268bc2008-02-19 21:38:47 +000057bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
Duncan Sands5208d1a2007-11-28 17:07:01 +000058 if (CallInst *CI = dyn_cast<CallInst>(I))
Dale Johannesen89268bc2008-02-19 21:38:47 +000059 return CI->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000060 else
Dale Johannesen89268bc2008-02-19 21:38:47 +000061 return cast<InvokeInst>(I)->paramHasAttr(i, attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000062}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000063uint16_t CallSite::getParamAlignment(uint16_t i) const {
64 if (CallInst *CI = dyn_cast<CallInst>(I))
65 return CI->getParamAlignment(i);
66 else
67 return cast<InvokeInst>(I)->getParamAlignment(i);
68}
69
Duncan Sands38ef3a82007-12-03 20:06:50 +000070bool CallSite::doesNotAccessMemory() const {
71 if (CallInst *CI = dyn_cast<CallInst>(I))
72 return CI->doesNotAccessMemory();
73 else
74 return cast<InvokeInst>(I)->doesNotAccessMemory();
75}
76bool CallSite::onlyReadsMemory() const {
77 if (CallInst *CI = dyn_cast<CallInst>(I))
78 return CI->onlyReadsMemory();
79 else
80 return cast<InvokeInst>(I)->onlyReadsMemory();
81}
Duncan Sands3353ed02007-12-18 09:59:50 +000082bool CallSite::doesNotThrow() const {
Duncan Sands8e4847e2007-12-16 15:51:49 +000083 if (CallInst *CI = dyn_cast<CallInst>(I))
Duncan Sands3353ed02007-12-18 09:59:50 +000084 return CI->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000085 else
Duncan Sands3353ed02007-12-18 09:59:50 +000086 return cast<InvokeInst>(I)->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000087}
Duncan Sandsaa31b922007-12-19 21:13:37 +000088void CallSite::setDoesNotThrow(bool doesNotThrow) {
89 if (CallInst *CI = dyn_cast<CallInst>(I))
90 CI->setDoesNotThrow(doesNotThrow);
91 else
92 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
93}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000094
Gordon Henriksen14a55692007-12-10 02:14:30 +000095//===----------------------------------------------------------------------===//
96// TerminatorInst Class
97//===----------------------------------------------------------------------===//
98
99// Out of line virtual method, so the vtable, etc has a home.
100TerminatorInst::~TerminatorInst() {
101}
102
Gabor Greiff6caff662008-05-10 08:32:32 +0000103//===----------------------------------------------------------------------===//
104// UnaryInstruction Class
105//===----------------------------------------------------------------------===//
106
Gordon Henriksen14a55692007-12-10 02:14:30 +0000107// Out of line virtual method, so the vtable, etc has a home.
108UnaryInstruction::~UnaryInstruction() {
109}
110
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000111//===----------------------------------------------------------------------===//
112// PHINode Class
113//===----------------------------------------------------------------------===//
114
115PHINode::PHINode(const PHINode &PN)
116 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000117 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000118 ReservedSpace(PN.getNumOperands()) {
119 Use *OL = OperandList;
120 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
121 OL[i].init(PN.getOperand(i), this);
122 OL[i+1].init(PN.getOperand(i+1), this);
123 }
124}
125
Gordon Henriksen14a55692007-12-10 02:14:30 +0000126PHINode::~PHINode() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000127 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000128}
129
130// removeIncomingValue - Remove an incoming value. This is useful if a
131// predecessor basic block is deleted.
132Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
133 unsigned NumOps = getNumOperands();
134 Use *OL = OperandList;
135 assert(Idx*2 < NumOps && "BB not in PHI node!");
136 Value *Removed = OL[Idx*2];
137
138 // Move everything after this operand down.
139 //
140 // FIXME: we could just swap with the end of the list, then erase. However,
141 // client might not expect this to happen. The code as it is thrashes the
142 // use/def lists, which is kinda lame.
143 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
144 OL[i-2] = OL[i];
145 OL[i-2+1] = OL[i+1];
146 }
147
148 // Nuke the last value.
149 OL[NumOps-2].set(0);
150 OL[NumOps-2+1].set(0);
151 NumOperands = NumOps-2;
152
153 // If the PHI node is dead, because it has zero entries, nuke it now.
154 if (NumOps == 2 && DeletePHIIfEmpty) {
155 // If anyone is using this PHI, make them use a dummy value instead...
156 replaceAllUsesWith(UndefValue::get(getType()));
157 eraseFromParent();
158 }
159 return Removed;
160}
161
162/// resizeOperands - resize operands - This adjusts the length of the operands
163/// list according to the following behavior:
164/// 1. If NumOps == 0, grow the operand list in response to a push_back style
165/// of operation. This grows the number of ops by 1.5 times.
166/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
167/// 3. If NumOps == NumOperands, trim the reserved space.
168///
169void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000170 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000171 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000172 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000173 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
174 } else if (NumOps*2 > NumOperands) {
175 // No resize needed.
176 if (ReservedSpace >= NumOps) return;
177 } else if (NumOps == NumOperands) {
178 if (ReservedSpace == NumOps) return;
179 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000180 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000181 }
182
183 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000184 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000185 Use *NewOps = allocHungoffUses(NumOps);
186 for (unsigned i = 0; i != e; ++i) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000187 NewOps[i].init(OldOps[i], this);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000188 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000189 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000190 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000191}
192
Nate Begemanb3923212005-08-04 23:24:19 +0000193/// hasConstantValue - If the specified PHI node always merges together the same
194/// value, return the value, otherwise return null.
195///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000196Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000197 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000198 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000199 if (getIncomingValue(0) != this) // not X = phi X
200 return getIncomingValue(0);
201 else
202 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000203 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000204
Nate Begemanb3923212005-08-04 23:24:19 +0000205 // Otherwise if all of the incoming values are the same for the PHI, replace
206 // the PHI node with the incoming value.
207 //
208 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000209 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000210 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000211 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000212 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000213 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000214 if (InVal && getIncomingValue(i) != InVal)
215 return 0; // Not the same, bail out.
216 else
217 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000218 }
Nate Begemanb3923212005-08-04 23:24:19 +0000219
220 // The only case that could cause InVal to be null is if we have a PHI node
221 // that only has entries for itself. In this case, there is no entry into the
222 // loop, so kill the PHI.
223 //
224 if (InVal == 0) InVal = UndefValue::get(getType());
225
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000226 // If we have a PHI node like phi(X, undef, X), where X is defined by some
227 // instruction, we cannot always return X as the result of the PHI node. Only
228 // do this if X is not an instruction (thus it must dominate the PHI block),
229 // or if the client is prepared to deal with this possibility.
230 if (HasUndefInput && !AllowNonDominatingInstruction)
231 if (Instruction *IV = dyn_cast<Instruction>(InVal))
232 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000233 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000234 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000235 return 0; // Cannot guarantee that InVal dominates this PHINode.
236
Nate Begemanb3923212005-08-04 23:24:19 +0000237 // All of the incoming values are the same, return the value now.
238 return InVal;
239}
240
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000241
242//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000243// CallInst Implementation
244//===----------------------------------------------------------------------===//
245
Gordon Henriksen14a55692007-12-10 02:14:30 +0000246CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000247}
248
Chris Lattner054ba2c2007-02-13 00:58:44 +0000249void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000250 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
251 Use *OL = OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000252 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253
Misha Brukmanb1c93172005-04-21 23:48:37 +0000254 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000255 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000256 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000257
Chris Lattner054ba2c2007-02-13 00:58:44 +0000258 assert((NumParams == FTy->getNumParams() ||
259 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000260 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000261 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000262 assert((i >= FTy->getNumParams() ||
263 FTy->getParamType(i) == Params[i]->getType()) &&
264 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000265 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000266 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000267}
268
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000269void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000270 assert(NumOperands == 3 && "NumOperands not set up?");
271 Use *OL = OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000272 OL[0].init(Func, this);
273 OL[1].init(Actual1, this);
274 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000275
276 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000277 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000278 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000280 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000281 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000282 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000283 assert((0 >= FTy->getNumParams() ||
284 FTy->getParamType(0) == Actual1->getType()) &&
285 "Calling a function with a bad signature!");
286 assert((1 >= FTy->getNumParams() ||
287 FTy->getParamType(1) == Actual2->getType()) &&
288 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289}
290
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000291void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000292 assert(NumOperands == 2 && "NumOperands not set up?");
293 Use *OL = OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000294 OL[0].init(Func, this);
295 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000296
297 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000299 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000301 assert((FTy->getNumParams() == 1 ||
302 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000304 assert((0 == FTy->getNumParams() ||
305 FTy->getParamType(0) == Actual->getType()) &&
306 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307}
308
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000309void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000310 assert(NumOperands == 1 && "NumOperands not set up?");
311 Use *OL = OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000312 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000313
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000314 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000316 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000317
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000318 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000319}
320
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000321CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000322 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
324 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000325 Instruction::Call,
326 OperandTraits<CallInst>::op_end(this) - 2,
327 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000328 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000329 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330}
331
332CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
333 BasicBlock *InsertAtEnd)
334 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
335 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000336 Instruction::Call,
337 OperandTraits<CallInst>::op_end(this) - 2,
338 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000339 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000340 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000342CallInst::CallInst(Value *Func, const std::string &Name,
343 Instruction *InsertBefore)
344 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
345 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000346 Instruction::Call,
347 OperandTraits<CallInst>::op_end(this) - 1,
348 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000349 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000350 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000351}
352
353CallInst::CallInst(Value *Func, const std::string &Name,
354 BasicBlock *InsertAtEnd)
355 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
356 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000357 Instruction::Call,
358 OperandTraits<CallInst>::op_end(this) - 1,
359 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000360 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000361 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362}
363
Misha Brukmanb1c93172005-04-21 23:48:37 +0000364CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000365 : Instruction(CI.getType(), Instruction::Call,
366 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000367 CI.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000368 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000369 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000370 Use *OL = OperandList;
371 Use *InOL = CI.OperandList;
372 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
373 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000374}
375
Chris Lattnerc994c022008-03-13 05:00:21 +0000376bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000377 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000378 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000379 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000380 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000381 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000382}
383
Duncan Sandsaa31b922007-12-19 21:13:37 +0000384void CallInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000385 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000386 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000387 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000388 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000389 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000390 setParamAttrs(PAL);
391}
392
Duncan Sands5208d1a2007-11-28 17:07:01 +0000393
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000394//===----------------------------------------------------------------------===//
395// InvokeInst Implementation
396//===----------------------------------------------------------------------===//
397
398void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000399 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000400 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
401 Use *OL = OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000402 OL[0].init(Fn, this);
403 OL[1].init(IfNormal, this);
404 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000405 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000406 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000407 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000408
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000409 assert(((NumArgs == FTy->getNumParams()) ||
410 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000411 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000412
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000413 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000414 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000415 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000416 "Invoking a function with a bad signature!");
417
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000418 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000419 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000420}
421
Misha Brukmanb1c93172005-04-21 23:48:37 +0000422InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000423 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greiff6caff662008-05-10 08:32:32 +0000424 OperandTraits<InvokeInst>::op_end(this) - II.getNumOperands(),
425 II.getNumOperands()) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000426 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000427 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000428 Use *OL = OperandList, *InOL = II.OperandList;
429 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
430 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000431}
432
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000433BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
434 return getSuccessor(idx);
435}
436unsigned InvokeInst::getNumSuccessorsV() const {
437 return getNumSuccessors();
438}
439void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
440 return setSuccessor(idx, B);
441}
442
Chris Lattnerc994c022008-03-13 05:00:21 +0000443bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
Chris Lattner8a923e72008-03-12 17:45:29 +0000444 if (ParamAttrs.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000445 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000446 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000447 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000448 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000449}
450
Duncan Sandsaa31b922007-12-19 21:13:37 +0000451void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000452 PAListPtr PAL = getParamAttrs();
Duncan Sandsaa31b922007-12-19 21:13:37 +0000453 if (doesNotThrow)
Chris Lattner8a923e72008-03-12 17:45:29 +0000454 PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000455 else
Chris Lattner8a923e72008-03-12 17:45:29 +0000456 PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000457 setParamAttrs(PAL);
458}
459
Duncan Sands5208d1a2007-11-28 17:07:01 +0000460
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000461//===----------------------------------------------------------------------===//
462// ReturnInst Implementation
463//===----------------------------------------------------------------------===//
464
Chris Lattner2195fc42007-02-24 00:55:48 +0000465ReturnInst::ReturnInst(const ReturnInst &RI)
466 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Gabor Greiff6caff662008-05-10 08:32:32 +0000467 OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(),
468 RI.getNumOperands()) {
Devang Patel59643e52008-02-23 00:35:18 +0000469 unsigned N = RI.getNumOperands();
Gabor Greiff6caff662008-05-10 08:32:32 +0000470 if (N == 1)
471 Op<0>().init(RI.Op<0>(), this);
Devang Patelae682fb2008-02-26 17:56:20 +0000472 else if (N) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000473 Use *OL = OperandList;
Devang Patelae682fb2008-02-26 17:56:20 +0000474 for (unsigned i = 0; i < N; ++i)
475 OL[i].init(RI.getOperand(i), this);
476 }
Chris Lattner2195fc42007-02-24 00:55:48 +0000477}
478
479ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000480 : TerminatorInst(Type::VoidTy, Instruction::Ret,
481 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
482 retVal != 0, InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000483 if (retVal)
484 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000485}
486ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000487 : TerminatorInst(Type::VoidTy, Instruction::Ret,
488 OperandTraits<ReturnInst>::op_end(this) - (retVal != 0),
489 retVal != 0, InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000490 if (retVal)
491 init(&retVal, 1);
Chris Lattner2195fc42007-02-24 00:55:48 +0000492}
493ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000494 : TerminatorInst(Type::VoidTy, Instruction::Ret,
495 OperandTraits<ReturnInst>::op_end(this),
496 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000497}
498
Devang Patela736c002008-02-26 19:38:17 +0000499ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
500 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000501 : TerminatorInst(Type::VoidTy, Instruction::Ret,
502 OperandTraits<ReturnInst>::op_end(this) - N,
503 N, InsertBefore) {
Devang Patela736c002008-02-26 19:38:17 +0000504 if (N != 0)
505 init(retVals, N);
506}
507ReturnInst::ReturnInst(Value * const* retVals, unsigned N,
508 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000509 : TerminatorInst(Type::VoidTy, Instruction::Ret,
510 OperandTraits<ReturnInst>::op_end(this) - N,
511 N, InsertAtEnd) {
Devang Patela736c002008-02-26 19:38:17 +0000512 if (N != 0)
513 init(retVals, N);
514}
515
Devang Patel060e79c2008-02-26 19:15:26 +0000516void ReturnInst::init(Value * const* retVals, unsigned N) {
Devang Patelc38eb522008-02-26 18:49:29 +0000517 assert (N > 0 && "Invalid operands numbers in ReturnInst init");
Devang Patel59643e52008-02-23 00:35:18 +0000518
Devang Patelc38eb522008-02-26 18:49:29 +0000519 NumOperands = N;
Devang Patel59643e52008-02-23 00:35:18 +0000520 if (NumOperands == 1) {
Devang Patel060e79c2008-02-26 19:15:26 +0000521 Value *V = *retVals;
Devang Patel59643e52008-02-23 00:35:18 +0000522 if (V->getType() == Type::VoidTy)
523 return;
Gabor Greiff6caff662008-05-10 08:32:32 +0000524 Op<0>().init(V, this);
Devang Patelae682fb2008-02-26 17:56:20 +0000525 return;
Devang Patel59643e52008-02-23 00:35:18 +0000526 }
527
Gabor Greiff6caff662008-05-10 08:32:32 +0000528 Use *OL = OperandList;
Devang Patel59643e52008-02-23 00:35:18 +0000529 for (unsigned i = 0; i < NumOperands; ++i) {
Devang Patel060e79c2008-02-26 19:15:26 +0000530 Value *V = *retVals++;
Devang Patel59643e52008-02-23 00:35:18 +0000531 assert(!isa<BasicBlock>(V) &&
532 "Cannot return basic block. Probably using the incorrect ctor");
Devang Patel060e79c2008-02-26 19:15:26 +0000533 OL[i].init(V, this);
Devang Patel59643e52008-02-23 00:35:18 +0000534 }
535}
536
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000537unsigned ReturnInst::getNumSuccessorsV() const {
538 return getNumSuccessors();
539}
540
Devang Patelae682fb2008-02-26 17:56:20 +0000541/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
542/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000543void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000544 assert(0 && "ReturnInst has no successors!");
545}
546
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000547BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
548 assert(0 && "ReturnInst has no successors!");
549 abort();
550 return 0;
551}
552
Devang Patel59643e52008-02-23 00:35:18 +0000553ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000554}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000555
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000556//===----------------------------------------------------------------------===//
557// UnwindInst Implementation
558//===----------------------------------------------------------------------===//
559
Chris Lattner2195fc42007-02-24 00:55:48 +0000560UnwindInst::UnwindInst(Instruction *InsertBefore)
561 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
562}
563UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
564 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
565}
566
567
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000568unsigned UnwindInst::getNumSuccessorsV() const {
569 return getNumSuccessors();
570}
571
572void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000573 assert(0 && "UnwindInst has no successors!");
574}
575
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000576BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
577 assert(0 && "UnwindInst has no successors!");
578 abort();
579 return 0;
580}
581
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000582//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000583// UnreachableInst Implementation
584//===----------------------------------------------------------------------===//
585
Chris Lattner2195fc42007-02-24 00:55:48 +0000586UnreachableInst::UnreachableInst(Instruction *InsertBefore)
587 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
588}
589UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
590 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
591}
592
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000593unsigned UnreachableInst::getNumSuccessorsV() const {
594 return getNumSuccessors();
595}
596
597void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
598 assert(0 && "UnwindInst has no successors!");
599}
600
601BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
602 assert(0 && "UnwindInst has no successors!");
603 abort();
604 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000605}
606
607//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000608// BranchInst Implementation
609//===----------------------------------------------------------------------===//
610
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000611void BranchInst::AssertOK() {
612 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000613 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000614 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000615}
616
Chris Lattner2195fc42007-02-24 00:55:48 +0000617BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000618 : TerminatorInst(Type::VoidTy, Instruction::Br,
619 OperandTraits<BranchInst>::op_end(this) - 1,
620 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000621 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greiff6caff662008-05-10 08:32:32 +0000622 Op<0>().init(reinterpret_cast<Value*>(IfTrue), this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000623}
624BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
625 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000626 : TerminatorInst(Type::VoidTy, Instruction::Br,
627 OperandTraits<BranchInst>::op_end(this) - 3,
628 3, InsertBefore) {
629 Op<0>().init(reinterpret_cast<Value*>(IfTrue), this);
630 Op<1>().init(reinterpret_cast<Value*>(IfFalse), this);
631 Op<2>().init(Cond, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000632#ifndef NDEBUG
633 AssertOK();
634#endif
635}
636
637BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000638 : TerminatorInst(Type::VoidTy, Instruction::Br,
639 OperandTraits<BranchInst>::op_end(this) - 1,
640 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000641 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greiff6caff662008-05-10 08:32:32 +0000642 Op<0>().init(reinterpret_cast<Value*>(IfTrue), this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000643}
644
645BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
646 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000647 : TerminatorInst(Type::VoidTy, Instruction::Br,
648 OperandTraits<BranchInst>::op_end(this) - 3,
649 3, InsertAtEnd) {
650 Op<0>().init(reinterpret_cast<Value*>(IfTrue), this);
651 Op<1>().init(reinterpret_cast<Value*>(IfFalse), this);
652 Op<2>().init(Cond, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000653#ifndef NDEBUG
654 AssertOK();
655#endif
656}
657
658
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000659BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000660 TerminatorInst(Type::VoidTy, Instruction::Br,
661 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
662 BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000663 OperandList[0].init(BI.getOperand(0), this);
664 if (BI.getNumOperands() != 1) {
665 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
666 OperandList[1].init(BI.getOperand(1), this);
667 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000668 }
669}
670
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000671BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
672 return getSuccessor(idx);
673}
674unsigned BranchInst::getNumSuccessorsV() const {
675 return getNumSuccessors();
676}
677void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
678 setSuccessor(idx, B);
679}
680
681
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000682//===----------------------------------------------------------------------===//
683// AllocationInst Implementation
684//===----------------------------------------------------------------------===//
685
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000686static Value *getAISize(Value *Amt) {
687 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000688 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000689 else {
690 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000691 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000692 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000693 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000694 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000695 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000696}
697
Misha Brukmanb1c93172005-04-21 23:48:37 +0000698AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000699 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000700 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000701 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000702 InsertBefore) {
703 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000704 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000705 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000706}
707
Misha Brukmanb1c93172005-04-21 23:48:37 +0000708AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000709 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000710 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000711 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000712 InsertAtEnd) {
713 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000714 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000715 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000716}
717
Gordon Henriksen14a55692007-12-10 02:14:30 +0000718// Out of line virtual method, so the vtable, etc has a home.
719AllocationInst::~AllocationInst() {
720}
721
Dan Gohmanaa583d72008-03-24 16:55:58 +0000722void AllocationInst::setAlignment(unsigned Align) {
723 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
724 SubclassData = Log2_32(Align) + 1;
725 assert(getAlignment() == Align && "Alignment representation error!");
726}
727
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000728bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000729 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
730 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000731 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732}
733
734const Type *AllocationInst::getAllocatedType() const {
735 return getType()->getElementType();
736}
737
738AllocaInst::AllocaInst(const AllocaInst &AI)
739 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000740 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000741}
742
743MallocInst::MallocInst(const MallocInst &MI)
744 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000745 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000746}
747
748//===----------------------------------------------------------------------===//
749// FreeInst Implementation
750//===----------------------------------------------------------------------===//
751
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000752void FreeInst::AssertOK() {
753 assert(isa<PointerType>(getOperand(0)->getType()) &&
754 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000755}
756
757FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000758 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000759 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000760}
761
762FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000763 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000764 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000765}
766
767
768//===----------------------------------------------------------------------===//
769// LoadInst Implementation
770//===----------------------------------------------------------------------===//
771
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000772void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000773 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000774 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000775}
776
777LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000778 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000779 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000780 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000781 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000782 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000783 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000784}
785
786LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000787 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000788 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000789 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000790 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000791 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000792 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000793}
794
795LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
796 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000797 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000798 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000799 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000800 setAlignment(0);
801 AssertOK();
802 setName(Name);
803}
804
805LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
806 unsigned Align, Instruction *InsertBef)
807 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
808 Load, Ptr, InsertBef) {
809 setVolatile(isVolatile);
810 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000811 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000812 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000813}
814
Dan Gohman68659282007-07-18 20:51:11 +0000815LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
816 unsigned Align, BasicBlock *InsertAE)
817 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
818 Load, Ptr, InsertAE) {
819 setVolatile(isVolatile);
820 setAlignment(Align);
821 AssertOK();
822 setName(Name);
823}
824
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000825LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
826 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000827 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000828 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000829 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000830 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000831 AssertOK();
832 setName(Name);
833}
834
835
836
837LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000838 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
839 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000840 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000841 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000842 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000843 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000844}
845
846LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000847 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
848 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000849 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000850 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000851 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000852 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000853}
854
855LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
856 Instruction *InsertBef)
857: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000858 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000859 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000860 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000861 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000862 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000863}
864
865LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
866 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000867 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
868 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000869 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000870 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000871 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000872 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000873}
874
Christopher Lamb84485702007-04-22 19:24:39 +0000875void LoadInst::setAlignment(unsigned Align) {
876 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
877 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
878}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000879
880//===----------------------------------------------------------------------===//
881// StoreInst Implementation
882//===----------------------------------------------------------------------===//
883
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000884void StoreInst::AssertOK() {
885 assert(isa<PointerType>(getOperand(1)->getType()) &&
886 "Ptr must have pointer type!");
887 assert(getOperand(0)->getType() ==
888 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000889 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000890}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000891
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000892
893StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000894 : Instruction(Type::VoidTy, Store,
895 OperandTraits<StoreInst>::op_begin(this),
896 OperandTraits<StoreInst>::operands(this),
897 InsertBefore) {
898 Op<0>().init(val, this);
899 Op<1>().init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000900 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000901 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000902 AssertOK();
903}
904
905StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000906 : Instruction(Type::VoidTy, Store,
907 OperandTraits<StoreInst>::op_begin(this),
908 OperandTraits<StoreInst>::operands(this),
909 InsertAtEnd) {
910 Op<0>().init(val, this);
911 Op<1>().init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000912 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000913 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000914 AssertOK();
915}
916
Misha Brukmanb1c93172005-04-21 23:48:37 +0000917StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000918 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000919 : Instruction(Type::VoidTy, Store,
920 OperandTraits<StoreInst>::op_begin(this),
921 OperandTraits<StoreInst>::operands(this),
922 InsertBefore) {
923 Op<0>().init(val, this);
924 Op<1>().init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000925 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000926 setAlignment(0);
927 AssertOK();
928}
929
930StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
931 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000932 : Instruction(Type::VoidTy, Store,
933 OperandTraits<StoreInst>::op_begin(this),
934 OperandTraits<StoreInst>::operands(this),
935 InsertBefore) {
936 Op<0>().init(val, this);
937 Op<1>().init(addr, this);
Christopher Lamb84485702007-04-22 19:24:39 +0000938 setVolatile(isVolatile);
939 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000940 AssertOK();
941}
942
Misha Brukmanb1c93172005-04-21 23:48:37 +0000943StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000944 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000945 : Instruction(Type::VoidTy, Store,
946 OperandTraits<StoreInst>::op_begin(this),
947 OperandTraits<StoreInst>::operands(this),
948 InsertAtEnd) {
949 Op<0>().init(val, this);
950 Op<1>().init(addr, this);
Dan Gohman68659282007-07-18 20:51:11 +0000951 setVolatile(isVolatile);
952 setAlignment(Align);
953 AssertOK();
954}
955
956StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000957 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000958 : Instruction(Type::VoidTy, Store,
959 OperandTraits<StoreInst>::op_begin(this),
960 OperandTraits<StoreInst>::operands(this),
961 InsertAtEnd) {
962 Op<0>().init(val, this);
963 Op<1>().init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000964 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000965 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000966 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000967}
968
Christopher Lamb84485702007-04-22 19:24:39 +0000969void StoreInst::setAlignment(unsigned Align) {
970 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
971 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
972}
973
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000974//===----------------------------------------------------------------------===//
975// GetElementPtrInst Implementation
976//===----------------------------------------------------------------------===//
977
Christopher Lambedf07882007-12-17 01:12:55 +0000978static unsigned retrieveAddrSpace(const Value *Val) {
979 return cast<PointerType>(Val->getType())->getAddressSpace();
980}
981
Chris Lattner79807c3d2007-01-31 19:47:18 +0000982void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000983 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
984 Use *OL = OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000985 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000986
Chris Lattner79807c3d2007-01-31 19:47:18 +0000987 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000988 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000989}
990
Chris Lattner82981202005-05-03 05:43:30 +0000991void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000992 assert(NumOperands == 2 && "NumOperands not initialized?");
993 Use *OL = OperandList;
Chris Lattner82981202005-05-03 05:43:30 +0000994 OL[0].init(Ptr, this);
995 OL[1].init(Idx, this);
996}
997
Gabor Greiff6caff662008-05-10 08:32:32 +0000998GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
999 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
1000 OperandTraits<GetElementPtrInst>::op_end(this) - GEPI.getNumOperands(),
1001 GEPI.getNumOperands()) {
1002 Use *OL = OperandList;
1003 Use *GEPIOL = GEPI.OperandList;
1004 for (unsigned i = 0, E = NumOperands; i != E; ++i)
1005 OL[i].init(GEPIOL[i], this);
1006}
1007
Chris Lattner82981202005-05-03 05:43:30 +00001008GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1009 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001010 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001011 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001012 GetElementPtr,
1013 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1014 2, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +00001015 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001016 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001017}
1018
1019GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1020 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001021 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001022 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001023 GetElementPtr,
1024 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1025 2, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +00001026 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001027 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001028}
1029
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001030// getIndexedType - Returns the type of the element that would be loaded with
1031// a load instruction with the specified parameters.
1032//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001033// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001034// pointer type.
1035//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001036const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001037 Value* const *Idxs,
1038 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001039 bool AllowCompositeLeaf) {
1040 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
1041
1042 // Handle the special case of the empty set index set...
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001043 if (NumIdx == 0) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001044 if (AllowCompositeLeaf ||
1045 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
1046 return cast<PointerType>(Ptr)->getElementType();
1047 else
1048 return 0;
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001049 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001050
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001051 unsigned CurIdx = 0;
1052 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001053 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001054 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
1055 return 0; // Can't load a whole structure or array!?!?
1056 }
1057
Chris Lattner302116a2007-01-31 04:40:28 +00001058 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001059 if (isa<PointerType>(CT) && CurIdx != 1)
1060 return 0; // Can only index into pointer types at the first index!
1061 if (!CT->indexValid(Index)) return 0;
1062 Ptr = CT->getTypeAtIndex(Index);
1063
1064 // If the new type forwards to another type, then it is in the middle
1065 // of being refined to another type (and hence, may have dropped all
1066 // references to what it was using before). So, use the new forwarded
1067 // type.
1068 if (const Type * Ty = Ptr->getForwardedType()) {
1069 Ptr = Ty;
1070 }
1071 }
Chris Lattner302116a2007-01-31 04:40:28 +00001072 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001073}
1074
Chris Lattner82981202005-05-03 05:43:30 +00001075const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1076 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1077 if (!PTy) return 0; // Type isn't a pointer type!
1078
1079 // Check the pointer index.
1080 if (!PTy->indexValid(Idx)) return 0;
1081
Chris Lattnerc2233332005-05-03 16:44:45 +00001082 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001083}
1084
Chris Lattner45f15572007-04-14 00:12:57 +00001085
1086/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1087/// zeros. If so, the result pointer and the first operand have the same
1088/// value, just potentially different types.
1089bool GetElementPtrInst::hasAllZeroIndices() const {
1090 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1091 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1092 if (!CI->isZero()) return false;
1093 } else {
1094 return false;
1095 }
1096 }
1097 return true;
1098}
1099
Chris Lattner27058292007-04-27 20:35:56 +00001100/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1101/// constant integers. If so, the result pointer and the first operand have
1102/// a constant offset between them.
1103bool GetElementPtrInst::hasAllConstantIndices() const {
1104 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1105 if (!isa<ConstantInt>(getOperand(i)))
1106 return false;
1107 }
1108 return true;
1109}
1110
Chris Lattner45f15572007-04-14 00:12:57 +00001111
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001112//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001113// ExtractElementInst Implementation
1114//===----------------------------------------------------------------------===//
1115
1116ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001117 const std::string &Name,
1118 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001119 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001120 ExtractElement,
1121 OperandTraits<ExtractElementInst>::op_begin(this),
1122 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001123 assert(isValidOperands(Val, Index) &&
1124 "Invalid extractelement instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001125 Op<0>().init(Val, this);
1126 Op<1>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001127 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001128}
1129
Chris Lattner65511ff2006-10-05 06:24:58 +00001130ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1131 const std::string &Name,
1132 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001133 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001134 ExtractElement,
1135 OperandTraits<ExtractElementInst>::op_begin(this),
1136 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001137 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001138 assert(isValidOperands(Val, Index) &&
1139 "Invalid extractelement instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001140 Op<0>().init(Val, this);
1141 Op<1>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001142 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001143}
1144
1145
Robert Bocchino23004482006-01-10 19:05:34 +00001146ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001147 const std::string &Name,
1148 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001149 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001150 ExtractElement,
1151 OperandTraits<ExtractElementInst>::op_begin(this),
1152 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001153 assert(isValidOperands(Val, Index) &&
1154 "Invalid extractelement instruction operands!");
1155
Gabor Greiff6caff662008-05-10 08:32:32 +00001156 Op<0>().init(Val, this);
1157 Op<1>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001158 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001159}
1160
Chris Lattner65511ff2006-10-05 06:24:58 +00001161ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1162 const std::string &Name,
1163 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001164 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001165 ExtractElement,
1166 OperandTraits<ExtractElementInst>::op_begin(this),
1167 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001168 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001169 assert(isValidOperands(Val, Index) &&
1170 "Invalid extractelement instruction operands!");
1171
Gabor Greiff6caff662008-05-10 08:32:32 +00001172 Op<0>().init(Val, this);
1173 Op<1>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001174 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001175}
1176
1177
Chris Lattner54865b32006-04-08 04:05:48 +00001178bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001179 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001180 return false;
1181 return true;
1182}
1183
1184
Robert Bocchino23004482006-01-10 19:05:34 +00001185//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001186// InsertElementInst Implementation
1187//===----------------------------------------------------------------------===//
1188
Chris Lattner0875d942006-04-14 22:20:32 +00001189InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001190 : Instruction(IE.getType(), InsertElement,
1191 OperandTraits<InsertElementInst>::op_begin(this), 3) {
1192 Op<0>().init(IE.Op<0>(), this);
1193 Op<1>().init(IE.Op<1>(), this);
1194 Op<2>().init(IE.Op<2>(), this);
Chris Lattner0875d942006-04-14 22:20:32 +00001195}
Chris Lattner54865b32006-04-08 04:05:48 +00001196InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001197 const std::string &Name,
1198 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001199 : Instruction(Vec->getType(), InsertElement,
1200 OperandTraits<InsertElementInst>::op_begin(this),
1201 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001202 assert(isValidOperands(Vec, Elt, Index) &&
1203 "Invalid insertelement instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001204 Op<0>().init(Vec, this);
1205 Op<1>().init(Elt, this);
1206 Op<2>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001207 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001208}
1209
Chris Lattner65511ff2006-10-05 06:24:58 +00001210InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1211 const std::string &Name,
1212 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001213 : Instruction(Vec->getType(), InsertElement,
1214 OperandTraits<InsertElementInst>::op_begin(this),
1215 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001216 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001217 assert(isValidOperands(Vec, Elt, Index) &&
1218 "Invalid insertelement instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001219 Op<0>().init(Vec, this);
1220 Op<1>().init(Elt, this);
1221 Op<2>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001222 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001223}
1224
1225
Chris Lattner54865b32006-04-08 04:05:48 +00001226InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001227 const std::string &Name,
1228 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001229 : Instruction(Vec->getType(), InsertElement,
1230 OperandTraits<InsertElementInst>::op_begin(this),
1231 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001232 assert(isValidOperands(Vec, Elt, Index) &&
1233 "Invalid insertelement instruction operands!");
1234
Gabor Greiff6caff662008-05-10 08:32:32 +00001235 Op<0>().init(Vec, this);
1236 Op<1>().init(Elt, this);
1237 Op<2>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001238 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001239}
1240
Chris Lattner65511ff2006-10-05 06:24:58 +00001241InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1242 const std::string &Name,
1243 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001244: Instruction(Vec->getType(), InsertElement,
1245 OperandTraits<InsertElementInst>::op_begin(this),
1246 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001247 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001248 assert(isValidOperands(Vec, Elt, Index) &&
1249 "Invalid insertelement instruction operands!");
1250
Gabor Greiff6caff662008-05-10 08:32:32 +00001251 Op<0>().init(Vec, this);
1252 Op<1>().init(Elt, this);
1253 Op<2>().init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001254 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001255}
1256
Chris Lattner54865b32006-04-08 04:05:48 +00001257bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1258 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001259 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001260 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001261
Reid Spencerd84d35b2007-02-15 02:26:10 +00001262 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001263 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001264
Reid Spencer8d9336d2006-12-31 05:26:44 +00001265 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001266 return false; // Third operand of insertelement must be uint.
1267 return true;
1268}
1269
1270
Robert Bocchinoca27f032006-01-17 20:07:22 +00001271//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001272// ShuffleVectorInst Implementation
1273//===----------------------------------------------------------------------===//
1274
Chris Lattner0875d942006-04-14 22:20:32 +00001275ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001276 : Instruction(SV.getType(), ShuffleVector,
1277 OperandTraits<ShuffleVectorInst>::op_begin(this),
1278 OperandTraits<ShuffleVectorInst>::operands(this)) {
1279 Op<0>().init(SV.Op<0>(), this);
1280 Op<1>().init(SV.Op<1>(), this);
1281 Op<2>().init(SV.Op<2>(), this);
Chris Lattner0875d942006-04-14 22:20:32 +00001282}
1283
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001284ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1285 const std::string &Name,
1286 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001287 : Instruction(V1->getType(), ShuffleVector,
1288 OperandTraits<ShuffleVectorInst>::op_begin(this),
1289 OperandTraits<ShuffleVectorInst>::operands(this),
1290 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001291 assert(isValidOperands(V1, V2, Mask) &&
1292 "Invalid shuffle vector instruction operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00001293 Op<0>().init(V1, this);
1294 Op<1>().init(V2, this);
1295 Op<2>().init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001296 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001297}
1298
1299ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1300 const std::string &Name,
1301 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001302 : Instruction(V1->getType(), ShuffleVector,
1303 OperandTraits<ShuffleVectorInst>::op_begin(this),
1304 OperandTraits<ShuffleVectorInst>::operands(this),
1305 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001306 assert(isValidOperands(V1, V2, Mask) &&
1307 "Invalid shuffle vector instruction operands!");
1308
Gabor Greiff6caff662008-05-10 08:32:32 +00001309 Op<0>().init(V1, this);
1310 Op<1>().init(V2, this);
1311 Op<2>().init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001312 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001313}
1314
1315bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1316 const Value *Mask) {
Chris Lattnerf724e342008-03-02 05:28:33 +00001317 if (!isa<VectorType>(V1->getType()) ||
1318 V1->getType() != V2->getType())
1319 return false;
1320
1321 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1322 if (!isa<Constant>(Mask) || MaskTy == 0 ||
1323 MaskTy->getElementType() != Type::Int32Ty ||
1324 MaskTy->getNumElements() !=
1325 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001326 return false;
1327 return true;
1328}
1329
Chris Lattnerf724e342008-03-02 05:28:33 +00001330/// getMaskValue - Return the index from the shuffle mask for the specified
1331/// output result. This is either -1 if the element is undef or a number less
1332/// than 2*numelements.
1333int ShuffleVectorInst::getMaskValue(unsigned i) const {
1334 const Constant *Mask = cast<Constant>(getOperand(2));
1335 if (isa<UndefValue>(Mask)) return -1;
1336 if (isa<ConstantAggregateZero>(Mask)) return 0;
1337 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1338 assert(i < MaskCV->getNumOperands() && "Index out of range");
1339
1340 if (isa<UndefValue>(MaskCV->getOperand(i)))
1341 return -1;
1342 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1343}
1344
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001345
1346//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001347// BinaryOperator Class
1348//===----------------------------------------------------------------------===//
1349
Chris Lattner2195fc42007-02-24 00:55:48 +00001350BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1351 const Type *Ty, const std::string &Name,
1352 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00001353 : Instruction(Ty, iType,
1354 OperandTraits<BinaryOperator>::op_begin(this),
1355 OperandTraits<BinaryOperator>::operands(this),
1356 InsertBefore) {
1357 Op<0>().init(S1, this);
1358 Op<1>().init(S2, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001359 init(iType);
1360 setName(Name);
1361}
1362
1363BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1364 const Type *Ty, const std::string &Name,
1365 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001366 : Instruction(Ty, iType,
1367 OperandTraits<BinaryOperator>::op_begin(this),
1368 OperandTraits<BinaryOperator>::operands(this),
1369 InsertAtEnd) {
1370 Op<0>().init(S1, this);
1371 Op<1>().init(S2, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001372 init(iType);
1373 setName(Name);
1374}
1375
1376
1377void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001378 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001379 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001380 assert(LHS->getType() == RHS->getType() &&
1381 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001382#ifndef NDEBUG
1383 switch (iType) {
1384 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001385 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001386 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001387 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001388 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001389 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001390 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001391 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001392 case UDiv:
1393 case SDiv:
1394 assert(getType() == LHS->getType() &&
1395 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001396 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1397 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001398 "Incorrect operand type (not integer) for S/UDIV");
1399 break;
1400 case FDiv:
1401 assert(getType() == LHS->getType() &&
1402 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001403 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1404 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001405 && "Incorrect operand type (not floating point) for FDIV");
1406 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001407 case URem:
1408 case SRem:
1409 assert(getType() == LHS->getType() &&
1410 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001411 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1412 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001413 "Incorrect operand type (not integer) for S/UREM");
1414 break;
1415 case FRem:
1416 assert(getType() == LHS->getType() &&
1417 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001418 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1419 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001420 && "Incorrect operand type (not floating point) for FREM");
1421 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001422 case Shl:
1423 case LShr:
1424 case AShr:
1425 assert(getType() == LHS->getType() &&
1426 "Shift operation should return same type as operands!");
1427 assert(getType()->isInteger() &&
1428 "Shift operation requires integer operands");
1429 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001430 case And: case Or:
1431 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001432 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001433 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001434 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001435 (isa<VectorType>(getType()) &&
1436 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001437 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001438 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001439 default:
1440 break;
1441 }
1442#endif
1443}
1444
1445BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001446 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001447 Instruction *InsertBefore) {
1448 assert(S1->getType() == S2->getType() &&
1449 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001450 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001451}
1452
1453BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001454 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001455 BasicBlock *InsertAtEnd) {
1456 BinaryOperator *Res = create(Op, S1, S2, Name);
1457 InsertAtEnd->getInstList().push_back(Res);
1458 return Res;
1459}
1460
1461BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1462 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001463 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1464 return new BinaryOperator(Instruction::Sub,
1465 zero, Op,
1466 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001467}
1468
1469BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1470 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001471 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1472 return new BinaryOperator(Instruction::Sub,
1473 zero, Op,
1474 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001475}
1476
1477BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1478 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001479 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001480 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001481 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001482 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001483 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001484 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001485 }
1486
1487 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001488 Op->getType(), Name, InsertBefore);
1489}
1490
1491BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1492 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001493 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001494 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001495 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001496 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001497 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001498 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001499 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001500 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001501 }
1502
1503 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001504 Op->getType(), Name, InsertAtEnd);
1505}
1506
1507
1508// isConstantAllOnes - Helper function for several functions below
1509static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001510 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1511 return CI->isAllOnesValue();
1512 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1513 return CV->isAllOnesValue();
1514 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001515}
1516
1517bool BinaryOperator::isNeg(const Value *V) {
1518 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1519 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001520 return Bop->getOperand(0) ==
1521 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001522 return false;
1523}
1524
1525bool BinaryOperator::isNot(const Value *V) {
1526 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1527 return (Bop->getOpcode() == Instruction::Xor &&
1528 (isConstantAllOnes(Bop->getOperand(1)) ||
1529 isConstantAllOnes(Bop->getOperand(0))));
1530 return false;
1531}
1532
Chris Lattner2c7d1772005-04-24 07:28:37 +00001533Value *BinaryOperator::getNegArgument(Value *BinOp) {
1534 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1535 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001536}
1537
Chris Lattner2c7d1772005-04-24 07:28:37 +00001538const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1539 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001540}
1541
Chris Lattner2c7d1772005-04-24 07:28:37 +00001542Value *BinaryOperator::getNotArgument(Value *BinOp) {
1543 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1544 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1545 Value *Op0 = BO->getOperand(0);
1546 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001547 if (isConstantAllOnes(Op0)) return Op1;
1548
1549 assert(isConstantAllOnes(Op1));
1550 return Op0;
1551}
1552
Chris Lattner2c7d1772005-04-24 07:28:37 +00001553const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1554 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001555}
1556
1557
1558// swapOperands - Exchange the two operands to this instruction. This
1559// instruction is safe to use on any binary instruction and does not
1560// modify the semantics of the instruction. If the instruction is
1561// order dependent (SetLT f.e.) the opcode is changed.
1562//
1563bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001564 if (!isCommutative())
1565 return true; // Can't commute operands
Gabor Greiff6caff662008-05-10 08:32:32 +00001566 std::swap(Op<0>(), Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001567 return false;
1568}
1569
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001570//===----------------------------------------------------------------------===//
1571// CastInst Class
1572//===----------------------------------------------------------------------===//
1573
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001574// Just determine if this cast only deals with integral->integral conversion.
1575bool CastInst::isIntegerCast() const {
1576 switch (getOpcode()) {
1577 default: return false;
1578 case Instruction::ZExt:
1579 case Instruction::SExt:
1580 case Instruction::Trunc:
1581 return true;
1582 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001583 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001584 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001585}
1586
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001587bool CastInst::isLosslessCast() const {
1588 // Only BitCast can be lossless, exit fast if we're not BitCast
1589 if (getOpcode() != Instruction::BitCast)
1590 return false;
1591
1592 // Identity cast is always lossless
1593 const Type* SrcTy = getOperand(0)->getType();
1594 const Type* DstTy = getType();
1595 if (SrcTy == DstTy)
1596 return true;
1597
Reid Spencer8d9336d2006-12-31 05:26:44 +00001598 // Pointer to pointer is always lossless.
1599 if (isa<PointerType>(SrcTy))
1600 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001601 return false; // Other types have no identity values
1602}
1603
1604/// This function determines if the CastInst does not require any bits to be
1605/// changed in order to effect the cast. Essentially, it identifies cases where
1606/// no code gen is necessary for the cast, hence the name no-op cast. For
1607/// example, the following are all no-op casts:
1608/// # bitcast uint %X, int
1609/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001610/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001611/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1612/// @brief Determine if a cast is a no-op.
1613bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1614 switch (getOpcode()) {
1615 default:
1616 assert(!"Invalid CastOp");
1617 case Instruction::Trunc:
1618 case Instruction::ZExt:
1619 case Instruction::SExt:
1620 case Instruction::FPTrunc:
1621 case Instruction::FPExt:
1622 case Instruction::UIToFP:
1623 case Instruction::SIToFP:
1624 case Instruction::FPToUI:
1625 case Instruction::FPToSI:
1626 return false; // These always modify bits
1627 case Instruction::BitCast:
1628 return true; // BitCast never modifies bits.
1629 case Instruction::PtrToInt:
1630 return IntPtrTy->getPrimitiveSizeInBits() ==
1631 getType()->getPrimitiveSizeInBits();
1632 case Instruction::IntToPtr:
1633 return IntPtrTy->getPrimitiveSizeInBits() ==
1634 getOperand(0)->getType()->getPrimitiveSizeInBits();
1635 }
1636}
1637
1638/// This function determines if a pair of casts can be eliminated and what
1639/// opcode should be used in the elimination. This assumes that there are two
1640/// instructions like this:
1641/// * %F = firstOpcode SrcTy %x to MidTy
1642/// * %S = secondOpcode MidTy %F to DstTy
1643/// The function returns a resultOpcode so these two casts can be replaced with:
1644/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1645/// If no such cast is permited, the function returns 0.
1646unsigned CastInst::isEliminableCastPair(
1647 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1648 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1649{
1650 // Define the 144 possibilities for these two cast instructions. The values
1651 // in this matrix determine what to do in a given situation and select the
1652 // case in the switch below. The rows correspond to firstOp, the columns
1653 // correspond to secondOp. In looking at the table below, keep in mind
1654 // the following cast properties:
1655 //
1656 // Size Compare Source Destination
1657 // Operator Src ? Size Type Sign Type Sign
1658 // -------- ------------ ------------------- ---------------------
1659 // TRUNC > Integer Any Integral Any
1660 // ZEXT < Integral Unsigned Integer Any
1661 // SEXT < Integral Signed Integer Any
1662 // FPTOUI n/a FloatPt n/a Integral Unsigned
1663 // FPTOSI n/a FloatPt n/a Integral Signed
1664 // UITOFP n/a Integral Unsigned FloatPt n/a
1665 // SITOFP n/a Integral Signed FloatPt n/a
1666 // FPTRUNC > FloatPt n/a FloatPt n/a
1667 // FPEXT < FloatPt n/a FloatPt n/a
1668 // PTRTOINT n/a Pointer n/a Integral Unsigned
1669 // INTTOPTR n/a Integral Unsigned Pointer n/a
1670 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001671 //
1672 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1673 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1674 // into "fptoui double to ulong", but this loses information about the range
1675 // of the produced value (we no longer know the top-part is all zeros).
1676 // Further this conversion is often much more expensive for typical hardware,
1677 // and causes issues when building libgcc. We disallow fptosi+sext for the
1678 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001679 const unsigned numCastOps =
1680 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1681 static const uint8_t CastResults[numCastOps][numCastOps] = {
1682 // T F F U S F F P I B -+
1683 // R Z S P P I I T P 2 N T |
1684 // U E E 2 2 2 2 R E I T C +- secondOp
1685 // N X X U S F F N X N 2 V |
1686 // C T T I I P P C T T P T -+
1687 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1688 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1689 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001690 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1691 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001692 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1693 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1694 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1695 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1696 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1697 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1698 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1699 };
1700
1701 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1702 [secondOp-Instruction::CastOpsBegin];
1703 switch (ElimCase) {
1704 case 0:
1705 // categorically disallowed
1706 return 0;
1707 case 1:
1708 // allowed, use first cast's opcode
1709 return firstOp;
1710 case 2:
1711 // allowed, use second cast's opcode
1712 return secondOp;
1713 case 3:
1714 // no-op cast in second op implies firstOp as long as the DestTy
1715 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001716 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001717 return firstOp;
1718 return 0;
1719 case 4:
1720 // no-op cast in second op implies firstOp as long as the DestTy
1721 // is floating point
1722 if (DstTy->isFloatingPoint())
1723 return firstOp;
1724 return 0;
1725 case 5:
1726 // no-op cast in first op implies secondOp as long as the SrcTy
1727 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001728 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001729 return secondOp;
1730 return 0;
1731 case 6:
1732 // no-op cast in first op implies secondOp as long as the SrcTy
1733 // is a floating point
1734 if (SrcTy->isFloatingPoint())
1735 return secondOp;
1736 return 0;
1737 case 7: {
1738 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1739 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1740 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1741 if (MidSize >= PtrSize)
1742 return Instruction::BitCast;
1743 return 0;
1744 }
1745 case 8: {
1746 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1747 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1748 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1749 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1750 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1751 if (SrcSize == DstSize)
1752 return Instruction::BitCast;
1753 else if (SrcSize < DstSize)
1754 return firstOp;
1755 return secondOp;
1756 }
1757 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1758 return Instruction::ZExt;
1759 case 10:
1760 // fpext followed by ftrunc is allowed if the bit size returned to is
1761 // the same as the original, in which case its just a bitcast
1762 if (SrcTy == DstTy)
1763 return Instruction::BitCast;
1764 return 0; // If the types are not the same we can't eliminate it.
1765 case 11:
1766 // bitcast followed by ptrtoint is allowed as long as the bitcast
1767 // is a pointer to pointer cast.
1768 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1769 return secondOp;
1770 return 0;
1771 case 12:
1772 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1773 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1774 return firstOp;
1775 return 0;
1776 case 13: {
1777 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1778 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1779 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1780 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1781 if (SrcSize <= PtrSize && SrcSize == DstSize)
1782 return Instruction::BitCast;
1783 return 0;
1784 }
1785 case 99:
1786 // cast combination can't happen (error in input). This is for all cases
1787 // where the MidTy is not the same for the two cast instructions.
1788 assert(!"Invalid Cast Combination");
1789 return 0;
1790 default:
1791 assert(!"Error in CastResults table!!!");
1792 return 0;
1793 }
1794 return 0;
1795}
1796
1797CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1798 const std::string &Name, Instruction *InsertBefore) {
1799 // Construct and return the appropriate CastInst subclass
1800 switch (op) {
1801 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1802 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1803 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1804 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1805 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1806 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1807 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1808 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1809 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1810 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1811 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1812 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1813 default:
1814 assert(!"Invalid opcode provided");
1815 }
1816 return 0;
1817}
1818
1819CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1820 const std::string &Name, BasicBlock *InsertAtEnd) {
1821 // Construct and return the appropriate CastInst subclass
1822 switch (op) {
1823 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1824 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1825 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1826 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1827 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1828 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1829 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1830 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1831 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1832 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1833 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1834 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1835 default:
1836 assert(!"Invalid opcode provided");
1837 }
1838 return 0;
1839}
1840
Reid Spencer5c140882006-12-04 20:17:56 +00001841CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1842 const std::string &Name,
1843 Instruction *InsertBefore) {
1844 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1845 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1846 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1847}
1848
1849CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1850 const std::string &Name,
1851 BasicBlock *InsertAtEnd) {
1852 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1853 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1854 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1855}
1856
1857CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1858 const std::string &Name,
1859 Instruction *InsertBefore) {
1860 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1861 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1862 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1863}
1864
1865CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1866 const std::string &Name,
1867 BasicBlock *InsertAtEnd) {
1868 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1869 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1870 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1871}
1872
1873CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1874 const std::string &Name,
1875 Instruction *InsertBefore) {
1876 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1877 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1878 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1879}
1880
1881CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1882 const std::string &Name,
1883 BasicBlock *InsertAtEnd) {
1884 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1885 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1886 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1887}
1888
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001889CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1890 const std::string &Name,
1891 BasicBlock *InsertAtEnd) {
1892 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001893 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001894 "Invalid cast");
1895
Chris Lattner03c49532007-01-15 02:27:26 +00001896 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001897 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1898 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1899}
1900
1901/// @brief Create a BitCast or a PtrToInt cast instruction
1902CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1903 const std::string &Name,
1904 Instruction *InsertBefore) {
1905 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001906 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001907 "Invalid cast");
1908
Chris Lattner03c49532007-01-15 02:27:26 +00001909 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001910 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1911 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1912}
1913
Reid Spencer7e933472006-12-12 00:49:44 +00001914CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1915 bool isSigned, const std::string &Name,
1916 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001917 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001918 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1919 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1920 Instruction::CastOps opcode =
1921 (SrcBits == DstBits ? Instruction::BitCast :
1922 (SrcBits > DstBits ? Instruction::Trunc :
1923 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1924 return create(opcode, C, Ty, Name, InsertBefore);
1925}
1926
1927CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1928 bool isSigned, const std::string &Name,
1929 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001930 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001931 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1932 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1933 Instruction::CastOps opcode =
1934 (SrcBits == DstBits ? Instruction::BitCast :
1935 (SrcBits > DstBits ? Instruction::Trunc :
1936 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1937 return create(opcode, C, Ty, Name, InsertAtEnd);
1938}
1939
1940CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1941 const std::string &Name,
1942 Instruction *InsertBefore) {
1943 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1944 "Invalid cast");
1945 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1946 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1947 Instruction::CastOps opcode =
1948 (SrcBits == DstBits ? Instruction::BitCast :
1949 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1950 return create(opcode, C, Ty, Name, InsertBefore);
1951}
1952
1953CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1954 const std::string &Name,
1955 BasicBlock *InsertAtEnd) {
1956 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1957 "Invalid cast");
1958 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1959 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1960 Instruction::CastOps opcode =
1961 (SrcBits == DstBits ? Instruction::BitCast :
1962 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1963 return create(opcode, C, Ty, Name, InsertAtEnd);
1964}
1965
Duncan Sands55e50902008-01-06 10:12:28 +00001966// Check whether it is valid to call getCastOpcode for these types.
1967// This routine must be kept in sync with getCastOpcode.
1968bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
1969 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
1970 return false;
1971
1972 if (SrcTy == DestTy)
1973 return true;
1974
1975 // Get the bit sizes, we'll need these
1976 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1977 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1978
1979 // Run through the possibilities ...
1980 if (DestTy->isInteger()) { // Casting to integral
1981 if (SrcTy->isInteger()) { // Casting from integral
1982 return true;
1983 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1984 return true;
1985 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1986 // Casting from vector
1987 return DestBits == PTy->getBitWidth();
1988 } else { // Casting from something else
1989 return isa<PointerType>(SrcTy);
1990 }
1991 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
1992 if (SrcTy->isInteger()) { // Casting from integral
1993 return true;
1994 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1995 return true;
1996 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1997 // Casting from vector
1998 return DestBits == PTy->getBitWidth();
1999 } else { // Casting from something else
2000 return false;
2001 }
2002 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2003 // Casting to vector
2004 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
2005 // Casting from vector
2006 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
2007 } else { // Casting from something else
2008 return DestPTy->getBitWidth() == SrcBits;
2009 }
2010 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2011 if (isa<PointerType>(SrcTy)) { // Casting from pointer
2012 return true;
2013 } else if (SrcTy->isInteger()) { // Casting from integral
2014 return true;
2015 } else { // Casting from something else
2016 return false;
2017 }
2018 } else { // Casting to something else
2019 return false;
2020 }
2021}
2022
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002023// Provide a way to get a "cast" where the cast opcode is inferred from the
2024// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002025// logic in the castIsValid function below. This axiom should hold:
2026// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2027// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002028// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002029// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002030Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002031CastInst::getCastOpcode(
2032 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002033 // Get the bit sizes, we'll need these
2034 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00002035 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
2036 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002037
Duncan Sands55e50902008-01-06 10:12:28 +00002038 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2039 "Only first class types are castable!");
2040
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002041 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002042 if (DestTy->isInteger()) { // Casting to integral
2043 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002044 if (DestBits < SrcBits)
2045 return Trunc; // int -> smaller int
2046 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002047 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002048 return SExt; // signed -> SEXT
2049 else
2050 return ZExt; // unsigned -> ZEXT
2051 } else {
2052 return BitCast; // Same size, No-op cast
2053 }
2054 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002055 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002056 return FPToSI; // FP -> sint
2057 else
2058 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002059 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002060 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002061 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002062 return BitCast; // Same size, no-op cast
2063 } else {
2064 assert(isa<PointerType>(SrcTy) &&
2065 "Casting from a value that is not first-class type");
2066 return PtrToInt; // ptr -> int
2067 }
2068 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002069 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002070 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002071 return SIToFP; // sint -> FP
2072 else
2073 return UIToFP; // uint -> FP
2074 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2075 if (DestBits < SrcBits) {
2076 return FPTrunc; // FP -> smaller FP
2077 } else if (DestBits > SrcBits) {
2078 return FPExt; // FP -> larger FP
2079 } else {
2080 return BitCast; // same size, no-op cast
2081 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002082 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002083 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002084 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002085 return BitCast; // same size, no-op cast
2086 } else {
2087 assert(0 && "Casting pointer or non-first class to float");
2088 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002089 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2090 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002091 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002092 "Casting vector to vector of different widths");
2093 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002094 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002095 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002096 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002097 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002098 }
2099 } else if (isa<PointerType>(DestTy)) {
2100 if (isa<PointerType>(SrcTy)) {
2101 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002102 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002103 return IntToPtr; // int -> ptr
2104 } else {
2105 assert(!"Casting pointer to other than pointer or int");
2106 }
2107 } else {
2108 assert(!"Casting to type that is not first-class");
2109 }
2110
2111 // If we fall through to here we probably hit an assertion cast above
2112 // and assertions are not turned on. Anything we return is an error, so
2113 // BitCast is as good a choice as any.
2114 return BitCast;
2115}
2116
2117//===----------------------------------------------------------------------===//
2118// CastInst SubClass Constructors
2119//===----------------------------------------------------------------------===//
2120
2121/// Check that the construction parameters for a CastInst are correct. This
2122/// could be broken out into the separate constructors but it is useful to have
2123/// it in one place and to eliminate the redundant code for getting the sizes
2124/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002125bool
2126CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002127
2128 // Check for type sanity on the arguments
2129 const Type *SrcTy = S->getType();
2130 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2131 return false;
2132
2133 // Get the size of the types in bits, we'll need this later
2134 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2135 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2136
2137 // Switch on the opcode provided
2138 switch (op) {
2139 default: return false; // This is an input error
2140 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002141 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002142 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002143 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002144 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002145 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002146 case Instruction::FPTrunc:
2147 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2148 SrcBitSize > DstBitSize;
2149 case Instruction::FPExt:
2150 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2151 SrcBitSize < DstBitSize;
2152 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002153 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002154 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2155 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2156 return SVTy->getElementType()->isInteger() &&
2157 DVTy->getElementType()->isFloatingPoint() &&
2158 SVTy->getNumElements() == DVTy->getNumElements();
2159 }
2160 }
Chris Lattner03c49532007-01-15 02:27:26 +00002161 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002162 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002163 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002164 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2165 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2166 return SVTy->getElementType()->isFloatingPoint() &&
2167 DVTy->getElementType()->isInteger() &&
2168 SVTy->getNumElements() == DVTy->getNumElements();
2169 }
2170 }
Chris Lattner03c49532007-01-15 02:27:26 +00002171 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002172 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002173 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002174 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002175 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002176 case Instruction::BitCast:
2177 // BitCast implies a no-op cast of type only. No bits change.
2178 // However, you can't cast pointers to anything but pointers.
2179 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2180 return false;
2181
Duncan Sands55e50902008-01-06 10:12:28 +00002182 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002183 // these cases, the cast is okay if the source and destination bit widths
2184 // are identical.
2185 return SrcBitSize == DstBitSize;
2186 }
2187}
2188
2189TruncInst::TruncInst(
2190 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2191) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002192 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002193}
2194
2195TruncInst::TruncInst(
2196 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2197) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002198 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002199}
2200
2201ZExtInst::ZExtInst(
2202 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2203) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002204 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002205}
2206
2207ZExtInst::ZExtInst(
2208 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2209) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002210 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002211}
2212SExtInst::SExtInst(
2213 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2214) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002215 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002216}
2217
Jeff Cohencc08c832006-12-02 02:22:01 +00002218SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002219 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2220) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002221 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002222}
2223
2224FPTruncInst::FPTruncInst(
2225 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2226) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002227 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002228}
2229
2230FPTruncInst::FPTruncInst(
2231 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2232) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002233 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002234}
2235
2236FPExtInst::FPExtInst(
2237 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2238) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002239 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002240}
2241
2242FPExtInst::FPExtInst(
2243 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2244) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002245 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002246}
2247
2248UIToFPInst::UIToFPInst(
2249 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2250) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002251 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002252}
2253
2254UIToFPInst::UIToFPInst(
2255 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2256) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002257 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002258}
2259
2260SIToFPInst::SIToFPInst(
2261 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2262) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002263 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002264}
2265
2266SIToFPInst::SIToFPInst(
2267 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2268) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002269 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002270}
2271
2272FPToUIInst::FPToUIInst(
2273 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2274) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002275 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002276}
2277
2278FPToUIInst::FPToUIInst(
2279 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2280) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002281 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002282}
2283
2284FPToSIInst::FPToSIInst(
2285 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2286) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002287 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002288}
2289
2290FPToSIInst::FPToSIInst(
2291 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2292) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002293 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002294}
2295
2296PtrToIntInst::PtrToIntInst(
2297 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2298) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002299 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002300}
2301
2302PtrToIntInst::PtrToIntInst(
2303 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2304) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002305 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306}
2307
2308IntToPtrInst::IntToPtrInst(
2309 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2310) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002311 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002312}
2313
2314IntToPtrInst::IntToPtrInst(
2315 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2316) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002317 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002318}
2319
2320BitCastInst::BitCastInst(
2321 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2322) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002323 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324}
2325
2326BitCastInst::BitCastInst(
2327 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2328) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002329 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002330}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002331
2332//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002333// CmpInst Classes
2334//===----------------------------------------------------------------------===//
2335
2336CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2337 const std::string &Name, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +00002338 : Instruction(Type::Int1Ty, op,
2339 OperandTraits<CmpInst>::op_begin(this),
2340 OperandTraits<CmpInst>::operands(this),
2341 InsertBefore) {
2342 Op<0>().init(LHS, this);
2343 Op<1>().init(RHS, this);
Reid Spencerd9436b62006-11-20 01:22:35 +00002344 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002345 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002346 if (op == Instruction::ICmp) {
2347 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2348 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2349 "Invalid ICmp predicate value");
2350 const Type* Op0Ty = getOperand(0)->getType();
2351 const Type* Op1Ty = getOperand(1)->getType();
2352 assert(Op0Ty == Op1Ty &&
2353 "Both operands to ICmp instruction are not of the same type!");
2354 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002355 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002356 "Invalid operand types for ICmp instruction");
2357 return;
2358 }
2359 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2360 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2361 "Invalid FCmp predicate value");
2362 const Type* Op0Ty = getOperand(0)->getType();
2363 const Type* Op1Ty = getOperand(1)->getType();
2364 assert(Op0Ty == Op1Ty &&
2365 "Both operands to FCmp instruction are not of the same type!");
2366 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002367 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002368 "Invalid operand types for FCmp instruction");
2369}
Gabor Greiff6caff662008-05-10 08:32:32 +00002370
Reid Spencerd9436b62006-11-20 01:22:35 +00002371CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2372 const std::string &Name, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00002373 : Instruction(Type::Int1Ty, op,
2374 OperandTraits<CmpInst>::op_begin(this),
2375 OperandTraits<CmpInst>::operands(this),
2376 InsertAtEnd) {
2377 Op<0>().init(LHS, this);
2378 Op<1>().init(RHS, this);
Reid Spencerd9436b62006-11-20 01:22:35 +00002379 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002380 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002381 if (op == Instruction::ICmp) {
2382 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2383 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2384 "Invalid ICmp predicate value");
2385
2386 const Type* Op0Ty = getOperand(0)->getType();
2387 const Type* Op1Ty = getOperand(1)->getType();
2388 assert(Op0Ty == Op1Ty &&
2389 "Both operands to ICmp instruction are not of the same type!");
2390 // Check that the operands are the right type
Anton Korobeynikov579f0712008-02-20 11:08:44 +00002391 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002392 "Invalid operand types for ICmp instruction");
2393 return;
2394 }
2395 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2396 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2397 "Invalid FCmp predicate value");
2398 const Type* Op0Ty = getOperand(0)->getType();
2399 const Type* Op1Ty = getOperand(1)->getType();
2400 assert(Op0Ty == Op1Ty &&
2401 "Both operands to FCmp instruction are not of the same type!");
2402 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002403 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002404 "Invalid operand types for FCmp instruction");
2405}
2406
2407CmpInst *
2408CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2409 const std::string &Name, Instruction *InsertBefore) {
2410 if (Op == Instruction::ICmp) {
2411 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2412 InsertBefore);
2413 }
2414 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2415 InsertBefore);
2416}
2417
2418CmpInst *
2419CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2420 const std::string &Name, BasicBlock *InsertAtEnd) {
2421 if (Op == Instruction::ICmp) {
2422 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2423 InsertAtEnd);
2424 }
2425 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2426 InsertAtEnd);
2427}
2428
2429void CmpInst::swapOperands() {
2430 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2431 IC->swapOperands();
2432 else
2433 cast<FCmpInst>(this)->swapOperands();
2434}
2435
2436bool CmpInst::isCommutative() {
2437 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2438 return IC->isCommutative();
2439 return cast<FCmpInst>(this)->isCommutative();
2440}
2441
2442bool CmpInst::isEquality() {
2443 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2444 return IC->isEquality();
2445 return cast<FCmpInst>(this)->isEquality();
2446}
2447
2448
2449ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2450 switch (pred) {
2451 default:
2452 assert(!"Unknown icmp predicate!");
2453 case ICMP_EQ: return ICMP_NE;
2454 case ICMP_NE: return ICMP_EQ;
2455 case ICMP_UGT: return ICMP_ULE;
2456 case ICMP_ULT: return ICMP_UGE;
2457 case ICMP_UGE: return ICMP_ULT;
2458 case ICMP_ULE: return ICMP_UGT;
2459 case ICMP_SGT: return ICMP_SLE;
2460 case ICMP_SLT: return ICMP_SGE;
2461 case ICMP_SGE: return ICMP_SLT;
2462 case ICMP_SLE: return ICMP_SGT;
2463 }
2464}
2465
2466ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2467 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002468 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002469 case ICMP_EQ: case ICMP_NE:
2470 return pred;
2471 case ICMP_SGT: return ICMP_SLT;
2472 case ICMP_SLT: return ICMP_SGT;
2473 case ICMP_SGE: return ICMP_SLE;
2474 case ICMP_SLE: return ICMP_SGE;
2475 case ICMP_UGT: return ICMP_ULT;
2476 case ICMP_ULT: return ICMP_UGT;
2477 case ICMP_UGE: return ICMP_ULE;
2478 case ICMP_ULE: return ICMP_UGE;
2479 }
2480}
2481
Reid Spencer266e42b2006-12-23 06:05:41 +00002482ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2483 switch (pred) {
2484 default: assert(! "Unknown icmp predicate!");
2485 case ICMP_EQ: case ICMP_NE:
2486 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2487 return pred;
2488 case ICMP_UGT: return ICMP_SGT;
2489 case ICMP_ULT: return ICMP_SLT;
2490 case ICMP_UGE: return ICMP_SGE;
2491 case ICMP_ULE: return ICMP_SLE;
2492 }
2493}
2494
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002495ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2496 switch (pred) {
2497 default: assert(! "Unknown icmp predicate!");
2498 case ICMP_EQ: case ICMP_NE:
2499 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2500 return pred;
2501 case ICMP_SGT: return ICMP_UGT;
2502 case ICMP_SLT: return ICMP_ULT;
2503 case ICMP_SGE: return ICMP_UGE;
2504 case ICMP_SLE: return ICMP_ULE;
2505 }
2506}
2507
Reid Spencer266e42b2006-12-23 06:05:41 +00002508bool ICmpInst::isSignedPredicate(Predicate pred) {
2509 switch (pred) {
2510 default: assert(! "Unknown icmp predicate!");
2511 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2512 return true;
2513 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2514 case ICMP_UGE: case ICMP_ULE:
2515 return false;
2516 }
2517}
2518
Reid Spencer0286bc12007-02-28 22:00:54 +00002519/// Initialize a set of values that all satisfy the condition with C.
2520///
2521ConstantRange
2522ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2523 APInt Lower(C);
2524 APInt Upper(C);
2525 uint32_t BitWidth = C.getBitWidth();
2526 switch (pred) {
2527 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2528 case ICmpInst::ICMP_EQ: Upper++; break;
2529 case ICmpInst::ICMP_NE: Lower++; break;
2530 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2531 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2532 case ICmpInst::ICMP_UGT:
2533 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2534 break;
2535 case ICmpInst::ICMP_SGT:
2536 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2537 break;
2538 case ICmpInst::ICMP_ULE:
2539 Lower = APInt::getMinValue(BitWidth); Upper++;
2540 break;
2541 case ICmpInst::ICMP_SLE:
2542 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2543 break;
2544 case ICmpInst::ICMP_UGE:
2545 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2546 break;
2547 case ICmpInst::ICMP_SGE:
2548 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2549 break;
2550 }
2551 return ConstantRange(Lower, Upper);
2552}
2553
Reid Spencerd9436b62006-11-20 01:22:35 +00002554FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2555 switch (pred) {
2556 default:
2557 assert(!"Unknown icmp predicate!");
2558 case FCMP_OEQ: return FCMP_UNE;
2559 case FCMP_ONE: return FCMP_UEQ;
2560 case FCMP_OGT: return FCMP_ULE;
2561 case FCMP_OLT: return FCMP_UGE;
2562 case FCMP_OGE: return FCMP_ULT;
2563 case FCMP_OLE: return FCMP_UGT;
2564 case FCMP_UEQ: return FCMP_ONE;
2565 case FCMP_UNE: return FCMP_OEQ;
2566 case FCMP_UGT: return FCMP_OLE;
2567 case FCMP_ULT: return FCMP_OGE;
2568 case FCMP_UGE: return FCMP_OLT;
2569 case FCMP_ULE: return FCMP_OGT;
2570 case FCMP_ORD: return FCMP_UNO;
2571 case FCMP_UNO: return FCMP_ORD;
2572 case FCMP_TRUE: return FCMP_FALSE;
2573 case FCMP_FALSE: return FCMP_TRUE;
2574 }
2575}
2576
2577FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2578 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002579 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002580 case FCMP_FALSE: case FCMP_TRUE:
2581 case FCMP_OEQ: case FCMP_ONE:
2582 case FCMP_UEQ: case FCMP_UNE:
2583 case FCMP_ORD: case FCMP_UNO:
2584 return pred;
2585 case FCMP_OGT: return FCMP_OLT;
2586 case FCMP_OLT: return FCMP_OGT;
2587 case FCMP_OGE: return FCMP_OLE;
2588 case FCMP_OLE: return FCMP_OGE;
2589 case FCMP_UGT: return FCMP_ULT;
2590 case FCMP_ULT: return FCMP_UGT;
2591 case FCMP_UGE: return FCMP_ULE;
2592 case FCMP_ULE: return FCMP_UGE;
2593 }
2594}
2595
Reid Spencer266e42b2006-12-23 06:05:41 +00002596bool CmpInst::isUnsigned(unsigned short predicate) {
2597 switch (predicate) {
2598 default: return false;
2599 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2600 case ICmpInst::ICMP_UGE: return true;
2601 }
2602}
2603
2604bool CmpInst::isSigned(unsigned short predicate){
2605 switch (predicate) {
2606 default: return false;
2607 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2608 case ICmpInst::ICMP_SGE: return true;
2609 }
2610}
2611
2612bool CmpInst::isOrdered(unsigned short predicate) {
2613 switch (predicate) {
2614 default: return false;
2615 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2616 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2617 case FCmpInst::FCMP_ORD: return true;
2618 }
2619}
2620
2621bool CmpInst::isUnordered(unsigned short predicate) {
2622 switch (predicate) {
2623 default: return false;
2624 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2625 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2626 case FCmpInst::FCMP_UNO: return true;
2627 }
2628}
2629
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002630//===----------------------------------------------------------------------===//
2631// SwitchInst Implementation
2632//===----------------------------------------------------------------------===//
2633
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002634void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002635 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002636 ReservedSpace = 2+NumCases*2;
2637 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002638 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002639
2640 OperandList[0].init(Value, this);
2641 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002642}
2643
Chris Lattner2195fc42007-02-24 00:55:48 +00002644/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2645/// switch on and a default destination. The number of additional cases can
2646/// be specified here to make memory allocation more efficient. This
2647/// constructor can also autoinsert before another instruction.
2648SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2649 Instruction *InsertBefore)
2650 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2651 init(Value, Default, NumCases);
2652}
2653
2654/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2655/// switch on and a default destination. The number of additional cases can
2656/// be specified here to make memory allocation more efficient. This
2657/// constructor also autoinserts at the end of the specified BasicBlock.
2658SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2659 BasicBlock *InsertAtEnd)
2660 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2661 init(Value, Default, NumCases);
2662}
2663
Misha Brukmanb1c93172005-04-21 23:48:37 +00002664SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002665 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002666 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002667 Use *OL = OperandList, *InOL = SI.OperandList;
2668 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2669 OL[i].init(InOL[i], this);
2670 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002671 }
2672}
2673
Gordon Henriksen14a55692007-12-10 02:14:30 +00002674SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002675 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002676}
2677
2678
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002679/// addCase - Add an entry to the switch instruction...
2680///
Chris Lattner47ac1872005-02-24 05:32:09 +00002681void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002682 unsigned OpNo = NumOperands;
2683 if (OpNo+2 > ReservedSpace)
2684 resizeOperands(0); // Get more space!
2685 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002686 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002687 NumOperands = OpNo+2;
2688 OperandList[OpNo].init(OnVal, this);
2689 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002690}
2691
2692/// removeCase - This method removes the specified successor from the switch
2693/// instruction. Note that this cannot be used to remove the default
2694/// destination (successor #0).
2695///
2696void SwitchInst::removeCase(unsigned idx) {
2697 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002698 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2699
2700 unsigned NumOps = getNumOperands();
2701 Use *OL = OperandList;
2702
2703 // Move everything after this operand down.
2704 //
2705 // FIXME: we could just swap with the end of the list, then erase. However,
2706 // client might not expect this to happen. The code as it is thrashes the
2707 // use/def lists, which is kinda lame.
2708 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2709 OL[i-2] = OL[i];
2710 OL[i-2+1] = OL[i+1];
2711 }
2712
2713 // Nuke the last value.
2714 OL[NumOps-2].set(0);
2715 OL[NumOps-2+1].set(0);
2716 NumOperands = NumOps-2;
2717}
2718
2719/// resizeOperands - resize operands - This adjusts the length of the operands
2720/// list according to the following behavior:
2721/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002722/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002723/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2724/// 3. If NumOps == NumOperands, trim the reserved space.
2725///
2726void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002727 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002728 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002729 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002730 } else if (NumOps*2 > NumOperands) {
2731 // No resize needed.
2732 if (ReservedSpace >= NumOps) return;
2733 } else if (NumOps == NumOperands) {
2734 if (ReservedSpace == NumOps) return;
2735 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002736 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002737 }
2738
2739 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002740 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002741 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002742 for (unsigned i = 0; i != e; ++i) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002743 NewOps[i].init(OldOps[i], this);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002744 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002745 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002746 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002747}
2748
2749
2750BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2751 return getSuccessor(idx);
2752}
2753unsigned SwitchInst::getNumSuccessorsV() const {
2754 return getNumSuccessors();
2755}
2756void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2757 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002758}
Chris Lattnerf22be932004-10-15 23:52:53 +00002759
Devang Patel295711f2008-02-19 22:15:16 +00002760//===----------------------------------------------------------------------===//
2761// GetResultInst Implementation
2762//===----------------------------------------------------------------------===//
2763
Devang Patel666d4512008-02-20 19:10:47 +00002764GetResultInst::GetResultInst(Value *Aggregate, unsigned Index,
Devang Patel295711f2008-02-19 22:15:16 +00002765 const std::string &Name,
2766 Instruction *InsertBef)
Devang Pateldcad9da2008-02-20 19:26:55 +00002767 : Instruction(cast<StructType>(Aggregate->getType())->getElementType(Index),
Gabor Greiff6caff662008-05-10 08:32:32 +00002768 GetResult,
2769 OperandTraits<GetResultInst>::op_begin(this),
2770 OperandTraits<GetResultInst>::operands(this),
2771 InsertBef) {
Devang Patel666d4512008-02-20 19:10:47 +00002772 assert(isValidOperands(Aggregate, Index) && "Invalid GetResultInst operands!");
Gabor Greiff6caff662008-05-10 08:32:32 +00002773 Op<0>().init(Aggregate, this);
Devang Patel666d4512008-02-20 19:10:47 +00002774 Idx = Index;
Devang Patel295711f2008-02-19 22:15:16 +00002775 setName(Name);
2776}
2777
Devang Patel666d4512008-02-20 19:10:47 +00002778bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) {
2779 if (!Aggregate)
Devang Patel295711f2008-02-19 22:15:16 +00002780 return false;
Devang Patel666d4512008-02-20 19:10:47 +00002781
Devang Patelcf193b82008-02-20 19:39:41 +00002782 if (const StructType *STy = dyn_cast<StructType>(Aggregate->getType())) {
2783 unsigned NumElements = STy->getNumElements();
Chris Lattnerb6917d22008-04-23 05:36:34 +00002784 if (Index >= NumElements || NumElements == 0)
Devang Patelcf193b82008-02-20 19:39:41 +00002785 return false;
2786
2787 // getresult aggregate value's element types are restricted to
2788 // avoid nested aggregates.
2789 for (unsigned i = 0; i < NumElements; ++i)
2790 if (!STy->getElementType(i)->isFirstClassType())
2791 return false;
2792
2793 // Otherwise, Aggregate is valid.
2794 return true;
2795 }
Devang Patel666d4512008-02-20 19:10:47 +00002796 return false;
Devang Patel295711f2008-02-19 22:15:16 +00002797}
2798
Chris Lattnerf22be932004-10-15 23:52:53 +00002799// Define these methods here so vtables don't get emitted into every translation
2800// unit that uses these classes.
2801
2802GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002803 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002804}
2805
2806BinaryOperator *BinaryOperator::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002807 return create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002808}
2809
Chris Lattner0b490b02007-08-24 20:48:18 +00002810FCmpInst* FCmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002811 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002812}
2813ICmpInst* ICmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002814 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002815}
2816
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002817MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2818AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2819FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2820LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2821StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2822CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2823CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2824CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2825CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2826CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2827CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2828CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2829CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2830CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2831CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2832CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2833CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greife9ecc682008-04-06 20:25:17 +00002834CallInst *CallInst::clone() const { return new(getNumOperands()) CallInst(*this); }
2835SelectInst *SelectInst::clone() const { return new(getNumOperands()) SelectInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002836VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2837
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002838ExtractElementInst *ExtractElementInst::clone() const {
2839 return new ExtractElementInst(*this);
2840}
2841InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002842 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002843}
2844ShuffleVectorInst *ShuffleVectorInst::clone() const {
2845 return new ShuffleVectorInst(*this);
2846}
Chris Lattnerf22be932004-10-15 23:52:53 +00002847PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greife9ecc682008-04-06 20:25:17 +00002848ReturnInst *ReturnInst::clone() const { return new(getNumOperands()) ReturnInst(*this); }
2849BranchInst *BranchInst::clone() const { return new(getNumOperands()) BranchInst(*this); }
Gabor Greiff6caff662008-05-10 08:32:32 +00002850SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
Gabor Greife9ecc682008-04-06 20:25:17 +00002851InvokeInst *InvokeInst::clone() const { return new(getNumOperands()) InvokeInst(*this); }
Chris Lattnerf22be932004-10-15 23:52:53 +00002852UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002853UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
Devang Patel295711f2008-02-19 22:15:16 +00002854GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }