blob: a03587107cca761b88b59231c375589a15032655 [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"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000019#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000020#include "llvm/Operator.h"
Dan Gohman22571482009-09-03 15:34:35 +000021#include "llvm/Analysis/Dominators.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000023#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000024#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000025#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000026using namespace llvm;
27
Chris Lattner3e13b8c2008-01-02 23:42:30 +000028//===----------------------------------------------------------------------===//
29// CallSite Class
30//===----------------------------------------------------------------------===//
31
Gabor Greif1b9921a2009-01-11 22:33:22 +000032#define CALLSITE_DELEGATE_GETTER(METHOD) \
33 Instruction *II(getInstruction()); \
34 return isCall() \
35 ? cast<CallInst>(II)->METHOD \
36 : cast<InvokeInst>(II)->METHOD
37
38#define CALLSITE_DELEGATE_SETTER(METHOD) \
39 Instruction *II(getInstruction()); \
40 if (isCall()) \
41 cast<CallInst>(II)->METHOD; \
42 else \
43 cast<InvokeInst>(II)->METHOD
44
Duncan Sands85fab3a2008-02-18 17:32:13 +000045CallSite::CallSite(Instruction *C) {
46 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
Gabor Greif1b9921a2009-01-11 22:33:22 +000047 I.setPointer(C);
48 I.setInt(isa<CallInst>(C));
Duncan Sands85fab3a2008-02-18 17:32:13 +000049}
Sandeep Patel68c5f472009-09-02 08:44:58 +000050CallingConv::ID CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000051 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000052}
Sandeep Patel68c5f472009-09-02 08:44:58 +000053void CallSite::setCallingConv(CallingConv::ID CC) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000054 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
Chris Lattnerf7b6d312005-05-06 20:26:43 +000055}
Devang Patel4c758ea2008-09-25 21:00:45 +000056const AttrListPtr &CallSite::getAttributes() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000057 CALLSITE_DELEGATE_GETTER(getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000058}
Devang Patel4c758ea2008-09-25 21:00:45 +000059void CallSite::setAttributes(const AttrListPtr &PAL) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000060 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000061}
Devang Patelba3fa6c2008-09-23 23:03:40 +000062bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000063 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
Duncan Sands5208d1a2007-11-28 17:07:01 +000064}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000065uint16_t CallSite::getParamAlignment(uint16_t i) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000066 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
Dale Johanneseneabc5f32008-02-22 17:49:45 +000067}
Duncan Sands38ef3a82007-12-03 20:06:50 +000068bool CallSite::doesNotAccessMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000069 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000070}
Duncan Sands78c88722008-07-08 08:38:44 +000071void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000072 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000073}
Duncan Sands38ef3a82007-12-03 20:06:50 +000074bool CallSite::onlyReadsMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000075 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000076}
Duncan Sands78c88722008-07-08 08:38:44 +000077void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000078 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000079}
80bool CallSite::doesNotReturn() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000081 CALLSITE_DELEGATE_GETTER(doesNotReturn());
Duncan Sands78c88722008-07-08 08:38:44 +000082}
83void CallSite::setDoesNotReturn(bool doesNotReturn) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000084 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
Duncan Sands78c88722008-07-08 08:38:44 +000085}
Duncan Sands3353ed02007-12-18 09:59:50 +000086bool CallSite::doesNotThrow() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000087 CALLSITE_DELEGATE_GETTER(doesNotThrow());
Duncan Sands8e4847e2007-12-16 15:51:49 +000088}
Duncan Sandsaa31b922007-12-19 21:13:37 +000089void CallSite::setDoesNotThrow(bool doesNotThrow) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000090 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
Duncan Sandsaa31b922007-12-19 21:13:37 +000091}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000092
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000093bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000094 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
95 if (AI->get() == Arg)
96 return true;
97 return false;
98}
99
Gabor Greif1b9921a2009-01-11 22:33:22 +0000100#undef CALLSITE_DELEGATE_GETTER
101#undef CALLSITE_DELEGATE_SETTER
102
Gordon Henriksen14a55692007-12-10 02:14:30 +0000103//===----------------------------------------------------------------------===//
104// TerminatorInst Class
105//===----------------------------------------------------------------------===//
106
107// Out of line virtual method, so the vtable, etc has a home.
108TerminatorInst::~TerminatorInst() {
109}
110
Gabor Greiff6caff662008-05-10 08:32:32 +0000111//===----------------------------------------------------------------------===//
112// UnaryInstruction Class
113//===----------------------------------------------------------------------===//
114
Gordon Henriksen14a55692007-12-10 02:14:30 +0000115// Out of line virtual method, so the vtable, etc has a home.
116UnaryInstruction::~UnaryInstruction() {
117}
118
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000119//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000120// SelectInst Class
121//===----------------------------------------------------------------------===//
122
123/// areInvalidOperands - Return a string if the specified operands are invalid
124/// for a select operation, otherwise return null.
125const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
126 if (Op1->getType() != Op2->getType())
127 return "both values to select must have same type";
128
129 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
130 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000131 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000132 return "vector select condition element type must be i1";
133 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
134 if (ET == 0)
135 return "selected values for vector select must be vectors";
136 if (ET->getNumElements() != VT->getNumElements())
137 return "vector select requires selected vectors to have "
138 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000139 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000140 return "select condition must be i1 or <n x i1>";
141 }
142 return 0;
143}
144
145
146//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000147// PHINode Class
148//===----------------------------------------------------------------------===//
149
150PHINode::PHINode(const PHINode &PN)
151 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000152 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000153 ReservedSpace(PN.getNumOperands()) {
154 Use *OL = OperandList;
155 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000156 OL[i] = PN.getOperand(i);
157 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000158 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000159 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000160}
161
Gordon Henriksen14a55692007-12-10 02:14:30 +0000162PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000163 if (OperandList)
164 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000165}
166
167// removeIncomingValue - Remove an incoming value. This is useful if a
168// predecessor basic block is deleted.
169Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
170 unsigned NumOps = getNumOperands();
171 Use *OL = OperandList;
172 assert(Idx*2 < NumOps && "BB not in PHI node!");
173 Value *Removed = OL[Idx*2];
174
175 // Move everything after this operand down.
176 //
177 // FIXME: we could just swap with the end of the list, then erase. However,
178 // client might not expect this to happen. The code as it is thrashes the
179 // use/def lists, which is kinda lame.
180 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
181 OL[i-2] = OL[i];
182 OL[i-2+1] = OL[i+1];
183 }
184
185 // Nuke the last value.
186 OL[NumOps-2].set(0);
187 OL[NumOps-2+1].set(0);
188 NumOperands = NumOps-2;
189
190 // If the PHI node is dead, because it has zero entries, nuke it now.
191 if (NumOps == 2 && DeletePHIIfEmpty) {
192 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000193 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000194 eraseFromParent();
195 }
196 return Removed;
197}
198
199/// resizeOperands - resize operands - This adjusts the length of the operands
200/// list according to the following behavior:
201/// 1. If NumOps == 0, grow the operand list in response to a push_back style
202/// of operation. This grows the number of ops by 1.5 times.
203/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
204/// 3. If NumOps == NumOperands, trim the reserved space.
205///
206void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000207 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000208 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000209 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000210 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
211 } else if (NumOps*2 > NumOperands) {
212 // No resize needed.
213 if (ReservedSpace >= NumOps) return;
214 } else if (NumOps == NumOperands) {
215 if (ReservedSpace == NumOps) return;
216 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000217 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000218 }
219
220 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000221 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000222 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000223 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000224 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000225 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000226}
227
Nate Begemanb3923212005-08-04 23:24:19 +0000228/// hasConstantValue - If the specified PHI node always merges together the same
229/// value, return the value, otherwise return null.
230///
Dan Gohman22571482009-09-03 15:34:35 +0000231/// If the PHI has undef operands, but all the rest of the operands are
232/// some unique value, return that value if it can be proved that the
233/// value dominates the PHI. If DT is null, use a conservative check,
234/// otherwise use DT to test for dominance.
235///
236Value *PHINode::hasConstantValue(DominatorTree *DT) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000237 // If the PHI node only has one incoming value, eliminate the PHI node...
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000238 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000239 if (getIncomingValue(0) != this) // not X = phi X
240 return getIncomingValue(0);
241 else
Owen Andersonb292b8c2009-07-30 23:03:37 +0000242 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000243 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000244
Nate Begemanb3923212005-08-04 23:24:19 +0000245 // Otherwise if all of the incoming values are the same for the PHI, replace
246 // the PHI node with the incoming value.
247 //
248 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000249 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000250 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000251 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000252 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000253 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000254 if (InVal && getIncomingValue(i) != InVal)
255 return 0; // Not the same, bail out.
256 else
257 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000258 }
Nate Begemanb3923212005-08-04 23:24:19 +0000259
260 // The only case that could cause InVal to be null is if we have a PHI node
261 // that only has entries for itself. In this case, there is no entry into the
262 // loop, so kill the PHI.
263 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000264 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000265
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000266 // If we have a PHI node like phi(X, undef, X), where X is defined by some
267 // instruction, we cannot always return X as the result of the PHI node. Only
268 // do this if X is not an instruction (thus it must dominate the PHI block),
269 // or if the client is prepared to deal with this possibility.
Dan Gohman22571482009-09-03 15:34:35 +0000270 if (HasUndefInput)
271 if (Instruction *IV = dyn_cast<Instruction>(InVal)) {
272 if (DT) {
273 // We have a DominatorTree. Do a precise test.
274 if (!DT->dominates(IV, this))
275 return 0;
276 } else {
277 // If it's in the entry block, it dominates everything.
278 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
279 isa<InvokeInst>(IV))
280 return 0; // Cannot guarantee that InVal dominates this PHINode.
281 }
282 }
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000283
Nate Begemanb3923212005-08-04 23:24:19 +0000284 // All of the incoming values are the same, return the value now.
285 return InVal;
286}
287
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000288
289//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290// CallInst Implementation
291//===----------------------------------------------------------------------===//
292
Gordon Henriksen14a55692007-12-10 02:14:30 +0000293CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000294}
295
Chris Lattner054ba2c2007-02-13 00:58:44 +0000296void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000297 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
298 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000299 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300
Misha Brukmanb1c93172005-04-21 23:48:37 +0000301 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000303 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304
Chris Lattner054ba2c2007-02-13 00:58:44 +0000305 assert((NumParams == FTy->getNumParams() ||
306 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000307 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000308 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000309 assert((i >= FTy->getNumParams() ||
310 FTy->getParamType(i) == Params[i]->getType()) &&
311 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000312 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000313 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314}
315
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000316void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000317 assert(NumOperands == 3 && "NumOperands not set up?");
318 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000319 OL[0] = Func;
320 OL[1] = Actual1;
321 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000322
323 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000325 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000326
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000327 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000328 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000330 assert((0 >= FTy->getNumParams() ||
331 FTy->getParamType(0) == Actual1->getType()) &&
332 "Calling a function with a bad signature!");
333 assert((1 >= FTy->getNumParams() ||
334 FTy->getParamType(1) == Actual2->getType()) &&
335 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336}
337
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000338void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000339 assert(NumOperands == 2 && "NumOperands not set up?");
340 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000341 OL[0] = Func;
342 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000343
344 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000345 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000346 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000347
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000348 assert((FTy->getNumParams() == 1 ||
349 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000351 assert((0 == FTy->getNumParams() ||
352 FTy->getParamType(0) == Actual->getType()) &&
353 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000354}
355
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000356void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000357 assert(NumOperands == 1 && "NumOperands not set up?");
358 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000359 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000360
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000361 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000363 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000364
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000365 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000366}
367
Daniel Dunbar4975db62009-07-25 04:41:11 +0000368CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000369 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000370 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
371 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000372 Instruction::Call,
373 OperandTraits<CallInst>::op_end(this) - 2,
374 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000375 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000376 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000377}
378
Daniel Dunbar4975db62009-07-25 04:41:11 +0000379CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000380 BasicBlock *InsertAtEnd)
381 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
382 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000383 Instruction::Call,
384 OperandTraits<CallInst>::op_end(this) - 2,
385 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000386 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000387 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000388}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000389CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000390 Instruction *InsertBefore)
391 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
392 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000393 Instruction::Call,
394 OperandTraits<CallInst>::op_end(this) - 1,
395 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000396 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000397 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000398}
399
Daniel Dunbar4975db62009-07-25 04:41:11 +0000400CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000401 BasicBlock *InsertAtEnd)
402 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
403 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000404 Instruction::Call,
405 OperandTraits<CallInst>::op_end(this) - 1,
406 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000407 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000408 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000409}
410
Misha Brukmanb1c93172005-04-21 23:48:37 +0000411CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000412 : Instruction(CI.getType(), Instruction::Call,
413 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000414 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000415 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000416 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000417 Use *OL = OperandList;
418 Use *InOL = CI.OperandList;
419 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000420 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000421 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000422}
423
Devang Patel4c758ea2008-09-25 21:00:45 +0000424void CallInst::addAttribute(unsigned i, Attributes attr) {
425 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000426 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000427 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000428}
429
Devang Patel4c758ea2008-09-25 21:00:45 +0000430void CallInst::removeAttribute(unsigned i, Attributes attr) {
431 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000432 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000433 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000434}
435
Devang Patelba3fa6c2008-09-23 23:03:40 +0000436bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000437 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000438 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000439 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000440 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000441 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000442}
443
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000444/// IsConstantOne - Return true only if val is constant int 1
445static bool IsConstantOne(Value *val) {
446 assert(val && "IsConstantOne does not work with NULL val");
447 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
448}
449
450static Value *checkArraySize(Value *Amt, const Type *IntPtrTy) {
451 if (!Amt)
452 Amt = ConstantInt::get(IntPtrTy, 1);
453 else {
454 assert(!isa<BasicBlock>(Amt) &&
455 "Passed basic block into malloc size parameter! Use other ctor");
456 assert(Amt->getType() == IntPtrTy &&
457 "Malloc array size is not an intptr!");
458 }
459 return Amt;
460}
461
462static Value *createMalloc(Instruction *InsertBefore, BasicBlock *InsertAtEnd,
463 const Type *AllocTy, const Type *IntPtrTy,
464 Value *ArraySize, const Twine &NameStr) {
465 assert((!InsertBefore && InsertAtEnd || InsertBefore && !InsertAtEnd) &&
466 "createMalloc needs only InsertBefore or InsertAtEnd");
467 const PointerType *AllocPtrType = dyn_cast<PointerType>(AllocTy);
468 assert(AllocPtrType && "CreateMalloc passed a non-pointer allocation type");
469
470 ArraySize = checkArraySize(ArraySize, IntPtrTy);
471
472 // malloc(type) becomes i8 *malloc(size)
473 Value *AllocSize = ConstantExpr::getSizeOf(AllocPtrType->getElementType());
474 AllocSize = ConstantExpr::getTruncOrBitCast(cast<Constant>(AllocSize),
475 IntPtrTy);
476 if (!IsConstantOne(ArraySize))
477 if (IsConstantOne(AllocSize)) {
478 AllocSize = ArraySize; // Operand * 1 = Operand
479 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
480 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
481 false /*ZExt*/);
482 // Malloc arg is constant product of type size and array size
483 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
484 } else {
485 Value *Scale = ArraySize;
486 if (Scale->getType() != IntPtrTy)
487 if (InsertBefore)
488 Scale = CastInst::CreateIntegerCast(Scale, IntPtrTy, false /*ZExt*/,
489 "", InsertBefore);
490 else
491 Scale = CastInst::CreateIntegerCast(Scale, IntPtrTy, false /*ZExt*/,
492 "", InsertAtEnd);
493 // Multiply type size by the array size...
494 if (InsertBefore)
495 AllocSize = BinaryOperator::CreateMul(Scale, AllocSize,
496 "", InsertBefore);
497 else
498 AllocSize = BinaryOperator::CreateMul(Scale, AllocSize,
499 "", InsertAtEnd);
500 }
501
502 // Create the call to Malloc.
503 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
504 Module* M = BB->getParent()->getParent();
505 const Type *BPTy = PointerType::getUnqual(Type::getInt8Ty(BB->getContext()));
506 // prototype malloc as "void *malloc(size_t)"
507 Constant *MallocFunc = M->getOrInsertFunction("malloc", BPTy,
508 IntPtrTy, NULL);
509 CallInst *MCall = NULL;
510 if (InsertBefore)
511 MCall = CallInst::Create(MallocFunc, AllocSize, NameStr, InsertBefore);
512 else
513 MCall = CallInst::Create(MallocFunc, AllocSize, NameStr, InsertAtEnd);
514 MCall->setTailCall();
515
516 // Create a cast instruction to convert to the right type...
517 const Type* VoidT = Type::getVoidTy(BB->getContext());
518 assert(MCall->getType() != VoidT && "Malloc has void return type");
519 Value *MCast;
520 if (InsertBefore)
521 MCast = new BitCastInst(MCall, AllocPtrType, NameStr, InsertBefore);
522 else
523 MCast = new BitCastInst(MCall, AllocPtrType, NameStr);
524 return MCast;
525}
526
527/// CreateMalloc - Generate the IR for a call to malloc:
528/// 1. Compute the malloc call's argument as the specified type's size,
529/// possibly multiplied by the array size if the array size is not
530/// constant 1.
531/// 2. Call malloc with that argument.
532/// 3. Bitcast the result of the malloc call to the specified type.
533Value *CallInst::CreateMalloc(Instruction *InsertBefore,
534 const Type *AllocTy, const Type *IntPtrTy,
535 Value *ArraySize, const Twine &NameStr) {
536 return createMalloc(InsertBefore, NULL, AllocTy,
537 IntPtrTy, ArraySize, NameStr);
538}
539
540/// CreateMalloc - Generate the IR for a call to malloc:
541/// 1. Compute the malloc call's argument as the specified type's size,
542/// possibly multiplied by the array size if the array size is not
543/// constant 1.
544/// 2. Call malloc with that argument.
545/// 3. Bitcast the result of the malloc call to the specified type.
546/// Note: This function does not add the bitcast to the basic block, that is the
547/// responsibility of the caller.
548Value *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
549 const Type *AllocTy, const Type *IntPtrTy,
550 Value *ArraySize, const Twine &NameStr) {
551 return createMalloc(NULL, InsertAtEnd, AllocTy,
552 IntPtrTy, ArraySize, NameStr);
553}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000554
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000555//===----------------------------------------------------------------------===//
556// InvokeInst Implementation
557//===----------------------------------------------------------------------===//
558
559void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000560 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000561 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000562 Use *OL = OperandList;
563 OL[0] = Fn;
564 OL[1] = IfNormal;
565 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000566 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000567 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000568 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000569
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000570 assert(((NumArgs == FTy->getNumParams()) ||
571 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000572 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000573
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000574 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000575 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000576 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000577 "Invoking a function with a bad signature!");
578
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000579 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000580 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000581}
582
Misha Brukmanb1c93172005-04-21 23:48:37 +0000583InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000584 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000585 OperandTraits<InvokeInst>::op_end(this)
586 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000587 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000588 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000589 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000590 Use *OL = OperandList, *InOL = II.OperandList;
591 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000592 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000593 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000594}
595
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
597 return getSuccessor(idx);
598}
599unsigned InvokeInst::getNumSuccessorsV() const {
600 return getNumSuccessors();
601}
602void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
603 return setSuccessor(idx, B);
604}
605
Devang Patelba3fa6c2008-09-23 23:03:40 +0000606bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000607 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000608 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000609 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000610 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000611 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000612}
613
Devang Patel4c758ea2008-09-25 21:00:45 +0000614void InvokeInst::addAttribute(unsigned i, Attributes attr) {
615 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000616 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000617 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000618}
619
Devang Patel4c758ea2008-09-25 21:00:45 +0000620void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
621 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000622 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000623 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000624}
625
Duncan Sands5208d1a2007-11-28 17:07:01 +0000626
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000627//===----------------------------------------------------------------------===//
628// ReturnInst Implementation
629//===----------------------------------------------------------------------===//
630
Chris Lattner2195fc42007-02-24 00:55:48 +0000631ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000632 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000633 OperandTraits<ReturnInst>::op_end(this) -
634 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000635 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000636 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000637 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000638 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000639}
640
Owen Anderson55f1c092009-08-13 21:58:54 +0000641ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
642 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000643 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
644 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000645 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000646 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000647}
Owen Anderson55f1c092009-08-13 21:58:54 +0000648ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
649 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000650 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
651 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000652 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000653 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000654}
Owen Anderson55f1c092009-08-13 21:58:54 +0000655ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
656 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000657 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000658}
659
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000660unsigned ReturnInst::getNumSuccessorsV() const {
661 return getNumSuccessors();
662}
663
Devang Patelae682fb2008-02-26 17:56:20 +0000664/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
665/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000666void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000667 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000668}
669
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000670BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000671 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000672 return 0;
673}
674
Devang Patel59643e52008-02-23 00:35:18 +0000675ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000676}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000677
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000678//===----------------------------------------------------------------------===//
679// UnwindInst Implementation
680//===----------------------------------------------------------------------===//
681
Owen Anderson55f1c092009-08-13 21:58:54 +0000682UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
683 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
684 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000685}
Owen Anderson55f1c092009-08-13 21:58:54 +0000686UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
687 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
688 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000689}
690
691
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000692unsigned UnwindInst::getNumSuccessorsV() const {
693 return getNumSuccessors();
694}
695
696void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000697 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000698}
699
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000700BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000701 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000702 return 0;
703}
704
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000705//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000706// UnreachableInst Implementation
707//===----------------------------------------------------------------------===//
708
Owen Anderson55f1c092009-08-13 21:58:54 +0000709UnreachableInst::UnreachableInst(LLVMContext &Context,
710 Instruction *InsertBefore)
711 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
712 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000713}
Owen Anderson55f1c092009-08-13 21:58:54 +0000714UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
715 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
716 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000717}
718
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000719unsigned UnreachableInst::getNumSuccessorsV() const {
720 return getNumSuccessors();
721}
722
723void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000724 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000725}
726
727BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000728 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000729 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000730}
731
732//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733// BranchInst Implementation
734//===----------------------------------------------------------------------===//
735
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000736void BranchInst::AssertOK() {
737 if (isConditional())
Owen Anderson55f1c092009-08-13 21:58:54 +0000738 assert(getCondition()->getType() == Type::getInt1Ty(getContext()) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000739 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000740}
741
Chris Lattner2195fc42007-02-24 00:55:48 +0000742BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000743 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000744 OperandTraits<BranchInst>::op_end(this) - 1,
745 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000746 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000747 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000748}
749BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
750 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000751 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000752 OperandTraits<BranchInst>::op_end(this) - 3,
753 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000754 Op<-1>() = IfTrue;
755 Op<-2>() = IfFalse;
756 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000757#ifndef NDEBUG
758 AssertOK();
759#endif
760}
761
762BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000763 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000764 OperandTraits<BranchInst>::op_end(this) - 1,
765 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000766 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000767 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000768}
769
770BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
771 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000772 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000773 OperandTraits<BranchInst>::op_end(this) - 3,
774 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000775 Op<-1>() = IfTrue;
776 Op<-2>() = IfFalse;
777 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000778#ifndef NDEBUG
779 AssertOK();
780#endif
781}
782
783
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000784BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000785 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000786 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
787 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000788 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000789 if (BI.getNumOperands() != 1) {
790 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000791 Op<-3>() = BI.Op<-3>();
792 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000793 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000794 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000795}
796
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000797
798Use* Use::getPrefix() {
799 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
800 if (PotentialPrefix.getOpaqueValue())
801 return 0;
802
803 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
804}
805
806BranchInst::~BranchInst() {
807 if (NumOperands == 1) {
808 if (Use *Prefix = OperandList->getPrefix()) {
809 Op<-1>() = 0;
810 //
811 // mark OperandList to have a special value for scrutiny
812 // by baseclass destructors and operator delete
813 OperandList = Prefix;
814 } else {
815 NumOperands = 3;
816 OperandList = op_begin();
817 }
818 }
819}
820
821
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000822BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
823 return getSuccessor(idx);
824}
825unsigned BranchInst::getNumSuccessorsV() const {
826 return getNumSuccessors();
827}
828void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
829 setSuccessor(idx, B);
830}
831
832
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000833//===----------------------------------------------------------------------===//
834// AllocationInst Implementation
835//===----------------------------------------------------------------------===//
836
Owen Andersonb6b25302009-07-14 23:09:55 +0000837static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000838 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000839 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000840 else {
841 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000842 "Passed basic block into allocation size parameter! Use other ctor");
Owen Anderson55f1c092009-08-13 21:58:54 +0000843 assert(Amt->getType() == Type::getInt32Ty(Context) &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000844 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000845 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000846 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000847}
848
Owen Anderson4fdeba92009-07-15 23:53:25 +0000849AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000850 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000851 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +0000852 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000853 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000854 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000855 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000856 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000857}
858
Owen Anderson4fdeba92009-07-15 23:53:25 +0000859AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000860 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000861 BasicBlock *InsertAtEnd)
Owen Anderson4056ca92009-07-29 22:17:13 +0000862 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000863 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000864 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000865 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000866 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000867}
868
Gordon Henriksen14a55692007-12-10 02:14:30 +0000869// Out of line virtual method, so the vtable, etc has a home.
870AllocationInst::~AllocationInst() {
871}
872
Dan Gohmanaa583d72008-03-24 16:55:58 +0000873void AllocationInst::setAlignment(unsigned Align) {
874 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
875 SubclassData = Log2_32(Align) + 1;
876 assert(getAlignment() == Align && "Alignment representation error!");
877}
878
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000879bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000880 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
881 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000882 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000883}
884
885const Type *AllocationInst::getAllocatedType() const {
886 return getType()->getElementType();
887}
888
Chris Lattner8b291e62008-11-26 02:54:17 +0000889/// isStaticAlloca - Return true if this alloca is in the entry block of the
890/// function and is a constant size. If so, the code generator will fold it
891/// into the prolog/epilog code, so it is basically free.
892bool AllocaInst::isStaticAlloca() const {
893 // Must be constant size.
894 if (!isa<ConstantInt>(getArraySize())) return false;
895
896 // Must be in the entry block.
897 const BasicBlock *Parent = getParent();
898 return Parent == &Parent->getParent()->front();
899}
900
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000901//===----------------------------------------------------------------------===//
902// FreeInst Implementation
903//===----------------------------------------------------------------------===//
904
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000905void FreeInst::AssertOK() {
906 assert(isa<PointerType>(getOperand(0)->getType()) &&
907 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000908}
909
910FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000911 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
912 Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000913 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000914}
915
916FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000917 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
918 Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000919 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000920}
921
922
923//===----------------------------------------------------------------------===//
924// LoadInst Implementation
925//===----------------------------------------------------------------------===//
926
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000927void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000928 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000929 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000930}
931
Daniel Dunbar4975db62009-07-25 04:41:11 +0000932LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000933 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000934 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000935 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000936 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000937 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000938 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000939}
940
Daniel Dunbar4975db62009-07-25 04:41:11 +0000941LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000942 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000943 Load, Ptr, InsertAE) {
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();
Chris Lattner0f048162007-02-13 07:54:42 +0000947 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000948}
949
Daniel Dunbar4975db62009-07-25 04:41:11 +0000950LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000951 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000952 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000953 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000954 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000955 setAlignment(0);
956 AssertOK();
957 setName(Name);
958}
959
Daniel Dunbar4975db62009-07-25 04:41:11 +0000960LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000961 unsigned Align, Instruction *InsertBef)
962 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
963 Load, Ptr, InsertBef) {
964 setVolatile(isVolatile);
965 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000966 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000967 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000968}
969
Daniel Dunbar4975db62009-07-25 04:41:11 +0000970LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000971 unsigned Align, BasicBlock *InsertAE)
972 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
973 Load, Ptr, InsertAE) {
974 setVolatile(isVolatile);
975 setAlignment(Align);
976 AssertOK();
977 setName(Name);
978}
979
Daniel Dunbar4975db62009-07-25 04:41:11 +0000980LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000981 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000982 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000983 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000984 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000985 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000986 AssertOK();
987 setName(Name);
988}
989
Daniel Dunbar27096822009-08-11 18:11:15 +0000990
991
992LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
993 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
994 Load, Ptr, InsertBef) {
995 setVolatile(false);
996 setAlignment(0);
997 AssertOK();
998 if (Name && Name[0]) setName(Name);
999}
1000
1001LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1002 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1003 Load, Ptr, InsertAE) {
1004 setVolatile(false);
1005 setAlignment(0);
1006 AssertOK();
1007 if (Name && Name[0]) setName(Name);
1008}
1009
1010LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1011 Instruction *InsertBef)
1012: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1013 Load, Ptr, InsertBef) {
1014 setVolatile(isVolatile);
1015 setAlignment(0);
1016 AssertOK();
1017 if (Name && Name[0]) setName(Name);
1018}
1019
1020LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1021 BasicBlock *InsertAE)
1022 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1023 Load, Ptr, InsertAE) {
1024 setVolatile(isVolatile);
1025 setAlignment(0);
1026 AssertOK();
1027 if (Name && Name[0]) setName(Name);
1028}
1029
Christopher Lamb84485702007-04-22 19:24:39 +00001030void LoadInst::setAlignment(unsigned Align) {
1031 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1032 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1033}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001034
1035//===----------------------------------------------------------------------===//
1036// StoreInst Implementation
1037//===----------------------------------------------------------------------===//
1038
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001039void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001040 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001041 assert(isa<PointerType>(getOperand(1)->getType()) &&
1042 "Ptr must have pointer type!");
1043 assert(getOperand(0)->getType() ==
1044 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001045 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001046}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001047
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001048
1049StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001050 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001051 OperandTraits<StoreInst>::op_begin(this),
1052 OperandTraits<StoreInst>::operands(this),
1053 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001054 Op<0>() = val;
1055 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001056 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001057 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001058 AssertOK();
1059}
1060
1061StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001062 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001063 OperandTraits<StoreInst>::op_begin(this),
1064 OperandTraits<StoreInst>::operands(this),
1065 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001066 Op<0>() = val;
1067 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001068 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001069 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001070 AssertOK();
1071}
1072
Misha Brukmanb1c93172005-04-21 23:48:37 +00001073StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001074 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001075 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001076 OperandTraits<StoreInst>::op_begin(this),
1077 OperandTraits<StoreInst>::operands(this),
1078 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001079 Op<0>() = val;
1080 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001081 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001082 setAlignment(0);
1083 AssertOK();
1084}
1085
1086StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1087 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001088 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001089 OperandTraits<StoreInst>::op_begin(this),
1090 OperandTraits<StoreInst>::operands(this),
1091 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001092 Op<0>() = val;
1093 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001094 setVolatile(isVolatile);
1095 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001096 AssertOK();
1097}
1098
Misha Brukmanb1c93172005-04-21 23:48:37 +00001099StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001100 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001101 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001102 OperandTraits<StoreInst>::op_begin(this),
1103 OperandTraits<StoreInst>::operands(this),
1104 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001105 Op<0>() = val;
1106 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001107 setVolatile(isVolatile);
1108 setAlignment(Align);
1109 AssertOK();
1110}
1111
1112StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001113 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001114 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001115 OperandTraits<StoreInst>::op_begin(this),
1116 OperandTraits<StoreInst>::operands(this),
1117 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001118 Op<0>() = val;
1119 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001120 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001121 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001122 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001123}
1124
Christopher Lamb84485702007-04-22 19:24:39 +00001125void StoreInst::setAlignment(unsigned Align) {
1126 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1127 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1128}
1129
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001130//===----------------------------------------------------------------------===//
1131// GetElementPtrInst Implementation
1132//===----------------------------------------------------------------------===//
1133
Christopher Lambedf07882007-12-17 01:12:55 +00001134static unsigned retrieveAddrSpace(const Value *Val) {
1135 return cast<PointerType>(Val->getType())->getAddressSpace();
1136}
1137
Gabor Greif21ba1842008-06-06 20:28:12 +00001138void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001139 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001140 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1141 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001142 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001143
Chris Lattner79807c3d2007-01-31 19:47:18 +00001144 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001145 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001146
1147 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001148}
1149
Daniel Dunbar4975db62009-07-25 04:41:11 +00001150void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001151 assert(NumOperands == 2 && "NumOperands not initialized?");
1152 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001153 OL[0] = Ptr;
1154 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001155
1156 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001157}
1158
Gabor Greiff6caff662008-05-10 08:32:32 +00001159GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001160 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001161 OperandTraits<GetElementPtrInst>::op_end(this)
1162 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001163 GEPI.getNumOperands()) {
1164 Use *OL = OperandList;
1165 Use *GEPIOL = GEPI.OperandList;
1166 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001167 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001168 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001169}
1170
Chris Lattner82981202005-05-03 05:43:30 +00001171GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001172 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001173 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001174 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001175 GetElementPtr,
1176 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1177 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001178 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001179}
1180
1181GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001182 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001183 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001184 checkType(getIndexedType(Ptr->getType(),Idx)),
1185 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001186 GetElementPtr,
1187 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1188 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001189 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001190}
1191
Chris Lattner6090a422009-03-09 04:46:40 +00001192/// getIndexedType - Returns the type of the element that would be accessed with
1193/// a gep instruction with the specified parameters.
1194///
1195/// The Idxs pointer should point to a continuous piece of memory containing the
1196/// indices, either as Value* or uint64_t.
1197///
1198/// A null type is returned if the indices are invalid for the specified
1199/// pointer type.
1200///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001201template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001202static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1203 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001204 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1205 if (!PTy) return 0; // Type isn't a pointer type!
1206 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001207
Chris Lattner6090a422009-03-09 04:46:40 +00001208 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001209 if (NumIdx == 0)
1210 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001211
1212 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001213 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1214 // that contain opaque types) under the assumption that it will be resolved to
1215 // a sane type later.
1216 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001217 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001218
Dan Gohman1ecaf452008-05-31 00:58:22 +00001219 unsigned CurIdx = 1;
1220 for (; CurIdx != NumIdx; ++CurIdx) {
1221 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1222 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001223 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001224 if (!CT->indexValid(Index)) return 0;
1225 Agg = CT->getTypeAtIndex(Index);
1226
1227 // If the new type forwards to another type, then it is in the middle
1228 // of being refined to another type (and hence, may have dropped all
1229 // references to what it was using before). So, use the new forwarded
1230 // type.
1231 if (const Type *Ty = Agg->getForwardedType())
1232 Agg = Ty;
1233 }
1234 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001235}
1236
Matthijs Kooijman04468622008-07-29 08:46:11 +00001237const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1238 Value* const *Idxs,
1239 unsigned NumIdx) {
1240 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1241}
1242
1243const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1244 uint64_t const *Idxs,
1245 unsigned NumIdx) {
1246 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1247}
1248
Chris Lattner82981202005-05-03 05:43:30 +00001249const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1250 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1251 if (!PTy) return 0; // Type isn't a pointer type!
1252
1253 // Check the pointer index.
1254 if (!PTy->indexValid(Idx)) return 0;
1255
Chris Lattnerc2233332005-05-03 16:44:45 +00001256 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001257}
1258
Chris Lattner45f15572007-04-14 00:12:57 +00001259
1260/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1261/// zeros. If so, the result pointer and the first operand have the same
1262/// value, just potentially different types.
1263bool GetElementPtrInst::hasAllZeroIndices() const {
1264 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1265 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1266 if (!CI->isZero()) return false;
1267 } else {
1268 return false;
1269 }
1270 }
1271 return true;
1272}
1273
Chris Lattner27058292007-04-27 20:35:56 +00001274/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1275/// constant integers. If so, the result pointer and the first operand have
1276/// a constant offset between them.
1277bool GetElementPtrInst::hasAllConstantIndices() const {
1278 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1279 if (!isa<ConstantInt>(getOperand(i)))
1280 return false;
1281 }
1282 return true;
1283}
1284
Dan Gohman1b849082009-09-07 23:54:19 +00001285void GetElementPtrInst::setIsInBounds(bool B) {
1286 cast<GEPOperator>(this)->setIsInBounds(B);
1287}
Chris Lattner45f15572007-04-14 00:12:57 +00001288
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001289//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001290// ExtractElementInst Implementation
1291//===----------------------------------------------------------------------===//
1292
1293ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001294 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001295 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001296 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001297 ExtractElement,
1298 OperandTraits<ExtractElementInst>::op_begin(this),
1299 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001300 assert(isValidOperands(Val, Index) &&
1301 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001302 Op<0>() = Val;
1303 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001304 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001305}
1306
1307ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001308 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001309 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001310 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001311 ExtractElement,
1312 OperandTraits<ExtractElementInst>::op_begin(this),
1313 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001314 assert(isValidOperands(Val, Index) &&
1315 "Invalid extractelement instruction operands!");
1316
Gabor Greif2d3024d2008-05-26 21:33:52 +00001317 Op<0>() = Val;
1318 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001319 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001320}
1321
Chris Lattner65511ff2006-10-05 06:24:58 +00001322
Chris Lattner54865b32006-04-08 04:05:48 +00001323bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Owen Anderson55f1c092009-08-13 21:58:54 +00001324 if (!isa<VectorType>(Val->getType()) ||
1325 Index->getType() != Type::getInt32Ty(Val->getContext()))
Chris Lattner54865b32006-04-08 04:05:48 +00001326 return false;
1327 return true;
1328}
1329
1330
Robert Bocchino23004482006-01-10 19:05:34 +00001331//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001332// InsertElementInst Implementation
1333//===----------------------------------------------------------------------===//
1334
Chris Lattner54865b32006-04-08 04:05:48 +00001335InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001336 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001337 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001338 : Instruction(Vec->getType(), InsertElement,
1339 OperandTraits<InsertElementInst>::op_begin(this),
1340 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001341 assert(isValidOperands(Vec, Elt, Index) &&
1342 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001343 Op<0>() = Vec;
1344 Op<1>() = Elt;
1345 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001346 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001347}
1348
Chris Lattner54865b32006-04-08 04:05:48 +00001349InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001350 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001351 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001352 : Instruction(Vec->getType(), InsertElement,
1353 OperandTraits<InsertElementInst>::op_begin(this),
1354 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001355 assert(isValidOperands(Vec, Elt, Index) &&
1356 "Invalid insertelement instruction operands!");
1357
Gabor Greif2d3024d2008-05-26 21:33:52 +00001358 Op<0>() = Vec;
1359 Op<1>() = Elt;
1360 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001361 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001362}
1363
Chris Lattner54865b32006-04-08 04:05:48 +00001364bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1365 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001366 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001367 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001368
Reid Spencerd84d35b2007-02-15 02:26:10 +00001369 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001370 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001371
Owen Anderson55f1c092009-08-13 21:58:54 +00001372 if (Index->getType() != Type::getInt32Ty(Vec->getContext()))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001373 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001374 return true;
1375}
1376
1377
Robert Bocchinoca27f032006-01-17 20:07:22 +00001378//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001379// ShuffleVectorInst Implementation
1380//===----------------------------------------------------------------------===//
1381
1382ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001383 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001384 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001385: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001386 cast<VectorType>(Mask->getType())->getNumElements()),
1387 ShuffleVector,
1388 OperandTraits<ShuffleVectorInst>::op_begin(this),
1389 OperandTraits<ShuffleVectorInst>::operands(this),
1390 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001391 assert(isValidOperands(V1, V2, Mask) &&
1392 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001393 Op<0>() = V1;
1394 Op<1>() = V2;
1395 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001396 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001397}
1398
1399ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001400 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001401 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001402: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1403 cast<VectorType>(Mask->getType())->getNumElements()),
1404 ShuffleVector,
1405 OperandTraits<ShuffleVectorInst>::op_begin(this),
1406 OperandTraits<ShuffleVectorInst>::operands(this),
1407 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001408 assert(isValidOperands(V1, V2, Mask) &&
1409 "Invalid shuffle vector instruction operands!");
1410
Gabor Greif2d3024d2008-05-26 21:33:52 +00001411 Op<0>() = V1;
1412 Op<1>() = V2;
1413 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001414 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001415}
1416
Mon P Wang25f01062008-11-10 04:46:22 +00001417bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001418 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001419 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001420 return false;
1421
1422 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1423 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Owen Anderson55f1c092009-08-13 21:58:54 +00001424 MaskTy->getElementType() != Type::getInt32Ty(V1->getContext()))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001425 return false;
1426 return true;
1427}
1428
Chris Lattnerf724e342008-03-02 05:28:33 +00001429/// getMaskValue - Return the index from the shuffle mask for the specified
1430/// output result. This is either -1 if the element is undef or a number less
1431/// than 2*numelements.
1432int ShuffleVectorInst::getMaskValue(unsigned i) const {
1433 const Constant *Mask = cast<Constant>(getOperand(2));
1434 if (isa<UndefValue>(Mask)) return -1;
1435 if (isa<ConstantAggregateZero>(Mask)) return 0;
1436 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1437 assert(i < MaskCV->getNumOperands() && "Index out of range");
1438
1439 if (isa<UndefValue>(MaskCV->getOperand(i)))
1440 return -1;
1441 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1442}
1443
Dan Gohman12fce772008-05-15 19:50:34 +00001444//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001445// InsertValueInst Class
1446//===----------------------------------------------------------------------===//
1447
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001448void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001449 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001450 assert(NumOperands == 2 && "NumOperands not initialized?");
1451 Op<0>() = Agg;
1452 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001453
Dan Gohman1ecaf452008-05-31 00:58:22 +00001454 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001455 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001456}
1457
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001458void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001459 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001460 assert(NumOperands == 2 && "NumOperands not initialized?");
1461 Op<0>() = Agg;
1462 Op<1>() = Val;
1463
1464 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001465 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001466}
1467
1468InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001469 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001470 OperandTraits<InsertValueInst>::op_begin(this), 2),
1471 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001472 Op<0>() = IVI.getOperand(0);
1473 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001474 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001475}
1476
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001477InsertValueInst::InsertValueInst(Value *Agg,
1478 Value *Val,
1479 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001480 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001481 Instruction *InsertBefore)
1482 : Instruction(Agg->getType(), InsertValue,
1483 OperandTraits<InsertValueInst>::op_begin(this),
1484 2, InsertBefore) {
1485 init(Agg, Val, Idx, Name);
1486}
1487
1488InsertValueInst::InsertValueInst(Value *Agg,
1489 Value *Val,
1490 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001491 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001492 BasicBlock *InsertAtEnd)
1493 : Instruction(Agg->getType(), InsertValue,
1494 OperandTraits<InsertValueInst>::op_begin(this),
1495 2, InsertAtEnd) {
1496 init(Agg, Val, Idx, Name);
1497}
1498
Dan Gohman0752bff2008-05-23 00:36:11 +00001499//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001500// ExtractValueInst Class
1501//===----------------------------------------------------------------------===//
1502
Gabor Greifcbcc4952008-06-06 21:06:32 +00001503void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001504 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001505 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001506
Dan Gohman1ecaf452008-05-31 00:58:22 +00001507 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001508 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001509}
1510
Daniel Dunbar4975db62009-07-25 04:41:11 +00001511void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001512 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001513
1514 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001515 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001516}
1517
1518ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001519 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001520 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001521 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001522}
1523
Dan Gohman12fce772008-05-15 19:50:34 +00001524// getIndexedType - Returns the type of the element that would be extracted
1525// with an extractvalue instruction with the specified parameters.
1526//
1527// A null type is returned if the indices are invalid for the specified
1528// pointer type.
1529//
1530const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001531 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001532 unsigned NumIdx) {
1533 unsigned CurIdx = 0;
1534 for (; CurIdx != NumIdx; ++CurIdx) {
1535 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001536 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1537 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001538 if (!CT->indexValid(Index)) return 0;
1539 Agg = CT->getTypeAtIndex(Index);
1540
1541 // If the new type forwards to another type, then it is in the middle
1542 // of being refined to another type (and hence, may have dropped all
1543 // references to what it was using before). So, use the new forwarded
1544 // type.
1545 if (const Type *Ty = Agg->getForwardedType())
1546 Agg = Ty;
1547 }
1548 return CurIdx == NumIdx ? Agg : 0;
1549}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001550
Dan Gohman4a3125b2008-06-17 21:07:55 +00001551const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001552 unsigned Idx) {
1553 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001554}
1555
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001556//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001557// BinaryOperator Class
1558//===----------------------------------------------------------------------===//
1559
Dan Gohmana5b96452009-06-04 22:49:04 +00001560/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1561/// type is floating-point, to help provide compatibility with an older API.
1562///
1563static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1564 const Type *Ty) {
1565 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1566 if (Ty->isFPOrFPVector()) {
1567 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1568 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1569 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1570 }
1571 return iType;
1572}
1573
Chris Lattner2195fc42007-02-24 00:55:48 +00001574BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001575 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001576 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001577 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001578 OperandTraits<BinaryOperator>::op_begin(this),
1579 OperandTraits<BinaryOperator>::operands(this),
1580 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001581 Op<0>() = S1;
1582 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001583 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001584 setName(Name);
1585}
1586
1587BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001588 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001589 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001590 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001591 OperandTraits<BinaryOperator>::op_begin(this),
1592 OperandTraits<BinaryOperator>::operands(this),
1593 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001594 Op<0>() = S1;
1595 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001596 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001597 setName(Name);
1598}
1599
1600
1601void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001602 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001603 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001604 assert(LHS->getType() == RHS->getType() &&
1605 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001606#ifndef NDEBUG
1607 switch (iType) {
1608 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001609 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001610 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001611 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001612 assert(getType()->isIntOrIntVector() &&
1613 "Tried to create an integer operation on a non-integer type!");
1614 break;
1615 case FAdd: case FSub:
1616 case FMul:
1617 assert(getType() == LHS->getType() &&
1618 "Arithmetic operation should return same type as operands!");
1619 assert(getType()->isFPOrFPVector() &&
1620 "Tried to create a floating-point operation on a "
1621 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001622 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001623 case UDiv:
1624 case SDiv:
1625 assert(getType() == LHS->getType() &&
1626 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001627 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1628 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001629 "Incorrect operand type (not integer) for S/UDIV");
1630 break;
1631 case FDiv:
1632 assert(getType() == LHS->getType() &&
1633 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001634 assert(getType()->isFPOrFPVector() &&
1635 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001636 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001637 case URem:
1638 case SRem:
1639 assert(getType() == LHS->getType() &&
1640 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001641 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1642 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001643 "Incorrect operand type (not integer) for S/UREM");
1644 break;
1645 case FRem:
1646 assert(getType() == LHS->getType() &&
1647 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001648 assert(getType()->isFPOrFPVector() &&
1649 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001650 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001651 case Shl:
1652 case LShr:
1653 case AShr:
1654 assert(getType() == LHS->getType() &&
1655 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001656 assert((getType()->isInteger() ||
1657 (isa<VectorType>(getType()) &&
1658 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1659 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001660 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001661 case And: case Or:
1662 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001663 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001664 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001665 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001666 (isa<VectorType>(getType()) &&
1667 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001668 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001669 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001670 default:
1671 break;
1672 }
1673#endif
1674}
1675
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001676BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001677 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001678 Instruction *InsertBefore) {
1679 assert(S1->getType() == S2->getType() &&
1680 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001681 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001682}
1683
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001684BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001685 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001686 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001687 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001688 InsertAtEnd->getInstList().push_back(Res);
1689 return Res;
1690}
1691
Dan Gohman5476cfd2009-08-12 16:23:25 +00001692BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001693 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001694 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001695 return new BinaryOperator(Instruction::Sub,
1696 zero, Op,
1697 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001698}
1699
Dan Gohman5476cfd2009-08-12 16:23:25 +00001700BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001701 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001702 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001703 return new BinaryOperator(Instruction::Sub,
1704 zero, Op,
1705 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001706}
1707
Dan Gohman5476cfd2009-08-12 16:23:25 +00001708BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001709 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001710 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001711 return new BinaryOperator(Instruction::FSub,
1712 zero, Op,
1713 Op->getType(), Name, InsertBefore);
1714}
1715
Dan Gohman5476cfd2009-08-12 16:23:25 +00001716BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001717 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001718 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001719 return new BinaryOperator(Instruction::FSub,
1720 zero, Op,
1721 Op->getType(), Name, InsertAtEnd);
1722}
1723
Dan Gohman5476cfd2009-08-12 16:23:25 +00001724BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001725 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001726 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001727 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001728 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001729 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001730 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001731 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001732 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001733 }
1734
1735 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001736 Op->getType(), Name, InsertBefore);
1737}
1738
Dan Gohman5476cfd2009-08-12 16:23:25 +00001739BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001740 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001741 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001742 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001743 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001744 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001745 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001746 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001747 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001748 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001749 }
1750
1751 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001752 Op->getType(), Name, InsertAtEnd);
1753}
1754
1755
1756// isConstantAllOnes - Helper function for several functions below
1757static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001758 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1759 return CI->isAllOnesValue();
1760 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1761 return CV->isAllOnesValue();
1762 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001763}
1764
Owen Andersonbb2501b2009-07-13 22:18:28 +00001765bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001766 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1767 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001768 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1769 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001770 return false;
1771}
1772
Owen Andersonbb2501b2009-07-13 22:18:28 +00001773bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001774 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1775 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001776 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1777 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001778 return false;
1779}
1780
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001781bool BinaryOperator::isNot(const Value *V) {
1782 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1783 return (Bop->getOpcode() == Instruction::Xor &&
1784 (isConstantAllOnes(Bop->getOperand(1)) ||
1785 isConstantAllOnes(Bop->getOperand(0))));
1786 return false;
1787}
1788
Chris Lattner2c7d1772005-04-24 07:28:37 +00001789Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001790 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001791}
1792
Chris Lattner2c7d1772005-04-24 07:28:37 +00001793const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1794 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001795}
1796
Dan Gohmana5b96452009-06-04 22:49:04 +00001797Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001798 return cast<BinaryOperator>(BinOp)->getOperand(1);
1799}
1800
1801const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1802 return getFNegArgument(const_cast<Value*>(BinOp));
1803}
1804
Chris Lattner2c7d1772005-04-24 07:28:37 +00001805Value *BinaryOperator::getNotArgument(Value *BinOp) {
1806 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1807 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1808 Value *Op0 = BO->getOperand(0);
1809 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001810 if (isConstantAllOnes(Op0)) return Op1;
1811
1812 assert(isConstantAllOnes(Op1));
1813 return Op0;
1814}
1815
Chris Lattner2c7d1772005-04-24 07:28:37 +00001816const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1817 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001818}
1819
1820
1821// swapOperands - Exchange the two operands to this instruction. This
1822// instruction is safe to use on any binary instruction and does not
1823// modify the semantics of the instruction. If the instruction is
1824// order dependent (SetLT f.e.) the opcode is changed.
1825//
1826bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001827 if (!isCommutative())
1828 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001829 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001830 return false;
1831}
1832
Dan Gohman1b849082009-09-07 23:54:19 +00001833void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1834 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1835}
1836
1837void BinaryOperator::setHasNoSignedWrap(bool b) {
1838 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1839}
1840
1841void BinaryOperator::setIsExact(bool b) {
1842 cast<SDivOperator>(this)->setIsExact(b);
1843}
1844
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001845//===----------------------------------------------------------------------===//
1846// CastInst Class
1847//===----------------------------------------------------------------------===//
1848
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001849// Just determine if this cast only deals with integral->integral conversion.
1850bool CastInst::isIntegerCast() const {
1851 switch (getOpcode()) {
1852 default: return false;
1853 case Instruction::ZExt:
1854 case Instruction::SExt:
1855 case Instruction::Trunc:
1856 return true;
1857 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001858 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001859 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001860}
1861
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001862bool CastInst::isLosslessCast() const {
1863 // Only BitCast can be lossless, exit fast if we're not BitCast
1864 if (getOpcode() != Instruction::BitCast)
1865 return false;
1866
1867 // Identity cast is always lossless
1868 const Type* SrcTy = getOperand(0)->getType();
1869 const Type* DstTy = getType();
1870 if (SrcTy == DstTy)
1871 return true;
1872
Reid Spencer8d9336d2006-12-31 05:26:44 +00001873 // Pointer to pointer is always lossless.
1874 if (isa<PointerType>(SrcTy))
1875 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001876 return false; // Other types have no identity values
1877}
1878
1879/// This function determines if the CastInst does not require any bits to be
1880/// changed in order to effect the cast. Essentially, it identifies cases where
1881/// no code gen is necessary for the cast, hence the name no-op cast. For
1882/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001883/// # bitcast i32* %x to i8*
1884/// # bitcast <2 x i32> %x to <4 x i16>
1885/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001886/// @brief Determine if a cast is a no-op.
1887bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1888 switch (getOpcode()) {
1889 default:
1890 assert(!"Invalid CastOp");
1891 case Instruction::Trunc:
1892 case Instruction::ZExt:
1893 case Instruction::SExt:
1894 case Instruction::FPTrunc:
1895 case Instruction::FPExt:
1896 case Instruction::UIToFP:
1897 case Instruction::SIToFP:
1898 case Instruction::FPToUI:
1899 case Instruction::FPToSI:
1900 return false; // These always modify bits
1901 case Instruction::BitCast:
1902 return true; // BitCast never modifies bits.
1903 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001904 return IntPtrTy->getScalarSizeInBits() ==
1905 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001906 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001907 return IntPtrTy->getScalarSizeInBits() ==
1908 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001909 }
1910}
1911
1912/// This function determines if a pair of casts can be eliminated and what
1913/// opcode should be used in the elimination. This assumes that there are two
1914/// instructions like this:
1915/// * %F = firstOpcode SrcTy %x to MidTy
1916/// * %S = secondOpcode MidTy %F to DstTy
1917/// The function returns a resultOpcode so these two casts can be replaced with:
1918/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1919/// If no such cast is permited, the function returns 0.
1920unsigned CastInst::isEliminableCastPair(
1921 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1922 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1923{
1924 // Define the 144 possibilities for these two cast instructions. The values
1925 // in this matrix determine what to do in a given situation and select the
1926 // case in the switch below. The rows correspond to firstOp, the columns
1927 // correspond to secondOp. In looking at the table below, keep in mind
1928 // the following cast properties:
1929 //
1930 // Size Compare Source Destination
1931 // Operator Src ? Size Type Sign Type Sign
1932 // -------- ------------ ------------------- ---------------------
1933 // TRUNC > Integer Any Integral Any
1934 // ZEXT < Integral Unsigned Integer Any
1935 // SEXT < Integral Signed Integer Any
1936 // FPTOUI n/a FloatPt n/a Integral Unsigned
1937 // FPTOSI n/a FloatPt n/a Integral Signed
1938 // UITOFP n/a Integral Unsigned FloatPt n/a
1939 // SITOFP n/a Integral Signed FloatPt n/a
1940 // FPTRUNC > FloatPt n/a FloatPt n/a
1941 // FPEXT < FloatPt n/a FloatPt n/a
1942 // PTRTOINT n/a Pointer n/a Integral Unsigned
1943 // INTTOPTR n/a Integral Unsigned Pointer n/a
1944 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001945 //
1946 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001947 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1948 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001949 // of the produced value (we no longer know the top-part is all zeros).
1950 // Further this conversion is often much more expensive for typical hardware,
1951 // and causes issues when building libgcc. We disallow fptosi+sext for the
1952 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001953 const unsigned numCastOps =
1954 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1955 static const uint8_t CastResults[numCastOps][numCastOps] = {
1956 // T F F U S F F P I B -+
1957 // R Z S P P I I T P 2 N T |
1958 // U E E 2 2 2 2 R E I T C +- secondOp
1959 // N X X U S F F N X N 2 V |
1960 // C T T I I P P C T T P T -+
1961 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1962 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1963 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001964 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1965 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001966 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1967 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1968 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1969 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1970 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1971 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1972 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1973 };
1974
1975 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1976 [secondOp-Instruction::CastOpsBegin];
1977 switch (ElimCase) {
1978 case 0:
1979 // categorically disallowed
1980 return 0;
1981 case 1:
1982 // allowed, use first cast's opcode
1983 return firstOp;
1984 case 2:
1985 // allowed, use second cast's opcode
1986 return secondOp;
1987 case 3:
1988 // no-op cast in second op implies firstOp as long as the DestTy
1989 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001990 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001991 return firstOp;
1992 return 0;
1993 case 4:
1994 // no-op cast in second op implies firstOp as long as the DestTy
1995 // is floating point
1996 if (DstTy->isFloatingPoint())
1997 return firstOp;
1998 return 0;
1999 case 5:
2000 // no-op cast in first op implies secondOp as long as the SrcTy
2001 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00002002 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002003 return secondOp;
2004 return 0;
2005 case 6:
2006 // no-op cast in first op implies secondOp as long as the SrcTy
2007 // is a floating point
2008 if (SrcTy->isFloatingPoint())
2009 return secondOp;
2010 return 0;
2011 case 7: {
2012 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002013 if (!IntPtrTy)
2014 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002015 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2016 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002017 if (MidSize >= PtrSize)
2018 return Instruction::BitCast;
2019 return 0;
2020 }
2021 case 8: {
2022 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2023 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2024 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002025 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2026 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002027 if (SrcSize == DstSize)
2028 return Instruction::BitCast;
2029 else if (SrcSize < DstSize)
2030 return firstOp;
2031 return secondOp;
2032 }
2033 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2034 return Instruction::ZExt;
2035 case 10:
2036 // fpext followed by ftrunc is allowed if the bit size returned to is
2037 // the same as the original, in which case its just a bitcast
2038 if (SrcTy == DstTy)
2039 return Instruction::BitCast;
2040 return 0; // If the types are not the same we can't eliminate it.
2041 case 11:
2042 // bitcast followed by ptrtoint is allowed as long as the bitcast
2043 // is a pointer to pointer cast.
2044 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
2045 return secondOp;
2046 return 0;
2047 case 12:
2048 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2049 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
2050 return firstOp;
2051 return 0;
2052 case 13: {
2053 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002054 if (!IntPtrTy)
2055 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002056 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2057 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2058 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002059 if (SrcSize <= PtrSize && SrcSize == DstSize)
2060 return Instruction::BitCast;
2061 return 0;
2062 }
2063 case 99:
2064 // cast combination can't happen (error in input). This is for all cases
2065 // where the MidTy is not the same for the two cast instructions.
2066 assert(!"Invalid Cast Combination");
2067 return 0;
2068 default:
2069 assert(!"Error in CastResults table!!!");
2070 return 0;
2071 }
2072 return 0;
2073}
2074
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002075CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002076 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002077 // Construct and return the appropriate CastInst subclass
2078 switch (op) {
2079 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2080 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2081 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2082 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2083 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2084 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2085 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2086 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2087 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2088 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2089 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2090 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2091 default:
2092 assert(!"Invalid opcode provided");
2093 }
2094 return 0;
2095}
2096
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002097CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002098 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002099 // Construct and return the appropriate CastInst subclass
2100 switch (op) {
2101 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2102 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2103 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2104 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2105 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2106 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2107 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2108 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2109 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2110 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2111 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2112 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2113 default:
2114 assert(!"Invalid opcode provided");
2115 }
2116 return 0;
2117}
2118
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002119CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002120 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002121 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002122 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002123 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2124 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002125}
2126
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002127CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002128 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002129 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002130 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002131 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2132 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002133}
2134
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002135CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002136 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002137 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002138 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002139 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2140 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002141}
2142
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002143CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002144 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002145 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002146 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002147 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2148 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002149}
2150
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002151CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002152 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002153 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002154 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002155 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2156 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002157}
2158
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002159CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002160 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002161 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002162 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002163 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2164 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002165}
2166
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002167CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002168 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002169 BasicBlock *InsertAtEnd) {
2170 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002171 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002172 "Invalid cast");
2173
Chris Lattner03c49532007-01-15 02:27:26 +00002174 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002175 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2176 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002177}
2178
2179/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002180CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002181 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002182 Instruction *InsertBefore) {
2183 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002184 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002185 "Invalid cast");
2186
Chris Lattner03c49532007-01-15 02:27:26 +00002187 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002188 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2189 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002190}
2191
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002192CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002193 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002194 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002195 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002196 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2197 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002198 Instruction::CastOps opcode =
2199 (SrcBits == DstBits ? Instruction::BitCast :
2200 (SrcBits > DstBits ? Instruction::Trunc :
2201 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002202 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002203}
2204
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002205CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002206 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002207 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002208 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2209 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002210 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2211 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002212 Instruction::CastOps opcode =
2213 (SrcBits == DstBits ? Instruction::BitCast :
2214 (SrcBits > DstBits ? Instruction::Trunc :
2215 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002216 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002217}
2218
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002219CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002220 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002221 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002222 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002223 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002224 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2225 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002226 Instruction::CastOps opcode =
2227 (SrcBits == DstBits ? Instruction::BitCast :
2228 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002229 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002230}
2231
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002232CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002233 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002234 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002235 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002236 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002237 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2238 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002239 Instruction::CastOps opcode =
2240 (SrcBits == DstBits ? Instruction::BitCast :
2241 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002242 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002243}
2244
Duncan Sands55e50902008-01-06 10:12:28 +00002245// Check whether it is valid to call getCastOpcode for these types.
2246// This routine must be kept in sync with getCastOpcode.
2247bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2248 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2249 return false;
2250
2251 if (SrcTy == DestTy)
2252 return true;
2253
2254 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002255 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2256 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002257
2258 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002259 if (DestTy->isInteger()) { // Casting to integral
2260 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002261 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002262 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002263 return true;
2264 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002265 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002266 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002267 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002268 return isa<PointerType>(SrcTy);
2269 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002270 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2271 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002272 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002273 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002274 return true;
2275 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002276 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002277 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002278 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002279 return false;
2280 }
2281 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002282 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002283 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002284 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002285 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002286 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002287 return DestPTy->getBitWidth() == SrcBits;
2288 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002289 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2290 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002291 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002292 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002293 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002294 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002295 return false;
2296 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002297 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002298 return false;
2299 }
2300}
2301
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002302// Provide a way to get a "cast" where the cast opcode is inferred from the
2303// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002304// logic in the castIsValid function below. This axiom should hold:
2305// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2306// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002307// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002308// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002309Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002310CastInst::getCastOpcode(
2311 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002312 // Get the bit sizes, we'll need these
2313 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002314 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2315 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002316
Duncan Sands55e50902008-01-06 10:12:28 +00002317 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2318 "Only first class types are castable!");
2319
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002320 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002321 if (DestTy->isInteger()) { // Casting to integral
2322 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002323 if (DestBits < SrcBits)
2324 return Trunc; // int -> smaller int
2325 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002326 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002327 return SExt; // signed -> SEXT
2328 else
2329 return ZExt; // unsigned -> ZEXT
2330 } else {
2331 return BitCast; // Same size, No-op cast
2332 }
2333 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002334 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002335 return FPToSI; // FP -> sint
2336 else
2337 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002338 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002339 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002340 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002341 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002342 return BitCast; // Same size, no-op cast
2343 } else {
2344 assert(isa<PointerType>(SrcTy) &&
2345 "Casting from a value that is not first-class type");
2346 return PtrToInt; // ptr -> int
2347 }
2348 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002349 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002350 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002351 return SIToFP; // sint -> FP
2352 else
2353 return UIToFP; // uint -> FP
2354 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2355 if (DestBits < SrcBits) {
2356 return FPTrunc; // FP -> smaller FP
2357 } else if (DestBits > SrcBits) {
2358 return FPExt; // FP -> larger FP
2359 } else {
2360 return BitCast; // same size, no-op cast
2361 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002362 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002364 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002365 PTy = NULL;
2366 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002367 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002368 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002369 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002370 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2371 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002372 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002373 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002374 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002375 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002376 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002377 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002379 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002380 }
2381 } else if (isa<PointerType>(DestTy)) {
2382 if (isa<PointerType>(SrcTy)) {
2383 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002384 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002385 return IntToPtr; // int -> ptr
2386 } else {
2387 assert(!"Casting pointer to other than pointer or int");
2388 }
2389 } else {
2390 assert(!"Casting to type that is not first-class");
2391 }
2392
2393 // If we fall through to here we probably hit an assertion cast above
2394 // and assertions are not turned on. Anything we return is an error, so
2395 // BitCast is as good a choice as any.
2396 return BitCast;
2397}
2398
2399//===----------------------------------------------------------------------===//
2400// CastInst SubClass Constructors
2401//===----------------------------------------------------------------------===//
2402
2403/// Check that the construction parameters for a CastInst are correct. This
2404/// could be broken out into the separate constructors but it is useful to have
2405/// it in one place and to eliminate the redundant code for getting the sizes
2406/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002407bool
2408CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002409
2410 // Check for type sanity on the arguments
2411 const Type *SrcTy = S->getType();
2412 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2413 return false;
2414
2415 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002416 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2417 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002418
2419 // Switch on the opcode provided
2420 switch (op) {
2421 default: return false; // This is an input error
2422 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002423 return SrcTy->isIntOrIntVector() &&
2424 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002426 return SrcTy->isIntOrIntVector() &&
2427 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002429 return SrcTy->isIntOrIntVector() &&
2430 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002431 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002432 return SrcTy->isFPOrFPVector() &&
2433 DstTy->isFPOrFPVector() &&
2434 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002436 return SrcTy->isFPOrFPVector() &&
2437 DstTy->isFPOrFPVector() &&
2438 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002439 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002440 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002441 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2442 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002443 return SVTy->getElementType()->isIntOrIntVector() &&
2444 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002445 SVTy->getNumElements() == DVTy->getNumElements();
2446 }
2447 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002448 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002449 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002450 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002451 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2452 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002453 return SVTy->getElementType()->isFPOrFPVector() &&
2454 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002455 SVTy->getNumElements() == DVTy->getNumElements();
2456 }
2457 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002458 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002460 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002461 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002462 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463 case Instruction::BitCast:
2464 // BitCast implies a no-op cast of type only. No bits change.
2465 // However, you can't cast pointers to anything but pointers.
2466 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2467 return false;
2468
Duncan Sands55e50902008-01-06 10:12:28 +00002469 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002470 // these cases, the cast is okay if the source and destination bit widths
2471 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002472 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002473 }
2474}
2475
2476TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002477 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002479 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002480}
2481
2482TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002483 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002484) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002485 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002486}
2487
2488ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002489 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002490) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002491 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002492}
2493
2494ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002495 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002496) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002497 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002498}
2499SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002500 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002501) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002502 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002503}
2504
Jeff Cohencc08c832006-12-02 02:22:01 +00002505SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002506 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002508 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002509}
2510
2511FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002512 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002513) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002514 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002515}
2516
2517FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002518 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002519) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002520 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002521}
2522
2523FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002524 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002525) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002526 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002527}
2528
2529FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002530 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002531) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002532 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002533}
2534
2535UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002536 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002537) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002538 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539}
2540
2541UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002542 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002543) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002544 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545}
2546
2547SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002548 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002549) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002550 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002551}
2552
2553SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002554 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002555) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002556 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002557}
2558
2559FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002560 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002562 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563}
2564
2565FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002566 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002567) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002568 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569}
2570
2571FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002572 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002573) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002574 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002575}
2576
2577FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002578 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002580 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002581}
2582
2583PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002584 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002586 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587}
2588
2589PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002590 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002592 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002593}
2594
2595IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002596 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002597) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002598 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002599}
2600
2601IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002602 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002603) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002604 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002605}
2606
2607BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002608 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002610 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611}
2612
2613BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002614 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002615) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002616 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002617}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002618
2619//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002620// CmpInst Classes
2621//===----------------------------------------------------------------------===//
2622
Nate Begemand2195702008-05-12 19:01:56 +00002623CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002624 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002625 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002626 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002627 OperandTraits<CmpInst>::op_begin(this),
2628 OperandTraits<CmpInst>::operands(this),
2629 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002630 Op<0>() = LHS;
2631 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002632 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002633 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002634}
Gabor Greiff6caff662008-05-10 08:32:32 +00002635
Nate Begemand2195702008-05-12 19:01:56 +00002636CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002637 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002638 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002639 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002640 OperandTraits<CmpInst>::op_begin(this),
2641 OperandTraits<CmpInst>::operands(this),
2642 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002643 Op<0>() = LHS;
2644 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002645 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002646 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002647}
2648
2649CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002650CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002651 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002652 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002653 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002654 if (InsertBefore)
2655 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2656 S1, S2, Name);
2657 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002658 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002659 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002660 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002661
2662 if (InsertBefore)
2663 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2664 S1, S2, Name);
2665 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002666 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002667 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002668}
2669
2670CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002671CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002672 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002673 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002674 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2675 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002676 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002677 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2678 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002679}
2680
2681void CmpInst::swapOperands() {
2682 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2683 IC->swapOperands();
2684 else
2685 cast<FCmpInst>(this)->swapOperands();
2686}
2687
2688bool CmpInst::isCommutative() {
2689 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2690 return IC->isCommutative();
2691 return cast<FCmpInst>(this)->isCommutative();
2692}
2693
2694bool CmpInst::isEquality() {
2695 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2696 return IC->isEquality();
2697 return cast<FCmpInst>(this)->isEquality();
2698}
2699
2700
Dan Gohman4e724382008-05-31 02:47:54 +00002701CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002702 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002703 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002704 case ICMP_EQ: return ICMP_NE;
2705 case ICMP_NE: return ICMP_EQ;
2706 case ICMP_UGT: return ICMP_ULE;
2707 case ICMP_ULT: return ICMP_UGE;
2708 case ICMP_UGE: return ICMP_ULT;
2709 case ICMP_ULE: return ICMP_UGT;
2710 case ICMP_SGT: return ICMP_SLE;
2711 case ICMP_SLT: return ICMP_SGE;
2712 case ICMP_SGE: return ICMP_SLT;
2713 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002714
Dan Gohman4e724382008-05-31 02:47:54 +00002715 case FCMP_OEQ: return FCMP_UNE;
2716 case FCMP_ONE: return FCMP_UEQ;
2717 case FCMP_OGT: return FCMP_ULE;
2718 case FCMP_OLT: return FCMP_UGE;
2719 case FCMP_OGE: return FCMP_ULT;
2720 case FCMP_OLE: return FCMP_UGT;
2721 case FCMP_UEQ: return FCMP_ONE;
2722 case FCMP_UNE: return FCMP_OEQ;
2723 case FCMP_UGT: return FCMP_OLE;
2724 case FCMP_ULT: return FCMP_OGE;
2725 case FCMP_UGE: return FCMP_OLT;
2726 case FCMP_ULE: return FCMP_OGT;
2727 case FCMP_ORD: return FCMP_UNO;
2728 case FCMP_UNO: return FCMP_ORD;
2729 case FCMP_TRUE: return FCMP_FALSE;
2730 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002731 }
2732}
2733
Reid Spencer266e42b2006-12-23 06:05:41 +00002734ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2735 switch (pred) {
2736 default: assert(! "Unknown icmp predicate!");
2737 case ICMP_EQ: case ICMP_NE:
2738 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2739 return pred;
2740 case ICMP_UGT: return ICMP_SGT;
2741 case ICMP_ULT: return ICMP_SLT;
2742 case ICMP_UGE: return ICMP_SGE;
2743 case ICMP_ULE: return ICMP_SLE;
2744 }
2745}
2746
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002747ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2748 switch (pred) {
2749 default: assert(! "Unknown icmp predicate!");
2750 case ICMP_EQ: case ICMP_NE:
2751 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2752 return pred;
2753 case ICMP_SGT: return ICMP_UGT;
2754 case ICMP_SLT: return ICMP_ULT;
2755 case ICMP_SGE: return ICMP_UGE;
2756 case ICMP_SLE: return ICMP_ULE;
2757 }
2758}
2759
Reid Spencer266e42b2006-12-23 06:05:41 +00002760bool ICmpInst::isSignedPredicate(Predicate pred) {
2761 switch (pred) {
2762 default: assert(! "Unknown icmp predicate!");
2763 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2764 return true;
2765 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2766 case ICMP_UGE: case ICMP_ULE:
2767 return false;
2768 }
2769}
2770
Reid Spencer0286bc12007-02-28 22:00:54 +00002771/// Initialize a set of values that all satisfy the condition with C.
2772///
2773ConstantRange
2774ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2775 APInt Lower(C);
2776 APInt Upper(C);
2777 uint32_t BitWidth = C.getBitWidth();
2778 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002779 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002780 case ICmpInst::ICMP_EQ: Upper++; break;
2781 case ICmpInst::ICMP_NE: Lower++; break;
2782 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2783 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2784 case ICmpInst::ICMP_UGT:
2785 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2786 break;
2787 case ICmpInst::ICMP_SGT:
2788 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2789 break;
2790 case ICmpInst::ICMP_ULE:
2791 Lower = APInt::getMinValue(BitWidth); Upper++;
2792 break;
2793 case ICmpInst::ICMP_SLE:
2794 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2795 break;
2796 case ICmpInst::ICMP_UGE:
2797 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2798 break;
2799 case ICmpInst::ICMP_SGE:
2800 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2801 break;
2802 }
2803 return ConstantRange(Lower, Upper);
2804}
2805
Dan Gohman4e724382008-05-31 02:47:54 +00002806CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002807 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002808 default: assert(!"Unknown cmp predicate!");
2809 case ICMP_EQ: case ICMP_NE:
2810 return pred;
2811 case ICMP_SGT: return ICMP_SLT;
2812 case ICMP_SLT: return ICMP_SGT;
2813 case ICMP_SGE: return ICMP_SLE;
2814 case ICMP_SLE: return ICMP_SGE;
2815 case ICMP_UGT: return ICMP_ULT;
2816 case ICMP_ULT: return ICMP_UGT;
2817 case ICMP_UGE: return ICMP_ULE;
2818 case ICMP_ULE: return ICMP_UGE;
2819
Reid Spencerd9436b62006-11-20 01:22:35 +00002820 case FCMP_FALSE: case FCMP_TRUE:
2821 case FCMP_OEQ: case FCMP_ONE:
2822 case FCMP_UEQ: case FCMP_UNE:
2823 case FCMP_ORD: case FCMP_UNO:
2824 return pred;
2825 case FCMP_OGT: return FCMP_OLT;
2826 case FCMP_OLT: return FCMP_OGT;
2827 case FCMP_OGE: return FCMP_OLE;
2828 case FCMP_OLE: return FCMP_OGE;
2829 case FCMP_UGT: return FCMP_ULT;
2830 case FCMP_ULT: return FCMP_UGT;
2831 case FCMP_UGE: return FCMP_ULE;
2832 case FCMP_ULE: return FCMP_UGE;
2833 }
2834}
2835
Reid Spencer266e42b2006-12-23 06:05:41 +00002836bool CmpInst::isUnsigned(unsigned short predicate) {
2837 switch (predicate) {
2838 default: return false;
2839 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2840 case ICmpInst::ICMP_UGE: return true;
2841 }
2842}
2843
2844bool CmpInst::isSigned(unsigned short predicate){
2845 switch (predicate) {
2846 default: return false;
2847 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2848 case ICmpInst::ICMP_SGE: return true;
2849 }
2850}
2851
2852bool CmpInst::isOrdered(unsigned short predicate) {
2853 switch (predicate) {
2854 default: return false;
2855 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2856 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2857 case FCmpInst::FCMP_ORD: return true;
2858 }
2859}
2860
2861bool CmpInst::isUnordered(unsigned short predicate) {
2862 switch (predicate) {
2863 default: return false;
2864 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2865 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2866 case FCmpInst::FCMP_UNO: return true;
2867 }
2868}
2869
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002870//===----------------------------------------------------------------------===//
2871// SwitchInst Implementation
2872//===----------------------------------------------------------------------===//
2873
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002874void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002875 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002876 ReservedSpace = 2+NumCases*2;
2877 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002878 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002879
Gabor Greif2d3024d2008-05-26 21:33:52 +00002880 OperandList[0] = Value;
2881 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002882}
2883
Chris Lattner2195fc42007-02-24 00:55:48 +00002884/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2885/// switch on and a default destination. The number of additional cases can
2886/// be specified here to make memory allocation more efficient. This
2887/// constructor can also autoinsert before another instruction.
2888SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2889 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002890 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2891 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002892 init(Value, Default, NumCases);
2893}
2894
2895/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2896/// switch on and a default destination. The number of additional cases can
2897/// be specified here to make memory allocation more efficient. This
2898/// constructor also autoinserts at the end of the specified BasicBlock.
2899SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2900 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002901 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2902 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002903 init(Value, Default, NumCases);
2904}
2905
Misha Brukmanb1c93172005-04-21 23:48:37 +00002906SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002907 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002908 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002909 Use *OL = OperandList, *InOL = SI.OperandList;
2910 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002911 OL[i] = InOL[i];
2912 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002913 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002914 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002915}
2916
Gordon Henriksen14a55692007-12-10 02:14:30 +00002917SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002918 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002919}
2920
2921
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002922/// addCase - Add an entry to the switch instruction...
2923///
Chris Lattner47ac1872005-02-24 05:32:09 +00002924void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002925 unsigned OpNo = NumOperands;
2926 if (OpNo+2 > ReservedSpace)
2927 resizeOperands(0); // Get more space!
2928 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002929 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002930 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002931 OperandList[OpNo] = OnVal;
2932 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002933}
2934
2935/// removeCase - This method removes the specified successor from the switch
2936/// instruction. Note that this cannot be used to remove the default
2937/// destination (successor #0).
2938///
2939void SwitchInst::removeCase(unsigned idx) {
2940 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002941 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2942
2943 unsigned NumOps = getNumOperands();
2944 Use *OL = OperandList;
2945
2946 // Move everything after this operand down.
2947 //
2948 // FIXME: we could just swap with the end of the list, then erase. However,
2949 // client might not expect this to happen. The code as it is thrashes the
2950 // use/def lists, which is kinda lame.
2951 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2952 OL[i-2] = OL[i];
2953 OL[i-2+1] = OL[i+1];
2954 }
2955
2956 // Nuke the last value.
2957 OL[NumOps-2].set(0);
2958 OL[NumOps-2+1].set(0);
2959 NumOperands = NumOps-2;
2960}
2961
2962/// resizeOperands - resize operands - This adjusts the length of the operands
2963/// list according to the following behavior:
2964/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002965/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002966/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2967/// 3. If NumOps == NumOperands, trim the reserved space.
2968///
2969void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002970 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002971 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002972 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002973 } else if (NumOps*2 > NumOperands) {
2974 // No resize needed.
2975 if (ReservedSpace >= NumOps) return;
2976 } else if (NumOps == NumOperands) {
2977 if (ReservedSpace == NumOps) return;
2978 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002979 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002980 }
2981
2982 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002983 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002984 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002985 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002986 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002987 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002988 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002989 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002990}
2991
2992
2993BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2994 return getSuccessor(idx);
2995}
2996unsigned SwitchInst::getNumSuccessorsV() const {
2997 return getNumSuccessors();
2998}
2999void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3000 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003001}
Chris Lattnerf22be932004-10-15 23:52:53 +00003002
Chris Lattnerf22be932004-10-15 23:52:53 +00003003// Define these methods here so vtables don't get emitted into every translation
3004// unit that uses these classes.
3005
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003006GetElementPtrInst *GetElementPtrInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003007 GetElementPtrInst *New = new(getNumOperands()) GetElementPtrInst(*this);
3008 New->SubclassOptionalData = SubclassOptionalData;
3009 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00003010}
3011
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003012BinaryOperator *BinaryOperator::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003013 BinaryOperator *New = Create(getOpcode(), Op<0>(), Op<1>());
3014 New->SubclassOptionalData = SubclassOptionalData;
3015 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00003016}
3017
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003018FCmpInst* FCmpInst::clone(LLVMContext &Context) const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003019 FCmpInst *New = new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003020 New->SubclassOptionalData = SubclassOptionalData;
3021 return New;
Chris Lattner0b490b02007-08-24 20:48:18 +00003022}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003023ICmpInst* ICmpInst::clone(LLVMContext &Context) const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003024 ICmpInst *New = new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003025 New->SubclassOptionalData = SubclassOptionalData;
3026 return New;
Reid Spencerd9436b62006-11-20 01:22:35 +00003027}
3028
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003029ExtractValueInst *ExtractValueInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003030 ExtractValueInst *New = new ExtractValueInst(*this);
3031 New->SubclassOptionalData = SubclassOptionalData;
3032 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00003033}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003034InsertValueInst *InsertValueInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003035 InsertValueInst *New = new InsertValueInst(*this);
3036 New->SubclassOptionalData = SubclassOptionalData;
3037 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00003038}
3039
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003040MallocInst *MallocInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003041 MallocInst *New = new MallocInst(getAllocatedType(),
3042 (Value*)getOperand(0),
3043 getAlignment());
3044 New->SubclassOptionalData = SubclassOptionalData;
3045 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003046}
Dan Gohman0752bff2008-05-23 00:36:11 +00003047
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003048AllocaInst *AllocaInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003049 AllocaInst *New = new AllocaInst(getAllocatedType(),
3050 (Value*)getOperand(0),
3051 getAlignment());
3052 New->SubclassOptionalData = SubclassOptionalData;
3053 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003054}
3055
3056FreeInst *FreeInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003057 FreeInst *New = new FreeInst(getOperand(0));
3058 New->SubclassOptionalData = SubclassOptionalData;
3059 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003060}
3061
3062LoadInst *LoadInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003063 LoadInst *New = new LoadInst(getOperand(0),
3064 Twine(), isVolatile(),
3065 getAlignment());
3066 New->SubclassOptionalData = SubclassOptionalData;
3067 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003068}
3069
3070StoreInst *StoreInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003071 StoreInst *New = new StoreInst(getOperand(0), getOperand(1),
3072 isVolatile(), getAlignment());
3073 New->SubclassOptionalData = SubclassOptionalData;
3074 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003075}
3076
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003077TruncInst *TruncInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003078 TruncInst *New = new TruncInst(getOperand(0), getType());
3079 New->SubclassOptionalData = SubclassOptionalData;
3080 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003081}
3082
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003083ZExtInst *ZExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003084 ZExtInst *New = new ZExtInst(getOperand(0), getType());
3085 New->SubclassOptionalData = SubclassOptionalData;
3086 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003087}
3088
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003089SExtInst *SExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003090 SExtInst *New = new SExtInst(getOperand(0), getType());
3091 New->SubclassOptionalData = SubclassOptionalData;
3092 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003093}
3094
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003095FPTruncInst *FPTruncInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003096 FPTruncInst *New = new FPTruncInst(getOperand(0), getType());
3097 New->SubclassOptionalData = SubclassOptionalData;
3098 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003099}
3100
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003101FPExtInst *FPExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003102 FPExtInst *New = new FPExtInst(getOperand(0), getType());
3103 New->SubclassOptionalData = SubclassOptionalData;
3104 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003105}
3106
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003107UIToFPInst *UIToFPInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003108 UIToFPInst *New = new UIToFPInst(getOperand(0), getType());
3109 New->SubclassOptionalData = SubclassOptionalData;
3110 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003111}
3112
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003113SIToFPInst *SIToFPInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003114 SIToFPInst *New = new SIToFPInst(getOperand(0), getType());
3115 New->SubclassOptionalData = SubclassOptionalData;
3116 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003117}
3118
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003119FPToUIInst *FPToUIInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003120 FPToUIInst *New = new FPToUIInst(getOperand(0), getType());
3121 New->SubclassOptionalData = SubclassOptionalData;
3122 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003123}
3124
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003125FPToSIInst *FPToSIInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003126 FPToSIInst *New = new FPToSIInst(getOperand(0), getType());
3127 New->SubclassOptionalData = SubclassOptionalData;
3128 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003129}
3130
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003131PtrToIntInst *PtrToIntInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003132 PtrToIntInst *New = new PtrToIntInst(getOperand(0), getType());
3133 New->SubclassOptionalData = SubclassOptionalData;
3134 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003135}
3136
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003137IntToPtrInst *IntToPtrInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003138 IntToPtrInst *New = new IntToPtrInst(getOperand(0), getType());
3139 New->SubclassOptionalData = SubclassOptionalData;
3140 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003141}
3142
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003143BitCastInst *BitCastInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003144 BitCastInst *New = new BitCastInst(getOperand(0), getType());
3145 New->SubclassOptionalData = SubclassOptionalData;
3146 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003147}
3148
3149CallInst *CallInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003150 CallInst *New = new(getNumOperands()) CallInst(*this);
3151 New->SubclassOptionalData = SubclassOptionalData;
3152 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003153}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003154
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003155SelectInst *SelectInst::clone(LLVMContext&) const {
3156 SelectInst *New = SelectInst::Create(getOperand(0),
3157 getOperand(1),
3158 getOperand(2));
3159 New->SubclassOptionalData = SubclassOptionalData;
3160 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003161}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003162
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003163VAArgInst *VAArgInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003164 VAArgInst *New = new VAArgInst(getOperand(0), getType());
3165 New->SubclassOptionalData = SubclassOptionalData;
3166 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003167}
3168
3169ExtractElementInst *ExtractElementInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003170 ExtractElementInst *New = ExtractElementInst::Create(getOperand(0),
3171 getOperand(1));
3172 New->SubclassOptionalData = SubclassOptionalData;
3173 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003174}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003175
3176InsertElementInst *InsertElementInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003177 InsertElementInst *New = InsertElementInst::Create(getOperand(0),
3178 getOperand(1),
3179 getOperand(2));
3180 New->SubclassOptionalData = SubclassOptionalData;
3181 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003182}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003183
3184ShuffleVectorInst *ShuffleVectorInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003185 ShuffleVectorInst *New = new ShuffleVectorInst(getOperand(0),
3186 getOperand(1),
3187 getOperand(2));
3188 New->SubclassOptionalData = SubclassOptionalData;
3189 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003190}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003191
3192PHINode *PHINode::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003193 PHINode *New = new PHINode(*this);
3194 New->SubclassOptionalData = SubclassOptionalData;
3195 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003196}
3197
3198ReturnInst *ReturnInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003199 ReturnInst *New = new(getNumOperands()) ReturnInst(*this);
3200 New->SubclassOptionalData = SubclassOptionalData;
3201 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003202}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003203
3204BranchInst *BranchInst::clone(LLVMContext&) const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003205 unsigned Ops(getNumOperands());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003206 BranchInst *New = new(Ops, Ops == 1) BranchInst(*this);
3207 New->SubclassOptionalData = SubclassOptionalData;
3208 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003209}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003210
3211SwitchInst *SwitchInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003212 SwitchInst *New = new SwitchInst(*this);
3213 New->SubclassOptionalData = SubclassOptionalData;
3214 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003215}
3216
3217InvokeInst *InvokeInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003218 InvokeInst *New = new(getNumOperands()) InvokeInst(*this);
3219 New->SubclassOptionalData = SubclassOptionalData;
3220 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003221}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003222
Owen Anderson55f1c092009-08-13 21:58:54 +00003223UnwindInst *UnwindInst::clone(LLVMContext &C) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003224 UnwindInst *New = new UnwindInst(C);
3225 New->SubclassOptionalData = SubclassOptionalData;
3226 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003227}
3228
Owen Anderson55f1c092009-08-13 21:58:54 +00003229UnreachableInst *UnreachableInst::clone(LLVMContext &C) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003230 UnreachableInst *New = new UnreachableInst(C);
3231 New->SubclassOptionalData = SubclassOptionalData;
3232 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003233}