blob: 66269301847c7d387d7ee8142c8d97a584446cd7 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000015#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Function.h"
18#include "llvm/Instructions.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000019#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000020#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000021#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000022#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023using namespace llvm;
24
Chris Lattner3e13b8c2008-01-02 23:42:30 +000025//===----------------------------------------------------------------------===//
26// CallSite Class
27//===----------------------------------------------------------------------===//
28
Gabor Greif1b9921a2009-01-11 22:33:22 +000029#define CALLSITE_DELEGATE_GETTER(METHOD) \
30 Instruction *II(getInstruction()); \
31 return isCall() \
32 ? cast<CallInst>(II)->METHOD \
33 : cast<InvokeInst>(II)->METHOD
34
35#define CALLSITE_DELEGATE_SETTER(METHOD) \
36 Instruction *II(getInstruction()); \
37 if (isCall()) \
38 cast<CallInst>(II)->METHOD; \
39 else \
40 cast<InvokeInst>(II)->METHOD
41
Duncan Sands85fab3a2008-02-18 17:32:13 +000042CallSite::CallSite(Instruction *C) {
43 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
Gabor Greif1b9921a2009-01-11 22:33:22 +000044 I.setPointer(C);
45 I.setInt(isa<CallInst>(C));
Duncan Sands85fab3a2008-02-18 17:32:13 +000046}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000047unsigned CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000048 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000049}
50void CallSite::setCallingConv(unsigned CC) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000051 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
Chris Lattnerf7b6d312005-05-06 20:26:43 +000052}
Devang Patel4c758ea2008-09-25 21:00:45 +000053const AttrListPtr &CallSite::getAttributes() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000054 CALLSITE_DELEGATE_GETTER(getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000055}
Devang Patel4c758ea2008-09-25 21:00:45 +000056void CallSite::setAttributes(const AttrListPtr &PAL) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000057 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000058}
Devang Patelba3fa6c2008-09-23 23:03:40 +000059bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000060 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
Duncan Sands5208d1a2007-11-28 17:07:01 +000061}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000062uint16_t CallSite::getParamAlignment(uint16_t i) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000063 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
Dale Johanneseneabc5f32008-02-22 17:49:45 +000064}
Duncan Sands38ef3a82007-12-03 20:06:50 +000065bool CallSite::doesNotAccessMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000066 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000067}
Duncan Sands78c88722008-07-08 08:38:44 +000068void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000069 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000070}
Duncan Sands38ef3a82007-12-03 20:06:50 +000071bool CallSite::onlyReadsMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000072 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000073}
Duncan Sands78c88722008-07-08 08:38:44 +000074void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000075 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000076}
77bool CallSite::doesNotReturn() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000078 CALLSITE_DELEGATE_GETTER(doesNotReturn());
Duncan Sands78c88722008-07-08 08:38:44 +000079}
80void CallSite::setDoesNotReturn(bool doesNotReturn) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000081 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
Duncan Sands78c88722008-07-08 08:38:44 +000082}
Duncan Sands3353ed02007-12-18 09:59:50 +000083bool CallSite::doesNotThrow() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000084 CALLSITE_DELEGATE_GETTER(doesNotThrow());
Duncan Sands8e4847e2007-12-16 15:51:49 +000085}
Duncan Sandsaa31b922007-12-19 21:13:37 +000086void CallSite::setDoesNotThrow(bool doesNotThrow) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000087 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
Duncan Sandsaa31b922007-12-19 21:13:37 +000088}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000089
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000090bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000091 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
92 if (AI->get() == Arg)
93 return true;
94 return false;
95}
96
Gabor Greif1b9921a2009-01-11 22:33:22 +000097#undef CALLSITE_DELEGATE_GETTER
98#undef CALLSITE_DELEGATE_SETTER
99
Gordon Henriksen14a55692007-12-10 02:14:30 +0000100//===----------------------------------------------------------------------===//
101// TerminatorInst Class
102//===----------------------------------------------------------------------===//
103
104// Out of line virtual method, so the vtable, etc has a home.
105TerminatorInst::~TerminatorInst() {
106}
107
Gabor Greiff6caff662008-05-10 08:32:32 +0000108//===----------------------------------------------------------------------===//
109// UnaryInstruction Class
110//===----------------------------------------------------------------------===//
111
Gordon Henriksen14a55692007-12-10 02:14:30 +0000112// Out of line virtual method, so the vtable, etc has a home.
113UnaryInstruction::~UnaryInstruction() {
114}
115
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000116//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000117// SelectInst Class
118//===----------------------------------------------------------------------===//
119
120/// areInvalidOperands - Return a string if the specified operands are invalid
121/// for a select operation, otherwise return null.
122const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
123 if (Op1->getType() != Op2->getType())
124 return "both values to select must have same type";
125
126 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
127 // Vector select.
128 if (VT->getElementType() != Type::Int1Ty)
129 return "vector select condition element type must be i1";
130 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
131 if (ET == 0)
132 return "selected values for vector select must be vectors";
133 if (ET->getNumElements() != VT->getNumElements())
134 return "vector select requires selected vectors to have "
135 "the same vector length as select condition";
136 } else if (Op0->getType() != Type::Int1Ty) {
137 return "select condition must be i1 or <n x i1>";
138 }
139 return 0;
140}
141
142
143//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000144// PHINode Class
145//===----------------------------------------------------------------------===//
146
147PHINode::PHINode(const PHINode &PN)
148 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000149 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000150 ReservedSpace(PN.getNumOperands()) {
151 Use *OL = OperandList;
152 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000153 OL[i] = PN.getOperand(i);
154 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000155 }
156}
157
Gordon Henriksen14a55692007-12-10 02:14:30 +0000158PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000159 if (OperandList)
160 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000161}
162
163// removeIncomingValue - Remove an incoming value. This is useful if a
164// predecessor basic block is deleted.
165Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
166 unsigned NumOps = getNumOperands();
167 Use *OL = OperandList;
168 assert(Idx*2 < NumOps && "BB not in PHI node!");
169 Value *Removed = OL[Idx*2];
170
171 // Move everything after this operand down.
172 //
173 // FIXME: we could just swap with the end of the list, then erase. However,
174 // client might not expect this to happen. The code as it is thrashes the
175 // use/def lists, which is kinda lame.
176 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
177 OL[i-2] = OL[i];
178 OL[i-2+1] = OL[i+1];
179 }
180
181 // Nuke the last value.
182 OL[NumOps-2].set(0);
183 OL[NumOps-2+1].set(0);
184 NumOperands = NumOps-2;
185
186 // If the PHI node is dead, because it has zero entries, nuke it now.
187 if (NumOps == 2 && DeletePHIIfEmpty) {
188 // If anyone is using this PHI, make them use a dummy value instead...
189 replaceAllUsesWith(UndefValue::get(getType()));
190 eraseFromParent();
191 }
192 return Removed;
193}
194
195/// resizeOperands - resize operands - This adjusts the length of the operands
196/// list according to the following behavior:
197/// 1. If NumOps == 0, grow the operand list in response to a push_back style
198/// of operation. This grows the number of ops by 1.5 times.
199/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
200/// 3. If NumOps == NumOperands, trim the reserved space.
201///
202void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000203 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000204 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000205 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000206 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
207 } else if (NumOps*2 > NumOperands) {
208 // No resize needed.
209 if (ReservedSpace >= NumOps) return;
210 } else if (NumOps == NumOperands) {
211 if (ReservedSpace == NumOps) return;
212 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000213 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000214 }
215
216 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000217 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000218 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000219 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000220 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000221 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000222}
223
Nate Begemanb3923212005-08-04 23:24:19 +0000224/// hasConstantValue - If the specified PHI node always merges together the same
225/// value, return the value, otherwise return null.
226///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000227Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000228 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000229 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000230 if (getIncomingValue(0) != this) // not X = phi X
231 return getIncomingValue(0);
232 else
233 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000234 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000235
Nate Begemanb3923212005-08-04 23:24:19 +0000236 // Otherwise if all of the incoming values are the same for the PHI, replace
237 // the PHI node with the incoming value.
238 //
239 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000240 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000241 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000242 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000243 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000244 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000245 if (InVal && getIncomingValue(i) != InVal)
246 return 0; // Not the same, bail out.
247 else
248 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000249 }
Nate Begemanb3923212005-08-04 23:24:19 +0000250
251 // The only case that could cause InVal to be null is if we have a PHI node
252 // that only has entries for itself. In this case, there is no entry into the
253 // loop, so kill the PHI.
254 //
255 if (InVal == 0) InVal = UndefValue::get(getType());
256
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000257 // If we have a PHI node like phi(X, undef, X), where X is defined by some
258 // instruction, we cannot always return X as the result of the PHI node. Only
259 // do this if X is not an instruction (thus it must dominate the PHI block),
260 // or if the client is prepared to deal with this possibility.
261 if (HasUndefInput && !AllowNonDominatingInstruction)
262 if (Instruction *IV = dyn_cast<Instruction>(InVal))
263 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000264 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000265 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000266 return 0; // Cannot guarantee that InVal dominates this PHINode.
267
Nate Begemanb3923212005-08-04 23:24:19 +0000268 // All of the incoming values are the same, return the value now.
269 return InVal;
270}
271
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000272
273//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000274// CallInst Implementation
275//===----------------------------------------------------------------------===//
276
Gordon Henriksen14a55692007-12-10 02:14:30 +0000277CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000278}
279
Chris Lattner054ba2c2007-02-13 00:58:44 +0000280void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000281 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
282 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000283 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284
Misha Brukmanb1c93172005-04-21 23:48:37 +0000285 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000287 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000288
Chris Lattner054ba2c2007-02-13 00:58:44 +0000289 assert((NumParams == FTy->getNumParams() ||
290 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000291 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000292 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000293 assert((i >= FTy->getNumParams() ||
294 FTy->getParamType(i) == Params[i]->getType()) &&
295 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000296 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000297 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298}
299
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000300void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000301 assert(NumOperands == 3 && "NumOperands not set up?");
302 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000303 OL[0] = Func;
304 OL[1] = Actual1;
305 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000306
307 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000308 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000309 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000310
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000311 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000312 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000314 assert((0 >= FTy->getNumParams() ||
315 FTy->getParamType(0) == Actual1->getType()) &&
316 "Calling a function with a bad signature!");
317 assert((1 >= FTy->getNumParams() ||
318 FTy->getParamType(1) == Actual2->getType()) &&
319 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320}
321
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000322void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000323 assert(NumOperands == 2 && "NumOperands not set up?");
324 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000325 OL[0] = Func;
326 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000327
328 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000330 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000332 assert((FTy->getNumParams() == 1 ||
333 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000334 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000335 assert((0 == FTy->getNumParams() ||
336 FTy->getParamType(0) == Actual->getType()) &&
337 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000338}
339
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000340void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000341 assert(NumOperands == 1 && "NumOperands not set up?");
342 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000343 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000344
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000345 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000346 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000347 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000348
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000349 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350}
351
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000352CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000353 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000354 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
355 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000356 Instruction::Call,
357 OperandTraits<CallInst>::op_end(this) - 2,
358 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000359 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000360 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000361}
362
363CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
364 BasicBlock *InsertAtEnd)
365 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
366 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000367 Instruction::Call,
368 OperandTraits<CallInst>::op_end(this) - 2,
369 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000370 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000371 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000372}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000373CallInst::CallInst(Value *Func, const std::string &Name,
374 Instruction *InsertBefore)
375 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
376 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000377 Instruction::Call,
378 OperandTraits<CallInst>::op_end(this) - 1,
379 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000380 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000381 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000382}
383
384CallInst::CallInst(Value *Func, const std::string &Name,
385 BasicBlock *InsertAtEnd)
386 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
387 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000388 Instruction::Call,
389 OperandTraits<CallInst>::op_end(this) - 1,
390 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000391 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000392 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000393}
394
Misha Brukmanb1c93172005-04-21 23:48:37 +0000395CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000396 : Instruction(CI.getType(), Instruction::Call,
397 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000398 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000399 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000400 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000401 Use *OL = OperandList;
402 Use *InOL = CI.OperandList;
403 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000404 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000405}
406
Devang Patel4c758ea2008-09-25 21:00:45 +0000407void CallInst::addAttribute(unsigned i, Attributes attr) {
408 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000409 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000410 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000411}
412
Devang Patel4c758ea2008-09-25 21:00:45 +0000413void CallInst::removeAttribute(unsigned i, Attributes attr) {
414 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000415 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000416 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000417}
418
Devang Patelba3fa6c2008-09-23 23:03:40 +0000419bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000420 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000421 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000422 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000423 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000424 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000425}
426
Duncan Sands5208d1a2007-11-28 17:07:01 +0000427
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000428//===----------------------------------------------------------------------===//
429// InvokeInst Implementation
430//===----------------------------------------------------------------------===//
431
432void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000433 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000434 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Bill Wendling4bb96e92009-03-13 21:15:59 +0000435 Use *OL = OperandList;
436 OL[0] = Fn;
437 OL[1] = IfNormal;
438 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000439 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000440 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000441 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000442
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000443 assert(((NumArgs == FTy->getNumParams()) ||
444 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000445 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000446
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000447 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000448 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000449 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000450 "Invoking a function with a bad signature!");
451
Bill Wendling4bb96e92009-03-13 21:15:59 +0000452 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000453 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000454}
455
Misha Brukmanb1c93172005-04-21 23:48:37 +0000456InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000457 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000458 OperandTraits<InvokeInst>::op_end(this)
459 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000460 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000461 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000462 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000463 Use *OL = OperandList, *InOL = II.OperandList;
464 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000465 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000466}
467
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000468BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
469 return getSuccessor(idx);
470}
471unsigned InvokeInst::getNumSuccessorsV() const {
472 return getNumSuccessors();
473}
474void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
475 return setSuccessor(idx, B);
476}
477
Devang Patelba3fa6c2008-09-23 23:03:40 +0000478bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000479 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000480 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000481 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000482 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000483 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000484}
485
Devang Patel4c758ea2008-09-25 21:00:45 +0000486void InvokeInst::addAttribute(unsigned i, Attributes attr) {
487 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000488 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000489 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000490}
491
Devang Patel4c758ea2008-09-25 21:00:45 +0000492void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
493 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000494 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000495 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000496}
497
Duncan Sands5208d1a2007-11-28 17:07:01 +0000498
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000499//===----------------------------------------------------------------------===//
500// ReturnInst Implementation
501//===----------------------------------------------------------------------===//
502
Chris Lattner2195fc42007-02-24 00:55:48 +0000503ReturnInst::ReturnInst(const ReturnInst &RI)
504 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000505 OperandTraits<ReturnInst>::op_end(this) -
506 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000507 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000508 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000509 Op<0>() = RI.Op<0>();
Chris Lattner2195fc42007-02-24 00:55:48 +0000510}
511
512ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000513 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000514 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
515 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000516 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000517 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000518}
519ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000520 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000521 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
522 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000523 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000524 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000525}
526ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000527 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000528 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000529}
530
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000531unsigned ReturnInst::getNumSuccessorsV() const {
532 return getNumSuccessors();
533}
534
Devang Patelae682fb2008-02-26 17:56:20 +0000535/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
536/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000537void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwin6dd27302009-07-08 18:01:40 +0000538 LLVM_UNREACHABLE("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000539}
540
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000541BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwin6dd27302009-07-08 18:01:40 +0000542 LLVM_UNREACHABLE("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000543 return 0;
544}
545
Devang Patel59643e52008-02-23 00:35:18 +0000546ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000547}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000548
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000549//===----------------------------------------------------------------------===//
550// UnwindInst Implementation
551//===----------------------------------------------------------------------===//
552
Chris Lattner2195fc42007-02-24 00:55:48 +0000553UnwindInst::UnwindInst(Instruction *InsertBefore)
554 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
555}
556UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
557 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
558}
559
560
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000561unsigned UnwindInst::getNumSuccessorsV() const {
562 return getNumSuccessors();
563}
564
565void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwin6dd27302009-07-08 18:01:40 +0000566 LLVM_UNREACHABLE("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000567}
568
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000569BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwin6dd27302009-07-08 18:01:40 +0000570 LLVM_UNREACHABLE("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000571 return 0;
572}
573
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000574//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000575// UnreachableInst Implementation
576//===----------------------------------------------------------------------===//
577
Chris Lattner2195fc42007-02-24 00:55:48 +0000578UnreachableInst::UnreachableInst(Instruction *InsertBefore)
579 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
580}
581UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
582 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
583}
584
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000585unsigned UnreachableInst::getNumSuccessorsV() const {
586 return getNumSuccessors();
587}
588
589void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwin6dd27302009-07-08 18:01:40 +0000590 LLVM_UNREACHABLE("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000591}
592
593BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwin6dd27302009-07-08 18:01:40 +0000594 LLVM_UNREACHABLE("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000595 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000596}
597
598//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000599// BranchInst Implementation
600//===----------------------------------------------------------------------===//
601
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000602void BranchInst::AssertOK() {
603 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000604 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000605 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000606}
607
Chris Lattner2195fc42007-02-24 00:55:48 +0000608BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000609 : TerminatorInst(Type::VoidTy, Instruction::Br,
610 OperandTraits<BranchInst>::op_end(this) - 1,
611 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000612 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000613 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000614}
615BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
616 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000617 : TerminatorInst(Type::VoidTy, Instruction::Br,
618 OperandTraits<BranchInst>::op_end(this) - 3,
619 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000620 Op<-1>() = IfTrue;
621 Op<-2>() = IfFalse;
622 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000623#ifndef NDEBUG
624 AssertOK();
625#endif
626}
627
628BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000629 : TerminatorInst(Type::VoidTy, Instruction::Br,
630 OperandTraits<BranchInst>::op_end(this) - 1,
631 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000632 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000633 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000634}
635
636BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
637 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000638 : TerminatorInst(Type::VoidTy, Instruction::Br,
639 OperandTraits<BranchInst>::op_end(this) - 3,
640 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000641 Op<-1>() = IfTrue;
642 Op<-2>() = IfFalse;
643 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000644#ifndef NDEBUG
645 AssertOK();
646#endif
647}
648
649
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000650BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000651 TerminatorInst(Type::VoidTy, Instruction::Br,
652 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
653 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000654 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655 if (BI.getNumOperands() != 1) {
656 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000657 Op<-3>() = BI.Op<-3>();
658 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000659 }
660}
661
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000662
663Use* Use::getPrefix() {
664 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
665 if (PotentialPrefix.getOpaqueValue())
666 return 0;
667
668 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
669}
670
671BranchInst::~BranchInst() {
672 if (NumOperands == 1) {
673 if (Use *Prefix = OperandList->getPrefix()) {
674 Op<-1>() = 0;
675 //
676 // mark OperandList to have a special value for scrutiny
677 // by baseclass destructors and operator delete
678 OperandList = Prefix;
679 } else {
680 NumOperands = 3;
681 OperandList = op_begin();
682 }
683 }
684}
685
686
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
688 return getSuccessor(idx);
689}
690unsigned BranchInst::getNumSuccessorsV() const {
691 return getNumSuccessors();
692}
693void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
694 setSuccessor(idx, B);
695}
696
697
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000698//===----------------------------------------------------------------------===//
699// AllocationInst Implementation
700//===----------------------------------------------------------------------===//
701
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000702static Value *getAISize(Value *Amt) {
703 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000704 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000705 else {
706 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000707 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000708 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000709 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000710 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000711 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000712}
713
Misha Brukmanb1c93172005-04-21 23:48:37 +0000714AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000715 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000716 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000717 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000718 InsertBefore) {
719 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000720 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000721 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000722}
723
Misha Brukmanb1c93172005-04-21 23:48:37 +0000724AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000725 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000726 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000727 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Dan Gohmanaa583d72008-03-24 16:55:58 +0000728 InsertAtEnd) {
729 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000730 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000731 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732}
733
Gordon Henriksen14a55692007-12-10 02:14:30 +0000734// Out of line virtual method, so the vtable, etc has a home.
735AllocationInst::~AllocationInst() {
736}
737
Dan Gohmanaa583d72008-03-24 16:55:58 +0000738void AllocationInst::setAlignment(unsigned Align) {
739 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
740 SubclassData = Log2_32(Align) + 1;
741 assert(getAlignment() == Align && "Alignment representation error!");
742}
743
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000744bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000745 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
746 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000747 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000748}
749
750const Type *AllocationInst::getAllocatedType() const {
751 return getType()->getElementType();
752}
753
754AllocaInst::AllocaInst(const AllocaInst &AI)
755 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000756 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000757}
758
Chris Lattner8b291e62008-11-26 02:54:17 +0000759/// isStaticAlloca - Return true if this alloca is in the entry block of the
760/// function and is a constant size. If so, the code generator will fold it
761/// into the prolog/epilog code, so it is basically free.
762bool AllocaInst::isStaticAlloca() const {
763 // Must be constant size.
764 if (!isa<ConstantInt>(getArraySize())) return false;
765
766 // Must be in the entry block.
767 const BasicBlock *Parent = getParent();
768 return Parent == &Parent->getParent()->front();
769}
770
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000771MallocInst::MallocInst(const MallocInst &MI)
772 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000773 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000774}
775
776//===----------------------------------------------------------------------===//
777// FreeInst Implementation
778//===----------------------------------------------------------------------===//
779
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000780void FreeInst::AssertOK() {
781 assert(isa<PointerType>(getOperand(0)->getType()) &&
782 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000783}
784
785FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000786 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000787 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000788}
789
790FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000791 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000792 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000793}
794
795
796//===----------------------------------------------------------------------===//
797// LoadInst Implementation
798//===----------------------------------------------------------------------===//
799
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000800void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000801 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000802 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803}
804
805LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000806 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000807 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000808 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000809 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000810 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000811 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000812}
813
814LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000815 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000816 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000817 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000818 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000819 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000820 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000821}
822
823LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
824 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000825 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000826 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000827 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000828 setAlignment(0);
829 AssertOK();
830 setName(Name);
831}
832
833LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
834 unsigned Align, Instruction *InsertBef)
835 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
836 Load, Ptr, InsertBef) {
837 setVolatile(isVolatile);
838 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000839 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000840 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000841}
842
Dan Gohman68659282007-07-18 20:51:11 +0000843LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
844 unsigned Align, BasicBlock *InsertAE)
845 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
846 Load, Ptr, InsertAE) {
847 setVolatile(isVolatile);
848 setAlignment(Align);
849 AssertOK();
850 setName(Name);
851}
852
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000853LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
854 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000855 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000856 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000857 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000858 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000859 AssertOK();
860 setName(Name);
861}
862
863
864
865LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000866 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
867 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000868 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000869 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000870 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000871 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000872}
873
874LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000875 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
876 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000877 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000878 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000879 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000880 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000881}
882
883LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
884 Instruction *InsertBef)
885: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000886 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000887 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000888 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000889 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000890 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000891}
892
893LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
894 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000895 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
896 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000897 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000898 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000899 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000900 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000901}
902
Christopher Lamb84485702007-04-22 19:24:39 +0000903void LoadInst::setAlignment(unsigned Align) {
904 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
905 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
906}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000907
908//===----------------------------------------------------------------------===//
909// StoreInst Implementation
910//===----------------------------------------------------------------------===//
911
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000912void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000913 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000914 assert(isa<PointerType>(getOperand(1)->getType()) &&
915 "Ptr must have pointer type!");
916 assert(getOperand(0)->getType() ==
917 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000918 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000919}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000920
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000921
922StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000923 : Instruction(Type::VoidTy, Store,
924 OperandTraits<StoreInst>::op_begin(this),
925 OperandTraits<StoreInst>::operands(this),
926 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000927 Op<0>() = val;
928 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000929 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000930 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000931 AssertOK();
932}
933
934StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000935 : Instruction(Type::VoidTy, Store,
936 OperandTraits<StoreInst>::op_begin(this),
937 OperandTraits<StoreInst>::operands(this),
938 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000939 Op<0>() = val;
940 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000941 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000942 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000943 AssertOK();
944}
945
Misha Brukmanb1c93172005-04-21 23:48:37 +0000946StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000947 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000948 : Instruction(Type::VoidTy, Store,
949 OperandTraits<StoreInst>::op_begin(this),
950 OperandTraits<StoreInst>::operands(this),
951 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000952 Op<0>() = val;
953 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000954 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000955 setAlignment(0);
956 AssertOK();
957}
958
959StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
960 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000961 : Instruction(Type::VoidTy, Store,
962 OperandTraits<StoreInst>::op_begin(this),
963 OperandTraits<StoreInst>::operands(this),
964 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000965 Op<0>() = val;
966 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000967 setVolatile(isVolatile);
968 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000969 AssertOK();
970}
971
Misha Brukmanb1c93172005-04-21 23:48:37 +0000972StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000973 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000974 : Instruction(Type::VoidTy, Store,
975 OperandTraits<StoreInst>::op_begin(this),
976 OperandTraits<StoreInst>::operands(this),
977 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000978 Op<0>() = val;
979 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000980 setVolatile(isVolatile);
981 setAlignment(Align);
982 AssertOK();
983}
984
985StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000986 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000987 : Instruction(Type::VoidTy, Store,
988 OperandTraits<StoreInst>::op_begin(this),
989 OperandTraits<StoreInst>::operands(this),
990 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000991 Op<0>() = val;
992 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000993 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000994 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000995 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000996}
997
Christopher Lamb84485702007-04-22 19:24:39 +0000998void StoreInst::setAlignment(unsigned Align) {
999 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1000 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1001}
1002
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001003//===----------------------------------------------------------------------===//
1004// GetElementPtrInst Implementation
1005//===----------------------------------------------------------------------===//
1006
Christopher Lambedf07882007-12-17 01:12:55 +00001007static unsigned retrieveAddrSpace(const Value *Val) {
1008 return cast<PointerType>(Val->getType())->getAddressSpace();
1009}
1010
Gabor Greif21ba1842008-06-06 20:28:12 +00001011void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Nate Begeman8a4afd92008-07-25 17:24:13 +00001012 const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001013 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1014 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001015 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001016
Chris Lattner79807c3d2007-01-31 19:47:18 +00001017 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001018 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001019
1020 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001021}
1022
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001023void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001024 assert(NumOperands == 2 && "NumOperands not initialized?");
1025 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001026 OL[0] = Ptr;
1027 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001028
1029 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001030}
1031
Gabor Greiff6caff662008-05-10 08:32:32 +00001032GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001033 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001034 OperandTraits<GetElementPtrInst>::op_end(this)
1035 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001036 GEPI.getNumOperands()) {
1037 Use *OL = OperandList;
1038 Use *GEPIOL = GEPI.OperandList;
1039 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001040 OL[i] = GEPIOL[i];
Gabor Greiff6caff662008-05-10 08:32:32 +00001041}
1042
Chris Lattner82981202005-05-03 05:43:30 +00001043GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1044 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001045 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001046 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001047 GetElementPtr,
1048 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1049 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001050 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001051}
1052
1053GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1054 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001055 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001056 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001057 GetElementPtr,
1058 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1059 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001060 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001061}
1062
Chris Lattner6090a422009-03-09 04:46:40 +00001063/// getIndexedType - Returns the type of the element that would be accessed with
1064/// a gep instruction with the specified parameters.
1065///
1066/// The Idxs pointer should point to a continuous piece of memory containing the
1067/// indices, either as Value* or uint64_t.
1068///
1069/// A null type is returned if the indices are invalid for the specified
1070/// pointer type.
1071///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001072template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001073static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1074 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001075 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1076 if (!PTy) return 0; // Type isn't a pointer type!
1077 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001078
Chris Lattner6090a422009-03-09 04:46:40 +00001079 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001080 if (NumIdx == 0)
1081 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001082
1083 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001084 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1085 // that contain opaque types) under the assumption that it will be resolved to
1086 // a sane type later.
1087 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001088 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001089
Dan Gohman1ecaf452008-05-31 00:58:22 +00001090 unsigned CurIdx = 1;
1091 for (; CurIdx != NumIdx; ++CurIdx) {
1092 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1093 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001094 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001095 if (!CT->indexValid(Index)) return 0;
1096 Agg = CT->getTypeAtIndex(Index);
1097
1098 // If the new type forwards to another type, then it is in the middle
1099 // of being refined to another type (and hence, may have dropped all
1100 // references to what it was using before). So, use the new forwarded
1101 // type.
1102 if (const Type *Ty = Agg->getForwardedType())
1103 Agg = Ty;
1104 }
1105 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001106}
1107
Matthijs Kooijman04468622008-07-29 08:46:11 +00001108const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1109 Value* const *Idxs,
1110 unsigned NumIdx) {
1111 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1112}
1113
1114const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1115 uint64_t const *Idxs,
1116 unsigned NumIdx) {
1117 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1118}
1119
Chris Lattner82981202005-05-03 05:43:30 +00001120const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1121 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1122 if (!PTy) return 0; // Type isn't a pointer type!
1123
1124 // Check the pointer index.
1125 if (!PTy->indexValid(Idx)) return 0;
1126
Chris Lattnerc2233332005-05-03 16:44:45 +00001127 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001128}
1129
Chris Lattner45f15572007-04-14 00:12:57 +00001130
1131/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1132/// zeros. If so, the result pointer and the first operand have the same
1133/// value, just potentially different types.
1134bool GetElementPtrInst::hasAllZeroIndices() const {
1135 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1136 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1137 if (!CI->isZero()) return false;
1138 } else {
1139 return false;
1140 }
1141 }
1142 return true;
1143}
1144
Chris Lattner27058292007-04-27 20:35:56 +00001145/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1146/// constant integers. If so, the result pointer and the first operand have
1147/// a constant offset between them.
1148bool GetElementPtrInst::hasAllConstantIndices() const {
1149 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1150 if (!isa<ConstantInt>(getOperand(i)))
1151 return false;
1152 }
1153 return true;
1154}
1155
Chris Lattner45f15572007-04-14 00:12:57 +00001156
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001157//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001158// ExtractElementInst Implementation
1159//===----------------------------------------------------------------------===//
1160
1161ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001162 const std::string &Name,
1163 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001164 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001165 ExtractElement,
1166 OperandTraits<ExtractElementInst>::op_begin(this),
1167 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001168 assert(isValidOperands(Val, Index) &&
1169 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001170 Op<0>() = Val;
1171 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001172 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001173}
1174
Chris Lattner65511ff2006-10-05 06:24:58 +00001175ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1176 const std::string &Name,
1177 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001178 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001179 ExtractElement,
1180 OperandTraits<ExtractElementInst>::op_begin(this),
1181 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001182 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001183 assert(isValidOperands(Val, Index) &&
1184 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001185 Op<0>() = Val;
1186 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001187 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001188}
1189
1190
Robert Bocchino23004482006-01-10 19:05:34 +00001191ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001192 const std::string &Name,
1193 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001194 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001195 ExtractElement,
1196 OperandTraits<ExtractElementInst>::op_begin(this),
1197 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001198 assert(isValidOperands(Val, Index) &&
1199 "Invalid extractelement instruction operands!");
1200
Gabor Greif2d3024d2008-05-26 21:33:52 +00001201 Op<0>() = Val;
1202 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001203 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001204}
1205
Chris Lattner65511ff2006-10-05 06:24:58 +00001206ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1207 const std::string &Name,
1208 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001209 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001210 ExtractElement,
1211 OperandTraits<ExtractElementInst>::op_begin(this),
1212 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001213 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001214 assert(isValidOperands(Val, Index) &&
1215 "Invalid extractelement instruction operands!");
1216
Gabor Greif2d3024d2008-05-26 21:33:52 +00001217 Op<0>() = Val;
1218 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001219 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001220}
1221
1222
Chris Lattner54865b32006-04-08 04:05:48 +00001223bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001224 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001225 return false;
1226 return true;
1227}
1228
1229
Robert Bocchino23004482006-01-10 19:05:34 +00001230//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001231// InsertElementInst Implementation
1232//===----------------------------------------------------------------------===//
1233
Chris Lattner0875d942006-04-14 22:20:32 +00001234InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001235 : Instruction(IE.getType(), InsertElement,
1236 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001237 Op<0>() = IE.Op<0>();
1238 Op<1>() = IE.Op<1>();
1239 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001240}
Chris Lattner54865b32006-04-08 04:05:48 +00001241InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001242 const std::string &Name,
1243 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001244 : Instruction(Vec->getType(), InsertElement,
1245 OperandTraits<InsertElementInst>::op_begin(this),
1246 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001247 assert(isValidOperands(Vec, Elt, Index) &&
1248 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001249 Op<0>() = Vec;
1250 Op<1>() = Elt;
1251 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001252 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001253}
1254
Chris Lattner65511ff2006-10-05 06:24:58 +00001255InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1256 const std::string &Name,
1257 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001258 : Instruction(Vec->getType(), InsertElement,
1259 OperandTraits<InsertElementInst>::op_begin(this),
1260 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001261 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001262 assert(isValidOperands(Vec, Elt, Index) &&
1263 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001264 Op<0>() = Vec;
1265 Op<1>() = Elt;
1266 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001267 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001268}
1269
1270
Chris Lattner54865b32006-04-08 04:05:48 +00001271InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001272 const std::string &Name,
1273 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001274 : Instruction(Vec->getType(), InsertElement,
1275 OperandTraits<InsertElementInst>::op_begin(this),
1276 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001277 assert(isValidOperands(Vec, Elt, Index) &&
1278 "Invalid insertelement instruction operands!");
1279
Gabor Greif2d3024d2008-05-26 21:33:52 +00001280 Op<0>() = Vec;
1281 Op<1>() = Elt;
1282 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001283 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001284}
1285
Chris Lattner65511ff2006-10-05 06:24:58 +00001286InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1287 const std::string &Name,
1288 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001289: Instruction(Vec->getType(), InsertElement,
1290 OperandTraits<InsertElementInst>::op_begin(this),
1291 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001292 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001293 assert(isValidOperands(Vec, Elt, Index) &&
1294 "Invalid insertelement instruction operands!");
1295
Gabor Greif2d3024d2008-05-26 21:33:52 +00001296 Op<0>() = Vec;
1297 Op<1>() = Elt;
1298 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001299 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001300}
1301
Chris Lattner54865b32006-04-08 04:05:48 +00001302bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1303 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001304 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001305 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001306
Reid Spencerd84d35b2007-02-15 02:26:10 +00001307 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001308 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001309
Reid Spencer8d9336d2006-12-31 05:26:44 +00001310 if (Index->getType() != Type::Int32Ty)
Dan Gohman4fe64de2009-06-14 23:30:43 +00001311 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001312 return true;
1313}
1314
1315
Robert Bocchinoca27f032006-01-17 20:07:22 +00001316//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001317// ShuffleVectorInst Implementation
1318//===----------------------------------------------------------------------===//
1319
Chris Lattner0875d942006-04-14 22:20:32 +00001320ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001321 : Instruction(SV.getType(), ShuffleVector,
1322 OperandTraits<ShuffleVectorInst>::op_begin(this),
1323 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001324 Op<0>() = SV.Op<0>();
1325 Op<1>() = SV.Op<1>();
1326 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001327}
1328
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001329ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1330 const std::string &Name,
1331 Instruction *InsertBefore)
Mon P Wang25f01062008-11-10 04:46:22 +00001332: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1333 cast<VectorType>(Mask->getType())->getNumElements()),
1334 ShuffleVector,
1335 OperandTraits<ShuffleVectorInst>::op_begin(this),
1336 OperandTraits<ShuffleVectorInst>::operands(this),
1337 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001338 assert(isValidOperands(V1, V2, Mask) &&
1339 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001340 Op<0>() = V1;
1341 Op<1>() = V2;
1342 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001343 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001344}
1345
1346ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Mon P Wang25f01062008-11-10 04:46:22 +00001347 const std::string &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001348 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001349 : Instruction(V1->getType(), ShuffleVector,
1350 OperandTraits<ShuffleVectorInst>::op_begin(this),
1351 OperandTraits<ShuffleVectorInst>::operands(this),
1352 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001353 assert(isValidOperands(V1, V2, Mask) &&
1354 "Invalid shuffle vector instruction operands!");
1355
Gabor Greif2d3024d2008-05-26 21:33:52 +00001356 Op<0>() = V1;
1357 Op<1>() = V2;
1358 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001359 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001360}
1361
Mon P Wang25f01062008-11-10 04:46:22 +00001362bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001363 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001364 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001365 return false;
1366
1367 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1368 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Mon P Wang25f01062008-11-10 04:46:22 +00001369 MaskTy->getElementType() != Type::Int32Ty)
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001370 return false;
1371 return true;
1372}
1373
Chris Lattnerf724e342008-03-02 05:28:33 +00001374/// getMaskValue - Return the index from the shuffle mask for the specified
1375/// output result. This is either -1 if the element is undef or a number less
1376/// than 2*numelements.
1377int ShuffleVectorInst::getMaskValue(unsigned i) const {
1378 const Constant *Mask = cast<Constant>(getOperand(2));
1379 if (isa<UndefValue>(Mask)) return -1;
1380 if (isa<ConstantAggregateZero>(Mask)) return 0;
1381 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1382 assert(i < MaskCV->getNumOperands() && "Index out of range");
1383
1384 if (isa<UndefValue>(MaskCV->getOperand(i)))
1385 return -1;
1386 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1387}
1388
Dan Gohman12fce772008-05-15 19:50:34 +00001389//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001390// InsertValueInst Class
1391//===----------------------------------------------------------------------===//
1392
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001393void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
1394 unsigned NumIdx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001395 assert(NumOperands == 2 && "NumOperands not initialized?");
1396 Op<0>() = Agg;
1397 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001398
Dan Gohman1ecaf452008-05-31 00:58:22 +00001399 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001400 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001401}
1402
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001403void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
1404 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001405 assert(NumOperands == 2 && "NumOperands not initialized?");
1406 Op<0>() = Agg;
1407 Op<1>() = Val;
1408
1409 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001410 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001411}
1412
1413InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001414 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001415 OperandTraits<InsertValueInst>::op_begin(this), 2),
1416 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001417 Op<0>() = IVI.getOperand(0);
1418 Op<1>() = IVI.getOperand(1);
Dan Gohman0752bff2008-05-23 00:36:11 +00001419}
1420
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001421InsertValueInst::InsertValueInst(Value *Agg,
1422 Value *Val,
1423 unsigned Idx,
1424 const std::string &Name,
1425 Instruction *InsertBefore)
1426 : Instruction(Agg->getType(), InsertValue,
1427 OperandTraits<InsertValueInst>::op_begin(this),
1428 2, InsertBefore) {
1429 init(Agg, Val, Idx, Name);
1430}
1431
1432InsertValueInst::InsertValueInst(Value *Agg,
1433 Value *Val,
1434 unsigned Idx,
1435 const std::string &Name,
1436 BasicBlock *InsertAtEnd)
1437 : Instruction(Agg->getType(), InsertValue,
1438 OperandTraits<InsertValueInst>::op_begin(this),
1439 2, InsertAtEnd) {
1440 init(Agg, Val, Idx, Name);
1441}
1442
Dan Gohman0752bff2008-05-23 00:36:11 +00001443//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001444// ExtractValueInst Class
1445//===----------------------------------------------------------------------===//
1446
Gabor Greifcbcc4952008-06-06 21:06:32 +00001447void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Gabor Greif1b9921a2009-01-11 22:33:22 +00001448 const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001449 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001450
Dan Gohman1ecaf452008-05-31 00:58:22 +00001451 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001452 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001453}
1454
Gabor Greifcbcc4952008-06-06 21:06:32 +00001455void ExtractValueInst::init(unsigned Idx, const std::string &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001456 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001457
1458 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001459 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001460}
1461
1462ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001463 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001464 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001465}
1466
Dan Gohman12fce772008-05-15 19:50:34 +00001467// getIndexedType - Returns the type of the element that would be extracted
1468// with an extractvalue instruction with the specified parameters.
1469//
1470// A null type is returned if the indices are invalid for the specified
1471// pointer type.
1472//
1473const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001474 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001475 unsigned NumIdx) {
1476 unsigned CurIdx = 0;
1477 for (; CurIdx != NumIdx; ++CurIdx) {
1478 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001479 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1480 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001481 if (!CT->indexValid(Index)) return 0;
1482 Agg = CT->getTypeAtIndex(Index);
1483
1484 // If the new type forwards to another type, then it is in the middle
1485 // of being refined to another type (and hence, may have dropped all
1486 // references to what it was using before). So, use the new forwarded
1487 // type.
1488 if (const Type *Ty = Agg->getForwardedType())
1489 Agg = Ty;
1490 }
1491 return CurIdx == NumIdx ? Agg : 0;
1492}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001493
Dan Gohman4a3125b2008-06-17 21:07:55 +00001494const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001495 unsigned Idx) {
1496 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001497}
1498
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001499//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001500// BinaryOperator Class
1501//===----------------------------------------------------------------------===//
1502
Dan Gohmana5b96452009-06-04 22:49:04 +00001503/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1504/// type is floating-point, to help provide compatibility with an older API.
1505///
1506static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1507 const Type *Ty) {
1508 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1509 if (Ty->isFPOrFPVector()) {
1510 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1511 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1512 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1513 }
1514 return iType;
1515}
1516
Chris Lattner2195fc42007-02-24 00:55:48 +00001517BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1518 const Type *Ty, const std::string &Name,
1519 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001520 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001521 OperandTraits<BinaryOperator>::op_begin(this),
1522 OperandTraits<BinaryOperator>::operands(this),
1523 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001524 Op<0>() = S1;
1525 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001526 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001527 setName(Name);
1528}
1529
1530BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1531 const Type *Ty, const std::string &Name,
1532 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001533 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001534 OperandTraits<BinaryOperator>::op_begin(this),
1535 OperandTraits<BinaryOperator>::operands(this),
1536 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001537 Op<0>() = S1;
1538 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001539 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001540 setName(Name);
1541}
1542
1543
1544void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001545 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001546 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001547 assert(LHS->getType() == RHS->getType() &&
1548 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001549#ifndef NDEBUG
1550 switch (iType) {
1551 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001552 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001553 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001554 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001555 assert(getType()->isIntOrIntVector() &&
1556 "Tried to create an integer operation on a non-integer type!");
1557 break;
1558 case FAdd: case FSub:
1559 case FMul:
1560 assert(getType() == LHS->getType() &&
1561 "Arithmetic operation should return same type as operands!");
1562 assert(getType()->isFPOrFPVector() &&
1563 "Tried to create a floating-point operation on a "
1564 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001565 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001566 case UDiv:
1567 case SDiv:
1568 assert(getType() == LHS->getType() &&
1569 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001570 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1571 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001572 "Incorrect operand type (not integer) for S/UDIV");
1573 break;
1574 case FDiv:
1575 assert(getType() == LHS->getType() &&
1576 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001577 assert(getType()->isFPOrFPVector() &&
1578 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001579 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001580 case URem:
1581 case SRem:
1582 assert(getType() == LHS->getType() &&
1583 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001584 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1585 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001586 "Incorrect operand type (not integer) for S/UREM");
1587 break;
1588 case FRem:
1589 assert(getType() == LHS->getType() &&
1590 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001591 assert(getType()->isFPOrFPVector() &&
1592 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001593 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001594 case Shl:
1595 case LShr:
1596 case AShr:
1597 assert(getType() == LHS->getType() &&
1598 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001599 assert((getType()->isInteger() ||
1600 (isa<VectorType>(getType()) &&
1601 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1602 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001603 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001604 case And: case Or:
1605 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001606 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001607 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001608 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001609 (isa<VectorType>(getType()) &&
1610 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001611 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001612 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001613 default:
1614 break;
1615 }
1616#endif
1617}
1618
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001619BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001620 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001621 Instruction *InsertBefore) {
1622 assert(S1->getType() == S2->getType() &&
1623 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001624 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001625}
1626
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001627BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001628 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001629 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001630 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001631 InsertAtEnd->getInstList().push_back(Res);
1632 return Res;
1633}
1634
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001635BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001636 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001637 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1638 return new BinaryOperator(Instruction::Sub,
1639 zero, Op,
1640 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001641}
1642
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001643BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001644 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001645 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1646 return new BinaryOperator(Instruction::Sub,
1647 zero, Op,
1648 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001649}
1650
Dan Gohmana5b96452009-06-04 22:49:04 +00001651BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const std::string &Name,
1652 Instruction *InsertBefore) {
1653 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1654 return new BinaryOperator(Instruction::FSub,
1655 zero, Op,
1656 Op->getType(), Name, InsertBefore);
1657}
1658
1659BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const std::string &Name,
1660 BasicBlock *InsertAtEnd) {
1661 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1662 return new BinaryOperator(Instruction::FSub,
1663 zero, Op,
1664 Op->getType(), Name, InsertAtEnd);
1665}
1666
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001667BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001668 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001669 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001670 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001671 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001672 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001673 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001674 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001675 }
1676
1677 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001678 Op->getType(), Name, InsertBefore);
1679}
1680
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001681BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001682 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001683 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001684 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001685 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001686 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001687 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001688 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001689 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001690 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001691 }
1692
1693 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001694 Op->getType(), Name, InsertAtEnd);
1695}
1696
1697
1698// isConstantAllOnes - Helper function for several functions below
1699static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001700 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1701 return CI->isAllOnesValue();
1702 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1703 return CV->isAllOnesValue();
1704 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001705}
1706
1707bool BinaryOperator::isNeg(const Value *V) {
1708 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1709 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001710 return Bop->getOperand(0) ==
1711 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001712 return false;
1713}
1714
Dan Gohmana5b96452009-06-04 22:49:04 +00001715bool BinaryOperator::isFNeg(const Value *V) {
1716 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1717 if (Bop->getOpcode() == Instruction::FSub)
1718 return Bop->getOperand(0) ==
1719 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
1720 return false;
1721}
1722
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001723bool BinaryOperator::isNot(const Value *V) {
1724 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1725 return (Bop->getOpcode() == Instruction::Xor &&
1726 (isConstantAllOnes(Bop->getOperand(1)) ||
1727 isConstantAllOnes(Bop->getOperand(0))));
1728 return false;
1729}
1730
Chris Lattner2c7d1772005-04-24 07:28:37 +00001731Value *BinaryOperator::getNegArgument(Value *BinOp) {
1732 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1733 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001734}
1735
Chris Lattner2c7d1772005-04-24 07:28:37 +00001736const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1737 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001738}
1739
Dan Gohmana5b96452009-06-04 22:49:04 +00001740Value *BinaryOperator::getFNegArgument(Value *BinOp) {
1741 assert(isFNeg(BinOp) && "getFNegArgument from non-'fneg' instruction!");
1742 return cast<BinaryOperator>(BinOp)->getOperand(1);
1743}
1744
1745const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1746 return getFNegArgument(const_cast<Value*>(BinOp));
1747}
1748
Chris Lattner2c7d1772005-04-24 07:28:37 +00001749Value *BinaryOperator::getNotArgument(Value *BinOp) {
1750 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1751 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1752 Value *Op0 = BO->getOperand(0);
1753 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001754 if (isConstantAllOnes(Op0)) return Op1;
1755
1756 assert(isConstantAllOnes(Op1));
1757 return Op0;
1758}
1759
Chris Lattner2c7d1772005-04-24 07:28:37 +00001760const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1761 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001762}
1763
1764
1765// swapOperands - Exchange the two operands to this instruction. This
1766// instruction is safe to use on any binary instruction and does not
1767// modify the semantics of the instruction. If the instruction is
1768// order dependent (SetLT f.e.) the opcode is changed.
1769//
1770bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001771 if (!isCommutative())
1772 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001773 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001774 return false;
1775}
1776
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001777//===----------------------------------------------------------------------===//
1778// CastInst Class
1779//===----------------------------------------------------------------------===//
1780
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001781// Just determine if this cast only deals with integral->integral conversion.
1782bool CastInst::isIntegerCast() const {
1783 switch (getOpcode()) {
1784 default: return false;
1785 case Instruction::ZExt:
1786 case Instruction::SExt:
1787 case Instruction::Trunc:
1788 return true;
1789 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001790 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001791 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001792}
1793
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001794bool CastInst::isLosslessCast() const {
1795 // Only BitCast can be lossless, exit fast if we're not BitCast
1796 if (getOpcode() != Instruction::BitCast)
1797 return false;
1798
1799 // Identity cast is always lossless
1800 const Type* SrcTy = getOperand(0)->getType();
1801 const Type* DstTy = getType();
1802 if (SrcTy == DstTy)
1803 return true;
1804
Reid Spencer8d9336d2006-12-31 05:26:44 +00001805 // Pointer to pointer is always lossless.
1806 if (isa<PointerType>(SrcTy))
1807 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001808 return false; // Other types have no identity values
1809}
1810
1811/// This function determines if the CastInst does not require any bits to be
1812/// changed in order to effect the cast. Essentially, it identifies cases where
1813/// no code gen is necessary for the cast, hence the name no-op cast. For
1814/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001815/// # bitcast i32* %x to i8*
1816/// # bitcast <2 x i32> %x to <4 x i16>
1817/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001818/// @brief Determine if a cast is a no-op.
1819bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1820 switch (getOpcode()) {
1821 default:
1822 assert(!"Invalid CastOp");
1823 case Instruction::Trunc:
1824 case Instruction::ZExt:
1825 case Instruction::SExt:
1826 case Instruction::FPTrunc:
1827 case Instruction::FPExt:
1828 case Instruction::UIToFP:
1829 case Instruction::SIToFP:
1830 case Instruction::FPToUI:
1831 case Instruction::FPToSI:
1832 return false; // These always modify bits
1833 case Instruction::BitCast:
1834 return true; // BitCast never modifies bits.
1835 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001836 return IntPtrTy->getScalarSizeInBits() ==
1837 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001838 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001839 return IntPtrTy->getScalarSizeInBits() ==
1840 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001841 }
1842}
1843
1844/// This function determines if a pair of casts can be eliminated and what
1845/// opcode should be used in the elimination. This assumes that there are two
1846/// instructions like this:
1847/// * %F = firstOpcode SrcTy %x to MidTy
1848/// * %S = secondOpcode MidTy %F to DstTy
1849/// The function returns a resultOpcode so these two casts can be replaced with:
1850/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1851/// If no such cast is permited, the function returns 0.
1852unsigned CastInst::isEliminableCastPair(
1853 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1854 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1855{
1856 // Define the 144 possibilities for these two cast instructions. The values
1857 // in this matrix determine what to do in a given situation and select the
1858 // case in the switch below. The rows correspond to firstOp, the columns
1859 // correspond to secondOp. In looking at the table below, keep in mind
1860 // the following cast properties:
1861 //
1862 // Size Compare Source Destination
1863 // Operator Src ? Size Type Sign Type Sign
1864 // -------- ------------ ------------------- ---------------------
1865 // TRUNC > Integer Any Integral Any
1866 // ZEXT < Integral Unsigned Integer Any
1867 // SEXT < Integral Signed Integer Any
1868 // FPTOUI n/a FloatPt n/a Integral Unsigned
1869 // FPTOSI n/a FloatPt n/a Integral Signed
1870 // UITOFP n/a Integral Unsigned FloatPt n/a
1871 // SITOFP n/a Integral Signed FloatPt n/a
1872 // FPTRUNC > FloatPt n/a FloatPt n/a
1873 // FPEXT < FloatPt n/a FloatPt n/a
1874 // PTRTOINT n/a Pointer n/a Integral Unsigned
1875 // INTTOPTR n/a Integral Unsigned Pointer n/a
1876 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001877 //
1878 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001879 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1880 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001881 // of the produced value (we no longer know the top-part is all zeros).
1882 // Further this conversion is often much more expensive for typical hardware,
1883 // and causes issues when building libgcc. We disallow fptosi+sext for the
1884 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001885 const unsigned numCastOps =
1886 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1887 static const uint8_t CastResults[numCastOps][numCastOps] = {
1888 // T F F U S F F P I B -+
1889 // R Z S P P I I T P 2 N T |
1890 // U E E 2 2 2 2 R E I T C +- secondOp
1891 // N X X U S F F N X N 2 V |
1892 // C T T I I P P C T T P T -+
1893 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1894 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1895 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001896 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1897 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001898 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1899 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1900 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1901 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1902 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1903 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1904 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1905 };
1906
1907 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1908 [secondOp-Instruction::CastOpsBegin];
1909 switch (ElimCase) {
1910 case 0:
1911 // categorically disallowed
1912 return 0;
1913 case 1:
1914 // allowed, use first cast's opcode
1915 return firstOp;
1916 case 2:
1917 // allowed, use second cast's opcode
1918 return secondOp;
1919 case 3:
1920 // no-op cast in second op implies firstOp as long as the DestTy
1921 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001922 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001923 return firstOp;
1924 return 0;
1925 case 4:
1926 // no-op cast in second op implies firstOp as long as the DestTy
1927 // is floating point
1928 if (DstTy->isFloatingPoint())
1929 return firstOp;
1930 return 0;
1931 case 5:
1932 // no-op cast in first op implies secondOp as long as the SrcTy
1933 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001934 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001935 return secondOp;
1936 return 0;
1937 case 6:
1938 // no-op cast in first op implies secondOp as long as the SrcTy
1939 // is a floating point
1940 if (SrcTy->isFloatingPoint())
1941 return secondOp;
1942 return 0;
1943 case 7: {
1944 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001945 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1946 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001947 if (MidSize >= PtrSize)
1948 return Instruction::BitCast;
1949 return 0;
1950 }
1951 case 8: {
1952 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1953 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1954 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001955 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1956 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001957 if (SrcSize == DstSize)
1958 return Instruction::BitCast;
1959 else if (SrcSize < DstSize)
1960 return firstOp;
1961 return secondOp;
1962 }
1963 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1964 return Instruction::ZExt;
1965 case 10:
1966 // fpext followed by ftrunc is allowed if the bit size returned to is
1967 // the same as the original, in which case its just a bitcast
1968 if (SrcTy == DstTy)
1969 return Instruction::BitCast;
1970 return 0; // If the types are not the same we can't eliminate it.
1971 case 11:
1972 // bitcast followed by ptrtoint is allowed as long as the bitcast
1973 // is a pointer to pointer cast.
1974 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1975 return secondOp;
1976 return 0;
1977 case 12:
1978 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1979 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1980 return firstOp;
1981 return 0;
1982 case 13: {
1983 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001984 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1985 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1986 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001987 if (SrcSize <= PtrSize && SrcSize == DstSize)
1988 return Instruction::BitCast;
1989 return 0;
1990 }
1991 case 99:
1992 // cast combination can't happen (error in input). This is for all cases
1993 // where the MidTy is not the same for the two cast instructions.
1994 assert(!"Invalid Cast Combination");
1995 return 0;
1996 default:
1997 assert(!"Error in CastResults table!!!");
1998 return 0;
1999 }
2000 return 0;
2001}
2002
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002003CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002004 const std::string &Name, Instruction *InsertBefore) {
2005 // Construct and return the appropriate CastInst subclass
2006 switch (op) {
2007 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2008 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2009 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2010 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2011 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2012 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2013 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2014 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2015 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2016 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2017 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2018 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2019 default:
2020 assert(!"Invalid opcode provided");
2021 }
2022 return 0;
2023}
2024
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002025CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002026 const std::string &Name, BasicBlock *InsertAtEnd) {
2027 // Construct and return the appropriate CastInst subclass
2028 switch (op) {
2029 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2030 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2031 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2032 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2033 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2034 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2035 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2036 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2037 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2038 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2039 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2040 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2041 default:
2042 assert(!"Invalid opcode provided");
2043 }
2044 return 0;
2045}
2046
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002047CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002048 const std::string &Name,
2049 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002050 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002051 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2052 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002053}
2054
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002055CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002056 const std::string &Name,
2057 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002058 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002059 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2060 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002061}
2062
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002063CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002064 const std::string &Name,
2065 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002066 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002067 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2068 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002069}
2070
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002071CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002072 const std::string &Name,
2073 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002074 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002075 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2076 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002077}
2078
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002079CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002080 const std::string &Name,
2081 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002082 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002083 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2084 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002085}
2086
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002087CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Reid Spencer5c140882006-12-04 20:17:56 +00002088 const std::string &Name,
2089 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002090 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002091 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2092 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002093}
2094
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002095CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002096 const std::string &Name,
2097 BasicBlock *InsertAtEnd) {
2098 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002099 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002100 "Invalid cast");
2101
Chris Lattner03c49532007-01-15 02:27:26 +00002102 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002103 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2104 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002105}
2106
2107/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002108CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002109 const std::string &Name,
2110 Instruction *InsertBefore) {
2111 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002112 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002113 "Invalid cast");
2114
Chris Lattner03c49532007-01-15 02:27:26 +00002115 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002116 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2117 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002118}
2119
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002120CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002121 bool isSigned, const std::string &Name,
2122 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002123 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002124 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2125 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002126 Instruction::CastOps opcode =
2127 (SrcBits == DstBits ? Instruction::BitCast :
2128 (SrcBits > DstBits ? Instruction::Trunc :
2129 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002130 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002131}
2132
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002133CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002134 bool isSigned, const std::string &Name,
2135 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002136 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2137 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002138 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2139 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002140 Instruction::CastOps opcode =
2141 (SrcBits == DstBits ? Instruction::BitCast :
2142 (SrcBits > DstBits ? Instruction::Trunc :
2143 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002144 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002145}
2146
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002147CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002148 const std::string &Name,
2149 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002150 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002151 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002152 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2153 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002154 Instruction::CastOps opcode =
2155 (SrcBits == DstBits ? Instruction::BitCast :
2156 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002157 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002158}
2159
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002160CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Reid Spencer7e933472006-12-12 00:49:44 +00002161 const std::string &Name,
2162 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002163 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002164 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002165 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2166 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002167 Instruction::CastOps opcode =
2168 (SrcBits == DstBits ? Instruction::BitCast :
2169 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002170 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002171}
2172
Duncan Sands55e50902008-01-06 10:12:28 +00002173// Check whether it is valid to call getCastOpcode for these types.
2174// This routine must be kept in sync with getCastOpcode.
2175bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2176 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2177 return false;
2178
2179 if (SrcTy == DestTy)
2180 return true;
2181
2182 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002183 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2184 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002185
2186 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002187 if (DestTy->isInteger()) { // Casting to integral
2188 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002189 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002190 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002191 return true;
2192 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002193 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002194 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002195 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002196 return isa<PointerType>(SrcTy);
2197 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002198 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2199 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002200 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002201 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002202 return true;
2203 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002204 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002205 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002206 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002207 return false;
2208 }
2209 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002210 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002211 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002212 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002213 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002214 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002215 return DestPTy->getBitWidth() == SrcBits;
2216 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002217 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2218 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002219 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002220 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002221 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002222 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002223 return false;
2224 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002225 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002226 return false;
2227 }
2228}
2229
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002230// Provide a way to get a "cast" where the cast opcode is inferred from the
2231// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002232// logic in the castIsValid function below. This axiom should hold:
2233// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2234// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002235// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002236// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002237Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002238CastInst::getCastOpcode(
2239 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002240 // Get the bit sizes, we'll need these
2241 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002242 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2243 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002244
Duncan Sands55e50902008-01-06 10:12:28 +00002245 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2246 "Only first class types are castable!");
2247
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002248 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002249 if (DestTy->isInteger()) { // Casting to integral
2250 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002251 if (DestBits < SrcBits)
2252 return Trunc; // int -> smaller int
2253 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002254 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002255 return SExt; // signed -> SEXT
2256 else
2257 return ZExt; // unsigned -> ZEXT
2258 } else {
2259 return BitCast; // Same size, No-op cast
2260 }
2261 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002262 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002263 return FPToSI; // FP -> sint
2264 else
2265 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002266 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002267 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002268 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002269 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002270 return BitCast; // Same size, no-op cast
2271 } else {
2272 assert(isa<PointerType>(SrcTy) &&
2273 "Casting from a value that is not first-class type");
2274 return PtrToInt; // ptr -> int
2275 }
2276 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002277 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002278 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002279 return SIToFP; // sint -> FP
2280 else
2281 return UIToFP; // uint -> FP
2282 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2283 if (DestBits < SrcBits) {
2284 return FPTrunc; // FP -> smaller FP
2285 } else if (DestBits > SrcBits) {
2286 return FPExt; // FP -> larger FP
2287 } else {
2288 return BitCast; // same size, no-op cast
2289 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002290 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002291 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002292 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002293 PTy = NULL;
2294 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002295 } else {
Torok Edwin6dd27302009-07-08 18:01:40 +00002296 LLVM_UNREACHABLE("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002297 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002298 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2299 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002300 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002301 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002302 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002303 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002304 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002305 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002307 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002308 }
2309 } else if (isa<PointerType>(DestTy)) {
2310 if (isa<PointerType>(SrcTy)) {
2311 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002312 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002313 return IntToPtr; // int -> ptr
2314 } else {
2315 assert(!"Casting pointer to other than pointer or int");
2316 }
2317 } else {
2318 assert(!"Casting to type that is not first-class");
2319 }
2320
2321 // If we fall through to here we probably hit an assertion cast above
2322 // and assertions are not turned on. Anything we return is an error, so
2323 // BitCast is as good a choice as any.
2324 return BitCast;
2325}
2326
2327//===----------------------------------------------------------------------===//
2328// CastInst SubClass Constructors
2329//===----------------------------------------------------------------------===//
2330
2331/// Check that the construction parameters for a CastInst are correct. This
2332/// could be broken out into the separate constructors but it is useful to have
2333/// it in one place and to eliminate the redundant code for getting the sizes
2334/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002335bool
2336CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002337
2338 // Check for type sanity on the arguments
2339 const Type *SrcTy = S->getType();
2340 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2341 return false;
2342
2343 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002344 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2345 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002346
2347 // Switch on the opcode provided
2348 switch (op) {
2349 default: return false; // This is an input error
2350 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002351 return SrcTy->isIntOrIntVector() &&
2352 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002353 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002354 return SrcTy->isIntOrIntVector() &&
2355 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002356 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002357 return SrcTy->isIntOrIntVector() &&
2358 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002359 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002360 return SrcTy->isFPOrFPVector() &&
2361 DstTy->isFPOrFPVector() &&
2362 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002364 return SrcTy->isFPOrFPVector() &&
2365 DstTy->isFPOrFPVector() &&
2366 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002367 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002368 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002369 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2370 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002371 return SVTy->getElementType()->isIntOrIntVector() &&
2372 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002373 SVTy->getNumElements() == DVTy->getNumElements();
2374 }
2375 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002376 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002377 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002379 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2380 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002381 return SVTy->getElementType()->isFPOrFPVector() &&
2382 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002383 SVTy->getNumElements() == DVTy->getNumElements();
2384 }
2385 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002386 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002387 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002388 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002389 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002390 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391 case Instruction::BitCast:
2392 // BitCast implies a no-op cast of type only. No bits change.
2393 // However, you can't cast pointers to anything but pointers.
2394 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2395 return false;
2396
Duncan Sands55e50902008-01-06 10:12:28 +00002397 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398 // these cases, the cast is okay if the source and destination bit widths
2399 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002400 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002401 }
2402}
2403
2404TruncInst::TruncInst(
2405 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2406) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002407 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408}
2409
2410TruncInst::TruncInst(
2411 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2412) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002413 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002414}
2415
2416ZExtInst::ZExtInst(
2417 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2418) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002419 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420}
2421
2422ZExtInst::ZExtInst(
2423 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2424) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002425 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002426}
2427SExtInst::SExtInst(
2428 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2429) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002430 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002431}
2432
Jeff Cohencc08c832006-12-02 02:22:01 +00002433SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002434 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2435) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002436 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437}
2438
2439FPTruncInst::FPTruncInst(
2440 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2441) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002442 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002443}
2444
2445FPTruncInst::FPTruncInst(
2446 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2447) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002448 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002449}
2450
2451FPExtInst::FPExtInst(
2452 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2453) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002454 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002455}
2456
2457FPExtInst::FPExtInst(
2458 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2459) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002460 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002461}
2462
2463UIToFPInst::UIToFPInst(
2464 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2465) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002466 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002467}
2468
2469UIToFPInst::UIToFPInst(
2470 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2471) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002472 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002473}
2474
2475SIToFPInst::SIToFPInst(
2476 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2477) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002478 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002479}
2480
2481SIToFPInst::SIToFPInst(
2482 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2483) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002484 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002485}
2486
2487FPToUIInst::FPToUIInst(
2488 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2489) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002490 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002491}
2492
2493FPToUIInst::FPToUIInst(
2494 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2495) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002496 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002497}
2498
2499FPToSIInst::FPToSIInst(
2500 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2501) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002502 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002503}
2504
2505FPToSIInst::FPToSIInst(
2506 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2507) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002508 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002509}
2510
2511PtrToIntInst::PtrToIntInst(
2512 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2513) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002514 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002515}
2516
2517PtrToIntInst::PtrToIntInst(
2518 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2519) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002520 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002521}
2522
2523IntToPtrInst::IntToPtrInst(
2524 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2525) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002526 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002527}
2528
2529IntToPtrInst::IntToPtrInst(
2530 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2531) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002532 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002533}
2534
2535BitCastInst::BitCastInst(
2536 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2537) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002538 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539}
2540
2541BitCastInst::BitCastInst(
2542 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2543) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002544 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002546
2547//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002548// CmpInst Classes
2549//===----------------------------------------------------------------------===//
2550
Nate Begemand2195702008-05-12 19:01:56 +00002551CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2552 Value *LHS, Value *RHS, const std::string &Name,
2553 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002554 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002555 OperandTraits<CmpInst>::op_begin(this),
2556 OperandTraits<CmpInst>::operands(this),
2557 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002558 Op<0>() = LHS;
2559 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002560 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002561 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002562}
Gabor Greiff6caff662008-05-10 08:32:32 +00002563
Nate Begemand2195702008-05-12 19:01:56 +00002564CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
2565 Value *LHS, Value *RHS, const std::string &Name,
2566 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002567 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002568 OperandTraits<CmpInst>::op_begin(this),
2569 OperandTraits<CmpInst>::operands(this),
2570 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002571 Op<0>() = LHS;
2572 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002573 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002574 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002575}
2576
2577CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002578CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002579 const std::string &Name, Instruction *InsertBefore) {
2580 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002581 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002582 InsertBefore);
2583 }
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002584 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2585 InsertBefore);
Reid Spencerd9436b62006-11-20 01:22:35 +00002586}
2587
2588CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002589CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Reid Spencerd9436b62006-11-20 01:22:35 +00002590 const std::string &Name, BasicBlock *InsertAtEnd) {
2591 if (Op == Instruction::ICmp) {
Nate Begemand2195702008-05-12 19:01:56 +00002592 return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
Reid Spencerd9436b62006-11-20 01:22:35 +00002593 InsertAtEnd);
2594 }
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002595 return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name,
2596 InsertAtEnd);
Reid Spencerd9436b62006-11-20 01:22:35 +00002597}
2598
2599void CmpInst::swapOperands() {
2600 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2601 IC->swapOperands();
2602 else
2603 cast<FCmpInst>(this)->swapOperands();
2604}
2605
2606bool CmpInst::isCommutative() {
2607 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2608 return IC->isCommutative();
2609 return cast<FCmpInst>(this)->isCommutative();
2610}
2611
2612bool CmpInst::isEquality() {
2613 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2614 return IC->isEquality();
2615 return cast<FCmpInst>(this)->isEquality();
2616}
2617
2618
Dan Gohman4e724382008-05-31 02:47:54 +00002619CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002620 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002621 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002622 case ICMP_EQ: return ICMP_NE;
2623 case ICMP_NE: return ICMP_EQ;
2624 case ICMP_UGT: return ICMP_ULE;
2625 case ICMP_ULT: return ICMP_UGE;
2626 case ICMP_UGE: return ICMP_ULT;
2627 case ICMP_ULE: return ICMP_UGT;
2628 case ICMP_SGT: return ICMP_SLE;
2629 case ICMP_SLT: return ICMP_SGE;
2630 case ICMP_SGE: return ICMP_SLT;
2631 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002632
Dan Gohman4e724382008-05-31 02:47:54 +00002633 case FCMP_OEQ: return FCMP_UNE;
2634 case FCMP_ONE: return FCMP_UEQ;
2635 case FCMP_OGT: return FCMP_ULE;
2636 case FCMP_OLT: return FCMP_UGE;
2637 case FCMP_OGE: return FCMP_ULT;
2638 case FCMP_OLE: return FCMP_UGT;
2639 case FCMP_UEQ: return FCMP_ONE;
2640 case FCMP_UNE: return FCMP_OEQ;
2641 case FCMP_UGT: return FCMP_OLE;
2642 case FCMP_ULT: return FCMP_OGE;
2643 case FCMP_UGE: return FCMP_OLT;
2644 case FCMP_ULE: return FCMP_OGT;
2645 case FCMP_ORD: return FCMP_UNO;
2646 case FCMP_UNO: return FCMP_ORD;
2647 case FCMP_TRUE: return FCMP_FALSE;
2648 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002649 }
2650}
2651
Reid Spencer266e42b2006-12-23 06:05:41 +00002652ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2653 switch (pred) {
2654 default: assert(! "Unknown icmp predicate!");
2655 case ICMP_EQ: case ICMP_NE:
2656 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2657 return pred;
2658 case ICMP_UGT: return ICMP_SGT;
2659 case ICMP_ULT: return ICMP_SLT;
2660 case ICMP_UGE: return ICMP_SGE;
2661 case ICMP_ULE: return ICMP_SLE;
2662 }
2663}
2664
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002665ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2666 switch (pred) {
2667 default: assert(! "Unknown icmp predicate!");
2668 case ICMP_EQ: case ICMP_NE:
2669 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2670 return pred;
2671 case ICMP_SGT: return ICMP_UGT;
2672 case ICMP_SLT: return ICMP_ULT;
2673 case ICMP_SGE: return ICMP_UGE;
2674 case ICMP_SLE: return ICMP_ULE;
2675 }
2676}
2677
Reid Spencer266e42b2006-12-23 06:05:41 +00002678bool ICmpInst::isSignedPredicate(Predicate pred) {
2679 switch (pred) {
2680 default: assert(! "Unknown icmp predicate!");
2681 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2682 return true;
2683 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2684 case ICMP_UGE: case ICMP_ULE:
2685 return false;
2686 }
2687}
2688
Reid Spencer0286bc12007-02-28 22:00:54 +00002689/// Initialize a set of values that all satisfy the condition with C.
2690///
2691ConstantRange
2692ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2693 APInt Lower(C);
2694 APInt Upper(C);
2695 uint32_t BitWidth = C.getBitWidth();
2696 switch (pred) {
2697 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2698 case ICmpInst::ICMP_EQ: Upper++; break;
2699 case ICmpInst::ICMP_NE: Lower++; break;
2700 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2701 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2702 case ICmpInst::ICMP_UGT:
2703 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2704 break;
2705 case ICmpInst::ICMP_SGT:
2706 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2707 break;
2708 case ICmpInst::ICMP_ULE:
2709 Lower = APInt::getMinValue(BitWidth); Upper++;
2710 break;
2711 case ICmpInst::ICMP_SLE:
2712 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2713 break;
2714 case ICmpInst::ICMP_UGE:
2715 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2716 break;
2717 case ICmpInst::ICMP_SGE:
2718 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2719 break;
2720 }
2721 return ConstantRange(Lower, Upper);
2722}
2723
Dan Gohman4e724382008-05-31 02:47:54 +00002724CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002725 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002726 default: assert(!"Unknown cmp predicate!");
2727 case ICMP_EQ: case ICMP_NE:
2728 return pred;
2729 case ICMP_SGT: return ICMP_SLT;
2730 case ICMP_SLT: return ICMP_SGT;
2731 case ICMP_SGE: return ICMP_SLE;
2732 case ICMP_SLE: return ICMP_SGE;
2733 case ICMP_UGT: return ICMP_ULT;
2734 case ICMP_ULT: return ICMP_UGT;
2735 case ICMP_UGE: return ICMP_ULE;
2736 case ICMP_ULE: return ICMP_UGE;
2737
Reid Spencerd9436b62006-11-20 01:22:35 +00002738 case FCMP_FALSE: case FCMP_TRUE:
2739 case FCMP_OEQ: case FCMP_ONE:
2740 case FCMP_UEQ: case FCMP_UNE:
2741 case FCMP_ORD: case FCMP_UNO:
2742 return pred;
2743 case FCMP_OGT: return FCMP_OLT;
2744 case FCMP_OLT: return FCMP_OGT;
2745 case FCMP_OGE: return FCMP_OLE;
2746 case FCMP_OLE: return FCMP_OGE;
2747 case FCMP_UGT: return FCMP_ULT;
2748 case FCMP_ULT: return FCMP_UGT;
2749 case FCMP_UGE: return FCMP_ULE;
2750 case FCMP_ULE: return FCMP_UGE;
2751 }
2752}
2753
Reid Spencer266e42b2006-12-23 06:05:41 +00002754bool CmpInst::isUnsigned(unsigned short predicate) {
2755 switch (predicate) {
2756 default: return false;
2757 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2758 case ICmpInst::ICMP_UGE: return true;
2759 }
2760}
2761
2762bool CmpInst::isSigned(unsigned short predicate){
2763 switch (predicate) {
2764 default: return false;
2765 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2766 case ICmpInst::ICMP_SGE: return true;
2767 }
2768}
2769
2770bool CmpInst::isOrdered(unsigned short predicate) {
2771 switch (predicate) {
2772 default: return false;
2773 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2774 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2775 case FCmpInst::FCMP_ORD: return true;
2776 }
2777}
2778
2779bool CmpInst::isUnordered(unsigned short predicate) {
2780 switch (predicate) {
2781 default: return false;
2782 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2783 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2784 case FCmpInst::FCMP_UNO: return true;
2785 }
2786}
2787
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002788//===----------------------------------------------------------------------===//
2789// SwitchInst Implementation
2790//===----------------------------------------------------------------------===//
2791
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002792void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002793 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002794 ReservedSpace = 2+NumCases*2;
2795 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002796 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002797
Gabor Greif2d3024d2008-05-26 21:33:52 +00002798 OperandList[0] = Value;
2799 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002800}
2801
Chris Lattner2195fc42007-02-24 00:55:48 +00002802/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2803/// switch on and a default destination. The number of additional cases can
2804/// be specified here to make memory allocation more efficient. This
2805/// constructor can also autoinsert before another instruction.
2806SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2807 Instruction *InsertBefore)
2808 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2809 init(Value, Default, NumCases);
2810}
2811
2812/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2813/// switch on and a default destination. The number of additional cases can
2814/// be specified here to make memory allocation more efficient. This
2815/// constructor also autoinserts at the end of the specified BasicBlock.
2816SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2817 BasicBlock *InsertAtEnd)
2818 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2819 init(Value, Default, NumCases);
2820}
2821
Misha Brukmanb1c93172005-04-21 23:48:37 +00002822SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002823 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002824 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002825 Use *OL = OperandList, *InOL = SI.OperandList;
2826 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002827 OL[i] = InOL[i];
2828 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002829 }
2830}
2831
Gordon Henriksen14a55692007-12-10 02:14:30 +00002832SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002833 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002834}
2835
2836
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002837/// addCase - Add an entry to the switch instruction...
2838///
Chris Lattner47ac1872005-02-24 05:32:09 +00002839void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002840 unsigned OpNo = NumOperands;
2841 if (OpNo+2 > ReservedSpace)
2842 resizeOperands(0); // Get more space!
2843 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002844 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002845 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002846 OperandList[OpNo] = OnVal;
2847 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002848}
2849
2850/// removeCase - This method removes the specified successor from the switch
2851/// instruction. Note that this cannot be used to remove the default
2852/// destination (successor #0).
2853///
2854void SwitchInst::removeCase(unsigned idx) {
2855 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002856 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2857
2858 unsigned NumOps = getNumOperands();
2859 Use *OL = OperandList;
2860
2861 // Move everything after this operand down.
2862 //
2863 // FIXME: we could just swap with the end of the list, then erase. However,
2864 // client might not expect this to happen. The code as it is thrashes the
2865 // use/def lists, which is kinda lame.
2866 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2867 OL[i-2] = OL[i];
2868 OL[i-2+1] = OL[i+1];
2869 }
2870
2871 // Nuke the last value.
2872 OL[NumOps-2].set(0);
2873 OL[NumOps-2+1].set(0);
2874 NumOperands = NumOps-2;
2875}
2876
2877/// resizeOperands - resize operands - This adjusts the length of the operands
2878/// list according to the following behavior:
2879/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002880/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002881/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2882/// 3. If NumOps == NumOperands, trim the reserved space.
2883///
2884void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002885 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002886 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002887 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002888 } else if (NumOps*2 > NumOperands) {
2889 // No resize needed.
2890 if (ReservedSpace >= NumOps) return;
2891 } else if (NumOps == NumOperands) {
2892 if (ReservedSpace == NumOps) return;
2893 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002894 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002895 }
2896
2897 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002898 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002899 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002900 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002901 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002902 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002903 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002904 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002905}
2906
2907
2908BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2909 return getSuccessor(idx);
2910}
2911unsigned SwitchInst::getNumSuccessorsV() const {
2912 return getNumSuccessors();
2913}
2914void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2915 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002916}
Chris Lattnerf22be932004-10-15 23:52:53 +00002917
Chris Lattnerf22be932004-10-15 23:52:53 +00002918// Define these methods here so vtables don't get emitted into every translation
2919// unit that uses these classes.
2920
2921GetElementPtrInst *GetElementPtrInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002922 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002923}
2924
2925BinaryOperator *BinaryOperator::clone() const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002926 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002927}
2928
Chris Lattner0b490b02007-08-24 20:48:18 +00002929FCmpInst* FCmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002930 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002931}
2932ICmpInst* ICmpInst::clone() const {
Gabor Greiff6caff662008-05-10 08:32:32 +00002933 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002934}
2935
Dan Gohman0752bff2008-05-23 00:36:11 +00002936ExtractValueInst *ExtractValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002937 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002938}
2939InsertValueInst *InsertValueInst::clone() const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002940 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002941}
2942
2943
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002944MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2945AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2946FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2947LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2948StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2949CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2950CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2951CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2952CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2953CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2954CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2955CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2956CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2957CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2958CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2959CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2960CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002961CallInst *CallInst::clone() const {
2962 return new(getNumOperands()) CallInst(*this);
2963}
2964SelectInst *SelectInst::clone() const {
2965 return new(getNumOperands()) SelectInst(*this);
2966}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002967VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2968
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002969ExtractElementInst *ExtractElementInst::clone() const {
2970 return new ExtractElementInst(*this);
2971}
2972InsertElementInst *InsertElementInst::clone() const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002973 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002974}
2975ShuffleVectorInst *ShuffleVectorInst::clone() const {
2976 return new ShuffleVectorInst(*this);
2977}
Chris Lattnerf22be932004-10-15 23:52:53 +00002978PHINode *PHINode::clone() const { return new PHINode(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002979ReturnInst *ReturnInst::clone() const {
2980 return new(getNumOperands()) ReturnInst(*this);
2981}
2982BranchInst *BranchInst::clone() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00002983 unsigned Ops(getNumOperands());
2984 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00002985}
Gabor Greiff6caff662008-05-10 08:32:32 +00002986SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
Gabor Greif697e94c2008-05-15 10:04:30 +00002987InvokeInst *InvokeInst::clone() const {
2988 return new(getNumOperands()) InvokeInst(*this);
2989}
Chris Lattnerf22be932004-10-15 23:52:53 +00002990UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002991UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}