blob: e04d54cee379589e5b119893560500e7ab16a8fc [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"
Dan Gohmand2a251f2009-07-17 21:33:58 +000019#include "llvm/Operator.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000020#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000023#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000024#include "llvm/Support/Streams.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000025using namespace llvm;
26
Chris Lattner3e13b8c2008-01-02 23:42:30 +000027//===----------------------------------------------------------------------===//
28// CallSite Class
29//===----------------------------------------------------------------------===//
30
Gabor Greif1b9921a2009-01-11 22:33:22 +000031#define CALLSITE_DELEGATE_GETTER(METHOD) \
32 Instruction *II(getInstruction()); \
33 return isCall() \
34 ? cast<CallInst>(II)->METHOD \
35 : cast<InvokeInst>(II)->METHOD
36
37#define CALLSITE_DELEGATE_SETTER(METHOD) \
38 Instruction *II(getInstruction()); \
39 if (isCall()) \
40 cast<CallInst>(II)->METHOD; \
41 else \
42 cast<InvokeInst>(II)->METHOD
43
Duncan Sands85fab3a2008-02-18 17:32:13 +000044CallSite::CallSite(Instruction *C) {
45 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
Gabor Greif1b9921a2009-01-11 22:33:22 +000046 I.setPointer(C);
47 I.setInt(isa<CallInst>(C));
Duncan Sands85fab3a2008-02-18 17:32:13 +000048}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000049unsigned CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000050 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000051}
52void CallSite::setCallingConv(unsigned CC) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000053 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
Chris Lattnerf7b6d312005-05-06 20:26:43 +000054}
Devang Patel4c758ea2008-09-25 21:00:45 +000055const AttrListPtr &CallSite::getAttributes() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000056 CALLSITE_DELEGATE_GETTER(getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000057}
Devang Patel4c758ea2008-09-25 21:00:45 +000058void CallSite::setAttributes(const AttrListPtr &PAL) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000059 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000060}
Devang Patelba3fa6c2008-09-23 23:03:40 +000061bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000062 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
Duncan Sands5208d1a2007-11-28 17:07:01 +000063}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000064uint16_t CallSite::getParamAlignment(uint16_t i) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000065 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
Dale Johanneseneabc5f32008-02-22 17:49:45 +000066}
Duncan Sands38ef3a82007-12-03 20:06:50 +000067bool CallSite::doesNotAccessMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000068 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000069}
Duncan Sands78c88722008-07-08 08:38:44 +000070void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000071 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000072}
Duncan Sands38ef3a82007-12-03 20:06:50 +000073bool CallSite::onlyReadsMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000074 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000075}
Duncan Sands78c88722008-07-08 08:38:44 +000076void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000077 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000078}
79bool CallSite::doesNotReturn() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000080 CALLSITE_DELEGATE_GETTER(doesNotReturn());
Duncan Sands78c88722008-07-08 08:38:44 +000081}
82void CallSite::setDoesNotReturn(bool doesNotReturn) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000083 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
Duncan Sands78c88722008-07-08 08:38:44 +000084}
Duncan Sands3353ed02007-12-18 09:59:50 +000085bool CallSite::doesNotThrow() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000086 CALLSITE_DELEGATE_GETTER(doesNotThrow());
Duncan Sands8e4847e2007-12-16 15:51:49 +000087}
Duncan Sandsaa31b922007-12-19 21:13:37 +000088void CallSite::setDoesNotThrow(bool doesNotThrow) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000089 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
Duncan Sandsaa31b922007-12-19 21:13:37 +000090}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000091
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000092bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000093 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
94 if (AI->get() == Arg)
95 return true;
96 return false;
97}
98
Gabor Greif1b9921a2009-01-11 22:33:22 +000099#undef CALLSITE_DELEGATE_GETTER
100#undef CALLSITE_DELEGATE_SETTER
101
Gordon Henriksen14a55692007-12-10 02:14:30 +0000102//===----------------------------------------------------------------------===//
103// TerminatorInst Class
104//===----------------------------------------------------------------------===//
105
106// Out of line virtual method, so the vtable, etc has a home.
107TerminatorInst::~TerminatorInst() {
108}
109
Gabor Greiff6caff662008-05-10 08:32:32 +0000110//===----------------------------------------------------------------------===//
111// UnaryInstruction Class
112//===----------------------------------------------------------------------===//
113
Gordon Henriksen14a55692007-12-10 02:14:30 +0000114// Out of line virtual method, so the vtable, etc has a home.
115UnaryInstruction::~UnaryInstruction() {
116}
117
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000118//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000119// SelectInst Class
120//===----------------------------------------------------------------------===//
121
122/// areInvalidOperands - Return a string if the specified operands are invalid
123/// for a select operation, otherwise return null.
124const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
125 if (Op1->getType() != Op2->getType())
126 return "both values to select must have same type";
127
128 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
129 // Vector select.
130 if (VT->getElementType() != Type::Int1Ty)
131 return "vector select condition element type must be i1";
132 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
133 if (ET == 0)
134 return "selected values for vector select must be vectors";
135 if (ET->getNumElements() != VT->getNumElements())
136 return "vector select requires selected vectors to have "
137 "the same vector length as select condition";
138 } else if (Op0->getType() != Type::Int1Ty) {
139 return "select condition must be i1 or <n x i1>";
140 }
141 return 0;
142}
143
144
145//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000146// PHINode Class
147//===----------------------------------------------------------------------===//
148
149PHINode::PHINode(const PHINode &PN)
150 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000151 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000152 ReservedSpace(PN.getNumOperands()) {
153 Use *OL = OperandList;
154 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000155 OL[i] = PN.getOperand(i);
156 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000157 }
158}
159
Gordon Henriksen14a55692007-12-10 02:14:30 +0000160PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000161 if (OperandList)
162 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000163}
164
165// removeIncomingValue - Remove an incoming value. This is useful if a
166// predecessor basic block is deleted.
167Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
168 unsigned NumOps = getNumOperands();
169 Use *OL = OperandList;
170 assert(Idx*2 < NumOps && "BB not in PHI node!");
171 Value *Removed = OL[Idx*2];
172
173 // Move everything after this operand down.
174 //
175 // FIXME: we could just swap with the end of the list, then erase. However,
176 // client might not expect this to happen. The code as it is thrashes the
177 // use/def lists, which is kinda lame.
178 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
179 OL[i-2] = OL[i];
180 OL[i-2+1] = OL[i+1];
181 }
182
183 // Nuke the last value.
184 OL[NumOps-2].set(0);
185 OL[NumOps-2+1].set(0);
186 NumOperands = NumOps-2;
187
188 // If the PHI node is dead, because it has zero entries, nuke it now.
189 if (NumOps == 2 && DeletePHIIfEmpty) {
190 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersond420fd42009-07-16 00:03:07 +0000191 replaceAllUsesWith(getType()->getContext().getUndef(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000192 eraseFromParent();
193 }
194 return Removed;
195}
196
197/// resizeOperands - resize operands - This adjusts the length of the operands
198/// list according to the following behavior:
199/// 1. If NumOps == 0, grow the operand list in response to a push_back style
200/// of operation. This grows the number of ops by 1.5 times.
201/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
202/// 3. If NumOps == NumOperands, trim the reserved space.
203///
204void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000205 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000206 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000207 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000208 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
209 } else if (NumOps*2 > NumOperands) {
210 // No resize needed.
211 if (ReservedSpace >= NumOps) return;
212 } else if (NumOps == NumOperands) {
213 if (ReservedSpace == NumOps) return;
214 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000215 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000216 }
217
218 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000219 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000220 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000221 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000222 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000223 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000224}
225
Nate Begemanb3923212005-08-04 23:24:19 +0000226/// hasConstantValue - If the specified PHI node always merges together the same
227/// value, return the value, otherwise return null.
228///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000229Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000230 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000231 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000232 if (getIncomingValue(0) != this) // not X = phi X
233 return getIncomingValue(0);
234 else
Owen Andersond420fd42009-07-16 00:03:07 +0000235 return
236 getType()->getContext().getUndef(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000237 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000238
Nate Begemanb3923212005-08-04 23:24:19 +0000239 // Otherwise if all of the incoming values are the same for the PHI, replace
240 // the PHI node with the incoming value.
241 //
242 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000243 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000244 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000245 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000246 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000247 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000248 if (InVal && getIncomingValue(i) != InVal)
249 return 0; // Not the same, bail out.
250 else
251 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000252 }
Nate Begemanb3923212005-08-04 23:24:19 +0000253
254 // The only case that could cause InVal to be null is if we have a PHI node
255 // that only has entries for itself. In this case, there is no entry into the
256 // loop, so kill the PHI.
257 //
Owen Andersond420fd42009-07-16 00:03:07 +0000258 if (InVal == 0) InVal = getType()->getContext().getUndef(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000259
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000260 // If we have a PHI node like phi(X, undef, X), where X is defined by some
261 // instruction, we cannot always return X as the result of the PHI node. Only
262 // do this if X is not an instruction (thus it must dominate the PHI block),
263 // or if the client is prepared to deal with this possibility.
264 if (HasUndefInput && !AllowNonDominatingInstruction)
265 if (Instruction *IV = dyn_cast<Instruction>(InVal))
266 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000267 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000268 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000269 return 0; // Cannot guarantee that InVal dominates this PHINode.
270
Nate Begemanb3923212005-08-04 23:24:19 +0000271 // All of the incoming values are the same, return the value now.
272 return InVal;
273}
274
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000275
276//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000277// CallInst Implementation
278//===----------------------------------------------------------------------===//
279
Gordon Henriksen14a55692007-12-10 02:14:30 +0000280CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000281}
282
Chris Lattner054ba2c2007-02-13 00:58:44 +0000283void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000284 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
285 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000286 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287
Misha Brukmanb1c93172005-04-21 23:48:37 +0000288 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000290 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000291
Chris Lattner054ba2c2007-02-13 00:58:44 +0000292 assert((NumParams == FTy->getNumParams() ||
293 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000294 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000295 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000296 assert((i >= FTy->getNumParams() ||
297 FTy->getParamType(i) == Params[i]->getType()) &&
298 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000299 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000300 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000301}
302
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000303void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000304 assert(NumOperands == 3 && "NumOperands not set up?");
305 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000306 OL[0] = Func;
307 OL[1] = Actual1;
308 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000309
310 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000312 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000314 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000315 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000316 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000317 assert((0 >= FTy->getNumParams() ||
318 FTy->getParamType(0) == Actual1->getType()) &&
319 "Calling a function with a bad signature!");
320 assert((1 >= FTy->getNumParams() ||
321 FTy->getParamType(1) == Actual2->getType()) &&
322 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323}
324
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000325void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000326 assert(NumOperands == 2 && "NumOperands not set up?");
327 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000328 OL[0] = Func;
329 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000330
331 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000333 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000334
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000335 assert((FTy->getNumParams() == 1 ||
336 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000337 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000338 assert((0 == FTy->getNumParams() ||
339 FTy->getParamType(0) == Actual->getType()) &&
340 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341}
342
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000343void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000344 assert(NumOperands == 1 && "NumOperands not set up?");
345 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000346 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000347
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000348 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000349 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000350 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000351
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000352 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000353}
354
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000355CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000356 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000357 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
358 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000359 Instruction::Call,
360 OperandTraits<CallInst>::op_end(this) - 2,
361 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000363 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000364}
365
366CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
367 BasicBlock *InsertAtEnd)
368 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
369 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000370 Instruction::Call,
371 OperandTraits<CallInst>::op_end(this) - 2,
372 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000373 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000374 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000375}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000376CallInst::CallInst(Value *Func, const std::string &Name,
377 Instruction *InsertBefore)
378 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
379 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000380 Instruction::Call,
381 OperandTraits<CallInst>::op_end(this) - 1,
382 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000383 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000384 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000385}
386
387CallInst::CallInst(Value *Func, const std::string &Name,
388 BasicBlock *InsertAtEnd)
389 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
390 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000391 Instruction::Call,
392 OperandTraits<CallInst>::op_end(this) - 1,
393 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000394 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000395 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000396}
397
Misha Brukmanb1c93172005-04-21 23:48:37 +0000398CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000399 : Instruction(CI.getType(), Instruction::Call,
400 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000401 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000402 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000403 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000404 Use *OL = OperandList;
405 Use *InOL = CI.OperandList;
406 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000407 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000408}
409
Devang Patel4c758ea2008-09-25 21:00:45 +0000410void CallInst::addAttribute(unsigned i, Attributes attr) {
411 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000412 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000413 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000414}
415
Devang Patel4c758ea2008-09-25 21:00:45 +0000416void CallInst::removeAttribute(unsigned i, Attributes attr) {
417 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000418 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000419 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000420}
421
Devang Patelba3fa6c2008-09-23 23:03:40 +0000422bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000423 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000424 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000425 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000426 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000427 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000428}
429
Duncan Sands5208d1a2007-11-28 17:07:01 +0000430
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000431//===----------------------------------------------------------------------===//
432// InvokeInst Implementation
433//===----------------------------------------------------------------------===//
434
435void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000436 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000437 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Bill Wendling4bb96e92009-03-13 21:15:59 +0000438 Use *OL = OperandList;
439 OL[0] = Fn;
440 OL[1] = IfNormal;
441 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000442 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000443 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000444 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000445
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000446 assert(((NumArgs == FTy->getNumParams()) ||
447 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000448 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000449
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000450 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000451 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000452 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000453 "Invoking a function with a bad signature!");
454
Bill Wendling4bb96e92009-03-13 21:15:59 +0000455 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000456 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000457}
458
Misha Brukmanb1c93172005-04-21 23:48:37 +0000459InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000460 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000461 OperandTraits<InvokeInst>::op_end(this)
462 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000463 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000464 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000465 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000466 Use *OL = OperandList, *InOL = II.OperandList;
467 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000468 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000469}
470
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000471BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
472 return getSuccessor(idx);
473}
474unsigned InvokeInst::getNumSuccessorsV() const {
475 return getNumSuccessors();
476}
477void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
478 return setSuccessor(idx, B);
479}
480
Devang Patelba3fa6c2008-09-23 23:03:40 +0000481bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000482 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000483 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000484 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000485 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000486 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000487}
488
Devang Patel4c758ea2008-09-25 21:00:45 +0000489void InvokeInst::addAttribute(unsigned i, Attributes attr) {
490 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000491 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000492 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000493}
494
Devang Patel4c758ea2008-09-25 21:00:45 +0000495void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
496 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000497 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000498 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000499}
500
Duncan Sands5208d1a2007-11-28 17:07:01 +0000501
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000502//===----------------------------------------------------------------------===//
503// ReturnInst Implementation
504//===----------------------------------------------------------------------===//
505
Chris Lattner2195fc42007-02-24 00:55:48 +0000506ReturnInst::ReturnInst(const ReturnInst &RI)
507 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000508 OperandTraits<ReturnInst>::op_end(this) -
509 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000510 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000511 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000512 Op<0>() = RI.Op<0>();
Chris Lattner2195fc42007-02-24 00:55:48 +0000513}
514
515ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000516 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000517 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
518 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000519 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000520 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000521}
522ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000523 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000524 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
525 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000526 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000527 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000528}
529ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000530 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000531 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000532}
533
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000534unsigned ReturnInst::getNumSuccessorsV() const {
535 return getNumSuccessors();
536}
537
Devang Patelae682fb2008-02-26 17:56:20 +0000538/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
539/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000540void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000541 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000542}
543
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000544BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000545 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000546 return 0;
547}
548
Devang Patel59643e52008-02-23 00:35:18 +0000549ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000550}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000551
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000552//===----------------------------------------------------------------------===//
553// UnwindInst Implementation
554//===----------------------------------------------------------------------===//
555
Chris Lattner2195fc42007-02-24 00:55:48 +0000556UnwindInst::UnwindInst(Instruction *InsertBefore)
557 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
558}
559UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
560 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
561}
562
563
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000564unsigned UnwindInst::getNumSuccessorsV() const {
565 return getNumSuccessors();
566}
567
568void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000569 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000570}
571
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000572BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000573 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000574 return 0;
575}
576
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000577//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000578// UnreachableInst Implementation
579//===----------------------------------------------------------------------===//
580
Chris Lattner2195fc42007-02-24 00:55:48 +0000581UnreachableInst::UnreachableInst(Instruction *InsertBefore)
582 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
583}
584UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
585 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
586}
587
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000588unsigned UnreachableInst::getNumSuccessorsV() const {
589 return getNumSuccessors();
590}
591
592void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000593 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000594}
595
596BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000597 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000598 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000599}
600
601//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000602// BranchInst Implementation
603//===----------------------------------------------------------------------===//
604
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000605void BranchInst::AssertOK() {
606 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000607 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000608 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000609}
610
Chris Lattner2195fc42007-02-24 00:55:48 +0000611BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000612 : TerminatorInst(Type::VoidTy, Instruction::Br,
613 OperandTraits<BranchInst>::op_end(this) - 1,
614 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000615 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000616 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000617}
618BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
619 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000620 : TerminatorInst(Type::VoidTy, Instruction::Br,
621 OperandTraits<BranchInst>::op_end(this) - 3,
622 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000623 Op<-1>() = IfTrue;
624 Op<-2>() = IfFalse;
625 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000626#ifndef NDEBUG
627 AssertOK();
628#endif
629}
630
631BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000632 : TerminatorInst(Type::VoidTy, Instruction::Br,
633 OperandTraits<BranchInst>::op_end(this) - 1,
634 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000635 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000636 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000637}
638
639BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
640 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000641 : TerminatorInst(Type::VoidTy, Instruction::Br,
642 OperandTraits<BranchInst>::op_end(this) - 3,
643 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000644 Op<-1>() = IfTrue;
645 Op<-2>() = IfFalse;
646 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000647#ifndef NDEBUG
648 AssertOK();
649#endif
650}
651
652
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000653BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000654 TerminatorInst(Type::VoidTy, Instruction::Br,
655 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
656 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000657 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000658 if (BI.getNumOperands() != 1) {
659 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000660 Op<-3>() = BI.Op<-3>();
661 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000662 }
663}
664
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000665
666Use* Use::getPrefix() {
667 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
668 if (PotentialPrefix.getOpaqueValue())
669 return 0;
670
671 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
672}
673
674BranchInst::~BranchInst() {
675 if (NumOperands == 1) {
676 if (Use *Prefix = OperandList->getPrefix()) {
677 Op<-1>() = 0;
678 //
679 // mark OperandList to have a special value for scrutiny
680 // by baseclass destructors and operator delete
681 OperandList = Prefix;
682 } else {
683 NumOperands = 3;
684 OperandList = op_begin();
685 }
686 }
687}
688
689
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000690BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
691 return getSuccessor(idx);
692}
693unsigned BranchInst::getNumSuccessorsV() const {
694 return getNumSuccessors();
695}
696void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
697 setSuccessor(idx, B);
698}
699
700
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000701//===----------------------------------------------------------------------===//
702// AllocationInst Implementation
703//===----------------------------------------------------------------------===//
704
Owen Andersonb6b25302009-07-14 23:09:55 +0000705static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000706 if (!Amt)
Owen Andersonb6b25302009-07-14 23:09:55 +0000707 Amt = Context.getConstantInt(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000708 else {
709 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000710 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000711 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000712 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000713 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000714 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000715}
716
Owen Anderson4fdeba92009-07-15 23:53:25 +0000717AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000718 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000719 Instruction *InsertBefore)
Owen Anderson4fdeba92009-07-15 23:53:25 +0000720 : UnaryInstruction(Ty->getContext().getPointerTypeUnqual(Ty), iTy,
721 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000722 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000723 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000724 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725}
726
Owen Anderson4fdeba92009-07-15 23:53:25 +0000727AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000728 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000729 BasicBlock *InsertAtEnd)
Owen Anderson4fdeba92009-07-15 23:53:25 +0000730 : UnaryInstruction(Ty->getContext().getPointerTypeUnqual(Ty), iTy,
731 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000732 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000733 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000734 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000735}
736
Gordon Henriksen14a55692007-12-10 02:14:30 +0000737// Out of line virtual method, so the vtable, etc has a home.
738AllocationInst::~AllocationInst() {
739}
740
Dan Gohmanaa583d72008-03-24 16:55:58 +0000741void AllocationInst::setAlignment(unsigned Align) {
742 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
743 SubclassData = Log2_32(Align) + 1;
744 assert(getAlignment() == Align && "Alignment representation error!");
745}
746
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000747bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000748 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
749 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000750 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000751}
752
753const Type *AllocationInst::getAllocatedType() const {
754 return getType()->getElementType();
755}
756
757AllocaInst::AllocaInst(const AllocaInst &AI)
Owen Anderson4fdeba92009-07-15 23:53:25 +0000758 : AllocationInst(AI.getType()->getElementType(),
Owen Andersonb6b25302009-07-14 23:09:55 +0000759 (Value*)AI.getOperand(0), Instruction::Alloca,
760 AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000761}
762
Chris Lattner8b291e62008-11-26 02:54:17 +0000763/// isStaticAlloca - Return true if this alloca is in the entry block of the
764/// function and is a constant size. If so, the code generator will fold it
765/// into the prolog/epilog code, so it is basically free.
766bool AllocaInst::isStaticAlloca() const {
767 // Must be constant size.
768 if (!isa<ConstantInt>(getArraySize())) return false;
769
770 // Must be in the entry block.
771 const BasicBlock *Parent = getParent();
772 return Parent == &Parent->getParent()->front();
773}
774
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000775MallocInst::MallocInst(const MallocInst &MI)
Owen Anderson4fdeba92009-07-15 23:53:25 +0000776 : AllocationInst(MI.getType()->getElementType(),
Owen Andersonb6b25302009-07-14 23:09:55 +0000777 (Value*)MI.getOperand(0), Instruction::Malloc,
778 MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000779}
780
781//===----------------------------------------------------------------------===//
782// FreeInst Implementation
783//===----------------------------------------------------------------------===//
784
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000785void FreeInst::AssertOK() {
786 assert(isa<PointerType>(getOperand(0)->getType()) &&
787 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000788}
789
790FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000791 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000792 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000793}
794
795FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000796 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000797 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000798}
799
800
801//===----------------------------------------------------------------------===//
802// LoadInst Implementation
803//===----------------------------------------------------------------------===//
804
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000805void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000806 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000807 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000808}
809
810LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000811 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000812 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000813 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000814 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000815 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000816 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000817}
818
819LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000820 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000821 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000822 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000823 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000824 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000825 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000826}
827
828LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
829 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000830 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000831 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000832 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000833 setAlignment(0);
834 AssertOK();
835 setName(Name);
836}
837
838LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
839 unsigned Align, Instruction *InsertBef)
840 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
841 Load, Ptr, InsertBef) {
842 setVolatile(isVolatile);
843 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000844 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000845 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000846}
847
Dan Gohman68659282007-07-18 20:51:11 +0000848LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
849 unsigned Align, BasicBlock *InsertAE)
850 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
851 Load, Ptr, InsertAE) {
852 setVolatile(isVolatile);
853 setAlignment(Align);
854 AssertOK();
855 setName(Name);
856}
857
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000858LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
859 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000860 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000861 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000862 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000863 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000864 AssertOK();
865 setName(Name);
866}
867
868
869
870LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000871 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
872 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000873 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000874 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000875 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000876 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000877}
878
879LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000880 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
881 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000882 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000883 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000884 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000885 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000886}
887
888LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
889 Instruction *InsertBef)
890: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000891 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000892 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000893 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000894 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000895 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000896}
897
898LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
899 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000900 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
901 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000902 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000903 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000904 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000905 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000906}
907
Christopher Lamb84485702007-04-22 19:24:39 +0000908void LoadInst::setAlignment(unsigned Align) {
909 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
910 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
911}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000912
913//===----------------------------------------------------------------------===//
914// StoreInst Implementation
915//===----------------------------------------------------------------------===//
916
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000917void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000918 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000919 assert(isa<PointerType>(getOperand(1)->getType()) &&
920 "Ptr must have pointer type!");
921 assert(getOperand(0)->getType() ==
922 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000923 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000924}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000925
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000926
927StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000928 : Instruction(Type::VoidTy, Store,
929 OperandTraits<StoreInst>::op_begin(this),
930 OperandTraits<StoreInst>::operands(this),
931 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000932 Op<0>() = val;
933 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000934 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000935 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000936 AssertOK();
937}
938
939StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000940 : Instruction(Type::VoidTy, Store,
941 OperandTraits<StoreInst>::op_begin(this),
942 OperandTraits<StoreInst>::operands(this),
943 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000944 Op<0>() = val;
945 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000946 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000947 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000948 AssertOK();
949}
950
Misha Brukmanb1c93172005-04-21 23:48:37 +0000951StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000952 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000953 : Instruction(Type::VoidTy, Store,
954 OperandTraits<StoreInst>::op_begin(this),
955 OperandTraits<StoreInst>::operands(this),
956 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000957 Op<0>() = val;
958 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000959 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000960 setAlignment(0);
961 AssertOK();
962}
963
964StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
965 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000966 : Instruction(Type::VoidTy, Store,
967 OperandTraits<StoreInst>::op_begin(this),
968 OperandTraits<StoreInst>::operands(this),
969 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000970 Op<0>() = val;
971 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000972 setVolatile(isVolatile);
973 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000974 AssertOK();
975}
976
Misha Brukmanb1c93172005-04-21 23:48:37 +0000977StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000978 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000979 : Instruction(Type::VoidTy, Store,
980 OperandTraits<StoreInst>::op_begin(this),
981 OperandTraits<StoreInst>::operands(this),
982 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000983 Op<0>() = val;
984 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000985 setVolatile(isVolatile);
986 setAlignment(Align);
987 AssertOK();
988}
989
990StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000991 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000992 : Instruction(Type::VoidTy, Store,
993 OperandTraits<StoreInst>::op_begin(this),
994 OperandTraits<StoreInst>::operands(this),
995 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000996 Op<0>() = val;
997 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000998 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000999 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001000 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001001}
1002
Christopher Lamb84485702007-04-22 19:24:39 +00001003void StoreInst::setAlignment(unsigned Align) {
1004 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1005 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1006}
1007
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001008//===----------------------------------------------------------------------===//
1009// GetElementPtrInst Implementation
1010//===----------------------------------------------------------------------===//
1011
Christopher Lambedf07882007-12-17 01:12:55 +00001012static unsigned retrieveAddrSpace(const Value *Val) {
1013 return cast<PointerType>(Val->getType())->getAddressSpace();
1014}
1015
Gabor Greif21ba1842008-06-06 20:28:12 +00001016void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Nate Begeman8a4afd92008-07-25 17:24:13 +00001017 const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001018 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1019 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001020 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001021
Chris Lattner79807c3d2007-01-31 19:47:18 +00001022 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001023 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001024
1025 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001026}
1027
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001028void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001029 assert(NumOperands == 2 && "NumOperands not initialized?");
1030 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001031 OL[0] = Ptr;
1032 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001033
1034 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001035}
1036
Gabor Greiff6caff662008-05-10 08:32:32 +00001037GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001038 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001039 OperandTraits<GetElementPtrInst>::op_end(this)
1040 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001041 GEPI.getNumOperands()) {
1042 Use *OL = OperandList;
1043 Use *GEPIOL = GEPI.OperandList;
1044 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001045 OL[i] = GEPIOL[i];
Gabor Greiff6caff662008-05-10 08:32:32 +00001046}
1047
Chris Lattner82981202005-05-03 05:43:30 +00001048GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1049 const std::string &Name, Instruction *InBe)
Owen Andersond420fd42009-07-16 00:03:07 +00001050 : Instruction(Ptr->getType()->getContext().getPointerType(
1051 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001052 GetElementPtr,
1053 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1054 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001055 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001056}
1057
1058GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1059 const std::string &Name, BasicBlock *IAE)
Owen Andersond420fd42009-07-16 00:03:07 +00001060 : Instruction(Ptr->getType()->getContext().getPointerType(
1061 checkType(getIndexedType(Ptr->getType(),Idx)),
1062 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001063 GetElementPtr,
1064 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1065 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001066 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001067}
1068
Chris Lattner6090a422009-03-09 04:46:40 +00001069/// getIndexedType - Returns the type of the element that would be accessed with
1070/// a gep instruction with the specified parameters.
1071///
1072/// The Idxs pointer should point to a continuous piece of memory containing the
1073/// indices, either as Value* or uint64_t.
1074///
1075/// A null type is returned if the indices are invalid for the specified
1076/// pointer type.
1077///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001078template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001079static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1080 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001081 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1082 if (!PTy) return 0; // Type isn't a pointer type!
1083 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001084
Chris Lattner6090a422009-03-09 04:46:40 +00001085 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001086 if (NumIdx == 0)
1087 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001088
1089 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001090 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1091 // that contain opaque types) under the assumption that it will be resolved to
1092 // a sane type later.
1093 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001094 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001095
Dan Gohman1ecaf452008-05-31 00:58:22 +00001096 unsigned CurIdx = 1;
1097 for (; CurIdx != NumIdx; ++CurIdx) {
1098 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1099 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001100 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001101 if (!CT->indexValid(Index)) return 0;
1102 Agg = CT->getTypeAtIndex(Index);
1103
1104 // If the new type forwards to another type, then it is in the middle
1105 // of being refined to another type (and hence, may have dropped all
1106 // references to what it was using before). So, use the new forwarded
1107 // type.
1108 if (const Type *Ty = Agg->getForwardedType())
1109 Agg = Ty;
1110 }
1111 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001112}
1113
Matthijs Kooijman04468622008-07-29 08:46:11 +00001114const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1115 Value* const *Idxs,
1116 unsigned NumIdx) {
1117 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1118}
1119
1120const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1121 uint64_t const *Idxs,
1122 unsigned NumIdx) {
1123 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1124}
1125
Chris Lattner82981202005-05-03 05:43:30 +00001126const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1127 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1128 if (!PTy) return 0; // Type isn't a pointer type!
1129
1130 // Check the pointer index.
1131 if (!PTy->indexValid(Idx)) return 0;
1132
Chris Lattnerc2233332005-05-03 16:44:45 +00001133 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001134}
1135
Chris Lattner45f15572007-04-14 00:12:57 +00001136
1137/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1138/// zeros. If so, the result pointer and the first operand have the same
1139/// value, just potentially different types.
1140bool GetElementPtrInst::hasAllZeroIndices() const {
1141 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1142 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1143 if (!CI->isZero()) return false;
1144 } else {
1145 return false;
1146 }
1147 }
1148 return true;
1149}
1150
Chris Lattner27058292007-04-27 20:35:56 +00001151/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1152/// constant integers. If so, the result pointer and the first operand have
1153/// a constant offset between them.
1154bool GetElementPtrInst::hasAllConstantIndices() const {
1155 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1156 if (!isa<ConstantInt>(getOperand(i)))
1157 return false;
1158 }
1159 return true;
1160}
1161
Chris Lattner45f15572007-04-14 00:12:57 +00001162
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001163//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001164// ExtractElementInst Implementation
1165//===----------------------------------------------------------------------===//
1166
1167ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001168 const std::string &Name,
1169 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001170 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001171 ExtractElement,
1172 OperandTraits<ExtractElementInst>::op_begin(this),
1173 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001174 assert(isValidOperands(Val, Index) &&
1175 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001176 Op<0>() = Val;
1177 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001178 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001179}
1180
1181ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001182 const std::string &Name,
1183 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001184 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001185 ExtractElement,
1186 OperandTraits<ExtractElementInst>::op_begin(this),
1187 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001188 assert(isValidOperands(Val, Index) &&
1189 "Invalid extractelement instruction operands!");
1190
Gabor Greif2d3024d2008-05-26 21:33:52 +00001191 Op<0>() = Val;
1192 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001193 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001194}
1195
Chris Lattner65511ff2006-10-05 06:24:58 +00001196
Chris Lattner54865b32006-04-08 04:05:48 +00001197bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001198 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001199 return false;
1200 return true;
1201}
1202
1203
Robert Bocchino23004482006-01-10 19:05:34 +00001204//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001205// InsertElementInst Implementation
1206//===----------------------------------------------------------------------===//
1207
Chris Lattner0875d942006-04-14 22:20:32 +00001208InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001209 : Instruction(IE.getType(), InsertElement,
1210 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001211 Op<0>() = IE.Op<0>();
1212 Op<1>() = IE.Op<1>();
1213 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001214}
Chris Lattner54865b32006-04-08 04:05:48 +00001215InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001216 const std::string &Name,
1217 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001218 : Instruction(Vec->getType(), InsertElement,
1219 OperandTraits<InsertElementInst>::op_begin(this),
1220 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001221 assert(isValidOperands(Vec, Elt, Index) &&
1222 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001223 Op<0>() = Vec;
1224 Op<1>() = Elt;
1225 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001226 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001227}
1228
Chris Lattner54865b32006-04-08 04:05:48 +00001229InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001230 const std::string &Name,
1231 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001232 : Instruction(Vec->getType(), InsertElement,
1233 OperandTraits<InsertElementInst>::op_begin(this),
1234 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001235 assert(isValidOperands(Vec, Elt, Index) &&
1236 "Invalid insertelement instruction operands!");
1237
Gabor Greif2d3024d2008-05-26 21:33:52 +00001238 Op<0>() = Vec;
1239 Op<1>() = Elt;
1240 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001241 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001242}
1243
Chris Lattner54865b32006-04-08 04:05:48 +00001244bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1245 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001246 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001247 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001248
Reid Spencerd84d35b2007-02-15 02:26:10 +00001249 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001250 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001251
Reid Spencer8d9336d2006-12-31 05:26:44 +00001252 if (Index->getType() != Type::Int32Ty)
Dan Gohman4fe64de2009-06-14 23:30:43 +00001253 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001254 return true;
1255}
1256
1257
Robert Bocchinoca27f032006-01-17 20:07:22 +00001258//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001259// ShuffleVectorInst Implementation
1260//===----------------------------------------------------------------------===//
1261
Chris Lattner0875d942006-04-14 22:20:32 +00001262ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001263 : Instruction(SV.getType(), ShuffleVector,
1264 OperandTraits<ShuffleVectorInst>::op_begin(this),
1265 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001266 Op<0>() = SV.Op<0>();
1267 Op<1>() = SV.Op<1>();
1268 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001269}
1270
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001271ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1272 const std::string &Name,
1273 Instruction *InsertBefore)
Owen Andersond420fd42009-07-16 00:03:07 +00001274: Instruction(V1->getType()->getContext().getVectorType(
1275 cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001276 cast<VectorType>(Mask->getType())->getNumElements()),
1277 ShuffleVector,
1278 OperandTraits<ShuffleVectorInst>::op_begin(this),
1279 OperandTraits<ShuffleVectorInst>::operands(this),
1280 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001281 assert(isValidOperands(V1, V2, Mask) &&
1282 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001283 Op<0>() = V1;
1284 Op<1>() = V2;
1285 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001286 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001287}
1288
1289ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Mon P Wang25f01062008-11-10 04:46:22 +00001290 const std::string &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001291 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001292 : Instruction(V1->getType(), ShuffleVector,
1293 OperandTraits<ShuffleVectorInst>::op_begin(this),
1294 OperandTraits<ShuffleVectorInst>::operands(this),
1295 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001296 assert(isValidOperands(V1, V2, Mask) &&
1297 "Invalid shuffle vector instruction operands!");
1298
Gabor Greif2d3024d2008-05-26 21:33:52 +00001299 Op<0>() = V1;
1300 Op<1>() = V2;
1301 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001302 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001303}
1304
Mon P Wang25f01062008-11-10 04:46:22 +00001305bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001306 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001307 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001308 return false;
1309
1310 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1311 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Mon P Wang25f01062008-11-10 04:46:22 +00001312 MaskTy->getElementType() != Type::Int32Ty)
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001313 return false;
1314 return true;
1315}
1316
Chris Lattnerf724e342008-03-02 05:28:33 +00001317/// getMaskValue - Return the index from the shuffle mask for the specified
1318/// output result. This is either -1 if the element is undef or a number less
1319/// than 2*numelements.
1320int ShuffleVectorInst::getMaskValue(unsigned i) const {
1321 const Constant *Mask = cast<Constant>(getOperand(2));
1322 if (isa<UndefValue>(Mask)) return -1;
1323 if (isa<ConstantAggregateZero>(Mask)) return 0;
1324 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1325 assert(i < MaskCV->getNumOperands() && "Index out of range");
1326
1327 if (isa<UndefValue>(MaskCV->getOperand(i)))
1328 return -1;
1329 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1330}
1331
Dan Gohman12fce772008-05-15 19:50:34 +00001332//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001333// InsertValueInst Class
1334//===----------------------------------------------------------------------===//
1335
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001336void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1337 unsigned NumIdx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001338 assert(NumOperands == 2 && "NumOperands not initialized?");
1339 Op<0>() = Agg;
1340 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001341
Dan Gohman1ecaf452008-05-31 00:58:22 +00001342 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001343 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001344}
1345
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001346void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1347 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001348 assert(NumOperands == 2 && "NumOperands not initialized?");
1349 Op<0>() = Agg;
1350 Op<1>() = Val;
1351
1352 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001353 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001354}
1355
1356InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001357 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001358 OperandTraits<InsertValueInst>::op_begin(this), 2),
1359 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001360 Op<0>() = IVI.getOperand(0);
1361 Op<1>() = IVI.getOperand(1);
Dan Gohman0752bff2008-05-23 00:36:11 +00001362}
1363
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001364InsertValueInst::InsertValueInst(Value *Agg,
1365 Value *Val,
1366 unsigned Idx,
1367 const std::string &Name,
1368 Instruction *InsertBefore)
1369 : Instruction(Agg->getType(), InsertValue,
1370 OperandTraits<InsertValueInst>::op_begin(this),
1371 2, InsertBefore) {
1372 init(Agg, Val, Idx, Name);
1373}
1374
1375InsertValueInst::InsertValueInst(Value *Agg,
1376 Value *Val,
1377 unsigned Idx,
1378 const std::string &Name,
1379 BasicBlock *InsertAtEnd)
1380 : Instruction(Agg->getType(), InsertValue,
1381 OperandTraits<InsertValueInst>::op_begin(this),
1382 2, InsertAtEnd) {
1383 init(Agg, Val, Idx, Name);
1384}
1385
Dan Gohman0752bff2008-05-23 00:36:11 +00001386//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001387// ExtractValueInst Class
1388//===----------------------------------------------------------------------===//
1389
Gabor Greifcbcc4952008-06-06 21:06:32 +00001390void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Gabor Greif1b9921a2009-01-11 22:33:22 +00001391 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001392 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001393
Dan Gohman1ecaf452008-05-31 00:58:22 +00001394 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001395 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001396}
1397
Gabor Greifcbcc4952008-06-06 21:06:32 +00001398void ExtractValueInst::init(unsigned Idx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001399 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001400
1401 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001402 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001403}
1404
1405ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001406 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001407 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001408}
1409
Dan Gohman12fce772008-05-15 19:50:34 +00001410// getIndexedType - Returns the type of the element that would be extracted
1411// with an extractvalue instruction with the specified parameters.
1412//
1413// A null type is returned if the indices are invalid for the specified
1414// pointer type.
1415//
1416const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001417 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001418 unsigned NumIdx) {
1419 unsigned CurIdx = 0;
1420 for (; CurIdx != NumIdx; ++CurIdx) {
1421 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001422 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1423 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001424 if (!CT->indexValid(Index)) return 0;
1425 Agg = CT->getTypeAtIndex(Index);
1426
1427 // If the new type forwards to another type, then it is in the middle
1428 // of being refined to another type (and hence, may have dropped all
1429 // references to what it was using before). So, use the new forwarded
1430 // type.
1431 if (const Type *Ty = Agg->getForwardedType())
1432 Agg = Ty;
1433 }
1434 return CurIdx == NumIdx ? Agg : 0;
1435}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001436
Dan Gohman4a3125b2008-06-17 21:07:55 +00001437const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001438 unsigned Idx) {
1439 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001440}
1441
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001442//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001443// BinaryOperator Class
1444//===----------------------------------------------------------------------===//
1445
Dan Gohmana5b96452009-06-04 22:49:04 +00001446/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1447/// type is floating-point, to help provide compatibility with an older API.
1448///
1449static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1450 const Type *Ty) {
1451 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1452 if (Ty->isFPOrFPVector()) {
1453 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1454 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1455 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1456 }
1457 return iType;
1458}
1459
Chris Lattner2195fc42007-02-24 00:55:48 +00001460BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1461 const Type *Ty, const std::string &Name,
1462 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001463 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001464 OperandTraits<BinaryOperator>::op_begin(this),
1465 OperandTraits<BinaryOperator>::operands(this),
1466 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001467 Op<0>() = S1;
1468 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001469 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001470 setName(Name);
1471}
1472
1473BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1474 const Type *Ty, const std::string &Name,
1475 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001476 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001477 OperandTraits<BinaryOperator>::op_begin(this),
1478 OperandTraits<BinaryOperator>::operands(this),
1479 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001480 Op<0>() = S1;
1481 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001482 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001483 setName(Name);
1484}
1485
1486
1487void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001488 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001489 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001490 assert(LHS->getType() == RHS->getType() &&
1491 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001492#ifndef NDEBUG
1493 switch (iType) {
1494 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001495 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001496 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001497 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001498 assert(getType()->isIntOrIntVector() &&
1499 "Tried to create an integer operation on a non-integer type!");
1500 break;
1501 case FAdd: case FSub:
1502 case FMul:
1503 assert(getType() == LHS->getType() &&
1504 "Arithmetic operation should return same type as operands!");
1505 assert(getType()->isFPOrFPVector() &&
1506 "Tried to create a floating-point operation on a "
1507 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001508 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001509 case UDiv:
1510 case SDiv:
1511 assert(getType() == LHS->getType() &&
1512 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001513 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1514 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001515 "Incorrect operand type (not integer) for S/UDIV");
1516 break;
1517 case FDiv:
1518 assert(getType() == LHS->getType() &&
1519 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001520 assert(getType()->isFPOrFPVector() &&
1521 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001522 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001523 case URem:
1524 case SRem:
1525 assert(getType() == LHS->getType() &&
1526 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001527 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1528 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001529 "Incorrect operand type (not integer) for S/UREM");
1530 break;
1531 case FRem:
1532 assert(getType() == LHS->getType() &&
1533 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001534 assert(getType()->isFPOrFPVector() &&
1535 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001536 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001537 case Shl:
1538 case LShr:
1539 case AShr:
1540 assert(getType() == LHS->getType() &&
1541 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001542 assert((getType()->isInteger() ||
1543 (isa<VectorType>(getType()) &&
1544 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1545 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001546 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001547 case And: case Or:
1548 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001549 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001550 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001551 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001552 (isa<VectorType>(getType()) &&
1553 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001554 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001555 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001556 default:
1557 break;
1558 }
1559#endif
1560}
1561
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001562BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001563 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001564 Instruction *InsertBefore) {
1565 assert(S1->getType() == S2->getType() &&
1566 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001567 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001568}
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 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001573 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001574 InsertAtEnd->getInstList().push_back(Res);
1575 return Res;
1576}
1577
Owen Anderson53a52212009-07-13 04:09:18 +00001578BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context,
1579 Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001580 Instruction *InsertBefore) {
Owen Anderson53a52212009-07-13 04:09:18 +00001581 Value *zero = Context.getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001582 return new BinaryOperator(Instruction::Sub,
1583 zero, Op,
1584 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001585}
1586
Owen Anderson53a52212009-07-13 04:09:18 +00001587BinaryOperator *BinaryOperator::CreateNeg(LLVMContext &Context,
1588 Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001589 BasicBlock *InsertAtEnd) {
Owen Anderson53a52212009-07-13 04:09:18 +00001590 Value *zero = Context.getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001591 return new BinaryOperator(Instruction::Sub,
1592 zero, Op,
1593 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001594}
1595
Owen Anderson53a52212009-07-13 04:09:18 +00001596BinaryOperator *BinaryOperator::CreateFNeg(LLVMContext &Context,
1597 Value *Op, const std::string &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001598 Instruction *InsertBefore) {
Owen Anderson53a52212009-07-13 04:09:18 +00001599 Value *zero = Context.getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001600 return new BinaryOperator(Instruction::FSub,
1601 zero, Op,
1602 Op->getType(), Name, InsertBefore);
1603}
1604
Owen Anderson53a52212009-07-13 04:09:18 +00001605BinaryOperator *BinaryOperator::CreateFNeg(LLVMContext &Context,
1606 Value *Op, const std::string &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001607 BasicBlock *InsertAtEnd) {
Owen Anderson53a52212009-07-13 04:09:18 +00001608 Value *zero = Context.getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001609 return new BinaryOperator(Instruction::FSub,
1610 zero, Op,
1611 Op->getType(), Name, InsertAtEnd);
1612}
1613
Owen Anderson542619e2009-07-13 20:58:05 +00001614BinaryOperator *BinaryOperator::CreateNot(LLVMContext &Context,
1615 Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001616 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001617 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001618 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson542619e2009-07-13 20:58:05 +00001619 C = Context.getAllOnesValue(PTy->getElementType());
Owen Andersonf945a9e2009-07-15 21:51:10 +00001620 C = Context.getConstantVector(
1621 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001622 } else {
Owen Anderson542619e2009-07-13 20:58:05 +00001623 C = Context.getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001624 }
1625
1626 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001627 Op->getType(), Name, InsertBefore);
1628}
1629
Owen Anderson542619e2009-07-13 20:58:05 +00001630BinaryOperator *BinaryOperator::CreateNot(LLVMContext &Context,
1631 Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001632 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001633 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001634 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001635 // Create a vector of all ones values.
Owen Anderson542619e2009-07-13 20:58:05 +00001636 Constant *Elt = Context.getAllOnesValue(PTy->getElementType());
Owen Andersonf945a9e2009-07-15 21:51:10 +00001637 AllOnes = Context.getConstantVector(
1638 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001639 } else {
Owen Anderson542619e2009-07-13 20:58:05 +00001640 AllOnes = Context.getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001641 }
1642
1643 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001644 Op->getType(), Name, InsertAtEnd);
1645}
1646
1647
1648// isConstantAllOnes - Helper function for several functions below
1649static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001650 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1651 return CI->isAllOnesValue();
1652 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1653 return CV->isAllOnesValue();
1654 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001655}
1656
Owen Andersonbb2501b2009-07-13 22:18:28 +00001657bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001658 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1659 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001660 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1661 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001662 return false;
1663}
1664
Owen Andersonbb2501b2009-07-13 22:18:28 +00001665bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001666 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1667 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001668 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1669 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001670 return false;
1671}
1672
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001673bool BinaryOperator::isNot(const Value *V) {
1674 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1675 return (Bop->getOpcode() == Instruction::Xor &&
1676 (isConstantAllOnes(Bop->getOperand(1)) ||
1677 isConstantAllOnes(Bop->getOperand(0))));
1678 return false;
1679}
1680
Chris Lattner2c7d1772005-04-24 07:28:37 +00001681Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001682 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001683}
1684
Chris Lattner2c7d1772005-04-24 07:28:37 +00001685const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1686 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001687}
1688
Dan Gohmana5b96452009-06-04 22:49:04 +00001689Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001690 return cast<BinaryOperator>(BinOp)->getOperand(1);
1691}
1692
1693const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1694 return getFNegArgument(const_cast<Value*>(BinOp));
1695}
1696
Chris Lattner2c7d1772005-04-24 07:28:37 +00001697Value *BinaryOperator::getNotArgument(Value *BinOp) {
1698 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1699 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1700 Value *Op0 = BO->getOperand(0);
1701 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001702 if (isConstantAllOnes(Op0)) return Op1;
1703
1704 assert(isConstantAllOnes(Op1));
1705 return Op0;
1706}
1707
Chris Lattner2c7d1772005-04-24 07:28:37 +00001708const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1709 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001710}
1711
1712
1713// swapOperands - Exchange the two operands to this instruction. This
1714// instruction is safe to use on any binary instruction and does not
1715// modify the semantics of the instruction. If the instruction is
1716// order dependent (SetLT f.e.) the opcode is changed.
1717//
1718bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001719 if (!isCommutative())
1720 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001721 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001722 return false;
1723}
1724
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001725//===----------------------------------------------------------------------===//
1726// CastInst Class
1727//===----------------------------------------------------------------------===//
1728
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001729// Just determine if this cast only deals with integral->integral conversion.
1730bool CastInst::isIntegerCast() const {
1731 switch (getOpcode()) {
1732 default: return false;
1733 case Instruction::ZExt:
1734 case Instruction::SExt:
1735 case Instruction::Trunc:
1736 return true;
1737 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001738 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001739 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001740}
1741
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001742bool CastInst::isLosslessCast() const {
1743 // Only BitCast can be lossless, exit fast if we're not BitCast
1744 if (getOpcode() != Instruction::BitCast)
1745 return false;
1746
1747 // Identity cast is always lossless
1748 const Type* SrcTy = getOperand(0)->getType();
1749 const Type* DstTy = getType();
1750 if (SrcTy == DstTy)
1751 return true;
1752
Reid Spencer8d9336d2006-12-31 05:26:44 +00001753 // Pointer to pointer is always lossless.
1754 if (isa<PointerType>(SrcTy))
1755 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001756 return false; // Other types have no identity values
1757}
1758
1759/// This function determines if the CastInst does not require any bits to be
1760/// changed in order to effect the cast. Essentially, it identifies cases where
1761/// no code gen is necessary for the cast, hence the name no-op cast. For
1762/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001763/// # bitcast i32* %x to i8*
1764/// # bitcast <2 x i32> %x to <4 x i16>
1765/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001766/// @brief Determine if a cast is a no-op.
1767bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1768 switch (getOpcode()) {
1769 default:
1770 assert(!"Invalid CastOp");
1771 case Instruction::Trunc:
1772 case Instruction::ZExt:
1773 case Instruction::SExt:
1774 case Instruction::FPTrunc:
1775 case Instruction::FPExt:
1776 case Instruction::UIToFP:
1777 case Instruction::SIToFP:
1778 case Instruction::FPToUI:
1779 case Instruction::FPToSI:
1780 return false; // These always modify bits
1781 case Instruction::BitCast:
1782 return true; // BitCast never modifies bits.
1783 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001784 return IntPtrTy->getScalarSizeInBits() ==
1785 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001786 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001787 return IntPtrTy->getScalarSizeInBits() ==
1788 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001789 }
1790}
1791
1792/// This function determines if a pair of casts can be eliminated and what
1793/// opcode should be used in the elimination. This assumes that there are two
1794/// instructions like this:
1795/// * %F = firstOpcode SrcTy %x to MidTy
1796/// * %S = secondOpcode MidTy %F to DstTy
1797/// The function returns a resultOpcode so these two casts can be replaced with:
1798/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1799/// If no such cast is permited, the function returns 0.
1800unsigned CastInst::isEliminableCastPair(
1801 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1802 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1803{
1804 // Define the 144 possibilities for these two cast instructions. The values
1805 // in this matrix determine what to do in a given situation and select the
1806 // case in the switch below. The rows correspond to firstOp, the columns
1807 // correspond to secondOp. In looking at the table below, keep in mind
1808 // the following cast properties:
1809 //
1810 // Size Compare Source Destination
1811 // Operator Src ? Size Type Sign Type Sign
1812 // -------- ------------ ------------------- ---------------------
1813 // TRUNC > Integer Any Integral Any
1814 // ZEXT < Integral Unsigned Integer Any
1815 // SEXT < Integral Signed Integer Any
1816 // FPTOUI n/a FloatPt n/a Integral Unsigned
1817 // FPTOSI n/a FloatPt n/a Integral Signed
1818 // UITOFP n/a Integral Unsigned FloatPt n/a
1819 // SITOFP n/a Integral Signed FloatPt n/a
1820 // FPTRUNC > FloatPt n/a FloatPt n/a
1821 // FPEXT < FloatPt n/a FloatPt n/a
1822 // PTRTOINT n/a Pointer n/a Integral Unsigned
1823 // INTTOPTR n/a Integral Unsigned Pointer n/a
1824 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001825 //
1826 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001827 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1828 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001829 // of the produced value (we no longer know the top-part is all zeros).
1830 // Further this conversion is often much more expensive for typical hardware,
1831 // and causes issues when building libgcc. We disallow fptosi+sext for the
1832 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001833 const unsigned numCastOps =
1834 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1835 static const uint8_t CastResults[numCastOps][numCastOps] = {
1836 // T F F U S F F P I B -+
1837 // R Z S P P I I T P 2 N T |
1838 // U E E 2 2 2 2 R E I T C +- secondOp
1839 // N X X U S F F N X N 2 V |
1840 // C T T I I P P C T T P T -+
1841 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1842 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1843 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001844 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1845 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001846 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1847 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1848 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1849 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1850 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1851 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1852 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1853 };
1854
1855 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1856 [secondOp-Instruction::CastOpsBegin];
1857 switch (ElimCase) {
1858 case 0:
1859 // categorically disallowed
1860 return 0;
1861 case 1:
1862 // allowed, use first cast's opcode
1863 return firstOp;
1864 case 2:
1865 // allowed, use second cast's opcode
1866 return secondOp;
1867 case 3:
1868 // no-op cast in second op implies firstOp as long as the DestTy
1869 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001870 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001871 return firstOp;
1872 return 0;
1873 case 4:
1874 // no-op cast in second op implies firstOp as long as the DestTy
1875 // is floating point
1876 if (DstTy->isFloatingPoint())
1877 return firstOp;
1878 return 0;
1879 case 5:
1880 // no-op cast in first op implies secondOp as long as the SrcTy
1881 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001882 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001883 return secondOp;
1884 return 0;
1885 case 6:
1886 // no-op cast in first op implies secondOp as long as the SrcTy
1887 // is a floating point
1888 if (SrcTy->isFloatingPoint())
1889 return secondOp;
1890 return 0;
1891 case 7: {
1892 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001893 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1894 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001895 if (MidSize >= PtrSize)
1896 return Instruction::BitCast;
1897 return 0;
1898 }
1899 case 8: {
1900 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1901 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1902 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001903 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1904 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001905 if (SrcSize == DstSize)
1906 return Instruction::BitCast;
1907 else if (SrcSize < DstSize)
1908 return firstOp;
1909 return secondOp;
1910 }
1911 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1912 return Instruction::ZExt;
1913 case 10:
1914 // fpext followed by ftrunc is allowed if the bit size returned to is
1915 // the same as the original, in which case its just a bitcast
1916 if (SrcTy == DstTy)
1917 return Instruction::BitCast;
1918 return 0; // If the types are not the same we can't eliminate it.
1919 case 11:
1920 // bitcast followed by ptrtoint is allowed as long as the bitcast
1921 // is a pointer to pointer cast.
1922 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1923 return secondOp;
1924 return 0;
1925 case 12:
1926 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1927 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1928 return firstOp;
1929 return 0;
1930 case 13: {
1931 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001932 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1933 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1934 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001935 if (SrcSize <= PtrSize && SrcSize == DstSize)
1936 return Instruction::BitCast;
1937 return 0;
1938 }
1939 case 99:
1940 // cast combination can't happen (error in input). This is for all cases
1941 // where the MidTy is not the same for the two cast instructions.
1942 assert(!"Invalid Cast Combination");
1943 return 0;
1944 default:
1945 assert(!"Error in CastResults table!!!");
1946 return 0;
1947 }
1948 return 0;
1949}
1950
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001951CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001952 const std::string &Name, Instruction *InsertBefore) {
1953 // Construct and return the appropriate CastInst subclass
1954 switch (op) {
1955 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1956 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1957 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1958 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1959 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1960 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1961 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1962 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1963 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1964 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1965 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1966 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1967 default:
1968 assert(!"Invalid opcode provided");
1969 }
1970 return 0;
1971}
1972
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001973CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001974 const std::string &Name, BasicBlock *InsertAtEnd) {
1975 // Construct and return the appropriate CastInst subclass
1976 switch (op) {
1977 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1978 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1979 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1980 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1981 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1982 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1983 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1984 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1985 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1986 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1987 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1988 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1989 default:
1990 assert(!"Invalid opcode provided");
1991 }
1992 return 0;
1993}
1994
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001995CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00001996 const std::string &Name,
1997 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001998 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001999 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2000 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002001}
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 BasicBlock *InsertAtEnd) {
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, InsertAtEnd);
2008 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002009}
2010
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002011CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002012 const std::string &Name,
2013 Instruction *InsertBefore) {
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, InsertBefore);
2016 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
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 BasicBlock *InsertAtEnd) {
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, InsertAtEnd);
2024 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002025}
2026
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002027CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002028 const std::string &Name,
2029 Instruction *InsertBefore) {
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, InsertBefore);
2032 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
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 BasicBlock *InsertAtEnd) {
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, InsertAtEnd);
2040 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002041}
2042
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002043CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002044 const std::string &Name,
2045 BasicBlock *InsertAtEnd) {
2046 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002047 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002048 "Invalid cast");
2049
Chris Lattner03c49532007-01-15 02:27:26 +00002050 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002051 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2052 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002053}
2054
2055/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002056CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002057 const std::string &Name,
2058 Instruction *InsertBefore) {
2059 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002060 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002061 "Invalid cast");
2062
Chris Lattner03c49532007-01-15 02:27:26 +00002063 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002064 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2065 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002066}
2067
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002068CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002069 bool isSigned, const std::string &Name,
2070 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002071 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002072 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2073 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002074 Instruction::CastOps opcode =
2075 (SrcBits == DstBits ? Instruction::BitCast :
2076 (SrcBits > DstBits ? Instruction::Trunc :
2077 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002078 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002079}
2080
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002081CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002082 bool isSigned, const std::string &Name,
2083 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002084 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2085 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002086 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2087 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002088 Instruction::CastOps opcode =
2089 (SrcBits == DstBits ? Instruction::BitCast :
2090 (SrcBits > DstBits ? Instruction::Trunc :
2091 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002092 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002093}
2094
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002095CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002096 const std::string &Name,
2097 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002098 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002099 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002100 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2101 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002102 Instruction::CastOps opcode =
2103 (SrcBits == DstBits ? Instruction::BitCast :
2104 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002105 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002106}
2107
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002108CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002109 const std::string &Name,
2110 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002111 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002112 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002113 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2114 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002115 Instruction::CastOps opcode =
2116 (SrcBits == DstBits ? Instruction::BitCast :
2117 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002118 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002119}
2120
Duncan Sands55e50902008-01-06 10:12:28 +00002121// Check whether it is valid to call getCastOpcode for these types.
2122// This routine must be kept in sync with getCastOpcode.
2123bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2124 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2125 return false;
2126
2127 if (SrcTy == DestTy)
2128 return true;
2129
2130 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002131 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2132 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002133
2134 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002135 if (DestTy->isInteger()) { // Casting to integral
2136 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002137 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002138 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002139 return true;
2140 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002141 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002142 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002143 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002144 return isa<PointerType>(SrcTy);
2145 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002146 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2147 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002148 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002149 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002150 return true;
2151 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002152 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002153 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002154 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002155 return false;
2156 }
2157 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002158 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002159 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002160 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002161 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002162 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002163 return DestPTy->getBitWidth() == SrcBits;
2164 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002165 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2166 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002167 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002168 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002169 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002170 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002171 return false;
2172 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002173 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002174 return false;
2175 }
2176}
2177
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002178// Provide a way to get a "cast" where the cast opcode is inferred from the
2179// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002180// logic in the castIsValid function below. This axiom should hold:
2181// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2182// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002183// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002184// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002185Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002186CastInst::getCastOpcode(
2187 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002188 // Get the bit sizes, we'll need these
2189 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002190 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2191 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002192
Duncan Sands55e50902008-01-06 10:12:28 +00002193 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2194 "Only first class types are castable!");
2195
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002196 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002197 if (DestTy->isInteger()) { // Casting to integral
2198 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002199 if (DestBits < SrcBits)
2200 return Trunc; // int -> smaller int
2201 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002202 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002203 return SExt; // signed -> SEXT
2204 else
2205 return ZExt; // unsigned -> ZEXT
2206 } else {
2207 return BitCast; // Same size, No-op cast
2208 }
2209 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002210 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002211 return FPToSI; // FP -> sint
2212 else
2213 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002214 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002215 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002216 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002217 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002218 return BitCast; // Same size, no-op cast
2219 } else {
2220 assert(isa<PointerType>(SrcTy) &&
2221 "Casting from a value that is not first-class type");
2222 return PtrToInt; // ptr -> int
2223 }
2224 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002225 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002226 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002227 return SIToFP; // sint -> FP
2228 else
2229 return UIToFP; // uint -> FP
2230 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2231 if (DestBits < SrcBits) {
2232 return FPTrunc; // FP -> smaller FP
2233 } else if (DestBits > SrcBits) {
2234 return FPExt; // FP -> larger FP
2235 } else {
2236 return BitCast; // same size, no-op cast
2237 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002238 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002239 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002240 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002241 PTy = NULL;
2242 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002243 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002244 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002245 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002246 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2247 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002248 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002249 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002250 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002251 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002252 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002253 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002254 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002255 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002256 }
2257 } else if (isa<PointerType>(DestTy)) {
2258 if (isa<PointerType>(SrcTy)) {
2259 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002260 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002261 return IntToPtr; // int -> ptr
2262 } else {
2263 assert(!"Casting pointer to other than pointer or int");
2264 }
2265 } else {
2266 assert(!"Casting to type that is not first-class");
2267 }
2268
2269 // If we fall through to here we probably hit an assertion cast above
2270 // and assertions are not turned on. Anything we return is an error, so
2271 // BitCast is as good a choice as any.
2272 return BitCast;
2273}
2274
2275//===----------------------------------------------------------------------===//
2276// CastInst SubClass Constructors
2277//===----------------------------------------------------------------------===//
2278
2279/// Check that the construction parameters for a CastInst are correct. This
2280/// could be broken out into the separate constructors but it is useful to have
2281/// it in one place and to eliminate the redundant code for getting the sizes
2282/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002283bool
2284CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002285
2286 // Check for type sanity on the arguments
2287 const Type *SrcTy = S->getType();
2288 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2289 return false;
2290
2291 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002292 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2293 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002294
2295 // Switch on the opcode provided
2296 switch (op) {
2297 default: return false; // This is an input error
2298 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002299 return SrcTy->isIntOrIntVector() &&
2300 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002302 return SrcTy->isIntOrIntVector() &&
2303 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002304 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002305 return SrcTy->isIntOrIntVector() &&
2306 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002307 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002308 return SrcTy->isFPOrFPVector() &&
2309 DstTy->isFPOrFPVector() &&
2310 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002311 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002312 return SrcTy->isFPOrFPVector() &&
2313 DstTy->isFPOrFPVector() &&
2314 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002315 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002316 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002317 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2318 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002319 return SVTy->getElementType()->isIntOrIntVector() &&
2320 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002321 SVTy->getNumElements() == DVTy->getNumElements();
2322 }
2323 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002324 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002325 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002326 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002327 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2328 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002329 return SVTy->getElementType()->isFPOrFPVector() &&
2330 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002331 SVTy->getNumElements() == DVTy->getNumElements();
2332 }
2333 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002334 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002335 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002336 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002337 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002338 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002339 case Instruction::BitCast:
2340 // BitCast implies a no-op cast of type only. No bits change.
2341 // However, you can't cast pointers to anything but pointers.
2342 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2343 return false;
2344
Duncan Sands55e50902008-01-06 10:12:28 +00002345 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002346 // these cases, the cast is okay if the source and destination bit widths
2347 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002348 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002349 }
2350}
2351
2352TruncInst::TruncInst(
2353 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2354) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002355 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002356}
2357
2358TruncInst::TruncInst(
2359 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2360) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002361 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362}
2363
2364ZExtInst::ZExtInst(
2365 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2366) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002367 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002368}
2369
2370ZExtInst::ZExtInst(
2371 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2372) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002373 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002374}
2375SExtInst::SExtInst(
2376 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2377) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002378 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379}
2380
Jeff Cohencc08c832006-12-02 02:22:01 +00002381SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002382 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2383) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002384 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002385}
2386
2387FPTruncInst::FPTruncInst(
2388 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2389) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002390 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391}
2392
2393FPTruncInst::FPTruncInst(
2394 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2395) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002396 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002397}
2398
2399FPExtInst::FPExtInst(
2400 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2401) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002402 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002403}
2404
2405FPExtInst::FPExtInst(
2406 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2407) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002408 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002409}
2410
2411UIToFPInst::UIToFPInst(
2412 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2413) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002414 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002415}
2416
2417UIToFPInst::UIToFPInst(
2418 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2419) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002420 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002421}
2422
2423SIToFPInst::SIToFPInst(
2424 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2425) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002426 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002427}
2428
2429SIToFPInst::SIToFPInst(
2430 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2431) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002432 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002433}
2434
2435FPToUIInst::FPToUIInst(
2436 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2437) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002438 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002439}
2440
2441FPToUIInst::FPToUIInst(
2442 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2443) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002444 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002445}
2446
2447FPToSIInst::FPToSIInst(
2448 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2449) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002450 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002451}
2452
2453FPToSIInst::FPToSIInst(
2454 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2455) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002456 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002457}
2458
2459PtrToIntInst::PtrToIntInst(
2460 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2461) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002462 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463}
2464
2465PtrToIntInst::PtrToIntInst(
2466 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2467) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002468 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469}
2470
2471IntToPtrInst::IntToPtrInst(
2472 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2473) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002474 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002475}
2476
2477IntToPtrInst::IntToPtrInst(
2478 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2479) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002480 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002481}
2482
2483BitCastInst::BitCastInst(
2484 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2485) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002486 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002487}
2488
2489BitCastInst::BitCastInst(
2490 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2491) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002492 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002493}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002494
2495//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002496// CmpInst Classes
2497//===----------------------------------------------------------------------===//
2498
Nate Begemand2195702008-05-12 19:01:56 +00002499CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2500 Value *LHS, Value *RHS, const std::string &Name,
2501 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002502 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002503 OperandTraits<CmpInst>::op_begin(this),
2504 OperandTraits<CmpInst>::operands(this),
2505 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002506 Op<0>() = LHS;
2507 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002508 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002509 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002510}
Gabor Greiff6caff662008-05-10 08:32:32 +00002511
Nate Begemand2195702008-05-12 19:01:56 +00002512CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2513 Value *LHS, Value *RHS, const std::string &Name,
2514 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002515 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002516 OperandTraits<CmpInst>::op_begin(this),
2517 OperandTraits<CmpInst>::operands(this),
2518 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002519 Op<0>() = LHS;
2520 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002521 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002522 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002523}
2524
2525CmpInst *
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002526CmpInst::Create(LLVMContext &Context, OtherOps Op, unsigned short predicate,
2527 Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002528 const std::string &Name, Instruction *InsertBefore) {
2529 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002530 if (InsertBefore)
2531 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2532 S1, S2, Name);
2533 else
2534 return new ICmpInst(Context, CmpInst::Predicate(predicate),
2535 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002536 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002537
2538 if (InsertBefore)
2539 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2540 S1, S2, Name);
2541 else
2542 return new FCmpInst(Context, CmpInst::Predicate(predicate),
2543 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002544}
2545
2546CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002547CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002548 const std::string &Name, BasicBlock *InsertAtEnd) {
2549 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002550 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2551 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002552 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002553 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2554 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002555}
2556
2557void CmpInst::swapOperands() {
2558 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2559 IC->swapOperands();
2560 else
2561 cast<FCmpInst>(this)->swapOperands();
2562}
2563
2564bool CmpInst::isCommutative() {
2565 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2566 return IC->isCommutative();
2567 return cast<FCmpInst>(this)->isCommutative();
2568}
2569
2570bool CmpInst::isEquality() {
2571 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2572 return IC->isEquality();
2573 return cast<FCmpInst>(this)->isEquality();
2574}
2575
2576
Dan Gohman4e724382008-05-31 02:47:54 +00002577CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002578 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002579 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002580 case ICMP_EQ: return ICMP_NE;
2581 case ICMP_NE: return ICMP_EQ;
2582 case ICMP_UGT: return ICMP_ULE;
2583 case ICMP_ULT: return ICMP_UGE;
2584 case ICMP_UGE: return ICMP_ULT;
2585 case ICMP_ULE: return ICMP_UGT;
2586 case ICMP_SGT: return ICMP_SLE;
2587 case ICMP_SLT: return ICMP_SGE;
2588 case ICMP_SGE: return ICMP_SLT;
2589 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002590
Dan Gohman4e724382008-05-31 02:47:54 +00002591 case FCMP_OEQ: return FCMP_UNE;
2592 case FCMP_ONE: return FCMP_UEQ;
2593 case FCMP_OGT: return FCMP_ULE;
2594 case FCMP_OLT: return FCMP_UGE;
2595 case FCMP_OGE: return FCMP_ULT;
2596 case FCMP_OLE: return FCMP_UGT;
2597 case FCMP_UEQ: return FCMP_ONE;
2598 case FCMP_UNE: return FCMP_OEQ;
2599 case FCMP_UGT: return FCMP_OLE;
2600 case FCMP_ULT: return FCMP_OGE;
2601 case FCMP_UGE: return FCMP_OLT;
2602 case FCMP_ULE: return FCMP_OGT;
2603 case FCMP_ORD: return FCMP_UNO;
2604 case FCMP_UNO: return FCMP_ORD;
2605 case FCMP_TRUE: return FCMP_FALSE;
2606 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002607 }
2608}
2609
Reid Spencer266e42b2006-12-23 06:05:41 +00002610ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2611 switch (pred) {
2612 default: assert(! "Unknown icmp predicate!");
2613 case ICMP_EQ: case ICMP_NE:
2614 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2615 return pred;
2616 case ICMP_UGT: return ICMP_SGT;
2617 case ICMP_ULT: return ICMP_SLT;
2618 case ICMP_UGE: return ICMP_SGE;
2619 case ICMP_ULE: return ICMP_SLE;
2620 }
2621}
2622
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002623ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2624 switch (pred) {
2625 default: assert(! "Unknown icmp predicate!");
2626 case ICMP_EQ: case ICMP_NE:
2627 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2628 return pred;
2629 case ICMP_SGT: return ICMP_UGT;
2630 case ICMP_SLT: return ICMP_ULT;
2631 case ICMP_SGE: return ICMP_UGE;
2632 case ICMP_SLE: return ICMP_ULE;
2633 }
2634}
2635
Reid Spencer266e42b2006-12-23 06:05:41 +00002636bool ICmpInst::isSignedPredicate(Predicate pred) {
2637 switch (pred) {
2638 default: assert(! "Unknown icmp predicate!");
2639 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2640 return true;
2641 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2642 case ICMP_UGE: case ICMP_ULE:
2643 return false;
2644 }
2645}
2646
Reid Spencer0286bc12007-02-28 22:00:54 +00002647/// Initialize a set of values that all satisfy the condition with C.
2648///
2649ConstantRange
2650ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2651 APInt Lower(C);
2652 APInt Upper(C);
2653 uint32_t BitWidth = C.getBitWidth();
2654 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002655 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002656 case ICmpInst::ICMP_EQ: Upper++; break;
2657 case ICmpInst::ICMP_NE: Lower++; break;
2658 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2659 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2660 case ICmpInst::ICMP_UGT:
2661 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2662 break;
2663 case ICmpInst::ICMP_SGT:
2664 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2665 break;
2666 case ICmpInst::ICMP_ULE:
2667 Lower = APInt::getMinValue(BitWidth); Upper++;
2668 break;
2669 case ICmpInst::ICMP_SLE:
2670 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2671 break;
2672 case ICmpInst::ICMP_UGE:
2673 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2674 break;
2675 case ICmpInst::ICMP_SGE:
2676 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2677 break;
2678 }
2679 return ConstantRange(Lower, Upper);
2680}
2681
Dan Gohman4e724382008-05-31 02:47:54 +00002682CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002683 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002684 default: assert(!"Unknown cmp predicate!");
2685 case ICMP_EQ: case ICMP_NE:
2686 return pred;
2687 case ICMP_SGT: return ICMP_SLT;
2688 case ICMP_SLT: return ICMP_SGT;
2689 case ICMP_SGE: return ICMP_SLE;
2690 case ICMP_SLE: return ICMP_SGE;
2691 case ICMP_UGT: return ICMP_ULT;
2692 case ICMP_ULT: return ICMP_UGT;
2693 case ICMP_UGE: return ICMP_ULE;
2694 case ICMP_ULE: return ICMP_UGE;
2695
Reid Spencerd9436b62006-11-20 01:22:35 +00002696 case FCMP_FALSE: case FCMP_TRUE:
2697 case FCMP_OEQ: case FCMP_ONE:
2698 case FCMP_UEQ: case FCMP_UNE:
2699 case FCMP_ORD: case FCMP_UNO:
2700 return pred;
2701 case FCMP_OGT: return FCMP_OLT;
2702 case FCMP_OLT: return FCMP_OGT;
2703 case FCMP_OGE: return FCMP_OLE;
2704 case FCMP_OLE: return FCMP_OGE;
2705 case FCMP_UGT: return FCMP_ULT;
2706 case FCMP_ULT: return FCMP_UGT;
2707 case FCMP_UGE: return FCMP_ULE;
2708 case FCMP_ULE: return FCMP_UGE;
2709 }
2710}
2711
Reid Spencer266e42b2006-12-23 06:05:41 +00002712bool CmpInst::isUnsigned(unsigned short predicate) {
2713 switch (predicate) {
2714 default: return false;
2715 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2716 case ICmpInst::ICMP_UGE: return true;
2717 }
2718}
2719
2720bool CmpInst::isSigned(unsigned short predicate){
2721 switch (predicate) {
2722 default: return false;
2723 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2724 case ICmpInst::ICMP_SGE: return true;
2725 }
2726}
2727
2728bool CmpInst::isOrdered(unsigned short predicate) {
2729 switch (predicate) {
2730 default: return false;
2731 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2732 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2733 case FCmpInst::FCMP_ORD: return true;
2734 }
2735}
2736
2737bool CmpInst::isUnordered(unsigned short predicate) {
2738 switch (predicate) {
2739 default: return false;
2740 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2741 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2742 case FCmpInst::FCMP_UNO: return true;
2743 }
2744}
2745
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002746//===----------------------------------------------------------------------===//
2747// SwitchInst Implementation
2748//===----------------------------------------------------------------------===//
2749
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002750void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002751 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002752 ReservedSpace = 2+NumCases*2;
2753 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002754 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002755
Gabor Greif2d3024d2008-05-26 21:33:52 +00002756 OperandList[0] = Value;
2757 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002758}
2759
Chris Lattner2195fc42007-02-24 00:55:48 +00002760/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2761/// switch on and a default destination. The number of additional cases can
2762/// be specified here to make memory allocation more efficient. This
2763/// constructor can also autoinsert before another instruction.
2764SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2765 Instruction *InsertBefore)
2766 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2767 init(Value, Default, NumCases);
2768}
2769
2770/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2771/// switch on and a default destination. The number of additional cases can
2772/// be specified here to make memory allocation more efficient. This
2773/// constructor also autoinserts at the end of the specified BasicBlock.
2774SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2775 BasicBlock *InsertAtEnd)
2776 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2777 init(Value, Default, NumCases);
2778}
2779
Misha Brukmanb1c93172005-04-21 23:48:37 +00002780SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002781 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002782 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002783 Use *OL = OperandList, *InOL = SI.OperandList;
2784 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002785 OL[i] = InOL[i];
2786 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002787 }
2788}
2789
Gordon Henriksen14a55692007-12-10 02:14:30 +00002790SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002791 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002792}
2793
2794
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002795/// addCase - Add an entry to the switch instruction...
2796///
Chris Lattner47ac1872005-02-24 05:32:09 +00002797void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002798 unsigned OpNo = NumOperands;
2799 if (OpNo+2 > ReservedSpace)
2800 resizeOperands(0); // Get more space!
2801 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002802 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002803 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002804 OperandList[OpNo] = OnVal;
2805 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002806}
2807
2808/// removeCase - This method removes the specified successor from the switch
2809/// instruction. Note that this cannot be used to remove the default
2810/// destination (successor #0).
2811///
2812void SwitchInst::removeCase(unsigned idx) {
2813 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002814 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2815
2816 unsigned NumOps = getNumOperands();
2817 Use *OL = OperandList;
2818
2819 // Move everything after this operand down.
2820 //
2821 // FIXME: we could just swap with the end of the list, then erase. However,
2822 // client might not expect this to happen. The code as it is thrashes the
2823 // use/def lists, which is kinda lame.
2824 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2825 OL[i-2] = OL[i];
2826 OL[i-2+1] = OL[i+1];
2827 }
2828
2829 // Nuke the last value.
2830 OL[NumOps-2].set(0);
2831 OL[NumOps-2+1].set(0);
2832 NumOperands = NumOps-2;
2833}
2834
2835/// resizeOperands - resize operands - This adjusts the length of the operands
2836/// list according to the following behavior:
2837/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002838/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002839/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2840/// 3. If NumOps == NumOperands, trim the reserved space.
2841///
2842void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002843 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002844 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002845 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002846 } else if (NumOps*2 > NumOperands) {
2847 // No resize needed.
2848 if (ReservedSpace >= NumOps) return;
2849 } else if (NumOps == NumOperands) {
2850 if (ReservedSpace == NumOps) return;
2851 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002852 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002853 }
2854
2855 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002856 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002857 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002858 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002859 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002860 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002861 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002862 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002863}
2864
2865
2866BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2867 return getSuccessor(idx);
2868}
2869unsigned SwitchInst::getNumSuccessorsV() const {
2870 return getNumSuccessors();
2871}
2872void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2873 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002874}
Chris Lattnerf22be932004-10-15 23:52:53 +00002875
Chris Lattnerf22be932004-10-15 23:52:53 +00002876// Define these methods here so vtables don't get emitted into every translation
2877// unit that uses these classes.
2878
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002879GetElementPtrInst *GetElementPtrInst::clone(LLVMContext&) const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002880 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002881}
2882
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002883BinaryOperator *BinaryOperator::clone(LLVMContext&) const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002884 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002885}
2886
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002887FCmpInst* FCmpInst::clone(LLVMContext &Context) const {
2888 return new FCmpInst(Context, getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002889}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002890ICmpInst* ICmpInst::clone(LLVMContext &Context) const {
2891 return new ICmpInst(Context, getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002892}
2893
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002894ExtractValueInst *ExtractValueInst::clone(LLVMContext&) const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002895 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002896}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002897InsertValueInst *InsertValueInst::clone(LLVMContext&) const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002898 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002899}
2900
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002901MallocInst *MallocInst::clone(LLVMContext&) const {
2902 return new MallocInst(*this);
2903}
Dan Gohman0752bff2008-05-23 00:36:11 +00002904
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002905AllocaInst *AllocaInst::clone(LLVMContext&) const {
2906 return new AllocaInst(*this);
2907}
2908
2909FreeInst *FreeInst::clone(LLVMContext&) const {
2910 return new FreeInst(getOperand(0));
2911}
2912
2913LoadInst *LoadInst::clone(LLVMContext&) const {
2914 return new LoadInst(*this);
2915}
2916
2917StoreInst *StoreInst::clone(LLVMContext&) const {
2918 return new StoreInst(*this);
2919}
2920
2921CastInst *TruncInst::clone(LLVMContext&) const {
2922 return new TruncInst(*this);
2923}
2924
2925CastInst *ZExtInst::clone(LLVMContext&) const {
2926 return new ZExtInst(*this);
2927}
2928
2929CastInst *SExtInst::clone(LLVMContext&) const {
2930 return new SExtInst(*this);
2931}
2932
2933CastInst *FPTruncInst::clone(LLVMContext&) const {
2934 return new FPTruncInst(*this);
2935}
2936
2937CastInst *FPExtInst::clone(LLVMContext&) const {
2938 return new FPExtInst(*this);
2939}
2940
2941CastInst *UIToFPInst::clone(LLVMContext&) const {
2942 return new UIToFPInst(*this);
2943}
2944
2945CastInst *SIToFPInst::clone(LLVMContext&) const {
2946 return new SIToFPInst(*this);
2947}
2948
2949CastInst *FPToUIInst::clone(LLVMContext&) const {
2950 return new FPToUIInst(*this);
2951}
2952
2953CastInst *FPToSIInst::clone(LLVMContext&) const {
2954 return new FPToSIInst(*this);
2955}
2956
2957CastInst *PtrToIntInst::clone(LLVMContext&) const {
2958 return new PtrToIntInst(*this);
2959}
2960
2961CastInst *IntToPtrInst::clone(LLVMContext&) const {
2962 return new IntToPtrInst(*this);
2963}
2964
2965CastInst *BitCastInst::clone(LLVMContext&) const {
2966 return new BitCastInst(*this);
2967}
2968
2969CallInst *CallInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00002970 return new(getNumOperands()) CallInst(*this);
2971}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002972
2973SelectInst *SelectInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00002974 return new(getNumOperands()) SelectInst(*this);
2975}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002976
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002977VAArgInst *VAArgInst::clone(LLVMContext&) const {
2978 return new VAArgInst(*this);
2979}
2980
2981ExtractElementInst *ExtractElementInst::clone(LLVMContext&) const {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002982 return new ExtractElementInst(*this);
2983}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002984
2985InsertElementInst *InsertElementInst::clone(LLVMContext&) const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002986 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002987}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002988
2989ShuffleVectorInst *ShuffleVectorInst::clone(LLVMContext&) const {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002990 return new ShuffleVectorInst(*this);
2991}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002992
2993PHINode *PHINode::clone(LLVMContext&) const {
2994 return new PHINode(*this);
2995}
2996
2997ReturnInst *ReturnInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00002998 return new(getNumOperands()) ReturnInst(*this);
2999}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003000
3001BranchInst *BranchInst::clone(LLVMContext&) const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003002 unsigned Ops(getNumOperands());
3003 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003004}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003005
3006SwitchInst *SwitchInst::clone(LLVMContext&) const {
3007 return new SwitchInst(*this);
3008}
3009
3010InvokeInst *InvokeInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00003011 return new(getNumOperands()) InvokeInst(*this);
3012}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003013
3014UnwindInst *UnwindInst::clone(LLVMContext&) const {
3015 return new UnwindInst();
3016}
3017
3018UnreachableInst *UnreachableInst::clone(LLVMContext&) const {
3019 return new UnreachableInst();
3020}