blob: 902d806f23eb90d5d36343d6d62aebd5b2b7fad6 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000015#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Function.h"
18#include "llvm/Instructions.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000019#include "llvm/Operator.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000020#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000023#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024using namespace llvm;
25
Chris Lattner3e13b8c2008-01-02 23:42:30 +000026//===----------------------------------------------------------------------===//
27// CallSite Class
28//===----------------------------------------------------------------------===//
29
Gabor Greif1b9921a2009-01-11 22:33:22 +000030#define CALLSITE_DELEGATE_GETTER(METHOD) \
31 Instruction *II(getInstruction()); \
32 return isCall() \
33 ? cast<CallInst>(II)->METHOD \
34 : cast<InvokeInst>(II)->METHOD
35
36#define CALLSITE_DELEGATE_SETTER(METHOD) \
37 Instruction *II(getInstruction()); \
38 if (isCall()) \
39 cast<CallInst>(II)->METHOD; \
40 else \
41 cast<InvokeInst>(II)->METHOD
42
Duncan Sands85fab3a2008-02-18 17:32:13 +000043CallSite::CallSite(Instruction *C) {
44 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
Gabor Greif1b9921a2009-01-11 22:33:22 +000045 I.setPointer(C);
46 I.setInt(isa<CallInst>(C));
Duncan Sands85fab3a2008-02-18 17:32:13 +000047}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000048unsigned CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000049 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000050}
51void CallSite::setCallingConv(unsigned CC) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000052 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
Chris Lattnerf7b6d312005-05-06 20:26:43 +000053}
Devang Patel4c758ea2008-09-25 21:00:45 +000054const AttrListPtr &CallSite::getAttributes() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000055 CALLSITE_DELEGATE_GETTER(getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000056}
Devang Patel4c758ea2008-09-25 21:00:45 +000057void CallSite::setAttributes(const AttrListPtr &PAL) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000058 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000059}
Devang Patelba3fa6c2008-09-23 23:03:40 +000060bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000061 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
Duncan Sands5208d1a2007-11-28 17:07:01 +000062}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000063uint16_t CallSite::getParamAlignment(uint16_t i) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000064 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
Dale Johanneseneabc5f32008-02-22 17:49:45 +000065}
Duncan Sands38ef3a82007-12-03 20:06:50 +000066bool CallSite::doesNotAccessMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000067 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000068}
Duncan Sands78c88722008-07-08 08:38:44 +000069void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000070 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000071}
Duncan Sands38ef3a82007-12-03 20:06:50 +000072bool CallSite::onlyReadsMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000073 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000074}
Duncan Sands78c88722008-07-08 08:38:44 +000075void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000076 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000077}
78bool CallSite::doesNotReturn() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000079 CALLSITE_DELEGATE_GETTER(doesNotReturn());
Duncan Sands78c88722008-07-08 08:38:44 +000080}
81void CallSite::setDoesNotReturn(bool doesNotReturn) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000082 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
Duncan Sands78c88722008-07-08 08:38:44 +000083}
Duncan Sands3353ed02007-12-18 09:59:50 +000084bool CallSite::doesNotThrow() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000085 CALLSITE_DELEGATE_GETTER(doesNotThrow());
Duncan Sands8e4847e2007-12-16 15:51:49 +000086}
Duncan Sandsaa31b922007-12-19 21:13:37 +000087void CallSite::setDoesNotThrow(bool doesNotThrow) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000088 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
Duncan Sandsaa31b922007-12-19 21:13:37 +000089}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000090
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000091bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000092 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
93 if (AI->get() == Arg)
94 return true;
95 return false;
96}
97
Gabor Greif1b9921a2009-01-11 22:33:22 +000098#undef CALLSITE_DELEGATE_GETTER
99#undef CALLSITE_DELEGATE_SETTER
100
Gordon Henriksen14a55692007-12-10 02:14:30 +0000101//===----------------------------------------------------------------------===//
102// TerminatorInst Class
103//===----------------------------------------------------------------------===//
104
105// Out of line virtual method, so the vtable, etc has a home.
106TerminatorInst::~TerminatorInst() {
107}
108
Gabor Greiff6caff662008-05-10 08:32:32 +0000109//===----------------------------------------------------------------------===//
110// UnaryInstruction Class
111//===----------------------------------------------------------------------===//
112
Gordon Henriksen14a55692007-12-10 02:14:30 +0000113// Out of line virtual method, so the vtable, etc has a home.
114UnaryInstruction::~UnaryInstruction() {
115}
116
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000118// SelectInst Class
119//===----------------------------------------------------------------------===//
120
121/// areInvalidOperands - Return a string if the specified operands are invalid
122/// for a select operation, otherwise return null.
123const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
124 if (Op1->getType() != Op2->getType())
125 return "both values to select must have same type";
126
127 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
128 // Vector select.
129 if (VT->getElementType() != Type::Int1Ty)
130 return "vector select condition element type must be i1";
131 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
132 if (ET == 0)
133 return "selected values for vector select must be vectors";
134 if (ET->getNumElements() != VT->getNumElements())
135 return "vector select requires selected vectors to have "
136 "the same vector length as select condition";
137 } else if (Op0->getType() != Type::Int1Ty) {
138 return "select condition must be i1 or <n x i1>";
139 }
140 return 0;
141}
142
143
144//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000145// PHINode Class
146//===----------------------------------------------------------------------===//
147
148PHINode::PHINode(const PHINode &PN)
149 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000150 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000151 ReservedSpace(PN.getNumOperands()) {
152 Use *OL = OperandList;
153 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000154 OL[i] = PN.getOperand(i);
155 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156 }
157}
158
Gordon Henriksen14a55692007-12-10 02:14:30 +0000159PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000160 if (OperandList)
161 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000162}
163
164// removeIncomingValue - Remove an incoming value. This is useful if a
165// predecessor basic block is deleted.
166Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
167 unsigned NumOps = getNumOperands();
168 Use *OL = OperandList;
169 assert(Idx*2 < NumOps && "BB not in PHI node!");
170 Value *Removed = OL[Idx*2];
171
172 // Move everything after this operand down.
173 //
174 // FIXME: we could just swap with the end of the list, then erase. However,
175 // client might not expect this to happen. The code as it is thrashes the
176 // use/def lists, which is kinda lame.
177 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
178 OL[i-2] = OL[i];
179 OL[i-2+1] = OL[i+1];
180 }
181
182 // Nuke the last value.
183 OL[NumOps-2].set(0);
184 OL[NumOps-2+1].set(0);
185 NumOperands = NumOps-2;
186
187 // If the PHI node is dead, because it has zero entries, nuke it now.
188 if (NumOps == 2 && DeletePHIIfEmpty) {
189 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000190 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000191 eraseFromParent();
192 }
193 return Removed;
194}
195
196/// resizeOperands - resize operands - This adjusts the length of the operands
197/// list according to the following behavior:
198/// 1. If NumOps == 0, grow the operand list in response to a push_back style
199/// of operation. This grows the number of ops by 1.5 times.
200/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
201/// 3. If NumOps == NumOperands, trim the reserved space.
202///
203void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000204 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000205 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000206 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000207 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
208 } else if (NumOps*2 > NumOperands) {
209 // No resize needed.
210 if (ReservedSpace >= NumOps) return;
211 } else if (NumOps == NumOperands) {
212 if (ReservedSpace == NumOps) return;
213 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000214 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215 }
216
217 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000218 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000219 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000220 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000221 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000222 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000223}
224
Nate Begemanb3923212005-08-04 23:24:19 +0000225/// hasConstantValue - If the specified PHI node always merges together the same
226/// value, return the value, otherwise return null.
227///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000228Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000229 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000230 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000231 if (getIncomingValue(0) != this) // not X = phi X
232 return getIncomingValue(0);
233 else
Owen Andersonb292b8c2009-07-30 23:03:37 +0000234 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000235 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000236
Nate Begemanb3923212005-08-04 23:24:19 +0000237 // Otherwise if all of the incoming values are the same for the PHI, replace
238 // the PHI node with the incoming value.
239 //
240 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000241 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000242 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000243 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000244 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000245 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000246 if (InVal && getIncomingValue(i) != InVal)
247 return 0; // Not the same, bail out.
248 else
249 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000250 }
Nate Begemanb3923212005-08-04 23:24:19 +0000251
252 // The only case that could cause InVal to be null is if we have a PHI node
253 // that only has entries for itself. In this case, there is no entry into the
254 // loop, so kill the PHI.
255 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000256 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000257
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000258 // If we have a PHI node like phi(X, undef, X), where X is defined by some
259 // instruction, we cannot always return X as the result of the PHI node. Only
260 // do this if X is not an instruction (thus it must dominate the PHI block),
261 // or if the client is prepared to deal with this possibility.
262 if (HasUndefInput && !AllowNonDominatingInstruction)
263 if (Instruction *IV = dyn_cast<Instruction>(InVal))
264 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000265 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000266 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000267 return 0; // Cannot guarantee that InVal dominates this PHINode.
268
Nate Begemanb3923212005-08-04 23:24:19 +0000269 // All of the incoming values are the same, return the value now.
270 return InVal;
271}
272
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000273
274//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000275// CallInst Implementation
276//===----------------------------------------------------------------------===//
277
Gordon Henriksen14a55692007-12-10 02:14:30 +0000278CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000279}
280
Chris Lattner054ba2c2007-02-13 00:58:44 +0000281void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000282 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
283 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000284 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285
Misha Brukmanb1c93172005-04-21 23:48:37 +0000286 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000288 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289
Chris Lattner054ba2c2007-02-13 00:58:44 +0000290 assert((NumParams == FTy->getNumParams() ||
291 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000292 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000293 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000294 assert((i >= FTy->getNumParams() ||
295 FTy->getParamType(i) == Params[i]->getType()) &&
296 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000297 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000298 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299}
300
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000301void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000302 assert(NumOperands == 3 && "NumOperands not set up?");
303 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000304 OL[0] = Func;
305 OL[1] = Actual1;
306 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000307
308 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000310 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000312 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000313 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000315 assert((0 >= FTy->getNumParams() ||
316 FTy->getParamType(0) == Actual1->getType()) &&
317 "Calling a function with a bad signature!");
318 assert((1 >= FTy->getNumParams() ||
319 FTy->getParamType(1) == Actual2->getType()) &&
320 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000321}
322
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000323void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000324 assert(NumOperands == 2 && "NumOperands not set up?");
325 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000326 OL[0] = Func;
327 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000328
329 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000331 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000333 assert((FTy->getNumParams() == 1 ||
334 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000336 assert((0 == FTy->getNumParams() ||
337 FTy->getParamType(0) == Actual->getType()) &&
338 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000339}
340
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000341void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000342 assert(NumOperands == 1 && "NumOperands not set up?");
343 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000344 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000345
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000346 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000347 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000348 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000349
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000350 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000351}
352
Daniel Dunbar4975db62009-07-25 04:41:11 +0000353CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000354 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000355 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
356 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000357 Instruction::Call,
358 OperandTraits<CallInst>::op_end(this) - 2,
359 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000360 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000361 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362}
363
Daniel Dunbar4975db62009-07-25 04:41:11 +0000364CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000365 BasicBlock *InsertAtEnd)
366 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
367 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000368 Instruction::Call,
369 OperandTraits<CallInst>::op_end(this) - 2,
370 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000372 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000373}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000374CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000375 Instruction *InsertBefore)
376 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
377 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000378 Instruction::Call,
379 OperandTraits<CallInst>::op_end(this) - 1,
380 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000381 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000382 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000383}
384
Daniel Dunbar4975db62009-07-25 04:41:11 +0000385CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000386 BasicBlock *InsertAtEnd)
387 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
388 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000389 Instruction::Call,
390 OperandTraits<CallInst>::op_end(this) - 1,
391 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000392 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000393 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000394}
395
Misha Brukmanb1c93172005-04-21 23:48:37 +0000396CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000397 : Instruction(CI.getType(), Instruction::Call,
398 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000399 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000400 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000401 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000402 Use *OL = OperandList;
403 Use *InOL = CI.OperandList;
404 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000405 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000406}
407
Devang Patel4c758ea2008-09-25 21:00:45 +0000408void CallInst::addAttribute(unsigned i, Attributes attr) {
409 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000410 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000411 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000412}
413
Devang Patel4c758ea2008-09-25 21:00:45 +0000414void CallInst::removeAttribute(unsigned i, Attributes attr) {
415 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000416 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000417 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000418}
419
Devang Patelba3fa6c2008-09-23 23:03:40 +0000420bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000421 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000422 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000423 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000424 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000425 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000426}
427
Duncan Sands5208d1a2007-11-28 17:07:01 +0000428
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000429//===----------------------------------------------------------------------===//
430// InvokeInst Implementation
431//===----------------------------------------------------------------------===//
432
433void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000434 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000435 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Bill Wendling4bb96e92009-03-13 21:15:59 +0000436 Use *OL = OperandList;
437 OL[0] = Fn;
438 OL[1] = IfNormal;
439 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000440 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000441 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000442 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000443
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000444 assert(((NumArgs == FTy->getNumParams()) ||
445 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000446 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000447
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000448 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000449 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000450 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000451 "Invoking a function with a bad signature!");
452
Bill Wendling4bb96e92009-03-13 21:15:59 +0000453 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000454 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000455}
456
Misha Brukmanb1c93172005-04-21 23:48:37 +0000457InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000458 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000459 OperandTraits<InvokeInst>::op_end(this)
460 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000461 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000462 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000463 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000464 Use *OL = OperandList, *InOL = II.OperandList;
465 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000466 OL[i] = InOL[i];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000467}
468
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000469BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
470 return getSuccessor(idx);
471}
472unsigned InvokeInst::getNumSuccessorsV() const {
473 return getNumSuccessors();
474}
475void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
476 return setSuccessor(idx, B);
477}
478
Devang Patelba3fa6c2008-09-23 23:03:40 +0000479bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000480 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000481 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000482 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000483 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000484 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000485}
486
Devang Patel4c758ea2008-09-25 21:00:45 +0000487void InvokeInst::addAttribute(unsigned i, Attributes attr) {
488 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000489 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000490 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000491}
492
Devang Patel4c758ea2008-09-25 21:00:45 +0000493void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
494 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000495 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000496 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000497}
498
Duncan Sands5208d1a2007-11-28 17:07:01 +0000499
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000500//===----------------------------------------------------------------------===//
501// ReturnInst Implementation
502//===----------------------------------------------------------------------===//
503
Chris Lattner2195fc42007-02-24 00:55:48 +0000504ReturnInst::ReturnInst(const ReturnInst &RI)
505 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000506 OperandTraits<ReturnInst>::op_end(this) -
507 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000508 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000509 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000510 Op<0>() = RI.Op<0>();
Chris Lattner2195fc42007-02-24 00:55:48 +0000511}
512
513ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000514 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000515 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
516 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000517 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000518 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000519}
520ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000521 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000522 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
523 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000524 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000525 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000526}
527ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000528 : TerminatorInst(Type::VoidTy, Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000529 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000530}
531
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000532unsigned ReturnInst::getNumSuccessorsV() const {
533 return getNumSuccessors();
534}
535
Devang Patelae682fb2008-02-26 17:56:20 +0000536/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
537/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000538void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000539 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000540}
541
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000542BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000543 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000544 return 0;
545}
546
Devang Patel59643e52008-02-23 00:35:18 +0000547ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000548}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000549
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000550//===----------------------------------------------------------------------===//
551// UnwindInst Implementation
552//===----------------------------------------------------------------------===//
553
Chris Lattner2195fc42007-02-24 00:55:48 +0000554UnwindInst::UnwindInst(Instruction *InsertBefore)
555 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
556}
557UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
558 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
559}
560
561
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000562unsigned UnwindInst::getNumSuccessorsV() const {
563 return getNumSuccessors();
564}
565
566void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000567 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000568}
569
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000570BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000571 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000572 return 0;
573}
574
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000575//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000576// UnreachableInst Implementation
577//===----------------------------------------------------------------------===//
578
Chris Lattner2195fc42007-02-24 00:55:48 +0000579UnreachableInst::UnreachableInst(Instruction *InsertBefore)
580 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
581}
582UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
583 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
584}
585
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000586unsigned UnreachableInst::getNumSuccessorsV() const {
587 return getNumSuccessors();
588}
589
590void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000591 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000592}
593
594BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000595 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000597}
598
599//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000600// BranchInst Implementation
601//===----------------------------------------------------------------------===//
602
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000603void BranchInst::AssertOK() {
604 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000605 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000606 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000607}
608
Chris Lattner2195fc42007-02-24 00:55:48 +0000609BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000610 : TerminatorInst(Type::VoidTy, Instruction::Br,
611 OperandTraits<BranchInst>::op_end(this) - 1,
612 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000613 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000614 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000615}
616BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
617 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000618 : TerminatorInst(Type::VoidTy, Instruction::Br,
619 OperandTraits<BranchInst>::op_end(this) - 3,
620 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000621 Op<-1>() = IfTrue;
622 Op<-2>() = IfFalse;
623 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000624#ifndef NDEBUG
625 AssertOK();
626#endif
627}
628
629BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000630 : TerminatorInst(Type::VoidTy, Instruction::Br,
631 OperandTraits<BranchInst>::op_end(this) - 1,
632 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000633 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000634 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000635}
636
637BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
638 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000639 : TerminatorInst(Type::VoidTy, Instruction::Br,
640 OperandTraits<BranchInst>::op_end(this) - 3,
641 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000642 Op<-1>() = IfTrue;
643 Op<-2>() = IfFalse;
644 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000645#ifndef NDEBUG
646 AssertOK();
647#endif
648}
649
650
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000651BranchInst::BranchInst(const BranchInst &BI) :
Gabor Greiff6caff662008-05-10 08:32:32 +0000652 TerminatorInst(Type::VoidTy, Instruction::Br,
653 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
654 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000655 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656 if (BI.getNumOperands() != 1) {
657 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000658 Op<-3>() = BI.Op<-3>();
659 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000660 }
661}
662
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000663
664Use* Use::getPrefix() {
665 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
666 if (PotentialPrefix.getOpaqueValue())
667 return 0;
668
669 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
670}
671
672BranchInst::~BranchInst() {
673 if (NumOperands == 1) {
674 if (Use *Prefix = OperandList->getPrefix()) {
675 Op<-1>() = 0;
676 //
677 // mark OperandList to have a special value for scrutiny
678 // by baseclass destructors and operator delete
679 OperandList = Prefix;
680 } else {
681 NumOperands = 3;
682 OperandList = op_begin();
683 }
684 }
685}
686
687
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000688BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
689 return getSuccessor(idx);
690}
691unsigned BranchInst::getNumSuccessorsV() const {
692 return getNumSuccessors();
693}
694void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
695 setSuccessor(idx, B);
696}
697
698
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000699//===----------------------------------------------------------------------===//
700// AllocationInst Implementation
701//===----------------------------------------------------------------------===//
702
Owen Andersonb6b25302009-07-14 23:09:55 +0000703static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000704 if (!Amt)
Owen Andersonedb4a702009-07-24 23:12:02 +0000705 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000706 else {
707 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000708 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000709 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000710 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000711 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000712 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000713}
714
Owen Anderson4fdeba92009-07-15 23:53:25 +0000715AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000716 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000717 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +0000718 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000719 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000720 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000722 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000723}
724
Owen Anderson4fdeba92009-07-15 23:53:25 +0000725AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000726 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000727 BasicBlock *InsertAtEnd)
Owen Anderson4056ca92009-07-29 22:17:13 +0000728 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000729 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000730 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000731 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000732 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733}
734
Gordon Henriksen14a55692007-12-10 02:14:30 +0000735// Out of line virtual method, so the vtable, etc has a home.
736AllocationInst::~AllocationInst() {
737}
738
Dan Gohmanaa583d72008-03-24 16:55:58 +0000739void AllocationInst::setAlignment(unsigned Align) {
740 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
741 SubclassData = Log2_32(Align) + 1;
742 assert(getAlignment() == Align && "Alignment representation error!");
743}
744
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000745bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000746 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
747 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000748 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000749}
750
751const Type *AllocationInst::getAllocatedType() const {
752 return getType()->getElementType();
753}
754
755AllocaInst::AllocaInst(const AllocaInst &AI)
Owen Anderson4fdeba92009-07-15 23:53:25 +0000756 : AllocationInst(AI.getType()->getElementType(),
Owen Andersonb6b25302009-07-14 23:09:55 +0000757 (Value*)AI.getOperand(0), Instruction::Alloca,
758 AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000759}
760
Chris Lattner8b291e62008-11-26 02:54:17 +0000761/// isStaticAlloca - Return true if this alloca is in the entry block of the
762/// function and is a constant size. If so, the code generator will fold it
763/// into the prolog/epilog code, so it is basically free.
764bool AllocaInst::isStaticAlloca() const {
765 // Must be constant size.
766 if (!isa<ConstantInt>(getArraySize())) return false;
767
768 // Must be in the entry block.
769 const BasicBlock *Parent = getParent();
770 return Parent == &Parent->getParent()->front();
771}
772
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000773MallocInst::MallocInst(const MallocInst &MI)
Owen Anderson4fdeba92009-07-15 23:53:25 +0000774 : AllocationInst(MI.getType()->getElementType(),
Owen Andersonb6b25302009-07-14 23:09:55 +0000775 (Value*)MI.getOperand(0), Instruction::Malloc,
776 MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000777}
778
779//===----------------------------------------------------------------------===//
780// FreeInst Implementation
781//===----------------------------------------------------------------------===//
782
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000783void FreeInst::AssertOK() {
784 assert(isa<PointerType>(getOperand(0)->getType()) &&
785 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000786}
787
788FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000789 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000790 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000791}
792
793FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000794 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000795 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000796}
797
798
799//===----------------------------------------------------------------------===//
800// LoadInst Implementation
801//===----------------------------------------------------------------------===//
802
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000803void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000804 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000805 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000806}
807
Daniel Dunbar4975db62009-07-25 04:41:11 +0000808LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000809 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000810 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000811 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000812 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000813 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000814 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000815}
816
Daniel Dunbar4975db62009-07-25 04:41:11 +0000817LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000818 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000819 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000820 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000821 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000822 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000823 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000824}
825
Daniel Dunbar4975db62009-07-25 04:41:11 +0000826LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000827 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000828 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000829 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000830 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000831 setAlignment(0);
832 AssertOK();
833 setName(Name);
834}
835
Daniel Dunbar4975db62009-07-25 04:41:11 +0000836LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000837 unsigned Align, Instruction *InsertBef)
838 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
839 Load, Ptr, InsertBef) {
840 setVolatile(isVolatile);
841 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000842 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000843 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000844}
845
Daniel Dunbar4975db62009-07-25 04:41:11 +0000846LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000847 unsigned Align, BasicBlock *InsertAE)
848 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
849 Load, Ptr, InsertAE) {
850 setVolatile(isVolatile);
851 setAlignment(Align);
852 AssertOK();
853 setName(Name);
854}
855
Daniel Dunbar4975db62009-07-25 04:41:11 +0000856LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000857 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000858 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000859 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000860 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000861 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000862 AssertOK();
863 setName(Name);
864}
865
Daniel Dunbar27096822009-08-11 18:11:15 +0000866
867
868LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
869 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
870 Load, Ptr, InsertBef) {
871 setVolatile(false);
872 setAlignment(0);
873 AssertOK();
874 if (Name && Name[0]) setName(Name);
875}
876
877LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
878 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
879 Load, Ptr, InsertAE) {
880 setVolatile(false);
881 setAlignment(0);
882 AssertOK();
883 if (Name && Name[0]) setName(Name);
884}
885
886LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
887 Instruction *InsertBef)
888: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
889 Load, Ptr, InsertBef) {
890 setVolatile(isVolatile);
891 setAlignment(0);
892 AssertOK();
893 if (Name && Name[0]) setName(Name);
894}
895
896LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
897 BasicBlock *InsertAE)
898 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
899 Load, Ptr, InsertAE) {
900 setVolatile(isVolatile);
901 setAlignment(0);
902 AssertOK();
903 if (Name && Name[0]) setName(Name);
904}
905
Christopher Lamb84485702007-04-22 19:24:39 +0000906void LoadInst::setAlignment(unsigned Align) {
907 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
908 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
909}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000910
911//===----------------------------------------------------------------------===//
912// StoreInst Implementation
913//===----------------------------------------------------------------------===//
914
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000915void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +0000916 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000917 assert(isa<PointerType>(getOperand(1)->getType()) &&
918 "Ptr must have pointer type!");
919 assert(getOperand(0)->getType() ==
920 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000921 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000922}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000923
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000924
925StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000926 : Instruction(Type::VoidTy, Store,
927 OperandTraits<StoreInst>::op_begin(this),
928 OperandTraits<StoreInst>::operands(this),
929 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000930 Op<0>() = val;
931 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000932 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000933 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000934 AssertOK();
935}
936
937StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000938 : Instruction(Type::VoidTy, Store,
939 OperandTraits<StoreInst>::op_begin(this),
940 OperandTraits<StoreInst>::operands(this),
941 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000942 Op<0>() = val;
943 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000944 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000945 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000946 AssertOK();
947}
948
Misha Brukmanb1c93172005-04-21 23:48:37 +0000949StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000950 Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000951 : Instruction(Type::VoidTy, Store,
952 OperandTraits<StoreInst>::op_begin(this),
953 OperandTraits<StoreInst>::operands(this),
954 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000955 Op<0>() = val;
956 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000957 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000958 setAlignment(0);
959 AssertOK();
960}
961
962StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
963 unsigned Align, Instruction *InsertBefore)
Gabor Greiff6caff662008-05-10 08:32:32 +0000964 : Instruction(Type::VoidTy, Store,
965 OperandTraits<StoreInst>::op_begin(this),
966 OperandTraits<StoreInst>::operands(this),
967 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000968 Op<0>() = val;
969 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +0000970 setVolatile(isVolatile);
971 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000972 AssertOK();
973}
974
Misha Brukmanb1c93172005-04-21 23:48:37 +0000975StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000976 unsigned Align, BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000977 : Instruction(Type::VoidTy, Store,
978 OperandTraits<StoreInst>::op_begin(this),
979 OperandTraits<StoreInst>::operands(this),
980 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000981 Op<0>() = val;
982 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +0000983 setVolatile(isVolatile);
984 setAlignment(Align);
985 AssertOK();
986}
987
988StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000989 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +0000990 : Instruction(Type::VoidTy, Store,
991 OperandTraits<StoreInst>::op_begin(this),
992 OperandTraits<StoreInst>::operands(this),
993 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000994 Op<0>() = val;
995 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +0000996 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000997 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000998 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000999}
1000
Christopher Lamb84485702007-04-22 19:24:39 +00001001void StoreInst::setAlignment(unsigned Align) {
1002 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1003 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1004}
1005
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001006//===----------------------------------------------------------------------===//
1007// GetElementPtrInst Implementation
1008//===----------------------------------------------------------------------===//
1009
Christopher Lambedf07882007-12-17 01:12:55 +00001010static unsigned retrieveAddrSpace(const Value *Val) {
1011 return cast<PointerType>(Val->getType())->getAddressSpace();
1012}
1013
Gabor Greif21ba1842008-06-06 20:28:12 +00001014void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001015 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001016 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1017 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001018 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001019
Chris Lattner79807c3d2007-01-31 19:47:18 +00001020 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001021 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001022
1023 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001024}
1025
Daniel Dunbar4975db62009-07-25 04:41:11 +00001026void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001027 assert(NumOperands == 2 && "NumOperands not initialized?");
1028 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001029 OL[0] = Ptr;
1030 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001031
1032 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001033}
1034
Gabor Greiff6caff662008-05-10 08:32:32 +00001035GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001036 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001037 OperandTraits<GetElementPtrInst>::op_end(this)
1038 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001039 GEPI.getNumOperands()) {
1040 Use *OL = OperandList;
1041 Use *GEPIOL = GEPI.OperandList;
1042 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001043 OL[i] = GEPIOL[i];
Gabor Greiff6caff662008-05-10 08:32:32 +00001044}
1045
Chris Lattner82981202005-05-03 05:43:30 +00001046GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001047 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001048 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001049 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001050 GetElementPtr,
1051 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1052 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001053 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001054}
1055
1056GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001057 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001058 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001059 checkType(getIndexedType(Ptr->getType(),Idx)),
1060 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001061 GetElementPtr,
1062 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1063 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001064 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001065}
1066
Chris Lattner6090a422009-03-09 04:46:40 +00001067/// getIndexedType - Returns the type of the element that would be accessed with
1068/// a gep instruction with the specified parameters.
1069///
1070/// The Idxs pointer should point to a continuous piece of memory containing the
1071/// indices, either as Value* or uint64_t.
1072///
1073/// A null type is returned if the indices are invalid for the specified
1074/// pointer type.
1075///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001076template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001077static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1078 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001079 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1080 if (!PTy) return 0; // Type isn't a pointer type!
1081 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001082
Chris Lattner6090a422009-03-09 04:46:40 +00001083 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001084 if (NumIdx == 0)
1085 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001086
1087 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001088 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1089 // that contain opaque types) under the assumption that it will be resolved to
1090 // a sane type later.
1091 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001092 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001093
Dan Gohman1ecaf452008-05-31 00:58:22 +00001094 unsigned CurIdx = 1;
1095 for (; CurIdx != NumIdx; ++CurIdx) {
1096 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1097 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001098 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001099 if (!CT->indexValid(Index)) return 0;
1100 Agg = CT->getTypeAtIndex(Index);
1101
1102 // If the new type forwards to another type, then it is in the middle
1103 // of being refined to another type (and hence, may have dropped all
1104 // references to what it was using before). So, use the new forwarded
1105 // type.
1106 if (const Type *Ty = Agg->getForwardedType())
1107 Agg = Ty;
1108 }
1109 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001110}
1111
Matthijs Kooijman04468622008-07-29 08:46:11 +00001112const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1113 Value* const *Idxs,
1114 unsigned NumIdx) {
1115 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1116}
1117
1118const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1119 uint64_t const *Idxs,
1120 unsigned NumIdx) {
1121 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1122}
1123
Chris Lattner82981202005-05-03 05:43:30 +00001124const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1125 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1126 if (!PTy) return 0; // Type isn't a pointer type!
1127
1128 // Check the pointer index.
1129 if (!PTy->indexValid(Idx)) return 0;
1130
Chris Lattnerc2233332005-05-03 16:44:45 +00001131 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001132}
1133
Chris Lattner45f15572007-04-14 00:12:57 +00001134
1135/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1136/// zeros. If so, the result pointer and the first operand have the same
1137/// value, just potentially different types.
1138bool GetElementPtrInst::hasAllZeroIndices() const {
1139 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1140 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1141 if (!CI->isZero()) return false;
1142 } else {
1143 return false;
1144 }
1145 }
1146 return true;
1147}
1148
Chris Lattner27058292007-04-27 20:35:56 +00001149/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1150/// constant integers. If so, the result pointer and the first operand have
1151/// a constant offset between them.
1152bool GetElementPtrInst::hasAllConstantIndices() const {
1153 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1154 if (!isa<ConstantInt>(getOperand(i)))
1155 return false;
1156 }
1157 return true;
1158}
1159
Chris Lattner45f15572007-04-14 00:12:57 +00001160
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001161//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001162// ExtractElementInst Implementation
1163//===----------------------------------------------------------------------===//
1164
1165ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001166 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001167 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001168 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001169 ExtractElement,
1170 OperandTraits<ExtractElementInst>::op_begin(this),
1171 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001172 assert(isValidOperands(Val, Index) &&
1173 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001174 Op<0>() = Val;
1175 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001176 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001177}
1178
1179ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001180 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001181 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001182 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001183 ExtractElement,
1184 OperandTraits<ExtractElementInst>::op_begin(this),
1185 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001186 assert(isValidOperands(Val, Index) &&
1187 "Invalid extractelement instruction operands!");
1188
Gabor Greif2d3024d2008-05-26 21:33:52 +00001189 Op<0>() = Val;
1190 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001191 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001192}
1193
Chris Lattner65511ff2006-10-05 06:24:58 +00001194
Chris Lattner54865b32006-04-08 04:05:48 +00001195bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001196 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001197 return false;
1198 return true;
1199}
1200
1201
Robert Bocchino23004482006-01-10 19:05:34 +00001202//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001203// InsertElementInst Implementation
1204//===----------------------------------------------------------------------===//
1205
Chris Lattner0875d942006-04-14 22:20:32 +00001206InsertElementInst::InsertElementInst(const InsertElementInst &IE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001207 : Instruction(IE.getType(), InsertElement,
1208 OperandTraits<InsertElementInst>::op_begin(this), 3) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001209 Op<0>() = IE.Op<0>();
1210 Op<1>() = IE.Op<1>();
1211 Op<2>() = IE.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001212}
Chris Lattner54865b32006-04-08 04:05:48 +00001213InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001214 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001215 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001216 : Instruction(Vec->getType(), InsertElement,
1217 OperandTraits<InsertElementInst>::op_begin(this),
1218 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001219 assert(isValidOperands(Vec, Elt, Index) &&
1220 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001221 Op<0>() = Vec;
1222 Op<1>() = Elt;
1223 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001224 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001225}
1226
Chris Lattner54865b32006-04-08 04:05:48 +00001227InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001228 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001229 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001230 : Instruction(Vec->getType(), InsertElement,
1231 OperandTraits<InsertElementInst>::op_begin(this),
1232 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001233 assert(isValidOperands(Vec, Elt, Index) &&
1234 "Invalid insertelement instruction operands!");
1235
Gabor Greif2d3024d2008-05-26 21:33:52 +00001236 Op<0>() = Vec;
1237 Op<1>() = Elt;
1238 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001239 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001240}
1241
Chris Lattner54865b32006-04-08 04:05:48 +00001242bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1243 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001244 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001245 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001246
Reid Spencerd84d35b2007-02-15 02:26:10 +00001247 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001248 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001249
Reid Spencer8d9336d2006-12-31 05:26:44 +00001250 if (Index->getType() != Type::Int32Ty)
Dan Gohman4fe64de2009-06-14 23:30:43 +00001251 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001252 return true;
1253}
1254
1255
Robert Bocchinoca27f032006-01-17 20:07:22 +00001256//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001257// ShuffleVectorInst Implementation
1258//===----------------------------------------------------------------------===//
1259
Chris Lattner0875d942006-04-14 22:20:32 +00001260ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
Gabor Greiff6caff662008-05-10 08:32:32 +00001261 : Instruction(SV.getType(), ShuffleVector,
1262 OperandTraits<ShuffleVectorInst>::op_begin(this),
1263 OperandTraits<ShuffleVectorInst>::operands(this)) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001264 Op<0>() = SV.Op<0>();
1265 Op<1>() = SV.Op<1>();
1266 Op<2>() = SV.Op<2>();
Chris Lattner0875d942006-04-14 22:20:32 +00001267}
1268
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001269ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001270 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001271 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001272: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001273 cast<VectorType>(Mask->getType())->getNumElements()),
1274 ShuffleVector,
1275 OperandTraits<ShuffleVectorInst>::op_begin(this),
1276 OperandTraits<ShuffleVectorInst>::operands(this),
1277 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001278 assert(isValidOperands(V1, V2, Mask) &&
1279 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001280 Op<0>() = V1;
1281 Op<1>() = V2;
1282 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001283 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001284}
1285
1286ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001287 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001288 BasicBlock *InsertAtEnd)
Gabor Greiff6caff662008-05-10 08:32:32 +00001289 : Instruction(V1->getType(), ShuffleVector,
1290 OperandTraits<ShuffleVectorInst>::op_begin(this),
1291 OperandTraits<ShuffleVectorInst>::operands(this),
1292 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001293 assert(isValidOperands(V1, V2, Mask) &&
1294 "Invalid shuffle vector instruction operands!");
1295
Gabor Greif2d3024d2008-05-26 21:33:52 +00001296 Op<0>() = V1;
1297 Op<1>() = V2;
1298 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001299 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001300}
1301
Mon P Wang25f01062008-11-10 04:46:22 +00001302bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001303 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001304 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001305 return false;
1306
1307 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1308 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Mon P Wang25f01062008-11-10 04:46:22 +00001309 MaskTy->getElementType() != Type::Int32Ty)
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001310 return false;
1311 return true;
1312}
1313
Chris Lattnerf724e342008-03-02 05:28:33 +00001314/// getMaskValue - Return the index from the shuffle mask for the specified
1315/// output result. This is either -1 if the element is undef or a number less
1316/// than 2*numelements.
1317int ShuffleVectorInst::getMaskValue(unsigned i) const {
1318 const Constant *Mask = cast<Constant>(getOperand(2));
1319 if (isa<UndefValue>(Mask)) return -1;
1320 if (isa<ConstantAggregateZero>(Mask)) return 0;
1321 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1322 assert(i < MaskCV->getNumOperands() && "Index out of range");
1323
1324 if (isa<UndefValue>(MaskCV->getOperand(i)))
1325 return -1;
1326 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1327}
1328
Dan Gohman12fce772008-05-15 19:50:34 +00001329//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001330// InsertValueInst Class
1331//===----------------------------------------------------------------------===//
1332
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001333void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001334 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001335 assert(NumOperands == 2 && "NumOperands not initialized?");
1336 Op<0>() = Agg;
1337 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001338
Dan Gohman1ecaf452008-05-31 00:58:22 +00001339 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001340 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001341}
1342
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001343void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001344 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001345 assert(NumOperands == 2 && "NumOperands not initialized?");
1346 Op<0>() = Agg;
1347 Op<1>() = Val;
1348
1349 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001350 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001351}
1352
1353InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001354 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001355 OperandTraits<InsertValueInst>::op_begin(this), 2),
1356 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001357 Op<0>() = IVI.getOperand(0);
1358 Op<1>() = IVI.getOperand(1);
Dan Gohman0752bff2008-05-23 00:36:11 +00001359}
1360
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001361InsertValueInst::InsertValueInst(Value *Agg,
1362 Value *Val,
1363 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001364 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001365 Instruction *InsertBefore)
1366 : Instruction(Agg->getType(), InsertValue,
1367 OperandTraits<InsertValueInst>::op_begin(this),
1368 2, InsertBefore) {
1369 init(Agg, Val, Idx, Name);
1370}
1371
1372InsertValueInst::InsertValueInst(Value *Agg,
1373 Value *Val,
1374 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001375 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001376 BasicBlock *InsertAtEnd)
1377 : Instruction(Agg->getType(), InsertValue,
1378 OperandTraits<InsertValueInst>::op_begin(this),
1379 2, InsertAtEnd) {
1380 init(Agg, Val, Idx, Name);
1381}
1382
Dan Gohman0752bff2008-05-23 00:36:11 +00001383//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001384// ExtractValueInst Class
1385//===----------------------------------------------------------------------===//
1386
Gabor Greifcbcc4952008-06-06 21:06:32 +00001387void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001388 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001389 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001390
Dan Gohman1ecaf452008-05-31 00:58:22 +00001391 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001392 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001393}
1394
Daniel Dunbar4975db62009-07-25 04:41:11 +00001395void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001396 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001397
1398 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001399 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001400}
1401
1402ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001403 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001404 Indices(EVI.Indices) {
Dan Gohman0752bff2008-05-23 00:36:11 +00001405}
1406
Dan Gohman12fce772008-05-15 19:50:34 +00001407// getIndexedType - Returns the type of the element that would be extracted
1408// with an extractvalue instruction with the specified parameters.
1409//
1410// A null type is returned if the indices are invalid for the specified
1411// pointer type.
1412//
1413const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001414 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001415 unsigned NumIdx) {
1416 unsigned CurIdx = 0;
1417 for (; CurIdx != NumIdx; ++CurIdx) {
1418 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001419 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1420 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001421 if (!CT->indexValid(Index)) return 0;
1422 Agg = CT->getTypeAtIndex(Index);
1423
1424 // If the new type forwards to another type, then it is in the middle
1425 // of being refined to another type (and hence, may have dropped all
1426 // references to what it was using before). So, use the new forwarded
1427 // type.
1428 if (const Type *Ty = Agg->getForwardedType())
1429 Agg = Ty;
1430 }
1431 return CurIdx == NumIdx ? Agg : 0;
1432}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001433
Dan Gohman4a3125b2008-06-17 21:07:55 +00001434const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001435 unsigned Idx) {
1436 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001437}
1438
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001439//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001440// BinaryOperator Class
1441//===----------------------------------------------------------------------===//
1442
Dan Gohmana5b96452009-06-04 22:49:04 +00001443/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1444/// type is floating-point, to help provide compatibility with an older API.
1445///
1446static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1447 const Type *Ty) {
1448 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1449 if (Ty->isFPOrFPVector()) {
1450 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1451 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1452 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1453 }
1454 return iType;
1455}
1456
Chris Lattner2195fc42007-02-24 00:55:48 +00001457BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001458 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001459 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001460 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001461 OperandTraits<BinaryOperator>::op_begin(this),
1462 OperandTraits<BinaryOperator>::operands(this),
1463 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001464 Op<0>() = S1;
1465 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001466 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001467 setName(Name);
1468}
1469
1470BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001471 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001472 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001473 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001474 OperandTraits<BinaryOperator>::op_begin(this),
1475 OperandTraits<BinaryOperator>::operands(this),
1476 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001477 Op<0>() = S1;
1478 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001479 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001480 setName(Name);
1481}
1482
1483
1484void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001485 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001486 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001487 assert(LHS->getType() == RHS->getType() &&
1488 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001489#ifndef NDEBUG
1490 switch (iType) {
1491 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001492 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001493 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001494 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001495 assert(getType()->isIntOrIntVector() &&
1496 "Tried to create an integer operation on a non-integer type!");
1497 break;
1498 case FAdd: case FSub:
1499 case FMul:
1500 assert(getType() == LHS->getType() &&
1501 "Arithmetic operation should return same type as operands!");
1502 assert(getType()->isFPOrFPVector() &&
1503 "Tried to create a floating-point operation on a "
1504 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001505 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001506 case UDiv:
1507 case SDiv:
1508 assert(getType() == LHS->getType() &&
1509 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001510 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1511 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001512 "Incorrect operand type (not integer) for S/UDIV");
1513 break;
1514 case FDiv:
1515 assert(getType() == LHS->getType() &&
1516 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001517 assert(getType()->isFPOrFPVector() &&
1518 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001519 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001520 case URem:
1521 case SRem:
1522 assert(getType() == LHS->getType() &&
1523 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001524 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1525 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001526 "Incorrect operand type (not integer) for S/UREM");
1527 break;
1528 case FRem:
1529 assert(getType() == LHS->getType() &&
1530 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001531 assert(getType()->isFPOrFPVector() &&
1532 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001533 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001534 case Shl:
1535 case LShr:
1536 case AShr:
1537 assert(getType() == LHS->getType() &&
1538 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001539 assert((getType()->isInteger() ||
1540 (isa<VectorType>(getType()) &&
1541 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1542 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001543 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001544 case And: case Or:
1545 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001546 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001547 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001548 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001549 (isa<VectorType>(getType()) &&
1550 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001551 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001552 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001553 default:
1554 break;
1555 }
1556#endif
1557}
1558
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001559BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001560 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001561 Instruction *InsertBefore) {
1562 assert(S1->getType() == S2->getType() &&
1563 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001564 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001565}
1566
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001567BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001568 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001569 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001570 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001571 InsertAtEnd->getInstList().push_back(Res);
1572 return Res;
1573}
1574
Dan Gohman5476cfd2009-08-12 16:23:25 +00001575BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001576 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001577 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001578 return new BinaryOperator(Instruction::Sub,
1579 zero, Op,
1580 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001581}
1582
Dan Gohman5476cfd2009-08-12 16:23:25 +00001583BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001584 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001585 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001586 return new BinaryOperator(Instruction::Sub,
1587 zero, Op,
1588 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001589}
1590
Dan Gohman5476cfd2009-08-12 16:23:25 +00001591BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001592 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001593 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001594 return new BinaryOperator(Instruction::FSub,
1595 zero, Op,
1596 Op->getType(), Name, InsertBefore);
1597}
1598
Dan Gohman5476cfd2009-08-12 16:23:25 +00001599BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001600 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001601 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001602 return new BinaryOperator(Instruction::FSub,
1603 zero, Op,
1604 Op->getType(), Name, InsertAtEnd);
1605}
1606
Dan Gohman5476cfd2009-08-12 16:23:25 +00001607BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001608 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001609 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001610 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001611 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001612 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001613 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001614 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001615 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001616 }
1617
1618 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001619 Op->getType(), Name, InsertBefore);
1620}
1621
Dan Gohman5476cfd2009-08-12 16:23:25 +00001622BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001623 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001624 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001625 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001626 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001627 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001628 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001629 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001630 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001631 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001632 }
1633
1634 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001635 Op->getType(), Name, InsertAtEnd);
1636}
1637
1638
1639// isConstantAllOnes - Helper function for several functions below
1640static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001641 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1642 return CI->isAllOnesValue();
1643 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1644 return CV->isAllOnesValue();
1645 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001646}
1647
Owen Andersonbb2501b2009-07-13 22:18:28 +00001648bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001649 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1650 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001651 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1652 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001653 return false;
1654}
1655
Owen Andersonbb2501b2009-07-13 22:18:28 +00001656bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001657 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1658 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001659 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1660 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001661 return false;
1662}
1663
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001664bool BinaryOperator::isNot(const Value *V) {
1665 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1666 return (Bop->getOpcode() == Instruction::Xor &&
1667 (isConstantAllOnes(Bop->getOperand(1)) ||
1668 isConstantAllOnes(Bop->getOperand(0))));
1669 return false;
1670}
1671
Chris Lattner2c7d1772005-04-24 07:28:37 +00001672Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001673 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001674}
1675
Chris Lattner2c7d1772005-04-24 07:28:37 +00001676const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1677 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001678}
1679
Dan Gohmana5b96452009-06-04 22:49:04 +00001680Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001681 return cast<BinaryOperator>(BinOp)->getOperand(1);
1682}
1683
1684const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1685 return getFNegArgument(const_cast<Value*>(BinOp));
1686}
1687
Chris Lattner2c7d1772005-04-24 07:28:37 +00001688Value *BinaryOperator::getNotArgument(Value *BinOp) {
1689 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1690 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1691 Value *Op0 = BO->getOperand(0);
1692 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001693 if (isConstantAllOnes(Op0)) return Op1;
1694
1695 assert(isConstantAllOnes(Op1));
1696 return Op0;
1697}
1698
Chris Lattner2c7d1772005-04-24 07:28:37 +00001699const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1700 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001701}
1702
1703
1704// swapOperands - Exchange the two operands to this instruction. This
1705// instruction is safe to use on any binary instruction and does not
1706// modify the semantics of the instruction. If the instruction is
1707// order dependent (SetLT f.e.) the opcode is changed.
1708//
1709bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001710 if (!isCommutative())
1711 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001712 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001713 return false;
1714}
1715
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001716//===----------------------------------------------------------------------===//
1717// CastInst Class
1718//===----------------------------------------------------------------------===//
1719
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001720// Just determine if this cast only deals with integral->integral conversion.
1721bool CastInst::isIntegerCast() const {
1722 switch (getOpcode()) {
1723 default: return false;
1724 case Instruction::ZExt:
1725 case Instruction::SExt:
1726 case Instruction::Trunc:
1727 return true;
1728 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001729 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001730 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001731}
1732
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001733bool CastInst::isLosslessCast() const {
1734 // Only BitCast can be lossless, exit fast if we're not BitCast
1735 if (getOpcode() != Instruction::BitCast)
1736 return false;
1737
1738 // Identity cast is always lossless
1739 const Type* SrcTy = getOperand(0)->getType();
1740 const Type* DstTy = getType();
1741 if (SrcTy == DstTy)
1742 return true;
1743
Reid Spencer8d9336d2006-12-31 05:26:44 +00001744 // Pointer to pointer is always lossless.
1745 if (isa<PointerType>(SrcTy))
1746 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001747 return false; // Other types have no identity values
1748}
1749
1750/// This function determines if the CastInst does not require any bits to be
1751/// changed in order to effect the cast. Essentially, it identifies cases where
1752/// no code gen is necessary for the cast, hence the name no-op cast. For
1753/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001754/// # bitcast i32* %x to i8*
1755/// # bitcast <2 x i32> %x to <4 x i16>
1756/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001757/// @brief Determine if a cast is a no-op.
1758bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1759 switch (getOpcode()) {
1760 default:
1761 assert(!"Invalid CastOp");
1762 case Instruction::Trunc:
1763 case Instruction::ZExt:
1764 case Instruction::SExt:
1765 case Instruction::FPTrunc:
1766 case Instruction::FPExt:
1767 case Instruction::UIToFP:
1768 case Instruction::SIToFP:
1769 case Instruction::FPToUI:
1770 case Instruction::FPToSI:
1771 return false; // These always modify bits
1772 case Instruction::BitCast:
1773 return true; // BitCast never modifies bits.
1774 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001775 return IntPtrTy->getScalarSizeInBits() ==
1776 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001777 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001778 return IntPtrTy->getScalarSizeInBits() ==
1779 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001780 }
1781}
1782
1783/// This function determines if a pair of casts can be eliminated and what
1784/// opcode should be used in the elimination. This assumes that there are two
1785/// instructions like this:
1786/// * %F = firstOpcode SrcTy %x to MidTy
1787/// * %S = secondOpcode MidTy %F to DstTy
1788/// The function returns a resultOpcode so these two casts can be replaced with:
1789/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1790/// If no such cast is permited, the function returns 0.
1791unsigned CastInst::isEliminableCastPair(
1792 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1793 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1794{
1795 // Define the 144 possibilities for these two cast instructions. The values
1796 // in this matrix determine what to do in a given situation and select the
1797 // case in the switch below. The rows correspond to firstOp, the columns
1798 // correspond to secondOp. In looking at the table below, keep in mind
1799 // the following cast properties:
1800 //
1801 // Size Compare Source Destination
1802 // Operator Src ? Size Type Sign Type Sign
1803 // -------- ------------ ------------------- ---------------------
1804 // TRUNC > Integer Any Integral Any
1805 // ZEXT < Integral Unsigned Integer Any
1806 // SEXT < Integral Signed Integer Any
1807 // FPTOUI n/a FloatPt n/a Integral Unsigned
1808 // FPTOSI n/a FloatPt n/a Integral Signed
1809 // UITOFP n/a Integral Unsigned FloatPt n/a
1810 // SITOFP n/a Integral Signed FloatPt n/a
1811 // FPTRUNC > FloatPt n/a FloatPt n/a
1812 // FPEXT < FloatPt n/a FloatPt n/a
1813 // PTRTOINT n/a Pointer n/a Integral Unsigned
1814 // INTTOPTR n/a Integral Unsigned Pointer n/a
1815 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001816 //
1817 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001818 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1819 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001820 // of the produced value (we no longer know the top-part is all zeros).
1821 // Further this conversion is often much more expensive for typical hardware,
1822 // and causes issues when building libgcc. We disallow fptosi+sext for the
1823 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001824 const unsigned numCastOps =
1825 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1826 static const uint8_t CastResults[numCastOps][numCastOps] = {
1827 // T F F U S F F P I B -+
1828 // R Z S P P I I T P 2 N T |
1829 // U E E 2 2 2 2 R E I T C +- secondOp
1830 // N X X U S F F N X N 2 V |
1831 // C T T I I P P C T T P T -+
1832 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1833 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1834 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001835 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1836 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001837 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1838 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1839 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1840 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1841 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1842 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1843 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1844 };
1845
1846 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1847 [secondOp-Instruction::CastOpsBegin];
1848 switch (ElimCase) {
1849 case 0:
1850 // categorically disallowed
1851 return 0;
1852 case 1:
1853 // allowed, use first cast's opcode
1854 return firstOp;
1855 case 2:
1856 // allowed, use second cast's opcode
1857 return secondOp;
1858 case 3:
1859 // no-op cast in second op implies firstOp as long as the DestTy
1860 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001861 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001862 return firstOp;
1863 return 0;
1864 case 4:
1865 // no-op cast in second op implies firstOp as long as the DestTy
1866 // is floating point
1867 if (DstTy->isFloatingPoint())
1868 return firstOp;
1869 return 0;
1870 case 5:
1871 // no-op cast in first op implies secondOp as long as the SrcTy
1872 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001873 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001874 return secondOp;
1875 return 0;
1876 case 6:
1877 // no-op cast in first op implies secondOp as long as the SrcTy
1878 // is a floating point
1879 if (SrcTy->isFloatingPoint())
1880 return secondOp;
1881 return 0;
1882 case 7: {
1883 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00001884 if (!IntPtrTy)
1885 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001886 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1887 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001888 if (MidSize >= PtrSize)
1889 return Instruction::BitCast;
1890 return 0;
1891 }
1892 case 8: {
1893 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1894 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1895 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001896 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1897 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001898 if (SrcSize == DstSize)
1899 return Instruction::BitCast;
1900 else if (SrcSize < DstSize)
1901 return firstOp;
1902 return secondOp;
1903 }
1904 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1905 return Instruction::ZExt;
1906 case 10:
1907 // fpext followed by ftrunc is allowed if the bit size returned to is
1908 // the same as the original, in which case its just a bitcast
1909 if (SrcTy == DstTy)
1910 return Instruction::BitCast;
1911 return 0; // If the types are not the same we can't eliminate it.
1912 case 11:
1913 // bitcast followed by ptrtoint is allowed as long as the bitcast
1914 // is a pointer to pointer cast.
1915 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1916 return secondOp;
1917 return 0;
1918 case 12:
1919 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1920 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1921 return firstOp;
1922 return 0;
1923 case 13: {
1924 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00001925 if (!IntPtrTy)
1926 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001927 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
1928 unsigned SrcSize = SrcTy->getScalarSizeInBits();
1929 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001930 if (SrcSize <= PtrSize && SrcSize == DstSize)
1931 return Instruction::BitCast;
1932 return 0;
1933 }
1934 case 99:
1935 // cast combination can't happen (error in input). This is for all cases
1936 // where the MidTy is not the same for the two cast instructions.
1937 assert(!"Invalid Cast Combination");
1938 return 0;
1939 default:
1940 assert(!"Error in CastResults table!!!");
1941 return 0;
1942 }
1943 return 0;
1944}
1945
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001946CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001947 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001948 // Construct and return the appropriate CastInst subclass
1949 switch (op) {
1950 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1951 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1952 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1953 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1954 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1955 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1956 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1957 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1958 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1959 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1960 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1961 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1962 default:
1963 assert(!"Invalid opcode provided");
1964 }
1965 return 0;
1966}
1967
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001968CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001969 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001970 // Construct and return the appropriate CastInst subclass
1971 switch (op) {
1972 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1973 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1974 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1975 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1976 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1977 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1978 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1979 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1980 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1981 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1982 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1983 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1984 default:
1985 assert(!"Invalid opcode provided");
1986 }
1987 return 0;
1988}
1989
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001990CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001991 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00001992 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001993 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001994 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1995 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00001996}
1997
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001998CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001999 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002000 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002001 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002002 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2003 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002004}
2005
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002006CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002007 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002008 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002009 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002010 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2011 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002012}
2013
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002014CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002015 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002016 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002017 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002018 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2019 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002020}
2021
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002022CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002023 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002024 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002025 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002026 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2027 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002028}
2029
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002030CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002031 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002032 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002033 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002034 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2035 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002036}
2037
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002038CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002039 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002040 BasicBlock *InsertAtEnd) {
2041 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002042 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002043 "Invalid cast");
2044
Chris Lattner03c49532007-01-15 02:27:26 +00002045 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002046 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2047 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002048}
2049
2050/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002051CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002052 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002053 Instruction *InsertBefore) {
2054 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002055 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002056 "Invalid cast");
2057
Chris Lattner03c49532007-01-15 02:27:26 +00002058 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002059 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2060 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002061}
2062
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002063CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002064 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002065 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002066 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002067 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2068 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002069 Instruction::CastOps opcode =
2070 (SrcBits == DstBits ? Instruction::BitCast :
2071 (SrcBits > DstBits ? Instruction::Trunc :
2072 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002073 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002074}
2075
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002076CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002077 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002078 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002079 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2080 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002081 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2082 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002083 Instruction::CastOps opcode =
2084 (SrcBits == DstBits ? Instruction::BitCast :
2085 (SrcBits > DstBits ? Instruction::Trunc :
2086 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002087 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002088}
2089
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002090CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002091 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002092 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002093 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002094 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002095 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2096 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002097 Instruction::CastOps opcode =
2098 (SrcBits == DstBits ? Instruction::BitCast :
2099 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002100 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002101}
2102
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002103CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002104 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002105 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002106 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002107 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002108 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2109 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002110 Instruction::CastOps opcode =
2111 (SrcBits == DstBits ? Instruction::BitCast :
2112 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002113 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002114}
2115
Duncan Sands55e50902008-01-06 10:12:28 +00002116// Check whether it is valid to call getCastOpcode for these types.
2117// This routine must be kept in sync with getCastOpcode.
2118bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2119 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2120 return false;
2121
2122 if (SrcTy == DestTy)
2123 return true;
2124
2125 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002126 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2127 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002128
2129 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002130 if (DestTy->isInteger()) { // Casting to integral
2131 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002132 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002133 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002134 return true;
2135 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002136 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002137 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002138 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002139 return isa<PointerType>(SrcTy);
2140 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002141 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2142 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002143 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002144 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002145 return true;
2146 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002147 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002148 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002149 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002150 return false;
2151 }
2152 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002153 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002154 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002155 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002156 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002157 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002158 return DestPTy->getBitWidth() == SrcBits;
2159 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002160 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2161 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002162 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002163 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002164 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002165 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002166 return false;
2167 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002168 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002169 return false;
2170 }
2171}
2172
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002173// Provide a way to get a "cast" where the cast opcode is inferred from the
2174// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002175// logic in the castIsValid function below. This axiom should hold:
2176// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2177// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002178// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002179// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002180Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002181CastInst::getCastOpcode(
2182 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002183 // Get the bit sizes, we'll need these
2184 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002185 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2186 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002187
Duncan Sands55e50902008-01-06 10:12:28 +00002188 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2189 "Only first class types are castable!");
2190
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002191 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002192 if (DestTy->isInteger()) { // Casting to integral
2193 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002194 if (DestBits < SrcBits)
2195 return Trunc; // int -> smaller int
2196 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002197 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002198 return SExt; // signed -> SEXT
2199 else
2200 return ZExt; // unsigned -> ZEXT
2201 } else {
2202 return BitCast; // Same size, No-op cast
2203 }
2204 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002205 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002206 return FPToSI; // FP -> sint
2207 else
2208 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002209 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002210 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002211 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002212 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002213 return BitCast; // Same size, no-op cast
2214 } else {
2215 assert(isa<PointerType>(SrcTy) &&
2216 "Casting from a value that is not first-class type");
2217 return PtrToInt; // ptr -> int
2218 }
2219 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002220 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002221 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002222 return SIToFP; // sint -> FP
2223 else
2224 return UIToFP; // uint -> FP
2225 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2226 if (DestBits < SrcBits) {
2227 return FPTrunc; // FP -> smaller FP
2228 } else if (DestBits > SrcBits) {
2229 return FPExt; // FP -> larger FP
2230 } else {
2231 return BitCast; // same size, no-op cast
2232 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002233 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002234 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002235 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002236 PTy = NULL;
2237 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002238 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002239 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002240 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002241 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2242 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002243 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002244 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002245 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002246 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002247 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002248 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002249 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002250 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002251 }
2252 } else if (isa<PointerType>(DestTy)) {
2253 if (isa<PointerType>(SrcTy)) {
2254 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002255 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002256 return IntToPtr; // int -> ptr
2257 } else {
2258 assert(!"Casting pointer to other than pointer or int");
2259 }
2260 } else {
2261 assert(!"Casting to type that is not first-class");
2262 }
2263
2264 // If we fall through to here we probably hit an assertion cast above
2265 // and assertions are not turned on. Anything we return is an error, so
2266 // BitCast is as good a choice as any.
2267 return BitCast;
2268}
2269
2270//===----------------------------------------------------------------------===//
2271// CastInst SubClass Constructors
2272//===----------------------------------------------------------------------===//
2273
2274/// Check that the construction parameters for a CastInst are correct. This
2275/// could be broken out into the separate constructors but it is useful to have
2276/// it in one place and to eliminate the redundant code for getting the sizes
2277/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002278bool
2279CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002280
2281 // Check for type sanity on the arguments
2282 const Type *SrcTy = S->getType();
2283 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2284 return false;
2285
2286 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002287 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2288 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002289
2290 // Switch on the opcode provided
2291 switch (op) {
2292 default: return false; // This is an input error
2293 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002294 return SrcTy->isIntOrIntVector() &&
2295 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002296 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002297 return SrcTy->isIntOrIntVector() &&
2298 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002299 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002300 return SrcTy->isIntOrIntVector() &&
2301 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002302 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002303 return SrcTy->isFPOrFPVector() &&
2304 DstTy->isFPOrFPVector() &&
2305 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002307 return SrcTy->isFPOrFPVector() &&
2308 DstTy->isFPOrFPVector() &&
2309 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002310 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002311 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002312 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2313 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002314 return SVTy->getElementType()->isIntOrIntVector() &&
2315 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002316 SVTy->getNumElements() == DVTy->getNumElements();
2317 }
2318 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002319 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002320 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002321 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002322 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2323 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002324 return SVTy->getElementType()->isFPOrFPVector() &&
2325 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002326 SVTy->getNumElements() == DVTy->getNumElements();
2327 }
2328 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002329 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002330 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002331 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002332 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002333 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002334 case Instruction::BitCast:
2335 // BitCast implies a no-op cast of type only. No bits change.
2336 // However, you can't cast pointers to anything but pointers.
2337 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2338 return false;
2339
Duncan Sands55e50902008-01-06 10:12:28 +00002340 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002341 // these cases, the cast is okay if the source and destination bit widths
2342 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002343 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344 }
2345}
2346
2347TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002348 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002349) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002350 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002351}
2352
2353TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002354 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002355) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002356 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357}
2358
2359ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002360 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002361) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002362 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363}
2364
2365ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002366 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002367) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002368 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002369}
2370SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002371 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002372) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002373 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002374}
2375
Jeff Cohencc08c832006-12-02 02:22:01 +00002376SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002377 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002379 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002380}
2381
2382FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002383 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002385 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002386}
2387
2388FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002389 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002390) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002391 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392}
2393
2394FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002395 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002396) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002397 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398}
2399
2400FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002401 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002403 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002404}
2405
2406UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002407 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002409 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002410}
2411
2412UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002413 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002414) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002415 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002416}
2417
2418SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002419 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002421 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422}
2423
2424SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002425 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002426) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002427 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428}
2429
2430FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002431 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002432) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002433 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002434}
2435
2436FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002437 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002438) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002439 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002440}
2441
2442FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002443 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002445 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002446}
2447
2448FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002449 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002451 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002452}
2453
2454PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002455 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002457 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002458}
2459
2460PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002461 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002462) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002463 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464}
2465
2466IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002467 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002468) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002469 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002470}
2471
2472IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002473 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002474) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002475 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002476}
2477
2478BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002479 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002480) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002481 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002482}
2483
2484BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002485 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002486) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002487 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002488}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002489
2490//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002491// CmpInst Classes
2492//===----------------------------------------------------------------------===//
2493
Nate Begemand2195702008-05-12 19:01:56 +00002494CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002495 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002496 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002497 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002498 OperandTraits<CmpInst>::op_begin(this),
2499 OperandTraits<CmpInst>::operands(this),
2500 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002501 Op<0>() = LHS;
2502 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002503 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002504 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002505}
Gabor Greiff6caff662008-05-10 08:32:32 +00002506
Nate Begemand2195702008-05-12 19:01:56 +00002507CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002508 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002509 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002510 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002511 OperandTraits<CmpInst>::op_begin(this),
2512 OperandTraits<CmpInst>::operands(this),
2513 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002514 Op<0>() = LHS;
2515 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002516 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002517 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002518}
2519
2520CmpInst *
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002521CmpInst::Create(LLVMContext &Context, OtherOps Op, unsigned short predicate,
2522 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002523 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002524 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002525 if (InsertBefore)
2526 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2527 S1, S2, Name);
2528 else
2529 return new ICmpInst(Context, CmpInst::Predicate(predicate),
2530 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002531 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002532
2533 if (InsertBefore)
2534 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2535 S1, S2, Name);
2536 else
2537 return new FCmpInst(Context, CmpInst::Predicate(predicate),
2538 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002539}
2540
2541CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002542CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002543 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002544 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002545 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2546 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002547 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002548 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2549 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002550}
2551
2552void CmpInst::swapOperands() {
2553 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2554 IC->swapOperands();
2555 else
2556 cast<FCmpInst>(this)->swapOperands();
2557}
2558
2559bool CmpInst::isCommutative() {
2560 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2561 return IC->isCommutative();
2562 return cast<FCmpInst>(this)->isCommutative();
2563}
2564
2565bool CmpInst::isEquality() {
2566 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2567 return IC->isEquality();
2568 return cast<FCmpInst>(this)->isEquality();
2569}
2570
2571
Dan Gohman4e724382008-05-31 02:47:54 +00002572CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002573 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002574 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002575 case ICMP_EQ: return ICMP_NE;
2576 case ICMP_NE: return ICMP_EQ;
2577 case ICMP_UGT: return ICMP_ULE;
2578 case ICMP_ULT: return ICMP_UGE;
2579 case ICMP_UGE: return ICMP_ULT;
2580 case ICMP_ULE: return ICMP_UGT;
2581 case ICMP_SGT: return ICMP_SLE;
2582 case ICMP_SLT: return ICMP_SGE;
2583 case ICMP_SGE: return ICMP_SLT;
2584 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002585
Dan Gohman4e724382008-05-31 02:47:54 +00002586 case FCMP_OEQ: return FCMP_UNE;
2587 case FCMP_ONE: return FCMP_UEQ;
2588 case FCMP_OGT: return FCMP_ULE;
2589 case FCMP_OLT: return FCMP_UGE;
2590 case FCMP_OGE: return FCMP_ULT;
2591 case FCMP_OLE: return FCMP_UGT;
2592 case FCMP_UEQ: return FCMP_ONE;
2593 case FCMP_UNE: return FCMP_OEQ;
2594 case FCMP_UGT: return FCMP_OLE;
2595 case FCMP_ULT: return FCMP_OGE;
2596 case FCMP_UGE: return FCMP_OLT;
2597 case FCMP_ULE: return FCMP_OGT;
2598 case FCMP_ORD: return FCMP_UNO;
2599 case FCMP_UNO: return FCMP_ORD;
2600 case FCMP_TRUE: return FCMP_FALSE;
2601 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002602 }
2603}
2604
Reid Spencer266e42b2006-12-23 06:05:41 +00002605ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2606 switch (pred) {
2607 default: assert(! "Unknown icmp predicate!");
2608 case ICMP_EQ: case ICMP_NE:
2609 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2610 return pred;
2611 case ICMP_UGT: return ICMP_SGT;
2612 case ICMP_ULT: return ICMP_SLT;
2613 case ICMP_UGE: return ICMP_SGE;
2614 case ICMP_ULE: return ICMP_SLE;
2615 }
2616}
2617
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002618ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2619 switch (pred) {
2620 default: assert(! "Unknown icmp predicate!");
2621 case ICMP_EQ: case ICMP_NE:
2622 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2623 return pred;
2624 case ICMP_SGT: return ICMP_UGT;
2625 case ICMP_SLT: return ICMP_ULT;
2626 case ICMP_SGE: return ICMP_UGE;
2627 case ICMP_SLE: return ICMP_ULE;
2628 }
2629}
2630
Reid Spencer266e42b2006-12-23 06:05:41 +00002631bool ICmpInst::isSignedPredicate(Predicate pred) {
2632 switch (pred) {
2633 default: assert(! "Unknown icmp predicate!");
2634 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2635 return true;
2636 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2637 case ICMP_UGE: case ICMP_ULE:
2638 return false;
2639 }
2640}
2641
Reid Spencer0286bc12007-02-28 22:00:54 +00002642/// Initialize a set of values that all satisfy the condition with C.
2643///
2644ConstantRange
2645ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2646 APInt Lower(C);
2647 APInt Upper(C);
2648 uint32_t BitWidth = C.getBitWidth();
2649 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002650 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002651 case ICmpInst::ICMP_EQ: Upper++; break;
2652 case ICmpInst::ICMP_NE: Lower++; break;
2653 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2654 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2655 case ICmpInst::ICMP_UGT:
2656 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2657 break;
2658 case ICmpInst::ICMP_SGT:
2659 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2660 break;
2661 case ICmpInst::ICMP_ULE:
2662 Lower = APInt::getMinValue(BitWidth); Upper++;
2663 break;
2664 case ICmpInst::ICMP_SLE:
2665 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2666 break;
2667 case ICmpInst::ICMP_UGE:
2668 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2669 break;
2670 case ICmpInst::ICMP_SGE:
2671 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2672 break;
2673 }
2674 return ConstantRange(Lower, Upper);
2675}
2676
Dan Gohman4e724382008-05-31 02:47:54 +00002677CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002678 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002679 default: assert(!"Unknown cmp predicate!");
2680 case ICMP_EQ: case ICMP_NE:
2681 return pred;
2682 case ICMP_SGT: return ICMP_SLT;
2683 case ICMP_SLT: return ICMP_SGT;
2684 case ICMP_SGE: return ICMP_SLE;
2685 case ICMP_SLE: return ICMP_SGE;
2686 case ICMP_UGT: return ICMP_ULT;
2687 case ICMP_ULT: return ICMP_UGT;
2688 case ICMP_UGE: return ICMP_ULE;
2689 case ICMP_ULE: return ICMP_UGE;
2690
Reid Spencerd9436b62006-11-20 01:22:35 +00002691 case FCMP_FALSE: case FCMP_TRUE:
2692 case FCMP_OEQ: case FCMP_ONE:
2693 case FCMP_UEQ: case FCMP_UNE:
2694 case FCMP_ORD: case FCMP_UNO:
2695 return pred;
2696 case FCMP_OGT: return FCMP_OLT;
2697 case FCMP_OLT: return FCMP_OGT;
2698 case FCMP_OGE: return FCMP_OLE;
2699 case FCMP_OLE: return FCMP_OGE;
2700 case FCMP_UGT: return FCMP_ULT;
2701 case FCMP_ULT: return FCMP_UGT;
2702 case FCMP_UGE: return FCMP_ULE;
2703 case FCMP_ULE: return FCMP_UGE;
2704 }
2705}
2706
Reid Spencer266e42b2006-12-23 06:05:41 +00002707bool CmpInst::isUnsigned(unsigned short predicate) {
2708 switch (predicate) {
2709 default: return false;
2710 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2711 case ICmpInst::ICMP_UGE: return true;
2712 }
2713}
2714
2715bool CmpInst::isSigned(unsigned short predicate){
2716 switch (predicate) {
2717 default: return false;
2718 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2719 case ICmpInst::ICMP_SGE: return true;
2720 }
2721}
2722
2723bool CmpInst::isOrdered(unsigned short predicate) {
2724 switch (predicate) {
2725 default: return false;
2726 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2727 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2728 case FCmpInst::FCMP_ORD: return true;
2729 }
2730}
2731
2732bool CmpInst::isUnordered(unsigned short predicate) {
2733 switch (predicate) {
2734 default: return false;
2735 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2736 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2737 case FCmpInst::FCMP_UNO: return true;
2738 }
2739}
2740
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002741//===----------------------------------------------------------------------===//
2742// SwitchInst Implementation
2743//===----------------------------------------------------------------------===//
2744
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002745void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002746 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002747 ReservedSpace = 2+NumCases*2;
2748 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002749 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002750
Gabor Greif2d3024d2008-05-26 21:33:52 +00002751 OperandList[0] = Value;
2752 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002753}
2754
Chris Lattner2195fc42007-02-24 00:55:48 +00002755/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2756/// switch on and a default destination. The number of additional cases can
2757/// be specified here to make memory allocation more efficient. This
2758/// constructor can also autoinsert before another instruction.
2759SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2760 Instruction *InsertBefore)
2761 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2762 init(Value, Default, NumCases);
2763}
2764
2765/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2766/// switch on and a default destination. The number of additional cases can
2767/// be specified here to make memory allocation more efficient. This
2768/// constructor also autoinserts at the end of the specified BasicBlock.
2769SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2770 BasicBlock *InsertAtEnd)
2771 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2772 init(Value, Default, NumCases);
2773}
2774
Misha Brukmanb1c93172005-04-21 23:48:37 +00002775SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002776 : TerminatorInst(Type::VoidTy, Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002777 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002778 Use *OL = OperandList, *InOL = SI.OperandList;
2779 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002780 OL[i] = InOL[i];
2781 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002782 }
2783}
2784
Gordon Henriksen14a55692007-12-10 02:14:30 +00002785SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002786 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002787}
2788
2789
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002790/// addCase - Add an entry to the switch instruction...
2791///
Chris Lattner47ac1872005-02-24 05:32:09 +00002792void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002793 unsigned OpNo = NumOperands;
2794 if (OpNo+2 > ReservedSpace)
2795 resizeOperands(0); // Get more space!
2796 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002797 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002798 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002799 OperandList[OpNo] = OnVal;
2800 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002801}
2802
2803/// removeCase - This method removes the specified successor from the switch
2804/// instruction. Note that this cannot be used to remove the default
2805/// destination (successor #0).
2806///
2807void SwitchInst::removeCase(unsigned idx) {
2808 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002809 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2810
2811 unsigned NumOps = getNumOperands();
2812 Use *OL = OperandList;
2813
2814 // Move everything after this operand down.
2815 //
2816 // FIXME: we could just swap with the end of the list, then erase. However,
2817 // client might not expect this to happen. The code as it is thrashes the
2818 // use/def lists, which is kinda lame.
2819 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2820 OL[i-2] = OL[i];
2821 OL[i-2+1] = OL[i+1];
2822 }
2823
2824 // Nuke the last value.
2825 OL[NumOps-2].set(0);
2826 OL[NumOps-2+1].set(0);
2827 NumOperands = NumOps-2;
2828}
2829
2830/// resizeOperands - resize operands - This adjusts the length of the operands
2831/// list according to the following behavior:
2832/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002833/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002834/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2835/// 3. If NumOps == NumOperands, trim the reserved space.
2836///
2837void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002838 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002839 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002840 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002841 } else if (NumOps*2 > NumOperands) {
2842 // No resize needed.
2843 if (ReservedSpace >= NumOps) return;
2844 } else if (NumOps == NumOperands) {
2845 if (ReservedSpace == NumOps) return;
2846 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002847 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002848 }
2849
2850 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002851 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002852 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002853 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002854 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002855 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002856 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002857 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002858}
2859
2860
2861BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2862 return getSuccessor(idx);
2863}
2864unsigned SwitchInst::getNumSuccessorsV() const {
2865 return getNumSuccessors();
2866}
2867void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2868 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002869}
Chris Lattnerf22be932004-10-15 23:52:53 +00002870
Chris Lattnerf22be932004-10-15 23:52:53 +00002871// Define these methods here so vtables don't get emitted into every translation
2872// unit that uses these classes.
2873
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002874GetElementPtrInst *GetElementPtrInst::clone(LLVMContext&) const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002875 return new(getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00002876}
2877
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002878BinaryOperator *BinaryOperator::clone(LLVMContext&) const {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002879 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00002880}
2881
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002882FCmpInst* FCmpInst::clone(LLVMContext &Context) const {
2883 return new FCmpInst(Context, getPredicate(), Op<0>(), Op<1>());
Chris Lattner0b490b02007-08-24 20:48:18 +00002884}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002885ICmpInst* ICmpInst::clone(LLVMContext &Context) const {
2886 return new ICmpInst(Context, getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00002887}
2888
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002889ExtractValueInst *ExtractValueInst::clone(LLVMContext&) const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002890 return new ExtractValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002891}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002892InsertValueInst *InsertValueInst::clone(LLVMContext&) const {
Dan Gohman1ecaf452008-05-31 00:58:22 +00002893 return new InsertValueInst(*this);
Dan Gohman0752bff2008-05-23 00:36:11 +00002894}
2895
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002896MallocInst *MallocInst::clone(LLVMContext&) const {
2897 return new MallocInst(*this);
2898}
Dan Gohman0752bff2008-05-23 00:36:11 +00002899
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002900AllocaInst *AllocaInst::clone(LLVMContext&) const {
2901 return new AllocaInst(*this);
2902}
2903
2904FreeInst *FreeInst::clone(LLVMContext&) const {
2905 return new FreeInst(getOperand(0));
2906}
2907
2908LoadInst *LoadInst::clone(LLVMContext&) const {
2909 return new LoadInst(*this);
2910}
2911
2912StoreInst *StoreInst::clone(LLVMContext&) const {
2913 return new StoreInst(*this);
2914}
2915
2916CastInst *TruncInst::clone(LLVMContext&) const {
2917 return new TruncInst(*this);
2918}
2919
2920CastInst *ZExtInst::clone(LLVMContext&) const {
2921 return new ZExtInst(*this);
2922}
2923
2924CastInst *SExtInst::clone(LLVMContext&) const {
2925 return new SExtInst(*this);
2926}
2927
2928CastInst *FPTruncInst::clone(LLVMContext&) const {
2929 return new FPTruncInst(*this);
2930}
2931
2932CastInst *FPExtInst::clone(LLVMContext&) const {
2933 return new FPExtInst(*this);
2934}
2935
2936CastInst *UIToFPInst::clone(LLVMContext&) const {
2937 return new UIToFPInst(*this);
2938}
2939
2940CastInst *SIToFPInst::clone(LLVMContext&) const {
2941 return new SIToFPInst(*this);
2942}
2943
2944CastInst *FPToUIInst::clone(LLVMContext&) const {
2945 return new FPToUIInst(*this);
2946}
2947
2948CastInst *FPToSIInst::clone(LLVMContext&) const {
2949 return new FPToSIInst(*this);
2950}
2951
2952CastInst *PtrToIntInst::clone(LLVMContext&) const {
2953 return new PtrToIntInst(*this);
2954}
2955
2956CastInst *IntToPtrInst::clone(LLVMContext&) const {
2957 return new IntToPtrInst(*this);
2958}
2959
2960CastInst *BitCastInst::clone(LLVMContext&) const {
2961 return new BitCastInst(*this);
2962}
2963
2964CallInst *CallInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00002965 return new(getNumOperands()) CallInst(*this);
2966}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002967
2968SelectInst *SelectInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00002969 return new(getNumOperands()) SelectInst(*this);
2970}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002971
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002972VAArgInst *VAArgInst::clone(LLVMContext&) const {
2973 return new VAArgInst(*this);
2974}
2975
2976ExtractElementInst *ExtractElementInst::clone(LLVMContext&) const {
Eric Christopherc9742252009-07-25 02:28:41 +00002977 return ExtractElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002978}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002979
2980InsertElementInst *InsertElementInst::clone(LLVMContext&) const {
Gabor Greife9ecc682008-04-06 20:25:17 +00002981 return InsertElementInst::Create(*this);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002982}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002983
2984ShuffleVectorInst *ShuffleVectorInst::clone(LLVMContext&) const {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002985 return new ShuffleVectorInst(*this);
2986}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002987
2988PHINode *PHINode::clone(LLVMContext&) const {
2989 return new PHINode(*this);
2990}
2991
2992ReturnInst *ReturnInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00002993 return new(getNumOperands()) ReturnInst(*this);
2994}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002995
2996BranchInst *BranchInst::clone(LLVMContext&) const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00002997 unsigned Ops(getNumOperands());
2998 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00002999}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003000
3001SwitchInst *SwitchInst::clone(LLVMContext&) const {
3002 return new SwitchInst(*this);
3003}
3004
3005InvokeInst *InvokeInst::clone(LLVMContext&) const {
Gabor Greif697e94c2008-05-15 10:04:30 +00003006 return new(getNumOperands()) InvokeInst(*this);
3007}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003008
3009UnwindInst *UnwindInst::clone(LLVMContext&) const {
3010 return new UnwindInst();
3011}
3012
3013UnreachableInst *UnreachableInst::clone(LLVMContext&) const {
3014 return new UnreachableInst();
3015}