blob: 8250ec009dbdb6b54743cae20f51c64a91508ff1 [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 {
Chris Lattner7d08da62009-09-21 22:27:34 +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);
Chris Lattner7d08da62009-09-21 22:27:34 +0000241 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000242 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000243
Nate Begemanb3923212005-08-04 23:24:19 +0000244 // Otherwise if all of the incoming values are the same for the PHI, replace
245 // the PHI node with the incoming value.
246 //
247 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000248 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000249 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000250 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000251 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000252 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000253 if (InVal && getIncomingValue(i) != InVal)
254 return 0; // Not the same, bail out.
Chris Lattner7d08da62009-09-21 22:27:34 +0000255 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000256 }
Nate Begemanb3923212005-08-04 23:24:19 +0000257
258 // The only case that could cause InVal to be null is if we have a PHI node
259 // that only has entries for itself. In this case, there is no entry into the
260 // loop, so kill the PHI.
261 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000262 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000263
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000264 // If we have a PHI node like phi(X, undef, X), where X is defined by some
265 // instruction, we cannot always return X as the result of the PHI node. Only
266 // do this if X is not an instruction (thus it must dominate the PHI block),
267 // or if the client is prepared to deal with this possibility.
Chris Lattner7d08da62009-09-21 22:27:34 +0000268 if (!HasUndefInput || !isa<Instruction>(InVal))
269 return InVal;
270
271 Instruction *IV = 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 is in the entry block, it obviously 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 }
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000282
Nate Begemanb3923212005-08-04 23:24:19 +0000283 // All of the incoming values are the same, return the value now.
284 return InVal;
285}
286
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000287
288//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289// CallInst Implementation
290//===----------------------------------------------------------------------===//
291
Gordon Henriksen14a55692007-12-10 02:14:30 +0000292CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000293}
294
Chris Lattner054ba2c2007-02-13 00:58:44 +0000295void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000296 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
297 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000298 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299
Misha Brukmanb1c93172005-04-21 23:48:37 +0000300 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000301 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000302 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303
Chris Lattner054ba2c2007-02-13 00:58:44 +0000304 assert((NumParams == FTy->getNumParams() ||
305 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000306 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000307 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000308 assert((i >= FTy->getNumParams() ||
309 FTy->getParamType(i) == Params[i]->getType()) &&
310 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000311 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000312 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313}
314
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000315void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000316 assert(NumOperands == 3 && "NumOperands not set up?");
317 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000318 OL[0] = Func;
319 OL[1] = Actual1;
320 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000321
322 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000324 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000325
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000326 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000327 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000328 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000329 assert((0 >= FTy->getNumParams() ||
330 FTy->getParamType(0) == Actual1->getType()) &&
331 "Calling a function with a bad signature!");
332 assert((1 >= FTy->getNumParams() ||
333 FTy->getParamType(1) == Actual2->getType()) &&
334 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335}
336
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000337void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000338 assert(NumOperands == 2 && "NumOperands not set up?");
339 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000340 OL[0] = Func;
341 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000342
343 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000344 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000345 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000346
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000347 assert((FTy->getNumParams() == 1 ||
348 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000349 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000350 assert((0 == FTy->getNumParams() ||
351 FTy->getParamType(0) == Actual->getType()) &&
352 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000353}
354
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000355void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000356 assert(NumOperands == 1 && "NumOperands not set up?");
357 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000358 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000359
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000360 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000361 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000362 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000363
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000364 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000365}
366
Daniel Dunbar4975db62009-07-25 04:41:11 +0000367CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000368 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000369 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
370 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000371 Instruction::Call,
372 OperandTraits<CallInst>::op_end(this) - 2,
373 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000374 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000375 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000376}
377
Daniel Dunbar4975db62009-07-25 04:41:11 +0000378CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000379 BasicBlock *InsertAtEnd)
380 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
381 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000382 Instruction::Call,
383 OperandTraits<CallInst>::op_end(this) - 2,
384 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000385 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000386 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000387}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000388CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000389 Instruction *InsertBefore)
390 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
391 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000392 Instruction::Call,
393 OperandTraits<CallInst>::op_end(this) - 1,
394 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000395 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000396 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000397}
398
Daniel Dunbar4975db62009-07-25 04:41:11 +0000399CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000400 BasicBlock *InsertAtEnd)
401 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
402 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000403 Instruction::Call,
404 OperandTraits<CallInst>::op_end(this) - 1,
405 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000406 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000407 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000408}
409
Misha Brukmanb1c93172005-04-21 23:48:37 +0000410CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000411 : Instruction(CI.getType(), Instruction::Call,
412 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000413 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000414 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000415 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000416 Use *OL = OperandList;
417 Use *InOL = CI.OperandList;
418 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000419 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000420 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000421}
422
Devang Patel4c758ea2008-09-25 21:00:45 +0000423void CallInst::addAttribute(unsigned i, Attributes attr) {
424 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000425 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000426 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000427}
428
Devang Patel4c758ea2008-09-25 21:00:45 +0000429void CallInst::removeAttribute(unsigned i, Attributes attr) {
430 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000431 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000432 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000433}
434
Devang Patelba3fa6c2008-09-23 23:03:40 +0000435bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000436 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000437 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000438 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000439 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000440 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000441}
442
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000443/// IsConstantOne - Return true only if val is constant int 1
444static bool IsConstantOne(Value *val) {
445 assert(val && "IsConstantOne does not work with NULL val");
446 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
447}
448
449static Value *checkArraySize(Value *Amt, const Type *IntPtrTy) {
450 if (!Amt)
451 Amt = ConstantInt::get(IntPtrTy, 1);
452 else {
453 assert(!isa<BasicBlock>(Amt) &&
454 "Passed basic block into malloc size parameter! Use other ctor");
455 assert(Amt->getType() == IntPtrTy &&
456 "Malloc array size is not an intptr!");
457 }
458 return Amt;
459}
460
461static Value *createMalloc(Instruction *InsertBefore, BasicBlock *InsertAtEnd,
Victor Hernandez788eaab2009-09-18 19:20:02 +0000462 const Type *IntPtrTy, const Type *AllocTy,
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000463 Value *ArraySize, const Twine &NameStr) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000464 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000465 "createMalloc needs either InsertBefore or InsertAtEnd");
466
467 // malloc(type) becomes:
468 // bitcast (i8* malloc(typeSize)) to type*
469 // malloc(type, arraySize) becomes:
470 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
471 Value *AllocSize = ConstantExpr::getSizeOf(AllocTy);
472 AllocSize = ConstantExpr::getTruncOrBitCast(cast<Constant>(AllocSize),
473 IntPtrTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000474 ArraySize = checkArraySize(ArraySize, IntPtrTy);
475
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000476 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000477 if (IsConstantOne(AllocSize)) {
478 AllocSize = ArraySize; // Operand * 1 = Operand
479 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
480 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
481 false /*ZExt*/);
482 // Malloc arg is constant product of type size and array size
483 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
484 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000485 // Multiply type size by the array size...
486 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000487 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
488 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000489 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000490 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
491 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000492 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000493 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000494
Victor Hernandez788eaab2009-09-18 19:20:02 +0000495 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000496 // Create the call to Malloc.
497 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
498 Module* M = BB->getParent()->getParent();
499 const Type *BPTy = PointerType::getUnqual(Type::getInt8Ty(BB->getContext()));
500 // prototype malloc as "void *malloc(size_t)"
Victor Hernandez788eaab2009-09-18 19:20:02 +0000501 Constant *MallocF = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
502 if (!cast<Function>(MallocF)->doesNotAlias(0))
503 cast<Function>(MallocF)->setDoesNotAlias(0);
504 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000505 CallInst *MCall = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000506 Value *MCast = NULL;
507 if (InsertBefore) {
508 MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertBefore);
509 // Create a cast instruction to convert to the right type...
510 MCast = new BitCastInst(MCall, AllocPtrType, NameStr, InsertBefore);
511 } else {
512 MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertAtEnd);
513 // Create a cast instruction to convert to the right type...
514 MCast = new BitCastInst(MCall, AllocPtrType, NameStr);
515 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000516 MCall->setTailCall();
Daniel Dunbarb9189002009-09-11 22:07:31 +0000517 assert(MCall->getType() != Type::getVoidTy(BB->getContext()) &&
518 "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000519
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000520 return MCast;
521}
522
523/// CreateMalloc - Generate the IR for a call to malloc:
524/// 1. Compute the malloc call's argument as the specified type's size,
525/// possibly multiplied by the array size if the array size is not
526/// constant 1.
527/// 2. Call malloc with that argument.
528/// 3. Bitcast the result of the malloc call to the specified type.
Victor Hernandez788eaab2009-09-18 19:20:02 +0000529Value *CallInst::CreateMalloc(Instruction *InsertBefore, const Type *IntPtrTy,
530 const Type *AllocTy, Value *ArraySize,
531 const Twine &Name) {
532 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, ArraySize, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000533}
534
535/// CreateMalloc - Generate the IR for a call to malloc:
536/// 1. Compute the malloc call's argument as the specified type's size,
537/// possibly multiplied by the array size if the array size is not
538/// constant 1.
539/// 2. Call malloc with that argument.
540/// 3. Bitcast the result of the malloc call to the specified type.
541/// Note: This function does not add the bitcast to the basic block, that is the
542/// responsibility of the caller.
Victor Hernandez788eaab2009-09-18 19:20:02 +0000543Value *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, const Type *IntPtrTy,
544 const Type *AllocTy, Value *ArraySize,
545 const Twine &Name) {
546 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, ArraySize, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000547}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000548
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000549//===----------------------------------------------------------------------===//
550// InvokeInst Implementation
551//===----------------------------------------------------------------------===//
552
553void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000554 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000555 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000556 Use *OL = OperandList;
557 OL[0] = Fn;
558 OL[1] = IfNormal;
559 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000560 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000561 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000562 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000563
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000564 assert(((NumArgs == FTy->getNumParams()) ||
565 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000566 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000567
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000568 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000569 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000570 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000571 "Invoking a function with a bad signature!");
572
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000573 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000574 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000575}
576
Misha Brukmanb1c93172005-04-21 23:48:37 +0000577InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000578 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000579 OperandTraits<InvokeInst>::op_end(this)
580 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000581 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000582 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000583 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000584 Use *OL = OperandList, *InOL = II.OperandList;
585 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000586 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000587 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000588}
589
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000590BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
591 return getSuccessor(idx);
592}
593unsigned InvokeInst::getNumSuccessorsV() const {
594 return getNumSuccessors();
595}
596void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
597 return setSuccessor(idx, B);
598}
599
Devang Patelba3fa6c2008-09-23 23:03:40 +0000600bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000601 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000602 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000603 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000604 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000605 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000606}
607
Devang Patel4c758ea2008-09-25 21:00:45 +0000608void InvokeInst::addAttribute(unsigned i, Attributes attr) {
609 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000610 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000611 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000612}
613
Devang Patel4c758ea2008-09-25 21:00:45 +0000614void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
615 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000616 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000617 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000618}
619
Duncan Sands5208d1a2007-11-28 17:07:01 +0000620
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000621//===----------------------------------------------------------------------===//
622// ReturnInst Implementation
623//===----------------------------------------------------------------------===//
624
Chris Lattner2195fc42007-02-24 00:55:48 +0000625ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000626 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000627 OperandTraits<ReturnInst>::op_end(this) -
628 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000629 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000630 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000631 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000632 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000633}
634
Owen Anderson55f1c092009-08-13 21:58:54 +0000635ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
636 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000637 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
638 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000639 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000640 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000641}
Owen Anderson55f1c092009-08-13 21:58:54 +0000642ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
643 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000644 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
645 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000646 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000647 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000648}
Owen Anderson55f1c092009-08-13 21:58:54 +0000649ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
650 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000651 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000652}
653
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000654unsigned ReturnInst::getNumSuccessorsV() const {
655 return getNumSuccessors();
656}
657
Devang Patelae682fb2008-02-26 17:56:20 +0000658/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
659/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000660void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000661 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000662}
663
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000664BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000665 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000666 return 0;
667}
668
Devang Patel59643e52008-02-23 00:35:18 +0000669ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000670}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000671
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000672//===----------------------------------------------------------------------===//
673// UnwindInst Implementation
674//===----------------------------------------------------------------------===//
675
Owen Anderson55f1c092009-08-13 21:58:54 +0000676UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
677 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
678 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000679}
Owen Anderson55f1c092009-08-13 21:58:54 +0000680UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
681 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
682 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000683}
684
685
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000686unsigned UnwindInst::getNumSuccessorsV() const {
687 return getNumSuccessors();
688}
689
690void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000691 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000692}
693
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000694BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000695 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000696 return 0;
697}
698
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000699//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000700// UnreachableInst Implementation
701//===----------------------------------------------------------------------===//
702
Owen Anderson55f1c092009-08-13 21:58:54 +0000703UnreachableInst::UnreachableInst(LLVMContext &Context,
704 Instruction *InsertBefore)
705 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
706 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000707}
Owen Anderson55f1c092009-08-13 21:58:54 +0000708UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
709 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
710 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000711}
712
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000713unsigned UnreachableInst::getNumSuccessorsV() const {
714 return getNumSuccessors();
715}
716
717void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000718 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000719}
720
721BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000722 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000723 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000724}
725
726//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000727// BranchInst Implementation
728//===----------------------------------------------------------------------===//
729
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000730void BranchInst::AssertOK() {
731 if (isConditional())
Owen Anderson55f1c092009-08-13 21:58:54 +0000732 assert(getCondition()->getType() == Type::getInt1Ty(getContext()) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000733 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000734}
735
Chris Lattner2195fc42007-02-24 00:55:48 +0000736BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000737 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000738 OperandTraits<BranchInst>::op_end(this) - 1,
739 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000740 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000741 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000742}
743BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
744 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000745 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000746 OperandTraits<BranchInst>::op_end(this) - 3,
747 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000748 Op<-1>() = IfTrue;
749 Op<-2>() = IfFalse;
750 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000751#ifndef NDEBUG
752 AssertOK();
753#endif
754}
755
756BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000757 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000758 OperandTraits<BranchInst>::op_end(this) - 1,
759 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000760 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000761 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000762}
763
764BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
765 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000766 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000767 OperandTraits<BranchInst>::op_end(this) - 3,
768 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000769 Op<-1>() = IfTrue;
770 Op<-2>() = IfFalse;
771 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000772#ifndef NDEBUG
773 AssertOK();
774#endif
775}
776
777
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000778BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000779 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000780 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
781 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000782 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000783 if (BI.getNumOperands() != 1) {
784 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000785 Op<-3>() = BI.Op<-3>();
786 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000787 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000788 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000789}
790
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000791
792Use* Use::getPrefix() {
793 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
794 if (PotentialPrefix.getOpaqueValue())
795 return 0;
796
797 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
798}
799
800BranchInst::~BranchInst() {
801 if (NumOperands == 1) {
802 if (Use *Prefix = OperandList->getPrefix()) {
803 Op<-1>() = 0;
804 //
805 // mark OperandList to have a special value for scrutiny
806 // by baseclass destructors and operator delete
807 OperandList = Prefix;
808 } else {
809 NumOperands = 3;
810 OperandList = op_begin();
811 }
812 }
813}
814
815
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000816BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
817 return getSuccessor(idx);
818}
819unsigned BranchInst::getNumSuccessorsV() const {
820 return getNumSuccessors();
821}
822void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
823 setSuccessor(idx, B);
824}
825
826
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000827//===----------------------------------------------------------------------===//
828// AllocationInst Implementation
829//===----------------------------------------------------------------------===//
830
Owen Andersonb6b25302009-07-14 23:09:55 +0000831static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000832 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000833 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000834 else {
835 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000836 "Passed basic block into allocation size parameter! Use other ctor");
Owen Anderson55f1c092009-08-13 21:58:54 +0000837 assert(Amt->getType() == Type::getInt32Ty(Context) &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000838 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000839 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000840 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000841}
842
Owen Anderson4fdeba92009-07-15 23:53:25 +0000843AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000844 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000845 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +0000846 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000847 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000848 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000849 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000850 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000851}
852
Owen Anderson4fdeba92009-07-15 23:53:25 +0000853AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Daniel Dunbar4975db62009-07-25 04:41:11 +0000854 unsigned Align, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000855 BasicBlock *InsertAtEnd)
Owen Anderson4056ca92009-07-29 22:17:13 +0000856 : UnaryInstruction(PointerType::getUnqual(Ty), iTy,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000857 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000858 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000859 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000860 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000861}
862
Gordon Henriksen14a55692007-12-10 02:14:30 +0000863// Out of line virtual method, so the vtable, etc has a home.
864AllocationInst::~AllocationInst() {
865}
866
Dan Gohmanaa583d72008-03-24 16:55:58 +0000867void AllocationInst::setAlignment(unsigned Align) {
868 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
869 SubclassData = Log2_32(Align) + 1;
870 assert(getAlignment() == Align && "Alignment representation error!");
871}
872
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000873bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000874 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
875 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000876 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000877}
878
879const Type *AllocationInst::getAllocatedType() const {
880 return getType()->getElementType();
881}
882
Chris Lattner8b291e62008-11-26 02:54:17 +0000883/// isStaticAlloca - Return true if this alloca is in the entry block of the
884/// function and is a constant size. If so, the code generator will fold it
885/// into the prolog/epilog code, so it is basically free.
886bool AllocaInst::isStaticAlloca() const {
887 // Must be constant size.
888 if (!isa<ConstantInt>(getArraySize())) return false;
889
890 // Must be in the entry block.
891 const BasicBlock *Parent = getParent();
892 return Parent == &Parent->getParent()->front();
893}
894
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000895//===----------------------------------------------------------------------===//
896// FreeInst Implementation
897//===----------------------------------------------------------------------===//
898
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000899void FreeInst::AssertOK() {
900 assert(isa<PointerType>(getOperand(0)->getType()) &&
901 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000902}
903
904FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000905 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
906 Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000907 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000908}
909
910FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000911 : UnaryInstruction(Type::getVoidTy(Ptr->getContext()),
912 Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000913 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000914}
915
916
917//===----------------------------------------------------------------------===//
918// LoadInst Implementation
919//===----------------------------------------------------------------------===//
920
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000921void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000922 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000923 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000924}
925
Daniel Dunbar4975db62009-07-25 04:41:11 +0000926LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000927 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000928 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000929 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000930 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000931 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000932 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000933}
934
Daniel Dunbar4975db62009-07-25 04:41:11 +0000935LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000936 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000937 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000938 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000939 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000940 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000941 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000942}
943
Daniel Dunbar4975db62009-07-25 04:41:11 +0000944LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000945 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000946 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000947 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000948 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000949 setAlignment(0);
950 AssertOK();
951 setName(Name);
952}
953
Daniel Dunbar4975db62009-07-25 04:41:11 +0000954LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000955 unsigned Align, Instruction *InsertBef)
956 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
957 Load, Ptr, InsertBef) {
958 setVolatile(isVolatile);
959 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000960 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000961 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000962}
963
Daniel Dunbar4975db62009-07-25 04:41:11 +0000964LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000965 unsigned Align, BasicBlock *InsertAE)
966 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
967 Load, Ptr, InsertAE) {
968 setVolatile(isVolatile);
969 setAlignment(Align);
970 AssertOK();
971 setName(Name);
972}
973
Daniel Dunbar4975db62009-07-25 04:41:11 +0000974LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000975 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000976 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000977 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000978 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000979 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000980 AssertOK();
981 setName(Name);
982}
983
Daniel Dunbar27096822009-08-11 18:11:15 +0000984
985
986LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
987 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
988 Load, Ptr, InsertBef) {
989 setVolatile(false);
990 setAlignment(0);
991 AssertOK();
992 if (Name && Name[0]) setName(Name);
993}
994
995LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
996 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
997 Load, Ptr, InsertAE) {
998 setVolatile(false);
999 setAlignment(0);
1000 AssertOK();
1001 if (Name && Name[0]) setName(Name);
1002}
1003
1004LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1005 Instruction *InsertBef)
1006: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1007 Load, Ptr, InsertBef) {
1008 setVolatile(isVolatile);
1009 setAlignment(0);
1010 AssertOK();
1011 if (Name && Name[0]) setName(Name);
1012}
1013
1014LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1015 BasicBlock *InsertAE)
1016 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1017 Load, Ptr, InsertAE) {
1018 setVolatile(isVolatile);
1019 setAlignment(0);
1020 AssertOK();
1021 if (Name && Name[0]) setName(Name);
1022}
1023
Christopher Lamb84485702007-04-22 19:24:39 +00001024void LoadInst::setAlignment(unsigned Align) {
1025 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1026 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1027}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001028
1029//===----------------------------------------------------------------------===//
1030// StoreInst Implementation
1031//===----------------------------------------------------------------------===//
1032
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001033void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001034 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001035 assert(isa<PointerType>(getOperand(1)->getType()) &&
1036 "Ptr must have pointer type!");
1037 assert(getOperand(0)->getType() ==
1038 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001039 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001040}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001041
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001042
1043StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001044 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001045 OperandTraits<StoreInst>::op_begin(this),
1046 OperandTraits<StoreInst>::operands(this),
1047 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001048 Op<0>() = val;
1049 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001050 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001051 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001052 AssertOK();
1053}
1054
1055StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001056 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001057 OperandTraits<StoreInst>::op_begin(this),
1058 OperandTraits<StoreInst>::operands(this),
1059 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001060 Op<0>() = val;
1061 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001062 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001063 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001064 AssertOK();
1065}
1066
Misha Brukmanb1c93172005-04-21 23:48:37 +00001067StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001068 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001069 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001070 OperandTraits<StoreInst>::op_begin(this),
1071 OperandTraits<StoreInst>::operands(this),
1072 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001073 Op<0>() = val;
1074 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001075 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001076 setAlignment(0);
1077 AssertOK();
1078}
1079
1080StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1081 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001082 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001083 OperandTraits<StoreInst>::op_begin(this),
1084 OperandTraits<StoreInst>::operands(this),
1085 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001086 Op<0>() = val;
1087 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001088 setVolatile(isVolatile);
1089 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001090 AssertOK();
1091}
1092
Misha Brukmanb1c93172005-04-21 23:48:37 +00001093StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001094 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001095 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001096 OperandTraits<StoreInst>::op_begin(this),
1097 OperandTraits<StoreInst>::operands(this),
1098 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001099 Op<0>() = val;
1100 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001101 setVolatile(isVolatile);
1102 setAlignment(Align);
1103 AssertOK();
1104}
1105
1106StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001107 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001108 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001109 OperandTraits<StoreInst>::op_begin(this),
1110 OperandTraits<StoreInst>::operands(this),
1111 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001112 Op<0>() = val;
1113 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001114 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001115 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001116 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001117}
1118
Christopher Lamb84485702007-04-22 19:24:39 +00001119void StoreInst::setAlignment(unsigned Align) {
1120 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1121 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1122}
1123
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001124//===----------------------------------------------------------------------===//
1125// GetElementPtrInst Implementation
1126//===----------------------------------------------------------------------===//
1127
Christopher Lambedf07882007-12-17 01:12:55 +00001128static unsigned retrieveAddrSpace(const Value *Val) {
1129 return cast<PointerType>(Val->getType())->getAddressSpace();
1130}
1131
Gabor Greif21ba1842008-06-06 20:28:12 +00001132void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001133 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001134 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1135 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001136 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001137
Chris Lattner79807c3d2007-01-31 19:47:18 +00001138 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001139 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001140
1141 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001142}
1143
Daniel Dunbar4975db62009-07-25 04:41:11 +00001144void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001145 assert(NumOperands == 2 && "NumOperands not initialized?");
1146 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001147 OL[0] = Ptr;
1148 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001149
1150 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001151}
1152
Gabor Greiff6caff662008-05-10 08:32:32 +00001153GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001154 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001155 OperandTraits<GetElementPtrInst>::op_end(this)
1156 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001157 GEPI.getNumOperands()) {
1158 Use *OL = OperandList;
1159 Use *GEPIOL = GEPI.OperandList;
1160 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001161 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001162 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001163}
1164
Chris Lattner82981202005-05-03 05:43:30 +00001165GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001166 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001167 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001168 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001169 GetElementPtr,
1170 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1171 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001172 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001173}
1174
1175GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001176 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001177 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001178 checkType(getIndexedType(Ptr->getType(),Idx)),
1179 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001180 GetElementPtr,
1181 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1182 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001183 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001184}
1185
Chris Lattner6090a422009-03-09 04:46:40 +00001186/// getIndexedType - Returns the type of the element that would be accessed with
1187/// a gep instruction with the specified parameters.
1188///
1189/// The Idxs pointer should point to a continuous piece of memory containing the
1190/// indices, either as Value* or uint64_t.
1191///
1192/// A null type is returned if the indices are invalid for the specified
1193/// pointer type.
1194///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001195template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001196static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1197 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001198 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1199 if (!PTy) return 0; // Type isn't a pointer type!
1200 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001201
Chris Lattner6090a422009-03-09 04:46:40 +00001202 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001203 if (NumIdx == 0)
1204 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001205
1206 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001207 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1208 // that contain opaque types) under the assumption that it will be resolved to
1209 // a sane type later.
1210 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001211 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001212
Dan Gohman1ecaf452008-05-31 00:58:22 +00001213 unsigned CurIdx = 1;
1214 for (; CurIdx != NumIdx; ++CurIdx) {
1215 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1216 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001217 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001218 if (!CT->indexValid(Index)) return 0;
1219 Agg = CT->getTypeAtIndex(Index);
1220
1221 // If the new type forwards to another type, then it is in the middle
1222 // of being refined to another type (and hence, may have dropped all
1223 // references to what it was using before). So, use the new forwarded
1224 // type.
1225 if (const Type *Ty = Agg->getForwardedType())
1226 Agg = Ty;
1227 }
1228 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001229}
1230
Matthijs Kooijman04468622008-07-29 08:46:11 +00001231const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1232 Value* const *Idxs,
1233 unsigned NumIdx) {
1234 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1235}
1236
1237const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1238 uint64_t const *Idxs,
1239 unsigned NumIdx) {
1240 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1241}
1242
Chris Lattner82981202005-05-03 05:43:30 +00001243const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1244 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1245 if (!PTy) return 0; // Type isn't a pointer type!
1246
1247 // Check the pointer index.
1248 if (!PTy->indexValid(Idx)) return 0;
1249
Chris Lattnerc2233332005-05-03 16:44:45 +00001250 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001251}
1252
Chris Lattner45f15572007-04-14 00:12:57 +00001253
1254/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1255/// zeros. If so, the result pointer and the first operand have the same
1256/// value, just potentially different types.
1257bool GetElementPtrInst::hasAllZeroIndices() const {
1258 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1259 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1260 if (!CI->isZero()) return false;
1261 } else {
1262 return false;
1263 }
1264 }
1265 return true;
1266}
1267
Chris Lattner27058292007-04-27 20:35:56 +00001268/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1269/// constant integers. If so, the result pointer and the first operand have
1270/// a constant offset between them.
1271bool GetElementPtrInst::hasAllConstantIndices() const {
1272 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1273 if (!isa<ConstantInt>(getOperand(i)))
1274 return false;
1275 }
1276 return true;
1277}
1278
Dan Gohman1b849082009-09-07 23:54:19 +00001279void GetElementPtrInst::setIsInBounds(bool B) {
1280 cast<GEPOperator>(this)->setIsInBounds(B);
1281}
Chris Lattner45f15572007-04-14 00:12:57 +00001282
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001283//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001284// ExtractElementInst Implementation
1285//===----------------------------------------------------------------------===//
1286
1287ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001288 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001289 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001290 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001291 ExtractElement,
1292 OperandTraits<ExtractElementInst>::op_begin(this),
1293 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001294 assert(isValidOperands(Val, Index) &&
1295 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001296 Op<0>() = Val;
1297 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001298 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001299}
1300
1301ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001302 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001303 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001304 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001305 ExtractElement,
1306 OperandTraits<ExtractElementInst>::op_begin(this),
1307 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001308 assert(isValidOperands(Val, Index) &&
1309 "Invalid extractelement instruction operands!");
1310
Gabor Greif2d3024d2008-05-26 21:33:52 +00001311 Op<0>() = Val;
1312 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001313 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001314}
1315
Chris Lattner65511ff2006-10-05 06:24:58 +00001316
Chris Lattner54865b32006-04-08 04:05:48 +00001317bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Owen Anderson55f1c092009-08-13 21:58:54 +00001318 if (!isa<VectorType>(Val->getType()) ||
1319 Index->getType() != Type::getInt32Ty(Val->getContext()))
Chris Lattner54865b32006-04-08 04:05:48 +00001320 return false;
1321 return true;
1322}
1323
1324
Robert Bocchino23004482006-01-10 19:05:34 +00001325//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001326// InsertElementInst Implementation
1327//===----------------------------------------------------------------------===//
1328
Chris Lattner54865b32006-04-08 04:05:48 +00001329InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001330 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001331 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001332 : Instruction(Vec->getType(), InsertElement,
1333 OperandTraits<InsertElementInst>::op_begin(this),
1334 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001335 assert(isValidOperands(Vec, Elt, Index) &&
1336 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001337 Op<0>() = Vec;
1338 Op<1>() = Elt;
1339 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001340 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001341}
1342
Chris Lattner54865b32006-04-08 04:05:48 +00001343InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001344 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001345 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001346 : Instruction(Vec->getType(), InsertElement,
1347 OperandTraits<InsertElementInst>::op_begin(this),
1348 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001349 assert(isValidOperands(Vec, Elt, Index) &&
1350 "Invalid insertelement instruction operands!");
1351
Gabor Greif2d3024d2008-05-26 21:33:52 +00001352 Op<0>() = Vec;
1353 Op<1>() = Elt;
1354 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001355 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001356}
1357
Chris Lattner54865b32006-04-08 04:05:48 +00001358bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1359 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001360 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001361 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001362
Reid Spencerd84d35b2007-02-15 02:26:10 +00001363 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001364 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001365
Owen Anderson55f1c092009-08-13 21:58:54 +00001366 if (Index->getType() != Type::getInt32Ty(Vec->getContext()))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001367 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001368 return true;
1369}
1370
1371
Robert Bocchinoca27f032006-01-17 20:07:22 +00001372//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001373// ShuffleVectorInst Implementation
1374//===----------------------------------------------------------------------===//
1375
1376ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001377 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001378 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001379: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001380 cast<VectorType>(Mask->getType())->getNumElements()),
1381 ShuffleVector,
1382 OperandTraits<ShuffleVectorInst>::op_begin(this),
1383 OperandTraits<ShuffleVectorInst>::operands(this),
1384 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001385 assert(isValidOperands(V1, V2, Mask) &&
1386 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001387 Op<0>() = V1;
1388 Op<1>() = V2;
1389 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001390 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001391}
1392
1393ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001394 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001395 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001396: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1397 cast<VectorType>(Mask->getType())->getNumElements()),
1398 ShuffleVector,
1399 OperandTraits<ShuffleVectorInst>::op_begin(this),
1400 OperandTraits<ShuffleVectorInst>::operands(this),
1401 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001402 assert(isValidOperands(V1, V2, Mask) &&
1403 "Invalid shuffle vector instruction operands!");
1404
Gabor Greif2d3024d2008-05-26 21:33:52 +00001405 Op<0>() = V1;
1406 Op<1>() = V2;
1407 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001408 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001409}
1410
Mon P Wang25f01062008-11-10 04:46:22 +00001411bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001412 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001413 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001414 return false;
1415
1416 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1417 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Owen Anderson55f1c092009-08-13 21:58:54 +00001418 MaskTy->getElementType() != Type::getInt32Ty(V1->getContext()))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001419 return false;
1420 return true;
1421}
1422
Chris Lattnerf724e342008-03-02 05:28:33 +00001423/// getMaskValue - Return the index from the shuffle mask for the specified
1424/// output result. This is either -1 if the element is undef or a number less
1425/// than 2*numelements.
1426int ShuffleVectorInst::getMaskValue(unsigned i) const {
1427 const Constant *Mask = cast<Constant>(getOperand(2));
1428 if (isa<UndefValue>(Mask)) return -1;
1429 if (isa<ConstantAggregateZero>(Mask)) return 0;
1430 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1431 assert(i < MaskCV->getNumOperands() && "Index out of range");
1432
1433 if (isa<UndefValue>(MaskCV->getOperand(i)))
1434 return -1;
1435 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1436}
1437
Dan Gohman12fce772008-05-15 19:50:34 +00001438//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001439// InsertValueInst Class
1440//===----------------------------------------------------------------------===//
1441
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001442void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001443 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001444 assert(NumOperands == 2 && "NumOperands not initialized?");
1445 Op<0>() = Agg;
1446 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001447
Dan Gohman1ecaf452008-05-31 00:58:22 +00001448 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001449 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001450}
1451
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001452void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001453 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001454 assert(NumOperands == 2 && "NumOperands not initialized?");
1455 Op<0>() = Agg;
1456 Op<1>() = Val;
1457
1458 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001459 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001460}
1461
1462InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001463 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001464 OperandTraits<InsertValueInst>::op_begin(this), 2),
1465 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001466 Op<0>() = IVI.getOperand(0);
1467 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001468 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001469}
1470
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001471InsertValueInst::InsertValueInst(Value *Agg,
1472 Value *Val,
1473 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001474 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001475 Instruction *InsertBefore)
1476 : Instruction(Agg->getType(), InsertValue,
1477 OperandTraits<InsertValueInst>::op_begin(this),
1478 2, InsertBefore) {
1479 init(Agg, Val, Idx, Name);
1480}
1481
1482InsertValueInst::InsertValueInst(Value *Agg,
1483 Value *Val,
1484 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001485 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001486 BasicBlock *InsertAtEnd)
1487 : Instruction(Agg->getType(), InsertValue,
1488 OperandTraits<InsertValueInst>::op_begin(this),
1489 2, InsertAtEnd) {
1490 init(Agg, Val, Idx, Name);
1491}
1492
Dan Gohman0752bff2008-05-23 00:36:11 +00001493//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001494// ExtractValueInst Class
1495//===----------------------------------------------------------------------===//
1496
Gabor Greifcbcc4952008-06-06 21:06:32 +00001497void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001498 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001499 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001500
Dan Gohman1ecaf452008-05-31 00:58:22 +00001501 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001502 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001503}
1504
Daniel Dunbar4975db62009-07-25 04:41:11 +00001505void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001506 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001507
1508 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001509 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001510}
1511
1512ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001513 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001514 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001515 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001516}
1517
Dan Gohman12fce772008-05-15 19:50:34 +00001518// getIndexedType - Returns the type of the element that would be extracted
1519// with an extractvalue instruction with the specified parameters.
1520//
1521// A null type is returned if the indices are invalid for the specified
1522// pointer type.
1523//
1524const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001525 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001526 unsigned NumIdx) {
1527 unsigned CurIdx = 0;
1528 for (; CurIdx != NumIdx; ++CurIdx) {
1529 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001530 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1531 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001532 if (!CT->indexValid(Index)) return 0;
1533 Agg = CT->getTypeAtIndex(Index);
1534
1535 // If the new type forwards to another type, then it is in the middle
1536 // of being refined to another type (and hence, may have dropped all
1537 // references to what it was using before). So, use the new forwarded
1538 // type.
1539 if (const Type *Ty = Agg->getForwardedType())
1540 Agg = Ty;
1541 }
1542 return CurIdx == NumIdx ? Agg : 0;
1543}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001544
Dan Gohman4a3125b2008-06-17 21:07:55 +00001545const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001546 unsigned Idx) {
1547 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001548}
1549
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001550//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001551// BinaryOperator Class
1552//===----------------------------------------------------------------------===//
1553
Dan Gohmana5b96452009-06-04 22:49:04 +00001554/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1555/// type is floating-point, to help provide compatibility with an older API.
1556///
1557static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1558 const Type *Ty) {
1559 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1560 if (Ty->isFPOrFPVector()) {
1561 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1562 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1563 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1564 }
1565 return iType;
1566}
1567
Chris Lattner2195fc42007-02-24 00:55:48 +00001568BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001569 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001570 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001571 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001572 OperandTraits<BinaryOperator>::op_begin(this),
1573 OperandTraits<BinaryOperator>::operands(this),
1574 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001575 Op<0>() = S1;
1576 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001577 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001578 setName(Name);
1579}
1580
1581BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001582 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001583 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001584 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001585 OperandTraits<BinaryOperator>::op_begin(this),
1586 OperandTraits<BinaryOperator>::operands(this),
1587 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001588 Op<0>() = S1;
1589 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001590 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001591 setName(Name);
1592}
1593
1594
1595void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001596 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001597 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001598 assert(LHS->getType() == RHS->getType() &&
1599 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001600#ifndef NDEBUG
1601 switch (iType) {
1602 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001603 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001604 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001605 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001606 assert(getType()->isIntOrIntVector() &&
1607 "Tried to create an integer operation on a non-integer type!");
1608 break;
1609 case FAdd: case FSub:
1610 case FMul:
1611 assert(getType() == LHS->getType() &&
1612 "Arithmetic operation should return same type as operands!");
1613 assert(getType()->isFPOrFPVector() &&
1614 "Tried to create a floating-point operation on a "
1615 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001616 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001617 case UDiv:
1618 case SDiv:
1619 assert(getType() == LHS->getType() &&
1620 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001621 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1622 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001623 "Incorrect operand type (not integer) for S/UDIV");
1624 break;
1625 case FDiv:
1626 assert(getType() == LHS->getType() &&
1627 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001628 assert(getType()->isFPOrFPVector() &&
1629 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001630 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001631 case URem:
1632 case SRem:
1633 assert(getType() == LHS->getType() &&
1634 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001635 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1636 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001637 "Incorrect operand type (not integer) for S/UREM");
1638 break;
1639 case FRem:
1640 assert(getType() == LHS->getType() &&
1641 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001642 assert(getType()->isFPOrFPVector() &&
1643 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001644 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001645 case Shl:
1646 case LShr:
1647 case AShr:
1648 assert(getType() == LHS->getType() &&
1649 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001650 assert((getType()->isInteger() ||
1651 (isa<VectorType>(getType()) &&
1652 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1653 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001654 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001655 case And: case Or:
1656 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001657 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001658 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001659 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001660 (isa<VectorType>(getType()) &&
1661 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001662 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001663 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001664 default:
1665 break;
1666 }
1667#endif
1668}
1669
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001670BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001671 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001672 Instruction *InsertBefore) {
1673 assert(S1->getType() == S2->getType() &&
1674 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001675 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001676}
1677
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001678BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001679 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001680 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001681 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001682 InsertAtEnd->getInstList().push_back(Res);
1683 return Res;
1684}
1685
Dan Gohman5476cfd2009-08-12 16:23:25 +00001686BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001687 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001688 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001689 return new BinaryOperator(Instruction::Sub,
1690 zero, Op,
1691 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001692}
1693
Dan Gohman5476cfd2009-08-12 16:23:25 +00001694BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001695 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001696 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001697 return new BinaryOperator(Instruction::Sub,
1698 zero, Op,
1699 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001700}
1701
Dan Gohman5476cfd2009-08-12 16:23:25 +00001702BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001703 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001704 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001705 return new BinaryOperator(Instruction::FSub,
1706 zero, Op,
1707 Op->getType(), Name, InsertBefore);
1708}
1709
Dan Gohman5476cfd2009-08-12 16:23:25 +00001710BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001711 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001712 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001713 return new BinaryOperator(Instruction::FSub,
1714 zero, Op,
1715 Op->getType(), Name, InsertAtEnd);
1716}
1717
Dan Gohman5476cfd2009-08-12 16:23:25 +00001718BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001719 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001720 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001721 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001722 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001723 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001724 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001725 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001726 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001727 }
1728
1729 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001730 Op->getType(), Name, InsertBefore);
1731}
1732
Dan Gohman5476cfd2009-08-12 16:23:25 +00001733BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001734 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001735 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001736 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001737 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001738 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001739 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001740 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001741 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001742 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001743 }
1744
1745 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001746 Op->getType(), Name, InsertAtEnd);
1747}
1748
1749
1750// isConstantAllOnes - Helper function for several functions below
1751static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001752 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1753 return CI->isAllOnesValue();
1754 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1755 return CV->isAllOnesValue();
1756 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001757}
1758
Owen Andersonbb2501b2009-07-13 22:18:28 +00001759bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001760 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1761 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001762 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1763 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001764 return false;
1765}
1766
Owen Andersonbb2501b2009-07-13 22:18:28 +00001767bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001768 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1769 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001770 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001771 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001772 return false;
1773}
1774
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001775bool BinaryOperator::isNot(const Value *V) {
1776 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1777 return (Bop->getOpcode() == Instruction::Xor &&
1778 (isConstantAllOnes(Bop->getOperand(1)) ||
1779 isConstantAllOnes(Bop->getOperand(0))));
1780 return false;
1781}
1782
Chris Lattner2c7d1772005-04-24 07:28:37 +00001783Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001784 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001785}
1786
Chris Lattner2c7d1772005-04-24 07:28:37 +00001787const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1788 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001789}
1790
Dan Gohmana5b96452009-06-04 22:49:04 +00001791Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001792 return cast<BinaryOperator>(BinOp)->getOperand(1);
1793}
1794
1795const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1796 return getFNegArgument(const_cast<Value*>(BinOp));
1797}
1798
Chris Lattner2c7d1772005-04-24 07:28:37 +00001799Value *BinaryOperator::getNotArgument(Value *BinOp) {
1800 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1801 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1802 Value *Op0 = BO->getOperand(0);
1803 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001804 if (isConstantAllOnes(Op0)) return Op1;
1805
1806 assert(isConstantAllOnes(Op1));
1807 return Op0;
1808}
1809
Chris Lattner2c7d1772005-04-24 07:28:37 +00001810const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1811 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001812}
1813
1814
1815// swapOperands - Exchange the two operands to this instruction. This
1816// instruction is safe to use on any binary instruction and does not
1817// modify the semantics of the instruction. If the instruction is
1818// order dependent (SetLT f.e.) the opcode is changed.
1819//
1820bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001821 if (!isCommutative())
1822 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001823 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001824 return false;
1825}
1826
Dan Gohman1b849082009-09-07 23:54:19 +00001827void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1828 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1829}
1830
1831void BinaryOperator::setHasNoSignedWrap(bool b) {
1832 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1833}
1834
1835void BinaryOperator::setIsExact(bool b) {
1836 cast<SDivOperator>(this)->setIsExact(b);
1837}
1838
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001839//===----------------------------------------------------------------------===//
1840// CastInst Class
1841//===----------------------------------------------------------------------===//
1842
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001843// Just determine if this cast only deals with integral->integral conversion.
1844bool CastInst::isIntegerCast() const {
1845 switch (getOpcode()) {
1846 default: return false;
1847 case Instruction::ZExt:
1848 case Instruction::SExt:
1849 case Instruction::Trunc:
1850 return true;
1851 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001852 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001853 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001854}
1855
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001856bool CastInst::isLosslessCast() const {
1857 // Only BitCast can be lossless, exit fast if we're not BitCast
1858 if (getOpcode() != Instruction::BitCast)
1859 return false;
1860
1861 // Identity cast is always lossless
1862 const Type* SrcTy = getOperand(0)->getType();
1863 const Type* DstTy = getType();
1864 if (SrcTy == DstTy)
1865 return true;
1866
Reid Spencer8d9336d2006-12-31 05:26:44 +00001867 // Pointer to pointer is always lossless.
1868 if (isa<PointerType>(SrcTy))
1869 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001870 return false; // Other types have no identity values
1871}
1872
1873/// This function determines if the CastInst does not require any bits to be
1874/// changed in order to effect the cast. Essentially, it identifies cases where
1875/// no code gen is necessary for the cast, hence the name no-op cast. For
1876/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001877/// # bitcast i32* %x to i8*
1878/// # bitcast <2 x i32> %x to <4 x i16>
1879/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001880/// @brief Determine if a cast is a no-op.
1881bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1882 switch (getOpcode()) {
1883 default:
1884 assert(!"Invalid CastOp");
1885 case Instruction::Trunc:
1886 case Instruction::ZExt:
1887 case Instruction::SExt:
1888 case Instruction::FPTrunc:
1889 case Instruction::FPExt:
1890 case Instruction::UIToFP:
1891 case Instruction::SIToFP:
1892 case Instruction::FPToUI:
1893 case Instruction::FPToSI:
1894 return false; // These always modify bits
1895 case Instruction::BitCast:
1896 return true; // BitCast never modifies bits.
1897 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001898 return IntPtrTy->getScalarSizeInBits() ==
1899 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001900 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001901 return IntPtrTy->getScalarSizeInBits() ==
1902 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001903 }
1904}
1905
1906/// This function determines if a pair of casts can be eliminated and what
1907/// opcode should be used in the elimination. This assumes that there are two
1908/// instructions like this:
1909/// * %F = firstOpcode SrcTy %x to MidTy
1910/// * %S = secondOpcode MidTy %F to DstTy
1911/// The function returns a resultOpcode so these two casts can be replaced with:
1912/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1913/// If no such cast is permited, the function returns 0.
1914unsigned CastInst::isEliminableCastPair(
1915 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1916 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1917{
1918 // Define the 144 possibilities for these two cast instructions. The values
1919 // in this matrix determine what to do in a given situation and select the
1920 // case in the switch below. The rows correspond to firstOp, the columns
1921 // correspond to secondOp. In looking at the table below, keep in mind
1922 // the following cast properties:
1923 //
1924 // Size Compare Source Destination
1925 // Operator Src ? Size Type Sign Type Sign
1926 // -------- ------------ ------------------- ---------------------
1927 // TRUNC > Integer Any Integral Any
1928 // ZEXT < Integral Unsigned Integer Any
1929 // SEXT < Integral Signed Integer Any
1930 // FPTOUI n/a FloatPt n/a Integral Unsigned
1931 // FPTOSI n/a FloatPt n/a Integral Signed
1932 // UITOFP n/a Integral Unsigned FloatPt n/a
1933 // SITOFP n/a Integral Signed FloatPt n/a
1934 // FPTRUNC > FloatPt n/a FloatPt n/a
1935 // FPEXT < FloatPt n/a FloatPt n/a
1936 // PTRTOINT n/a Pointer n/a Integral Unsigned
1937 // INTTOPTR n/a Integral Unsigned Pointer n/a
1938 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001939 //
1940 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001941 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1942 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001943 // of the produced value (we no longer know the top-part is all zeros).
1944 // Further this conversion is often much more expensive for typical hardware,
1945 // and causes issues when building libgcc. We disallow fptosi+sext for the
1946 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001947 const unsigned numCastOps =
1948 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1949 static const uint8_t CastResults[numCastOps][numCastOps] = {
1950 // T F F U S F F P I B -+
1951 // R Z S P P I I T P 2 N T |
1952 // U E E 2 2 2 2 R E I T C +- secondOp
1953 // N X X U S F F N X N 2 V |
1954 // C T T I I P P C T T P T -+
1955 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1956 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1957 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001958 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1959 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001960 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1961 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1962 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1963 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1964 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1965 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1966 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1967 };
1968
1969 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1970 [secondOp-Instruction::CastOpsBegin];
1971 switch (ElimCase) {
1972 case 0:
1973 // categorically disallowed
1974 return 0;
1975 case 1:
1976 // allowed, use first cast's opcode
1977 return firstOp;
1978 case 2:
1979 // allowed, use second cast's opcode
1980 return secondOp;
1981 case 3:
1982 // no-op cast in second op implies firstOp as long as the DestTy
1983 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001984 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001985 return firstOp;
1986 return 0;
1987 case 4:
1988 // no-op cast in second op implies firstOp as long as the DestTy
1989 // is floating point
1990 if (DstTy->isFloatingPoint())
1991 return firstOp;
1992 return 0;
1993 case 5:
1994 // no-op cast in first op implies secondOp as long as the SrcTy
1995 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001996 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001997 return secondOp;
1998 return 0;
1999 case 6:
2000 // no-op cast in first op implies secondOp as long as the SrcTy
2001 // is a floating point
2002 if (SrcTy->isFloatingPoint())
2003 return secondOp;
2004 return 0;
2005 case 7: {
2006 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002007 if (!IntPtrTy)
2008 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002009 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2010 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002011 if (MidSize >= PtrSize)
2012 return Instruction::BitCast;
2013 return 0;
2014 }
2015 case 8: {
2016 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2017 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2018 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002019 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2020 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002021 if (SrcSize == DstSize)
2022 return Instruction::BitCast;
2023 else if (SrcSize < DstSize)
2024 return firstOp;
2025 return secondOp;
2026 }
2027 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2028 return Instruction::ZExt;
2029 case 10:
2030 // fpext followed by ftrunc is allowed if the bit size returned to is
2031 // the same as the original, in which case its just a bitcast
2032 if (SrcTy == DstTy)
2033 return Instruction::BitCast;
2034 return 0; // If the types are not the same we can't eliminate it.
2035 case 11:
2036 // bitcast followed by ptrtoint is allowed as long as the bitcast
2037 // is a pointer to pointer cast.
2038 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
2039 return secondOp;
2040 return 0;
2041 case 12:
2042 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2043 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
2044 return firstOp;
2045 return 0;
2046 case 13: {
2047 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002048 if (!IntPtrTy)
2049 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002050 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2051 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2052 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002053 if (SrcSize <= PtrSize && SrcSize == DstSize)
2054 return Instruction::BitCast;
2055 return 0;
2056 }
2057 case 99:
2058 // cast combination can't happen (error in input). This is for all cases
2059 // where the MidTy is not the same for the two cast instructions.
2060 assert(!"Invalid Cast Combination");
2061 return 0;
2062 default:
2063 assert(!"Error in CastResults table!!!");
2064 return 0;
2065 }
2066 return 0;
2067}
2068
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002069CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002070 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002071 // Construct and return the appropriate CastInst subclass
2072 switch (op) {
2073 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2074 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2075 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2076 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2077 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2078 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2079 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2080 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2081 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2082 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2083 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2084 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2085 default:
2086 assert(!"Invalid opcode provided");
2087 }
2088 return 0;
2089}
2090
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002091CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002092 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002093 // Construct and return the appropriate CastInst subclass
2094 switch (op) {
2095 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2096 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2097 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2098 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2099 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2100 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2101 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2102 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2103 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2104 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2105 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2106 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2107 default:
2108 assert(!"Invalid opcode provided");
2109 }
2110 return 0;
2111}
2112
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002113CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002114 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002115 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002116 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002117 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2118 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002119}
2120
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002121CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002122 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002123 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002124 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002125 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2126 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002127}
2128
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002129CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002130 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002131 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002132 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002133 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2134 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002135}
2136
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002137CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002138 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002139 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002140 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002141 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2142 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002143}
2144
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002145CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002146 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002147 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002148 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002149 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2150 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002151}
2152
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002153CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002154 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002155 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002156 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002157 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2158 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002159}
2160
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002161CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002162 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002163 BasicBlock *InsertAtEnd) {
2164 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002165 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002166 "Invalid cast");
2167
Chris Lattner03c49532007-01-15 02:27:26 +00002168 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002169 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2170 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002171}
2172
2173/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002174CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002175 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002176 Instruction *InsertBefore) {
2177 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002178 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002179 "Invalid cast");
2180
Chris Lattner03c49532007-01-15 02:27:26 +00002181 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002182 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2183 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002184}
2185
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002186CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002187 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002188 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002189 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002190 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2191 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002192 Instruction::CastOps opcode =
2193 (SrcBits == DstBits ? Instruction::BitCast :
2194 (SrcBits > DstBits ? Instruction::Trunc :
2195 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002196 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002197}
2198
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002199CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002200 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002201 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002202 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2203 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002204 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2205 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002206 Instruction::CastOps opcode =
2207 (SrcBits == DstBits ? Instruction::BitCast :
2208 (SrcBits > DstBits ? Instruction::Trunc :
2209 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002210 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002211}
2212
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002213CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002214 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002215 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002216 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002217 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002218 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2219 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002220 Instruction::CastOps opcode =
2221 (SrcBits == DstBits ? Instruction::BitCast :
2222 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002223 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002224}
2225
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002226CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002227 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002228 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002229 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002230 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002231 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2232 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002233 Instruction::CastOps opcode =
2234 (SrcBits == DstBits ? Instruction::BitCast :
2235 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002236 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002237}
2238
Duncan Sands55e50902008-01-06 10:12:28 +00002239// Check whether it is valid to call getCastOpcode for these types.
2240// This routine must be kept in sync with getCastOpcode.
2241bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2242 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2243 return false;
2244
2245 if (SrcTy == DestTy)
2246 return true;
2247
2248 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002249 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2250 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002251
2252 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002253 if (DestTy->isInteger()) { // Casting to integral
2254 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002255 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002256 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002257 return true;
2258 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002259 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002260 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002261 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002262 return isa<PointerType>(SrcTy);
2263 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002264 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2265 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002266 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002267 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002268 return true;
2269 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002270 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002271 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002272 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002273 return false;
2274 }
2275 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002276 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002277 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002278 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002279 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002280 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002281 return DestPTy->getBitWidth() == SrcBits;
2282 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002283 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2284 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002285 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002286 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002287 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002288 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002289 return false;
2290 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002291 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002292 return false;
2293 }
2294}
2295
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002296// Provide a way to get a "cast" where the cast opcode is inferred from the
2297// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002298// logic in the castIsValid function below. This axiom should hold:
2299// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2300// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002301// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002302// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002303Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002304CastInst::getCastOpcode(
2305 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002306 // Get the bit sizes, we'll need these
2307 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002308 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2309 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002310
Duncan Sands55e50902008-01-06 10:12:28 +00002311 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2312 "Only first class types are castable!");
2313
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002314 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002315 if (DestTy->isInteger()) { // Casting to integral
2316 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002317 if (DestBits < SrcBits)
2318 return Trunc; // int -> smaller int
2319 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002320 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002321 return SExt; // signed -> SEXT
2322 else
2323 return ZExt; // unsigned -> ZEXT
2324 } else {
2325 return BitCast; // Same size, No-op cast
2326 }
2327 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002328 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002329 return FPToSI; // FP -> sint
2330 else
2331 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002332 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002333 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002334 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002335 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002336 return BitCast; // Same size, no-op cast
2337 } else {
2338 assert(isa<PointerType>(SrcTy) &&
2339 "Casting from a value that is not first-class type");
2340 return PtrToInt; // ptr -> int
2341 }
2342 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002343 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002344 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002345 return SIToFP; // sint -> FP
2346 else
2347 return UIToFP; // uint -> FP
2348 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2349 if (DestBits < SrcBits) {
2350 return FPTrunc; // FP -> smaller FP
2351 } else if (DestBits > SrcBits) {
2352 return FPExt; // FP -> larger FP
2353 } else {
2354 return BitCast; // same size, no-op cast
2355 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002356 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002358 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002359 PTy = NULL;
2360 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002361 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002362 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002364 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2365 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002366 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002367 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002368 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002369 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002370 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002371 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002372 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002373 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002374 }
2375 } else if (isa<PointerType>(DestTy)) {
2376 if (isa<PointerType>(SrcTy)) {
2377 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002378 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379 return IntToPtr; // int -> ptr
2380 } else {
2381 assert(!"Casting pointer to other than pointer or int");
2382 }
2383 } else {
2384 assert(!"Casting to type that is not first-class");
2385 }
2386
2387 // If we fall through to here we probably hit an assertion cast above
2388 // and assertions are not turned on. Anything we return is an error, so
2389 // BitCast is as good a choice as any.
2390 return BitCast;
2391}
2392
2393//===----------------------------------------------------------------------===//
2394// CastInst SubClass Constructors
2395//===----------------------------------------------------------------------===//
2396
2397/// Check that the construction parameters for a CastInst are correct. This
2398/// could be broken out into the separate constructors but it is useful to have
2399/// it in one place and to eliminate the redundant code for getting the sizes
2400/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002401bool
2402CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002403
2404 // Check for type sanity on the arguments
2405 const Type *SrcTy = S->getType();
2406 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2407 return false;
2408
2409 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002410 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2411 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002412
2413 // Switch on the opcode provided
2414 switch (op) {
2415 default: return false; // This is an input error
2416 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002417 return SrcTy->isIntOrIntVector() &&
2418 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002419 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002420 return SrcTy->isIntOrIntVector() &&
2421 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002423 return SrcTy->isIntOrIntVector() &&
2424 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002426 return SrcTy->isFPOrFPVector() &&
2427 DstTy->isFPOrFPVector() &&
2428 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002430 return SrcTy->isFPOrFPVector() &&
2431 DstTy->isFPOrFPVector() &&
2432 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002433 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002434 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002435 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2436 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002437 return SVTy->getElementType()->isIntOrIntVector() &&
2438 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002439 SVTy->getNumElements() == DVTy->getNumElements();
2440 }
2441 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002442 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002443 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002445 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2446 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002447 return SVTy->getElementType()->isFPOrFPVector() &&
2448 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002449 SVTy->getNumElements() == DVTy->getNumElements();
2450 }
2451 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002452 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002454 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002455 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002456 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002457 case Instruction::BitCast:
2458 // BitCast implies a no-op cast of type only. No bits change.
2459 // However, you can't cast pointers to anything but pointers.
2460 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2461 return false;
2462
Duncan Sands55e50902008-01-06 10:12:28 +00002463 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464 // these cases, the cast is okay if the source and destination bit widths
2465 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002466 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002467 }
2468}
2469
2470TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002471 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002473 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002474}
2475
2476TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002477 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002479 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002480}
2481
2482ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002483 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002484) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002485 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002486}
2487
2488ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002489 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002490) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002491 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002492}
2493SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002494 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002495) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002496 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002497}
2498
Jeff Cohencc08c832006-12-02 02:22:01 +00002499SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002500 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002501) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002502 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002503}
2504
2505FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002506 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002508 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002509}
2510
2511FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002512 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002513) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002514 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002515}
2516
2517FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002518 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002519) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002520 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002521}
2522
2523FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002524 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002525) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002526 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002527}
2528
2529UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002530 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002531) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002532 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002533}
2534
2535UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002536 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002537) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002538 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539}
2540
2541SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002542 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002543) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002544 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545}
2546
2547SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002548 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002549) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002550 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002551}
2552
2553FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002554 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002555) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002556 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002557}
2558
2559FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002560 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002562 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563}
2564
2565FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002566 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002567) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002568 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569}
2570
2571FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002572 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002573) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002574 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002575}
2576
2577PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002578 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002579) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002580 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002581}
2582
2583PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002584 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002586 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587}
2588
2589IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002590 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002592 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002593}
2594
2595IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002596 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002597) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002598 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002599}
2600
2601BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002602 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002603) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002604 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002605}
2606
2607BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002608 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002609) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002610 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002611}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002612
2613//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002614// CmpInst Classes
2615//===----------------------------------------------------------------------===//
2616
Nate Begemand2195702008-05-12 19:01:56 +00002617CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002618 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002619 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002620 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002621 OperandTraits<CmpInst>::op_begin(this),
2622 OperandTraits<CmpInst>::operands(this),
2623 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002624 Op<0>() = LHS;
2625 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002626 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002627 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002628}
Gabor Greiff6caff662008-05-10 08:32:32 +00002629
Nate Begemand2195702008-05-12 19:01:56 +00002630CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002631 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002632 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002633 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002634 OperandTraits<CmpInst>::op_begin(this),
2635 OperandTraits<CmpInst>::operands(this),
2636 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002637 Op<0>() = LHS;
2638 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002639 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002640 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002641}
2642
2643CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002644CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002645 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002646 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002647 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002648 if (InsertBefore)
2649 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2650 S1, S2, Name);
2651 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002652 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002653 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002654 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002655
2656 if (InsertBefore)
2657 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2658 S1, S2, Name);
2659 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002660 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002661 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002662}
2663
2664CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002665CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002666 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002667 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002668 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2669 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002670 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002671 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2672 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002673}
2674
2675void CmpInst::swapOperands() {
2676 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2677 IC->swapOperands();
2678 else
2679 cast<FCmpInst>(this)->swapOperands();
2680}
2681
2682bool CmpInst::isCommutative() {
2683 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2684 return IC->isCommutative();
2685 return cast<FCmpInst>(this)->isCommutative();
2686}
2687
2688bool CmpInst::isEquality() {
2689 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2690 return IC->isEquality();
2691 return cast<FCmpInst>(this)->isEquality();
2692}
2693
2694
Dan Gohman4e724382008-05-31 02:47:54 +00002695CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002696 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002697 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002698 case ICMP_EQ: return ICMP_NE;
2699 case ICMP_NE: return ICMP_EQ;
2700 case ICMP_UGT: return ICMP_ULE;
2701 case ICMP_ULT: return ICMP_UGE;
2702 case ICMP_UGE: return ICMP_ULT;
2703 case ICMP_ULE: return ICMP_UGT;
2704 case ICMP_SGT: return ICMP_SLE;
2705 case ICMP_SLT: return ICMP_SGE;
2706 case ICMP_SGE: return ICMP_SLT;
2707 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002708
Dan Gohman4e724382008-05-31 02:47:54 +00002709 case FCMP_OEQ: return FCMP_UNE;
2710 case FCMP_ONE: return FCMP_UEQ;
2711 case FCMP_OGT: return FCMP_ULE;
2712 case FCMP_OLT: return FCMP_UGE;
2713 case FCMP_OGE: return FCMP_ULT;
2714 case FCMP_OLE: return FCMP_UGT;
2715 case FCMP_UEQ: return FCMP_ONE;
2716 case FCMP_UNE: return FCMP_OEQ;
2717 case FCMP_UGT: return FCMP_OLE;
2718 case FCMP_ULT: return FCMP_OGE;
2719 case FCMP_UGE: return FCMP_OLT;
2720 case FCMP_ULE: return FCMP_OGT;
2721 case FCMP_ORD: return FCMP_UNO;
2722 case FCMP_UNO: return FCMP_ORD;
2723 case FCMP_TRUE: return FCMP_FALSE;
2724 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002725 }
2726}
2727
Reid Spencer266e42b2006-12-23 06:05:41 +00002728ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2729 switch (pred) {
2730 default: assert(! "Unknown icmp predicate!");
2731 case ICMP_EQ: case ICMP_NE:
2732 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2733 return pred;
2734 case ICMP_UGT: return ICMP_SGT;
2735 case ICMP_ULT: return ICMP_SLT;
2736 case ICMP_UGE: return ICMP_SGE;
2737 case ICMP_ULE: return ICMP_SLE;
2738 }
2739}
2740
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002741ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2742 switch (pred) {
2743 default: assert(! "Unknown icmp predicate!");
2744 case ICMP_EQ: case ICMP_NE:
2745 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2746 return pred;
2747 case ICMP_SGT: return ICMP_UGT;
2748 case ICMP_SLT: return ICMP_ULT;
2749 case ICMP_SGE: return ICMP_UGE;
2750 case ICMP_SLE: return ICMP_ULE;
2751 }
2752}
2753
Reid Spencer266e42b2006-12-23 06:05:41 +00002754bool ICmpInst::isSignedPredicate(Predicate pred) {
2755 switch (pred) {
2756 default: assert(! "Unknown icmp predicate!");
2757 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2758 return true;
2759 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2760 case ICMP_UGE: case ICMP_ULE:
2761 return false;
2762 }
2763}
2764
Reid Spencer0286bc12007-02-28 22:00:54 +00002765/// Initialize a set of values that all satisfy the condition with C.
2766///
2767ConstantRange
2768ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2769 APInt Lower(C);
2770 APInt Upper(C);
2771 uint32_t BitWidth = C.getBitWidth();
2772 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002773 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002774 case ICmpInst::ICMP_EQ: Upper++; break;
2775 case ICmpInst::ICMP_NE: Lower++; break;
2776 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2777 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2778 case ICmpInst::ICMP_UGT:
2779 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2780 break;
2781 case ICmpInst::ICMP_SGT:
2782 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2783 break;
2784 case ICmpInst::ICMP_ULE:
2785 Lower = APInt::getMinValue(BitWidth); Upper++;
2786 break;
2787 case ICmpInst::ICMP_SLE:
2788 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2789 break;
2790 case ICmpInst::ICMP_UGE:
2791 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2792 break;
2793 case ICmpInst::ICMP_SGE:
2794 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2795 break;
2796 }
2797 return ConstantRange(Lower, Upper);
2798}
2799
Dan Gohman4e724382008-05-31 02:47:54 +00002800CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002801 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002802 default: assert(!"Unknown cmp predicate!");
2803 case ICMP_EQ: case ICMP_NE:
2804 return pred;
2805 case ICMP_SGT: return ICMP_SLT;
2806 case ICMP_SLT: return ICMP_SGT;
2807 case ICMP_SGE: return ICMP_SLE;
2808 case ICMP_SLE: return ICMP_SGE;
2809 case ICMP_UGT: return ICMP_ULT;
2810 case ICMP_ULT: return ICMP_UGT;
2811 case ICMP_UGE: return ICMP_ULE;
2812 case ICMP_ULE: return ICMP_UGE;
2813
Reid Spencerd9436b62006-11-20 01:22:35 +00002814 case FCMP_FALSE: case FCMP_TRUE:
2815 case FCMP_OEQ: case FCMP_ONE:
2816 case FCMP_UEQ: case FCMP_UNE:
2817 case FCMP_ORD: case FCMP_UNO:
2818 return pred;
2819 case FCMP_OGT: return FCMP_OLT;
2820 case FCMP_OLT: return FCMP_OGT;
2821 case FCMP_OGE: return FCMP_OLE;
2822 case FCMP_OLE: return FCMP_OGE;
2823 case FCMP_UGT: return FCMP_ULT;
2824 case FCMP_ULT: return FCMP_UGT;
2825 case FCMP_UGE: return FCMP_ULE;
2826 case FCMP_ULE: return FCMP_UGE;
2827 }
2828}
2829
Reid Spencer266e42b2006-12-23 06:05:41 +00002830bool CmpInst::isUnsigned(unsigned short predicate) {
2831 switch (predicate) {
2832 default: return false;
2833 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2834 case ICmpInst::ICMP_UGE: return true;
2835 }
2836}
2837
2838bool CmpInst::isSigned(unsigned short predicate){
2839 switch (predicate) {
2840 default: return false;
2841 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2842 case ICmpInst::ICMP_SGE: return true;
2843 }
2844}
2845
2846bool CmpInst::isOrdered(unsigned short predicate) {
2847 switch (predicate) {
2848 default: return false;
2849 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2850 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2851 case FCmpInst::FCMP_ORD: return true;
2852 }
2853}
2854
2855bool CmpInst::isUnordered(unsigned short predicate) {
2856 switch (predicate) {
2857 default: return false;
2858 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2859 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2860 case FCmpInst::FCMP_UNO: return true;
2861 }
2862}
2863
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002864//===----------------------------------------------------------------------===//
2865// SwitchInst Implementation
2866//===----------------------------------------------------------------------===//
2867
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002868void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002869 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002870 ReservedSpace = 2+NumCases*2;
2871 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002872 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002873
Gabor Greif2d3024d2008-05-26 21:33:52 +00002874 OperandList[0] = Value;
2875 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002876}
2877
Chris Lattner2195fc42007-02-24 00:55:48 +00002878/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2879/// switch on and a default destination. The number of additional cases can
2880/// be specified here to make memory allocation more efficient. This
2881/// constructor can also autoinsert before another instruction.
2882SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2883 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002884 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2885 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002886 init(Value, Default, NumCases);
2887}
2888
2889/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2890/// switch on and a default destination. The number of additional cases can
2891/// be specified here to make memory allocation more efficient. This
2892/// constructor also autoinserts at the end of the specified BasicBlock.
2893SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2894 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002895 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2896 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002897 init(Value, Default, NumCases);
2898}
2899
Misha Brukmanb1c93172005-04-21 23:48:37 +00002900SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002901 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002902 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002903 Use *OL = OperandList, *InOL = SI.OperandList;
2904 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002905 OL[i] = InOL[i];
2906 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002907 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002908 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002909}
2910
Gordon Henriksen14a55692007-12-10 02:14:30 +00002911SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002912 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002913}
2914
2915
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002916/// addCase - Add an entry to the switch instruction...
2917///
Chris Lattner47ac1872005-02-24 05:32:09 +00002918void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002919 unsigned OpNo = NumOperands;
2920 if (OpNo+2 > ReservedSpace)
2921 resizeOperands(0); // Get more space!
2922 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002923 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002924 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002925 OperandList[OpNo] = OnVal;
2926 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002927}
2928
2929/// removeCase - This method removes the specified successor from the switch
2930/// instruction. Note that this cannot be used to remove the default
2931/// destination (successor #0).
2932///
2933void SwitchInst::removeCase(unsigned idx) {
2934 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002935 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2936
2937 unsigned NumOps = getNumOperands();
2938 Use *OL = OperandList;
2939
2940 // Move everything after this operand down.
2941 //
2942 // FIXME: we could just swap with the end of the list, then erase. However,
2943 // client might not expect this to happen. The code as it is thrashes the
2944 // use/def lists, which is kinda lame.
2945 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2946 OL[i-2] = OL[i];
2947 OL[i-2+1] = OL[i+1];
2948 }
2949
2950 // Nuke the last value.
2951 OL[NumOps-2].set(0);
2952 OL[NumOps-2+1].set(0);
2953 NumOperands = NumOps-2;
2954}
2955
2956/// resizeOperands - resize operands - This adjusts the length of the operands
2957/// list according to the following behavior:
2958/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00002959/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002960/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2961/// 3. If NumOps == NumOperands, trim the reserved space.
2962///
2963void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002964 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002965 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00002966 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002967 } else if (NumOps*2 > NumOperands) {
2968 // No resize needed.
2969 if (ReservedSpace >= NumOps) return;
2970 } else if (NumOps == NumOperands) {
2971 if (ReservedSpace == NumOps) return;
2972 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002973 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002974 }
2975
2976 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002977 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002978 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00002979 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002980 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002981 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002982 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00002983 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002984}
2985
2986
2987BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2988 return getSuccessor(idx);
2989}
2990unsigned SwitchInst::getNumSuccessorsV() const {
2991 return getNumSuccessors();
2992}
2993void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2994 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002995}
Chris Lattnerf22be932004-10-15 23:52:53 +00002996
Chris Lattnerf22be932004-10-15 23:52:53 +00002997// Define these methods here so vtables don't get emitted into every translation
2998// unit that uses these classes.
2999
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003000GetElementPtrInst *GetElementPtrInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003001 GetElementPtrInst *New = new(getNumOperands()) GetElementPtrInst(*this);
3002 New->SubclassOptionalData = SubclassOptionalData;
3003 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00003004}
3005
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003006BinaryOperator *BinaryOperator::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003007 BinaryOperator *New = Create(getOpcode(), Op<0>(), Op<1>());
3008 New->SubclassOptionalData = SubclassOptionalData;
3009 return New;
Chris Lattnerf22be932004-10-15 23:52:53 +00003010}
3011
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003012FCmpInst* FCmpInst::clone(LLVMContext &Context) const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003013 FCmpInst *New = new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003014 New->SubclassOptionalData = SubclassOptionalData;
3015 return New;
Chris Lattner0b490b02007-08-24 20:48:18 +00003016}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003017ICmpInst* ICmpInst::clone(LLVMContext &Context) const {
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003018 ICmpInst *New = new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003019 New->SubclassOptionalData = SubclassOptionalData;
3020 return New;
Reid Spencerd9436b62006-11-20 01:22:35 +00003021}
3022
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003023ExtractValueInst *ExtractValueInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003024 ExtractValueInst *New = new ExtractValueInst(*this);
3025 New->SubclassOptionalData = SubclassOptionalData;
3026 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00003027}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003028InsertValueInst *InsertValueInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003029 InsertValueInst *New = new InsertValueInst(*this);
3030 New->SubclassOptionalData = SubclassOptionalData;
3031 return New;
Dan Gohman0752bff2008-05-23 00:36:11 +00003032}
3033
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003034MallocInst *MallocInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003035 MallocInst *New = new MallocInst(getAllocatedType(),
3036 (Value*)getOperand(0),
3037 getAlignment());
3038 New->SubclassOptionalData = SubclassOptionalData;
3039 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003040}
Dan Gohman0752bff2008-05-23 00:36:11 +00003041
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003042AllocaInst *AllocaInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003043 AllocaInst *New = new AllocaInst(getAllocatedType(),
3044 (Value*)getOperand(0),
3045 getAlignment());
3046 New->SubclassOptionalData = SubclassOptionalData;
3047 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003048}
3049
3050FreeInst *FreeInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003051 FreeInst *New = new FreeInst(getOperand(0));
3052 New->SubclassOptionalData = SubclassOptionalData;
3053 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003054}
3055
3056LoadInst *LoadInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003057 LoadInst *New = new LoadInst(getOperand(0),
3058 Twine(), isVolatile(),
3059 getAlignment());
3060 New->SubclassOptionalData = SubclassOptionalData;
3061 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003062}
3063
3064StoreInst *StoreInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003065 StoreInst *New = new StoreInst(getOperand(0), getOperand(1),
3066 isVolatile(), getAlignment());
3067 New->SubclassOptionalData = SubclassOptionalData;
3068 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003069}
3070
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003071TruncInst *TruncInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003072 TruncInst *New = new TruncInst(getOperand(0), getType());
3073 New->SubclassOptionalData = SubclassOptionalData;
3074 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003075}
3076
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003077ZExtInst *ZExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003078 ZExtInst *New = new ZExtInst(getOperand(0), getType());
3079 New->SubclassOptionalData = SubclassOptionalData;
3080 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003081}
3082
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003083SExtInst *SExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003084 SExtInst *New = new SExtInst(getOperand(0), getType());
3085 New->SubclassOptionalData = SubclassOptionalData;
3086 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003087}
3088
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003089FPTruncInst *FPTruncInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003090 FPTruncInst *New = new FPTruncInst(getOperand(0), getType());
3091 New->SubclassOptionalData = SubclassOptionalData;
3092 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003093}
3094
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003095FPExtInst *FPExtInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003096 FPExtInst *New = new FPExtInst(getOperand(0), getType());
3097 New->SubclassOptionalData = SubclassOptionalData;
3098 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003099}
3100
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003101UIToFPInst *UIToFPInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003102 UIToFPInst *New = new UIToFPInst(getOperand(0), getType());
3103 New->SubclassOptionalData = SubclassOptionalData;
3104 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003105}
3106
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003107SIToFPInst *SIToFPInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003108 SIToFPInst *New = new SIToFPInst(getOperand(0), getType());
3109 New->SubclassOptionalData = SubclassOptionalData;
3110 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003111}
3112
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003113FPToUIInst *FPToUIInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003114 FPToUIInst *New = new FPToUIInst(getOperand(0), getType());
3115 New->SubclassOptionalData = SubclassOptionalData;
3116 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003117}
3118
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003119FPToSIInst *FPToSIInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003120 FPToSIInst *New = new FPToSIInst(getOperand(0), getType());
3121 New->SubclassOptionalData = SubclassOptionalData;
3122 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003123}
3124
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003125PtrToIntInst *PtrToIntInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003126 PtrToIntInst *New = new PtrToIntInst(getOperand(0), getType());
3127 New->SubclassOptionalData = SubclassOptionalData;
3128 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003129}
3130
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003131IntToPtrInst *IntToPtrInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003132 IntToPtrInst *New = new IntToPtrInst(getOperand(0), getType());
3133 New->SubclassOptionalData = SubclassOptionalData;
3134 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003135}
3136
Dan Gohmanfaf516f2009-08-25 22:29:08 +00003137BitCastInst *BitCastInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003138 BitCastInst *New = new BitCastInst(getOperand(0), getType());
3139 New->SubclassOptionalData = SubclassOptionalData;
3140 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003141}
3142
3143CallInst *CallInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003144 CallInst *New = new(getNumOperands()) CallInst(*this);
3145 New->SubclassOptionalData = SubclassOptionalData;
3146 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003147}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003148
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003149SelectInst *SelectInst::clone(LLVMContext&) const {
3150 SelectInst *New = SelectInst::Create(getOperand(0),
3151 getOperand(1),
3152 getOperand(2));
3153 New->SubclassOptionalData = SubclassOptionalData;
3154 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003155}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003156
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003157VAArgInst *VAArgInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003158 VAArgInst *New = new VAArgInst(getOperand(0), getType());
3159 New->SubclassOptionalData = SubclassOptionalData;
3160 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003161}
3162
3163ExtractElementInst *ExtractElementInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003164 ExtractElementInst *New = ExtractElementInst::Create(getOperand(0),
3165 getOperand(1));
3166 New->SubclassOptionalData = SubclassOptionalData;
3167 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003168}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003169
3170InsertElementInst *InsertElementInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003171 InsertElementInst *New = InsertElementInst::Create(getOperand(0),
3172 getOperand(1),
3173 getOperand(2));
3174 New->SubclassOptionalData = SubclassOptionalData;
3175 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003176}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003177
3178ShuffleVectorInst *ShuffleVectorInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003179 ShuffleVectorInst *New = new ShuffleVectorInst(getOperand(0),
3180 getOperand(1),
3181 getOperand(2));
3182 New->SubclassOptionalData = SubclassOptionalData;
3183 return New;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003184}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003185
3186PHINode *PHINode::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003187 PHINode *New = new PHINode(*this);
3188 New->SubclassOptionalData = SubclassOptionalData;
3189 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003190}
3191
3192ReturnInst *ReturnInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003193 ReturnInst *New = new(getNumOperands()) ReturnInst(*this);
3194 New->SubclassOptionalData = SubclassOptionalData;
3195 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003196}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003197
3198BranchInst *BranchInst::clone(LLVMContext&) const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003199 unsigned Ops(getNumOperands());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003200 BranchInst *New = new(Ops, Ops == 1) BranchInst(*this);
3201 New->SubclassOptionalData = SubclassOptionalData;
3202 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003203}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003204
3205SwitchInst *SwitchInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003206 SwitchInst *New = new SwitchInst(*this);
3207 New->SubclassOptionalData = SubclassOptionalData;
3208 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003209}
3210
3211InvokeInst *InvokeInst::clone(LLVMContext&) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003212 InvokeInst *New = new(getNumOperands()) InvokeInst(*this);
3213 New->SubclassOptionalData = SubclassOptionalData;
3214 return New;
Gabor Greif697e94c2008-05-15 10:04:30 +00003215}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003216
Owen Anderson55f1c092009-08-13 21:58:54 +00003217UnwindInst *UnwindInst::clone(LLVMContext &C) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003218 UnwindInst *New = new UnwindInst(C);
3219 New->SubclassOptionalData = SubclassOptionalData;
3220 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003221}
3222
Owen Anderson55f1c092009-08-13 21:58:54 +00003223UnreachableInst *UnreachableInst::clone(LLVMContext &C) const {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003224 UnreachableInst *New = new UnreachableInst(C);
3225 New->SubclassOptionalData = SubclassOptionalData;
3226 return New;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003227}