blob: 475d8cdd56c2562095b64f495e13727a07941d20 [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
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000015#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Function.h"
18#include "llvm/Instructions.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000019#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000020#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"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000023#include "llvm/Support/Streams.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024using namespace llvm;
25
Chris Lattner3e13b8c2008-01-02 23:42:30 +000026//===----------------------------------------------------------------------===//
27// CallSite Class
28//===----------------------------------------------------------------------===//
29
Gabor Greif1b9921a2009-01-11 22:33:22 +000030#define CALLSITE_DELEGATE_GETTER(METHOD) \
31 Instruction *II(getInstruction()); \
32 return isCall() \
33 ? cast<CallInst>(II)->METHOD \
34 : cast<InvokeInst>(II)->METHOD
35
36#define CALLSITE_DELEGATE_SETTER(METHOD) \
37 Instruction *II(getInstruction()); \
38 if (isCall()) \
39 cast<CallInst>(II)->METHOD; \
40 else \
41 cast<InvokeInst>(II)->METHOD
42
Duncan Sands85fab3a2008-02-18 17:32:13 +000043CallSite::CallSite(Instruction *C) {
44 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
Gabor Greif1b9921a2009-01-11 22:33:22 +000045 I.setPointer(C);
46 I.setInt(isa<CallInst>(C));
Duncan Sands85fab3a2008-02-18 17:32:13 +000047}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000048unsigned CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000049 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000050}
51void CallSite::setCallingConv(unsigned CC) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000052 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
Chris Lattnerf7b6d312005-05-06 20:26:43 +000053}
Devang Patel4c758ea2008-09-25 21:00:45 +000054const AttrListPtr &CallSite::getAttributes() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000055 CALLSITE_DELEGATE_GETTER(getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000056}
Devang Patel4c758ea2008-09-25 21:00:45 +000057void CallSite::setAttributes(const AttrListPtr &PAL) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000058 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000059}
Devang Patelba3fa6c2008-09-23 23:03:40 +000060bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000061 CALLSITE_DELEGATE_GETTER(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 {
Gabor Greif1b9921a2009-01-11 22:33:22 +000064 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
Dale Johanneseneabc5f32008-02-22 17:49:45 +000065}
Duncan Sands38ef3a82007-12-03 20:06:50 +000066bool CallSite::doesNotAccessMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000067 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000068}
Duncan Sands78c88722008-07-08 08:38:44 +000069void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000070 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000071}
Duncan Sands38ef3a82007-12-03 20:06:50 +000072bool CallSite::onlyReadsMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000073 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000074}
Duncan Sands78c88722008-07-08 08:38:44 +000075void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000076 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000077}
78bool CallSite::doesNotReturn() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000079 CALLSITE_DELEGATE_GETTER(doesNotReturn());
Duncan Sands78c88722008-07-08 08:38:44 +000080}
81void CallSite::setDoesNotReturn(bool doesNotReturn) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000082 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
Duncan Sands78c88722008-07-08 08:38:44 +000083}
Duncan Sands3353ed02007-12-18 09:59:50 +000084bool CallSite::doesNotThrow() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000085 CALLSITE_DELEGATE_GETTER(doesNotThrow());
Duncan Sands8e4847e2007-12-16 15:51:49 +000086}
Duncan Sandsaa31b922007-12-19 21:13:37 +000087void CallSite::setDoesNotThrow(bool doesNotThrow) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000088 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
Duncan Sandsaa31b922007-12-19 21:13:37 +000089}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000090
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000091bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000092 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
93 if (AI->get() == Arg)
94 return true;
95 return false;
96}
97
Gabor Greif1b9921a2009-01-11 22:33:22 +000098#undef CALLSITE_DELEGATE_GETTER
99#undef CALLSITE_DELEGATE_SETTER
100
Gordon Henriksen14a55692007-12-10 02:14:30 +0000101//===----------------------------------------------------------------------===//
102// TerminatorInst Class
103//===----------------------------------------------------------------------===//
104
105// Out of line virtual method, so the vtable, etc has a home.
106TerminatorInst::~TerminatorInst() {
107}
108
Gabor Greiff6caff662008-05-10 08:32:32 +0000109//===----------------------------------------------------------------------===//
110// UnaryInstruction Class
111//===----------------------------------------------------------------------===//
112
Gordon Henriksen14a55692007-12-10 02:14:30 +0000113// Out of line virtual method, so the vtable, etc has a home.
114UnaryInstruction::~UnaryInstruction() {
115}
116
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000118// SelectInst Class
119//===----------------------------------------------------------------------===//
120
121/// areInvalidOperands - Return a string if the specified operands are invalid
122/// for a select operation, otherwise return null.
123const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
124 if (Op1->getType() != Op2->getType())
125 return "both values to select must have same type";
126
127 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
128 // Vector select.
129 if (VT->getElementType() != Type::Int1Ty)
130 return "vector select condition element type must be i1";
131 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
132 if (ET == 0)
133 return "selected values for vector select must be vectors";
134 if (ET->getNumElements() != VT->getNumElements())
135 return "vector select requires selected vectors to have "
136 "the same vector length as select condition";
137 } else if (Op0->getType() != Type::Int1Ty) {
138 return "select condition must be i1 or <n x i1>";
139 }
140 return 0;
141}
142
143
144//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000145// PHINode Class
146//===----------------------------------------------------------------------===//
147
148PHINode::PHINode(const PHINode &PN)
149 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000150 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000151 ReservedSpace(PN.getNumOperands()) {
152 Use *OL = OperandList;
153 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000154 OL[i] = PN.getOperand(i);
155 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156 }
157}
158
Gordon Henriksen14a55692007-12-10 02:14:30 +0000159PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000160 if (OperandList)
161 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000162}
163
164// removeIncomingValue - Remove an incoming value. This is useful if a
165// predecessor basic block is deleted.
166Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
167 unsigned NumOps = getNumOperands();
168 Use *OL = OperandList;
169 assert(Idx*2 < NumOps && "BB not in PHI node!");
170 Value *Removed = OL[Idx*2];
171
172 // Move everything after this operand down.
173 //
174 // FIXME: we could just swap with the end of the list, then erase. However,
175 // client might not expect this to happen. The code as it is thrashes the
176 // use/def lists, which is kinda lame.
177 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
178 OL[i-2] = OL[i];
179 OL[i-2+1] = OL[i+1];
180 }
181
182 // Nuke the last value.
183 OL[NumOps-2].set(0);
184 OL[NumOps-2+1].set(0);
185 NumOperands = NumOps-2;
186
187 // If the PHI node is dead, because it has zero entries, nuke it now.
188 if (NumOps == 2 && DeletePHIIfEmpty) {
189 // If anyone is using this PHI, make them use a dummy value instead...
190 replaceAllUsesWith(UndefValue::get(getType()));
191 eraseFromParent();
192 }
193 return Removed;
194}
195
196/// resizeOperands - resize operands - This adjusts the length of the operands
197/// list according to the following behavior:
198/// 1. If NumOps == 0, grow the operand list in response to a push_back style
199/// of operation. This grows the number of ops by 1.5 times.
200/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
201/// 3. If NumOps == NumOperands, trim the reserved space.
202///
203void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000204 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000205 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000206 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000207 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
208 } else if (NumOps*2 > NumOperands) {
209 // No resize needed.
210 if (ReservedSpace >= NumOps) return;
211 } else if (NumOps == NumOperands) {
212 if (ReservedSpace == NumOps) return;
213 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000214 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215 }
216
217 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000218 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000219 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000220 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000221 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000222 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000223}
224
Nate Begemanb3923212005-08-04 23:24:19 +0000225/// hasConstantValue - If the specified PHI node always merges together the same
226/// value, return the value, otherwise return null.
227///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000228Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000229 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000230 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000231 if (getIncomingValue(0) != this) // not X = phi X
232 return getIncomingValue(0);
233 else
234 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000235 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000236
Nate Begemanb3923212005-08-04 23:24:19 +0000237 // Otherwise if all of the incoming values are the same for the PHI, replace
238 // the PHI node with the incoming value.
239 //
240 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000241 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000242 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000243 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000244 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000245 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000246 if (InVal && getIncomingValue(i) != InVal)
247 return 0; // Not the same, bail out.
248 else
249 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000250 }
Nate Begemanb3923212005-08-04 23:24:19 +0000251
252 // The only case that could cause InVal to be null is if we have a PHI node
253 // that only has entries for itself. In this case, there is no entry into the
254 // loop, so kill the PHI.
255 //
256 if (InVal == 0) InVal = UndefValue::get(getType());
257
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000258 // If we have a PHI node like phi(X, undef, X), where X is defined by some
259 // instruction, we cannot always return X as the result of the PHI node. Only
260 // do this if X is not an instruction (thus it must dominate the PHI block),
261 // or if the client is prepared to deal with this possibility.
262 if (HasUndefInput && !AllowNonDominatingInstruction)
263 if (Instruction *IV = dyn_cast<Instruction>(InVal))
264 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000265 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000266 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000267 return 0; // Cannot guarantee that InVal dominates this PHINode.
268
Nate Begemanb3923212005-08-04 23:24:19 +0000269 // All of the incoming values are the same, return the value now.
270 return InVal;
271}
272
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000273
274//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000275// CallInst Implementation
276//===----------------------------------------------------------------------===//
277
Gordon Henriksen14a55692007-12-10 02:14:30 +0000278CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000279}
280
Chris Lattner054ba2c2007-02-13 00:58:44 +0000281void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000282 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
283 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000284 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285
Misha Brukmanb1c93172005-04-21 23:48:37 +0000286 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000288 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289
Chris Lattner054ba2c2007-02-13 00:58:44 +0000290 assert((NumParams == FTy->getNumParams() ||
291 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000292 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000293 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000294 assert((i >= FTy->getNumParams() ||
295 FTy->getParamType(i) == Params[i]->getType()) &&
296 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000297 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000298 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299}
300
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000301void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000302 assert(NumOperands == 3 && "NumOperands not set up?");
303 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000304 OL[0] = Func;
305 OL[1] = Actual1;
306 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000307
308 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000310 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000312 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000313 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000315 assert((0 >= FTy->getNumParams() ||
316 FTy->getParamType(0) == Actual1->getType()) &&
317 "Calling a function with a bad signature!");
318 assert((1 >= FTy->getNumParams() ||
319 FTy->getParamType(1) == Actual2->getType()) &&
320 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000321}
322
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000323void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000324 assert(NumOperands == 2 && "NumOperands not set up?");
325 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000326 OL[0] = Func;
327 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000328
329 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000331 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000333 assert((FTy->getNumParams() == 1 ||
334 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000336 assert((0 == FTy->getNumParams() ||
337 FTy->getParamType(0) == Actual->getType()) &&
338 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000339}
340
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000341void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000342 assert(NumOperands == 1 && "NumOperands not set up?");
343 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000344 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000345
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000346 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000347 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000348 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000349
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000350 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000351}
352
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000353CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000354 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000355 : 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) - 2,
359 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000360 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000361 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362}
363
364CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
365 BasicBlock *InsertAtEnd)
366 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
367 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000368 Instruction::Call,
369 OperandTraits<CallInst>::op_end(this) - 2,
370 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000372 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000373}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000374CallInst::CallInst(Value *Func, const std::string &Name,
375 Instruction *InsertBefore)
376 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
377 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000378 Instruction::Call,
379 OperandTraits<CallInst>::op_end(this) - 1,
380 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000381 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000382 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000383}
384
385CallInst::CallInst(Value *Func, const std::string &Name,
386 BasicBlock *InsertAtEnd)
387 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
388 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000389 Instruction::Call,
390 OperandTraits<CallInst>::op_end(this) - 1,
391 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000392 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000393 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000394}
395
Misha Brukmanb1c93172005-04-21 23:48:37 +0000396CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000397 : Instruction(CI.getType(), Instruction::Call,
398 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000399 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000400 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000401 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000402 Use *OL = OperandList;
403 Use *InOL = CI.OperandList;
404 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000405 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000406}
407
Devang Patel4c758ea2008-09-25 21:00:45 +0000408void CallInst::addAttribute(unsigned i, Attributes attr) {
409 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000410 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000411 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000412}
413
Devang Patel4c758ea2008-09-25 21:00:45 +0000414void CallInst::removeAttribute(unsigned i, Attributes attr) {
415 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000416 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000417 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000418}
419
Devang Patelba3fa6c2008-09-23 23:03:40 +0000420bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000421 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000422 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000423 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000424 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000425 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000426}
427
Duncan Sands5208d1a2007-11-28 17:07:01 +0000428
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000429//===----------------------------------------------------------------------===//
430// InvokeInst Implementation
431//===----------------------------------------------------------------------===//
432
433void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000434 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000435 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Bill Wendling4bb96e92009-03-13 21:15:59 +0000436 Use *OL = OperandList;
437 OL[0] = Fn;
438 OL[1] = IfNormal;
439 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000440 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000441 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000442 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000443
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000444 assert(((NumArgs == FTy->getNumParams()) ||
445 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000446 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000447
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000448 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000449 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000450 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000451 "Invoking a function with a bad signature!");
452
Bill Wendling4bb96e92009-03-13 21:15:59 +0000453 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000454 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000455}
456
Misha Brukmanb1c93172005-04-21 23:48:37 +0000457InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000458 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000459 OperandTraits<InvokeInst>::op_end(this)
460 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000461 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000462 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000463 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000464 Use *OL = OperandList, *InOL = II.OperandList;
465 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000466 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000467}
468
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000469BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
470 return getSuccessor(idx);
471}
472unsigned InvokeInst::getNumSuccessorsV() const {
473 return getNumSuccessors();
474}
475void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
476 return setSuccessor(idx, B);
477}
478
Devang Patelba3fa6c2008-09-23 23:03:40 +0000479bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000480 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000481 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000482 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000483 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000484 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000485}
486
Devang Patel4c758ea2008-09-25 21:00:45 +0000487void InvokeInst::addAttribute(unsigned i, Attributes attr) {
488 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000489 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000490 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000491}
492
Devang Patel4c758ea2008-09-25 21:00:45 +0000493void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
494 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000495 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000496 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000497}
498
Duncan Sands5208d1a2007-11-28 17:07:01 +0000499
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000500//===----------------------------------------------------------------------===//
501// ReturnInst Implementation
502//===----------------------------------------------------------------------===//
503
Chris Lattner2195fc42007-02-24 00:55:48 +0000504ReturnInst::ReturnInst(const ReturnInst &RI)
505 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000506 OperandTraits<ReturnInst>::op_end(this) -
507 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000508 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000509 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000510 Op<0>() = RI.Op<0>();
Chris Lattner2195fc42007-02-24 00:55:48 +0000511}
512
513ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000514 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000515 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
516 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000517 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000518 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000519}
520ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000521 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000522 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
523 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000524 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000525 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000526}
527ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000528 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000529 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000530}
531
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000532unsigned ReturnInst::getNumSuccessorsV() const {
533 return getNumSuccessors();
534}
535
Devang Patelae682fb2008-02-26 17:56:20 +0000536/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
537/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000538void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwin6dd27302009-07-08 18:01:40 +0000539 LLVM_UNREACHABLE("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000540}
541
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000542BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwin6dd27302009-07-08 18:01:40 +0000543 LLVM_UNREACHABLE("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000544 return 0;
545}
546
Devang Patel59643e52008-02-23 00:35:18 +0000547ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000548}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000549
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000550//===----------------------------------------------------------------------===//
551// UnwindInst Implementation
552//===----------------------------------------------------------------------===//
553
Chris Lattner2195fc42007-02-24 00:55:48 +0000554UnwindInst::UnwindInst(Instruction *InsertBefore)
555 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
556}
557UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
558 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
559}
560
561
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000562unsigned UnwindInst::getNumSuccessorsV() const {
563 return getNumSuccessors();
564}
565
566void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwin6dd27302009-07-08 18:01:40 +0000567 LLVM_UNREACHABLE("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000568}
569
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000570BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwin6dd27302009-07-08 18:01:40 +0000571 LLVM_UNREACHABLE("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000572 return 0;
573}
574
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000575//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000576// UnreachableInst Implementation
577//===----------------------------------------------------------------------===//
578
Chris Lattner2195fc42007-02-24 00:55:48 +0000579UnreachableInst::UnreachableInst(Instruction *InsertBefore)
580 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
581}
582UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
583 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
584}
585
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000586unsigned UnreachableInst::getNumSuccessorsV() const {
587 return getNumSuccessors();
588}
589
590void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwin6dd27302009-07-08 18:01:40 +0000591 LLVM_UNREACHABLE("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000592}
593
594BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwin6dd27302009-07-08 18:01:40 +0000595 LLVM_UNREACHABLE("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000597}
598
599//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000600// BranchInst Implementation
601//===----------------------------------------------------------------------===//
602
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000603void BranchInst::AssertOK() {
604 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000605 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000606 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000607}
608
Chris Lattner2195fc42007-02-24 00:55:48 +0000609BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000610 : TerminatorInst(Type::VoidTy, Instruction::Br,
611 OperandTraits<BranchInst>::op_end(this) - 1,
612 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000613 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000614 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000615}
616BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
617 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000618 : TerminatorInst(Type::VoidTy, Instruction::Br,
619 OperandTraits<BranchInst>::op_end(this) - 3,
620 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000621 Op<-1>() = IfTrue;
622 Op<-2>() = IfFalse;
623 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000624#ifndef NDEBUG
625 AssertOK();
626#endif
627}
628
629BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000630 : TerminatorInst(Type::VoidTy, Instruction::Br,
631 OperandTraits<BranchInst>::op_end(this) - 1,
632 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000633 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000634 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000635}
636
637BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
638 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000639 : TerminatorInst(Type::VoidTy, Instruction::Br,
640 OperandTraits<BranchInst>::op_end(this) - 3,
641 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000642 Op<-1>() = IfTrue;
643 Op<-2>() = IfFalse;
644 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000645#ifndef NDEBUG
646 AssertOK();
647#endif
648}
649
650
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000651BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000652 TerminatorInst(Type::VoidTy, Instruction::Br,
653 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
654 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000655 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656 if (BI.getNumOperands() != 1) {
657 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000658 Op<-3>() = BI.Op<-3>();
659 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000660 }
661}
662
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000663
664Use* Use::getPrefix() {
665 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
666 if (PotentialPrefix.getOpaqueValue())
667 return 0;
668
669 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
670}
671
672BranchInst::~BranchInst() {
673 if (NumOperands == 1) {
674 if (Use *Prefix = OperandList->getPrefix()) {
675 Op<-1>() = 0;
676 //
677 // mark OperandList to have a special value for scrutiny
678 // by baseclass destructors and operator delete
679 OperandList = Prefix;
680 } else {
681 NumOperands = 3;
682 OperandList = op_begin();
683 }
684 }
685}
686
687
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000688BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
689 return getSuccessor(idx);
690}
691unsigned BranchInst::getNumSuccessorsV() const {
692 return getNumSuccessors();
693}
694void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
695 setSuccessor(idx, B);
696}
697
698
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000699//===----------------------------------------------------------------------===//
700// AllocationInst Implementation
701//===----------------------------------------------------------------------===//
702
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000703static Value *getAISize(Value *Amt) {
704 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000705 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000706 else {
707 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000708 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000709 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000710 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000711 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000712 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000713}
714
Misha Brukmanb1c93172005-04-21 23:48:37 +0000715AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000716 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000717 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000718 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000719 InsertBefore) {
720 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000722 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000723}
724
Misha Brukmanb1c93172005-04-21 23:48:37 +0000725AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000726 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000727 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000728 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000729 InsertAtEnd) {
730 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000731 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000732 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733}
734
Gordon Henriksen14a55692007-12-10 02:14:30 +0000735// Out of line virtual method, so the vtable, etc has a home.
736AllocationInst::~AllocationInst() {
737}
738
Dan Gohmanaa583d72008-03-24 16:55:58 +0000739void AllocationInst::setAlignment(unsigned Align) {
740 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
741 SubclassData = Log2_32(Align) + 1;
742 assert(getAlignment() == Align && "Alignment representation error!");
743}
744
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000745bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000746 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
747 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000748 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000749}
750
751const Type *AllocationInst::getAllocatedType() const {
752 return getType()->getElementType();
753}
754
755AllocaInst::AllocaInst(const AllocaInst &AI)
756 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000757 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000758}
759
Chris Lattner8b291e62008-11-26 02:54:17 +0000760/// isStaticAlloca - Return true if this alloca is in the entry block of the
761/// function and is a constant size. If so, the code generator will fold it
762/// into the prolog/epilog code, so it is basically free.
763bool AllocaInst::isStaticAlloca() const {
764 // Must be constant size.
765 if (!isa<ConstantInt>(getArraySize())) return false;
766
767 // Must be in the entry block.
768 const BasicBlock *Parent = getParent();
769 return Parent == &Parent->getParent()->front();
770}
771
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000772MallocInst::MallocInst(const MallocInst &MI)
773 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000774 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000775}
776
777//===----------------------------------------------------------------------===//
778// FreeInst Implementation
779//===----------------------------------------------------------------------===//
780
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000781void FreeInst::AssertOK() {
782 assert(isa<PointerType>(getOperand(0)->getType()) &&
783 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000784}
785
786FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000787 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000788 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000789}
790
791FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000792 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000793 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000794}
795
796
797//===----------------------------------------------------------------------===//
798// LoadInst Implementation
799//===----------------------------------------------------------------------===//
800
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000801void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000802 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000804}
805
806LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000807 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000808 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000809 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000810 setAlignment(0);
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
815LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000816 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000817 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000818 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000819 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000820 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000821 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000822}
823
824LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
825 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000826 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000827 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000828 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000829 setAlignment(0);
830 AssertOK();
831 setName(Name);
832}
833
834LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
835 unsigned Align, Instruction *InsertBef)
836 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
837 Load, Ptr, InsertBef) {
838 setVolatile(isVolatile);
839 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000840 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000841 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000842}
843
Dan Gohman68659282007-07-18 20:51:11 +0000844LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
845 unsigned Align, BasicBlock *InsertAE)
846 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
847 Load, Ptr, InsertAE) {
848 setVolatile(isVolatile);
849 setAlignment(Align);
850 AssertOK();
851 setName(Name);
852}
853
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000854LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
855 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000856 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000857 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000858 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000859 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000860 AssertOK();
861 setName(Name);
862}
863
864
865
866LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000867 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
868 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000869 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000870 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000871 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000872 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000873}
874
875LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000876 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
877 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000878 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000879 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000880 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000881 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000882}
883
884LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
885 Instruction *InsertBef)
886: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000887 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000888 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000889 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000890 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000891 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000892}
893
894LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
895 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000896 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
897 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000898 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000899 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000900 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000901 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000902}
903
Christopher Lamb84485702007-04-22 19:24:39 +0000904void LoadInst::setAlignment(unsigned Align) {
905 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
906 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
907}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000908
909//===----------------------------------------------------------------------===//
910// StoreInst Implementation
911//===----------------------------------------------------------------------===//
912
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000913void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000914 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000915 assert(isa<PointerType>(getOperand(1)->getType()) &&
916 "Ptr must have pointer type!");
917 assert(getOperand(0)->getType() ==
918 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000919 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000920}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000921
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000922
923StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000924 : Instruction(Type::VoidTy, Store,
925 OperandTraits<StoreInst>::op_begin(this),
926 OperandTraits<StoreInst>::operands(this),
927 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000928 Op<0>() = val;
929 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000930 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000931 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000932 AssertOK();
933}
934
935StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000936 : Instruction(Type::VoidTy, Store,
937 OperandTraits<StoreInst>::op_begin(this),
938 OperandTraits<StoreInst>::operands(this),
939 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000940 Op<0>() = val;
941 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000942 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000943 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000944 AssertOK();
945}
946
Misha Brukmanb1c93172005-04-21 23:48:37 +0000947StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000948 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000949 : Instruction(Type::VoidTy, Store,
950 OperandTraits<StoreInst>::op_begin(this),
951 OperandTraits<StoreInst>::operands(this),
952 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000953 Op<0>() = val;
954 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000955 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000956 setAlignment(0);
957 AssertOK();
958}
959
960StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
961 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000962 : Instruction(Type::VoidTy, Store,
963 OperandTraits<StoreInst>::op_begin(this),
964 OperandTraits<StoreInst>::operands(this),
965 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000966 Op<0>() = val;
967 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000968 setVolatile(isVolatile);
969 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000970 AssertOK();
971}
972
Misha Brukmanb1c93172005-04-21 23:48:37 +0000973StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000974 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000975 : Instruction(Type::VoidTy, Store,
976 OperandTraits<StoreInst>::op_begin(this),
977 OperandTraits<StoreInst>::operands(this),
978 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000979 Op<0>() = val;
980 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000981 setVolatile(isVolatile);
982 setAlignment(Align);
983 AssertOK();
984}
985
986StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000987 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000988 : Instruction(Type::VoidTy, Store,
989 OperandTraits<StoreInst>::op_begin(this),
990 OperandTraits<StoreInst>::operands(this),
991 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000992 Op<0>() = val;
993 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000994 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000995 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000996 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000997}
998
Christopher Lamb84485702007-04-22 19:24:39 +0000999void StoreInst::setAlignment(unsigned Align) {
1000 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1001 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1002}
1003
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001004//===----------------------------------------------------------------------===//
1005// GetElementPtrInst Implementation
1006//===----------------------------------------------------------------------===//
1007
Christopher Lambedf07882007-12-17 01:12:55 +00001008static unsigned retrieveAddrSpace(const Value *Val) {
1009 return cast<PointerType>(Val->getType())->getAddressSpace();
1010}
1011
Gabor Greif21ba1842008-06-06 20:28:12 +00001012void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Nate Begeman8a4afd92008-07-25 17:24:13 +00001013 const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001014 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1015 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001016 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001017
Chris Lattner79807c3d2007-01-31 19:47:18 +00001018 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001019 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001020
1021 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001022}
1023
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001024void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001025 assert(NumOperands == 2 && "NumOperands not initialized?");
1026 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001027 OL[0] = Ptr;
1028 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001029
1030 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001031}
1032
Gabor Greiff6caff662008-05-10 08:32:32 +00001033GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001034 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001035 OperandTraits<GetElementPtrInst>::op_end(this)
1036 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001037 GEPI.getNumOperands()) {
1038 Use *OL = OperandList;
1039 Use *GEPIOL = GEPI.OperandList;
1040 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001041 OL[i] = GEPIOL[i];
Gabor Greiff6caff662008-05-10 08:32:32 +00001042}
1043
Chris Lattner82981202005-05-03 05:43:30 +00001044GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1045 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001046 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001047 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001048 GetElementPtr,
1049 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1050 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001051 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001052}
1053
1054GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1055 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001056 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001057 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001058 GetElementPtr,
1059 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1060 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001061 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001062}
1063
Chris Lattner6090a422009-03-09 04:46:40 +00001064/// getIndexedType - Returns the type of the element that would be accessed with
1065/// a gep instruction with the specified parameters.
1066///
1067/// The Idxs pointer should point to a continuous piece of memory containing the
1068/// indices, either as Value* or uint64_t.
1069///
1070/// A null type is returned if the indices are invalid for the specified
1071/// pointer type.
1072///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001073template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001074static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1075 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001076 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1077 if (!PTy) return 0; // Type isn't a pointer type!
1078 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001079
Chris Lattner6090a422009-03-09 04:46:40 +00001080 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001081 if (NumIdx == 0)
1082 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001083
1084 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001085 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1086 // that contain opaque types) under the assumption that it will be resolved to
1087 // a sane type later.
1088 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001089 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001090
Dan Gohman1ecaf452008-05-31 00:58:22 +00001091 unsigned CurIdx = 1;
1092 for (; CurIdx != NumIdx; ++CurIdx) {
1093 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1094 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001095 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001096 if (!CT->indexValid(Index)) return 0;
1097 Agg = CT->getTypeAtIndex(Index);
1098
1099 // If the new type forwards to another type, then it is in the middle
1100 // of being refined to another type (and hence, may have dropped all
1101 // references to what it was using before). So, use the new forwarded
1102 // type.
1103 if (const Type *Ty = Agg->getForwardedType())
1104 Agg = Ty;
1105 }
1106 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001107}
1108
Matthijs Kooijman04468622008-07-29 08:46:11 +00001109const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1110 Value* const *Idxs,
1111 unsigned NumIdx) {
1112 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1113}
1114
1115const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1116 uint64_t const *Idxs,
1117 unsigned NumIdx) {
1118 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1119}
1120
Chris Lattner82981202005-05-03 05:43:30 +00001121const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1122 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1123 if (!PTy) return 0; // Type isn't a pointer type!
1124
1125 // Check the pointer index.
1126 if (!PTy->indexValid(Idx)) return 0;
1127
Chris Lattnerc2233332005-05-03 16:44:45 +00001128 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001129}
1130
Chris Lattner45f15572007-04-14 00:12:57 +00001131
1132/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1133/// zeros. If so, the result pointer and the first operand have the same
1134/// value, just potentially different types.
1135bool GetElementPtrInst::hasAllZeroIndices() const {
1136 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1137 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1138 if (!CI->isZero()) return false;
1139 } else {
1140 return false;
1141 }
1142 }
1143 return true;
1144}
1145
Chris Lattner27058292007-04-27 20:35:56 +00001146/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1147/// constant integers. If so, the result pointer and the first operand have
1148/// a constant offset between them.
1149bool GetElementPtrInst::hasAllConstantIndices() const {
1150 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1151 if (!isa<ConstantInt>(getOperand(i)))
1152 return false;
1153 }
1154 return true;
1155}
1156
Chris Lattner45f15572007-04-14 00:12:57 +00001157
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001158//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001159// ExtractElementInst Implementation
1160//===----------------------------------------------------------------------===//
1161
1162ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001163 const std::string &Name,
1164 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001165 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001166 ExtractElement,
1167 OperandTraits<ExtractElementInst>::op_begin(this),
1168 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001169 assert(isValidOperands(Val, Index) &&
1170 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001171 Op<0>() = Val;
1172 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001173 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001174}
1175
Chris Lattner65511ff2006-10-05 06:24:58 +00001176ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1177 const std::string &Name,
1178 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001179 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001180 ExtractElement,
1181 OperandTraits<ExtractElementInst>::op_begin(this),
1182 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001183 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001184 assert(isValidOperands(Val, Index) &&
1185 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001186 Op<0>() = Val;
1187 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001188 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001189}
1190
1191
Robert Bocchino23004482006-01-10 19:05:34 +00001192ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001193 const std::string &Name,
1194 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001195 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001196 ExtractElement,
1197 OperandTraits<ExtractElementInst>::op_begin(this),
1198 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001199 assert(isValidOperands(Val, Index) &&
1200 "Invalid extractelement instruction operands!");
1201
Gabor Greif2d3024d2008-05-26 21:33:52 +00001202 Op<0>() = Val;
1203 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001204 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001205}
1206
Chris Lattner65511ff2006-10-05 06:24:58 +00001207ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1208 const std::string &Name,
1209 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001210 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001211 ExtractElement,
1212 OperandTraits<ExtractElementInst>::op_begin(this),
1213 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001214 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001215 assert(isValidOperands(Val, Index) &&
1216 "Invalid extractelement instruction operands!");
1217
Gabor Greif2d3024d2008-05-26 21:33:52 +00001218 Op<0>() = Val;
1219 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001220 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001221}
1222
1223
Chris Lattner54865b32006-04-08 04:05:48 +00001224bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001225 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001226 return false;
1227 return true;
1228}
1229
1230
Robert Bocchino23004482006-01-10 19:05:34 +00001231//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001232// InsertElementInst Implementation
1233//===----------------------------------------------------------------------===//
1234
Chris Lattner0875d942006-04-14 22:20:32 +00001235InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001236 : Instruction(IE.getType(), InsertElement,
1237 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001238 Op<0>() = IE.Op<0>();
1239 Op<1>() = IE.Op<1>();
1240 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001241}
Chris Lattner54865b32006-04-08 04:05:48 +00001242InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001243 const std::string &Name,
1244 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001245 : Instruction(Vec->getType(), InsertElement,
1246 OperandTraits<InsertElementInst>::op_begin(this),
1247 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001248 assert(isValidOperands(Vec, Elt, Index) &&
1249 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001250 Op<0>() = Vec;
1251 Op<1>() = Elt;
1252 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001253 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001254}
1255
Chris Lattner65511ff2006-10-05 06:24:58 +00001256InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1257 const std::string &Name,
1258 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001259 : Instruction(Vec->getType(), InsertElement,
1260 OperandTraits<InsertElementInst>::op_begin(this),
1261 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001262 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001263 assert(isValidOperands(Vec, Elt, Index) &&
1264 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001265 Op<0>() = Vec;
1266 Op<1>() = Elt;
1267 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001268 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001269}
1270
1271
Chris Lattner54865b32006-04-08 04:05:48 +00001272InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001273 const std::string &Name,
1274 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001275 : Instruction(Vec->getType(), InsertElement,
1276 OperandTraits<InsertElementInst>::op_begin(this),
1277 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001278 assert(isValidOperands(Vec, Elt, Index) &&
1279 "Invalid insertelement instruction operands!");
1280
Gabor Greif2d3024d2008-05-26 21:33:52 +00001281 Op<0>() = Vec;
1282 Op<1>() = Elt;
1283 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001284 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001285}
1286
Chris Lattner65511ff2006-10-05 06:24:58 +00001287InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1288 const std::string &Name,
1289 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001290: Instruction(Vec->getType(), InsertElement,
1291 OperandTraits<InsertElementInst>::op_begin(this),
1292 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001293 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001294 assert(isValidOperands(Vec, Elt, Index) &&
1295 "Invalid insertelement instruction operands!");
1296
Gabor Greif2d3024d2008-05-26 21:33:52 +00001297 Op<0>() = Vec;
1298 Op<1>() = Elt;
1299 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001300 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001301}
1302
Chris Lattner54865b32006-04-08 04:05:48 +00001303bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1304 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001305 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001306 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001307
Reid Spencerd84d35b2007-02-15 02:26:10 +00001308 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001309 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001310
Reid Spencer8d9336d2006-12-31 05:26:44 +00001311 if (Index->getType() != Type::Int32Ty)
Dan Gohman4fe64de2009-06-14 23:30:43 +00001312 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001313 return true;
1314}
1315
1316
Robert Bocchinoca27f032006-01-17 20:07:22 +00001317//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001318// ShuffleVectorInst Implementation
1319//===----------------------------------------------------------------------===//
1320
Chris Lattner0875d942006-04-14 22:20:32 +00001321ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001322 : Instruction(SV.getType(), ShuffleVector,
1323 OperandTraits<ShuffleVectorInst>::op_begin(this),
1324 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001325 Op<0>() = SV.Op<0>();
1326 Op<1>() = SV.Op<1>();
1327 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001328}
1329
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001330ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1331 const std::string &Name,
1332 Instruction *InsertBefore)
Mon P Wang25f01062008-11-10 04:46:22 +00001333: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1334 cast<VectorType>(Mask->getType())->getNumElements()),
1335 ShuffleVector,
1336 OperandTraits<ShuffleVectorInst>::op_begin(this),
1337 OperandTraits<ShuffleVectorInst>::operands(this),
1338 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001339 assert(isValidOperands(V1, V2, Mask) &&
1340 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001341 Op<0>() = V1;
1342 Op<1>() = V2;
1343 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001344 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001345}
1346
1347ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Mon P Wang25f01062008-11-10 04:46:22 +00001348 const std::string &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001349 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001350 : Instruction(V1->getType(), ShuffleVector,
1351 OperandTraits<ShuffleVectorInst>::op_begin(this),
1352 OperandTraits<ShuffleVectorInst>::operands(this),
1353 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001354 assert(isValidOperands(V1, V2, Mask) &&
1355 "Invalid shuffle vector instruction operands!");
1356
Gabor Greif2d3024d2008-05-26 21:33:52 +00001357 Op<0>() = V1;
1358 Op<1>() = V2;
1359 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001360 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001361}
1362
Mon P Wang25f01062008-11-10 04:46:22 +00001363bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001364 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001365 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001366 return false;
1367
1368 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1369 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Mon P Wang25f01062008-11-10 04:46:22 +00001370 MaskTy->getElementType() != Type::Int32Ty)
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001371 return false;
1372 return true;
1373}
1374
Chris Lattnerf724e342008-03-02 05:28:33 +00001375/// getMaskValue - Return the index from the shuffle mask for the specified
1376/// output result. This is either -1 if the element is undef or a number less
1377/// than 2*numelements.
1378int ShuffleVectorInst::getMaskValue(unsigned i) const {
1379 const Constant *Mask = cast<Constant>(getOperand(2));
1380 if (isa<UndefValue>(Mask)) return -1;
1381 if (isa<ConstantAggregateZero>(Mask)) return 0;
1382 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1383 assert(i < MaskCV->getNumOperands() && "Index out of range");
1384
1385 if (isa<UndefValue>(MaskCV->getOperand(i)))
1386 return -1;
1387 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1388}
1389
Dan Gohman12fce772008-05-15 19:50:34 +00001390//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001391// InsertValueInst Class
1392//===----------------------------------------------------------------------===//
1393
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001394void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1395 unsigned NumIdx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001396 assert(NumOperands == 2 && "NumOperands not initialized?");
1397 Op<0>() = Agg;
1398 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001399
Dan Gohman1ecaf452008-05-31 00:58:22 +00001400 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001401 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001402}
1403
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001404void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1405 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001406 assert(NumOperands == 2 && "NumOperands not initialized?");
1407 Op<0>() = Agg;
1408 Op<1>() = Val;
1409
1410 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001411 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001412}
1413
1414InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001415 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001416 OperandTraits<InsertValueInst>::op_begin(this), 2),
1417 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001418 Op<0>() = IVI.getOperand(0);
1419 Op<1>() = IVI.getOperand(1);
Dan Gohman0752bff2008-05-23 00:36:11 +00001420}
1421
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001422InsertValueInst::InsertValueInst(Value *Agg,
1423 Value *Val,
1424 unsigned Idx,
1425 const std::string &Name,
1426 Instruction *InsertBefore)
1427 : Instruction(Agg->getType(), InsertValue,
1428 OperandTraits<InsertValueInst>::op_begin(this),
1429 2, InsertBefore) {
1430 init(Agg, Val, Idx, Name);
1431}
1432
1433InsertValueInst::InsertValueInst(Value *Agg,
1434 Value *Val,
1435 unsigned Idx,
1436 const std::string &Name,
1437 BasicBlock *InsertAtEnd)
1438 : Instruction(Agg->getType(), InsertValue,
1439 OperandTraits<InsertValueInst>::op_begin(this),
1440 2, InsertAtEnd) {
1441 init(Agg, Val, Idx, Name);
1442}
1443
Dan Gohman0752bff2008-05-23 00:36:11 +00001444//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001445// ExtractValueInst Class
1446//===----------------------------------------------------------------------===//
1447
Gabor Greifcbcc4952008-06-06 21:06:32 +00001448void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Gabor Greif1b9921a2009-01-11 22:33:22 +00001449 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001450 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001451
Dan Gohman1ecaf452008-05-31 00:58:22 +00001452 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001453 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001454}
1455
Gabor Greifcbcc4952008-06-06 21:06:32 +00001456void ExtractValueInst::init(unsigned Idx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001457 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001458
1459 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001460 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001461}
1462
1463ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001464 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001465 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001466}
1467
Dan Gohman12fce772008-05-15 19:50:34 +00001468// getIndexedType - Returns the type of the element that would be extracted
1469// with an extractvalue instruction with the specified parameters.
1470//
1471// A null type is returned if the indices are invalid for the specified
1472// pointer type.
1473//
1474const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001475 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001476 unsigned NumIdx) {
1477 unsigned CurIdx = 0;
1478 for (; CurIdx != NumIdx; ++CurIdx) {
1479 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001480 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1481 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001482 if (!CT->indexValid(Index)) return 0;
1483 Agg = CT->getTypeAtIndex(Index);
1484
1485 // If the new type forwards to another type, then it is in the middle
1486 // of being refined to another type (and hence, may have dropped all
1487 // references to what it was using before). So, use the new forwarded
1488 // type.
1489 if (const Type *Ty = Agg->getForwardedType())
1490 Agg = Ty;
1491 }
1492 return CurIdx == NumIdx ? Agg : 0;
1493}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001494
Dan Gohman4a3125b2008-06-17 21:07:55 +00001495const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001496 unsigned Idx) {
1497 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001498}
1499
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001500//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001501// BinaryOperator Class
1502//===----------------------------------------------------------------------===//
1503
Dan Gohmana5b96452009-06-04 22:49:04 +00001504/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1505/// type is floating-point, to help provide compatibility with an older API.
1506///
1507static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1508 const Type *Ty) {
1509 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1510 if (Ty->isFPOrFPVector()) {
1511 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1512 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1513 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1514 }
1515 return iType;
1516}
1517
Chris Lattner2195fc42007-02-24 00:55:48 +00001518BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1519 const Type *Ty, const std::string &Name,
1520 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001521 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001522 OperandTraits<BinaryOperator>::op_begin(this),
1523 OperandTraits<BinaryOperator>::operands(this),
1524 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001525 Op<0>() = S1;
1526 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001527 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001528 setName(Name);
1529}
1530
1531BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1532 const Type *Ty, const std::string &Name,
1533 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001534 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001535 OperandTraits<BinaryOperator>::op_begin(this),
1536 OperandTraits<BinaryOperator>::operands(this),
1537 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001538 Op<0>() = S1;
1539 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001540 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001541 setName(Name);
1542}
1543
1544
1545void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001546 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001547 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001548 assert(LHS->getType() == RHS->getType() &&
1549 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001550#ifndef NDEBUG
1551 switch (iType) {
1552 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001553 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001554 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001555 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001556 assert(getType()->isIntOrIntVector() &&
1557 "Tried to create an integer operation on a non-integer type!");
1558 break;
1559 case FAdd: case FSub:
1560 case FMul:
1561 assert(getType() == LHS->getType() &&
1562 "Arithmetic operation should return same type as operands!");
1563 assert(getType()->isFPOrFPVector() &&
1564 "Tried to create a floating-point operation on a "
1565 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001566 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001567 case UDiv:
1568 case SDiv:
1569 assert(getType() == LHS->getType() &&
1570 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001571 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1572 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001573 "Incorrect operand type (not integer) for S/UDIV");
1574 break;
1575 case FDiv:
1576 assert(getType() == LHS->getType() &&
1577 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001578 assert(getType()->isFPOrFPVector() &&
1579 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001580 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001581 case URem:
1582 case SRem:
1583 assert(getType() == LHS->getType() &&
1584 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001585 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1586 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001587 "Incorrect operand type (not integer) for S/UREM");
1588 break;
1589 case FRem:
1590 assert(getType() == LHS->getType() &&
1591 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001592 assert(getType()->isFPOrFPVector() &&
1593 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001594 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001595 case Shl:
1596 case LShr:
1597 case AShr:
1598 assert(getType() == LHS->getType() &&
1599 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001600 assert((getType()->isInteger() ||
1601 (isa<VectorType>(getType()) &&
1602 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1603 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001604 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001605 case And: case Or:
1606 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001607 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001608 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001609 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001610 (isa<VectorType>(getType()) &&
1611 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001612 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001613 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001614 default:
1615 break;
1616 }
1617#endif
1618}
1619
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001620BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001621 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001622 Instruction *InsertBefore) {
1623 assert(S1->getType() == S2->getType() &&
1624 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001625 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001626}
1627
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001628BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001629 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001630 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001631 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001632 InsertAtEnd->getInstList().push_back(Res);
1633 return Res;
1634}
1635
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001636BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001637 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001638 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1639 return new BinaryOperator(Instruction::Sub,
1640 zero, Op,
1641 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001642}
1643
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001644BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001645 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001646 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1647 return new BinaryOperator(Instruction::Sub,
1648 zero, Op,
1649 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001650}
1651
Dan Gohmana5b96452009-06-04 22:49:04 +00001652BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const std::string &Name,
1653 Instruction *InsertBefore) {
1654 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1655 return new BinaryOperator(Instruction::FSub,
1656 zero, Op,
1657 Op->getType(), Name, InsertBefore);
1658}
1659
1660BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const std::string &Name,
1661 BasicBlock *InsertAtEnd) {
1662 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1663 return new BinaryOperator(Instruction::FSub,
1664 zero, Op,
1665 Op->getType(), Name, InsertAtEnd);
1666}
1667
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001668BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001669 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001670 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001671 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001672 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001673 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001674 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001675 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001676 }
1677
1678 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001679 Op->getType(), Name, InsertBefore);
1680}
1681
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001682BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001683 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001684 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001685 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001686 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001687 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001688 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001689 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001690 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001691 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001692 }
1693
1694 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001695 Op->getType(), Name, InsertAtEnd);
1696}
1697
1698
1699// isConstantAllOnes - Helper function for several functions below
1700static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001701 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1702 return CI->isAllOnesValue();
1703 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1704 return CV->isAllOnesValue();
1705 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001706}
1707
1708bool BinaryOperator::isNeg(const Value *V) {
1709 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1710 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001711 return Bop->getOperand(0) ==
1712 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001713 return false;
1714}
1715
Dan Gohmana5b96452009-06-04 22:49:04 +00001716bool BinaryOperator::isFNeg(const Value *V) {
1717 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1718 if (Bop->getOpcode() == Instruction::FSub)
1719 return Bop->getOperand(0) ==
1720 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
1721 return false;
1722}
1723
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001724bool BinaryOperator::isNot(const Value *V) {
1725 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1726 return (Bop->getOpcode() == Instruction::Xor &&
1727 (isConstantAllOnes(Bop->getOperand(1)) ||
1728 isConstantAllOnes(Bop->getOperand(0))));
1729 return false;
1730}
1731
Chris Lattner2c7d1772005-04-24 07:28:37 +00001732Value *BinaryOperator::getNegArgument(Value *BinOp) {
1733 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1734 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001735}
1736
Chris Lattner2c7d1772005-04-24 07:28:37 +00001737const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1738 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001739}
1740
Dan Gohmana5b96452009-06-04 22:49:04 +00001741Value *BinaryOperator::getFNegArgument(Value *BinOp) {
1742 assert(isFNeg(BinOp) && "getFNegArgument from non-'fneg' instruction!");
1743 return cast<BinaryOperator>(BinOp)->getOperand(1);
1744}
1745
1746const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1747 return getFNegArgument(const_cast<Value*>(BinOp));
1748}
1749
Chris Lattner2c7d1772005-04-24 07:28:37 +00001750Value *BinaryOperator::getNotArgument(Value *BinOp) {
1751 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1752 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1753 Value *Op0 = BO->getOperand(0);
1754 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001755 if (isConstantAllOnes(Op0)) return Op1;
1756
1757 assert(isConstantAllOnes(Op1));
1758 return Op0;
1759}
1760
Chris Lattner2c7d1772005-04-24 07:28:37 +00001761const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1762 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001763}
1764
1765
1766// swapOperands - Exchange the two operands to this instruction. This
1767// instruction is safe to use on any binary instruction and does not
1768// modify the semantics of the instruction. If the instruction is
1769// order dependent (SetLT f.e.) the opcode is changed.
1770//
1771bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001772 if (!isCommutative())
1773 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001774 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001775 return false;
1776}
1777
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001778//===----------------------------------------------------------------------===//
1779// CastInst Class
1780//===----------------------------------------------------------------------===//
1781
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001782// Just determine if this cast only deals with integral->integral conversion.
1783bool CastInst::isIntegerCast() const {
1784 switch (getOpcode()) {
1785 default: return false;
1786 case Instruction::ZExt:
1787 case Instruction::SExt:
1788 case Instruction::Trunc:
1789 return true;
1790 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001791 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001792 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001793}
1794
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001795bool CastInst::isLosslessCast() const {
1796 // Only BitCast can be lossless, exit fast if we're not BitCast
1797 if (getOpcode() != Instruction::BitCast)
1798 return false;
1799
1800 // Identity cast is always lossless
1801 const Type* SrcTy = getOperand(0)->getType();
1802 const Type* DstTy = getType();
1803 if (SrcTy == DstTy)
1804 return true;
1805
Reid Spencer8d9336d2006-12-31 05:26:44 +00001806 // Pointer to pointer is always lossless.
1807 if (isa<PointerType>(SrcTy))
1808 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001809 return false; // Other types have no identity values
1810}
1811
1812/// This function determines if the CastInst does not require any bits to be
1813/// changed in order to effect the cast. Essentially, it identifies cases where
1814/// no code gen is necessary for the cast, hence the name no-op cast. For
1815/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001816/// # bitcast i32* %x to i8*
1817/// # bitcast <2 x i32> %x to <4 x i16>
1818/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001819/// @brief Determine if a cast is a no-op.
1820bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1821 switch (getOpcode()) {
1822 default:
1823 assert(!"Invalid CastOp");
1824 case Instruction::Trunc:
1825 case Instruction::ZExt:
1826 case Instruction::SExt:
1827 case Instruction::FPTrunc:
1828 case Instruction::FPExt:
1829 case Instruction::UIToFP:
1830 case Instruction::SIToFP:
1831 case Instruction::FPToUI:
1832 case Instruction::FPToSI:
1833 return false; // These always modify bits
1834 case Instruction::BitCast:
1835 return true; // BitCast never modifies bits.
1836 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001837 return IntPtrTy->getScalarSizeInBits() ==
1838 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001839 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001840 return IntPtrTy->getScalarSizeInBits() ==
1841 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001842 }
1843}
1844
1845/// This function determines if a pair of casts can be eliminated and what
1846/// opcode should be used in the elimination. This assumes that there are two
1847/// instructions like this:
1848/// * %F = firstOpcode SrcTy %x to MidTy
1849/// * %S = secondOpcode MidTy %F to DstTy
1850/// The function returns a resultOpcode so these two casts can be replaced with:
1851/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1852/// If no such cast is permited, the function returns 0.
1853unsigned CastInst::isEliminableCastPair(
1854 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1855 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1856{
1857 // Define the 144 possibilities for these two cast instructions. The values
1858 // in this matrix determine what to do in a given situation and select the
1859 // case in the switch below. The rows correspond to firstOp, the columns
1860 // correspond to secondOp. In looking at the table below, keep in mind
1861 // the following cast properties:
1862 //
1863 // Size Compare Source Destination
1864 // Operator Src ? Size Type Sign Type Sign
1865 // -------- ------------ ------------------- ---------------------
1866 // TRUNC > Integer Any Integral Any
1867 // ZEXT < Integral Unsigned Integer Any
1868 // SEXT < Integral Signed Integer Any
1869 // FPTOUI n/a FloatPt n/a Integral Unsigned
1870 // FPTOSI n/a FloatPt n/a Integral Signed
1871 // UITOFP n/a Integral Unsigned FloatPt n/a
1872 // SITOFP n/a Integral Signed FloatPt n/a
1873 // FPTRUNC > FloatPt n/a FloatPt n/a
1874 // FPEXT < FloatPt n/a FloatPt n/a
1875 // PTRTOINT n/a Pointer n/a Integral Unsigned
1876 // INTTOPTR n/a Integral Unsigned Pointer n/a
1877 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001878 //
1879 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001880 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1881 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001882 // of the produced value (we no longer know the top-part is all zeros).
1883 // Further this conversion is often much more expensive for typical hardware,
1884 // and causes issues when building libgcc. We disallow fptosi+sext for the
1885 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001886 const unsigned numCastOps =
1887 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1888 static const uint8_t CastResults[numCastOps][numCastOps] = {
1889 // T F F U S F F P I B -+
1890 // R Z S P P I I T P 2 N T |
1891 // U E E 2 2 2 2 R E I T C +- secondOp
1892 // N X X U S F F N X N 2 V |
1893 // C T T I I P P C T T P T -+
1894 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1895 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1896 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001897 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1898 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001899 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1900 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1901 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1902 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1903 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1904 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1905 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1906 };
1907
1908 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1909 [secondOp-Instruction::CastOpsBegin];
1910 switch (ElimCase) {
1911 case 0:
1912 // categorically disallowed
1913 return 0;
1914 case 1:
1915 // allowed, use first cast's opcode
1916 return firstOp;
1917 case 2:
1918 // allowed, use second cast's opcode
1919 return secondOp;
1920 case 3:
1921 // no-op cast in second op implies firstOp as long as the DestTy
1922 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001923 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001924 return firstOp;
1925 return 0;
1926 case 4:
1927 // no-op cast in second op implies firstOp as long as the DestTy
1928 // is floating point
1929 if (DstTy->isFloatingPoint())
1930 return firstOp;
1931 return 0;
1932 case 5:
1933 // no-op cast in first op implies secondOp as long as the SrcTy
1934 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001935 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001936 return secondOp;
1937 return 0;
1938 case 6:
1939 // no-op cast in first op implies secondOp as long as the SrcTy
1940 // is a floating point
1941 if (SrcTy->isFloatingPoint())
1942 return secondOp;
1943 return 0;
1944 case 7: {
1945 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001946 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1947 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001948 if (MidSize >= PtrSize)
1949 return Instruction::BitCast;
1950 return 0;
1951 }
1952 case 8: {
1953 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1954 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1955 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001956 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1957 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001958 if (SrcSize == DstSize)
1959 return Instruction::BitCast;
1960 else if (SrcSize < DstSize)
1961 return firstOp;
1962 return secondOp;
1963 }
1964 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1965 return Instruction::ZExt;
1966 case 10:
1967 // fpext followed by ftrunc is allowed if the bit size returned to is
1968 // the same as the original, in which case its just a bitcast
1969 if (SrcTy == DstTy)
1970 return Instruction::BitCast;
1971 return 0; // If the types are not the same we can't eliminate it.
1972 case 11:
1973 // bitcast followed by ptrtoint is allowed as long as the bitcast
1974 // is a pointer to pointer cast.
1975 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1976 return secondOp;
1977 return 0;
1978 case 12:
1979 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1980 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1981 return firstOp;
1982 return 0;
1983 case 13: {
1984 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001985 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1986 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1987 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001988 if (SrcSize <= PtrSize && SrcSize == DstSize)
1989 return Instruction::BitCast;
1990 return 0;
1991 }
1992 case 99:
1993 // cast combination can't happen (error in input). This is for all cases
1994 // where the MidTy is not the same for the two cast instructions.
1995 assert(!"Invalid Cast Combination");
1996 return 0;
1997 default:
1998 assert(!"Error in CastResults table!!!");
1999 return 0;
2000 }
2001 return 0;
2002}
2003
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002004CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002005 const std::string &Name, Instruction *InsertBefore) {
2006 // Construct and return the appropriate CastInst subclass
2007 switch (op) {
2008 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2009 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2010 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2011 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2012 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2013 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2014 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2015 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2016 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2017 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2018 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2019 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2020 default:
2021 assert(!"Invalid opcode provided");
2022 }
2023 return 0;
2024}
2025
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002026CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002027 const std::string &Name, BasicBlock *InsertAtEnd) {
2028 // Construct and return the appropriate CastInst subclass
2029 switch (op) {
2030 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2031 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2032 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2033 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2034 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2035 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2036 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2037 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2038 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2039 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2040 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2041 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2042 default:
2043 assert(!"Invalid opcode provided");
2044 }
2045 return 0;
2046}
2047
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002048CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002049 const std::string &Name,
2050 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002051 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002052 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2053 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002054}
2055
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002056CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002057 const std::string &Name,
2058 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002059 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002060 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2061 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002062}
2063
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002064CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002065 const std::string &Name,
2066 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002067 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002068 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2069 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002070}
2071
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002072CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002073 const std::string &Name,
2074 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002075 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002076 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2077 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002078}
2079
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002080CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002081 const std::string &Name,
2082 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002083 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002084 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2085 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002086}
2087
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002088CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002089 const std::string &Name,
2090 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002091 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002092 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2093 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002094}
2095
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002096CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002097 const std::string &Name,
2098 BasicBlock *InsertAtEnd) {
2099 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002100 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002101 "Invalid cast");
2102
Chris Lattner03c49532007-01-15 02:27:26 +00002103 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002104 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2105 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002106}
2107
2108/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002109CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002110 const std::string &Name,
2111 Instruction *InsertBefore) {
2112 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002113 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002114 "Invalid cast");
2115
Chris Lattner03c49532007-01-15 02:27:26 +00002116 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002117 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2118 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002119}
2120
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002121CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002122 bool isSigned, const std::string &Name,
2123 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002124 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002125 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2126 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002127 Instruction::CastOps opcode =
2128 (SrcBits == DstBits ? Instruction::BitCast :
2129 (SrcBits > DstBits ? Instruction::Trunc :
2130 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002131 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002132}
2133
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002134CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002135 bool isSigned, const std::string &Name,
2136 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002137 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2138 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002139 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2140 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002141 Instruction::CastOps opcode =
2142 (SrcBits == DstBits ? Instruction::BitCast :
2143 (SrcBits > DstBits ? Instruction::Trunc :
2144 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002145 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002146}
2147
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002148CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002149 const std::string &Name,
2150 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002151 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002152 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002153 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2154 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002155 Instruction::CastOps opcode =
2156 (SrcBits == DstBits ? Instruction::BitCast :
2157 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002158 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002159}
2160
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002161CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002162 const std::string &Name,
2163 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002164 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002165 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002166 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2167 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002168 Instruction::CastOps opcode =
2169 (SrcBits == DstBits ? Instruction::BitCast :
2170 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002171 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002172}
2173
Duncan Sands55e50902008-01-06 10:12:28 +00002174// Check whether it is valid to call getCastOpcode for these types.
2175// This routine must be kept in sync with getCastOpcode.
2176bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2177 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2178 return false;
2179
2180 if (SrcTy == DestTy)
2181 return true;
2182
2183 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002184 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2185 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002186
2187 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002188 if (DestTy->isInteger()) { // Casting to integral
2189 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002190 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002191 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002192 return true;
2193 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002194 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002195 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002196 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002197 return isa<PointerType>(SrcTy);
2198 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002199 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2200 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002201 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002202 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002203 return true;
2204 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002205 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002206 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002207 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002208 return false;
2209 }
2210 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002211 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002212 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002213 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002214 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002215 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002216 return DestPTy->getBitWidth() == SrcBits;
2217 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002218 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2219 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002220 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002221 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002222 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002223 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002224 return false;
2225 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002226 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002227 return false;
2228 }
2229}
2230
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002231// Provide a way to get a "cast" where the cast opcode is inferred from the
2232// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002233// logic in the castIsValid function below. This axiom should hold:
2234// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2235// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002236// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002237// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002238Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002239CastInst::getCastOpcode(
2240 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002241 // Get the bit sizes, we'll need these
2242 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002243 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2244 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002245
Duncan Sands55e50902008-01-06 10:12:28 +00002246 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2247 "Only first class types are castable!");
2248
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002249 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002250 if (DestTy->isInteger()) { // Casting to integral
2251 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002252 if (DestBits < SrcBits)
2253 return Trunc; // int -> smaller int
2254 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002255 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002256 return SExt; // signed -> SEXT
2257 else
2258 return ZExt; // unsigned -> ZEXT
2259 } else {
2260 return BitCast; // Same size, No-op cast
2261 }
2262 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002263 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002264 return FPToSI; // FP -> sint
2265 else
2266 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002267 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002268 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002269 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002270 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002271 return BitCast; // Same size, no-op cast
2272 } else {
2273 assert(isa<PointerType>(SrcTy) &&
2274 "Casting from a value that is not first-class type");
2275 return PtrToInt; // ptr -> int
2276 }
2277 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002278 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002279 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002280 return SIToFP; // sint -> FP
2281 else
2282 return UIToFP; // uint -> FP
2283 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2284 if (DestBits < SrcBits) {
2285 return FPTrunc; // FP -> smaller FP
2286 } else if (DestBits > SrcBits) {
2287 return FPExt; // FP -> larger FP
2288 } else {
2289 return BitCast; // same size, no-op cast
2290 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002291 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002292 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002293 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002294 PTy = NULL;
2295 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002296 } else {
Torok Edwin6dd27302009-07-08 18:01:40 +00002297 LLVM_UNREACHABLE("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002298 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002299 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2300 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002302 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002303 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002304 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002305 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002306 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002307 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002308 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002309 }
2310 } else if (isa<PointerType>(DestTy)) {
2311 if (isa<PointerType>(SrcTy)) {
2312 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002313 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002314 return IntToPtr; // int -> ptr
2315 } else {
2316 assert(!"Casting pointer to other than pointer or int");
2317 }
2318 } else {
2319 assert(!"Casting to type that is not first-class");
2320 }
2321
2322 // If we fall through to here we probably hit an assertion cast above
2323 // and assertions are not turned on. Anything we return is an error, so
2324 // BitCast is as good a choice as any.
2325 return BitCast;
2326}
2327
2328//===----------------------------------------------------------------------===//
2329// CastInst SubClass Constructors
2330//===----------------------------------------------------------------------===//
2331
2332/// Check that the construction parameters for a CastInst are correct. This
2333/// could be broken out into the separate constructors but it is useful to have
2334/// it in one place and to eliminate the redundant code for getting the sizes
2335/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002336bool
2337CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002338
2339 // Check for type sanity on the arguments
2340 const Type *SrcTy = S->getType();
2341 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2342 return false;
2343
2344 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002345 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2346 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002347
2348 // Switch on the opcode provided
2349 switch (op) {
2350 default: return false; // This is an input error
2351 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002352 return SrcTy->isIntOrIntVector() &&
2353 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002354 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002355 return SrcTy->isIntOrIntVector() &&
2356 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002358 return SrcTy->isIntOrIntVector() &&
2359 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002360 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002361 return SrcTy->isFPOrFPVector() &&
2362 DstTy->isFPOrFPVector() &&
2363 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002364 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002365 return SrcTy->isFPOrFPVector() &&
2366 DstTy->isFPOrFPVector() &&
2367 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002368 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002369 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002370 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2371 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002372 return SVTy->getElementType()->isIntOrIntVector() &&
2373 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002374 SVTy->getNumElements() == DVTy->getNumElements();
2375 }
2376 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002377 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002380 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2381 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002382 return SVTy->getElementType()->isFPOrFPVector() &&
2383 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002384 SVTy->getNumElements() == DVTy->getNumElements();
2385 }
2386 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002387 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002388 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002389 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002390 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002391 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392 case Instruction::BitCast:
2393 // BitCast implies a no-op cast of type only. No bits change.
2394 // However, you can't cast pointers to anything but pointers.
2395 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2396 return false;
2397
Duncan Sands55e50902008-01-06 10:12:28 +00002398 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002399 // these cases, the cast is okay if the source and destination bit widths
2400 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002401 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402 }
2403}
2404
2405TruncInst::TruncInst(
2406 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2407) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002408 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002409}
2410
2411TruncInst::TruncInst(
2412 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2413) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002414 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002415}
2416
2417ZExtInst::ZExtInst(
2418 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2419) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002420 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002421}
2422
2423ZExtInst::ZExtInst(
2424 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2425) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002426 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002427}
2428SExtInst::SExtInst(
2429 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2430) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002431 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002432}
2433
Jeff Cohencc08c832006-12-02 02:22:01 +00002434SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2436) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002437 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002438}
2439
2440FPTruncInst::FPTruncInst(
2441 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2442) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002443 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444}
2445
2446FPTruncInst::FPTruncInst(
2447 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2448) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002449 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450}
2451
2452FPExtInst::FPExtInst(
2453 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2454) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002455 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456}
2457
2458FPExtInst::FPExtInst(
2459 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2460) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002461 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002462}
2463
2464UIToFPInst::UIToFPInst(
2465 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2466) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002467 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002468}
2469
2470UIToFPInst::UIToFPInst(
2471 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2472) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002473 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002474}
2475
2476SIToFPInst::SIToFPInst(
2477 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2478) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002479 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002480}
2481
2482SIToFPInst::SIToFPInst(
2483 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2484) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002485 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002486}
2487
2488FPToUIInst::FPToUIInst(
2489 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2490) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002491 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002492}
2493
2494FPToUIInst::FPToUIInst(
2495 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2496) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002497 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002498}
2499
2500FPToSIInst::FPToSIInst(
2501 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2502) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002503 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002504}
2505
2506FPToSIInst::FPToSIInst(
2507 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2508) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002509 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002510}
2511
2512PtrToIntInst::PtrToIntInst(
2513 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2514) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002515 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516}
2517
2518PtrToIntInst::PtrToIntInst(
2519 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2520) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002521 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522}
2523
2524IntToPtrInst::IntToPtrInst(
2525 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2526) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002527 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528}
2529
2530IntToPtrInst::IntToPtrInst(
2531 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2532) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002533 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002534}
2535
2536BitCastInst::BitCastInst(
2537 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2538) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002539 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540}
2541
2542BitCastInst::BitCastInst(
2543 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2544) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002545 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002546}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002547
2548//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002549// CmpInst Classes
2550//===----------------------------------------------------------------------===//
2551
Nate Begemand2195702008-05-12 19:01:56 +00002552CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2553 Value *LHS, Value *RHS, const std::string &Name,
2554 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002555 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002556 OperandTraits<CmpInst>::op_begin(this),
2557 OperandTraits<CmpInst>::operands(this),
2558 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002559 Op<0>() = LHS;
2560 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002561 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002562 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002563}
Gabor Greiff6caff662008-05-10 08:32:32 +00002564
Nate Begemand2195702008-05-12 19:01:56 +00002565CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2566 Value *LHS, Value *RHS, const std::string &Name,
2567 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002568 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002569 OperandTraits<CmpInst>::op_begin(this),
2570 OperandTraits<CmpInst>::operands(this),
2571 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002572 Op<0>() = LHS;
2573 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002574 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002575 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002576}
2577
2578CmpInst *
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002579CmpInst::Create(LLVMContext &Context, OtherOps Op, unsigned short predicate,
2580 Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002581 const std::string &Name, Instruction *InsertBefore) {
2582 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002583 if (InsertBefore)
2584 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2585 S1, S2, Name);
2586 else
2587 return new ICmpInst(Context, CmpInst::Predicate(predicate),
2588 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002589 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002590
2591 if (InsertBefore)
2592 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2593 S1, S2, Name);
2594 else
2595 return new FCmpInst(Context, CmpInst::Predicate(predicate),
2596 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002597}
2598
2599CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002600CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002601 const std::string &Name, BasicBlock *InsertAtEnd) {
2602 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002603 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2604 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002605 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002606 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2607 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002608}
2609
2610void CmpInst::swapOperands() {
2611 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2612 IC->swapOperands();
2613 else
2614 cast<FCmpInst>(this)->swapOperands();
2615}
2616
2617bool CmpInst::isCommutative() {
2618 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2619 return IC->isCommutative();
2620 return cast<FCmpInst>(this)->isCommutative();
2621}
2622
2623bool CmpInst::isEquality() {
2624 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2625 return IC->isEquality();
2626 return cast<FCmpInst>(this)->isEquality();
2627}
2628
2629
Dan Gohman4e724382008-05-31 02:47:54 +00002630CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002631 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002632 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002633 case ICMP_EQ: return ICMP_NE;
2634 case ICMP_NE: return ICMP_EQ;
2635 case ICMP_UGT: return ICMP_ULE;
2636 case ICMP_ULT: return ICMP_UGE;
2637 case ICMP_UGE: return ICMP_ULT;
2638 case ICMP_ULE: return ICMP_UGT;
2639 case ICMP_SGT: return ICMP_SLE;
2640 case ICMP_SLT: return ICMP_SGE;
2641 case ICMP_SGE: return ICMP_SLT;
2642 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002643
Dan Gohman4e724382008-05-31 02:47:54 +00002644 case FCMP_OEQ: return FCMP_UNE;
2645 case FCMP_ONE: return FCMP_UEQ;
2646 case FCMP_OGT: return FCMP_ULE;
2647 case FCMP_OLT: return FCMP_UGE;
2648 case FCMP_OGE: return FCMP_ULT;
2649 case FCMP_OLE: return FCMP_UGT;
2650 case FCMP_UEQ: return FCMP_ONE;
2651 case FCMP_UNE: return FCMP_OEQ;
2652 case FCMP_UGT: return FCMP_OLE;
2653 case FCMP_ULT: return FCMP_OGE;
2654 case FCMP_UGE: return FCMP_OLT;
2655 case FCMP_ULE: return FCMP_OGT;
2656 case FCMP_ORD: return FCMP_UNO;
2657 case FCMP_UNO: return FCMP_ORD;
2658 case FCMP_TRUE: return FCMP_FALSE;
2659 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002660 }
2661}
2662
Reid Spencer266e42b2006-12-23 06:05:41 +00002663ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2664 switch (pred) {
2665 default: assert(! "Unknown icmp predicate!");
2666 case ICMP_EQ: case ICMP_NE:
2667 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2668 return pred;
2669 case ICMP_UGT: return ICMP_SGT;
2670 case ICMP_ULT: return ICMP_SLT;
2671 case ICMP_UGE: return ICMP_SGE;
2672 case ICMP_ULE: return ICMP_SLE;
2673 }
2674}
2675
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002676ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2677 switch (pred) {
2678 default: assert(! "Unknown icmp predicate!");
2679 case ICMP_EQ: case ICMP_NE:
2680 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2681 return pred;
2682 case ICMP_SGT: return ICMP_UGT;
2683 case ICMP_SLT: return ICMP_ULT;
2684 case ICMP_SGE: return ICMP_UGE;
2685 case ICMP_SLE: return ICMP_ULE;
2686 }
2687}
2688
Reid Spencer266e42b2006-12-23 06:05:41 +00002689bool ICmpInst::isSignedPredicate(Predicate pred) {
2690 switch (pred) {
2691 default: assert(! "Unknown icmp predicate!");
2692 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2693 return true;
2694 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2695 case ICMP_UGE: case ICMP_ULE:
2696 return false;
2697 }
2698}
2699
Reid Spencer0286bc12007-02-28 22:00:54 +00002700/// Initialize a set of values that all satisfy the condition with C.
2701///
2702ConstantRange
2703ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2704 APInt Lower(C);
2705 APInt Upper(C);
2706 uint32_t BitWidth = C.getBitWidth();
2707 switch (pred) {
Torok Edwin56d06592009-07-11 20:10:48 +00002708 default: LLVM_UNREACHABLE("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002709 case ICmpInst::ICMP_EQ: Upper++; break;
2710 case ICmpInst::ICMP_NE: Lower++; break;
2711 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2712 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2713 case ICmpInst::ICMP_UGT:
2714 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2715 break;
2716 case ICmpInst::ICMP_SGT:
2717 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2718 break;
2719 case ICmpInst::ICMP_ULE:
2720 Lower = APInt::getMinValue(BitWidth); Upper++;
2721 break;
2722 case ICmpInst::ICMP_SLE:
2723 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2724 break;
2725 case ICmpInst::ICMP_UGE:
2726 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2727 break;
2728 case ICmpInst::ICMP_SGE:
2729 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2730 break;
2731 }
2732 return ConstantRange(Lower, Upper);
2733}
2734
Dan Gohman4e724382008-05-31 02:47:54 +00002735CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002736 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002737 default: assert(!"Unknown cmp predicate!");
2738 case ICMP_EQ: case ICMP_NE:
2739 return pred;
2740 case ICMP_SGT: return ICMP_SLT;
2741 case ICMP_SLT: return ICMP_SGT;
2742 case ICMP_SGE: return ICMP_SLE;
2743 case ICMP_SLE: return ICMP_SGE;
2744 case ICMP_UGT: return ICMP_ULT;
2745 case ICMP_ULT: return ICMP_UGT;
2746 case ICMP_UGE: return ICMP_ULE;
2747 case ICMP_ULE: return ICMP_UGE;
2748
Reid Spencerd9436b62006-11-20 01:22:35 +00002749 case FCMP_FALSE: case FCMP_TRUE:
2750 case FCMP_OEQ: case FCMP_ONE:
2751 case FCMP_UEQ: case FCMP_UNE:
2752 case FCMP_ORD: case FCMP_UNO:
2753 return pred;
2754 case FCMP_OGT: return FCMP_OLT;
2755 case FCMP_OLT: return FCMP_OGT;
2756 case FCMP_OGE: return FCMP_OLE;
2757 case FCMP_OLE: return FCMP_OGE;
2758 case FCMP_UGT: return FCMP_ULT;
2759 case FCMP_ULT: return FCMP_UGT;
2760 case FCMP_UGE: return FCMP_ULE;
2761 case FCMP_ULE: return FCMP_UGE;
2762 }
2763}
2764
Reid Spencer266e42b2006-12-23 06:05:41 +00002765bool CmpInst::isUnsigned(unsigned short predicate) {
2766 switch (predicate) {
2767 default: return false;
2768 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2769 case ICmpInst::ICMP_UGE: return true;
2770 }
2771}
2772
2773bool CmpInst::isSigned(unsigned short predicate){
2774 switch (predicate) {
2775 default: return false;
2776 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2777 case ICmpInst::ICMP_SGE: return true;
2778 }
2779}
2780
2781bool CmpInst::isOrdered(unsigned short predicate) {
2782 switch (predicate) {
2783 default: return false;
2784 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2785 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2786 case FCmpInst::FCMP_ORD: return true;
2787 }
2788}
2789
2790bool CmpInst::isUnordered(unsigned short predicate) {
2791 switch (predicate) {
2792 default: return false;
2793 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2794 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2795 case FCmpInst::FCMP_UNO: return true;
2796 }
2797}
2798
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002799//===----------------------------------------------------------------------===//
2800// SwitchInst Implementation
2801//===----------------------------------------------------------------------===//
2802
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002803void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002804 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002805 ReservedSpace = 2+NumCases*2;
2806 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002807 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002808
Gabor Greif2d3024d2008-05-26 21:33:52 +00002809 OperandList[0] = Value;
2810 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002811}
2812
Chris Lattner2195fc42007-02-24 00:55:48 +00002813/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2814/// switch on and a default destination. The number of additional cases can
2815/// be specified here to make memory allocation more efficient. This
2816/// constructor can also autoinsert before another instruction.
2817SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2818 Instruction *InsertBefore)
2819 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2820 init(Value, Default, NumCases);
2821}
2822
2823/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2824/// switch on and a default destination. The number of additional cases can
2825/// be specified here to make memory allocation more efficient. This
2826/// constructor also autoinserts at the end of the specified BasicBlock.
2827SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2828 BasicBlock *InsertAtEnd)
2829 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2830 init(Value, Default, NumCases);
2831}
2832
Misha Brukmanb1c93172005-04-21 23:48:37 +00002833SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002834 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002835 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002836 Use *OL = OperandList, *InOL = SI.OperandList;
2837 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002838 OL[i] = InOL[i];
2839 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002840 }
2841}
2842
Gordon Henriksen14a55692007-12-10 02:14:30 +00002843SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002844 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002845}
2846
2847
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002848/// addCase - Add an entry to the switch instruction...
2849///
Chris Lattner47ac1872005-02-24 05:32:09 +00002850void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002851 unsigned OpNo = NumOperands;
2852 if (OpNo+2 > ReservedSpace)
2853 resizeOperands(0); // Get more space!
2854 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002855 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002856 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002857 OperandList[OpNo] = OnVal;
2858 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002859}
2860
2861/// removeCase - This method removes the specified successor from the switch
2862/// instruction. Note that this cannot be used to remove the default
2863/// destination (successor #0).
2864///
2865void SwitchInst::removeCase(unsigned idx) {
2866 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002867 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2868
2869 unsigned NumOps = getNumOperands();
2870 Use *OL = OperandList;
2871
2872 // Move everything after this operand down.
2873 //
2874 // FIXME: we could just swap with the end of the list, then erase. However,
2875 // client might not expect this to happen. The code as it is thrashes the
2876 // use/def lists, which is kinda lame.
2877 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2878 OL[i-2] = OL[i];
2879 OL[i-2+1] = OL[i+1];
2880 }
2881
2882 // Nuke the last value.
2883 OL[NumOps-2].set(0);
2884 OL[NumOps-2+1].set(0);
2885 NumOperands = NumOps-2;
2886}
2887
2888/// resizeOperands - resize operands - This adjusts the length of the operands
2889/// list according to the following behavior:
2890/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002891/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002892/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2893/// 3. If NumOps == NumOperands, trim the reserved space.
2894///
2895void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002896 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002897 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002898 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002899 } else if (NumOps*2 > NumOperands) {
2900 // No resize needed.
2901 if (ReservedSpace >= NumOps) return;
2902 } else if (NumOps == NumOperands) {
2903 if (ReservedSpace == NumOps) return;
2904 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002905 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002906 }
2907
2908 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002909 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002910 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002911 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002912 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002913 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002914 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002915 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002916}
2917
2918
2919BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2920 return getSuccessor(idx);
2921}
2922unsigned SwitchInst::getNumSuccessorsV() const {
2923 return getNumSuccessors();
2924}
2925void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2926 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002927}
Chris Lattnerf22be932004-10-15 23:52:53 +00002928
Chris Lattnerf22be932004-10-15 23:52:53 +00002929// Define these methods here so vtables don't get emitted into every translation
2930// unit that uses these classes.
2931
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002932GetElementPtrInst *GetElementPtrInst::clone(LLVMContext&) const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002933 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002934}
2935
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002936BinaryOperator *BinaryOperator::clone(LLVMContext&) const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002937 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002938}
2939
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002940FCmpInst* FCmpInst::clone(LLVMContext &Context) const {
2941 return new FCmpInst(Context, getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002942}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002943ICmpInst* ICmpInst::clone(LLVMContext &Context) const {
2944 return new ICmpInst(Context, getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002945}
2946
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002947ExtractValueInst *ExtractValueInst::clone(LLVMContext&) const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002948 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002949}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002950InsertValueInst *InsertValueInst::clone(LLVMContext&) const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002951 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002952}
2953
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002954MallocInst *MallocInst::clone(LLVMContext&) const {
2955 return new MallocInst(*this);
2956}
Dan Gohman0752bff2008-05-23 00:36:11 +00002957
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002958AllocaInst *AllocaInst::clone(LLVMContext&) const {
2959 return new AllocaInst(*this);
2960}
2961
2962FreeInst *FreeInst::clone(LLVMContext&) const {
2963 return new FreeInst(getOperand(0));
2964}
2965
2966LoadInst *LoadInst::clone(LLVMContext&) const {
2967 return new LoadInst(*this);
2968}
2969
2970StoreInst *StoreInst::clone(LLVMContext&) const {
2971 return new StoreInst(*this);
2972}
2973
2974CastInst *TruncInst::clone(LLVMContext&) const {
2975 return new TruncInst(*this);
2976}
2977
2978CastInst *ZExtInst::clone(LLVMContext&) const {
2979 return new ZExtInst(*this);
2980}
2981
2982CastInst *SExtInst::clone(LLVMContext&) const {
2983 return new SExtInst(*this);
2984}
2985
2986CastInst *FPTruncInst::clone(LLVMContext&) const {
2987 return new FPTruncInst(*this);
2988}
2989
2990CastInst *FPExtInst::clone(LLVMContext&) const {
2991 return new FPExtInst(*this);
2992}
2993
2994CastInst *UIToFPInst::clone(LLVMContext&) const {
2995 return new UIToFPInst(*this);
2996}
2997
2998CastInst *SIToFPInst::clone(LLVMContext&) const {
2999 return new SIToFPInst(*this);
3000}
3001
3002CastInst *FPToUIInst::clone(LLVMContext&) const {
3003 return new FPToUIInst(*this);
3004}
3005
3006CastInst *FPToSIInst::clone(LLVMContext&) const {
3007 return new FPToSIInst(*this);
3008}
3009
3010CastInst *PtrToIntInst::clone(LLVMContext&) const {
3011 return new PtrToIntInst(*this);
3012}
3013
3014CastInst *IntToPtrInst::clone(LLVMContext&) const {
3015 return new IntToPtrInst(*this);
3016}
3017
3018CastInst *BitCastInst::clone(LLVMContext&) const {
3019 return new BitCastInst(*this);
3020}
3021
3022CallInst *CallInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00003023 return new(getNumOperands()) CallInst(*this);
3024}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003025
3026SelectInst *SelectInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00003027 return new(getNumOperands()) SelectInst(*this);
3028}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003029
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003030VAArgInst *VAArgInst::clone(LLVMContext&) const {
3031 return new VAArgInst(*this);
3032}
3033
3034ExtractElementInst *ExtractElementInst::clone(LLVMContext&) const {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003035 return new ExtractElementInst(*this);
3036}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003037
3038InsertElementInst *InsertElementInst::clone(LLVMContext&) const {
Gabor Greife9ecc682008-04-06 20:25:17 +00003039 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003040}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003041
3042ShuffleVectorInst *ShuffleVectorInst::clone(LLVMContext&) const {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003043 return new ShuffleVectorInst(*this);
3044}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003045
3046PHINode *PHINode::clone(LLVMContext&) const {
3047 return new PHINode(*this);
3048}
3049
3050ReturnInst *ReturnInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00003051 return new(getNumOperands()) ReturnInst(*this);
3052}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003053
3054BranchInst *BranchInst::clone(LLVMContext&) const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003055 unsigned Ops(getNumOperands());
3056 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003057}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003058
3059SwitchInst *SwitchInst::clone(LLVMContext&) const {
3060 return new SwitchInst(*this);
3061}
3062
3063InvokeInst *InvokeInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00003064 return new(getNumOperands()) InvokeInst(*this);
3065}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003066
3067UnwindInst *UnwindInst::clone(LLVMContext&) const {
3068 return new UnwindInst();
3069}
3070
3071UnreachableInst *UnreachableInst::clone(LLVMContext&) const {
3072 return new UnreachableInst();
3073}