blob: 65786dffed578dce0a37e659645e1279d1a3b5be [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
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Dan Gohman22571482009-09-03 15:34:35 +000022#include "llvm/Analysis/Dominators.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000025#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greif1b9921a2009-01-11 22:33:22 +000033#define CALLSITE_DELEGATE_GETTER(METHOD) \
Eric Christopherb1a382d2010-03-25 04:49:10 +000034 Instruction *II = getInstruction(); \
Gabor Greif1b9921a2009-01-11 22:33:22 +000035 return isCall() \
36 ? cast<CallInst>(II)->METHOD \
37 : cast<InvokeInst>(II)->METHOD
38
39#define CALLSITE_DELEGATE_SETTER(METHOD) \
Eric Christopherb1a382d2010-03-25 04:49:10 +000040 Instruction *II = getInstruction(); \
Gabor Greif1b9921a2009-01-11 22:33:22 +000041 if (isCall()) \
42 cast<CallInst>(II)->METHOD; \
43 else \
44 cast<InvokeInst>(II)->METHOD
45
Sandeep Patel68c5f472009-09-02 08:44:58 +000046CallingConv::ID CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000047 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000048}
Sandeep Patel68c5f472009-09-02 08:44:58 +000049void CallSite::setCallingConv(CallingConv::ID CC) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000050 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
Chris Lattnerf7b6d312005-05-06 20:26:43 +000051}
Devang Patel4c758ea2008-09-25 21:00:45 +000052const AttrListPtr &CallSite::getAttributes() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000053 CALLSITE_DELEGATE_GETTER(getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000054}
Devang Patel4c758ea2008-09-25 21:00:45 +000055void CallSite::setAttributes(const AttrListPtr &PAL) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000056 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000057}
Devang Patelba3fa6c2008-09-23 23:03:40 +000058bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000059 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
Duncan Sands5208d1a2007-11-28 17:07:01 +000060}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000061uint16_t CallSite::getParamAlignment(uint16_t i) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000062 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
Dale Johanneseneabc5f32008-02-22 17:49:45 +000063}
Eric Christopherb1a382d2010-03-25 04:49:10 +000064
65/// @brief Return true if the call should not be inlined.
66bool CallSite::isNoInline() const {
67 CALLSITE_DELEGATE_GETTER(isNoInline());
68}
69
70void CallSite::setIsNoInline(bool Value) {
71 CALLSITE_DELEGATE_GETTER(setIsNoInline(Value));
72}
73
74
Duncan Sands38ef3a82007-12-03 20:06:50 +000075bool CallSite::doesNotAccessMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000076 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000077}
Duncan Sands78c88722008-07-08 08:38:44 +000078void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000079 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000080}
Duncan Sands38ef3a82007-12-03 20:06:50 +000081bool CallSite::onlyReadsMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000082 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000083}
Duncan Sands78c88722008-07-08 08:38:44 +000084void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000085 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000086}
87bool CallSite::doesNotReturn() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000088 CALLSITE_DELEGATE_GETTER(doesNotReturn());
Duncan Sands78c88722008-07-08 08:38:44 +000089}
90void CallSite::setDoesNotReturn(bool doesNotReturn) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000091 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
Duncan Sands78c88722008-07-08 08:38:44 +000092}
Duncan Sands3353ed02007-12-18 09:59:50 +000093bool CallSite::doesNotThrow() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000094 CALLSITE_DELEGATE_GETTER(doesNotThrow());
Duncan Sands8e4847e2007-12-16 15:51:49 +000095}
Duncan Sandsaa31b922007-12-19 21:13:37 +000096void CallSite::setDoesNotThrow(bool doesNotThrow) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000097 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
Duncan Sandsaa31b922007-12-19 21:13:37 +000098}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000099
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +0000100bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +0000101 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
102 if (AI->get() == Arg)
103 return true;
104 return false;
105}
106
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000107User::op_iterator CallSite::getCallee() const {
108 Instruction *II(getInstruction());
109 return isCall()
110 ? cast<CallInst>(II)->op_begin()
111 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Function
112}
113
Gabor Greif1b9921a2009-01-11 22:33:22 +0000114#undef CALLSITE_DELEGATE_GETTER
115#undef CALLSITE_DELEGATE_SETTER
116
Gordon Henriksen14a55692007-12-10 02:14:30 +0000117//===----------------------------------------------------------------------===//
118// TerminatorInst Class
119//===----------------------------------------------------------------------===//
120
121// Out of line virtual method, so the vtable, etc has a home.
122TerminatorInst::~TerminatorInst() {
123}
124
Gabor Greiff6caff662008-05-10 08:32:32 +0000125//===----------------------------------------------------------------------===//
126// UnaryInstruction Class
127//===----------------------------------------------------------------------===//
128
Gordon Henriksen14a55692007-12-10 02:14:30 +0000129// Out of line virtual method, so the vtable, etc has a home.
130UnaryInstruction::~UnaryInstruction() {
131}
132
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000133//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000134// SelectInst Class
135//===----------------------------------------------------------------------===//
136
137/// areInvalidOperands - Return a string if the specified operands are invalid
138/// for a select operation, otherwise return null.
139const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
140 if (Op1->getType() != Op2->getType())
141 return "both values to select must have same type";
142
143 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
144 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000145 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000146 return "vector select condition element type must be i1";
147 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
148 if (ET == 0)
149 return "selected values for vector select must be vectors";
150 if (ET->getNumElements() != VT->getNumElements())
151 return "vector select requires selected vectors to have "
152 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000153 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000154 return "select condition must be i1 or <n x i1>";
155 }
156 return 0;
157}
158
159
160//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000161// PHINode Class
162//===----------------------------------------------------------------------===//
163
164PHINode::PHINode(const PHINode &PN)
165 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000166 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000167 ReservedSpace(PN.getNumOperands()) {
168 Use *OL = OperandList;
169 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000170 OL[i] = PN.getOperand(i);
171 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000172 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000173 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000174}
175
Gordon Henriksen14a55692007-12-10 02:14:30 +0000176PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000177 if (OperandList)
178 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000179}
180
181// removeIncomingValue - Remove an incoming value. This is useful if a
182// predecessor basic block is deleted.
183Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
184 unsigned NumOps = getNumOperands();
185 Use *OL = OperandList;
186 assert(Idx*2 < NumOps && "BB not in PHI node!");
187 Value *Removed = OL[Idx*2];
188
189 // Move everything after this operand down.
190 //
191 // FIXME: we could just swap with the end of the list, then erase. However,
192 // client might not expect this to happen. The code as it is thrashes the
193 // use/def lists, which is kinda lame.
194 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
195 OL[i-2] = OL[i];
196 OL[i-2+1] = OL[i+1];
197 }
198
199 // Nuke the last value.
200 OL[NumOps-2].set(0);
201 OL[NumOps-2+1].set(0);
202 NumOperands = NumOps-2;
203
204 // If the PHI node is dead, because it has zero entries, nuke it now.
205 if (NumOps == 2 && DeletePHIIfEmpty) {
206 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000207 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000208 eraseFromParent();
209 }
210 return Removed;
211}
212
213/// resizeOperands - resize operands - This adjusts the length of the operands
214/// list according to the following behavior:
215/// 1. If NumOps == 0, grow the operand list in response to a push_back style
216/// of operation. This grows the number of ops by 1.5 times.
217/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
218/// 3. If NumOps == NumOperands, trim the reserved space.
219///
220void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000221 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000222 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000223 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000224 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
225 } else if (NumOps*2 > NumOperands) {
226 // No resize needed.
227 if (ReservedSpace >= NumOps) return;
228 } else if (NumOps == NumOperands) {
229 if (ReservedSpace == NumOps) return;
230 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000231 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000232 }
233
234 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000235 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000236 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000237 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000238 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000239 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240}
241
Nate Begemanb3923212005-08-04 23:24:19 +0000242/// hasConstantValue - If the specified PHI node always merges together the same
243/// value, return the value, otherwise return null.
244///
Dan Gohman22571482009-09-03 15:34:35 +0000245/// If the PHI has undef operands, but all the rest of the operands are
246/// some unique value, return that value if it can be proved that the
247/// value dominates the PHI. If DT is null, use a conservative check,
248/// otherwise use DT to test for dominance.
249///
250Value *PHINode::hasConstantValue(DominatorTree *DT) const {
Chris Lattner7d08da62009-09-21 22:27:34 +0000251 // If the PHI node only has one incoming value, eliminate the PHI node.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000252 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000253 if (getIncomingValue(0) != this) // not X = phi X
254 return getIncomingValue(0);
Chris Lattner7d08da62009-09-21 22:27:34 +0000255 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000256 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000257
Nate Begemanb3923212005-08-04 23:24:19 +0000258 // Otherwise if all of the incoming values are the same for the PHI, replace
259 // the PHI node with the incoming value.
260 //
261 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000262 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000263 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000264 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000265 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000266 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000267 if (InVal && getIncomingValue(i) != InVal)
268 return 0; // Not the same, bail out.
Chris Lattner7d08da62009-09-21 22:27:34 +0000269 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000270 }
Nate Begemanb3923212005-08-04 23:24:19 +0000271
272 // The only case that could cause InVal to be null is if we have a PHI node
273 // that only has entries for itself. In this case, there is no entry into the
274 // loop, so kill the PHI.
275 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000276 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000277
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000278 // If we have a PHI node like phi(X, undef, X), where X is defined by some
279 // instruction, we cannot always return X as the result of the PHI node. Only
280 // do this if X is not an instruction (thus it must dominate the PHI block),
281 // or if the client is prepared to deal with this possibility.
Chris Lattner7d08da62009-09-21 22:27:34 +0000282 if (!HasUndefInput || !isa<Instruction>(InVal))
283 return InVal;
284
285 Instruction *IV = cast<Instruction>(InVal);
286 if (DT) {
287 // We have a DominatorTree. Do a precise test.
288 if (!DT->dominates(IV, this))
289 return 0;
290 } else {
291 // If it is in the entry block, it obviously dominates everything.
292 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
293 isa<InvokeInst>(IV))
294 return 0; // Cannot guarantee that InVal dominates this PHINode.
295 }
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000296
Nate Begemanb3923212005-08-04 23:24:19 +0000297 // All of the incoming values are the same, return the value now.
298 return InVal;
299}
300
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000301
302//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303// CallInst Implementation
304//===----------------------------------------------------------------------===//
305
Gordon Henriksen14a55692007-12-10 02:14:30 +0000306CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000307}
308
Chris Lattner054ba2c2007-02-13 00:58:44 +0000309void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000310 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
311 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000312 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313
Misha Brukmanb1c93172005-04-21 23:48:37 +0000314 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000316 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000317
Chris Lattner054ba2c2007-02-13 00:58:44 +0000318 assert((NumParams == FTy->getNumParams() ||
319 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000320 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000321 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000322 assert((i >= FTy->getNumParams() ||
323 FTy->getParamType(i) == Params[i]->getType()) &&
324 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000325 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000326 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327}
328
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000329void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000330 assert(NumOperands == 3 && "NumOperands not set up?");
331 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000332 OL[0] = Func;
333 OL[1] = Actual1;
334 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000335
336 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000337 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000338 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000339
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000340 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000341 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000342 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000343 assert((0 >= FTy->getNumParams() ||
344 FTy->getParamType(0) == Actual1->getType()) &&
345 "Calling a function with a bad signature!");
346 assert((1 >= FTy->getNumParams() ||
347 FTy->getParamType(1) == Actual2->getType()) &&
348 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000349}
350
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000351void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000352 assert(NumOperands == 2 && "NumOperands not set up?");
353 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000354 OL[0] = Func;
355 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000356
357 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000358 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000359 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000360
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000361 assert((FTy->getNumParams() == 1 ||
362 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000363 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000364 assert((0 == FTy->getNumParams() ||
365 FTy->getParamType(0) == Actual->getType()) &&
366 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000367}
368
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000369void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000370 assert(NumOperands == 1 && "NumOperands not set up?");
371 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000372 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000373
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000374 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000375 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000376 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000377
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000378 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000379}
380
Daniel Dunbar4975db62009-07-25 04:41:11 +0000381CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000382 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000383 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
384 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000385 Instruction::Call,
386 OperandTraits<CallInst>::op_end(this) - 2,
387 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000388 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000389 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000390}
391
Daniel Dunbar4975db62009-07-25 04:41:11 +0000392CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000393 BasicBlock *InsertAtEnd)
394 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
395 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000396 Instruction::Call,
397 OperandTraits<CallInst>::op_end(this) - 2,
398 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000399 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000400 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000401}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000402CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000403 Instruction *InsertBefore)
404 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
405 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000406 Instruction::Call,
407 OperandTraits<CallInst>::op_end(this) - 1,
408 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000409 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000410 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000411}
412
Daniel Dunbar4975db62009-07-25 04:41:11 +0000413CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000414 BasicBlock *InsertAtEnd)
415 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
416 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000417 Instruction::Call,
418 OperandTraits<CallInst>::op_end(this) - 1,
419 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000420 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000421 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000422}
423
Misha Brukmanb1c93172005-04-21 23:48:37 +0000424CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000425 : Instruction(CI.getType(), Instruction::Call,
426 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000427 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000428 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000429 setTailCall(CI.isTailCall());
430 setCallingConv(CI.getCallingConv());
431
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000432 Use *OL = OperandList;
433 Use *InOL = CI.OperandList;
434 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000435 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000436 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000437}
438
Devang Patel4c758ea2008-09-25 21:00:45 +0000439void CallInst::addAttribute(unsigned i, Attributes attr) {
440 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000441 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000442 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000443}
444
Devang Patel4c758ea2008-09-25 21:00:45 +0000445void CallInst::removeAttribute(unsigned i, Attributes attr) {
446 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000447 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000448 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000449}
450
Devang Patelba3fa6c2008-09-23 23:03:40 +0000451bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000452 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000453 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000454 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000455 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000456 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000457}
458
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000459/// IsConstantOne - Return true only if val is constant int 1
460static bool IsConstantOne(Value *val) {
461 assert(val && "IsConstantOne does not work with NULL val");
462 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
463}
464
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000465static Instruction *createMalloc(Instruction *InsertBefore,
466 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000467 const Type *AllocTy, Value *AllocSize,
468 Value *ArraySize, Function *MallocF,
469 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000470 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000471 "createMalloc needs either InsertBefore or InsertAtEnd");
472
473 // malloc(type) becomes:
474 // bitcast (i8* malloc(typeSize)) to type*
475 // malloc(type, arraySize) becomes:
476 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000477 if (!ArraySize)
478 ArraySize = ConstantInt::get(IntPtrTy, 1);
479 else if (ArraySize->getType() != IntPtrTy) {
480 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000481 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
482 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000483 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000484 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
485 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000486 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000487
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000488 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000489 if (IsConstantOne(AllocSize)) {
490 AllocSize = ArraySize; // Operand * 1 = Operand
491 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
492 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
493 false /*ZExt*/);
494 // Malloc arg is constant product of type size and array size
495 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
496 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000497 // Multiply type size by the array size...
498 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000499 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
500 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000501 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000502 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
503 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000504 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000505 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000506
Victor Hernandez788eaab2009-09-18 19:20:02 +0000507 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000508 // Create the call to Malloc.
509 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
510 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000511 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000512 Value *MallocFunc = MallocF;
513 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000514 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000515 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000516 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000517 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000518 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000519 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000520 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000521 Result = MCall;
522 if (Result->getType() != AllocPtrType)
523 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000524 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000525 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000526 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000527 Result = MCall;
528 if (Result->getType() != AllocPtrType) {
529 InsertAtEnd->getInstList().push_back(MCall);
530 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000531 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000532 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000533 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000534 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000535 if (Function *F = dyn_cast<Function>(MallocFunc)) {
536 MCall->setCallingConv(F->getCallingConv());
537 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
538 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000539 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000540
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000541 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000542}
543
544/// CreateMalloc - Generate the IR for a call to malloc:
545/// 1. Compute the malloc call's argument as the specified type's size,
546/// possibly multiplied by the array size if the array size is not
547/// constant 1.
548/// 2. Call malloc with that argument.
549/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000550Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
551 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000552 Value *AllocSize, Value *ArraySize,
553 const Twine &Name) {
554 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000555 ArraySize, NULL, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000556}
557
558/// CreateMalloc - Generate the IR for a call to malloc:
559/// 1. Compute the malloc call's argument as the specified type's size,
560/// possibly multiplied by the array size if the array size is not
561/// constant 1.
562/// 2. Call malloc with that argument.
563/// 3. Bitcast the result of the malloc call to the specified type.
564/// Note: This function does not add the bitcast to the basic block, that is the
565/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000566Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
567 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000568 Value *AllocSize, Value *ArraySize,
569 Function *MallocF, const Twine &Name) {
570 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000571 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000572}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000573
Victor Hernandeze2971492009-10-24 04:23:03 +0000574static Instruction* createFree(Value* Source, Instruction *InsertBefore,
575 BasicBlock *InsertAtEnd) {
576 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
577 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000578 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000579 "Can not free something of nonpointer type!");
580
581 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
582 Module* M = BB->getParent()->getParent();
583
584 const Type *VoidTy = Type::getVoidTy(M->getContext());
585 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
586 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000587 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000588 CallInst* Result = NULL;
589 Value *PtrCast = Source;
590 if (InsertBefore) {
591 if (Source->getType() != IntPtrTy)
592 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
593 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
594 } else {
595 if (Source->getType() != IntPtrTy)
596 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
597 Result = CallInst::Create(FreeFunc, PtrCast, "");
598 }
599 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000600 if (Function *F = dyn_cast<Function>(FreeFunc))
601 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000602
603 return Result;
604}
605
606/// CreateFree - Generate the IR for a call to the builtin free function.
607void CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
608 createFree(Source, InsertBefore, NULL);
609}
610
611/// CreateFree - Generate the IR for a call to the builtin free function.
612/// Note: This function does not add the call to the basic block, that is the
613/// responsibility of the caller.
614Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
615 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
616 assert(FreeCall && "CreateFree did not create a CallInst");
617 return FreeCall;
618}
619
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000620//===----------------------------------------------------------------------===//
621// InvokeInst Implementation
622//===----------------------------------------------------------------------===//
623
624void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000625 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000626 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000627 Op<-3>() = Fn;
628 Op<-2>() = IfNormal;
629 Op<-1>() = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000630 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000631 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000632 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000633
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000634 assert(((NumArgs == FTy->getNumParams()) ||
635 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000636 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000637
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000638 Use *OL = OperandList;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000639 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000640 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000641 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000642 "Invoking a function with a bad signature!");
643
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000644 OL[i] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000645 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000646}
647
Misha Brukmanb1c93172005-04-21 23:48:37 +0000648InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000649 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000650 OperandTraits<InvokeInst>::op_end(this)
651 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000652 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000653 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000654 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655 Use *OL = OperandList, *InOL = II.OperandList;
656 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000657 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000658 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000659}
660
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000661BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
662 return getSuccessor(idx);
663}
664unsigned InvokeInst::getNumSuccessorsV() const {
665 return getNumSuccessors();
666}
667void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
668 return setSuccessor(idx, B);
669}
670
Devang Patelba3fa6c2008-09-23 23:03:40 +0000671bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000672 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000673 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000674 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000675 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000676 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000677}
678
Devang Patel4c758ea2008-09-25 21:00:45 +0000679void InvokeInst::addAttribute(unsigned i, Attributes attr) {
680 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000681 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000682 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000683}
684
Devang Patel4c758ea2008-09-25 21:00:45 +0000685void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
686 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000687 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000688 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000689}
690
Duncan Sands5208d1a2007-11-28 17:07:01 +0000691
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000692//===----------------------------------------------------------------------===//
693// ReturnInst Implementation
694//===----------------------------------------------------------------------===//
695
Chris Lattner2195fc42007-02-24 00:55:48 +0000696ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000697 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000698 OperandTraits<ReturnInst>::op_end(this) -
699 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000700 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000701 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000702 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000703 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000704}
705
Owen Anderson55f1c092009-08-13 21:58:54 +0000706ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
707 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000708 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
709 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000710 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000711 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000712}
Owen Anderson55f1c092009-08-13 21:58:54 +0000713ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
714 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000715 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
716 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000717 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000718 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000719}
Owen Anderson55f1c092009-08-13 21:58:54 +0000720ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
721 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000722 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000723}
724
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000725unsigned ReturnInst::getNumSuccessorsV() const {
726 return getNumSuccessors();
727}
728
Devang Patelae682fb2008-02-26 17:56:20 +0000729/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
730/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000731void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000732 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000733}
734
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000735BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000736 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000737 return 0;
738}
739
Devang Patel59643e52008-02-23 00:35:18 +0000740ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000741}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000742
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000743//===----------------------------------------------------------------------===//
744// UnwindInst Implementation
745//===----------------------------------------------------------------------===//
746
Owen Anderson55f1c092009-08-13 21:58:54 +0000747UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
748 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
749 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000750}
Owen Anderson55f1c092009-08-13 21:58:54 +0000751UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
752 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
753 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000754}
755
756
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000757unsigned UnwindInst::getNumSuccessorsV() const {
758 return getNumSuccessors();
759}
760
761void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000762 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000763}
764
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000765BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000766 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000767 return 0;
768}
769
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000770//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000771// UnreachableInst Implementation
772//===----------------------------------------------------------------------===//
773
Owen Anderson55f1c092009-08-13 21:58:54 +0000774UnreachableInst::UnreachableInst(LLVMContext &Context,
775 Instruction *InsertBefore)
776 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
777 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000778}
Owen Anderson55f1c092009-08-13 21:58:54 +0000779UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
780 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
781 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000782}
783
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000784unsigned UnreachableInst::getNumSuccessorsV() const {
785 return getNumSuccessors();
786}
787
788void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000789 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000790}
791
792BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000793 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000794 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000795}
796
797//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000798// BranchInst Implementation
799//===----------------------------------------------------------------------===//
800
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000801void BranchInst::AssertOK() {
802 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000803 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000804 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000805}
806
Chris Lattner2195fc42007-02-24 00:55:48 +0000807BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000808 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000809 OperandTraits<BranchInst>::op_end(this) - 1,
810 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000811 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000812 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000813}
814BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
815 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000816 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000817 OperandTraits<BranchInst>::op_end(this) - 3,
818 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000819 Op<-1>() = IfTrue;
820 Op<-2>() = IfFalse;
821 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000822#ifndef NDEBUG
823 AssertOK();
824#endif
825}
826
827BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000828 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000829 OperandTraits<BranchInst>::op_end(this) - 1,
830 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000831 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000832 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000833}
834
835BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
836 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000837 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000838 OperandTraits<BranchInst>::op_end(this) - 3,
839 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000840 Op<-1>() = IfTrue;
841 Op<-2>() = IfFalse;
842 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000843#ifndef NDEBUG
844 AssertOK();
845#endif
846}
847
848
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000849BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000850 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000851 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
852 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000853 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000854 if (BI.getNumOperands() != 1) {
855 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000856 Op<-3>() = BI.Op<-3>();
857 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000858 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000859 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000860}
861
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000862
863Use* Use::getPrefix() {
864 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
865 if (PotentialPrefix.getOpaqueValue())
866 return 0;
867
868 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
869}
870
871BranchInst::~BranchInst() {
872 if (NumOperands == 1) {
873 if (Use *Prefix = OperandList->getPrefix()) {
874 Op<-1>() = 0;
875 //
876 // mark OperandList to have a special value for scrutiny
877 // by baseclass destructors and operator delete
878 OperandList = Prefix;
879 } else {
880 NumOperands = 3;
881 OperandList = op_begin();
882 }
883 }
884}
885
886
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000887BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
888 return getSuccessor(idx);
889}
890unsigned BranchInst::getNumSuccessorsV() const {
891 return getNumSuccessors();
892}
893void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
894 setSuccessor(idx, B);
895}
896
897
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000898//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000899// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000900//===----------------------------------------------------------------------===//
901
Owen Andersonb6b25302009-07-14 23:09:55 +0000902static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000903 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000904 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000905 else {
906 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000907 "Passed basic block into allocation size parameter! Use other ctor");
Duncan Sands9dff9be2010-02-15 16:12:20 +0000908 assert(Amt->getType()->isIntegerTy(32) &&
Victor Hernandeza3aaf852009-10-17 01:18:07 +0000909 "Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000910 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000911 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000912}
913
Victor Hernandez8acf2952009-10-23 21:09:37 +0000914AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
915 const Twine &Name, Instruction *InsertBefore)
916 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
917 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
918 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000919 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000920 setName(Name);
921}
922
923AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
924 const Twine &Name, BasicBlock *InsertAtEnd)
925 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
926 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
927 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000928 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000929 setName(Name);
930}
931
932AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
933 Instruction *InsertBefore)
934 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
935 getAISize(Ty->getContext(), 0), InsertBefore) {
936 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000937 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000938 setName(Name);
939}
940
941AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
942 BasicBlock *InsertAtEnd)
943 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
944 getAISize(Ty->getContext(), 0), InsertAtEnd) {
945 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000946 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000947 setName(Name);
948}
949
950AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
951 const Twine &Name, Instruction *InsertBefore)
952 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000953 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000954 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000955 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000956 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000957}
958
Victor Hernandez8acf2952009-10-23 21:09:37 +0000959AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
960 const Twine &Name, BasicBlock *InsertAtEnd)
961 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000962 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000963 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000964 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000965 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000966}
967
Gordon Henriksen14a55692007-12-10 02:14:30 +0000968// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000969AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000970}
971
Victor Hernandez8acf2952009-10-23 21:09:37 +0000972void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000973 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000974 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000975 assert(getAlignment() == Align && "Alignment representation error!");
976}
977
Victor Hernandez8acf2952009-10-23 21:09:37 +0000978bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000979 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
980 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000981 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000982}
983
Victor Hernandez8acf2952009-10-23 21:09:37 +0000984const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000985 return getType()->getElementType();
986}
987
Chris Lattner8b291e62008-11-26 02:54:17 +0000988/// isStaticAlloca - Return true if this alloca is in the entry block of the
989/// function and is a constant size. If so, the code generator will fold it
990/// into the prolog/epilog code, so it is basically free.
991bool AllocaInst::isStaticAlloca() const {
992 // Must be constant size.
993 if (!isa<ConstantInt>(getArraySize())) return false;
994
995 // Must be in the entry block.
996 const BasicBlock *Parent = getParent();
997 return Parent == &Parent->getParent()->front();
998}
999
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001000//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001001// LoadInst Implementation
1002//===----------------------------------------------------------------------===//
1003
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001004void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001005 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001006 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001007}
1008
Daniel Dunbar4975db62009-07-25 04:41:11 +00001009LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001010 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001011 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001012 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001013 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001014 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001015 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001016}
1017
Daniel Dunbar4975db62009-07-25 04:41:11 +00001018LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001019 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001020 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001021 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001022 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001023 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001024 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001025}
1026
Daniel Dunbar4975db62009-07-25 04:41:11 +00001027LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001028 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001029 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001030 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001031 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001032 setAlignment(0);
1033 AssertOK();
1034 setName(Name);
1035}
1036
Daniel Dunbar4975db62009-07-25 04:41:11 +00001037LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001038 unsigned Align, Instruction *InsertBef)
1039 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1040 Load, Ptr, InsertBef) {
1041 setVolatile(isVolatile);
1042 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001043 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001044 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001045}
1046
Daniel Dunbar4975db62009-07-25 04:41:11 +00001047LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001048 unsigned Align, BasicBlock *InsertAE)
1049 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1050 Load, Ptr, InsertAE) {
1051 setVolatile(isVolatile);
1052 setAlignment(Align);
1053 AssertOK();
1054 setName(Name);
1055}
1056
Daniel Dunbar4975db62009-07-25 04:41:11 +00001057LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001058 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001059 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001060 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001061 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001062 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +00001063 AssertOK();
1064 setName(Name);
1065}
1066
Daniel Dunbar27096822009-08-11 18:11:15 +00001067
1068
1069LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1070 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1071 Load, Ptr, InsertBef) {
1072 setVolatile(false);
1073 setAlignment(0);
1074 AssertOK();
1075 if (Name && Name[0]) setName(Name);
1076}
1077
1078LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1079 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1080 Load, Ptr, InsertAE) {
1081 setVolatile(false);
1082 setAlignment(0);
1083 AssertOK();
1084 if (Name && Name[0]) setName(Name);
1085}
1086
1087LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1088 Instruction *InsertBef)
1089: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1090 Load, Ptr, InsertBef) {
1091 setVolatile(isVolatile);
1092 setAlignment(0);
1093 AssertOK();
1094 if (Name && Name[0]) setName(Name);
1095}
1096
1097LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1098 BasicBlock *InsertAE)
1099 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1100 Load, Ptr, InsertAE) {
1101 setVolatile(isVolatile);
1102 setAlignment(0);
1103 AssertOK();
1104 if (Name && Name[0]) setName(Name);
1105}
1106
Christopher Lamb84485702007-04-22 19:24:39 +00001107void LoadInst::setAlignment(unsigned Align) {
1108 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001109 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1110 ((Log2_32(Align)+1)<<1));
Christopher Lamb84485702007-04-22 19:24:39 +00001111}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001112
1113//===----------------------------------------------------------------------===//
1114// StoreInst Implementation
1115//===----------------------------------------------------------------------===//
1116
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001117void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001118 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001119 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001120 "Ptr must have pointer type!");
1121 assert(getOperand(0)->getType() ==
1122 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001123 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001124}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001125
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001126
1127StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001128 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001129 OperandTraits<StoreInst>::op_begin(this),
1130 OperandTraits<StoreInst>::operands(this),
1131 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001132 Op<0>() = val;
1133 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001134 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001135 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001136 AssertOK();
1137}
1138
1139StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001140 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001141 OperandTraits<StoreInst>::op_begin(this),
1142 OperandTraits<StoreInst>::operands(this),
1143 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001144 Op<0>() = val;
1145 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001146 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001147 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001148 AssertOK();
1149}
1150
Misha Brukmanb1c93172005-04-21 23:48:37 +00001151StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001152 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001153 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001154 OperandTraits<StoreInst>::op_begin(this),
1155 OperandTraits<StoreInst>::operands(this),
1156 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001157 Op<0>() = val;
1158 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001159 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001160 setAlignment(0);
1161 AssertOK();
1162}
1163
1164StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1165 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001166 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001167 OperandTraits<StoreInst>::op_begin(this),
1168 OperandTraits<StoreInst>::operands(this),
1169 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001170 Op<0>() = val;
1171 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001172 setVolatile(isVolatile);
1173 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001174 AssertOK();
1175}
1176
Misha Brukmanb1c93172005-04-21 23:48:37 +00001177StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001178 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001179 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001180 OperandTraits<StoreInst>::op_begin(this),
1181 OperandTraits<StoreInst>::operands(this),
1182 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001183 Op<0>() = val;
1184 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001185 setVolatile(isVolatile);
1186 setAlignment(Align);
1187 AssertOK();
1188}
1189
1190StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001191 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001192 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001193 OperandTraits<StoreInst>::op_begin(this),
1194 OperandTraits<StoreInst>::operands(this),
1195 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001196 Op<0>() = val;
1197 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001198 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001199 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001200 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001201}
1202
Christopher Lamb84485702007-04-22 19:24:39 +00001203void StoreInst::setAlignment(unsigned Align) {
1204 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001205 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1206 ((Log2_32(Align)+1) << 1));
Christopher Lamb84485702007-04-22 19:24:39 +00001207}
1208
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001209//===----------------------------------------------------------------------===//
1210// GetElementPtrInst Implementation
1211//===----------------------------------------------------------------------===//
1212
Christopher Lambedf07882007-12-17 01:12:55 +00001213static unsigned retrieveAddrSpace(const Value *Val) {
1214 return cast<PointerType>(Val->getType())->getAddressSpace();
1215}
1216
Gabor Greif21ba1842008-06-06 20:28:12 +00001217void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001218 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001219 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1220 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001221 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001222
Chris Lattner79807c3d2007-01-31 19:47:18 +00001223 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001224 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001225
1226 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001227}
1228
Daniel Dunbar4975db62009-07-25 04:41:11 +00001229void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001230 assert(NumOperands == 2 && "NumOperands not initialized?");
1231 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001232 OL[0] = Ptr;
1233 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001234
1235 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001236}
1237
Gabor Greiff6caff662008-05-10 08:32:32 +00001238GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001239 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001240 OperandTraits<GetElementPtrInst>::op_end(this)
1241 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001242 GEPI.getNumOperands()) {
1243 Use *OL = OperandList;
1244 Use *GEPIOL = GEPI.OperandList;
1245 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001246 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001247 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001248}
1249
Chris Lattner82981202005-05-03 05:43:30 +00001250GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001251 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001252 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001253 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001254 GetElementPtr,
1255 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1256 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001257 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001258}
1259
1260GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001261 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001262 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001263 checkType(getIndexedType(Ptr->getType(),Idx)),
1264 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001265 GetElementPtr,
1266 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1267 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001268 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001269}
1270
Chris Lattner6090a422009-03-09 04:46:40 +00001271/// getIndexedType - Returns the type of the element that would be accessed with
1272/// a gep instruction with the specified parameters.
1273///
1274/// The Idxs pointer should point to a continuous piece of memory containing the
1275/// indices, either as Value* or uint64_t.
1276///
1277/// A null type is returned if the indices are invalid for the specified
1278/// pointer type.
1279///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001280template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001281static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1282 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001283 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1284 if (!PTy) return 0; // Type isn't a pointer type!
1285 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001286
Chris Lattner6090a422009-03-09 04:46:40 +00001287 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001288 if (NumIdx == 0)
1289 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001290
1291 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001292 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1293 // that contain opaque types) under the assumption that it will be resolved to
1294 // a sane type later.
1295 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001296 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001297
Dan Gohman1ecaf452008-05-31 00:58:22 +00001298 unsigned CurIdx = 1;
1299 for (; CurIdx != NumIdx; ++CurIdx) {
1300 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001301 if (!CT || CT->isPointerTy()) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001302 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001303 if (!CT->indexValid(Index)) return 0;
1304 Agg = CT->getTypeAtIndex(Index);
1305
1306 // If the new type forwards to another type, then it is in the middle
1307 // of being refined to another type (and hence, may have dropped all
1308 // references to what it was using before). So, use the new forwarded
1309 // type.
1310 if (const Type *Ty = Agg->getForwardedType())
1311 Agg = Ty;
1312 }
1313 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001314}
1315
Matthijs Kooijman04468622008-07-29 08:46:11 +00001316const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1317 Value* const *Idxs,
1318 unsigned NumIdx) {
1319 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1320}
1321
1322const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1323 uint64_t const *Idxs,
1324 unsigned NumIdx) {
1325 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1326}
1327
Chris Lattner82981202005-05-03 05:43:30 +00001328const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1329 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1330 if (!PTy) return 0; // Type isn't a pointer type!
1331
1332 // Check the pointer index.
1333 if (!PTy->indexValid(Idx)) return 0;
1334
Chris Lattnerc2233332005-05-03 16:44:45 +00001335 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001336}
1337
Chris Lattner45f15572007-04-14 00:12:57 +00001338
1339/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1340/// zeros. If so, the result pointer and the first operand have the same
1341/// value, just potentially different types.
1342bool GetElementPtrInst::hasAllZeroIndices() const {
1343 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1344 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1345 if (!CI->isZero()) return false;
1346 } else {
1347 return false;
1348 }
1349 }
1350 return true;
1351}
1352
Chris Lattner27058292007-04-27 20:35:56 +00001353/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1354/// constant integers. If so, the result pointer and the first operand have
1355/// a constant offset between them.
1356bool GetElementPtrInst::hasAllConstantIndices() const {
1357 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1358 if (!isa<ConstantInt>(getOperand(i)))
1359 return false;
1360 }
1361 return true;
1362}
1363
Dan Gohman1b849082009-09-07 23:54:19 +00001364void GetElementPtrInst::setIsInBounds(bool B) {
1365 cast<GEPOperator>(this)->setIsInBounds(B);
1366}
Chris Lattner45f15572007-04-14 00:12:57 +00001367
Nick Lewycky28a5f252009-09-27 21:33:04 +00001368bool GetElementPtrInst::isInBounds() const {
1369 return cast<GEPOperator>(this)->isInBounds();
1370}
1371
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001372//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001373// ExtractElementInst Implementation
1374//===----------------------------------------------------------------------===//
1375
1376ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001377 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001378 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001379 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001380 ExtractElement,
1381 OperandTraits<ExtractElementInst>::op_begin(this),
1382 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001383 assert(isValidOperands(Val, Index) &&
1384 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001385 Op<0>() = Val;
1386 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001387 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001388}
1389
1390ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001391 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001392 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001393 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001394 ExtractElement,
1395 OperandTraits<ExtractElementInst>::op_begin(this),
1396 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001397 assert(isValidOperands(Val, Index) &&
1398 "Invalid extractelement instruction operands!");
1399
Gabor Greif2d3024d2008-05-26 21:33:52 +00001400 Op<0>() = Val;
1401 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001402 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001403}
1404
Chris Lattner65511ff2006-10-05 06:24:58 +00001405
Chris Lattner54865b32006-04-08 04:05:48 +00001406bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001407 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001408 return false;
1409 return true;
1410}
1411
1412
Robert Bocchino23004482006-01-10 19:05:34 +00001413//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001414// InsertElementInst Implementation
1415//===----------------------------------------------------------------------===//
1416
Chris Lattner54865b32006-04-08 04:05:48 +00001417InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001418 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001419 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001420 : Instruction(Vec->getType(), InsertElement,
1421 OperandTraits<InsertElementInst>::op_begin(this),
1422 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001423 assert(isValidOperands(Vec, Elt, Index) &&
1424 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001425 Op<0>() = Vec;
1426 Op<1>() = Elt;
1427 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001428 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001429}
1430
Chris Lattner54865b32006-04-08 04:05:48 +00001431InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001432 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001433 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001434 : Instruction(Vec->getType(), InsertElement,
1435 OperandTraits<InsertElementInst>::op_begin(this),
1436 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001437 assert(isValidOperands(Vec, Elt, Index) &&
1438 "Invalid insertelement instruction operands!");
1439
Gabor Greif2d3024d2008-05-26 21:33:52 +00001440 Op<0>() = Vec;
1441 Op<1>() = Elt;
1442 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001443 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001444}
1445
Chris Lattner54865b32006-04-08 04:05:48 +00001446bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1447 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001448 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001449 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001450
Reid Spencerd84d35b2007-02-15 02:26:10 +00001451 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001452 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001453
Duncan Sands9dff9be2010-02-15 16:12:20 +00001454 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001455 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001456 return true;
1457}
1458
1459
Robert Bocchinoca27f032006-01-17 20:07:22 +00001460//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001461// ShuffleVectorInst Implementation
1462//===----------------------------------------------------------------------===//
1463
1464ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001465 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001466 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001467: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001468 cast<VectorType>(Mask->getType())->getNumElements()),
1469 ShuffleVector,
1470 OperandTraits<ShuffleVectorInst>::op_begin(this),
1471 OperandTraits<ShuffleVectorInst>::operands(this),
1472 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001473 assert(isValidOperands(V1, V2, Mask) &&
1474 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001475 Op<0>() = V1;
1476 Op<1>() = V2;
1477 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001478 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001479}
1480
1481ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001482 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001483 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001484: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1485 cast<VectorType>(Mask->getType())->getNumElements()),
1486 ShuffleVector,
1487 OperandTraits<ShuffleVectorInst>::op_begin(this),
1488 OperandTraits<ShuffleVectorInst>::operands(this),
1489 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001490 assert(isValidOperands(V1, V2, Mask) &&
1491 "Invalid shuffle vector instruction operands!");
1492
Gabor Greif2d3024d2008-05-26 21:33:52 +00001493 Op<0>() = V1;
1494 Op<1>() = V2;
1495 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001496 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001497}
1498
Mon P Wang25f01062008-11-10 04:46:22 +00001499bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001500 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001501 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001502 return false;
1503
1504 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1505 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Duncan Sands9dff9be2010-02-15 16:12:20 +00001506 !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001507 return false;
1508 return true;
1509}
1510
Chris Lattnerf724e342008-03-02 05:28:33 +00001511/// getMaskValue - Return the index from the shuffle mask for the specified
1512/// output result. This is either -1 if the element is undef or a number less
1513/// than 2*numelements.
1514int ShuffleVectorInst::getMaskValue(unsigned i) const {
1515 const Constant *Mask = cast<Constant>(getOperand(2));
1516 if (isa<UndefValue>(Mask)) return -1;
1517 if (isa<ConstantAggregateZero>(Mask)) return 0;
1518 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1519 assert(i < MaskCV->getNumOperands() && "Index out of range");
1520
1521 if (isa<UndefValue>(MaskCV->getOperand(i)))
1522 return -1;
1523 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1524}
1525
Dan Gohman12fce772008-05-15 19:50:34 +00001526//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001527// InsertValueInst Class
1528//===----------------------------------------------------------------------===//
1529
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001530void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001531 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001532 assert(NumOperands == 2 && "NumOperands not initialized?");
1533 Op<0>() = Agg;
1534 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001535
Dan Gohman1ecaf452008-05-31 00:58:22 +00001536 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001537 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001538}
1539
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001540void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001541 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001542 assert(NumOperands == 2 && "NumOperands not initialized?");
1543 Op<0>() = Agg;
1544 Op<1>() = Val;
1545
1546 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001547 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001548}
1549
1550InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001551 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001552 OperandTraits<InsertValueInst>::op_begin(this), 2),
1553 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001554 Op<0>() = IVI.getOperand(0);
1555 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001556 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001557}
1558
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001559InsertValueInst::InsertValueInst(Value *Agg,
1560 Value *Val,
1561 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001562 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001563 Instruction *InsertBefore)
1564 : Instruction(Agg->getType(), InsertValue,
1565 OperandTraits<InsertValueInst>::op_begin(this),
1566 2, InsertBefore) {
1567 init(Agg, Val, Idx, Name);
1568}
1569
1570InsertValueInst::InsertValueInst(Value *Agg,
1571 Value *Val,
1572 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001573 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001574 BasicBlock *InsertAtEnd)
1575 : Instruction(Agg->getType(), InsertValue,
1576 OperandTraits<InsertValueInst>::op_begin(this),
1577 2, InsertAtEnd) {
1578 init(Agg, Val, Idx, Name);
1579}
1580
Dan Gohman0752bff2008-05-23 00:36:11 +00001581//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001582// ExtractValueInst Class
1583//===----------------------------------------------------------------------===//
1584
Gabor Greifcbcc4952008-06-06 21:06:32 +00001585void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001586 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001587 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001588
Dan Gohman1ecaf452008-05-31 00:58:22 +00001589 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001590 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001591}
1592
Daniel Dunbar4975db62009-07-25 04:41:11 +00001593void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001594 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001595
1596 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001597 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001598}
1599
1600ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001601 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001602 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001603 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001604}
1605
Dan Gohman12fce772008-05-15 19:50:34 +00001606// getIndexedType - Returns the type of the element that would be extracted
1607// with an extractvalue instruction with the specified parameters.
1608//
1609// A null type is returned if the indices are invalid for the specified
1610// pointer type.
1611//
1612const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001613 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001614 unsigned NumIdx) {
1615 unsigned CurIdx = 0;
1616 for (; CurIdx != NumIdx; ++CurIdx) {
1617 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001618 if (!CT || CT->isPointerTy() || CT->isVectorTy()) return 0;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001619 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001620 if (!CT->indexValid(Index)) return 0;
1621 Agg = CT->getTypeAtIndex(Index);
1622
1623 // If the new type forwards to another type, then it is in the middle
1624 // of being refined to another type (and hence, may have dropped all
1625 // references to what it was using before). So, use the new forwarded
1626 // type.
1627 if (const Type *Ty = Agg->getForwardedType())
1628 Agg = Ty;
1629 }
1630 return CurIdx == NumIdx ? Agg : 0;
1631}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001632
Dan Gohman4a3125b2008-06-17 21:07:55 +00001633const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001634 unsigned Idx) {
1635 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001636}
1637
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001638//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001639// BinaryOperator Class
1640//===----------------------------------------------------------------------===//
1641
Dan Gohmana5b96452009-06-04 22:49:04 +00001642/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1643/// type is floating-point, to help provide compatibility with an older API.
1644///
1645static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1646 const Type *Ty) {
1647 // API compatibility: Adjust integer opcodes to floating-point opcodes.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001648 if (Ty->isFPOrFPVectorTy()) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001649 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1650 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1651 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1652 }
1653 return iType;
1654}
1655
Chris Lattner2195fc42007-02-24 00:55:48 +00001656BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001657 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001658 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001659 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001660 OperandTraits<BinaryOperator>::op_begin(this),
1661 OperandTraits<BinaryOperator>::operands(this),
1662 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001663 Op<0>() = S1;
1664 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001665 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001666 setName(Name);
1667}
1668
1669BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001670 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001671 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001672 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001673 OperandTraits<BinaryOperator>::op_begin(this),
1674 OperandTraits<BinaryOperator>::operands(this),
1675 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001676 Op<0>() = S1;
1677 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001678 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001679 setName(Name);
1680}
1681
1682
1683void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001684 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001685 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001686 assert(LHS->getType() == RHS->getType() &&
1687 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001688#ifndef NDEBUG
1689 switch (iType) {
1690 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001691 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001692 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001693 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001694 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001695 "Tried to create an integer operation on a non-integer type!");
1696 break;
1697 case FAdd: case FSub:
1698 case FMul:
1699 assert(getType() == LHS->getType() &&
1700 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001701 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001702 "Tried to create a floating-point operation on a "
1703 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001704 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001705 case UDiv:
1706 case SDiv:
1707 assert(getType() == LHS->getType() &&
1708 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001709 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001710 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001711 "Incorrect operand type (not integer) for S/UDIV");
1712 break;
1713 case FDiv:
1714 assert(getType() == LHS->getType() &&
1715 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001716 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001717 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001718 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001719 case URem:
1720 case SRem:
1721 assert(getType() == LHS->getType() &&
1722 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001723 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001724 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001725 "Incorrect operand type (not integer) for S/UREM");
1726 break;
1727 case FRem:
1728 assert(getType() == LHS->getType() &&
1729 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001730 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001731 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001732 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001733 case Shl:
1734 case LShr:
1735 case AShr:
1736 assert(getType() == LHS->getType() &&
1737 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001738 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001739 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001740 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001741 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001742 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001743 case And: case Or:
1744 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001745 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001746 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001747 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001748 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001749 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001750 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001751 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001752 default:
1753 break;
1754 }
1755#endif
1756}
1757
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001758BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001759 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001760 Instruction *InsertBefore) {
1761 assert(S1->getType() == S2->getType() &&
1762 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001763 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001764}
1765
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001766BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001767 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001768 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001769 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001770 InsertAtEnd->getInstList().push_back(Res);
1771 return Res;
1772}
1773
Dan Gohman5476cfd2009-08-12 16:23:25 +00001774BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001775 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001776 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001777 return new BinaryOperator(Instruction::Sub,
1778 zero, Op,
1779 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001780}
1781
Dan Gohman5476cfd2009-08-12 16:23:25 +00001782BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001783 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001784 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001785 return new BinaryOperator(Instruction::Sub,
1786 zero, Op,
1787 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001788}
1789
Dan Gohman4ab44202009-12-18 02:58:50 +00001790BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1791 Instruction *InsertBefore) {
1792 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1793 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1794}
1795
1796BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1797 BasicBlock *InsertAtEnd) {
1798 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1799 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1800}
1801
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001802BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1803 Instruction *InsertBefore) {
1804 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1805 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1806}
1807
1808BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1809 BasicBlock *InsertAtEnd) {
1810 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1811 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1812}
1813
Dan Gohman5476cfd2009-08-12 16:23:25 +00001814BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001815 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001816 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001817 return new BinaryOperator(Instruction::FSub,
1818 zero, Op,
1819 Op->getType(), Name, InsertBefore);
1820}
1821
Dan Gohman5476cfd2009-08-12 16:23:25 +00001822BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001823 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001824 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001825 return new BinaryOperator(Instruction::FSub,
1826 zero, Op,
1827 Op->getType(), Name, InsertAtEnd);
1828}
1829
Dan Gohman5476cfd2009-08-12 16:23:25 +00001830BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001831 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001832 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001833 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001834 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001835 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001836 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001837 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001838 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001839 }
1840
1841 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001842 Op->getType(), Name, InsertBefore);
1843}
1844
Dan Gohman5476cfd2009-08-12 16:23:25 +00001845BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001846 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001847 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001848 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001849 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001850 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001851 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001852 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001853 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001854 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001855 }
1856
1857 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001858 Op->getType(), Name, InsertAtEnd);
1859}
1860
1861
1862// isConstantAllOnes - Helper function for several functions below
1863static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001864 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1865 return CI->isAllOnesValue();
1866 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1867 return CV->isAllOnesValue();
1868 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001869}
1870
Owen Andersonbb2501b2009-07-13 22:18:28 +00001871bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001872 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1873 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001874 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1875 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001876 return false;
1877}
1878
Owen Andersonbb2501b2009-07-13 22:18:28 +00001879bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001880 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1881 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001882 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001883 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001884 return false;
1885}
1886
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001887bool BinaryOperator::isNot(const Value *V) {
1888 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1889 return (Bop->getOpcode() == Instruction::Xor &&
1890 (isConstantAllOnes(Bop->getOperand(1)) ||
1891 isConstantAllOnes(Bop->getOperand(0))));
1892 return false;
1893}
1894
Chris Lattner2c7d1772005-04-24 07:28:37 +00001895Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001896 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001897}
1898
Chris Lattner2c7d1772005-04-24 07:28:37 +00001899const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1900 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001901}
1902
Dan Gohmana5b96452009-06-04 22:49:04 +00001903Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001904 return cast<BinaryOperator>(BinOp)->getOperand(1);
1905}
1906
1907const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1908 return getFNegArgument(const_cast<Value*>(BinOp));
1909}
1910
Chris Lattner2c7d1772005-04-24 07:28:37 +00001911Value *BinaryOperator::getNotArgument(Value *BinOp) {
1912 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1913 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1914 Value *Op0 = BO->getOperand(0);
1915 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001916 if (isConstantAllOnes(Op0)) return Op1;
1917
1918 assert(isConstantAllOnes(Op1));
1919 return Op0;
1920}
1921
Chris Lattner2c7d1772005-04-24 07:28:37 +00001922const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1923 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001924}
1925
1926
1927// swapOperands - Exchange the two operands to this instruction. This
1928// instruction is safe to use on any binary instruction and does not
1929// modify the semantics of the instruction. If the instruction is
1930// order dependent (SetLT f.e.) the opcode is changed.
1931//
1932bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001933 if (!isCommutative())
1934 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001935 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001936 return false;
1937}
1938
Dan Gohman1b849082009-09-07 23:54:19 +00001939void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1940 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1941}
1942
1943void BinaryOperator::setHasNoSignedWrap(bool b) {
1944 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1945}
1946
1947void BinaryOperator::setIsExact(bool b) {
1948 cast<SDivOperator>(this)->setIsExact(b);
1949}
1950
Nick Lewycky28a5f252009-09-27 21:33:04 +00001951bool BinaryOperator::hasNoUnsignedWrap() const {
1952 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1953}
1954
1955bool BinaryOperator::hasNoSignedWrap() const {
1956 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1957}
1958
1959bool BinaryOperator::isExact() const {
1960 return cast<SDivOperator>(this)->isExact();
1961}
1962
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001963//===----------------------------------------------------------------------===//
1964// CastInst Class
1965//===----------------------------------------------------------------------===//
1966
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001967// Just determine if this cast only deals with integral->integral conversion.
1968bool CastInst::isIntegerCast() const {
1969 switch (getOpcode()) {
1970 default: return false;
1971 case Instruction::ZExt:
1972 case Instruction::SExt:
1973 case Instruction::Trunc:
1974 return true;
1975 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001976 return getOperand(0)->getType()->isIntegerTy() &&
1977 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001978 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001979}
1980
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001981bool CastInst::isLosslessCast() const {
1982 // Only BitCast can be lossless, exit fast if we're not BitCast
1983 if (getOpcode() != Instruction::BitCast)
1984 return false;
1985
1986 // Identity cast is always lossless
1987 const Type* SrcTy = getOperand(0)->getType();
1988 const Type* DstTy = getType();
1989 if (SrcTy == DstTy)
1990 return true;
1991
Reid Spencer8d9336d2006-12-31 05:26:44 +00001992 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001993 if (SrcTy->isPointerTy())
1994 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001995 return false; // Other types have no identity values
1996}
1997
1998/// This function determines if the CastInst does not require any bits to be
1999/// changed in order to effect the cast. Essentially, it identifies cases where
2000/// no code gen is necessary for the cast, hence the name no-op cast. For
2001/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002002/// # bitcast i32* %x to i8*
2003/// # bitcast <2 x i32> %x to <4 x i16>
2004/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002005/// @brief Determine if a cast is a no-op.
2006bool CastInst::isNoopCast(const Type *IntPtrTy) const {
2007 switch (getOpcode()) {
2008 default:
2009 assert(!"Invalid CastOp");
2010 case Instruction::Trunc:
2011 case Instruction::ZExt:
2012 case Instruction::SExt:
2013 case Instruction::FPTrunc:
2014 case Instruction::FPExt:
2015 case Instruction::UIToFP:
2016 case Instruction::SIToFP:
2017 case Instruction::FPToUI:
2018 case Instruction::FPToSI:
2019 return false; // These always modify bits
2020 case Instruction::BitCast:
2021 return true; // BitCast never modifies bits.
2022 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002023 return IntPtrTy->getScalarSizeInBits() ==
2024 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002025 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002026 return IntPtrTy->getScalarSizeInBits() ==
2027 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002028 }
2029}
2030
2031/// This function determines if a pair of casts can be eliminated and what
2032/// opcode should be used in the elimination. This assumes that there are two
2033/// instructions like this:
2034/// * %F = firstOpcode SrcTy %x to MidTy
2035/// * %S = secondOpcode MidTy %F to DstTy
2036/// The function returns a resultOpcode so these two casts can be replaced with:
2037/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2038/// If no such cast is permited, the function returns 0.
2039unsigned CastInst::isEliminableCastPair(
2040 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
2041 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
2042{
2043 // Define the 144 possibilities for these two cast instructions. The values
2044 // in this matrix determine what to do in a given situation and select the
2045 // case in the switch below. The rows correspond to firstOp, the columns
2046 // correspond to secondOp. In looking at the table below, keep in mind
2047 // the following cast properties:
2048 //
2049 // Size Compare Source Destination
2050 // Operator Src ? Size Type Sign Type Sign
2051 // -------- ------------ ------------------- ---------------------
2052 // TRUNC > Integer Any Integral Any
2053 // ZEXT < Integral Unsigned Integer Any
2054 // SEXT < Integral Signed Integer Any
2055 // FPTOUI n/a FloatPt n/a Integral Unsigned
2056 // FPTOSI n/a FloatPt n/a Integral Signed
2057 // UITOFP n/a Integral Unsigned FloatPt n/a
2058 // SITOFP n/a Integral Signed FloatPt n/a
2059 // FPTRUNC > FloatPt n/a FloatPt n/a
2060 // FPEXT < FloatPt n/a FloatPt n/a
2061 // PTRTOINT n/a Pointer n/a Integral Unsigned
2062 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002063 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002064 //
2065 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002066 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2067 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002068 // of the produced value (we no longer know the top-part is all zeros).
2069 // Further this conversion is often much more expensive for typical hardware,
2070 // and causes issues when building libgcc. We disallow fptosi+sext for the
2071 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002072 const unsigned numCastOps =
2073 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2074 static const uint8_t CastResults[numCastOps][numCastOps] = {
2075 // T F F U S F F P I B -+
2076 // R Z S P P I I T P 2 N T |
2077 // U E E 2 2 2 2 R E I T C +- secondOp
2078 // N X X U S F F N X N 2 V |
2079 // C T T I I P P C T T P T -+
2080 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2081 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2082 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002083 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2084 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002085 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2086 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2087 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2088 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2089 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2090 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2091 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2092 };
2093
2094 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2095 [secondOp-Instruction::CastOpsBegin];
2096 switch (ElimCase) {
2097 case 0:
2098 // categorically disallowed
2099 return 0;
2100 case 1:
2101 // allowed, use first cast's opcode
2102 return firstOp;
2103 case 2:
2104 // allowed, use second cast's opcode
2105 return secondOp;
2106 case 3:
2107 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002108 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002109 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002110 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002111 return firstOp;
2112 return 0;
2113 case 4:
2114 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002115 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002116 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002117 return firstOp;
2118 return 0;
2119 case 5:
2120 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002121 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002122 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002123 return secondOp;
2124 return 0;
2125 case 6:
2126 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002127 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002128 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002129 return secondOp;
2130 return 0;
2131 case 7: {
2132 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002133 if (!IntPtrTy)
2134 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002135 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2136 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002137 if (MidSize >= PtrSize)
2138 return Instruction::BitCast;
2139 return 0;
2140 }
2141 case 8: {
2142 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2143 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2144 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002145 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2146 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002147 if (SrcSize == DstSize)
2148 return Instruction::BitCast;
2149 else if (SrcSize < DstSize)
2150 return firstOp;
2151 return secondOp;
2152 }
2153 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2154 return Instruction::ZExt;
2155 case 10:
2156 // fpext followed by ftrunc is allowed if the bit size returned to is
2157 // the same as the original, in which case its just a bitcast
2158 if (SrcTy == DstTy)
2159 return Instruction::BitCast;
2160 return 0; // If the types are not the same we can't eliminate it.
2161 case 11:
2162 // bitcast followed by ptrtoint is allowed as long as the bitcast
2163 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002164 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002165 return secondOp;
2166 return 0;
2167 case 12:
2168 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002169 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002170 return firstOp;
2171 return 0;
2172 case 13: {
2173 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002174 if (!IntPtrTy)
2175 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002176 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2177 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2178 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002179 if (SrcSize <= PtrSize && SrcSize == DstSize)
2180 return Instruction::BitCast;
2181 return 0;
2182 }
2183 case 99:
2184 // cast combination can't happen (error in input). This is for all cases
2185 // where the MidTy is not the same for the two cast instructions.
2186 assert(!"Invalid Cast Combination");
2187 return 0;
2188 default:
2189 assert(!"Error in CastResults table!!!");
2190 return 0;
2191 }
2192 return 0;
2193}
2194
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002195CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002196 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002197 // Construct and return the appropriate CastInst subclass
2198 switch (op) {
2199 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2200 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2201 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2202 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2203 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2204 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2205 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2206 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2207 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2208 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2209 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2210 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2211 default:
2212 assert(!"Invalid opcode provided");
2213 }
2214 return 0;
2215}
2216
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002217CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002218 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002219 // Construct and return the appropriate CastInst subclass
2220 switch (op) {
2221 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2222 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2223 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2224 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2225 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2226 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2227 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2228 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2229 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2230 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2231 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2232 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2233 default:
2234 assert(!"Invalid opcode provided");
2235 }
2236 return 0;
2237}
2238
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002239CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002240 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002241 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002242 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002243 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2244 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002245}
2246
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002247CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002248 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002249 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002250 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002251 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2252 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002253}
2254
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002255CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002256 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002257 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002258 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002259 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2260 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002261}
2262
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002263CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002264 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002265 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002266 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002267 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2268 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002269}
2270
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002271CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002272 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002273 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002274 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002275 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2276 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002277}
2278
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002279CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002280 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002281 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002282 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002283 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2284 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002285}
2286
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002287CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002288 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002289 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002290 assert(S->getType()->isPointerTy() && "Invalid cast");
2291 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002292 "Invalid cast");
2293
Duncan Sands9dff9be2010-02-15 16:12:20 +00002294 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002295 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2296 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002297}
2298
2299/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002300CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002301 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002302 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002303 assert(S->getType()->isPointerTy() && "Invalid cast");
2304 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002305 "Invalid cast");
2306
Duncan Sands9dff9be2010-02-15 16:12:20 +00002307 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002308 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2309 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002310}
2311
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002312CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002313 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002314 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002315 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002316 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002317 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2318 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002319 Instruction::CastOps opcode =
2320 (SrcBits == DstBits ? Instruction::BitCast :
2321 (SrcBits > DstBits ? Instruction::Trunc :
2322 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002323 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002324}
2325
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002326CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002327 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002328 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002329 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002330 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002331 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2332 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002333 Instruction::CastOps opcode =
2334 (SrcBits == DstBits ? Instruction::BitCast :
2335 (SrcBits > DstBits ? Instruction::Trunc :
2336 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002337 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002338}
2339
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002340CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002341 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002342 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002343 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002344 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002345 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2346 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002347 Instruction::CastOps opcode =
2348 (SrcBits == DstBits ? Instruction::BitCast :
2349 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002350 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002351}
2352
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002353CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002354 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002355 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002356 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002357 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002358 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2359 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002360 Instruction::CastOps opcode =
2361 (SrcBits == DstBits ? Instruction::BitCast :
2362 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002363 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002364}
2365
Duncan Sands55e50902008-01-06 10:12:28 +00002366// Check whether it is valid to call getCastOpcode for these types.
2367// This routine must be kept in sync with getCastOpcode.
2368bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2369 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2370 return false;
2371
2372 if (SrcTy == DestTy)
2373 return true;
2374
2375 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002376 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2377 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002378
2379 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002380 if (DestTy->isIntegerTy()) { // Casting to integral
2381 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002382 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002383 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002384 return true;
2385 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002386 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002387 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002388 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002389 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002390 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002391 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2392 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002393 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002394 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002395 return true;
2396 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002397 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002398 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002399 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002400 return false;
2401 }
2402 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002403 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002404 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002405 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002406 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002407 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002408 return DestPTy->getBitWidth() == SrcBits;
2409 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002410 } else if (DestTy->isPointerTy()) { // Casting to pointer
2411 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002412 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002413 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002414 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002415 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002416 return false;
2417 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002418 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002419 return false;
2420 }
2421}
2422
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002423// Provide a way to get a "cast" where the cast opcode is inferred from the
2424// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002425// logic in the castIsValid function below. This axiom should hold:
2426// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2427// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002429// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002430Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002431CastInst::getCastOpcode(
2432 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002433 // Get the bit sizes, we'll need these
2434 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002435 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2436 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002437
Duncan Sands55e50902008-01-06 10:12:28 +00002438 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2439 "Only first class types are castable!");
2440
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002441 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002442 if (DestTy->isIntegerTy()) { // Casting to integral
2443 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444 if (DestBits < SrcBits)
2445 return Trunc; // int -> smaller int
2446 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002447 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002448 return SExt; // signed -> SEXT
2449 else
2450 return ZExt; // unsigned -> ZEXT
2451 } else {
2452 return BitCast; // Same size, No-op cast
2453 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002454 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002455 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456 return FPToSI; // FP -> sint
2457 else
2458 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002459 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002460 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002461 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002462 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463 return BitCast; // Same size, no-op cast
2464 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002465 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002466 "Casting from a value that is not first-class type");
2467 return PtrToInt; // ptr -> int
2468 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002469 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2470 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002471 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472 return SIToFP; // sint -> FP
2473 else
2474 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002475 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002476 if (DestBits < SrcBits) {
2477 return FPTrunc; // FP -> smaller FP
2478 } else if (DestBits > SrcBits) {
2479 return FPExt; // FP -> larger FP
2480 } else {
2481 return BitCast; // same size, no-op cast
2482 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002483 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002484 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002485 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002486 PTy = NULL;
2487 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002488 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002489 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002490 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002491 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2492 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002493 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002494 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002495 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002496 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002497 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002498 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002499 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002500 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002501 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002502 } else if (DestTy->isPointerTy()) {
2503 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002504 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002505 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002506 return IntToPtr; // int -> ptr
2507 } else {
2508 assert(!"Casting pointer to other than pointer or int");
2509 }
2510 } else {
2511 assert(!"Casting to type that is not first-class");
2512 }
2513
2514 // If we fall through to here we probably hit an assertion cast above
2515 // and assertions are not turned on. Anything we return is an error, so
2516 // BitCast is as good a choice as any.
2517 return BitCast;
2518}
2519
2520//===----------------------------------------------------------------------===//
2521// CastInst SubClass Constructors
2522//===----------------------------------------------------------------------===//
2523
2524/// Check that the construction parameters for a CastInst are correct. This
2525/// could be broken out into the separate constructors but it is useful to have
2526/// it in one place and to eliminate the redundant code for getting the sizes
2527/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002528bool
2529CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530
2531 // Check for type sanity on the arguments
2532 const Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002533 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2534 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002535 return false;
2536
2537 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002538 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2539 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540
2541 // Switch on the opcode provided
2542 switch (op) {
2543 default: return false; // This is an input error
2544 case Instruction::Trunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002545 return SrcTy->isIntOrIntVectorTy() &&
2546 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547 case Instruction::ZExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002548 return SrcTy->isIntOrIntVectorTy() &&
2549 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550 case Instruction::SExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002551 return SrcTy->isIntOrIntVectorTy() &&
2552 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002553 case Instruction::FPTrunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002554 return SrcTy->isFPOrFPVectorTy() &&
2555 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002556 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002557 case Instruction::FPExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002558 return SrcTy->isFPOrFPVectorTy() &&
2559 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002560 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002563 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2564 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002565 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2566 DVTy->getElementType()->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002567 SVTy->getNumElements() == DVTy->getNumElements();
2568 }
2569 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002570 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002572 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002573 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2574 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002575 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2576 DVTy->getElementType()->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002577 SVTy->getNumElements() == DVTy->getNumElements();
2578 }
2579 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002580 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002581 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002582 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002584 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002585 case Instruction::BitCast:
2586 // BitCast implies a no-op cast of type only. No bits change.
2587 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002588 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002589 return false;
2590
Duncan Sands55e50902008-01-06 10:12:28 +00002591 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002592 // these cases, the cast is okay if the source and destination bit widths
2593 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002594 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002595 }
2596}
2597
2598TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002599 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002601 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002602}
2603
2604TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002605 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002607 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002608}
2609
2610ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002611 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002613 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002614}
2615
2616ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002617 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002618) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002619 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002620}
2621SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002622 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002623) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002624 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002625}
2626
Jeff Cohencc08c832006-12-02 02:22:01 +00002627SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002628 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002629) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002630 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002631}
2632
2633FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002634 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002635) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002636 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002637}
2638
2639FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002640 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002641) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002642 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002643}
2644
2645FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002646 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002647) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002648 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002649}
2650
2651FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002652 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002653) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002654 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002655}
2656
2657UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002658 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002659) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002660 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002661}
2662
2663UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002664 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002665) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002666 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002667}
2668
2669SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002670 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002671) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002672 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002673}
2674
2675SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002676 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002677) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002678 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002679}
2680
2681FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002682 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002683) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002684 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002685}
2686
2687FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002688 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002689) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002690 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002691}
2692
2693FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002694 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002695) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002696 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002697}
2698
2699FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002700 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002701) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002702 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002703}
2704
2705PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002706 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002707) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002708 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002709}
2710
2711PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002712 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002713) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002714 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002715}
2716
2717IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002718 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002719) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002720 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002721}
2722
2723IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002724 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002725) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002726 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002727}
2728
2729BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002730 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002731) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002732 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002733}
2734
2735BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002736 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002737) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002738 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002739}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002740
2741//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002742// CmpInst Classes
2743//===----------------------------------------------------------------------===//
2744
Chris Lattneraec33da2010-01-22 06:25:37 +00002745void CmpInst::Anchor() const {}
2746
Nate Begemand2195702008-05-12 19:01:56 +00002747CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002748 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002749 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002750 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002751 OperandTraits<CmpInst>::op_begin(this),
2752 OperandTraits<CmpInst>::operands(this),
2753 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002754 Op<0>() = LHS;
2755 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002756 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002757 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002758}
Gabor Greiff6caff662008-05-10 08:32:32 +00002759
Nate Begemand2195702008-05-12 19:01:56 +00002760CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002761 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002762 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002763 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002764 OperandTraits<CmpInst>::op_begin(this),
2765 OperandTraits<CmpInst>::operands(this),
2766 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002767 Op<0>() = LHS;
2768 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002769 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002770 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002771}
2772
2773CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002774CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002775 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002776 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002777 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002778 if (InsertBefore)
2779 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2780 S1, S2, Name);
2781 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002782 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002783 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002784 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002785
2786 if (InsertBefore)
2787 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2788 S1, S2, Name);
2789 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002790 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002791 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002792}
2793
2794CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002795CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002796 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002797 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002798 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2799 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002800 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002801 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2802 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002803}
2804
2805void CmpInst::swapOperands() {
2806 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2807 IC->swapOperands();
2808 else
2809 cast<FCmpInst>(this)->swapOperands();
2810}
2811
2812bool CmpInst::isCommutative() {
2813 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2814 return IC->isCommutative();
2815 return cast<FCmpInst>(this)->isCommutative();
2816}
2817
2818bool CmpInst::isEquality() {
2819 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2820 return IC->isEquality();
2821 return cast<FCmpInst>(this)->isEquality();
2822}
2823
2824
Dan Gohman4e724382008-05-31 02:47:54 +00002825CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002826 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002827 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002828 case ICMP_EQ: return ICMP_NE;
2829 case ICMP_NE: return ICMP_EQ;
2830 case ICMP_UGT: return ICMP_ULE;
2831 case ICMP_ULT: return ICMP_UGE;
2832 case ICMP_UGE: return ICMP_ULT;
2833 case ICMP_ULE: return ICMP_UGT;
2834 case ICMP_SGT: return ICMP_SLE;
2835 case ICMP_SLT: return ICMP_SGE;
2836 case ICMP_SGE: return ICMP_SLT;
2837 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002838
Dan Gohman4e724382008-05-31 02:47:54 +00002839 case FCMP_OEQ: return FCMP_UNE;
2840 case FCMP_ONE: return FCMP_UEQ;
2841 case FCMP_OGT: return FCMP_ULE;
2842 case FCMP_OLT: return FCMP_UGE;
2843 case FCMP_OGE: return FCMP_ULT;
2844 case FCMP_OLE: return FCMP_UGT;
2845 case FCMP_UEQ: return FCMP_ONE;
2846 case FCMP_UNE: return FCMP_OEQ;
2847 case FCMP_UGT: return FCMP_OLE;
2848 case FCMP_ULT: return FCMP_OGE;
2849 case FCMP_UGE: return FCMP_OLT;
2850 case FCMP_ULE: return FCMP_OGT;
2851 case FCMP_ORD: return FCMP_UNO;
2852 case FCMP_UNO: return FCMP_ORD;
2853 case FCMP_TRUE: return FCMP_FALSE;
2854 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002855 }
2856}
2857
Reid Spencer266e42b2006-12-23 06:05:41 +00002858ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2859 switch (pred) {
2860 default: assert(! "Unknown icmp predicate!");
2861 case ICMP_EQ: case ICMP_NE:
2862 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2863 return pred;
2864 case ICMP_UGT: return ICMP_SGT;
2865 case ICMP_ULT: return ICMP_SLT;
2866 case ICMP_UGE: return ICMP_SGE;
2867 case ICMP_ULE: return ICMP_SLE;
2868 }
2869}
2870
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002871ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2872 switch (pred) {
2873 default: assert(! "Unknown icmp predicate!");
2874 case ICMP_EQ: case ICMP_NE:
2875 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2876 return pred;
2877 case ICMP_SGT: return ICMP_UGT;
2878 case ICMP_SLT: return ICMP_ULT;
2879 case ICMP_SGE: return ICMP_UGE;
2880 case ICMP_SLE: return ICMP_ULE;
2881 }
2882}
2883
Reid Spencer0286bc12007-02-28 22:00:54 +00002884/// Initialize a set of values that all satisfy the condition with C.
2885///
2886ConstantRange
2887ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2888 APInt Lower(C);
2889 APInt Upper(C);
2890 uint32_t BitWidth = C.getBitWidth();
2891 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002892 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002893 case ICmpInst::ICMP_EQ: Upper++; break;
2894 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002895 case ICmpInst::ICMP_ULT:
2896 Lower = APInt::getMinValue(BitWidth);
2897 // Check for an empty-set condition.
2898 if (Lower == Upper)
2899 return ConstantRange(BitWidth, /*isFullSet=*/false);
2900 break;
2901 case ICmpInst::ICMP_SLT:
2902 Lower = APInt::getSignedMinValue(BitWidth);
2903 // Check for an empty-set condition.
2904 if (Lower == Upper)
2905 return ConstantRange(BitWidth, /*isFullSet=*/false);
2906 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002907 case ICmpInst::ICMP_UGT:
2908 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002909 // Check for an empty-set condition.
2910 if (Lower == Upper)
2911 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002912 break;
2913 case ICmpInst::ICMP_SGT:
2914 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002915 // Check for an empty-set condition.
2916 if (Lower == Upper)
2917 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002918 break;
2919 case ICmpInst::ICMP_ULE:
2920 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002921 // Check for a full-set condition.
2922 if (Lower == Upper)
2923 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002924 break;
2925 case ICmpInst::ICMP_SLE:
2926 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002927 // Check for a full-set condition.
2928 if (Lower == Upper)
2929 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002930 break;
2931 case ICmpInst::ICMP_UGE:
2932 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002933 // Check for a full-set condition.
2934 if (Lower == Upper)
2935 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002936 break;
2937 case ICmpInst::ICMP_SGE:
2938 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002939 // Check for a full-set condition.
2940 if (Lower == Upper)
2941 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002942 break;
2943 }
2944 return ConstantRange(Lower, Upper);
2945}
2946
Dan Gohman4e724382008-05-31 02:47:54 +00002947CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002948 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002949 default: assert(!"Unknown cmp predicate!");
2950 case ICMP_EQ: case ICMP_NE:
2951 return pred;
2952 case ICMP_SGT: return ICMP_SLT;
2953 case ICMP_SLT: return ICMP_SGT;
2954 case ICMP_SGE: return ICMP_SLE;
2955 case ICMP_SLE: return ICMP_SGE;
2956 case ICMP_UGT: return ICMP_ULT;
2957 case ICMP_ULT: return ICMP_UGT;
2958 case ICMP_UGE: return ICMP_ULE;
2959 case ICMP_ULE: return ICMP_UGE;
2960
Reid Spencerd9436b62006-11-20 01:22:35 +00002961 case FCMP_FALSE: case FCMP_TRUE:
2962 case FCMP_OEQ: case FCMP_ONE:
2963 case FCMP_UEQ: case FCMP_UNE:
2964 case FCMP_ORD: case FCMP_UNO:
2965 return pred;
2966 case FCMP_OGT: return FCMP_OLT;
2967 case FCMP_OLT: return FCMP_OGT;
2968 case FCMP_OGE: return FCMP_OLE;
2969 case FCMP_OLE: return FCMP_OGE;
2970 case FCMP_UGT: return FCMP_ULT;
2971 case FCMP_ULT: return FCMP_UGT;
2972 case FCMP_UGE: return FCMP_ULE;
2973 case FCMP_ULE: return FCMP_UGE;
2974 }
2975}
2976
Reid Spencer266e42b2006-12-23 06:05:41 +00002977bool CmpInst::isUnsigned(unsigned short predicate) {
2978 switch (predicate) {
2979 default: return false;
2980 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2981 case ICmpInst::ICMP_UGE: return true;
2982 }
2983}
2984
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002985bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002986 switch (predicate) {
2987 default: return false;
2988 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2989 case ICmpInst::ICMP_SGE: return true;
2990 }
2991}
2992
2993bool CmpInst::isOrdered(unsigned short predicate) {
2994 switch (predicate) {
2995 default: return false;
2996 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2997 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2998 case FCmpInst::FCMP_ORD: return true;
2999 }
3000}
3001
3002bool CmpInst::isUnordered(unsigned short predicate) {
3003 switch (predicate) {
3004 default: return false;
3005 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3006 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3007 case FCmpInst::FCMP_UNO: return true;
3008 }
3009}
3010
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003011bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3012 switch(predicate) {
3013 default: return false;
3014 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3015 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3016 }
3017}
3018
3019bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3020 switch(predicate) {
3021 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3022 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3023 default: return false;
3024 }
3025}
3026
3027
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003028//===----------------------------------------------------------------------===//
3029// SwitchInst Implementation
3030//===----------------------------------------------------------------------===//
3031
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003032void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003033 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003034 ReservedSpace = 2+NumCases*2;
3035 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00003036 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003037
Gabor Greif2d3024d2008-05-26 21:33:52 +00003038 OperandList[0] = Value;
3039 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003040}
3041
Chris Lattner2195fc42007-02-24 00:55:48 +00003042/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3043/// switch on and a default destination. The number of additional cases can
3044/// be specified here to make memory allocation more efficient. This
3045/// constructor can also autoinsert before another instruction.
3046SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3047 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003048 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3049 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00003050 init(Value, Default, NumCases);
3051}
3052
3053/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3054/// switch on and a default destination. The number of additional cases can
3055/// be specified here to make memory allocation more efficient. This
3056/// constructor also autoinserts at the end of the specified BasicBlock.
3057SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3058 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003059 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3060 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00003061 init(Value, Default, NumCases);
3062}
3063
Misha Brukmanb1c93172005-04-21 23:48:37 +00003064SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00003065 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00003066 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003067 Use *OL = OperandList, *InOL = SI.OperandList;
3068 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003069 OL[i] = InOL[i];
3070 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003071 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003072 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003073}
3074
Gordon Henriksen14a55692007-12-10 02:14:30 +00003075SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003076 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003077}
3078
3079
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003080/// addCase - Add an entry to the switch instruction...
3081///
Chris Lattner47ac1872005-02-24 05:32:09 +00003082void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003083 unsigned OpNo = NumOperands;
3084 if (OpNo+2 > ReservedSpace)
3085 resizeOperands(0); // Get more space!
3086 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003087 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003088 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003089 OperandList[OpNo] = OnVal;
3090 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003091}
3092
3093/// removeCase - This method removes the specified successor from the switch
3094/// instruction. Note that this cannot be used to remove the default
3095/// destination (successor #0).
3096///
3097void SwitchInst::removeCase(unsigned idx) {
3098 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003099 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3100
3101 unsigned NumOps = getNumOperands();
3102 Use *OL = OperandList;
3103
3104 // Move everything after this operand down.
3105 //
3106 // FIXME: we could just swap with the end of the list, then erase. However,
3107 // client might not expect this to happen. The code as it is thrashes the
3108 // use/def lists, which is kinda lame.
3109 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3110 OL[i-2] = OL[i];
3111 OL[i-2+1] = OL[i+1];
3112 }
3113
3114 // Nuke the last value.
3115 OL[NumOps-2].set(0);
3116 OL[NumOps-2+1].set(0);
3117 NumOperands = NumOps-2;
3118}
3119
3120/// resizeOperands - resize operands - This adjusts the length of the operands
3121/// list according to the following behavior:
3122/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003123/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003124/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3125/// 3. If NumOps == NumOperands, trim the reserved space.
3126///
3127void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003128 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003129 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003130 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003131 } else if (NumOps*2 > NumOperands) {
3132 // No resize needed.
3133 if (ReservedSpace >= NumOps) return;
3134 } else if (NumOps == NumOperands) {
3135 if (ReservedSpace == NumOps) return;
3136 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003137 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003138 }
3139
3140 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003141 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003142 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003143 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003144 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003145 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003146 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003147 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003148}
3149
3150
3151BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3152 return getSuccessor(idx);
3153}
3154unsigned SwitchInst::getNumSuccessorsV() const {
3155 return getNumSuccessors();
3156}
3157void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3158 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003159}
Chris Lattnerf22be932004-10-15 23:52:53 +00003160
Chris Lattner3ed871f2009-10-27 19:13:16 +00003161//===----------------------------------------------------------------------===//
3162// SwitchInst Implementation
3163//===----------------------------------------------------------------------===//
3164
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003165void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003166 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003167 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003168 ReservedSpace = 1+NumDests;
3169 NumOperands = 1;
3170 OperandList = allocHungoffUses(ReservedSpace);
3171
3172 OperandList[0] = Address;
3173}
3174
3175
3176/// resizeOperands - resize operands - This adjusts the length of the operands
3177/// list according to the following behavior:
3178/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3179/// of operation. This grows the number of ops by 2 times.
3180/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3181/// 3. If NumOps == NumOperands, trim the reserved space.
3182///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003183void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003184 unsigned e = getNumOperands();
3185 if (NumOps == 0) {
3186 NumOps = e*2;
3187 } else if (NumOps*2 > NumOperands) {
3188 // No resize needed.
3189 if (ReservedSpace >= NumOps) return;
3190 } else if (NumOps == NumOperands) {
3191 if (ReservedSpace == NumOps) return;
3192 } else {
3193 return;
3194 }
3195
3196 ReservedSpace = NumOps;
3197 Use *NewOps = allocHungoffUses(NumOps);
3198 Use *OldOps = OperandList;
3199 for (unsigned i = 0; i != e; ++i)
3200 NewOps[i] = OldOps[i];
3201 OperandList = NewOps;
3202 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3203}
3204
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003205IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3206 Instruction *InsertBefore)
3207: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003208 0, 0, InsertBefore) {
3209 init(Address, NumCases);
3210}
3211
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003212IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3213 BasicBlock *InsertAtEnd)
3214: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003215 0, 0, InsertAtEnd) {
3216 init(Address, NumCases);
3217}
3218
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003219IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3220 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003221 allocHungoffUses(IBI.getNumOperands()),
3222 IBI.getNumOperands()) {
3223 Use *OL = OperandList, *InOL = IBI.OperandList;
3224 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3225 OL[i] = InOL[i];
3226 SubclassOptionalData = IBI.SubclassOptionalData;
3227}
3228
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003229IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003230 dropHungoffUses(OperandList);
3231}
3232
3233/// addDestination - Add a destination.
3234///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003235void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003236 unsigned OpNo = NumOperands;
3237 if (OpNo+1 > ReservedSpace)
3238 resizeOperands(0); // Get more space!
3239 // Initialize some new operands.
3240 assert(OpNo < ReservedSpace && "Growing didn't work!");
3241 NumOperands = OpNo+1;
3242 OperandList[OpNo] = DestBB;
3243}
3244
3245/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003246/// indirectbr instruction.
3247void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003248 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3249
3250 unsigned NumOps = getNumOperands();
3251 Use *OL = OperandList;
3252
3253 // Replace this value with the last one.
3254 OL[idx+1] = OL[NumOps-1];
3255
3256 // Nuke the last value.
3257 OL[NumOps-1].set(0);
3258 NumOperands = NumOps-1;
3259}
3260
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003261BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003262 return getSuccessor(idx);
3263}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003264unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003265 return getNumSuccessors();
3266}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003267void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003268 setSuccessor(idx, B);
3269}
3270
3271//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003272// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003273//===----------------------------------------------------------------------===//
3274
Chris Lattnerf22be932004-10-15 23:52:53 +00003275// Define these methods here so vtables don't get emitted into every translation
3276// unit that uses these classes.
3277
Devang Patel11cf3f42009-10-27 22:16:29 +00003278GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3279 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003280}
3281
Devang Patel11cf3f42009-10-27 22:16:29 +00003282BinaryOperator *BinaryOperator::clone_impl() const {
3283 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003284}
3285
Devang Patel11cf3f42009-10-27 22:16:29 +00003286FCmpInst* FCmpInst::clone_impl() const {
3287 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003288}
3289
Devang Patel11cf3f42009-10-27 22:16:29 +00003290ICmpInst* ICmpInst::clone_impl() const {
3291 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003292}
3293
Devang Patel11cf3f42009-10-27 22:16:29 +00003294ExtractValueInst *ExtractValueInst::clone_impl() const {
3295 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003296}
3297
Devang Patel11cf3f42009-10-27 22:16:29 +00003298InsertValueInst *InsertValueInst::clone_impl() const {
3299 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003300}
3301
Devang Patel11cf3f42009-10-27 22:16:29 +00003302AllocaInst *AllocaInst::clone_impl() const {
3303 return new AllocaInst(getAllocatedType(),
3304 (Value*)getOperand(0),
3305 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003306}
3307
Devang Patel11cf3f42009-10-27 22:16:29 +00003308LoadInst *LoadInst::clone_impl() const {
3309 return new LoadInst(getOperand(0),
3310 Twine(), isVolatile(),
3311 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003312}
3313
Devang Patel11cf3f42009-10-27 22:16:29 +00003314StoreInst *StoreInst::clone_impl() const {
3315 return new StoreInst(getOperand(0), getOperand(1),
3316 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003317}
3318
Devang Patel11cf3f42009-10-27 22:16:29 +00003319TruncInst *TruncInst::clone_impl() const {
3320 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003321}
3322
Devang Patel11cf3f42009-10-27 22:16:29 +00003323ZExtInst *ZExtInst::clone_impl() const {
3324 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003325}
3326
Devang Patel11cf3f42009-10-27 22:16:29 +00003327SExtInst *SExtInst::clone_impl() const {
3328 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003329}
3330
Devang Patel11cf3f42009-10-27 22:16:29 +00003331FPTruncInst *FPTruncInst::clone_impl() const {
3332 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003333}
3334
Devang Patel11cf3f42009-10-27 22:16:29 +00003335FPExtInst *FPExtInst::clone_impl() const {
3336 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003337}
3338
Devang Patel11cf3f42009-10-27 22:16:29 +00003339UIToFPInst *UIToFPInst::clone_impl() const {
3340 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003341}
3342
Devang Patel11cf3f42009-10-27 22:16:29 +00003343SIToFPInst *SIToFPInst::clone_impl() const {
3344 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003345}
3346
Devang Patel11cf3f42009-10-27 22:16:29 +00003347FPToUIInst *FPToUIInst::clone_impl() const {
3348 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003349}
3350
Devang Patel11cf3f42009-10-27 22:16:29 +00003351FPToSIInst *FPToSIInst::clone_impl() const {
3352 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003353}
3354
Devang Patel11cf3f42009-10-27 22:16:29 +00003355PtrToIntInst *PtrToIntInst::clone_impl() const {
3356 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003357}
3358
Devang Patel11cf3f42009-10-27 22:16:29 +00003359IntToPtrInst *IntToPtrInst::clone_impl() const {
3360 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003361}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003362
Devang Patel11cf3f42009-10-27 22:16:29 +00003363BitCastInst *BitCastInst::clone_impl() const {
3364 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003365}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003366
Devang Patel11cf3f42009-10-27 22:16:29 +00003367CallInst *CallInst::clone_impl() const {
3368 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003369}
3370
Devang Patel11cf3f42009-10-27 22:16:29 +00003371SelectInst *SelectInst::clone_impl() const {
3372 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003373}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003374
Devang Patel11cf3f42009-10-27 22:16:29 +00003375VAArgInst *VAArgInst::clone_impl() const {
3376 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003377}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003378
Devang Patel11cf3f42009-10-27 22:16:29 +00003379ExtractElementInst *ExtractElementInst::clone_impl() const {
3380 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003381}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003382
Devang Patel11cf3f42009-10-27 22:16:29 +00003383InsertElementInst *InsertElementInst::clone_impl() const {
3384 return InsertElementInst::Create(getOperand(0),
3385 getOperand(1),
3386 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003387}
3388
Devang Patel11cf3f42009-10-27 22:16:29 +00003389ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3390 return new ShuffleVectorInst(getOperand(0),
3391 getOperand(1),
3392 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003393}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003394
Devang Patel11cf3f42009-10-27 22:16:29 +00003395PHINode *PHINode::clone_impl() const {
3396 return new PHINode(*this);
3397}
3398
3399ReturnInst *ReturnInst::clone_impl() const {
3400 return new(getNumOperands()) ReturnInst(*this);
3401}
3402
3403BranchInst *BranchInst::clone_impl() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003404 unsigned Ops(getNumOperands());
Devang Patel11cf3f42009-10-27 22:16:29 +00003405 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003406}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003407
Devang Patel11cf3f42009-10-27 22:16:29 +00003408SwitchInst *SwitchInst::clone_impl() const {
3409 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003410}
3411
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003412IndirectBrInst *IndirectBrInst::clone_impl() const {
3413 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003414}
3415
3416
Devang Patel11cf3f42009-10-27 22:16:29 +00003417InvokeInst *InvokeInst::clone_impl() const {
3418 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003419}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003420
Devang Patel11cf3f42009-10-27 22:16:29 +00003421UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003422 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003423 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003424}
3425
Devang Patel11cf3f42009-10-27 22:16:29 +00003426UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003427 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003428 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003429}