blob: a5e082c2377ad53629cc6d9bce7857fd00c22e95 [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...
Owen Andersond420fd42009-07-16 00:03:07 +0000190 replaceAllUsesWith(getType()->getContext().getUndef(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000191 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
Owen Andersond420fd42009-07-16 00:03:07 +0000234 return
235 getType()->getContext().getUndef(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000236 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000237
Nate Begemanb3923212005-08-04 23:24:19 +0000238 // Otherwise if all of the incoming values are the same for the PHI, replace
239 // the PHI node with the incoming value.
240 //
241 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000242 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000243 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000244 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000245 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000246 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000247 if (InVal && getIncomingValue(i) != InVal)
248 return 0; // Not the same, bail out.
249 else
250 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000251 }
Nate Begemanb3923212005-08-04 23:24:19 +0000252
253 // The only case that could cause InVal to be null is if we have a PHI node
254 // that only has entries for itself. In this case, there is no entry into the
255 // loop, so kill the PHI.
256 //
Owen Andersond420fd42009-07-16 00:03:07 +0000257 if (InVal == 0) InVal = getType()->getContext().getUndef(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000258
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000259 // If we have a PHI node like phi(X, undef, X), where X is defined by some
260 // instruction, we cannot always return X as the result of the PHI node. Only
261 // do this if X is not an instruction (thus it must dominate the PHI block),
262 // or if the client is prepared to deal with this possibility.
263 if (HasUndefInput && !AllowNonDominatingInstruction)
264 if (Instruction *IV = dyn_cast<Instruction>(InVal))
265 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000266 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000267 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000268 return 0; // Cannot guarantee that InVal dominates this PHINode.
269
Nate Begemanb3923212005-08-04 23:24:19 +0000270 // All of the incoming values are the same, return the value now.
271 return InVal;
272}
273
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000274
275//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000276// CallInst Implementation
277//===----------------------------------------------------------------------===//
278
Gordon Henriksen14a55692007-12-10 02:14:30 +0000279CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000280}
281
Chris Lattner054ba2c2007-02-13 00:58:44 +0000282void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000283 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
284 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000285 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286
Misha Brukmanb1c93172005-04-21 23:48:37 +0000287 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000288 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000289 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290
Chris Lattner054ba2c2007-02-13 00:58:44 +0000291 assert((NumParams == FTy->getNumParams() ||
292 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000293 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000294 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000295 assert((i >= FTy->getNumParams() ||
296 FTy->getParamType(i) == Params[i]->getType()) &&
297 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000298 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000299 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300}
301
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000302void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000303 assert(NumOperands == 3 && "NumOperands not set up?");
304 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000305 OL[0] = Func;
306 OL[1] = Actual1;
307 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000308
309 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000310 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000311 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000313 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000314 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000316 assert((0 >= FTy->getNumParams() ||
317 FTy->getParamType(0) == Actual1->getType()) &&
318 "Calling a function with a bad signature!");
319 assert((1 >= FTy->getNumParams() ||
320 FTy->getParamType(1) == Actual2->getType()) &&
321 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322}
323
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000324void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000325 assert(NumOperands == 2 && "NumOperands not set up?");
326 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000327 OL[0] = Func;
328 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000329
330 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000332 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000333
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000334 assert((FTy->getNumParams() == 1 ||
335 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000337 assert((0 == FTy->getNumParams() ||
338 FTy->getParamType(0) == Actual->getType()) &&
339 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000340}
341
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000342void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000343 assert(NumOperands == 1 && "NumOperands not set up?");
344 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000345 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000346
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000347 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000349 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000351 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000352}
353
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000354CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000355 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000356 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
357 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000358 Instruction::Call,
359 OperandTraits<CallInst>::op_end(this) - 2,
360 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000361 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000362 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000363}
364
365CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
366 BasicBlock *InsertAtEnd)
367 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
368 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000369 Instruction::Call,
370 OperandTraits<CallInst>::op_end(this) - 2,
371 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000372 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000373 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000374}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000375CallInst::CallInst(Value *Func, const std::string &Name,
376 Instruction *InsertBefore)
377 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
378 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000379 Instruction::Call,
380 OperandTraits<CallInst>::op_end(this) - 1,
381 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000382 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000383 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000384}
385
386CallInst::CallInst(Value *Func, const std::string &Name,
387 BasicBlock *InsertAtEnd)
388 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
389 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000390 Instruction::Call,
391 OperandTraits<CallInst>::op_end(this) - 1,
392 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000393 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000394 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000395}
396
Misha Brukmanb1c93172005-04-21 23:48:37 +0000397CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000398 : Instruction(CI.getType(), Instruction::Call,
399 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000400 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000401 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000402 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000403 Use *OL = OperandList;
404 Use *InOL = CI.OperandList;
405 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000406 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000407}
408
Devang Patel4c758ea2008-09-25 21:00:45 +0000409void CallInst::addAttribute(unsigned i, Attributes attr) {
410 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000411 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000412 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000413}
414
Devang Patel4c758ea2008-09-25 21:00:45 +0000415void CallInst::removeAttribute(unsigned i, Attributes attr) {
416 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000417 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000418 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000419}
420
Devang Patelba3fa6c2008-09-23 23:03:40 +0000421bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000422 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000423 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000424 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000425 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000426 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000427}
428
Duncan Sands5208d1a2007-11-28 17:07:01 +0000429
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000430//===----------------------------------------------------------------------===//
431// InvokeInst Implementation
432//===----------------------------------------------------------------------===//
433
434void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000435 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000436 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Bill Wendling4bb96e92009-03-13 21:15:59 +0000437 Use *OL = OperandList;
438 OL[0] = Fn;
439 OL[1] = IfNormal;
440 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000441 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000442 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000443 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000444
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000445 assert(((NumArgs == FTy->getNumParams()) ||
446 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000447 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000448
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000449 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000450 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000451 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000452 "Invoking a function with a bad signature!");
453
Bill Wendling4bb96e92009-03-13 21:15:59 +0000454 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000455 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000456}
457
Misha Brukmanb1c93172005-04-21 23:48:37 +0000458InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000459 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000460 OperandTraits<InvokeInst>::op_end(this)
461 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000462 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000463 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000464 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000465 Use *OL = OperandList, *InOL = II.OperandList;
466 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000467 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000468}
469
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000470BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
471 return getSuccessor(idx);
472}
473unsigned InvokeInst::getNumSuccessorsV() const {
474 return getNumSuccessors();
475}
476void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
477 return setSuccessor(idx, B);
478}
479
Devang Patelba3fa6c2008-09-23 23:03:40 +0000480bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000481 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000482 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000483 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000484 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000485 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000486}
487
Devang Patel4c758ea2008-09-25 21:00:45 +0000488void InvokeInst::addAttribute(unsigned i, Attributes attr) {
489 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000490 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000491 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000492}
493
Devang Patel4c758ea2008-09-25 21:00:45 +0000494void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
495 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000496 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000497 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000498}
499
Duncan Sands5208d1a2007-11-28 17:07:01 +0000500
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000501//===----------------------------------------------------------------------===//
502// ReturnInst Implementation
503//===----------------------------------------------------------------------===//
504
Chris Lattner2195fc42007-02-24 00:55:48 +0000505ReturnInst::ReturnInst(const ReturnInst &RI)
506 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000507 OperandTraits<ReturnInst>::op_end(this) -
508 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000509 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000510 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000511 Op<0>() = RI.Op<0>();
Chris Lattner2195fc42007-02-24 00:55:48 +0000512}
513
514ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000515 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000516 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
517 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000518 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000519 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000520}
521ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000522 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000523 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
524 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000525 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000526 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000527}
528ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000529 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000530 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000531}
532
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000533unsigned ReturnInst::getNumSuccessorsV() const {
534 return getNumSuccessors();
535}
536
Devang Patelae682fb2008-02-26 17:56:20 +0000537/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
538/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000539void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000540 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000541}
542
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000543BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000544 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000545 return 0;
546}
547
Devang Patel59643e52008-02-23 00:35:18 +0000548ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000549}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000550
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000551//===----------------------------------------------------------------------===//
552// UnwindInst Implementation
553//===----------------------------------------------------------------------===//
554
Chris Lattner2195fc42007-02-24 00:55:48 +0000555UnwindInst::UnwindInst(Instruction *InsertBefore)
556 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
557}
558UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
559 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
560}
561
562
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000563unsigned UnwindInst::getNumSuccessorsV() const {
564 return getNumSuccessors();
565}
566
567void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000568 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000569}
570
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000571BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000572 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000573 return 0;
574}
575
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000576//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000577// UnreachableInst Implementation
578//===----------------------------------------------------------------------===//
579
Chris Lattner2195fc42007-02-24 00:55:48 +0000580UnreachableInst::UnreachableInst(Instruction *InsertBefore)
581 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
582}
583UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
584 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
585}
586
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000587unsigned UnreachableInst::getNumSuccessorsV() const {
588 return getNumSuccessors();
589}
590
591void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000592 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000593}
594
595BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000596 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000597 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000598}
599
600//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000601// BranchInst Implementation
602//===----------------------------------------------------------------------===//
603
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000604void BranchInst::AssertOK() {
605 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000606 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000607 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000608}
609
Chris Lattner2195fc42007-02-24 00:55:48 +0000610BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000611 : TerminatorInst(Type::VoidTy, Instruction::Br,
612 OperandTraits<BranchInst>::op_end(this) - 1,
613 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000614 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000615 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000616}
617BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
618 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000619 : TerminatorInst(Type::VoidTy, Instruction::Br,
620 OperandTraits<BranchInst>::op_end(this) - 3,
621 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000622 Op<-1>() = IfTrue;
623 Op<-2>() = IfFalse;
624 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000625#ifndef NDEBUG
626 AssertOK();
627#endif
628}
629
630BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000631 : TerminatorInst(Type::VoidTy, Instruction::Br,
632 OperandTraits<BranchInst>::op_end(this) - 1,
633 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000634 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000635 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000636}
637
638BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
639 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000640 : TerminatorInst(Type::VoidTy, Instruction::Br,
641 OperandTraits<BranchInst>::op_end(this) - 3,
642 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000643 Op<-1>() = IfTrue;
644 Op<-2>() = IfFalse;
645 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000646#ifndef NDEBUG
647 AssertOK();
648#endif
649}
650
651
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000652BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000653 TerminatorInst(Type::VoidTy, Instruction::Br,
654 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
655 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000656 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000657 if (BI.getNumOperands() != 1) {
658 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000659 Op<-3>() = BI.Op<-3>();
660 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000661 }
662}
663
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000664
665Use* Use::getPrefix() {
666 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
667 if (PotentialPrefix.getOpaqueValue())
668 return 0;
669
670 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
671}
672
673BranchInst::~BranchInst() {
674 if (NumOperands == 1) {
675 if (Use *Prefix = OperandList->getPrefix()) {
676 Op<-1>() = 0;
677 //
678 // mark OperandList to have a special value for scrutiny
679 // by baseclass destructors and operator delete
680 OperandList = Prefix;
681 } else {
682 NumOperands = 3;
683 OperandList = op_begin();
684 }
685 }
686}
687
688
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000689BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
690 return getSuccessor(idx);
691}
692unsigned BranchInst::getNumSuccessorsV() const {
693 return getNumSuccessors();
694}
695void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
696 setSuccessor(idx, B);
697}
698
699
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000700//===----------------------------------------------------------------------===//
701// AllocationInst Implementation
702//===----------------------------------------------------------------------===//
703
Owen Andersonb6b25302009-07-14 23:09:55 +0000704static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000705 if (!Amt)
Owen Andersonb6b25302009-07-14 23:09:55 +0000706 Amt = Context.getConstantInt(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000707 else {
708 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000709 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000710 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000711 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000712 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000713 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000714}
715
Owen Anderson4fdeba92009-07-15 23:53:25 +0000716AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000717 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000718 Instruction *InsertBefore)
Owen Anderson4fdeba92009-07-15 23:53:25 +0000719 : UnaryInstruction(Ty->getContext().getPointerTypeUnqual(Ty), iTy,
720 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000721 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000722 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000723 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000724}
725
Owen Anderson4fdeba92009-07-15 23:53:25 +0000726AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000727 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000728 BasicBlock *InsertAtEnd)
Owen Anderson4fdeba92009-07-15 23:53:25 +0000729 : UnaryInstruction(Ty->getContext().getPointerTypeUnqual(Ty), iTy,
730 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000731 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000732 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000733 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000734}
735
Gordon Henriksen14a55692007-12-10 02:14:30 +0000736// Out of line virtual method, so the vtable, etc has a home.
737AllocationInst::~AllocationInst() {
738}
739
Dan Gohmanaa583d72008-03-24 16:55:58 +0000740void AllocationInst::setAlignment(unsigned Align) {
741 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
742 SubclassData = Log2_32(Align) + 1;
743 assert(getAlignment() == Align && "Alignment representation error!");
744}
745
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000746bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000747 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
748 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000749 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000750}
751
752const Type *AllocationInst::getAllocatedType() const {
753 return getType()->getElementType();
754}
755
756AllocaInst::AllocaInst(const AllocaInst &AI)
Owen Anderson4fdeba92009-07-15 23:53:25 +0000757 : AllocationInst(AI.getType()->getElementType(),
Owen Andersonb6b25302009-07-14 23:09:55 +0000758 (Value*)AI.getOperand(0), Instruction::Alloca,
759 AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000760}
761
Chris Lattner8b291e62008-11-26 02:54:17 +0000762/// isStaticAlloca - Return true if this alloca is in the entry block of the
763/// function and is a constant size. If so, the code generator will fold it
764/// into the prolog/epilog code, so it is basically free.
765bool AllocaInst::isStaticAlloca() const {
766 // Must be constant size.
767 if (!isa<ConstantInt>(getArraySize())) return false;
768
769 // Must be in the entry block.
770 const BasicBlock *Parent = getParent();
771 return Parent == &Parent->getParent()->front();
772}
773
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000774MallocInst::MallocInst(const MallocInst &MI)
Owen Anderson4fdeba92009-07-15 23:53:25 +0000775 : AllocationInst(MI.getType()->getElementType(),
Owen Andersonb6b25302009-07-14 23:09:55 +0000776 (Value*)MI.getOperand(0), Instruction::Malloc,
777 MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000778}
779
780//===----------------------------------------------------------------------===//
781// FreeInst Implementation
782//===----------------------------------------------------------------------===//
783
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000784void FreeInst::AssertOK() {
785 assert(isa<PointerType>(getOperand(0)->getType()) &&
786 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000787}
788
789FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000790 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000791 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000792}
793
794FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000795 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000796 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000797}
798
799
800//===----------------------------------------------------------------------===//
801// LoadInst Implementation
802//===----------------------------------------------------------------------===//
803
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000804void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000805 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000806 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000807}
808
809LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000810 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000811 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000812 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000813 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000814 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000815 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000816}
817
818LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000819 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000820 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000821 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000822 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000823 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000824 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000825}
826
827LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
828 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000829 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000830 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000831 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000832 setAlignment(0);
833 AssertOK();
834 setName(Name);
835}
836
837LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
838 unsigned Align, Instruction *InsertBef)
839 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
840 Load, Ptr, InsertBef) {
841 setVolatile(isVolatile);
842 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000843 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000844 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000845}
846
Dan Gohman68659282007-07-18 20:51:11 +0000847LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
848 unsigned Align, BasicBlock *InsertAE)
849 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
850 Load, Ptr, InsertAE) {
851 setVolatile(isVolatile);
852 setAlignment(Align);
853 AssertOK();
854 setName(Name);
855}
856
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000857LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
858 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000859 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000860 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000861 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000862 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000863 AssertOK();
864 setName(Name);
865}
866
867
868
869LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000870 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
871 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000872 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000873 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000874 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000875 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000876}
877
878LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000879 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
880 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000881 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000882 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000883 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000884 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000885}
886
887LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
888 Instruction *InsertBef)
889: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000890 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000891 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000892 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000893 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000894 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000895}
896
897LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
898 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000899 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
900 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000901 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000902 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000903 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000904 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000905}
906
Christopher Lamb84485702007-04-22 19:24:39 +0000907void LoadInst::setAlignment(unsigned Align) {
908 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
909 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
910}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000911
912//===----------------------------------------------------------------------===//
913// StoreInst Implementation
914//===----------------------------------------------------------------------===//
915
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000916void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000917 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000918 assert(isa<PointerType>(getOperand(1)->getType()) &&
919 "Ptr must have pointer type!");
920 assert(getOperand(0)->getType() ==
921 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000922 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000923}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000924
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000925
926StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000927 : Instruction(Type::VoidTy, Store,
928 OperandTraits<StoreInst>::op_begin(this),
929 OperandTraits<StoreInst>::operands(this),
930 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000931 Op<0>() = val;
932 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000933 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000934 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000935 AssertOK();
936}
937
938StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000939 : Instruction(Type::VoidTy, Store,
940 OperandTraits<StoreInst>::op_begin(this),
941 OperandTraits<StoreInst>::operands(this),
942 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000943 Op<0>() = val;
944 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000945 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000946 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000947 AssertOK();
948}
949
Misha Brukmanb1c93172005-04-21 23:48:37 +0000950StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000951 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000952 : Instruction(Type::VoidTy, Store,
953 OperandTraits<StoreInst>::op_begin(this),
954 OperandTraits<StoreInst>::operands(this),
955 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000956 Op<0>() = val;
957 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000958 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000959 setAlignment(0);
960 AssertOK();
961}
962
963StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
964 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000965 : Instruction(Type::VoidTy, Store,
966 OperandTraits<StoreInst>::op_begin(this),
967 OperandTraits<StoreInst>::operands(this),
968 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000969 Op<0>() = val;
970 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000971 setVolatile(isVolatile);
972 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000973 AssertOK();
974}
975
Misha Brukmanb1c93172005-04-21 23:48:37 +0000976StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000977 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000978 : Instruction(Type::VoidTy, Store,
979 OperandTraits<StoreInst>::op_begin(this),
980 OperandTraits<StoreInst>::operands(this),
981 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000982 Op<0>() = val;
983 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000984 setVolatile(isVolatile);
985 setAlignment(Align);
986 AssertOK();
987}
988
989StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000990 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000991 : Instruction(Type::VoidTy, Store,
992 OperandTraits<StoreInst>::op_begin(this),
993 OperandTraits<StoreInst>::operands(this),
994 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000995 Op<0>() = val;
996 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000997 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000998 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000999 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001000}
1001
Christopher Lamb84485702007-04-22 19:24:39 +00001002void StoreInst::setAlignment(unsigned Align) {
1003 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1004 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1005}
1006
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001007//===----------------------------------------------------------------------===//
1008// GetElementPtrInst Implementation
1009//===----------------------------------------------------------------------===//
1010
Christopher Lambedf07882007-12-17 01:12:55 +00001011static unsigned retrieveAddrSpace(const Value *Val) {
1012 return cast<PointerType>(Val->getType())->getAddressSpace();
1013}
1014
Gabor Greif21ba1842008-06-06 20:28:12 +00001015void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Nate Begeman8a4afd92008-07-25 17:24:13 +00001016 const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001017 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1018 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001019 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001020
Chris Lattner79807c3d2007-01-31 19:47:18 +00001021 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001022 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001023
1024 setName(Name);
Dan Gohman97190a22009-07-17 19:23:21 +00001025
1026 // GetElementPtr instructions have undefined results on overflow by default.
1027 setHasNoPointerOverflow(true);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001028}
1029
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001030void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001031 assert(NumOperands == 2 && "NumOperands not initialized?");
1032 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001033 OL[0] = Ptr;
1034 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001035
1036 setName(Name);
Dan Gohman97190a22009-07-17 19:23:21 +00001037
1038 // GetElementPtr instructions have undefined results on overflow by default.
1039 setHasNoPointerOverflow(true);
Chris Lattner82981202005-05-03 05:43:30 +00001040}
1041
Gabor Greiff6caff662008-05-10 08:32:32 +00001042GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001043 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001044 OperandTraits<GetElementPtrInst>::op_end(this)
1045 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001046 GEPI.getNumOperands()) {
1047 Use *OL = OperandList;
1048 Use *GEPIOL = GEPI.OperandList;
1049 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001050 OL[i] = GEPIOL[i];
Dan Gohman97190a22009-07-17 19:23:21 +00001051
1052 // Transfor the hasNoPointerOverflow() value from the original GEPI.
1053 setHasNoPointerOverflow(GEPI.hasNoPointerOverflow());
Gabor Greiff6caff662008-05-10 08:32:32 +00001054}
1055
Chris Lattner82981202005-05-03 05:43:30 +00001056GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1057 const std::string &Name, Instruction *InBe)
Owen Andersond420fd42009-07-16 00:03:07 +00001058 : Instruction(Ptr->getType()->getContext().getPointerType(
1059 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001060 GetElementPtr,
1061 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1062 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001063 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001064}
1065
1066GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1067 const std::string &Name, BasicBlock *IAE)
Owen Andersond420fd42009-07-16 00:03:07 +00001068 : Instruction(Ptr->getType()->getContext().getPointerType(
1069 checkType(getIndexedType(Ptr->getType(),Idx)),
1070 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001071 GetElementPtr,
1072 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1073 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001074 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001075}
1076
Chris Lattner6090a422009-03-09 04:46:40 +00001077/// getIndexedType - Returns the type of the element that would be accessed with
1078/// a gep instruction with the specified parameters.
1079///
1080/// The Idxs pointer should point to a continuous piece of memory containing the
1081/// indices, either as Value* or uint64_t.
1082///
1083/// A null type is returned if the indices are invalid for the specified
1084/// pointer type.
1085///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001086template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001087static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1088 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001089 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1090 if (!PTy) return 0; // Type isn't a pointer type!
1091 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001092
Chris Lattner6090a422009-03-09 04:46:40 +00001093 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001094 if (NumIdx == 0)
1095 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001096
1097 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001098 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1099 // that contain opaque types) under the assumption that it will be resolved to
1100 // a sane type later.
1101 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001102 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001103
Dan Gohman1ecaf452008-05-31 00:58:22 +00001104 unsigned CurIdx = 1;
1105 for (; CurIdx != NumIdx; ++CurIdx) {
1106 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1107 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001108 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001109 if (!CT->indexValid(Index)) return 0;
1110 Agg = CT->getTypeAtIndex(Index);
1111
1112 // If the new type forwards to another type, then it is in the middle
1113 // of being refined to another type (and hence, may have dropped all
1114 // references to what it was using before). So, use the new forwarded
1115 // type.
1116 if (const Type *Ty = Agg->getForwardedType())
1117 Agg = Ty;
1118 }
1119 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001120}
1121
Matthijs Kooijman04468622008-07-29 08:46:11 +00001122const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1123 Value* const *Idxs,
1124 unsigned NumIdx) {
1125 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1126}
1127
1128const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1129 uint64_t const *Idxs,
1130 unsigned NumIdx) {
1131 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1132}
1133
Chris Lattner82981202005-05-03 05:43:30 +00001134const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1135 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1136 if (!PTy) return 0; // Type isn't a pointer type!
1137
1138 // Check the pointer index.
1139 if (!PTy->indexValid(Idx)) return 0;
1140
Chris Lattnerc2233332005-05-03 16:44:45 +00001141 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001142}
1143
Chris Lattner45f15572007-04-14 00:12:57 +00001144
1145/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1146/// zeros. If so, the result pointer and the first operand have the same
1147/// value, just potentially different types.
1148bool GetElementPtrInst::hasAllZeroIndices() const {
1149 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1150 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1151 if (!CI->isZero()) return false;
1152 } else {
1153 return false;
1154 }
1155 }
1156 return true;
1157}
1158
Chris Lattner27058292007-04-27 20:35:56 +00001159/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1160/// constant integers. If so, the result pointer and the first operand have
1161/// a constant offset between them.
1162bool GetElementPtrInst::hasAllConstantIndices() const {
1163 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1164 if (!isa<ConstantInt>(getOperand(i)))
1165 return false;
1166 }
1167 return true;
1168}
1169
Chris Lattner45f15572007-04-14 00:12:57 +00001170
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001171//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001172// ExtractElementInst Implementation
1173//===----------------------------------------------------------------------===//
1174
1175ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001176 const std::string &Name,
1177 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001178 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001179 ExtractElement,
1180 OperandTraits<ExtractElementInst>::op_begin(this),
1181 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001182 assert(isValidOperands(Val, Index) &&
1183 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001184 Op<0>() = Val;
1185 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001186 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001187}
1188
1189ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001190 const std::string &Name,
1191 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001192 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001193 ExtractElement,
1194 OperandTraits<ExtractElementInst>::op_begin(this),
1195 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001196 assert(isValidOperands(Val, Index) &&
1197 "Invalid extractelement instruction operands!");
1198
Gabor Greif2d3024d2008-05-26 21:33:52 +00001199 Op<0>() = Val;
1200 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001201 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001202}
1203
Chris Lattner65511ff2006-10-05 06:24:58 +00001204
Chris Lattner54865b32006-04-08 04:05:48 +00001205bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001206 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001207 return false;
1208 return true;
1209}
1210
1211
Robert Bocchino23004482006-01-10 19:05:34 +00001212//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001213// InsertElementInst Implementation
1214//===----------------------------------------------------------------------===//
1215
Chris Lattner0875d942006-04-14 22:20:32 +00001216InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001217 : Instruction(IE.getType(), InsertElement,
1218 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001219 Op<0>() = IE.Op<0>();
1220 Op<1>() = IE.Op<1>();
1221 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001222}
Chris Lattner54865b32006-04-08 04:05:48 +00001223InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001224 const std::string &Name,
1225 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001226 : Instruction(Vec->getType(), InsertElement,
1227 OperandTraits<InsertElementInst>::op_begin(this),
1228 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001229 assert(isValidOperands(Vec, Elt, Index) &&
1230 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001231 Op<0>() = Vec;
1232 Op<1>() = Elt;
1233 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001234 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001235}
1236
Chris Lattner54865b32006-04-08 04:05:48 +00001237InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001238 const std::string &Name,
1239 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001240 : Instruction(Vec->getType(), InsertElement,
1241 OperandTraits<InsertElementInst>::op_begin(this),
1242 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001243 assert(isValidOperands(Vec, Elt, Index) &&
1244 "Invalid insertelement instruction operands!");
1245
Gabor Greif2d3024d2008-05-26 21:33:52 +00001246 Op<0>() = Vec;
1247 Op<1>() = Elt;
1248 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001249 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001250}
1251
Chris Lattner54865b32006-04-08 04:05:48 +00001252bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1253 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001254 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001255 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001256
Reid Spencerd84d35b2007-02-15 02:26:10 +00001257 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001258 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001259
Reid Spencer8d9336d2006-12-31 05:26:44 +00001260 if (Index->getType() != Type::Int32Ty)
Dan Gohman4fe64de2009-06-14 23:30:43 +00001261 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001262 return true;
1263}
1264
1265
Robert Bocchinoca27f032006-01-17 20:07:22 +00001266//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001267// ShuffleVectorInst Implementation
1268//===----------------------------------------------------------------------===//
1269
Chris Lattner0875d942006-04-14 22:20:32 +00001270ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001271 : Instruction(SV.getType(), ShuffleVector,
1272 OperandTraits<ShuffleVectorInst>::op_begin(this),
1273 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001274 Op<0>() = SV.Op<0>();
1275 Op<1>() = SV.Op<1>();
1276 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001277}
1278
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001279ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1280 const std::string &Name,
1281 Instruction *InsertBefore)
Owen Andersond420fd42009-07-16 00:03:07 +00001282: Instruction(V1->getType()->getContext().getVectorType(
1283 cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001284 cast<VectorType>(Mask->getType())->getNumElements()),
1285 ShuffleVector,
1286 OperandTraits<ShuffleVectorInst>::op_begin(this),
1287 OperandTraits<ShuffleVectorInst>::operands(this),
1288 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001289 assert(isValidOperands(V1, V2, Mask) &&
1290 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001291 Op<0>() = V1;
1292 Op<1>() = V2;
1293 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001294 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001295}
1296
1297ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Mon P Wang25f01062008-11-10 04:46:22 +00001298 const std::string &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001299 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001300 : Instruction(V1->getType(), ShuffleVector,
1301 OperandTraits<ShuffleVectorInst>::op_begin(this),
1302 OperandTraits<ShuffleVectorInst>::operands(this),
1303 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001304 assert(isValidOperands(V1, V2, Mask) &&
1305 "Invalid shuffle vector instruction operands!");
1306
Gabor Greif2d3024d2008-05-26 21:33:52 +00001307 Op<0>() = V1;
1308 Op<1>() = V2;
1309 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001310 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001311}
1312
Mon P Wang25f01062008-11-10 04:46:22 +00001313bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001314 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001315 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001316 return false;
1317
1318 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1319 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Mon P Wang25f01062008-11-10 04:46:22 +00001320 MaskTy->getElementType() != Type::Int32Ty)
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001321 return false;
1322 return true;
1323}
1324
Chris Lattnerf724e342008-03-02 05:28:33 +00001325/// getMaskValue - Return the index from the shuffle mask for the specified
1326/// output result. This is either -1 if the element is undef or a number less
1327/// than 2*numelements.
1328int ShuffleVectorInst::getMaskValue(unsigned i) const {
1329 const Constant *Mask = cast<Constant>(getOperand(2));
1330 if (isa<UndefValue>(Mask)) return -1;
1331 if (isa<ConstantAggregateZero>(Mask)) return 0;
1332 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1333 assert(i < MaskCV->getNumOperands() && "Index out of range");
1334
1335 if (isa<UndefValue>(MaskCV->getOperand(i)))
1336 return -1;
1337 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1338}
1339
Dan Gohman12fce772008-05-15 19:50:34 +00001340//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001341// InsertValueInst Class
1342//===----------------------------------------------------------------------===//
1343
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001344void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1345 unsigned NumIdx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001346 assert(NumOperands == 2 && "NumOperands not initialized?");
1347 Op<0>() = Agg;
1348 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001349
Dan Gohman1ecaf452008-05-31 00:58:22 +00001350 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001351 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001352}
1353
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001354void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1355 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001356 assert(NumOperands == 2 && "NumOperands not initialized?");
1357 Op<0>() = Agg;
1358 Op<1>() = Val;
1359
1360 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001361 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001362}
1363
1364InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001365 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001366 OperandTraits<InsertValueInst>::op_begin(this), 2),
1367 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001368 Op<0>() = IVI.getOperand(0);
1369 Op<1>() = IVI.getOperand(1);
Dan Gohman0752bff2008-05-23 00:36:11 +00001370}
1371
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001372InsertValueInst::InsertValueInst(Value *Agg,
1373 Value *Val,
1374 unsigned Idx,
1375 const std::string &Name,
1376 Instruction *InsertBefore)
1377 : Instruction(Agg->getType(), InsertValue,
1378 OperandTraits<InsertValueInst>::op_begin(this),
1379 2, InsertBefore) {
1380 init(Agg, Val, Idx, Name);
1381}
1382
1383InsertValueInst::InsertValueInst(Value *Agg,
1384 Value *Val,
1385 unsigned Idx,
1386 const std::string &Name,
1387 BasicBlock *InsertAtEnd)
1388 : Instruction(Agg->getType(), InsertValue,
1389 OperandTraits<InsertValueInst>::op_begin(this),
1390 2, InsertAtEnd) {
1391 init(Agg, Val, Idx, Name);
1392}
1393
Dan Gohman0752bff2008-05-23 00:36:11 +00001394//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001395// ExtractValueInst Class
1396//===----------------------------------------------------------------------===//
1397
Gabor Greifcbcc4952008-06-06 21:06:32 +00001398void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Gabor Greif1b9921a2009-01-11 22:33:22 +00001399 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001400 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001401
Dan Gohman1ecaf452008-05-31 00:58:22 +00001402 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001403 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001404}
1405
Gabor Greifcbcc4952008-06-06 21:06:32 +00001406void ExtractValueInst::init(unsigned Idx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001407 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001408
1409 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001410 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001411}
1412
1413ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001414 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001415 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001416}
1417
Dan Gohman12fce772008-05-15 19:50:34 +00001418// getIndexedType - Returns the type of the element that would be extracted
1419// with an extractvalue instruction with the specified parameters.
1420//
1421// A null type is returned if the indices are invalid for the specified
1422// pointer type.
1423//
1424const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001425 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001426 unsigned NumIdx) {
1427 unsigned CurIdx = 0;
1428 for (; CurIdx != NumIdx; ++CurIdx) {
1429 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001430 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1431 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001432 if (!CT->indexValid(Index)) return 0;
1433 Agg = CT->getTypeAtIndex(Index);
1434
1435 // If the new type forwards to another type, then it is in the middle
1436 // of being refined to another type (and hence, may have dropped all
1437 // references to what it was using before). So, use the new forwarded
1438 // type.
1439 if (const Type *Ty = Agg->getForwardedType())
1440 Agg = Ty;
1441 }
1442 return CurIdx == NumIdx ? Agg : 0;
1443}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001444
Dan Gohman4a3125b2008-06-17 21:07:55 +00001445const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001446 unsigned Idx) {
1447 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001448}
1449
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001450//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001451// BinaryOperator Class
1452//===----------------------------------------------------------------------===//
1453
Dan Gohmana5b96452009-06-04 22:49:04 +00001454/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1455/// type is floating-point, to help provide compatibility with an older API.
1456///
1457static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1458 const Type *Ty) {
1459 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1460 if (Ty->isFPOrFPVector()) {
1461 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1462 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1463 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1464 }
1465 return iType;
1466}
1467
Chris Lattner2195fc42007-02-24 00:55:48 +00001468BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1469 const Type *Ty, const std::string &Name,
1470 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001471 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001472 OperandTraits<BinaryOperator>::op_begin(this),
1473 OperandTraits<BinaryOperator>::operands(this),
1474 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001475 Op<0>() = S1;
1476 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001477 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001478 setName(Name);
1479}
1480
1481BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1482 const Type *Ty, const std::string &Name,
1483 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001484 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001485 OperandTraits<BinaryOperator>::op_begin(this),
1486 OperandTraits<BinaryOperator>::operands(this),
1487 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001488 Op<0>() = S1;
1489 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001490 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001491 setName(Name);
1492}
1493
1494
1495void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001496 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001497 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001498 assert(LHS->getType() == RHS->getType() &&
1499 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001500#ifndef NDEBUG
1501 switch (iType) {
1502 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001503 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001504 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001505 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001506 assert(getType()->isIntOrIntVector() &&
1507 "Tried to create an integer operation on a non-integer type!");
1508 break;
1509 case FAdd: case FSub:
1510 case FMul:
1511 assert(getType() == LHS->getType() &&
1512 "Arithmetic operation should return same type as operands!");
1513 assert(getType()->isFPOrFPVector() &&
1514 "Tried to create a floating-point operation on a "
1515 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001516 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001517 case UDiv:
1518 case SDiv:
1519 assert(getType() == LHS->getType() &&
1520 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001521 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1522 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001523 "Incorrect operand type (not integer) for S/UDIV");
1524 break;
1525 case FDiv:
1526 assert(getType() == LHS->getType() &&
1527 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001528 assert(getType()->isFPOrFPVector() &&
1529 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001530 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001531 case URem:
1532 case SRem:
1533 assert(getType() == LHS->getType() &&
1534 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001535 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1536 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001537 "Incorrect operand type (not integer) for S/UREM");
1538 break;
1539 case FRem:
1540 assert(getType() == LHS->getType() &&
1541 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001542 assert(getType()->isFPOrFPVector() &&
1543 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001544 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001545 case Shl:
1546 case LShr:
1547 case AShr:
1548 assert(getType() == LHS->getType() &&
1549 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001550 assert((getType()->isInteger() ||
1551 (isa<VectorType>(getType()) &&
1552 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1553 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001554 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001555 case And: case Or:
1556 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001557 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001558 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001559 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001560 (isa<VectorType>(getType()) &&
1561 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001562 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001563 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001564 default:
1565 break;
1566 }
1567#endif
1568}
1569
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001570BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001571 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001572 Instruction *InsertBefore) {
1573 assert(S1->getType() == S2->getType() &&
1574 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001575 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001576}
1577
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001578BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001579 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001580 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001581 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001582 InsertAtEnd->getInstList().push_back(Res);
1583 return Res;
1584}
1585
Owen Anderson53a52212009-07-13 04:09:18 +00001586BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context,
1587 Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001588 Instruction *InsertBefore) {
Owen Anderson53a52212009-07-13 04:09:18 +00001589 Value *zero = Context.getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001590 return new BinaryOperator(Instruction::Sub,
1591 zero, Op,
1592 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001593}
1594
Owen Anderson53a52212009-07-13 04:09:18 +00001595BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context,
1596 Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001597 BasicBlock *InsertAtEnd) {
Owen Anderson53a52212009-07-13 04:09:18 +00001598 Value *zero = Context.getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001599 return new BinaryOperator(Instruction::Sub,
1600 zero, Op,
1601 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001602}
1603
Owen Anderson53a52212009-07-13 04:09:18 +00001604BinaryOperator *BinaryOperator::CreateFNeg(LLVMContext &Context,
1605 Value *Op, const std::string &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001606 Instruction *InsertBefore) {
Owen Anderson53a52212009-07-13 04:09:18 +00001607 Value *zero = Context.getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001608 return new BinaryOperator(Instruction::FSub,
1609 zero, Op,
1610 Op->getType(), Name, InsertBefore);
1611}
1612
Owen Anderson53a52212009-07-13 04:09:18 +00001613BinaryOperator *BinaryOperator::CreateFNeg(LLVMContext &Context,
1614 Value *Op, const std::string &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001615 BasicBlock *InsertAtEnd) {
Owen Anderson53a52212009-07-13 04:09:18 +00001616 Value *zero = Context.getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001617 return new BinaryOperator(Instruction::FSub,
1618 zero, Op,
1619 Op->getType(), Name, InsertAtEnd);
1620}
1621
Owen Anderson542619e2009-07-13 20:58:05 +00001622BinaryOperator *BinaryOperator::CreateNot(LLVMContext &Context,
1623 Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001624 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001625 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001626 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson542619e2009-07-13 20:58:05 +00001627 C = Context.getAllOnesValue(PTy->getElementType());
Owen Andersonf945a9e2009-07-15 21:51:10 +00001628 C = Context.getConstantVector(
1629 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001630 } else {
Owen Anderson542619e2009-07-13 20:58:05 +00001631 C = Context.getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001632 }
1633
1634 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001635 Op->getType(), Name, InsertBefore);
1636}
1637
Owen Anderson542619e2009-07-13 20:58:05 +00001638BinaryOperator *BinaryOperator::CreateNot(LLVMContext &Context,
1639 Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001640 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001641 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001642 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001643 // Create a vector of all ones values.
Owen Anderson542619e2009-07-13 20:58:05 +00001644 Constant *Elt = Context.getAllOnesValue(PTy->getElementType());
Owen Andersonf945a9e2009-07-15 21:51:10 +00001645 AllOnes = Context.getConstantVector(
1646 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001647 } else {
Owen Anderson542619e2009-07-13 20:58:05 +00001648 AllOnes = Context.getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001649 }
1650
1651 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001652 Op->getType(), Name, InsertAtEnd);
1653}
1654
1655
1656// isConstantAllOnes - Helper function for several functions below
1657static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001658 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1659 return CI->isAllOnesValue();
1660 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1661 return CV->isAllOnesValue();
1662 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001663}
1664
Owen Andersonbb2501b2009-07-13 22:18:28 +00001665bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001666 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1667 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001668 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1669 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001670 return false;
1671}
1672
Owen Andersonbb2501b2009-07-13 22:18:28 +00001673bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001674 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1675 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001676 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1677 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001678 return false;
1679}
1680
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001681bool BinaryOperator::isNot(const Value *V) {
1682 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1683 return (Bop->getOpcode() == Instruction::Xor &&
1684 (isConstantAllOnes(Bop->getOperand(1)) ||
1685 isConstantAllOnes(Bop->getOperand(0))));
1686 return false;
1687}
1688
Chris Lattner2c7d1772005-04-24 07:28:37 +00001689Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001690 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001691}
1692
Chris Lattner2c7d1772005-04-24 07:28:37 +00001693const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1694 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001695}
1696
Dan Gohmana5b96452009-06-04 22:49:04 +00001697Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001698 return cast<BinaryOperator>(BinOp)->getOperand(1);
1699}
1700
1701const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1702 return getFNegArgument(const_cast<Value*>(BinOp));
1703}
1704
Chris Lattner2c7d1772005-04-24 07:28:37 +00001705Value *BinaryOperator::getNotArgument(Value *BinOp) {
1706 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1707 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1708 Value *Op0 = BO->getOperand(0);
1709 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001710 if (isConstantAllOnes(Op0)) return Op1;
1711
1712 assert(isConstantAllOnes(Op1));
1713 return Op0;
1714}
1715
Chris Lattner2c7d1772005-04-24 07:28:37 +00001716const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1717 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001718}
1719
1720
1721// swapOperands - Exchange the two operands to this instruction. This
1722// instruction is safe to use on any binary instruction and does not
1723// modify the semantics of the instruction. If the instruction is
1724// order dependent (SetLT f.e.) the opcode is changed.
1725//
1726bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001727 if (!isCommutative())
1728 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001729 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001730 return false;
1731}
1732
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001733//===----------------------------------------------------------------------===//
1734// CastInst Class
1735//===----------------------------------------------------------------------===//
1736
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001737// Just determine if this cast only deals with integral->integral conversion.
1738bool CastInst::isIntegerCast() const {
1739 switch (getOpcode()) {
1740 default: return false;
1741 case Instruction::ZExt:
1742 case Instruction::SExt:
1743 case Instruction::Trunc:
1744 return true;
1745 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001746 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001747 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001748}
1749
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001750bool CastInst::isLosslessCast() const {
1751 // Only BitCast can be lossless, exit fast if we're not BitCast
1752 if (getOpcode() != Instruction::BitCast)
1753 return false;
1754
1755 // Identity cast is always lossless
1756 const Type* SrcTy = getOperand(0)->getType();
1757 const Type* DstTy = getType();
1758 if (SrcTy == DstTy)
1759 return true;
1760
Reid Spencer8d9336d2006-12-31 05:26:44 +00001761 // Pointer to pointer is always lossless.
1762 if (isa<PointerType>(SrcTy))
1763 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001764 return false; // Other types have no identity values
1765}
1766
1767/// This function determines if the CastInst does not require any bits to be
1768/// changed in order to effect the cast. Essentially, it identifies cases where
1769/// no code gen is necessary for the cast, hence the name no-op cast. For
1770/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001771/// # bitcast i32* %x to i8*
1772/// # bitcast <2 x i32> %x to <4 x i16>
1773/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001774/// @brief Determine if a cast is a no-op.
1775bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1776 switch (getOpcode()) {
1777 default:
1778 assert(!"Invalid CastOp");
1779 case Instruction::Trunc:
1780 case Instruction::ZExt:
1781 case Instruction::SExt:
1782 case Instruction::FPTrunc:
1783 case Instruction::FPExt:
1784 case Instruction::UIToFP:
1785 case Instruction::SIToFP:
1786 case Instruction::FPToUI:
1787 case Instruction::FPToSI:
1788 return false; // These always modify bits
1789 case Instruction::BitCast:
1790 return true; // BitCast never modifies bits.
1791 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001792 return IntPtrTy->getScalarSizeInBits() ==
1793 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001794 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001795 return IntPtrTy->getScalarSizeInBits() ==
1796 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001797 }
1798}
1799
1800/// This function determines if a pair of casts can be eliminated and what
1801/// opcode should be used in the elimination. This assumes that there are two
1802/// instructions like this:
1803/// * %F = firstOpcode SrcTy %x to MidTy
1804/// * %S = secondOpcode MidTy %F to DstTy
1805/// The function returns a resultOpcode so these two casts can be replaced with:
1806/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1807/// If no such cast is permited, the function returns 0.
1808unsigned CastInst::isEliminableCastPair(
1809 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1810 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1811{
1812 // Define the 144 possibilities for these two cast instructions. The values
1813 // in this matrix determine what to do in a given situation and select the
1814 // case in the switch below. The rows correspond to firstOp, the columns
1815 // correspond to secondOp. In looking at the table below, keep in mind
1816 // the following cast properties:
1817 //
1818 // Size Compare Source Destination
1819 // Operator Src ? Size Type Sign Type Sign
1820 // -------- ------------ ------------------- ---------------------
1821 // TRUNC > Integer Any Integral Any
1822 // ZEXT < Integral Unsigned Integer Any
1823 // SEXT < Integral Signed Integer Any
1824 // FPTOUI n/a FloatPt n/a Integral Unsigned
1825 // FPTOSI n/a FloatPt n/a Integral Signed
1826 // UITOFP n/a Integral Unsigned FloatPt n/a
1827 // SITOFP n/a Integral Signed FloatPt n/a
1828 // FPTRUNC > FloatPt n/a FloatPt n/a
1829 // FPEXT < FloatPt n/a FloatPt n/a
1830 // PTRTOINT n/a Pointer n/a Integral Unsigned
1831 // INTTOPTR n/a Integral Unsigned Pointer n/a
1832 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001833 //
1834 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001835 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1836 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001837 // of the produced value (we no longer know the top-part is all zeros).
1838 // Further this conversion is often much more expensive for typical hardware,
1839 // and causes issues when building libgcc. We disallow fptosi+sext for the
1840 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001841 const unsigned numCastOps =
1842 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1843 static const uint8_t CastResults[numCastOps][numCastOps] = {
1844 // T F F U S F F P I B -+
1845 // R Z S P P I I T P 2 N T |
1846 // U E E 2 2 2 2 R E I T C +- secondOp
1847 // N X X U S F F N X N 2 V |
1848 // C T T I I P P C T T P T -+
1849 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1850 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1851 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001852 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1853 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001854 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1855 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1856 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1857 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1858 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1859 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1860 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1861 };
1862
1863 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1864 [secondOp-Instruction::CastOpsBegin];
1865 switch (ElimCase) {
1866 case 0:
1867 // categorically disallowed
1868 return 0;
1869 case 1:
1870 // allowed, use first cast's opcode
1871 return firstOp;
1872 case 2:
1873 // allowed, use second cast's opcode
1874 return secondOp;
1875 case 3:
1876 // no-op cast in second op implies firstOp as long as the DestTy
1877 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001878 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001879 return firstOp;
1880 return 0;
1881 case 4:
1882 // no-op cast in second op implies firstOp as long as the DestTy
1883 // is floating point
1884 if (DstTy->isFloatingPoint())
1885 return firstOp;
1886 return 0;
1887 case 5:
1888 // no-op cast in first op implies secondOp as long as the SrcTy
1889 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001890 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001891 return secondOp;
1892 return 0;
1893 case 6:
1894 // no-op cast in first op implies secondOp as long as the SrcTy
1895 // is a floating point
1896 if (SrcTy->isFloatingPoint())
1897 return secondOp;
1898 return 0;
1899 case 7: {
1900 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001901 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1902 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001903 if (MidSize >= PtrSize)
1904 return Instruction::BitCast;
1905 return 0;
1906 }
1907 case 8: {
1908 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1909 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1910 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001911 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1912 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001913 if (SrcSize == DstSize)
1914 return Instruction::BitCast;
1915 else if (SrcSize < DstSize)
1916 return firstOp;
1917 return secondOp;
1918 }
1919 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1920 return Instruction::ZExt;
1921 case 10:
1922 // fpext followed by ftrunc is allowed if the bit size returned to is
1923 // the same as the original, in which case its just a bitcast
1924 if (SrcTy == DstTy)
1925 return Instruction::BitCast;
1926 return 0; // If the types are not the same we can't eliminate it.
1927 case 11:
1928 // bitcast followed by ptrtoint is allowed as long as the bitcast
1929 // is a pointer to pointer cast.
1930 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1931 return secondOp;
1932 return 0;
1933 case 12:
1934 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1935 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1936 return firstOp;
1937 return 0;
1938 case 13: {
1939 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001940 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1941 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1942 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001943 if (SrcSize <= PtrSize && SrcSize == DstSize)
1944 return Instruction::BitCast;
1945 return 0;
1946 }
1947 case 99:
1948 // cast combination can't happen (error in input). This is for all cases
1949 // where the MidTy is not the same for the two cast instructions.
1950 assert(!"Invalid Cast Combination");
1951 return 0;
1952 default:
1953 assert(!"Error in CastResults table!!!");
1954 return 0;
1955 }
1956 return 0;
1957}
1958
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001959CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001960 const std::string &Name, Instruction *InsertBefore) {
1961 // Construct and return the appropriate CastInst subclass
1962 switch (op) {
1963 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1964 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1965 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1966 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1967 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1968 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1969 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1970 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1971 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1972 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1973 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1974 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1975 default:
1976 assert(!"Invalid opcode provided");
1977 }
1978 return 0;
1979}
1980
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001981CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001982 const std::string &Name, BasicBlock *InsertAtEnd) {
1983 // Construct and return the appropriate CastInst subclass
1984 switch (op) {
1985 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1986 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1987 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1988 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1989 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1990 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1991 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1992 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1993 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1994 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1995 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1996 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1997 default:
1998 assert(!"Invalid opcode provided");
1999 }
2000 return 0;
2001}
2002
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002003CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002004 const std::string &Name,
2005 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002006 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002007 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2008 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002009}
2010
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002011CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002012 const std::string &Name,
2013 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002014 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002015 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2016 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002017}
2018
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002019CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002020 const std::string &Name,
2021 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002022 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002023 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2024 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002025}
2026
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002027CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002028 const std::string &Name,
2029 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002030 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002031 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2032 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002033}
2034
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002035CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002036 const std::string &Name,
2037 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002038 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002039 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2040 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002041}
2042
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002043CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002044 const std::string &Name,
2045 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002046 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002047 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2048 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002049}
2050
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002051CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002052 const std::string &Name,
2053 BasicBlock *InsertAtEnd) {
2054 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002055 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002056 "Invalid cast");
2057
Chris Lattner03c49532007-01-15 02:27:26 +00002058 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002059 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2060 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002061}
2062
2063/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002064CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002065 const std::string &Name,
2066 Instruction *InsertBefore) {
2067 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002068 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002069 "Invalid cast");
2070
Chris Lattner03c49532007-01-15 02:27:26 +00002071 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002072 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2073 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002074}
2075
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002076CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002077 bool isSigned, const std::string &Name,
2078 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002079 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002080 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2081 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002082 Instruction::CastOps opcode =
2083 (SrcBits == DstBits ? Instruction::BitCast :
2084 (SrcBits > DstBits ? Instruction::Trunc :
2085 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002086 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002087}
2088
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002089CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002090 bool isSigned, const std::string &Name,
2091 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002092 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2093 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002094 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2095 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002096 Instruction::CastOps opcode =
2097 (SrcBits == DstBits ? Instruction::BitCast :
2098 (SrcBits > DstBits ? Instruction::Trunc :
2099 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002100 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002101}
2102
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002103CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002104 const std::string &Name,
2105 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002106 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002107 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002108 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2109 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002110 Instruction::CastOps opcode =
2111 (SrcBits == DstBits ? Instruction::BitCast :
2112 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002113 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002114}
2115
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002116CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002117 const std::string &Name,
2118 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002119 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002120 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002121 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2122 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002123 Instruction::CastOps opcode =
2124 (SrcBits == DstBits ? Instruction::BitCast :
2125 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002126 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002127}
2128
Duncan Sands55e50902008-01-06 10:12:28 +00002129// Check whether it is valid to call getCastOpcode for these types.
2130// This routine must be kept in sync with getCastOpcode.
2131bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2132 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2133 return false;
2134
2135 if (SrcTy == DestTy)
2136 return true;
2137
2138 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002139 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2140 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002141
2142 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002143 if (DestTy->isInteger()) { // Casting to integral
2144 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002145 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002146 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002147 return true;
2148 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002149 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002150 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002151 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002152 return isa<PointerType>(SrcTy);
2153 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002154 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2155 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002156 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002157 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002158 return true;
2159 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002160 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002161 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002162 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002163 return false;
2164 }
2165 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002166 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002167 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002168 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002169 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002170 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002171 return DestPTy->getBitWidth() == SrcBits;
2172 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002173 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2174 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002175 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002176 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002177 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002178 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002179 return false;
2180 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002181 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002182 return false;
2183 }
2184}
2185
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002186// Provide a way to get a "cast" where the cast opcode is inferred from the
2187// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002188// logic in the castIsValid function below. This axiom should hold:
2189// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2190// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002191// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002192// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002193Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002194CastInst::getCastOpcode(
2195 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002196 // Get the bit sizes, we'll need these
2197 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002198 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2199 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002200
Duncan Sands55e50902008-01-06 10:12:28 +00002201 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2202 "Only first class types are castable!");
2203
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002204 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002205 if (DestTy->isInteger()) { // Casting to integral
2206 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002207 if (DestBits < SrcBits)
2208 return Trunc; // int -> smaller int
2209 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002210 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002211 return SExt; // signed -> SEXT
2212 else
2213 return ZExt; // unsigned -> ZEXT
2214 } else {
2215 return BitCast; // Same size, No-op cast
2216 }
2217 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002218 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002219 return FPToSI; // FP -> sint
2220 else
2221 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002222 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002223 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002224 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002225 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002226 return BitCast; // Same size, no-op cast
2227 } else {
2228 assert(isa<PointerType>(SrcTy) &&
2229 "Casting from a value that is not first-class type");
2230 return PtrToInt; // ptr -> int
2231 }
2232 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002233 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002234 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002235 return SIToFP; // sint -> FP
2236 else
2237 return UIToFP; // uint -> FP
2238 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2239 if (DestBits < SrcBits) {
2240 return FPTrunc; // FP -> smaller FP
2241 } else if (DestBits > SrcBits) {
2242 return FPExt; // FP -> larger FP
2243 } else {
2244 return BitCast; // same size, no-op cast
2245 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002246 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002247 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002248 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002249 PTy = NULL;
2250 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002251 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002252 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002253 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002254 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2255 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002256 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002257 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002258 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002259 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002260 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002261 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002262 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002263 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002264 }
2265 } else if (isa<PointerType>(DestTy)) {
2266 if (isa<PointerType>(SrcTy)) {
2267 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002268 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002269 return IntToPtr; // int -> ptr
2270 } else {
2271 assert(!"Casting pointer to other than pointer or int");
2272 }
2273 } else {
2274 assert(!"Casting to type that is not first-class");
2275 }
2276
2277 // If we fall through to here we probably hit an assertion cast above
2278 // and assertions are not turned on. Anything we return is an error, so
2279 // BitCast is as good a choice as any.
2280 return BitCast;
2281}
2282
2283//===----------------------------------------------------------------------===//
2284// CastInst SubClass Constructors
2285//===----------------------------------------------------------------------===//
2286
2287/// Check that the construction parameters for a CastInst are correct. This
2288/// could be broken out into the separate constructors but it is useful to have
2289/// it in one place and to eliminate the redundant code for getting the sizes
2290/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002291bool
2292CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002293
2294 // Check for type sanity on the arguments
2295 const Type *SrcTy = S->getType();
2296 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2297 return false;
2298
2299 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002300 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2301 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002302
2303 // Switch on the opcode provided
2304 switch (op) {
2305 default: return false; // This is an input error
2306 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002307 return SrcTy->isIntOrIntVector() &&
2308 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002309 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002310 return SrcTy->isIntOrIntVector() &&
2311 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002312 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002313 return SrcTy->isIntOrIntVector() &&
2314 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002315 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002316 return SrcTy->isFPOrFPVector() &&
2317 DstTy->isFPOrFPVector() &&
2318 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002319 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002320 return SrcTy->isFPOrFPVector() &&
2321 DstTy->isFPOrFPVector() &&
2322 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002323 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002324 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002325 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2326 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002327 return SVTy->getElementType()->isIntOrIntVector() &&
2328 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002329 SVTy->getNumElements() == DVTy->getNumElements();
2330 }
2331 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002332 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002333 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002334 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002335 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2336 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002337 return SVTy->getElementType()->isFPOrFPVector() &&
2338 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002339 SVTy->getNumElements() == DVTy->getNumElements();
2340 }
2341 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002342 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002343 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002344 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002345 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002346 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002347 case Instruction::BitCast:
2348 // BitCast implies a no-op cast of type only. No bits change.
2349 // However, you can't cast pointers to anything but pointers.
2350 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2351 return false;
2352
Duncan Sands55e50902008-01-06 10:12:28 +00002353 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002354 // these cases, the cast is okay if the source and destination bit widths
2355 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002356 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357 }
2358}
2359
2360TruncInst::TruncInst(
2361 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2362) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002363 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002364}
2365
2366TruncInst::TruncInst(
2367 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2368) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002369 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002370}
2371
2372ZExtInst::ZExtInst(
2373 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2374) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002375 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002376}
2377
2378ZExtInst::ZExtInst(
2379 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2380) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002381 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002382}
2383SExtInst::SExtInst(
2384 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2385) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002386 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002387}
2388
Jeff Cohencc08c832006-12-02 02:22:01 +00002389SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002390 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2391) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002392 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393}
2394
2395FPTruncInst::FPTruncInst(
2396 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2397) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002398 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002399}
2400
2401FPTruncInst::FPTruncInst(
2402 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2403) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002404 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002405}
2406
2407FPExtInst::FPExtInst(
2408 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2409) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002410 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002411}
2412
2413FPExtInst::FPExtInst(
2414 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2415) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002416 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002417}
2418
2419UIToFPInst::UIToFPInst(
2420 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2421) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002422 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002423}
2424
2425UIToFPInst::UIToFPInst(
2426 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2427) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002428 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429}
2430
2431SIToFPInst::SIToFPInst(
2432 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2433) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002434 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435}
2436
2437SIToFPInst::SIToFPInst(
2438 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2439) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002440 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002441}
2442
2443FPToUIInst::FPToUIInst(
2444 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2445) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002446 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447}
2448
2449FPToUIInst::FPToUIInst(
2450 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2451) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002452 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453}
2454
2455FPToSIInst::FPToSIInst(
2456 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2457) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002458 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459}
2460
2461FPToSIInst::FPToSIInst(
2462 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2463) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002464 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465}
2466
2467PtrToIntInst::PtrToIntInst(
2468 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2469) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002470 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471}
2472
2473PtrToIntInst::PtrToIntInst(
2474 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2475) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002476 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002477}
2478
2479IntToPtrInst::IntToPtrInst(
2480 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2481) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002482 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002483}
2484
2485IntToPtrInst::IntToPtrInst(
2486 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2487) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002488 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002489}
2490
2491BitCastInst::BitCastInst(
2492 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2493) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002494 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002495}
2496
2497BitCastInst::BitCastInst(
2498 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2499) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002500 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002501}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002502
2503//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002504// CmpInst Classes
2505//===----------------------------------------------------------------------===//
2506
Nate Begemand2195702008-05-12 19:01:56 +00002507CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2508 Value *LHS, Value *RHS, const std::string &Name,
2509 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002510 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002511 OperandTraits<CmpInst>::op_begin(this),
2512 OperandTraits<CmpInst>::operands(this),
2513 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002514 Op<0>() = LHS;
2515 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002516 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002517 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002518}
Gabor Greiff6caff662008-05-10 08:32:32 +00002519
Nate Begemand2195702008-05-12 19:01:56 +00002520CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2521 Value *LHS, Value *RHS, const std::string &Name,
2522 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002523 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002524 OperandTraits<CmpInst>::op_begin(this),
2525 OperandTraits<CmpInst>::operands(this),
2526 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002527 Op<0>() = LHS;
2528 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002529 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002530 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002531}
2532
2533CmpInst *
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002534CmpInst::Create(LLVMContext &Context, OtherOps Op, unsigned short predicate,
2535 Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002536 const std::string &Name, Instruction *InsertBefore) {
2537 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002538 if (InsertBefore)
2539 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2540 S1, S2, Name);
2541 else
2542 return new ICmpInst(Context, CmpInst::Predicate(predicate),
2543 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002544 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002545
2546 if (InsertBefore)
2547 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2548 S1, S2, Name);
2549 else
2550 return new FCmpInst(Context, CmpInst::Predicate(predicate),
2551 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002552}
2553
2554CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002555CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002556 const std::string &Name, BasicBlock *InsertAtEnd) {
2557 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002558 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2559 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002560 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002561 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2562 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002563}
2564
2565void CmpInst::swapOperands() {
2566 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2567 IC->swapOperands();
2568 else
2569 cast<FCmpInst>(this)->swapOperands();
2570}
2571
2572bool CmpInst::isCommutative() {
2573 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2574 return IC->isCommutative();
2575 return cast<FCmpInst>(this)->isCommutative();
2576}
2577
2578bool CmpInst::isEquality() {
2579 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2580 return IC->isEquality();
2581 return cast<FCmpInst>(this)->isEquality();
2582}
2583
2584
Dan Gohman4e724382008-05-31 02:47:54 +00002585CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002586 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002587 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002588 case ICMP_EQ: return ICMP_NE;
2589 case ICMP_NE: return ICMP_EQ;
2590 case ICMP_UGT: return ICMP_ULE;
2591 case ICMP_ULT: return ICMP_UGE;
2592 case ICMP_UGE: return ICMP_ULT;
2593 case ICMP_ULE: return ICMP_UGT;
2594 case ICMP_SGT: return ICMP_SLE;
2595 case ICMP_SLT: return ICMP_SGE;
2596 case ICMP_SGE: return ICMP_SLT;
2597 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002598
Dan Gohman4e724382008-05-31 02:47:54 +00002599 case FCMP_OEQ: return FCMP_UNE;
2600 case FCMP_ONE: return FCMP_UEQ;
2601 case FCMP_OGT: return FCMP_ULE;
2602 case FCMP_OLT: return FCMP_UGE;
2603 case FCMP_OGE: return FCMP_ULT;
2604 case FCMP_OLE: return FCMP_UGT;
2605 case FCMP_UEQ: return FCMP_ONE;
2606 case FCMP_UNE: return FCMP_OEQ;
2607 case FCMP_UGT: return FCMP_OLE;
2608 case FCMP_ULT: return FCMP_OGE;
2609 case FCMP_UGE: return FCMP_OLT;
2610 case FCMP_ULE: return FCMP_OGT;
2611 case FCMP_ORD: return FCMP_UNO;
2612 case FCMP_UNO: return FCMP_ORD;
2613 case FCMP_TRUE: return FCMP_FALSE;
2614 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002615 }
2616}
2617
Reid Spencer266e42b2006-12-23 06:05:41 +00002618ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2619 switch (pred) {
2620 default: assert(! "Unknown icmp predicate!");
2621 case ICMP_EQ: case ICMP_NE:
2622 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2623 return pred;
2624 case ICMP_UGT: return ICMP_SGT;
2625 case ICMP_ULT: return ICMP_SLT;
2626 case ICMP_UGE: return ICMP_SGE;
2627 case ICMP_ULE: return ICMP_SLE;
2628 }
2629}
2630
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002631ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2632 switch (pred) {
2633 default: assert(! "Unknown icmp predicate!");
2634 case ICMP_EQ: case ICMP_NE:
2635 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2636 return pred;
2637 case ICMP_SGT: return ICMP_UGT;
2638 case ICMP_SLT: return ICMP_ULT;
2639 case ICMP_SGE: return ICMP_UGE;
2640 case ICMP_SLE: return ICMP_ULE;
2641 }
2642}
2643
Reid Spencer266e42b2006-12-23 06:05:41 +00002644bool ICmpInst::isSignedPredicate(Predicate pred) {
2645 switch (pred) {
2646 default: assert(! "Unknown icmp predicate!");
2647 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2648 return true;
2649 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2650 case ICMP_UGE: case ICMP_ULE:
2651 return false;
2652 }
2653}
2654
Reid Spencer0286bc12007-02-28 22:00:54 +00002655/// Initialize a set of values that all satisfy the condition with C.
2656///
2657ConstantRange
2658ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2659 APInt Lower(C);
2660 APInt Upper(C);
2661 uint32_t BitWidth = C.getBitWidth();
2662 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002663 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002664 case ICmpInst::ICMP_EQ: Upper++; break;
2665 case ICmpInst::ICMP_NE: Lower++; break;
2666 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2667 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2668 case ICmpInst::ICMP_UGT:
2669 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2670 break;
2671 case ICmpInst::ICMP_SGT:
2672 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2673 break;
2674 case ICmpInst::ICMP_ULE:
2675 Lower = APInt::getMinValue(BitWidth); Upper++;
2676 break;
2677 case ICmpInst::ICMP_SLE:
2678 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2679 break;
2680 case ICmpInst::ICMP_UGE:
2681 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2682 break;
2683 case ICmpInst::ICMP_SGE:
2684 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2685 break;
2686 }
2687 return ConstantRange(Lower, Upper);
2688}
2689
Dan Gohman4e724382008-05-31 02:47:54 +00002690CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002691 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002692 default: assert(!"Unknown cmp predicate!");
2693 case ICMP_EQ: case ICMP_NE:
2694 return pred;
2695 case ICMP_SGT: return ICMP_SLT;
2696 case ICMP_SLT: return ICMP_SGT;
2697 case ICMP_SGE: return ICMP_SLE;
2698 case ICMP_SLE: return ICMP_SGE;
2699 case ICMP_UGT: return ICMP_ULT;
2700 case ICMP_ULT: return ICMP_UGT;
2701 case ICMP_UGE: return ICMP_ULE;
2702 case ICMP_ULE: return ICMP_UGE;
2703
Reid Spencerd9436b62006-11-20 01:22:35 +00002704 case FCMP_FALSE: case FCMP_TRUE:
2705 case FCMP_OEQ: case FCMP_ONE:
2706 case FCMP_UEQ: case FCMP_UNE:
2707 case FCMP_ORD: case FCMP_UNO:
2708 return pred;
2709 case FCMP_OGT: return FCMP_OLT;
2710 case FCMP_OLT: return FCMP_OGT;
2711 case FCMP_OGE: return FCMP_OLE;
2712 case FCMP_OLE: return FCMP_OGE;
2713 case FCMP_UGT: return FCMP_ULT;
2714 case FCMP_ULT: return FCMP_UGT;
2715 case FCMP_UGE: return FCMP_ULE;
2716 case FCMP_ULE: return FCMP_UGE;
2717 }
2718}
2719
Reid Spencer266e42b2006-12-23 06:05:41 +00002720bool CmpInst::isUnsigned(unsigned short predicate) {
2721 switch (predicate) {
2722 default: return false;
2723 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2724 case ICmpInst::ICMP_UGE: return true;
2725 }
2726}
2727
2728bool CmpInst::isSigned(unsigned short predicate){
2729 switch (predicate) {
2730 default: return false;
2731 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2732 case ICmpInst::ICMP_SGE: return true;
2733 }
2734}
2735
2736bool CmpInst::isOrdered(unsigned short predicate) {
2737 switch (predicate) {
2738 default: return false;
2739 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2740 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2741 case FCmpInst::FCMP_ORD: return true;
2742 }
2743}
2744
2745bool CmpInst::isUnordered(unsigned short predicate) {
2746 switch (predicate) {
2747 default: return false;
2748 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2749 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2750 case FCmpInst::FCMP_UNO: return true;
2751 }
2752}
2753
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002754//===----------------------------------------------------------------------===//
2755// SwitchInst Implementation
2756//===----------------------------------------------------------------------===//
2757
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002758void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002759 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002760 ReservedSpace = 2+NumCases*2;
2761 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002762 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002763
Gabor Greif2d3024d2008-05-26 21:33:52 +00002764 OperandList[0] = Value;
2765 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002766}
2767
Chris Lattner2195fc42007-02-24 00:55:48 +00002768/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2769/// switch on and a default destination. The number of additional cases can
2770/// be specified here to make memory allocation more efficient. This
2771/// constructor can also autoinsert before another instruction.
2772SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2773 Instruction *InsertBefore)
2774 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2775 init(Value, Default, NumCases);
2776}
2777
2778/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2779/// switch on and a default destination. The number of additional cases can
2780/// be specified here to make memory allocation more efficient. This
2781/// constructor also autoinserts at the end of the specified BasicBlock.
2782SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2783 BasicBlock *InsertAtEnd)
2784 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2785 init(Value, Default, NumCases);
2786}
2787
Misha Brukmanb1c93172005-04-21 23:48:37 +00002788SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002789 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002790 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002791 Use *OL = OperandList, *InOL = SI.OperandList;
2792 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002793 OL[i] = InOL[i];
2794 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002795 }
2796}
2797
Gordon Henriksen14a55692007-12-10 02:14:30 +00002798SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002799 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002800}
2801
2802
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002803/// addCase - Add an entry to the switch instruction...
2804///
Chris Lattner47ac1872005-02-24 05:32:09 +00002805void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002806 unsigned OpNo = NumOperands;
2807 if (OpNo+2 > ReservedSpace)
2808 resizeOperands(0); // Get more space!
2809 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002810 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002811 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002812 OperandList[OpNo] = OnVal;
2813 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002814}
2815
2816/// removeCase - This method removes the specified successor from the switch
2817/// instruction. Note that this cannot be used to remove the default
2818/// destination (successor #0).
2819///
2820void SwitchInst::removeCase(unsigned idx) {
2821 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002822 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2823
2824 unsigned NumOps = getNumOperands();
2825 Use *OL = OperandList;
2826
2827 // Move everything after this operand down.
2828 //
2829 // FIXME: we could just swap with the end of the list, then erase. However,
2830 // client might not expect this to happen. The code as it is thrashes the
2831 // use/def lists, which is kinda lame.
2832 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2833 OL[i-2] = OL[i];
2834 OL[i-2+1] = OL[i+1];
2835 }
2836
2837 // Nuke the last value.
2838 OL[NumOps-2].set(0);
2839 OL[NumOps-2+1].set(0);
2840 NumOperands = NumOps-2;
2841}
2842
2843/// resizeOperands - resize operands - This adjusts the length of the operands
2844/// list according to the following behavior:
2845/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002846/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002847/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2848/// 3. If NumOps == NumOperands, trim the reserved space.
2849///
2850void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002851 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002852 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002853 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002854 } else if (NumOps*2 > NumOperands) {
2855 // No resize needed.
2856 if (ReservedSpace >= NumOps) return;
2857 } else if (NumOps == NumOperands) {
2858 if (ReservedSpace == NumOps) return;
2859 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002860 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002861 }
2862
2863 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002864 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002865 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002866 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002867 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002868 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002869 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002870 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002871}
2872
2873
2874BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2875 return getSuccessor(idx);
2876}
2877unsigned SwitchInst::getNumSuccessorsV() const {
2878 return getNumSuccessors();
2879}
2880void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2881 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002882}
Chris Lattnerf22be932004-10-15 23:52:53 +00002883
Chris Lattnerf22be932004-10-15 23:52:53 +00002884// Define these methods here so vtables don't get emitted into every translation
2885// unit that uses these classes.
2886
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002887GetElementPtrInst *GetElementPtrInst::clone(LLVMContext&) const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002888 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002889}
2890
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002891BinaryOperator *BinaryOperator::clone(LLVMContext&) const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002892 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002893}
2894
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002895FCmpInst* FCmpInst::clone(LLVMContext &Context) const {
2896 return new FCmpInst(Context, getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002897}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002898ICmpInst* ICmpInst::clone(LLVMContext &Context) const {
2899 return new ICmpInst(Context, getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002900}
2901
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002902ExtractValueInst *ExtractValueInst::clone(LLVMContext&) const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002903 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002904}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002905InsertValueInst *InsertValueInst::clone(LLVMContext&) const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002906 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002907}
2908
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002909MallocInst *MallocInst::clone(LLVMContext&) const {
2910 return new MallocInst(*this);
2911}
Dan Gohman0752bff2008-05-23 00:36:11 +00002912
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002913AllocaInst *AllocaInst::clone(LLVMContext&) const {
2914 return new AllocaInst(*this);
2915}
2916
2917FreeInst *FreeInst::clone(LLVMContext&) const {
2918 return new FreeInst(getOperand(0));
2919}
2920
2921LoadInst *LoadInst::clone(LLVMContext&) const {
2922 return new LoadInst(*this);
2923}
2924
2925StoreInst *StoreInst::clone(LLVMContext&) const {
2926 return new StoreInst(*this);
2927}
2928
2929CastInst *TruncInst::clone(LLVMContext&) const {
2930 return new TruncInst(*this);
2931}
2932
2933CastInst *ZExtInst::clone(LLVMContext&) const {
2934 return new ZExtInst(*this);
2935}
2936
2937CastInst *SExtInst::clone(LLVMContext&) const {
2938 return new SExtInst(*this);
2939}
2940
2941CastInst *FPTruncInst::clone(LLVMContext&) const {
2942 return new FPTruncInst(*this);
2943}
2944
2945CastInst *FPExtInst::clone(LLVMContext&) const {
2946 return new FPExtInst(*this);
2947}
2948
2949CastInst *UIToFPInst::clone(LLVMContext&) const {
2950 return new UIToFPInst(*this);
2951}
2952
2953CastInst *SIToFPInst::clone(LLVMContext&) const {
2954 return new SIToFPInst(*this);
2955}
2956
2957CastInst *FPToUIInst::clone(LLVMContext&) const {
2958 return new FPToUIInst(*this);
2959}
2960
2961CastInst *FPToSIInst::clone(LLVMContext&) const {
2962 return new FPToSIInst(*this);
2963}
2964
2965CastInst *PtrToIntInst::clone(LLVMContext&) const {
2966 return new PtrToIntInst(*this);
2967}
2968
2969CastInst *IntToPtrInst::clone(LLVMContext&) const {
2970 return new IntToPtrInst(*this);
2971}
2972
2973CastInst *BitCastInst::clone(LLVMContext&) const {
2974 return new BitCastInst(*this);
2975}
2976
2977CallInst *CallInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00002978 return new(getNumOperands()) CallInst(*this);
2979}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002980
2981SelectInst *SelectInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00002982 return new(getNumOperands()) SelectInst(*this);
2983}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002984
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002985VAArgInst *VAArgInst::clone(LLVMContext&) const {
2986 return new VAArgInst(*this);
2987}
2988
2989ExtractElementInst *ExtractElementInst::clone(LLVMContext&) const {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002990 return new ExtractElementInst(*this);
2991}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002992
2993InsertElementInst *InsertElementInst::clone(LLVMContext&) const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002994 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002995}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002996
2997ShuffleVectorInst *ShuffleVectorInst::clone(LLVMContext&) const {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002998 return new ShuffleVectorInst(*this);
2999}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003000
3001PHINode *PHINode::clone(LLVMContext&) const {
3002 return new PHINode(*this);
3003}
3004
3005ReturnInst *ReturnInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00003006 return new(getNumOperands()) ReturnInst(*this);
3007}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003008
3009BranchInst *BranchInst::clone(LLVMContext&) const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003010 unsigned Ops(getNumOperands());
3011 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003012}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003013
3014SwitchInst *SwitchInst::clone(LLVMContext&) const {
3015 return new SwitchInst(*this);
3016}
3017
3018InvokeInst *InvokeInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00003019 return new(getNumOperands()) InvokeInst(*this);
3020}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003021
3022UnwindInst *UnwindInst::clone(LLVMContext&) const {
3023 return new UnwindInst();
3024}
3025
3026UnreachableInst *UnreachableInst::clone(LLVMContext&) const {
3027 return new UnreachableInst();
3028}