blob: d479d9ac99cc8f98690626c61e2a0dff682326af [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,
Victor Hernandez788eaab2009-09-18 19:20:02 +0000463 const Type *IntPtrTy, const Type *AllocTy,
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000464 Value *ArraySize, const Twine &NameStr) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000465 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000466 "createMalloc needs either InsertBefore or InsertAtEnd");
467
468 // malloc(type) becomes:
469 // bitcast (i8* malloc(typeSize)) to type*
470 // malloc(type, arraySize) becomes:
471 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
472 Value *AllocSize = ConstantExpr::getSizeOf(AllocTy);
473 AllocSize = ConstantExpr::getTruncOrBitCast(cast<Constant>(AllocSize),
474 IntPtrTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000475 ArraySize = checkArraySize(ArraySize, IntPtrTy);
476
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000477 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000478 if (IsConstantOne(AllocSize)) {
479 AllocSize = ArraySize; // Operand * 1 = Operand
480 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
481 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
482 false /*ZExt*/);
483 // Malloc arg is constant product of type size and array size
484 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
485 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000486 // Multiply type size by the array size...
487 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000488 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
489 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000490 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000491 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
492 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000493 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000494 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000495
Victor Hernandez788eaab2009-09-18 19:20:02 +0000496 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000497 // Create the call to Malloc.
498 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
499 Module* M = BB->getParent()->getParent();
500 const Type *BPTy = PointerType::getUnqual(Type::getInt8Ty(BB->getContext()));
501 // prototype malloc as "void *malloc(size_t)"
Victor Hernandez788eaab2009-09-18 19:20:02 +0000502 Constant *MallocF = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
503 if (!cast<Function>(MallocF)->doesNotAlias(0))
504 cast<Function>(MallocF)->setDoesNotAlias(0);
505 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000506 CallInst *MCall = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000507 Value *MCast = NULL;
508 if (InsertBefore) {
509 MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertBefore);
510 // Create a cast instruction to convert to the right type...
511 MCast = new BitCastInst(MCall, AllocPtrType, NameStr, InsertBefore);
512 } else {
513 MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertAtEnd);
514 // Create a cast instruction to convert to the right type...
515 MCast = new BitCastInst(MCall, AllocPtrType, NameStr);
516 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000517 MCall->setTailCall();
Daniel Dunbarb9189002009-09-11 22:07:31 +0000518 assert(MCall->getType() != Type::getVoidTy(BB->getContext()) &&
519 "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000520
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000521 return MCast;
522}
523
524/// CreateMalloc - Generate the IR for a call to malloc:
525/// 1. Compute the malloc call's argument as the specified type's size,
526/// possibly multiplied by the array size if the array size is not
527/// constant 1.
528/// 2. Call malloc with that argument.
529/// 3. Bitcast the result of the malloc call to the specified type.
Victor Hernandez788eaab2009-09-18 19:20:02 +0000530Value *CallInst::CreateMalloc(Instruction *InsertBefore, const Type *IntPtrTy,
531 const Type *AllocTy, Value *ArraySize,
532 const Twine &Name) {
533 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, ArraySize, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000534}
535
536/// CreateMalloc - Generate the IR for a call to malloc:
537/// 1. Compute the malloc call's argument as the specified type's size,
538/// possibly multiplied by the array size if the array size is not
539/// constant 1.
540/// 2. Call malloc with that argument.
541/// 3. Bitcast the result of the malloc call to the specified type.
542/// Note: This function does not add the bitcast to the basic block, that is the
543/// responsibility of the caller.
Victor Hernandez788eaab2009-09-18 19:20:02 +0000544Value *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, const Type *IntPtrTy,
545 const Type *AllocTy, Value *ArraySize,
546 const Twine &Name) {
547 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, ArraySize, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000548}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000549
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000550//===----------------------------------------------------------------------===//
551// InvokeInst Implementation
552//===----------------------------------------------------------------------===//
553
554void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000555 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000556 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000557 Use *OL = OperandList;
558 OL[0] = Fn;
559 OL[1] = IfNormal;
560 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000561 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000562 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000563 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000564
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000565 assert(((NumArgs == FTy->getNumParams()) ||
566 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000567 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000568
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000569 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000570 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000571 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000572 "Invoking a function with a bad signature!");
573
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000574 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000575 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000576}
577
Misha Brukmanb1c93172005-04-21 23:48:37 +0000578InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000579 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000580 OperandTraits<InvokeInst>::op_end(this)
581 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000582 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000583 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000584 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000585 Use *OL = OperandList, *InOL = II.OperandList;
586 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000587 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000588 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000589}
590
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000591BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
592 return getSuccessor(idx);
593}
594unsigned InvokeInst::getNumSuccessorsV() const {
595 return getNumSuccessors();
596}
597void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
598 return setSuccessor(idx, B);
599}
600
Devang Patelba3fa6c2008-09-23 23:03:40 +0000601bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000602 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000603 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000604 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000605 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000606 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000607}
608
Devang Patel4c758ea2008-09-25 21:00:45 +0000609void InvokeInst::addAttribute(unsigned i, Attributes attr) {
610 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000611 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000612 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000613}
614
Devang Patel4c758ea2008-09-25 21:00:45 +0000615void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
616 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000617 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000618 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000619}
620
Duncan Sands5208d1a2007-11-28 17:07:01 +0000621
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000622//===----------------------------------------------------------------------===//
623// ReturnInst Implementation
624//===----------------------------------------------------------------------===//
625
Chris Lattner2195fc42007-02-24 00:55:48 +0000626ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000627 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000628 OperandTraits<ReturnInst>::op_end(this) -
629 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000630 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000631 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000632 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000633 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000634}
635
Owen Anderson55f1c092009-08-13 21:58:54 +0000636ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
637 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000638 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
639 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000640 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000641 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000642}
Owen Anderson55f1c092009-08-13 21:58:54 +0000643ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
644 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000645 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
646 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000647 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000648 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000649}
Owen Anderson55f1c092009-08-13 21:58:54 +0000650ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
651 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000652 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000653}
654
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655unsigned ReturnInst::getNumSuccessorsV() const {
656 return getNumSuccessors();
657}
658
Devang Patelae682fb2008-02-26 17:56:20 +0000659/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
660/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000661void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000662 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000663}
664
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000665BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000666 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000667 return 0;
668}
669
Devang Patel59643e52008-02-23 00:35:18 +0000670ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000671}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000672
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000673//===----------------------------------------------------------------------===//
674// UnwindInst Implementation
675//===----------------------------------------------------------------------===//
676
Owen Anderson55f1c092009-08-13 21:58:54 +0000677UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
678 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
679 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000680}
Owen Anderson55f1c092009-08-13 21:58:54 +0000681UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
682 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
683 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000684}
685
686
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687unsigned UnwindInst::getNumSuccessorsV() const {
688 return getNumSuccessors();
689}
690
691void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000692 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000693}
694
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000695BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000696 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000697 return 0;
698}
699
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000700//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000701// UnreachableInst Implementation
702//===----------------------------------------------------------------------===//
703
Owen Anderson55f1c092009-08-13 21:58:54 +0000704UnreachableInst::UnreachableInst(LLVMContext &Context,
705 Instruction *InsertBefore)
706 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
707 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000708}
Owen Anderson55f1c092009-08-13 21:58:54 +0000709UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
710 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
711 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000712}
713
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000714unsigned UnreachableInst::getNumSuccessorsV() const {
715 return getNumSuccessors();
716}
717
718void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000719 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000720}
721
722BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000723 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000724 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000725}
726
727//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000728// BranchInst Implementation
729//===----------------------------------------------------------------------===//
730
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000731void BranchInst::AssertOK() {
732 if (isConditional())
Owen Anderson55f1c092009-08-13 21:58:54 +0000733 assert(getCondition()->getType() == Type::getInt1Ty(getContext()) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000734 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000735}
736
Chris Lattner2195fc42007-02-24 00:55:48 +0000737BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000738 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000739 OperandTraits<BranchInst>::op_end(this) - 1,
740 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000741 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000742 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000743}
744BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
745 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000746 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000747 OperandTraits<BranchInst>::op_end(this) - 3,
748 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000749 Op<-1>() = IfTrue;
750 Op<-2>() = IfFalse;
751 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000752#ifndef NDEBUG
753 AssertOK();
754#endif
755}
756
757BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000758 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000759 OperandTraits<BranchInst>::op_end(this) - 1,
760 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000761 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000762 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000763}
764
765BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
766 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000767 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000768 OperandTraits<BranchInst>::op_end(this) - 3,
769 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000770 Op<-1>() = IfTrue;
771 Op<-2>() = IfFalse;
772 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000773#ifndef NDEBUG
774 AssertOK();
775#endif
776}
777
778
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000779BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000780 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000781 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
782 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000783 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000784 if (BI.getNumOperands() != 1) {
785 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000786 Op<-3>() = BI.Op<-3>();
787 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000788 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000789 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000790}
791
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000792
793Use* Use::getPrefix() {
794 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
795 if (PotentialPrefix.getOpaqueValue())
796 return 0;
797
798 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
799}
800
801BranchInst::~BranchInst() {
802 if (NumOperands == 1) {
803 if (Use *Prefix = OperandList->getPrefix()) {
804 Op<-1>() = 0;
805 //
806 // mark OperandList to have a special value for scrutiny
807 // by baseclass destructors and operator delete
808 OperandList = Prefix;
809 } else {
810 NumOperands = 3;
811 OperandList = op_begin();
812 }
813 }
814}
815
816
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000817BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
818 return getSuccessor(idx);
819}
820unsigned BranchInst::getNumSuccessorsV() const {
821 return getNumSuccessors();
822}
823void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
824 setSuccessor(idx, B);
825}
826
827
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000828//===----------------------------------------------------------------------===//
829// AllocationInst Implementation
830//===----------------------------------------------------------------------===//
831
Owen Andersonb6b25302009-07-14 23:09:55 +0000832static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000833 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000834 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000835 else {
836 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000837 "Passed basic block into allocation size parameter! Use other ctor");
Owen Anderson55f1c092009-08-13 21:58:54 +0000838 assert(Amt->getType() == Type::getInt32Ty(Context) &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000839 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000840 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000841 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000842}
843
Owen Anderson4fdeba92009-07-15 23:53:25 +0000844AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000845 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000846 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +0000847 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000848 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000849 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000850 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000851 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000852}
853
Owen Anderson4fdeba92009-07-15 23:53:25 +0000854AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000855 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000856 BasicBlock *InsertAtEnd)
Owen Anderson4056ca92009-07-29 22:17:13 +0000857 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000858 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000859 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000860 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000861 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000862}
863
Gordon Henriksen14a55692007-12-10 02:14:30 +0000864// Out of line virtual method, so the vtable, etc has a home.
865AllocationInst::~AllocationInst() {
866}
867
Dan Gohmanaa583d72008-03-24 16:55:58 +0000868void AllocationInst::setAlignment(unsigned Align) {
869 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
870 SubclassData = Log2_32(Align) + 1;
871 assert(getAlignment() == Align && "Alignment representation error!");
872}
873
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000874bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000875 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
876 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000877 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000878}
879
880const Type *AllocationInst::getAllocatedType() const {
881 return getType()->getElementType();
882}
883
Chris Lattner8b291e62008-11-26 02:54:17 +0000884/// isStaticAlloca - Return true if this alloca is in the entry block of the
885/// function and is a constant size. If so, the code generator will fold it
886/// into the prolog/epilog code, so it is basically free.
887bool AllocaInst::isStaticAlloca() const {
888 // Must be constant size.
889 if (!isa<ConstantInt>(getArraySize())) return false;
890
891 // Must be in the entry block.
892 const BasicBlock *Parent = getParent();
893 return Parent == &Parent->getParent()->front();
894}
895
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000896//===----------------------------------------------------------------------===//
897// FreeInst Implementation
898//===----------------------------------------------------------------------===//
899
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000900void FreeInst::AssertOK() {
901 assert(isa<PointerType>(getOperand(0)->getType()) &&
902 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000903}
904
905FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000906 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
907 Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000908 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000909}
910
911FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000912 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
913 Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000914 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000915}
916
917
918//===----------------------------------------------------------------------===//
919// LoadInst Implementation
920//===----------------------------------------------------------------------===//
921
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000922void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000923 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000924 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000925}
926
Daniel Dunbar4975db62009-07-25 04:41:11 +0000927LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000928 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000929 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000930 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000931 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000932 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000933 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000934}
935
Daniel Dunbar4975db62009-07-25 04:41:11 +0000936LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000937 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000938 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000939 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000940 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000941 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000942 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000943}
944
Daniel Dunbar4975db62009-07-25 04:41:11 +0000945LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000946 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000947 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000948 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000949 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000950 setAlignment(0);
951 AssertOK();
952 setName(Name);
953}
954
Daniel Dunbar4975db62009-07-25 04:41:11 +0000955LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000956 unsigned Align, Instruction *InsertBef)
957 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
958 Load, Ptr, InsertBef) {
959 setVolatile(isVolatile);
960 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000961 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000962 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000963}
964
Daniel Dunbar4975db62009-07-25 04:41:11 +0000965LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000966 unsigned Align, BasicBlock *InsertAE)
967 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
968 Load, Ptr, InsertAE) {
969 setVolatile(isVolatile);
970 setAlignment(Align);
971 AssertOK();
972 setName(Name);
973}
974
Daniel Dunbar4975db62009-07-25 04:41:11 +0000975LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000976 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000977 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000978 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000979 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000980 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000981 AssertOK();
982 setName(Name);
983}
984
Daniel Dunbar27096822009-08-11 18:11:15 +0000985
986
987LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
988 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
989 Load, Ptr, InsertBef) {
990 setVolatile(false);
991 setAlignment(0);
992 AssertOK();
993 if (Name && Name[0]) setName(Name);
994}
995
996LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
997 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
998 Load, Ptr, InsertAE) {
999 setVolatile(false);
1000 setAlignment(0);
1001 AssertOK();
1002 if (Name && Name[0]) setName(Name);
1003}
1004
1005LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1006 Instruction *InsertBef)
1007: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1008 Load, Ptr, InsertBef) {
1009 setVolatile(isVolatile);
1010 setAlignment(0);
1011 AssertOK();
1012 if (Name && Name[0]) setName(Name);
1013}
1014
1015LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1016 BasicBlock *InsertAE)
1017 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1018 Load, Ptr, InsertAE) {
1019 setVolatile(isVolatile);
1020 setAlignment(0);
1021 AssertOK();
1022 if (Name && Name[0]) setName(Name);
1023}
1024
Christopher Lamb84485702007-04-22 19:24:39 +00001025void LoadInst::setAlignment(unsigned Align) {
1026 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1027 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1028}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001029
1030//===----------------------------------------------------------------------===//
1031// StoreInst Implementation
1032//===----------------------------------------------------------------------===//
1033
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001034void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001035 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001036 assert(isa<PointerType>(getOperand(1)->getType()) &&
1037 "Ptr must have pointer type!");
1038 assert(getOperand(0)->getType() ==
1039 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001040 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001041}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001042
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001043
1044StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001045 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001046 OperandTraits<StoreInst>::op_begin(this),
1047 OperandTraits<StoreInst>::operands(this),
1048 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001049 Op<0>() = val;
1050 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001051 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001052 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001053 AssertOK();
1054}
1055
1056StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001057 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001058 OperandTraits<StoreInst>::op_begin(this),
1059 OperandTraits<StoreInst>::operands(this),
1060 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001061 Op<0>() = val;
1062 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001063 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001064 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001065 AssertOK();
1066}
1067
Misha Brukmanb1c93172005-04-21 23:48:37 +00001068StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001069 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001070 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001071 OperandTraits<StoreInst>::op_begin(this),
1072 OperandTraits<StoreInst>::operands(this),
1073 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001074 Op<0>() = val;
1075 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001076 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001077 setAlignment(0);
1078 AssertOK();
1079}
1080
1081StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1082 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001083 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001084 OperandTraits<StoreInst>::op_begin(this),
1085 OperandTraits<StoreInst>::operands(this),
1086 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001087 Op<0>() = val;
1088 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001089 setVolatile(isVolatile);
1090 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001091 AssertOK();
1092}
1093
Misha Brukmanb1c93172005-04-21 23:48:37 +00001094StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001095 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001096 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001097 OperandTraits<StoreInst>::op_begin(this),
1098 OperandTraits<StoreInst>::operands(this),
1099 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001100 Op<0>() = val;
1101 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001102 setVolatile(isVolatile);
1103 setAlignment(Align);
1104 AssertOK();
1105}
1106
1107StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001108 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001109 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001110 OperandTraits<StoreInst>::op_begin(this),
1111 OperandTraits<StoreInst>::operands(this),
1112 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001113 Op<0>() = val;
1114 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001115 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001116 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001117 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001118}
1119
Christopher Lamb84485702007-04-22 19:24:39 +00001120void StoreInst::setAlignment(unsigned Align) {
1121 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1122 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1123}
1124
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001125//===----------------------------------------------------------------------===//
1126// GetElementPtrInst Implementation
1127//===----------------------------------------------------------------------===//
1128
Christopher Lambedf07882007-12-17 01:12:55 +00001129static unsigned retrieveAddrSpace(const Value *Val) {
1130 return cast<PointerType>(Val->getType())->getAddressSpace();
1131}
1132
Gabor Greif21ba1842008-06-06 20:28:12 +00001133void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001134 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001135 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1136 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001137 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001138
Chris Lattner79807c3d2007-01-31 19:47:18 +00001139 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001140 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001141
1142 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001143}
1144
Daniel Dunbar4975db62009-07-25 04:41:11 +00001145void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001146 assert(NumOperands == 2 && "NumOperands not initialized?");
1147 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001148 OL[0] = Ptr;
1149 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001150
1151 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001152}
1153
Gabor Greiff6caff662008-05-10 08:32:32 +00001154GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001155 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001156 OperandTraits<GetElementPtrInst>::op_end(this)
1157 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001158 GEPI.getNumOperands()) {
1159 Use *OL = OperandList;
1160 Use *GEPIOL = GEPI.OperandList;
1161 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001162 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001163 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001164}
1165
Chris Lattner82981202005-05-03 05:43:30 +00001166GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001167 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001168 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001169 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001170 GetElementPtr,
1171 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1172 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001173 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001174}
1175
1176GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001177 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001178 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001179 checkType(getIndexedType(Ptr->getType(),Idx)),
1180 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001181 GetElementPtr,
1182 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1183 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001184 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001185}
1186
Chris Lattner6090a422009-03-09 04:46:40 +00001187/// getIndexedType - Returns the type of the element that would be accessed with
1188/// a gep instruction with the specified parameters.
1189///
1190/// The Idxs pointer should point to a continuous piece of memory containing the
1191/// indices, either as Value* or uint64_t.
1192///
1193/// A null type is returned if the indices are invalid for the specified
1194/// pointer type.
1195///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001196template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001197static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1198 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001199 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1200 if (!PTy) return 0; // Type isn't a pointer type!
1201 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001202
Chris Lattner6090a422009-03-09 04:46:40 +00001203 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001204 if (NumIdx == 0)
1205 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001206
1207 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001208 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1209 // that contain opaque types) under the assumption that it will be resolved to
1210 // a sane type later.
1211 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001212 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001213
Dan Gohman1ecaf452008-05-31 00:58:22 +00001214 unsigned CurIdx = 1;
1215 for (; CurIdx != NumIdx; ++CurIdx) {
1216 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1217 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001218 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001219 if (!CT->indexValid(Index)) return 0;
1220 Agg = CT->getTypeAtIndex(Index);
1221
1222 // If the new type forwards to another type, then it is in the middle
1223 // of being refined to another type (and hence, may have dropped all
1224 // references to what it was using before). So, use the new forwarded
1225 // type.
1226 if (const Type *Ty = Agg->getForwardedType())
1227 Agg = Ty;
1228 }
1229 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001230}
1231
Matthijs Kooijman04468622008-07-29 08:46:11 +00001232const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1233 Value* const *Idxs,
1234 unsigned NumIdx) {
1235 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1236}
1237
1238const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1239 uint64_t const *Idxs,
1240 unsigned NumIdx) {
1241 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1242}
1243
Chris Lattner82981202005-05-03 05:43:30 +00001244const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1245 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1246 if (!PTy) return 0; // Type isn't a pointer type!
1247
1248 // Check the pointer index.
1249 if (!PTy->indexValid(Idx)) return 0;
1250
Chris Lattnerc2233332005-05-03 16:44:45 +00001251 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001252}
1253
Chris Lattner45f15572007-04-14 00:12:57 +00001254
1255/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1256/// zeros. If so, the result pointer and the first operand have the same
1257/// value, just potentially different types.
1258bool GetElementPtrInst::hasAllZeroIndices() const {
1259 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1260 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1261 if (!CI->isZero()) return false;
1262 } else {
1263 return false;
1264 }
1265 }
1266 return true;
1267}
1268
Chris Lattner27058292007-04-27 20:35:56 +00001269/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1270/// constant integers. If so, the result pointer and the first operand have
1271/// a constant offset between them.
1272bool GetElementPtrInst::hasAllConstantIndices() const {
1273 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1274 if (!isa<ConstantInt>(getOperand(i)))
1275 return false;
1276 }
1277 return true;
1278}
1279
Dan Gohman1b849082009-09-07 23:54:19 +00001280void GetElementPtrInst::setIsInBounds(bool B) {
1281 cast<GEPOperator>(this)->setIsInBounds(B);
1282}
Chris Lattner45f15572007-04-14 00:12:57 +00001283
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001284//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001285// ExtractElementInst Implementation
1286//===----------------------------------------------------------------------===//
1287
1288ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001289 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001290 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001291 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001292 ExtractElement,
1293 OperandTraits<ExtractElementInst>::op_begin(this),
1294 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001295 assert(isValidOperands(Val, Index) &&
1296 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001297 Op<0>() = Val;
1298 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001299 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001300}
1301
1302ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001303 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001304 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001305 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001306 ExtractElement,
1307 OperandTraits<ExtractElementInst>::op_begin(this),
1308 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001309 assert(isValidOperands(Val, Index) &&
1310 "Invalid extractelement instruction operands!");
1311
Gabor Greif2d3024d2008-05-26 21:33:52 +00001312 Op<0>() = Val;
1313 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001314 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001315}
1316
Chris Lattner65511ff2006-10-05 06:24:58 +00001317
Chris Lattner54865b32006-04-08 04:05:48 +00001318bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Owen Anderson55f1c092009-08-13 21:58:54 +00001319 if (!isa<VectorType>(Val->getType()) ||
1320 Index->getType() != Type::getInt32Ty(Val->getContext()))
Chris Lattner54865b32006-04-08 04:05:48 +00001321 return false;
1322 return true;
1323}
1324
1325
Robert Bocchino23004482006-01-10 19:05:34 +00001326//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001327// InsertElementInst Implementation
1328//===----------------------------------------------------------------------===//
1329
Chris Lattner54865b32006-04-08 04:05:48 +00001330InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001331 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001332 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001333 : Instruction(Vec->getType(), InsertElement,
1334 OperandTraits<InsertElementInst>::op_begin(this),
1335 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001336 assert(isValidOperands(Vec, Elt, Index) &&
1337 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001338 Op<0>() = Vec;
1339 Op<1>() = Elt;
1340 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001341 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001342}
1343
Chris Lattner54865b32006-04-08 04:05:48 +00001344InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001345 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001346 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001347 : Instruction(Vec->getType(), InsertElement,
1348 OperandTraits<InsertElementInst>::op_begin(this),
1349 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001350 assert(isValidOperands(Vec, Elt, Index) &&
1351 "Invalid insertelement instruction operands!");
1352
Gabor Greif2d3024d2008-05-26 21:33:52 +00001353 Op<0>() = Vec;
1354 Op<1>() = Elt;
1355 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001356 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001357}
1358
Chris Lattner54865b32006-04-08 04:05:48 +00001359bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1360 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001361 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001362 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001363
Reid Spencerd84d35b2007-02-15 02:26:10 +00001364 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001365 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001366
Owen Anderson55f1c092009-08-13 21:58:54 +00001367 if (Index->getType() != Type::getInt32Ty(Vec->getContext()))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001368 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001369 return true;
1370}
1371
1372
Robert Bocchinoca27f032006-01-17 20:07:22 +00001373//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001374// ShuffleVectorInst Implementation
1375//===----------------------------------------------------------------------===//
1376
1377ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001378 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001379 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001380: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001381 cast<VectorType>(Mask->getType())->getNumElements()),
1382 ShuffleVector,
1383 OperandTraits<ShuffleVectorInst>::op_begin(this),
1384 OperandTraits<ShuffleVectorInst>::operands(this),
1385 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001386 assert(isValidOperands(V1, V2, Mask) &&
1387 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001388 Op<0>() = V1;
1389 Op<1>() = V2;
1390 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001391 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001392}
1393
1394ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001395 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001396 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001397: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1398 cast<VectorType>(Mask->getType())->getNumElements()),
1399 ShuffleVector,
1400 OperandTraits<ShuffleVectorInst>::op_begin(this),
1401 OperandTraits<ShuffleVectorInst>::operands(this),
1402 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001403 assert(isValidOperands(V1, V2, Mask) &&
1404 "Invalid shuffle vector instruction operands!");
1405
Gabor Greif2d3024d2008-05-26 21:33:52 +00001406 Op<0>() = V1;
1407 Op<1>() = V2;
1408 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001409 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001410}
1411
Mon P Wang25f01062008-11-10 04:46:22 +00001412bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001413 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001414 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001415 return false;
1416
1417 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1418 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Owen Anderson55f1c092009-08-13 21:58:54 +00001419 MaskTy->getElementType() != Type::getInt32Ty(V1->getContext()))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001420 return false;
1421 return true;
1422}
1423
Chris Lattnerf724e342008-03-02 05:28:33 +00001424/// getMaskValue - Return the index from the shuffle mask for the specified
1425/// output result. This is either -1 if the element is undef or a number less
1426/// than 2*numelements.
1427int ShuffleVectorInst::getMaskValue(unsigned i) const {
1428 const Constant *Mask = cast<Constant>(getOperand(2));
1429 if (isa<UndefValue>(Mask)) return -1;
1430 if (isa<ConstantAggregateZero>(Mask)) return 0;
1431 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1432 assert(i < MaskCV->getNumOperands() && "Index out of range");
1433
1434 if (isa<UndefValue>(MaskCV->getOperand(i)))
1435 return -1;
1436 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1437}
1438
Dan Gohman12fce772008-05-15 19:50:34 +00001439//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001440// InsertValueInst Class
1441//===----------------------------------------------------------------------===//
1442
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001443void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001444 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001445 assert(NumOperands == 2 && "NumOperands not initialized?");
1446 Op<0>() = Agg;
1447 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001448
Dan Gohman1ecaf452008-05-31 00:58:22 +00001449 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001450 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001451}
1452
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001453void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001454 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001455 assert(NumOperands == 2 && "NumOperands not initialized?");
1456 Op<0>() = Agg;
1457 Op<1>() = Val;
1458
1459 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001460 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001461}
1462
1463InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001464 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001465 OperandTraits<InsertValueInst>::op_begin(this), 2),
1466 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001467 Op<0>() = IVI.getOperand(0);
1468 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001469 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001470}
1471
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001472InsertValueInst::InsertValueInst(Value *Agg,
1473 Value *Val,
1474 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001475 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001476 Instruction *InsertBefore)
1477 : Instruction(Agg->getType(), InsertValue,
1478 OperandTraits<InsertValueInst>::op_begin(this),
1479 2, InsertBefore) {
1480 init(Agg, Val, Idx, Name);
1481}
1482
1483InsertValueInst::InsertValueInst(Value *Agg,
1484 Value *Val,
1485 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001486 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001487 BasicBlock *InsertAtEnd)
1488 : Instruction(Agg->getType(), InsertValue,
1489 OperandTraits<InsertValueInst>::op_begin(this),
1490 2, InsertAtEnd) {
1491 init(Agg, Val, Idx, Name);
1492}
1493
Dan Gohman0752bff2008-05-23 00:36:11 +00001494//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001495// ExtractValueInst Class
1496//===----------------------------------------------------------------------===//
1497
Gabor Greifcbcc4952008-06-06 21:06:32 +00001498void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001499 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001500 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001501
Dan Gohman1ecaf452008-05-31 00:58:22 +00001502 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001503 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001504}
1505
Daniel Dunbar4975db62009-07-25 04:41:11 +00001506void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001507 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001508
1509 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001510 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001511}
1512
1513ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001514 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001515 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001516 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001517}
1518
Dan Gohman12fce772008-05-15 19:50:34 +00001519// getIndexedType - Returns the type of the element that would be extracted
1520// with an extractvalue instruction with the specified parameters.
1521//
1522// A null type is returned if the indices are invalid for the specified
1523// pointer type.
1524//
1525const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001526 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001527 unsigned NumIdx) {
1528 unsigned CurIdx = 0;
1529 for (; CurIdx != NumIdx; ++CurIdx) {
1530 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001531 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1532 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001533 if (!CT->indexValid(Index)) return 0;
1534 Agg = CT->getTypeAtIndex(Index);
1535
1536 // If the new type forwards to another type, then it is in the middle
1537 // of being refined to another type (and hence, may have dropped all
1538 // references to what it was using before). So, use the new forwarded
1539 // type.
1540 if (const Type *Ty = Agg->getForwardedType())
1541 Agg = Ty;
1542 }
1543 return CurIdx == NumIdx ? Agg : 0;
1544}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001545
Dan Gohman4a3125b2008-06-17 21:07:55 +00001546const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001547 unsigned Idx) {
1548 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001549}
1550
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001551//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001552// BinaryOperator Class
1553//===----------------------------------------------------------------------===//
1554
Dan Gohmana5b96452009-06-04 22:49:04 +00001555/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1556/// type is floating-point, to help provide compatibility with an older API.
1557///
1558static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1559 const Type *Ty) {
1560 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1561 if (Ty->isFPOrFPVector()) {
1562 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1563 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1564 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1565 }
1566 return iType;
1567}
1568
Chris Lattner2195fc42007-02-24 00:55:48 +00001569BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001570 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001571 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001572 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001573 OperandTraits<BinaryOperator>::op_begin(this),
1574 OperandTraits<BinaryOperator>::operands(this),
1575 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001576 Op<0>() = S1;
1577 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001578 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001579 setName(Name);
1580}
1581
1582BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001583 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001584 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001585 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001586 OperandTraits<BinaryOperator>::op_begin(this),
1587 OperandTraits<BinaryOperator>::operands(this),
1588 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001589 Op<0>() = S1;
1590 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001591 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001592 setName(Name);
1593}
1594
1595
1596void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001597 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001598 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001599 assert(LHS->getType() == RHS->getType() &&
1600 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001601#ifndef NDEBUG
1602 switch (iType) {
1603 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001604 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001605 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001606 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001607 assert(getType()->isIntOrIntVector() &&
1608 "Tried to create an integer operation on a non-integer type!");
1609 break;
1610 case FAdd: case FSub:
1611 case FMul:
1612 assert(getType() == LHS->getType() &&
1613 "Arithmetic operation should return same type as operands!");
1614 assert(getType()->isFPOrFPVector() &&
1615 "Tried to create a floating-point operation on a "
1616 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001617 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001618 case UDiv:
1619 case SDiv:
1620 assert(getType() == LHS->getType() &&
1621 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001622 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1623 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001624 "Incorrect operand type (not integer) for S/UDIV");
1625 break;
1626 case FDiv:
1627 assert(getType() == LHS->getType() &&
1628 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001629 assert(getType()->isFPOrFPVector() &&
1630 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001631 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001632 case URem:
1633 case SRem:
1634 assert(getType() == LHS->getType() &&
1635 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001636 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1637 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001638 "Incorrect operand type (not integer) for S/UREM");
1639 break;
1640 case FRem:
1641 assert(getType() == LHS->getType() &&
1642 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001643 assert(getType()->isFPOrFPVector() &&
1644 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001645 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001646 case Shl:
1647 case LShr:
1648 case AShr:
1649 assert(getType() == LHS->getType() &&
1650 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001651 assert((getType()->isInteger() ||
1652 (isa<VectorType>(getType()) &&
1653 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1654 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001655 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001656 case And: case Or:
1657 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001658 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001659 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001660 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001661 (isa<VectorType>(getType()) &&
1662 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001663 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001664 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001665 default:
1666 break;
1667 }
1668#endif
1669}
1670
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001671BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001672 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001673 Instruction *InsertBefore) {
1674 assert(S1->getType() == S2->getType() &&
1675 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001676 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001677}
1678
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001679BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001680 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001681 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001682 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001683 InsertAtEnd->getInstList().push_back(Res);
1684 return Res;
1685}
1686
Dan Gohman5476cfd2009-08-12 16:23:25 +00001687BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001688 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001689 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001690 return new BinaryOperator(Instruction::Sub,
1691 zero, Op,
1692 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001693}
1694
Dan Gohman5476cfd2009-08-12 16:23:25 +00001695BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001696 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001697 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001698 return new BinaryOperator(Instruction::Sub,
1699 zero, Op,
1700 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001701}
1702
Dan Gohman5476cfd2009-08-12 16:23:25 +00001703BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001704 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001705 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001706 return new BinaryOperator(Instruction::FSub,
1707 zero, Op,
1708 Op->getType(), Name, InsertBefore);
1709}
1710
Dan Gohman5476cfd2009-08-12 16:23:25 +00001711BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001712 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001713 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001714 return new BinaryOperator(Instruction::FSub,
1715 zero, Op,
1716 Op->getType(), Name, InsertAtEnd);
1717}
1718
Dan Gohman5476cfd2009-08-12 16:23:25 +00001719BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001720 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001721 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001722 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001723 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001724 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001725 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001726 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001727 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001728 }
1729
1730 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001731 Op->getType(), Name, InsertBefore);
1732}
1733
Dan Gohman5476cfd2009-08-12 16:23:25 +00001734BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001735 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001736 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001737 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001738 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001739 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001740 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001741 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001742 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001743 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001744 }
1745
1746 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001747 Op->getType(), Name, InsertAtEnd);
1748}
1749
1750
1751// isConstantAllOnes - Helper function for several functions below
1752static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001753 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1754 return CI->isAllOnesValue();
1755 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1756 return CV->isAllOnesValue();
1757 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001758}
1759
Owen Andersonbb2501b2009-07-13 22:18:28 +00001760bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001761 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1762 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001763 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1764 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001765 return false;
1766}
1767
Owen Andersonbb2501b2009-07-13 22:18:28 +00001768bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001769 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1770 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001771 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001772 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001773 return false;
1774}
1775
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001776bool BinaryOperator::isNot(const Value *V) {
1777 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1778 return (Bop->getOpcode() == Instruction::Xor &&
1779 (isConstantAllOnes(Bop->getOperand(1)) ||
1780 isConstantAllOnes(Bop->getOperand(0))));
1781 return false;
1782}
1783
Chris Lattner2c7d1772005-04-24 07:28:37 +00001784Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001785 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001786}
1787
Chris Lattner2c7d1772005-04-24 07:28:37 +00001788const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1789 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001790}
1791
Dan Gohmana5b96452009-06-04 22:49:04 +00001792Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001793 return cast<BinaryOperator>(BinOp)->getOperand(1);
1794}
1795
1796const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1797 return getFNegArgument(const_cast<Value*>(BinOp));
1798}
1799
Chris Lattner2c7d1772005-04-24 07:28:37 +00001800Value *BinaryOperator::getNotArgument(Value *BinOp) {
1801 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1802 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1803 Value *Op0 = BO->getOperand(0);
1804 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001805 if (isConstantAllOnes(Op0)) return Op1;
1806
1807 assert(isConstantAllOnes(Op1));
1808 return Op0;
1809}
1810
Chris Lattner2c7d1772005-04-24 07:28:37 +00001811const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1812 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001813}
1814
1815
1816// swapOperands - Exchange the two operands to this instruction. This
1817// instruction is safe to use on any binary instruction and does not
1818// modify the semantics of the instruction. If the instruction is
1819// order dependent (SetLT f.e.) the opcode is changed.
1820//
1821bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001822 if (!isCommutative())
1823 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001824 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001825 return false;
1826}
1827
Dan Gohman1b849082009-09-07 23:54:19 +00001828void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1829 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1830}
1831
1832void BinaryOperator::setHasNoSignedWrap(bool b) {
1833 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1834}
1835
1836void BinaryOperator::setIsExact(bool b) {
1837 cast<SDivOperator>(this)->setIsExact(b);
1838}
1839
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001840//===----------------------------------------------------------------------===//
1841// CastInst Class
1842//===----------------------------------------------------------------------===//
1843
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001844// Just determine if this cast only deals with integral->integral conversion.
1845bool CastInst::isIntegerCast() const {
1846 switch (getOpcode()) {
1847 default: return false;
1848 case Instruction::ZExt:
1849 case Instruction::SExt:
1850 case Instruction::Trunc:
1851 return true;
1852 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001853 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001854 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001855}
1856
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001857bool CastInst::isLosslessCast() const {
1858 // Only BitCast can be lossless, exit fast if we're not BitCast
1859 if (getOpcode() != Instruction::BitCast)
1860 return false;
1861
1862 // Identity cast is always lossless
1863 const Type* SrcTy = getOperand(0)->getType();
1864 const Type* DstTy = getType();
1865 if (SrcTy == DstTy)
1866 return true;
1867
Reid Spencer8d9336d2006-12-31 05:26:44 +00001868 // Pointer to pointer is always lossless.
1869 if (isa<PointerType>(SrcTy))
1870 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001871 return false; // Other types have no identity values
1872}
1873
1874/// This function determines if the CastInst does not require any bits to be
1875/// changed in order to effect the cast. Essentially, it identifies cases where
1876/// no code gen is necessary for the cast, hence the name no-op cast. For
1877/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001878/// # bitcast i32* %x to i8*
1879/// # bitcast <2 x i32> %x to <4 x i16>
1880/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001881/// @brief Determine if a cast is a no-op.
1882bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1883 switch (getOpcode()) {
1884 default:
1885 assert(!"Invalid CastOp");
1886 case Instruction::Trunc:
1887 case Instruction::ZExt:
1888 case Instruction::SExt:
1889 case Instruction::FPTrunc:
1890 case Instruction::FPExt:
1891 case Instruction::UIToFP:
1892 case Instruction::SIToFP:
1893 case Instruction::FPToUI:
1894 case Instruction::FPToSI:
1895 return false; // These always modify bits
1896 case Instruction::BitCast:
1897 return true; // BitCast never modifies bits.
1898 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001899 return IntPtrTy->getScalarSizeInBits() ==
1900 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001901 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001902 return IntPtrTy->getScalarSizeInBits() ==
1903 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001904 }
1905}
1906
1907/// This function determines if a pair of casts can be eliminated and what
1908/// opcode should be used in the elimination. This assumes that there are two
1909/// instructions like this:
1910/// * %F = firstOpcode SrcTy %x to MidTy
1911/// * %S = secondOpcode MidTy %F to DstTy
1912/// The function returns a resultOpcode so these two casts can be replaced with:
1913/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1914/// If no such cast is permited, the function returns 0.
1915unsigned CastInst::isEliminableCastPair(
1916 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1917 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1918{
1919 // Define the 144 possibilities for these two cast instructions. The values
1920 // in this matrix determine what to do in a given situation and select the
1921 // case in the switch below. The rows correspond to firstOp, the columns
1922 // correspond to secondOp. In looking at the table below, keep in mind
1923 // the following cast properties:
1924 //
1925 // Size Compare Source Destination
1926 // Operator Src ? Size Type Sign Type Sign
1927 // -------- ------------ ------------------- ---------------------
1928 // TRUNC > Integer Any Integral Any
1929 // ZEXT < Integral Unsigned Integer Any
1930 // SEXT < Integral Signed Integer Any
1931 // FPTOUI n/a FloatPt n/a Integral Unsigned
1932 // FPTOSI n/a FloatPt n/a Integral Signed
1933 // UITOFP n/a Integral Unsigned FloatPt n/a
1934 // SITOFP n/a Integral Signed FloatPt n/a
1935 // FPTRUNC > FloatPt n/a FloatPt n/a
1936 // FPEXT < FloatPt n/a FloatPt n/a
1937 // PTRTOINT n/a Pointer n/a Integral Unsigned
1938 // INTTOPTR n/a Integral Unsigned Pointer n/a
1939 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001940 //
1941 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001942 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1943 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001944 // of the produced value (we no longer know the top-part is all zeros).
1945 // Further this conversion is often much more expensive for typical hardware,
1946 // and causes issues when building libgcc. We disallow fptosi+sext for the
1947 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001948 const unsigned numCastOps =
1949 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1950 static const uint8_t CastResults[numCastOps][numCastOps] = {
1951 // T F F U S F F P I B -+
1952 // R Z S P P I I T P 2 N T |
1953 // U E E 2 2 2 2 R E I T C +- secondOp
1954 // N X X U S F F N X N 2 V |
1955 // C T T I I P P C T T P T -+
1956 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1957 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1958 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001959 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1960 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001961 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1962 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1963 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1964 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1965 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1966 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1967 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1968 };
1969
1970 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1971 [secondOp-Instruction::CastOpsBegin];
1972 switch (ElimCase) {
1973 case 0:
1974 // categorically disallowed
1975 return 0;
1976 case 1:
1977 // allowed, use first cast's opcode
1978 return firstOp;
1979 case 2:
1980 // allowed, use second cast's opcode
1981 return secondOp;
1982 case 3:
1983 // no-op cast in second op implies firstOp as long as the DestTy
1984 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001985 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001986 return firstOp;
1987 return 0;
1988 case 4:
1989 // no-op cast in second op implies firstOp as long as the DestTy
1990 // is floating point
1991 if (DstTy->isFloatingPoint())
1992 return firstOp;
1993 return 0;
1994 case 5:
1995 // no-op cast in first op implies secondOp as long as the SrcTy
1996 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001997 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001998 return secondOp;
1999 return 0;
2000 case 6:
2001 // no-op cast in first op implies secondOp as long as the SrcTy
2002 // is a floating point
2003 if (SrcTy->isFloatingPoint())
2004 return secondOp;
2005 return 0;
2006 case 7: {
2007 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002008 if (!IntPtrTy)
2009 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002010 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2011 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002012 if (MidSize >= PtrSize)
2013 return Instruction::BitCast;
2014 return 0;
2015 }
2016 case 8: {
2017 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2018 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2019 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002020 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2021 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002022 if (SrcSize == DstSize)
2023 return Instruction::BitCast;
2024 else if (SrcSize < DstSize)
2025 return firstOp;
2026 return secondOp;
2027 }
2028 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2029 return Instruction::ZExt;
2030 case 10:
2031 // fpext followed by ftrunc is allowed if the bit size returned to is
2032 // the same as the original, in which case its just a bitcast
2033 if (SrcTy == DstTy)
2034 return Instruction::BitCast;
2035 return 0; // If the types are not the same we can't eliminate it.
2036 case 11:
2037 // bitcast followed by ptrtoint is allowed as long as the bitcast
2038 // is a pointer to pointer cast.
2039 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
2040 return secondOp;
2041 return 0;
2042 case 12:
2043 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2044 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
2045 return firstOp;
2046 return 0;
2047 case 13: {
2048 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002049 if (!IntPtrTy)
2050 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002051 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2052 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2053 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002054 if (SrcSize <= PtrSize && SrcSize == DstSize)
2055 return Instruction::BitCast;
2056 return 0;
2057 }
2058 case 99:
2059 // cast combination can't happen (error in input). This is for all cases
2060 // where the MidTy is not the same for the two cast instructions.
2061 assert(!"Invalid Cast Combination");
2062 return 0;
2063 default:
2064 assert(!"Error in CastResults table!!!");
2065 return 0;
2066 }
2067 return 0;
2068}
2069
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002070CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002071 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002072 // Construct and return the appropriate CastInst subclass
2073 switch (op) {
2074 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2075 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2076 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2077 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2078 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2079 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2080 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2081 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2082 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2083 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2084 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2085 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2086 default:
2087 assert(!"Invalid opcode provided");
2088 }
2089 return 0;
2090}
2091
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002092CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002093 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002094 // Construct and return the appropriate CastInst subclass
2095 switch (op) {
2096 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2097 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2098 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2099 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2100 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2101 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2102 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2103 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2104 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2105 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2106 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2107 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2108 default:
2109 assert(!"Invalid opcode provided");
2110 }
2111 return 0;
2112}
2113
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002114CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002115 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002116 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002117 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002118 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2119 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002120}
2121
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002122CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002123 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002124 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002125 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002126 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2127 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002128}
2129
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002130CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002131 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002132 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002133 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002134 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2135 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002136}
2137
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002138CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002139 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002140 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002141 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002142 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2143 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002144}
2145
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002146CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002147 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002148 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002149 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002150 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2151 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002152}
2153
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002154CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002155 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002156 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002157 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002158 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2159 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002160}
2161
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002162CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002163 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002164 BasicBlock *InsertAtEnd) {
2165 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002166 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002167 "Invalid cast");
2168
Chris Lattner03c49532007-01-15 02:27:26 +00002169 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002170 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2171 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002172}
2173
2174/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002175CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002176 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002177 Instruction *InsertBefore) {
2178 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002179 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002180 "Invalid cast");
2181
Chris Lattner03c49532007-01-15 02:27:26 +00002182 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002183 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2184 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002185}
2186
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002187CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002188 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002189 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002190 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002191 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2192 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002193 Instruction::CastOps opcode =
2194 (SrcBits == DstBits ? Instruction::BitCast :
2195 (SrcBits > DstBits ? Instruction::Trunc :
2196 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002197 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002198}
2199
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002200CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002201 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002202 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002203 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2204 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002205 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2206 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002207 Instruction::CastOps opcode =
2208 (SrcBits == DstBits ? Instruction::BitCast :
2209 (SrcBits > DstBits ? Instruction::Trunc :
2210 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002211 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002212}
2213
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002214CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002215 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002216 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002217 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002218 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002219 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2220 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002221 Instruction::CastOps opcode =
2222 (SrcBits == DstBits ? Instruction::BitCast :
2223 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002224 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002225}
2226
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002227CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002228 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002229 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002230 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002231 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002232 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2233 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002234 Instruction::CastOps opcode =
2235 (SrcBits == DstBits ? Instruction::BitCast :
2236 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002237 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002238}
2239
Duncan Sands55e50902008-01-06 10:12:28 +00002240// Check whether it is valid to call getCastOpcode for these types.
2241// This routine must be kept in sync with getCastOpcode.
2242bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2243 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2244 return false;
2245
2246 if (SrcTy == DestTy)
2247 return true;
2248
2249 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002250 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2251 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002252
2253 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002254 if (DestTy->isInteger()) { // Casting to integral
2255 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002256 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002257 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002258 return true;
2259 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002260 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002261 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002262 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002263 return isa<PointerType>(SrcTy);
2264 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002265 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2266 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002267 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002268 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002269 return true;
2270 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002271 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002272 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002273 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002274 return false;
2275 }
2276 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002277 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002278 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002279 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002280 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002281 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002282 return DestPTy->getBitWidth() == SrcBits;
2283 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002284 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2285 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002286 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002287 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002288 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002289 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002290 return false;
2291 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002292 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002293 return false;
2294 }
2295}
2296
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002297// Provide a way to get a "cast" where the cast opcode is inferred from the
2298// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002299// logic in the castIsValid function below. This axiom should hold:
2300// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2301// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002302// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002303// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002304Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002305CastInst::getCastOpcode(
2306 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002307 // Get the bit sizes, we'll need these
2308 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002309 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2310 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002311
Duncan Sands55e50902008-01-06 10:12:28 +00002312 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2313 "Only first class types are castable!");
2314
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002315 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002316 if (DestTy->isInteger()) { // Casting to integral
2317 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002318 if (DestBits < SrcBits)
2319 return Trunc; // int -> smaller int
2320 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002321 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002322 return SExt; // signed -> SEXT
2323 else
2324 return ZExt; // unsigned -> ZEXT
2325 } else {
2326 return BitCast; // Same size, No-op cast
2327 }
2328 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002329 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002330 return FPToSI; // FP -> sint
2331 else
2332 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002333 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002334 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002335 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002336 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002337 return BitCast; // Same size, no-op cast
2338 } else {
2339 assert(isa<PointerType>(SrcTy) &&
2340 "Casting from a value that is not first-class type");
2341 return PtrToInt; // ptr -> int
2342 }
2343 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002344 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002345 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002346 return SIToFP; // sint -> FP
2347 else
2348 return UIToFP; // uint -> FP
2349 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2350 if (DestBits < SrcBits) {
2351 return FPTrunc; // FP -> smaller FP
2352 } else if (DestBits > SrcBits) {
2353 return FPExt; // FP -> larger FP
2354 } else {
2355 return BitCast; // same size, no-op cast
2356 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002357 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002358 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002359 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002360 PTy = NULL;
2361 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002363 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002364 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002365 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2366 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002367 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002368 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002369 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002370 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002371 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002372 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002373 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002374 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002375 }
2376 } else if (isa<PointerType>(DestTy)) {
2377 if (isa<PointerType>(SrcTy)) {
2378 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002379 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002380 return IntToPtr; // int -> ptr
2381 } else {
2382 assert(!"Casting pointer to other than pointer or int");
2383 }
2384 } else {
2385 assert(!"Casting to type that is not first-class");
2386 }
2387
2388 // If we fall through to here we probably hit an assertion cast above
2389 // and assertions are not turned on. Anything we return is an error, so
2390 // BitCast is as good a choice as any.
2391 return BitCast;
2392}
2393
2394//===----------------------------------------------------------------------===//
2395// CastInst SubClass Constructors
2396//===----------------------------------------------------------------------===//
2397
2398/// Check that the construction parameters for a CastInst are correct. This
2399/// could be broken out into the separate constructors but it is useful to have
2400/// it in one place and to eliminate the redundant code for getting the sizes
2401/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002402bool
2403CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002404
2405 // Check for type sanity on the arguments
2406 const Type *SrcTy = S->getType();
2407 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2408 return false;
2409
2410 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002411 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2412 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002413
2414 // Switch on the opcode provided
2415 switch (op) {
2416 default: return false; // This is an input error
2417 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002418 return SrcTy->isIntOrIntVector() &&
2419 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002421 return SrcTy->isIntOrIntVector() &&
2422 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002423 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002424 return SrcTy->isIntOrIntVector() &&
2425 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002426 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002427 return SrcTy->isFPOrFPVector() &&
2428 DstTy->isFPOrFPVector() &&
2429 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002430 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002431 return SrcTy->isFPOrFPVector() &&
2432 DstTy->isFPOrFPVector() &&
2433 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002434 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002436 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2437 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002438 return SVTy->getElementType()->isIntOrIntVector() &&
2439 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002440 SVTy->getNumElements() == DVTy->getNumElements();
2441 }
2442 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002443 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002445 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002446 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2447 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002448 return SVTy->getElementType()->isFPOrFPVector() &&
2449 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002450 SVTy->getNumElements() == DVTy->getNumElements();
2451 }
2452 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002453 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002454 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002455 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002457 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002458 case Instruction::BitCast:
2459 // BitCast implies a no-op cast of type only. No bits change.
2460 // However, you can't cast pointers to anything but pointers.
2461 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2462 return false;
2463
Duncan Sands55e50902008-01-06 10:12:28 +00002464 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465 // these cases, the cast is okay if the source and destination bit widths
2466 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002467 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002468 }
2469}
2470
2471TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002472 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002473) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002474 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002475}
2476
2477TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002478 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002479) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002480 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002481}
2482
2483ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002484 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002485) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002486 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002487}
2488
2489ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002490 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002491) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002492 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002493}
2494SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002495 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002496) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002497 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002498}
2499
Jeff Cohencc08c832006-12-02 02:22:01 +00002500SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002501 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002503 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002504}
2505
2506FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002507 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002508) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002509 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002510}
2511
2512FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002513 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002514) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002515 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516}
2517
2518FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002519 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002520) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002521 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522}
2523
2524FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002525 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002526) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002527 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528}
2529
2530UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002531 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002533 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002534}
2535
2536UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002537 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002539 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540}
2541
2542SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002543 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002545 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002546}
2547
2548SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002549 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002551 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002552}
2553
2554FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002555 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002556) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002557 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002558}
2559
2560FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002561 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002563 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564}
2565
2566FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002567 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002569 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002570}
2571
2572FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002573 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002575 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002576}
2577
2578PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002579 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002581 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002582}
2583
2584PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002585 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002587 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002588}
2589
2590IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002591 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002592) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002593 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594}
2595
2596IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002597 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002598) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002599 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600}
2601
2602BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002603 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002605 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606}
2607
2608BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002609 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002610) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002611 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002613
2614//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002615// CmpInst Classes
2616//===----------------------------------------------------------------------===//
2617
Nate Begemand2195702008-05-12 19:01:56 +00002618CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002619 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002620 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002621 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002622 OperandTraits<CmpInst>::op_begin(this),
2623 OperandTraits<CmpInst>::operands(this),
2624 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002625 Op<0>() = LHS;
2626 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002627 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002628 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002629}
Gabor Greiff6caff662008-05-10 08:32:32 +00002630
Nate Begemand2195702008-05-12 19:01:56 +00002631CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002632 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002633 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002634 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002635 OperandTraits<CmpInst>::op_begin(this),
2636 OperandTraits<CmpInst>::operands(this),
2637 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002638 Op<0>() = LHS;
2639 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002640 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002641 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002642}
2643
2644CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002645CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002646 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002647 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002648 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002649 if (InsertBefore)
2650 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2651 S1, S2, Name);
2652 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002653 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002654 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002655 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002656
2657 if (InsertBefore)
2658 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2659 S1, S2, Name);
2660 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002661 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002662 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002663}
2664
2665CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002666CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002667 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002668 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002669 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2670 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002671 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002672 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2673 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002674}
2675
2676void CmpInst::swapOperands() {
2677 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2678 IC->swapOperands();
2679 else
2680 cast<FCmpInst>(this)->swapOperands();
2681}
2682
2683bool CmpInst::isCommutative() {
2684 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2685 return IC->isCommutative();
2686 return cast<FCmpInst>(this)->isCommutative();
2687}
2688
2689bool CmpInst::isEquality() {
2690 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2691 return IC->isEquality();
2692 return cast<FCmpInst>(this)->isEquality();
2693}
2694
2695
Dan Gohman4e724382008-05-31 02:47:54 +00002696CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002697 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002698 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002699 case ICMP_EQ: return ICMP_NE;
2700 case ICMP_NE: return ICMP_EQ;
2701 case ICMP_UGT: return ICMP_ULE;
2702 case ICMP_ULT: return ICMP_UGE;
2703 case ICMP_UGE: return ICMP_ULT;
2704 case ICMP_ULE: return ICMP_UGT;
2705 case ICMP_SGT: return ICMP_SLE;
2706 case ICMP_SLT: return ICMP_SGE;
2707 case ICMP_SGE: return ICMP_SLT;
2708 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002709
Dan Gohman4e724382008-05-31 02:47:54 +00002710 case FCMP_OEQ: return FCMP_UNE;
2711 case FCMP_ONE: return FCMP_UEQ;
2712 case FCMP_OGT: return FCMP_ULE;
2713 case FCMP_OLT: return FCMP_UGE;
2714 case FCMP_OGE: return FCMP_ULT;
2715 case FCMP_OLE: return FCMP_UGT;
2716 case FCMP_UEQ: return FCMP_ONE;
2717 case FCMP_UNE: return FCMP_OEQ;
2718 case FCMP_UGT: return FCMP_OLE;
2719 case FCMP_ULT: return FCMP_OGE;
2720 case FCMP_UGE: return FCMP_OLT;
2721 case FCMP_ULE: return FCMP_OGT;
2722 case FCMP_ORD: return FCMP_UNO;
2723 case FCMP_UNO: return FCMP_ORD;
2724 case FCMP_TRUE: return FCMP_FALSE;
2725 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002726 }
2727}
2728
Reid Spencer266e42b2006-12-23 06:05:41 +00002729ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2730 switch (pred) {
2731 default: assert(! "Unknown icmp predicate!");
2732 case ICMP_EQ: case ICMP_NE:
2733 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2734 return pred;
2735 case ICMP_UGT: return ICMP_SGT;
2736 case ICMP_ULT: return ICMP_SLT;
2737 case ICMP_UGE: return ICMP_SGE;
2738 case ICMP_ULE: return ICMP_SLE;
2739 }
2740}
2741
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002742ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2743 switch (pred) {
2744 default: assert(! "Unknown icmp predicate!");
2745 case ICMP_EQ: case ICMP_NE:
2746 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2747 return pred;
2748 case ICMP_SGT: return ICMP_UGT;
2749 case ICMP_SLT: return ICMP_ULT;
2750 case ICMP_SGE: return ICMP_UGE;
2751 case ICMP_SLE: return ICMP_ULE;
2752 }
2753}
2754
Reid Spencer266e42b2006-12-23 06:05:41 +00002755bool ICmpInst::isSignedPredicate(Predicate pred) {
2756 switch (pred) {
2757 default: assert(! "Unknown icmp predicate!");
2758 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2759 return true;
2760 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2761 case ICMP_UGE: case ICMP_ULE:
2762 return false;
2763 }
2764}
2765
Reid Spencer0286bc12007-02-28 22:00:54 +00002766/// Initialize a set of values that all satisfy the condition with C.
2767///
2768ConstantRange
2769ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2770 APInt Lower(C);
2771 APInt Upper(C);
2772 uint32_t BitWidth = C.getBitWidth();
2773 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002774 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002775 case ICmpInst::ICMP_EQ: Upper++; break;
2776 case ICmpInst::ICMP_NE: Lower++; break;
2777 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2778 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2779 case ICmpInst::ICMP_UGT:
2780 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2781 break;
2782 case ICmpInst::ICMP_SGT:
2783 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2784 break;
2785 case ICmpInst::ICMP_ULE:
2786 Lower = APInt::getMinValue(BitWidth); Upper++;
2787 break;
2788 case ICmpInst::ICMP_SLE:
2789 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2790 break;
2791 case ICmpInst::ICMP_UGE:
2792 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2793 break;
2794 case ICmpInst::ICMP_SGE:
2795 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2796 break;
2797 }
2798 return ConstantRange(Lower, Upper);
2799}
2800
Dan Gohman4e724382008-05-31 02:47:54 +00002801CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002802 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002803 default: assert(!"Unknown cmp predicate!");
2804 case ICMP_EQ: case ICMP_NE:
2805 return pred;
2806 case ICMP_SGT: return ICMP_SLT;
2807 case ICMP_SLT: return ICMP_SGT;
2808 case ICMP_SGE: return ICMP_SLE;
2809 case ICMP_SLE: return ICMP_SGE;
2810 case ICMP_UGT: return ICMP_ULT;
2811 case ICMP_ULT: return ICMP_UGT;
2812 case ICMP_UGE: return ICMP_ULE;
2813 case ICMP_ULE: return ICMP_UGE;
2814
Reid Spencerd9436b62006-11-20 01:22:35 +00002815 case FCMP_FALSE: case FCMP_TRUE:
2816 case FCMP_OEQ: case FCMP_ONE:
2817 case FCMP_UEQ: case FCMP_UNE:
2818 case FCMP_ORD: case FCMP_UNO:
2819 return pred;
2820 case FCMP_OGT: return FCMP_OLT;
2821 case FCMP_OLT: return FCMP_OGT;
2822 case FCMP_OGE: return FCMP_OLE;
2823 case FCMP_OLE: return FCMP_OGE;
2824 case FCMP_UGT: return FCMP_ULT;
2825 case FCMP_ULT: return FCMP_UGT;
2826 case FCMP_UGE: return FCMP_ULE;
2827 case FCMP_ULE: return FCMP_UGE;
2828 }
2829}
2830
Reid Spencer266e42b2006-12-23 06:05:41 +00002831bool CmpInst::isUnsigned(unsigned short predicate) {
2832 switch (predicate) {
2833 default: return false;
2834 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2835 case ICmpInst::ICMP_UGE: return true;
2836 }
2837}
2838
2839bool CmpInst::isSigned(unsigned short predicate){
2840 switch (predicate) {
2841 default: return false;
2842 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2843 case ICmpInst::ICMP_SGE: return true;
2844 }
2845}
2846
2847bool CmpInst::isOrdered(unsigned short predicate) {
2848 switch (predicate) {
2849 default: return false;
2850 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2851 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2852 case FCmpInst::FCMP_ORD: return true;
2853 }
2854}
2855
2856bool CmpInst::isUnordered(unsigned short predicate) {
2857 switch (predicate) {
2858 default: return false;
2859 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2860 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2861 case FCmpInst::FCMP_UNO: return true;
2862 }
2863}
2864
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002865//===----------------------------------------------------------------------===//
2866// SwitchInst Implementation
2867//===----------------------------------------------------------------------===//
2868
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002869void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002870 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002871 ReservedSpace = 2+NumCases*2;
2872 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002873 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002874
Gabor Greif2d3024d2008-05-26 21:33:52 +00002875 OperandList[0] = Value;
2876 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002877}
2878
Chris Lattner2195fc42007-02-24 00:55:48 +00002879/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2880/// switch on and a default destination. The number of additional cases can
2881/// be specified here to make memory allocation more efficient. This
2882/// constructor can also autoinsert before another instruction.
2883SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2884 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002885 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2886 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002887 init(Value, Default, NumCases);
2888}
2889
2890/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2891/// switch on and a default destination. The number of additional cases can
2892/// be specified here to make memory allocation more efficient. This
2893/// constructor also autoinserts at the end of the specified BasicBlock.
2894SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2895 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002896 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2897 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002898 init(Value, Default, NumCases);
2899}
2900
Misha Brukmanb1c93172005-04-21 23:48:37 +00002901SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002902 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002903 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002904 Use *OL = OperandList, *InOL = SI.OperandList;
2905 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002906 OL[i] = InOL[i];
2907 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002908 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002909 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002910}
2911
Gordon Henriksen14a55692007-12-10 02:14:30 +00002912SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002913 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002914}
2915
2916
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002917/// addCase - Add an entry to the switch instruction...
2918///
Chris Lattner47ac1872005-02-24 05:32:09 +00002919void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002920 unsigned OpNo = NumOperands;
2921 if (OpNo+2 > ReservedSpace)
2922 resizeOperands(0); // Get more space!
2923 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002924 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002925 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002926 OperandList[OpNo] = OnVal;
2927 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002928}
2929
2930/// removeCase - This method removes the specified successor from the switch
2931/// instruction. Note that this cannot be used to remove the default
2932/// destination (successor #0).
2933///
2934void SwitchInst::removeCase(unsigned idx) {
2935 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002936 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2937
2938 unsigned NumOps = getNumOperands();
2939 Use *OL = OperandList;
2940
2941 // Move everything after this operand down.
2942 //
2943 // FIXME: we could just swap with the end of the list, then erase. However,
2944 // client might not expect this to happen. The code as it is thrashes the
2945 // use/def lists, which is kinda lame.
2946 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2947 OL[i-2] = OL[i];
2948 OL[i-2+1] = OL[i+1];
2949 }
2950
2951 // Nuke the last value.
2952 OL[NumOps-2].set(0);
2953 OL[NumOps-2+1].set(0);
2954 NumOperands = NumOps-2;
2955}
2956
2957/// resizeOperands - resize operands - This adjusts the length of the operands
2958/// list according to the following behavior:
2959/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002960/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002961/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2962/// 3. If NumOps == NumOperands, trim the reserved space.
2963///
2964void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002965 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002966 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002967 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002968 } else if (NumOps*2 > NumOperands) {
2969 // No resize needed.
2970 if (ReservedSpace >= NumOps) return;
2971 } else if (NumOps == NumOperands) {
2972 if (ReservedSpace == NumOps) return;
2973 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002974 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002975 }
2976
2977 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002978 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002979 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002980 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002981 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002982 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002983 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002984 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002985}
2986
2987
2988BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2989 return getSuccessor(idx);
2990}
2991unsigned SwitchInst::getNumSuccessorsV() const {
2992 return getNumSuccessors();
2993}
2994void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2995 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002996}
Chris Lattnerf22be932004-10-15 23:52:53 +00002997
Chris Lattnerf22be932004-10-15 23:52:53 +00002998// Define these methods here so vtables don't get emitted into every translation
2999// unit that uses these classes.
3000
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003001GetElementPtrInst *GetElementPtrInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003002 GetElementPtrInst *New = new(getNumOperands()) GetElementPtrInst(*this);
3003 New->SubclassOptionalData = SubclassOptionalData;
3004 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00003005}
3006
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003007BinaryOperator *BinaryOperator::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003008 BinaryOperator *New = Create(getOpcode(), Op<0>(), Op<1>());
3009 New->SubclassOptionalData = SubclassOptionalData;
3010 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00003011}
3012
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003013FCmpInst* FCmpInst::clone(LLVMContext &Context) const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003014 FCmpInst *New = new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003015 New->SubclassOptionalData = SubclassOptionalData;
3016 return New;
Chris Lattner0b490b02007-08-24 20:48:18 +00003017}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003018ICmpInst* ICmpInst::clone(LLVMContext &Context) const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003019 ICmpInst *New = new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003020 New->SubclassOptionalData = SubclassOptionalData;
3021 return New;
Reid Spencerd9436b62006-11-20 01:22:35 +00003022}
3023
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003024ExtractValueInst *ExtractValueInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003025 ExtractValueInst *New = new ExtractValueInst(*this);
3026 New->SubclassOptionalData = SubclassOptionalData;
3027 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00003028}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003029InsertValueInst *InsertValueInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003030 InsertValueInst *New = new InsertValueInst(*this);
3031 New->SubclassOptionalData = SubclassOptionalData;
3032 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00003033}
3034
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003035MallocInst *MallocInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003036 MallocInst *New = new MallocInst(getAllocatedType(),
3037 (Value*)getOperand(0),
3038 getAlignment());
3039 New->SubclassOptionalData = SubclassOptionalData;
3040 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003041}
Dan Gohman0752bff2008-05-23 00:36:11 +00003042
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003043AllocaInst *AllocaInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003044 AllocaInst *New = new AllocaInst(getAllocatedType(),
3045 (Value*)getOperand(0),
3046 getAlignment());
3047 New->SubclassOptionalData = SubclassOptionalData;
3048 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003049}
3050
3051FreeInst *FreeInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003052 FreeInst *New = new FreeInst(getOperand(0));
3053 New->SubclassOptionalData = SubclassOptionalData;
3054 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003055}
3056
3057LoadInst *LoadInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003058 LoadInst *New = new LoadInst(getOperand(0),
3059 Twine(), isVolatile(),
3060 getAlignment());
3061 New->SubclassOptionalData = SubclassOptionalData;
3062 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003063}
3064
3065StoreInst *StoreInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003066 StoreInst *New = new StoreInst(getOperand(0), getOperand(1),
3067 isVolatile(), getAlignment());
3068 New->SubclassOptionalData = SubclassOptionalData;
3069 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003070}
3071
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003072TruncInst *TruncInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003073 TruncInst *New = new TruncInst(getOperand(0), getType());
3074 New->SubclassOptionalData = SubclassOptionalData;
3075 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003076}
3077
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003078ZExtInst *ZExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003079 ZExtInst *New = new ZExtInst(getOperand(0), getType());
3080 New->SubclassOptionalData = SubclassOptionalData;
3081 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003082}
3083
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003084SExtInst *SExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003085 SExtInst *New = new SExtInst(getOperand(0), getType());
3086 New->SubclassOptionalData = SubclassOptionalData;
3087 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003088}
3089
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003090FPTruncInst *FPTruncInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003091 FPTruncInst *New = new FPTruncInst(getOperand(0), getType());
3092 New->SubclassOptionalData = SubclassOptionalData;
3093 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003094}
3095
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003096FPExtInst *FPExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003097 FPExtInst *New = new FPExtInst(getOperand(0), getType());
3098 New->SubclassOptionalData = SubclassOptionalData;
3099 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003100}
3101
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003102UIToFPInst *UIToFPInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003103 UIToFPInst *New = new UIToFPInst(getOperand(0), getType());
3104 New->SubclassOptionalData = SubclassOptionalData;
3105 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003106}
3107
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003108SIToFPInst *SIToFPInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003109 SIToFPInst *New = new SIToFPInst(getOperand(0), getType());
3110 New->SubclassOptionalData = SubclassOptionalData;
3111 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003112}
3113
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003114FPToUIInst *FPToUIInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003115 FPToUIInst *New = new FPToUIInst(getOperand(0), getType());
3116 New->SubclassOptionalData = SubclassOptionalData;
3117 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003118}
3119
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003120FPToSIInst *FPToSIInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003121 FPToSIInst *New = new FPToSIInst(getOperand(0), getType());
3122 New->SubclassOptionalData = SubclassOptionalData;
3123 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003124}
3125
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003126PtrToIntInst *PtrToIntInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003127 PtrToIntInst *New = new PtrToIntInst(getOperand(0), getType());
3128 New->SubclassOptionalData = SubclassOptionalData;
3129 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003130}
3131
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003132IntToPtrInst *IntToPtrInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003133 IntToPtrInst *New = new IntToPtrInst(getOperand(0), getType());
3134 New->SubclassOptionalData = SubclassOptionalData;
3135 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003136}
3137
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003138BitCastInst *BitCastInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003139 BitCastInst *New = new BitCastInst(getOperand(0), getType());
3140 New->SubclassOptionalData = SubclassOptionalData;
3141 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003142}
3143
3144CallInst *CallInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003145 CallInst *New = new(getNumOperands()) CallInst(*this);
3146 New->SubclassOptionalData = SubclassOptionalData;
3147 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003148}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003149
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003150SelectInst *SelectInst::clone(LLVMContext&) const {
3151 SelectInst *New = SelectInst::Create(getOperand(0),
3152 getOperand(1),
3153 getOperand(2));
3154 New->SubclassOptionalData = SubclassOptionalData;
3155 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003156}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003157
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003158VAArgInst *VAArgInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003159 VAArgInst *New = new VAArgInst(getOperand(0), getType());
3160 New->SubclassOptionalData = SubclassOptionalData;
3161 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003162}
3163
3164ExtractElementInst *ExtractElementInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003165 ExtractElementInst *New = ExtractElementInst::Create(getOperand(0),
3166 getOperand(1));
3167 New->SubclassOptionalData = SubclassOptionalData;
3168 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003169}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003170
3171InsertElementInst *InsertElementInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003172 InsertElementInst *New = InsertElementInst::Create(getOperand(0),
3173 getOperand(1),
3174 getOperand(2));
3175 New->SubclassOptionalData = SubclassOptionalData;
3176 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003177}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003178
3179ShuffleVectorInst *ShuffleVectorInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003180 ShuffleVectorInst *New = new ShuffleVectorInst(getOperand(0),
3181 getOperand(1),
3182 getOperand(2));
3183 New->SubclassOptionalData = SubclassOptionalData;
3184 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003185}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003186
3187PHINode *PHINode::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003188 PHINode *New = new PHINode(*this);
3189 New->SubclassOptionalData = SubclassOptionalData;
3190 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003191}
3192
3193ReturnInst *ReturnInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003194 ReturnInst *New = new(getNumOperands()) ReturnInst(*this);
3195 New->SubclassOptionalData = SubclassOptionalData;
3196 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003197}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003198
3199BranchInst *BranchInst::clone(LLVMContext&) const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003200 unsigned Ops(getNumOperands());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003201 BranchInst *New = new(Ops, Ops == 1) BranchInst(*this);
3202 New->SubclassOptionalData = SubclassOptionalData;
3203 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003204}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003205
3206SwitchInst *SwitchInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003207 SwitchInst *New = new SwitchInst(*this);
3208 New->SubclassOptionalData = SubclassOptionalData;
3209 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003210}
3211
3212InvokeInst *InvokeInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003213 InvokeInst *New = new(getNumOperands()) InvokeInst(*this);
3214 New->SubclassOptionalData = SubclassOptionalData;
3215 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003216}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003217
Owen Anderson55f1c092009-08-13 21:58:54 +00003218UnwindInst *UnwindInst::clone(LLVMContext &C) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003219 UnwindInst *New = new UnwindInst(C);
3220 New->SubclassOptionalData = SubclassOptionalData;
3221 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003222}
3223
Owen Anderson55f1c092009-08-13 21:58:54 +00003224UnreachableInst *UnreachableInst::clone(LLVMContext &C) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003225 UnreachableInst *New = new UnreachableInst(C);
3226 New->SubclassOptionalData = SubclassOptionalData;
3227 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003228}