blob: 97fec399e48b02355ec2ffdc815fd8234a5ee18f [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) \
34 Instruction *II(getInstruction()); \
35 return isCall() \
36 ? cast<CallInst>(II)->METHOD \
37 : cast<InvokeInst>(II)->METHOD
38
39#define CALLSITE_DELEGATE_SETTER(METHOD) \
40 Instruction *II(getInstruction()); \
41 if (isCall()) \
42 cast<CallInst>(II)->METHOD; \
43 else \
44 cast<InvokeInst>(II)->METHOD
45
Duncan Sands85fab3a2008-02-18 17:32:13 +000046CallSite::CallSite(Instruction *C) {
47 assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
Gabor Greif1b9921a2009-01-11 22:33:22 +000048 I.setPointer(C);
49 I.setInt(isa<CallInst>(C));
Duncan Sands85fab3a2008-02-18 17:32:13 +000050}
Sandeep Patel68c5f472009-09-02 08:44:58 +000051CallingConv::ID CallSite::getCallingConv() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000052 CALLSITE_DELEGATE_GETTER(getCallingConv());
Chris Lattnerf7b6d312005-05-06 20:26:43 +000053}
Sandeep Patel68c5f472009-09-02 08:44:58 +000054void CallSite::setCallingConv(CallingConv::ID CC) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000055 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
Chris Lattnerf7b6d312005-05-06 20:26:43 +000056}
Devang Patel4c758ea2008-09-25 21:00:45 +000057const AttrListPtr &CallSite::getAttributes() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000058 CALLSITE_DELEGATE_GETTER(getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000059}
Devang Patel4c758ea2008-09-25 21:00:45 +000060void CallSite::setAttributes(const AttrListPtr &PAL) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000061 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000062}
Devang Patelba3fa6c2008-09-23 23:03:40 +000063bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000064 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
Duncan Sands5208d1a2007-11-28 17:07:01 +000065}
Dale Johanneseneabc5f32008-02-22 17:49:45 +000066uint16_t CallSite::getParamAlignment(uint16_t i) const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000067 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
Dale Johanneseneabc5f32008-02-22 17:49:45 +000068}
Duncan Sands38ef3a82007-12-03 20:06:50 +000069bool CallSite::doesNotAccessMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000070 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000071}
Duncan Sands78c88722008-07-08 08:38:44 +000072void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000073 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000074}
Duncan Sands38ef3a82007-12-03 20:06:50 +000075bool CallSite::onlyReadsMemory() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000076 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
Duncan Sands38ef3a82007-12-03 20:06:50 +000077}
Duncan Sands78c88722008-07-08 08:38:44 +000078void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000079 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
Duncan Sands78c88722008-07-08 08:38:44 +000080}
81bool CallSite::doesNotReturn() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000082 CALLSITE_DELEGATE_GETTER(doesNotReturn());
Duncan Sands78c88722008-07-08 08:38:44 +000083}
84void CallSite::setDoesNotReturn(bool doesNotReturn) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000085 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
Duncan Sands78c88722008-07-08 08:38:44 +000086}
Duncan Sands3353ed02007-12-18 09:59:50 +000087bool CallSite::doesNotThrow() const {
Gabor Greif1b9921a2009-01-11 22:33:22 +000088 CALLSITE_DELEGATE_GETTER(doesNotThrow());
Duncan Sands8e4847e2007-12-16 15:51:49 +000089}
Duncan Sandsaa31b922007-12-19 21:13:37 +000090void CallSite::setDoesNotThrow(bool doesNotThrow) {
Gabor Greif1b9921a2009-01-11 22:33:22 +000091 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
Duncan Sandsaa31b922007-12-19 21:13:37 +000092}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000093
Matthijs Kooijmanb0dffd62008-06-05 08:04:58 +000094bool CallSite::hasArgument(const Value *Arg) const {
Matthijs Kooijmand901e582008-06-04 16:31:12 +000095 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
96 if (AI->get() == Arg)
97 return true;
98 return false;
99}
100
Gabor Greif1b9921a2009-01-11 22:33:22 +0000101#undef CALLSITE_DELEGATE_GETTER
102#undef CALLSITE_DELEGATE_SETTER
103
Gordon Henriksen14a55692007-12-10 02:14:30 +0000104//===----------------------------------------------------------------------===//
105// TerminatorInst Class
106//===----------------------------------------------------------------------===//
107
108// Out of line virtual method, so the vtable, etc has a home.
109TerminatorInst::~TerminatorInst() {
110}
111
Gabor Greiff6caff662008-05-10 08:32:32 +0000112//===----------------------------------------------------------------------===//
113// UnaryInstruction Class
114//===----------------------------------------------------------------------===//
115
Gordon Henriksen14a55692007-12-10 02:14:30 +0000116// Out of line virtual method, so the vtable, etc has a home.
117UnaryInstruction::~UnaryInstruction() {
118}
119
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +0000121// SelectInst Class
122//===----------------------------------------------------------------------===//
123
124/// areInvalidOperands - Return a string if the specified operands are invalid
125/// for a select operation, otherwise return null.
126const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
127 if (Op1->getType() != Op2->getType())
128 return "both values to select must have same type";
129
130 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
131 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000132 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000133 return "vector select condition element type must be i1";
134 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
135 if (ET == 0)
136 return "selected values for vector select must be vectors";
137 if (ET->getNumElements() != VT->getNumElements())
138 return "vector select requires selected vectors to have "
139 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000140 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000141 return "select condition must be i1 or <n x i1>";
142 }
143 return 0;
144}
145
146
147//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000148// PHINode Class
149//===----------------------------------------------------------------------===//
150
151PHINode::PHINode(const PHINode &PN)
152 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +0000153 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000154 ReservedSpace(PN.getNumOperands()) {
155 Use *OL = OperandList;
156 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +0000157 OL[i] = PN.getOperand(i);
158 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000159 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000160 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000161}
162
Gordon Henriksen14a55692007-12-10 02:14:30 +0000163PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000164 if (OperandList)
165 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000166}
167
168// removeIncomingValue - Remove an incoming value. This is useful if a
169// predecessor basic block is deleted.
170Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
171 unsigned NumOps = getNumOperands();
172 Use *OL = OperandList;
173 assert(Idx*2 < NumOps && "BB not in PHI node!");
174 Value *Removed = OL[Idx*2];
175
176 // Move everything after this operand down.
177 //
178 // FIXME: we could just swap with the end of the list, then erase. However,
179 // client might not expect this to happen. The code as it is thrashes the
180 // use/def lists, which is kinda lame.
181 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
182 OL[i-2] = OL[i];
183 OL[i-2+1] = OL[i+1];
184 }
185
186 // Nuke the last value.
187 OL[NumOps-2].set(0);
188 OL[NumOps-2+1].set(0);
189 NumOperands = NumOps-2;
190
191 // If the PHI node is dead, because it has zero entries, nuke it now.
192 if (NumOps == 2 && DeletePHIIfEmpty) {
193 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000194 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000195 eraseFromParent();
196 }
197 return Removed;
198}
199
200/// resizeOperands - resize operands - This adjusts the length of the operands
201/// list according to the following behavior:
202/// 1. If NumOps == 0, grow the operand list in response to a push_back style
203/// of operation. This grows the number of ops by 1.5 times.
204/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
205/// 3. If NumOps == NumOperands, trim the reserved space.
206///
207void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000208 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000209 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000210 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000211 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
212 } else if (NumOps*2 > NumOperands) {
213 // No resize needed.
214 if (ReservedSpace >= NumOps) return;
215 } else if (NumOps == NumOperands) {
216 if (ReservedSpace == NumOps) return;
217 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000218 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000219 }
220
221 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000222 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000223 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000224 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000225 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000226 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000227}
228
Nate Begemanb3923212005-08-04 23:24:19 +0000229/// hasConstantValue - If the specified PHI node always merges together the same
230/// value, return the value, otherwise return null.
231///
Dan Gohman22571482009-09-03 15:34:35 +0000232/// If the PHI has undef operands, but all the rest of the operands are
233/// some unique value, return that value if it can be proved that the
234/// value dominates the PHI. If DT is null, use a conservative check,
235/// otherwise use DT to test for dominance.
236///
237Value *PHINode::hasConstantValue(DominatorTree *DT) const {
Chris Lattner7d08da62009-09-21 22:27:34 +0000238 // If the PHI node only has one incoming value, eliminate the PHI node.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000239 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000240 if (getIncomingValue(0) != this) // not X = phi X
241 return getIncomingValue(0);
Chris Lattner7d08da62009-09-21 22:27:34 +0000242 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000243 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000244
Nate Begemanb3923212005-08-04 23:24:19 +0000245 // Otherwise if all of the incoming values are the same for the PHI, replace
246 // the PHI node with the incoming value.
247 //
248 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000249 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000250 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000251 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000252 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000253 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000254 if (InVal && getIncomingValue(i) != InVal)
255 return 0; // Not the same, bail out.
Chris Lattner7d08da62009-09-21 22:27:34 +0000256 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000257 }
Nate Begemanb3923212005-08-04 23:24:19 +0000258
259 // The only case that could cause InVal to be null is if we have a PHI node
260 // that only has entries for itself. In this case, there is no entry into the
261 // loop, so kill the PHI.
262 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000263 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000264
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000265 // If we have a PHI node like phi(X, undef, X), where X is defined by some
266 // instruction, we cannot always return X as the result of the PHI node. Only
267 // do this if X is not an instruction (thus it must dominate the PHI block),
268 // or if the client is prepared to deal with this possibility.
Chris Lattner7d08da62009-09-21 22:27:34 +0000269 if (!HasUndefInput || !isa<Instruction>(InVal))
270 return InVal;
271
272 Instruction *IV = cast<Instruction>(InVal);
273 if (DT) {
274 // We have a DominatorTree. Do a precise test.
275 if (!DT->dominates(IV, this))
276 return 0;
277 } else {
278 // If it is in the entry block, it obviously dominates everything.
279 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
280 isa<InvokeInst>(IV))
281 return 0; // Cannot guarantee that InVal dominates this PHINode.
282 }
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000283
Nate Begemanb3923212005-08-04 23:24:19 +0000284 // All of the incoming values are the same, return the value now.
285 return InVal;
286}
287
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000288
289//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290// CallInst Implementation
291//===----------------------------------------------------------------------===//
292
Gordon Henriksen14a55692007-12-10 02:14:30 +0000293CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000294}
295
Chris Lattner054ba2c2007-02-13 00:58:44 +0000296void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000297 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
298 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000299 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300
Misha Brukmanb1c93172005-04-21 23:48:37 +0000301 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000303 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304
Chris Lattner054ba2c2007-02-13 00:58:44 +0000305 assert((NumParams == FTy->getNumParams() ||
306 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000307 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000308 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000309 assert((i >= FTy->getNumParams() ||
310 FTy->getParamType(i) == Params[i]->getType()) &&
311 "Calling a function with a bad signature!");
Gabor Greif2d3024d2008-05-26 21:33:52 +0000312 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000313 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314}
315
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000316void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000317 assert(NumOperands == 3 && "NumOperands not set up?");
318 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000319 OL[0] = Func;
320 OL[1] = Actual1;
321 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000322
323 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000325 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000326
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000327 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000328 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000330 assert((0 >= FTy->getNumParams() ||
331 FTy->getParamType(0) == Actual1->getType()) &&
332 "Calling a function with a bad signature!");
333 assert((1 >= FTy->getNumParams() ||
334 FTy->getParamType(1) == Actual2->getType()) &&
335 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336}
337
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000338void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000339 assert(NumOperands == 2 && "NumOperands not set up?");
340 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000341 OL[0] = Func;
342 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000343
344 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000345 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000346 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000347
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000348 assert((FTy->getNumParams() == 1 ||
349 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000350 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000351 assert((0 == FTy->getNumParams() ||
352 FTy->getParamType(0) == Actual->getType()) &&
353 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000354}
355
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000356void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000357 assert(NumOperands == 1 && "NumOperands not set up?");
358 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +0000359 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000360
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000361 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000363 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000364
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000365 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000366}
367
Daniel Dunbar4975db62009-07-25 04:41:11 +0000368CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000369 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000370 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
371 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000372 Instruction::Call,
373 OperandTraits<CallInst>::op_end(this) - 2,
374 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000375 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000376 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000377}
378
Daniel Dunbar4975db62009-07-25 04:41:11 +0000379CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000380 BasicBlock *InsertAtEnd)
381 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
382 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000383 Instruction::Call,
384 OperandTraits<CallInst>::op_end(this) - 2,
385 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000386 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000387 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000388}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000389CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000390 Instruction *InsertBefore)
391 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
392 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000393 Instruction::Call,
394 OperandTraits<CallInst>::op_end(this) - 1,
395 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000396 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000397 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000398}
399
Daniel Dunbar4975db62009-07-25 04:41:11 +0000400CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000401 BasicBlock *InsertAtEnd)
402 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
403 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000404 Instruction::Call,
405 OperandTraits<CallInst>::op_end(this) - 1,
406 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000407 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000408 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000409}
410
Misha Brukmanb1c93172005-04-21 23:48:37 +0000411CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000412 : Instruction(CI.getType(), Instruction::Call,
413 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000414 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000415 setAttributes(CI.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000416 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000417 Use *OL = OperandList;
418 Use *InOL = CI.OperandList;
419 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000420 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000421 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000422}
423
Devang Patel4c758ea2008-09-25 21:00:45 +0000424void CallInst::addAttribute(unsigned i, Attributes attr) {
425 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000426 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000427 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000428}
429
Devang Patel4c758ea2008-09-25 21:00:45 +0000430void CallInst::removeAttribute(unsigned i, Attributes attr) {
431 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000432 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000433 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000434}
435
Devang Patelba3fa6c2008-09-23 23:03:40 +0000436bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000437 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000438 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000439 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000440 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000441 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000442}
443
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000444/// IsConstantOne - Return true only if val is constant int 1
445static bool IsConstantOne(Value *val) {
446 assert(val && "IsConstantOne does not work with NULL val");
447 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
448}
449
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000450static Instruction *createMalloc(Instruction *InsertBefore,
451 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000452 const Type *AllocTy, Value *AllocSize,
453 Value *ArraySize, Function *MallocF,
454 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000455 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000456 "createMalloc needs either InsertBefore or InsertAtEnd");
457
458 // malloc(type) becomes:
459 // bitcast (i8* malloc(typeSize)) to type*
460 // malloc(type, arraySize) becomes:
461 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000462 if (!ArraySize)
463 ArraySize = ConstantInt::get(IntPtrTy, 1);
464 else if (ArraySize->getType() != IntPtrTy) {
465 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000466 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
467 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000468 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000469 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
470 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000471 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000472
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000473 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000474 if (IsConstantOne(AllocSize)) {
475 AllocSize = ArraySize; // Operand * 1 = Operand
476 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
477 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
478 false /*ZExt*/);
479 // Malloc arg is constant product of type size and array size
480 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
481 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000482 // Multiply type size by the array size...
483 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000484 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
485 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000486 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000487 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
488 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000489 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000490 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000491
Victor Hernandez788eaab2009-09-18 19:20:02 +0000492 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000493 // Create the call to Malloc.
494 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
495 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000496 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000497 Value *MallocFunc = MallocF;
498 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000499 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000500 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000501 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000502 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000503 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000504 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000505 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000506 Result = MCall;
507 if (Result->getType() != AllocPtrType)
508 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000509 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000510 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000511 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000512 Result = MCall;
513 if (Result->getType() != AllocPtrType) {
514 InsertAtEnd->getInstList().push_back(MCall);
515 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000516 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000517 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000518 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000519 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000520 if (Function *F = dyn_cast<Function>(MallocFunc)) {
521 MCall->setCallingConv(F->getCallingConv());
522 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
523 }
Daniel Dunbarb9189002009-09-11 22:07:31 +0000524 assert(MCall->getType() != Type::getVoidTy(BB->getContext()) &&
525 "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000526
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000527 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000528}
529
530/// CreateMalloc - Generate the IR for a call to malloc:
531/// 1. Compute the malloc call's argument as the specified type's size,
532/// possibly multiplied by the array size if the array size is not
533/// constant 1.
534/// 2. Call malloc with that argument.
535/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000536Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
537 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000538 Value *AllocSize, Value *ArraySize,
539 const Twine &Name) {
540 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000541 ArraySize, NULL, Name);
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.
550/// Note: This function does not add the bitcast to the basic block, that is the
551/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000552Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
553 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000554 Value *AllocSize, Value *ArraySize,
555 Function *MallocF, const Twine &Name) {
556 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000557 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000558}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000559
Victor Hernandeze2971492009-10-24 04:23:03 +0000560static Instruction* createFree(Value* Source, Instruction *InsertBefore,
561 BasicBlock *InsertAtEnd) {
562 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
563 "createFree needs either InsertBefore or InsertAtEnd");
564 assert(isa<PointerType>(Source->getType()) &&
565 "Can not free something of nonpointer type!");
566
567 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
568 Module* M = BB->getParent()->getParent();
569
570 const Type *VoidTy = Type::getVoidTy(M->getContext());
571 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
572 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000573 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000574 CallInst* Result = NULL;
575 Value *PtrCast = Source;
576 if (InsertBefore) {
577 if (Source->getType() != IntPtrTy)
578 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
579 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
580 } else {
581 if (Source->getType() != IntPtrTy)
582 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
583 Result = CallInst::Create(FreeFunc, PtrCast, "");
584 }
585 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000586 if (Function *F = dyn_cast<Function>(FreeFunc))
587 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000588
589 return Result;
590}
591
592/// CreateFree - Generate the IR for a call to the builtin free function.
593void CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
594 createFree(Source, InsertBefore, NULL);
595}
596
597/// CreateFree - Generate the IR for a call to the builtin free function.
598/// Note: This function does not add the call to the basic block, that is the
599/// responsibility of the caller.
600Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
601 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
602 assert(FreeCall && "CreateFree did not create a CallInst");
603 return FreeCall;
604}
605
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000606//===----------------------------------------------------------------------===//
607// InvokeInst Implementation
608//===----------------------------------------------------------------------===//
609
610void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000611 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000612 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000613 Use *OL = OperandList;
614 OL[0] = Fn;
615 OL[1] = IfNormal;
616 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000617 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000618 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000619 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000620
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000621 assert(((NumArgs == FTy->getNumParams()) ||
622 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000623 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000624
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000625 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000626 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000627 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000628 "Invoking a function with a bad signature!");
629
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000630 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000631 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000632}
633
Misha Brukmanb1c93172005-04-21 23:48:37 +0000634InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000635 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000636 OperandTraits<InvokeInst>::op_end(this)
637 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000638 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000639 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000640 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000641 Use *OL = OperandList, *InOL = II.OperandList;
642 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000643 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000644 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000645}
646
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000647BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
648 return getSuccessor(idx);
649}
650unsigned InvokeInst::getNumSuccessorsV() const {
651 return getNumSuccessors();
652}
653void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
654 return setSuccessor(idx, B);
655}
656
Devang Patelba3fa6c2008-09-23 23:03:40 +0000657bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000658 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000659 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000660 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000661 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000662 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000663}
664
Devang Patel4c758ea2008-09-25 21:00:45 +0000665void InvokeInst::addAttribute(unsigned i, Attributes attr) {
666 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000667 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000668 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000669}
670
Devang Patel4c758ea2008-09-25 21:00:45 +0000671void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
672 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000673 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000674 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000675}
676
Duncan Sands5208d1a2007-11-28 17:07:01 +0000677
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000678//===----------------------------------------------------------------------===//
679// ReturnInst Implementation
680//===----------------------------------------------------------------------===//
681
Chris Lattner2195fc42007-02-24 00:55:48 +0000682ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000683 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000684 OperandTraits<ReturnInst>::op_end(this) -
685 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000686 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000687 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000688 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000689 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000690}
691
Owen Anderson55f1c092009-08-13 21:58:54 +0000692ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
693 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000694 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
695 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000696 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000697 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000698}
Owen Anderson55f1c092009-08-13 21:58:54 +0000699ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
700 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000701 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
702 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000703 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000704 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000705}
Owen Anderson55f1c092009-08-13 21:58:54 +0000706ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
707 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000708 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000709}
710
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000711unsigned ReturnInst::getNumSuccessorsV() const {
712 return getNumSuccessors();
713}
714
Devang Patelae682fb2008-02-26 17:56:20 +0000715/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
716/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000717void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000718 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000719}
720
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000722 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000723 return 0;
724}
725
Devang Patel59643e52008-02-23 00:35:18 +0000726ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000727}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000728
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000729//===----------------------------------------------------------------------===//
730// UnwindInst Implementation
731//===----------------------------------------------------------------------===//
732
Owen Anderson55f1c092009-08-13 21:58:54 +0000733UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
734 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
735 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000736}
Owen Anderson55f1c092009-08-13 21:58:54 +0000737UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
738 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
739 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000740}
741
742
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000743unsigned UnwindInst::getNumSuccessorsV() const {
744 return getNumSuccessors();
745}
746
747void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000748 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000749}
750
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000751BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000752 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000753 return 0;
754}
755
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000756//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000757// UnreachableInst Implementation
758//===----------------------------------------------------------------------===//
759
Owen Anderson55f1c092009-08-13 21:58:54 +0000760UnreachableInst::UnreachableInst(LLVMContext &Context,
761 Instruction *InsertBefore)
762 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
763 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000764}
Owen Anderson55f1c092009-08-13 21:58:54 +0000765UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
766 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
767 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000768}
769
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000770unsigned UnreachableInst::getNumSuccessorsV() const {
771 return getNumSuccessors();
772}
773
774void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000775 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000776}
777
778BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000779 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000780 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000781}
782
783//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000784// BranchInst Implementation
785//===----------------------------------------------------------------------===//
786
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000787void BranchInst::AssertOK() {
788 if (isConditional())
Owen Anderson55f1c092009-08-13 21:58:54 +0000789 assert(getCondition()->getType() == Type::getInt1Ty(getContext()) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000790 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000791}
792
Chris Lattner2195fc42007-02-24 00:55:48 +0000793BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000794 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000795 OperandTraits<BranchInst>::op_end(this) - 1,
796 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000797 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000798 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000799}
800BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
801 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000802 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000803 OperandTraits<BranchInst>::op_end(this) - 3,
804 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000805 Op<-1>() = IfTrue;
806 Op<-2>() = IfFalse;
807 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000808#ifndef NDEBUG
809 AssertOK();
810#endif
811}
812
813BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000814 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000815 OperandTraits<BranchInst>::op_end(this) - 1,
816 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000817 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000818 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000819}
820
821BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
822 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000823 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000824 OperandTraits<BranchInst>::op_end(this) - 3,
825 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000826 Op<-1>() = IfTrue;
827 Op<-2>() = IfFalse;
828 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000829#ifndef NDEBUG
830 AssertOK();
831#endif
832}
833
834
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000835BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000836 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000837 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
838 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000839 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000840 if (BI.getNumOperands() != 1) {
841 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000842 Op<-3>() = BI.Op<-3>();
843 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000844 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000845 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000846}
847
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000848
849Use* Use::getPrefix() {
850 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
851 if (PotentialPrefix.getOpaqueValue())
852 return 0;
853
854 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
855}
856
857BranchInst::~BranchInst() {
858 if (NumOperands == 1) {
859 if (Use *Prefix = OperandList->getPrefix()) {
860 Op<-1>() = 0;
861 //
862 // mark OperandList to have a special value for scrutiny
863 // by baseclass destructors and operator delete
864 OperandList = Prefix;
865 } else {
866 NumOperands = 3;
867 OperandList = op_begin();
868 }
869 }
870}
871
872
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000873BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
874 return getSuccessor(idx);
875}
876unsigned BranchInst::getNumSuccessorsV() const {
877 return getNumSuccessors();
878}
879void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
880 setSuccessor(idx, B);
881}
882
883
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000884//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000885// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000886//===----------------------------------------------------------------------===//
887
Owen Andersonb6b25302009-07-14 23:09:55 +0000888static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000889 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000890 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000891 else {
892 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000893 "Passed basic block into allocation size parameter! Use other ctor");
Owen Anderson55f1c092009-08-13 21:58:54 +0000894 assert(Amt->getType() == Type::getInt32Ty(Context) &&
Victor Hernandeza3aaf852009-10-17 01:18:07 +0000895 "Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000896 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000897 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000898}
899
Victor Hernandez8acf2952009-10-23 21:09:37 +0000900AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
901 const Twine &Name, Instruction *InsertBefore)
902 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
903 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
904 setAlignment(0);
905 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
906 setName(Name);
907}
908
909AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
910 const Twine &Name, BasicBlock *InsertAtEnd)
911 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
912 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
913 setAlignment(0);
914 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
915 setName(Name);
916}
917
918AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
919 Instruction *InsertBefore)
920 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
921 getAISize(Ty->getContext(), 0), InsertBefore) {
922 setAlignment(0);
923 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
924 setName(Name);
925}
926
927AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
928 BasicBlock *InsertAtEnd)
929 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
930 getAISize(Ty->getContext(), 0), InsertAtEnd) {
931 setAlignment(0);
932 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
933 setName(Name);
934}
935
936AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
937 const Twine &Name, Instruction *InsertBefore)
938 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000939 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000940 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000941 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000942 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000943}
944
Victor Hernandez8acf2952009-10-23 21:09:37 +0000945AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
946 const Twine &Name, BasicBlock *InsertAtEnd)
947 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000948 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000949 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000950 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000951 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000952}
953
Gordon Henriksen14a55692007-12-10 02:14:30 +0000954// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000955AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000956}
957
Victor Hernandez8acf2952009-10-23 21:09:37 +0000958void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000959 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
960 SubclassData = Log2_32(Align) + 1;
961 assert(getAlignment() == Align && "Alignment representation error!");
962}
963
Victor Hernandez8acf2952009-10-23 21:09:37 +0000964bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000965 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
966 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000967 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000968}
969
Victor Hernandez8acf2952009-10-23 21:09:37 +0000970const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000971 return getType()->getElementType();
972}
973
Chris Lattner8b291e62008-11-26 02:54:17 +0000974/// isStaticAlloca - Return true if this alloca is in the entry block of the
975/// function and is a constant size. If so, the code generator will fold it
976/// into the prolog/epilog code, so it is basically free.
977bool AllocaInst::isStaticAlloca() const {
978 // Must be constant size.
979 if (!isa<ConstantInt>(getArraySize())) return false;
980
981 // Must be in the entry block.
982 const BasicBlock *Parent = getParent();
983 return Parent == &Parent->getParent()->front();
984}
985
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000986//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000987// LoadInst Implementation
988//===----------------------------------------------------------------------===//
989
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000990void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000991 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000992 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000993}
994
Daniel Dunbar4975db62009-07-25 04:41:11 +0000995LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000996 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000997 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000998 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000999 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001000 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001001 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001002}
1003
Daniel Dunbar4975db62009-07-25 04:41:11 +00001004LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001005 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001006 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001007 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001008 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001009 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001010 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001011}
1012
Daniel Dunbar4975db62009-07-25 04:41:11 +00001013LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001014 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001015 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001016 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001017 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001018 setAlignment(0);
1019 AssertOK();
1020 setName(Name);
1021}
1022
Daniel Dunbar4975db62009-07-25 04:41:11 +00001023LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001024 unsigned Align, Instruction *InsertBef)
1025 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1026 Load, Ptr, InsertBef) {
1027 setVolatile(isVolatile);
1028 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001029 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001030 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001031}
1032
Daniel Dunbar4975db62009-07-25 04:41:11 +00001033LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001034 unsigned Align, BasicBlock *InsertAE)
1035 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1036 Load, Ptr, InsertAE) {
1037 setVolatile(isVolatile);
1038 setAlignment(Align);
1039 AssertOK();
1040 setName(Name);
1041}
1042
Daniel Dunbar4975db62009-07-25 04:41:11 +00001043LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001044 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001045 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001046 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001047 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001048 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +00001049 AssertOK();
1050 setName(Name);
1051}
1052
Daniel Dunbar27096822009-08-11 18:11:15 +00001053
1054
1055LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1056 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1057 Load, Ptr, InsertBef) {
1058 setVolatile(false);
1059 setAlignment(0);
1060 AssertOK();
1061 if (Name && Name[0]) setName(Name);
1062}
1063
1064LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1065 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1066 Load, Ptr, InsertAE) {
1067 setVolatile(false);
1068 setAlignment(0);
1069 AssertOK();
1070 if (Name && Name[0]) setName(Name);
1071}
1072
1073LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1074 Instruction *InsertBef)
1075: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1076 Load, Ptr, InsertBef) {
1077 setVolatile(isVolatile);
1078 setAlignment(0);
1079 AssertOK();
1080 if (Name && Name[0]) setName(Name);
1081}
1082
1083LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1084 BasicBlock *InsertAE)
1085 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1086 Load, Ptr, InsertAE) {
1087 setVolatile(isVolatile);
1088 setAlignment(0);
1089 AssertOK();
1090 if (Name && Name[0]) setName(Name);
1091}
1092
Christopher Lamb84485702007-04-22 19:24:39 +00001093void LoadInst::setAlignment(unsigned Align) {
1094 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1095 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1096}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001097
1098//===----------------------------------------------------------------------===//
1099// StoreInst Implementation
1100//===----------------------------------------------------------------------===//
1101
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001102void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001103 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001104 assert(isa<PointerType>(getOperand(1)->getType()) &&
1105 "Ptr must have pointer type!");
1106 assert(getOperand(0)->getType() ==
1107 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001108 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001109}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001110
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001111
1112StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001113 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001114 OperandTraits<StoreInst>::op_begin(this),
1115 OperandTraits<StoreInst>::operands(this),
1116 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001117 Op<0>() = val;
1118 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001119 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001120 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001121 AssertOK();
1122}
1123
1124StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001125 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001126 OperandTraits<StoreInst>::op_begin(this),
1127 OperandTraits<StoreInst>::operands(this),
1128 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001129 Op<0>() = val;
1130 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001131 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001132 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001133 AssertOK();
1134}
1135
Misha Brukmanb1c93172005-04-21 23:48:37 +00001136StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001137 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001138 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001139 OperandTraits<StoreInst>::op_begin(this),
1140 OperandTraits<StoreInst>::operands(this),
1141 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001142 Op<0>() = val;
1143 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001144 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001145 setAlignment(0);
1146 AssertOK();
1147}
1148
1149StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1150 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001151 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001152 OperandTraits<StoreInst>::op_begin(this),
1153 OperandTraits<StoreInst>::operands(this),
1154 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001155 Op<0>() = val;
1156 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001157 setVolatile(isVolatile);
1158 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001159 AssertOK();
1160}
1161
Misha Brukmanb1c93172005-04-21 23:48:37 +00001162StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001163 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001164 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001165 OperandTraits<StoreInst>::op_begin(this),
1166 OperandTraits<StoreInst>::operands(this),
1167 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001168 Op<0>() = val;
1169 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001170 setVolatile(isVolatile);
1171 setAlignment(Align);
1172 AssertOK();
1173}
1174
1175StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001176 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001177 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001178 OperandTraits<StoreInst>::op_begin(this),
1179 OperandTraits<StoreInst>::operands(this),
1180 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001181 Op<0>() = val;
1182 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001183 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001184 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001185 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001186}
1187
Christopher Lamb84485702007-04-22 19:24:39 +00001188void StoreInst::setAlignment(unsigned Align) {
1189 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1190 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1191}
1192
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001193//===----------------------------------------------------------------------===//
1194// GetElementPtrInst Implementation
1195//===----------------------------------------------------------------------===//
1196
Christopher Lambedf07882007-12-17 01:12:55 +00001197static unsigned retrieveAddrSpace(const Value *Val) {
1198 return cast<PointerType>(Val->getType())->getAddressSpace();
1199}
1200
Gabor Greif21ba1842008-06-06 20:28:12 +00001201void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001202 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001203 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1204 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001205 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001206
Chris Lattner79807c3d2007-01-31 19:47:18 +00001207 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001208 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001209
1210 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001211}
1212
Daniel Dunbar4975db62009-07-25 04:41:11 +00001213void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001214 assert(NumOperands == 2 && "NumOperands not initialized?");
1215 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001216 OL[0] = Ptr;
1217 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001218
1219 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001220}
1221
Gabor Greiff6caff662008-05-10 08:32:32 +00001222GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001223 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001224 OperandTraits<GetElementPtrInst>::op_end(this)
1225 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001226 GEPI.getNumOperands()) {
1227 Use *OL = OperandList;
1228 Use *GEPIOL = GEPI.OperandList;
1229 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001230 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001231 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001232}
1233
Chris Lattner82981202005-05-03 05:43:30 +00001234GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001235 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001236 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001237 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001238 GetElementPtr,
1239 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1240 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001241 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001242}
1243
1244GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001245 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001246 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001247 checkType(getIndexedType(Ptr->getType(),Idx)),
1248 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001249 GetElementPtr,
1250 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1251 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001252 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001253}
1254
Chris Lattner6090a422009-03-09 04:46:40 +00001255/// getIndexedType - Returns the type of the element that would be accessed with
1256/// a gep instruction with the specified parameters.
1257///
1258/// The Idxs pointer should point to a continuous piece of memory containing the
1259/// indices, either as Value* or uint64_t.
1260///
1261/// A null type is returned if the indices are invalid for the specified
1262/// pointer type.
1263///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001264template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001265static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1266 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001267 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1268 if (!PTy) return 0; // Type isn't a pointer type!
1269 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001270
Chris Lattner6090a422009-03-09 04:46:40 +00001271 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001272 if (NumIdx == 0)
1273 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001274
1275 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001276 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1277 // that contain opaque types) under the assumption that it will be resolved to
1278 // a sane type later.
1279 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001280 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001281
Dan Gohman1ecaf452008-05-31 00:58:22 +00001282 unsigned CurIdx = 1;
1283 for (; CurIdx != NumIdx; ++CurIdx) {
1284 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1285 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001286 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001287 if (!CT->indexValid(Index)) return 0;
1288 Agg = CT->getTypeAtIndex(Index);
1289
1290 // If the new type forwards to another type, then it is in the middle
1291 // of being refined to another type (and hence, may have dropped all
1292 // references to what it was using before). So, use the new forwarded
1293 // type.
1294 if (const Type *Ty = Agg->getForwardedType())
1295 Agg = Ty;
1296 }
1297 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001298}
1299
Matthijs Kooijman04468622008-07-29 08:46:11 +00001300const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1301 Value* const *Idxs,
1302 unsigned NumIdx) {
1303 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1304}
1305
1306const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1307 uint64_t const *Idxs,
1308 unsigned NumIdx) {
1309 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1310}
1311
Chris Lattner82981202005-05-03 05:43:30 +00001312const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1313 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1314 if (!PTy) return 0; // Type isn't a pointer type!
1315
1316 // Check the pointer index.
1317 if (!PTy->indexValid(Idx)) return 0;
1318
Chris Lattnerc2233332005-05-03 16:44:45 +00001319 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001320}
1321
Chris Lattner45f15572007-04-14 00:12:57 +00001322
1323/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1324/// zeros. If so, the result pointer and the first operand have the same
1325/// value, just potentially different types.
1326bool GetElementPtrInst::hasAllZeroIndices() const {
1327 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1328 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1329 if (!CI->isZero()) return false;
1330 } else {
1331 return false;
1332 }
1333 }
1334 return true;
1335}
1336
Chris Lattner27058292007-04-27 20:35:56 +00001337/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1338/// constant integers. If so, the result pointer and the first operand have
1339/// a constant offset between them.
1340bool GetElementPtrInst::hasAllConstantIndices() const {
1341 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1342 if (!isa<ConstantInt>(getOperand(i)))
1343 return false;
1344 }
1345 return true;
1346}
1347
Dan Gohman1b849082009-09-07 23:54:19 +00001348void GetElementPtrInst::setIsInBounds(bool B) {
1349 cast<GEPOperator>(this)->setIsInBounds(B);
1350}
Chris Lattner45f15572007-04-14 00:12:57 +00001351
Nick Lewycky28a5f252009-09-27 21:33:04 +00001352bool GetElementPtrInst::isInBounds() const {
1353 return cast<GEPOperator>(this)->isInBounds();
1354}
1355
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001356//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001357// ExtractElementInst Implementation
1358//===----------------------------------------------------------------------===//
1359
1360ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001361 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001362 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001363 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001364 ExtractElement,
1365 OperandTraits<ExtractElementInst>::op_begin(this),
1366 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001367 assert(isValidOperands(Val, Index) &&
1368 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001369 Op<0>() = Val;
1370 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001371 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001372}
1373
1374ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001375 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001376 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001377 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001378 ExtractElement,
1379 OperandTraits<ExtractElementInst>::op_begin(this),
1380 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001381 assert(isValidOperands(Val, Index) &&
1382 "Invalid extractelement instruction operands!");
1383
Gabor Greif2d3024d2008-05-26 21:33:52 +00001384 Op<0>() = Val;
1385 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001386 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001387}
1388
Chris Lattner65511ff2006-10-05 06:24:58 +00001389
Chris Lattner54865b32006-04-08 04:05:48 +00001390bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Owen Anderson55f1c092009-08-13 21:58:54 +00001391 if (!isa<VectorType>(Val->getType()) ||
1392 Index->getType() != Type::getInt32Ty(Val->getContext()))
Chris Lattner54865b32006-04-08 04:05:48 +00001393 return false;
1394 return true;
1395}
1396
1397
Robert Bocchino23004482006-01-10 19:05:34 +00001398//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001399// InsertElementInst Implementation
1400//===----------------------------------------------------------------------===//
1401
Chris Lattner54865b32006-04-08 04:05:48 +00001402InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001403 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001404 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001405 : Instruction(Vec->getType(), InsertElement,
1406 OperandTraits<InsertElementInst>::op_begin(this),
1407 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001408 assert(isValidOperands(Vec, Elt, Index) &&
1409 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001410 Op<0>() = Vec;
1411 Op<1>() = Elt;
1412 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001413 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001414}
1415
Chris Lattner54865b32006-04-08 04:05:48 +00001416InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001417 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001418 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001419 : Instruction(Vec->getType(), InsertElement,
1420 OperandTraits<InsertElementInst>::op_begin(this),
1421 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001422 assert(isValidOperands(Vec, Elt, Index) &&
1423 "Invalid insertelement instruction operands!");
1424
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 +00001431bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1432 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001433 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001434 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001435
Reid Spencerd84d35b2007-02-15 02:26:10 +00001436 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001437 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001438
Owen Anderson55f1c092009-08-13 21:58:54 +00001439 if (Index->getType() != Type::getInt32Ty(Vec->getContext()))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001440 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001441 return true;
1442}
1443
1444
Robert Bocchinoca27f032006-01-17 20:07:22 +00001445//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001446// ShuffleVectorInst Implementation
1447//===----------------------------------------------------------------------===//
1448
1449ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001450 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001451 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001452: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001453 cast<VectorType>(Mask->getType())->getNumElements()),
1454 ShuffleVector,
1455 OperandTraits<ShuffleVectorInst>::op_begin(this),
1456 OperandTraits<ShuffleVectorInst>::operands(this),
1457 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001458 assert(isValidOperands(V1, V2, Mask) &&
1459 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001460 Op<0>() = V1;
1461 Op<1>() = V2;
1462 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001463 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001464}
1465
1466ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001467 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001468 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001469: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1470 cast<VectorType>(Mask->getType())->getNumElements()),
1471 ShuffleVector,
1472 OperandTraits<ShuffleVectorInst>::op_begin(this),
1473 OperandTraits<ShuffleVectorInst>::operands(this),
1474 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001475 assert(isValidOperands(V1, V2, Mask) &&
1476 "Invalid shuffle vector instruction operands!");
1477
Gabor Greif2d3024d2008-05-26 21:33:52 +00001478 Op<0>() = V1;
1479 Op<1>() = V2;
1480 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001481 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001482}
1483
Mon P Wang25f01062008-11-10 04:46:22 +00001484bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001485 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001486 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001487 return false;
1488
1489 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1490 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Owen Anderson55f1c092009-08-13 21:58:54 +00001491 MaskTy->getElementType() != Type::getInt32Ty(V1->getContext()))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001492 return false;
1493 return true;
1494}
1495
Chris Lattnerf724e342008-03-02 05:28:33 +00001496/// getMaskValue - Return the index from the shuffle mask for the specified
1497/// output result. This is either -1 if the element is undef or a number less
1498/// than 2*numelements.
1499int ShuffleVectorInst::getMaskValue(unsigned i) const {
1500 const Constant *Mask = cast<Constant>(getOperand(2));
1501 if (isa<UndefValue>(Mask)) return -1;
1502 if (isa<ConstantAggregateZero>(Mask)) return 0;
1503 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1504 assert(i < MaskCV->getNumOperands() && "Index out of range");
1505
1506 if (isa<UndefValue>(MaskCV->getOperand(i)))
1507 return -1;
1508 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1509}
1510
Dan Gohman12fce772008-05-15 19:50:34 +00001511//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001512// InsertValueInst Class
1513//===----------------------------------------------------------------------===//
1514
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001515void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001516 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001517 assert(NumOperands == 2 && "NumOperands not initialized?");
1518 Op<0>() = Agg;
1519 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001520
Dan Gohman1ecaf452008-05-31 00:58:22 +00001521 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001522 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001523}
1524
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001525void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001526 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001527 assert(NumOperands == 2 && "NumOperands not initialized?");
1528 Op<0>() = Agg;
1529 Op<1>() = Val;
1530
1531 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001532 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001533}
1534
1535InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001536 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001537 OperandTraits<InsertValueInst>::op_begin(this), 2),
1538 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001539 Op<0>() = IVI.getOperand(0);
1540 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001541 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001542}
1543
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001544InsertValueInst::InsertValueInst(Value *Agg,
1545 Value *Val,
1546 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001547 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001548 Instruction *InsertBefore)
1549 : Instruction(Agg->getType(), InsertValue,
1550 OperandTraits<InsertValueInst>::op_begin(this),
1551 2, InsertBefore) {
1552 init(Agg, Val, Idx, Name);
1553}
1554
1555InsertValueInst::InsertValueInst(Value *Agg,
1556 Value *Val,
1557 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001558 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001559 BasicBlock *InsertAtEnd)
1560 : Instruction(Agg->getType(), InsertValue,
1561 OperandTraits<InsertValueInst>::op_begin(this),
1562 2, InsertAtEnd) {
1563 init(Agg, Val, Idx, Name);
1564}
1565
Dan Gohman0752bff2008-05-23 00:36:11 +00001566//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001567// ExtractValueInst Class
1568//===----------------------------------------------------------------------===//
1569
Gabor Greifcbcc4952008-06-06 21:06:32 +00001570void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001571 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001572 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001573
Dan Gohman1ecaf452008-05-31 00:58:22 +00001574 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001575 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001576}
1577
Daniel Dunbar4975db62009-07-25 04:41:11 +00001578void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001579 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001580
1581 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001582 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001583}
1584
1585ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001586 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001587 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001588 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001589}
1590
Dan Gohman12fce772008-05-15 19:50:34 +00001591// getIndexedType - Returns the type of the element that would be extracted
1592// with an extractvalue instruction with the specified parameters.
1593//
1594// A null type is returned if the indices are invalid for the specified
1595// pointer type.
1596//
1597const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001598 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001599 unsigned NumIdx) {
1600 unsigned CurIdx = 0;
1601 for (; CurIdx != NumIdx; ++CurIdx) {
1602 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001603 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1604 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001605 if (!CT->indexValid(Index)) return 0;
1606 Agg = CT->getTypeAtIndex(Index);
1607
1608 // If the new type forwards to another type, then it is in the middle
1609 // of being refined to another type (and hence, may have dropped all
1610 // references to what it was using before). So, use the new forwarded
1611 // type.
1612 if (const Type *Ty = Agg->getForwardedType())
1613 Agg = Ty;
1614 }
1615 return CurIdx == NumIdx ? Agg : 0;
1616}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001617
Dan Gohman4a3125b2008-06-17 21:07:55 +00001618const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001619 unsigned Idx) {
1620 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001621}
1622
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001623//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001624// BinaryOperator Class
1625//===----------------------------------------------------------------------===//
1626
Dan Gohmana5b96452009-06-04 22:49:04 +00001627/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1628/// type is floating-point, to help provide compatibility with an older API.
1629///
1630static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1631 const Type *Ty) {
1632 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1633 if (Ty->isFPOrFPVector()) {
1634 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1635 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1636 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1637 }
1638 return iType;
1639}
1640
Chris Lattner2195fc42007-02-24 00:55:48 +00001641BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001642 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001643 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001644 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001645 OperandTraits<BinaryOperator>::op_begin(this),
1646 OperandTraits<BinaryOperator>::operands(this),
1647 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001648 Op<0>() = S1;
1649 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001650 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001651 setName(Name);
1652}
1653
1654BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001655 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001656 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001657 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001658 OperandTraits<BinaryOperator>::op_begin(this),
1659 OperandTraits<BinaryOperator>::operands(this),
1660 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001661 Op<0>() = S1;
1662 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001663 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001664 setName(Name);
1665}
1666
1667
1668void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001669 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001670 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001671 assert(LHS->getType() == RHS->getType() &&
1672 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001673#ifndef NDEBUG
1674 switch (iType) {
1675 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001676 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001677 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001678 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001679 assert(getType()->isIntOrIntVector() &&
1680 "Tried to create an integer operation on a non-integer type!");
1681 break;
1682 case FAdd: case FSub:
1683 case FMul:
1684 assert(getType() == LHS->getType() &&
1685 "Arithmetic operation should return same type as operands!");
1686 assert(getType()->isFPOrFPVector() &&
1687 "Tried to create a floating-point operation on a "
1688 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001689 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001690 case UDiv:
1691 case SDiv:
1692 assert(getType() == LHS->getType() &&
1693 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001694 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1695 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001696 "Incorrect operand type (not integer) for S/UDIV");
1697 break;
1698 case FDiv:
1699 assert(getType() == LHS->getType() &&
1700 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001701 assert(getType()->isFPOrFPVector() &&
1702 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001703 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001704 case URem:
1705 case SRem:
1706 assert(getType() == LHS->getType() &&
1707 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001708 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1709 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001710 "Incorrect operand type (not integer) for S/UREM");
1711 break;
1712 case FRem:
1713 assert(getType() == LHS->getType() &&
1714 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001715 assert(getType()->isFPOrFPVector() &&
1716 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001717 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001718 case Shl:
1719 case LShr:
1720 case AShr:
1721 assert(getType() == LHS->getType() &&
1722 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001723 assert((getType()->isInteger() ||
1724 (isa<VectorType>(getType()) &&
1725 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1726 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001727 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001728 case And: case Or:
1729 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001730 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001731 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001732 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001733 (isa<VectorType>(getType()) &&
1734 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001735 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001736 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001737 default:
1738 break;
1739 }
1740#endif
1741}
1742
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001743BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001744 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001745 Instruction *InsertBefore) {
1746 assert(S1->getType() == S2->getType() &&
1747 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001748 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001749}
1750
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001751BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001752 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001753 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001754 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001755 InsertAtEnd->getInstList().push_back(Res);
1756 return Res;
1757}
1758
Dan Gohman5476cfd2009-08-12 16:23:25 +00001759BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001760 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001761 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001762 return new BinaryOperator(Instruction::Sub,
1763 zero, Op,
1764 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001765}
1766
Dan Gohman5476cfd2009-08-12 16:23:25 +00001767BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001768 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001769 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001770 return new BinaryOperator(Instruction::Sub,
1771 zero, Op,
1772 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001773}
1774
Dan Gohman4ab44202009-12-18 02:58:50 +00001775BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1776 Instruction *InsertBefore) {
1777 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1778 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1779}
1780
1781BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1782 BasicBlock *InsertAtEnd) {
1783 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1784 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1785}
1786
Dan Gohman5476cfd2009-08-12 16:23:25 +00001787BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001788 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001789 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001790 return new BinaryOperator(Instruction::FSub,
1791 zero, Op,
1792 Op->getType(), Name, InsertBefore);
1793}
1794
Dan Gohman5476cfd2009-08-12 16:23:25 +00001795BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001796 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001797 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001798 return new BinaryOperator(Instruction::FSub,
1799 zero, Op,
1800 Op->getType(), Name, InsertAtEnd);
1801}
1802
Dan Gohman5476cfd2009-08-12 16:23:25 +00001803BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001804 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001805 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001806 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001807 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001808 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001809 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001810 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001811 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001812 }
1813
1814 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001815 Op->getType(), Name, InsertBefore);
1816}
1817
Dan Gohman5476cfd2009-08-12 16:23:25 +00001818BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001819 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001820 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001821 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001822 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001823 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001824 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001825 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001826 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001827 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001828 }
1829
1830 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001831 Op->getType(), Name, InsertAtEnd);
1832}
1833
1834
1835// isConstantAllOnes - Helper function for several functions below
1836static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001837 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1838 return CI->isAllOnesValue();
1839 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1840 return CV->isAllOnesValue();
1841 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001842}
1843
Owen Andersonbb2501b2009-07-13 22:18:28 +00001844bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001845 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1846 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001847 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1848 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001849 return false;
1850}
1851
Owen Andersonbb2501b2009-07-13 22:18:28 +00001852bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001853 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1854 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001855 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001856 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001857 return false;
1858}
1859
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001860bool BinaryOperator::isNot(const Value *V) {
1861 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1862 return (Bop->getOpcode() == Instruction::Xor &&
1863 (isConstantAllOnes(Bop->getOperand(1)) ||
1864 isConstantAllOnes(Bop->getOperand(0))));
1865 return false;
1866}
1867
Chris Lattner2c7d1772005-04-24 07:28:37 +00001868Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001869 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001870}
1871
Chris Lattner2c7d1772005-04-24 07:28:37 +00001872const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1873 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001874}
1875
Dan Gohmana5b96452009-06-04 22:49:04 +00001876Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001877 return cast<BinaryOperator>(BinOp)->getOperand(1);
1878}
1879
1880const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1881 return getFNegArgument(const_cast<Value*>(BinOp));
1882}
1883
Chris Lattner2c7d1772005-04-24 07:28:37 +00001884Value *BinaryOperator::getNotArgument(Value *BinOp) {
1885 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1886 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1887 Value *Op0 = BO->getOperand(0);
1888 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001889 if (isConstantAllOnes(Op0)) return Op1;
1890
1891 assert(isConstantAllOnes(Op1));
1892 return Op0;
1893}
1894
Chris Lattner2c7d1772005-04-24 07:28:37 +00001895const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1896 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001897}
1898
1899
1900// swapOperands - Exchange the two operands to this instruction. This
1901// instruction is safe to use on any binary instruction and does not
1902// modify the semantics of the instruction. If the instruction is
1903// order dependent (SetLT f.e.) the opcode is changed.
1904//
1905bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001906 if (!isCommutative())
1907 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001908 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001909 return false;
1910}
1911
Dan Gohman1b849082009-09-07 23:54:19 +00001912void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1913 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1914}
1915
1916void BinaryOperator::setHasNoSignedWrap(bool b) {
1917 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1918}
1919
1920void BinaryOperator::setIsExact(bool b) {
1921 cast<SDivOperator>(this)->setIsExact(b);
1922}
1923
Nick Lewycky28a5f252009-09-27 21:33:04 +00001924bool BinaryOperator::hasNoUnsignedWrap() const {
1925 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1926}
1927
1928bool BinaryOperator::hasNoSignedWrap() const {
1929 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1930}
1931
1932bool BinaryOperator::isExact() const {
1933 return cast<SDivOperator>(this)->isExact();
1934}
1935
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001936//===----------------------------------------------------------------------===//
1937// CastInst Class
1938//===----------------------------------------------------------------------===//
1939
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001940// Just determine if this cast only deals with integral->integral conversion.
1941bool CastInst::isIntegerCast() const {
1942 switch (getOpcode()) {
1943 default: return false;
1944 case Instruction::ZExt:
1945 case Instruction::SExt:
1946 case Instruction::Trunc:
1947 return true;
1948 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001949 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001950 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001951}
1952
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001953bool CastInst::isLosslessCast() const {
1954 // Only BitCast can be lossless, exit fast if we're not BitCast
1955 if (getOpcode() != Instruction::BitCast)
1956 return false;
1957
1958 // Identity cast is always lossless
1959 const Type* SrcTy = getOperand(0)->getType();
1960 const Type* DstTy = getType();
1961 if (SrcTy == DstTy)
1962 return true;
1963
Reid Spencer8d9336d2006-12-31 05:26:44 +00001964 // Pointer to pointer is always lossless.
1965 if (isa<PointerType>(SrcTy))
1966 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001967 return false; // Other types have no identity values
1968}
1969
1970/// This function determines if the CastInst does not require any bits to be
1971/// changed in order to effect the cast. Essentially, it identifies cases where
1972/// no code gen is necessary for the cast, hence the name no-op cast. For
1973/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001974/// # bitcast i32* %x to i8*
1975/// # bitcast <2 x i32> %x to <4 x i16>
1976/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001977/// @brief Determine if a cast is a no-op.
1978bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1979 switch (getOpcode()) {
1980 default:
1981 assert(!"Invalid CastOp");
1982 case Instruction::Trunc:
1983 case Instruction::ZExt:
1984 case Instruction::SExt:
1985 case Instruction::FPTrunc:
1986 case Instruction::FPExt:
1987 case Instruction::UIToFP:
1988 case Instruction::SIToFP:
1989 case Instruction::FPToUI:
1990 case Instruction::FPToSI:
1991 return false; // These always modify bits
1992 case Instruction::BitCast:
1993 return true; // BitCast never modifies bits.
1994 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001995 return IntPtrTy->getScalarSizeInBits() ==
1996 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001997 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001998 return IntPtrTy->getScalarSizeInBits() ==
1999 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002000 }
2001}
2002
2003/// This function determines if a pair of casts can be eliminated and what
2004/// opcode should be used in the elimination. This assumes that there are two
2005/// instructions like this:
2006/// * %F = firstOpcode SrcTy %x to MidTy
2007/// * %S = secondOpcode MidTy %F to DstTy
2008/// The function returns a resultOpcode so these two casts can be replaced with:
2009/// * %Replacement = resultOpcode %SrcTy %x to DstTy
2010/// If no such cast is permited, the function returns 0.
2011unsigned CastInst::isEliminableCastPair(
2012 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
2013 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
2014{
2015 // Define the 144 possibilities for these two cast instructions. The values
2016 // in this matrix determine what to do in a given situation and select the
2017 // case in the switch below. The rows correspond to firstOp, the columns
2018 // correspond to secondOp. In looking at the table below, keep in mind
2019 // the following cast properties:
2020 //
2021 // Size Compare Source Destination
2022 // Operator Src ? Size Type Sign Type Sign
2023 // -------- ------------ ------------------- ---------------------
2024 // TRUNC > Integer Any Integral Any
2025 // ZEXT < Integral Unsigned Integer Any
2026 // SEXT < Integral Signed Integer Any
2027 // FPTOUI n/a FloatPt n/a Integral Unsigned
2028 // FPTOSI n/a FloatPt n/a Integral Signed
2029 // UITOFP n/a Integral Unsigned FloatPt n/a
2030 // SITOFP n/a Integral Signed FloatPt n/a
2031 // FPTRUNC > FloatPt n/a FloatPt n/a
2032 // FPEXT < FloatPt n/a FloatPt n/a
2033 // PTRTOINT n/a Pointer n/a Integral Unsigned
2034 // INTTOPTR n/a Integral Unsigned Pointer n/a
2035 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002036 //
2037 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002038 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2039 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002040 // of the produced value (we no longer know the top-part is all zeros).
2041 // Further this conversion is often much more expensive for typical hardware,
2042 // and causes issues when building libgcc. We disallow fptosi+sext for the
2043 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002044 const unsigned numCastOps =
2045 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2046 static const uint8_t CastResults[numCastOps][numCastOps] = {
2047 // T F F U S F F P I B -+
2048 // R Z S P P I I T P 2 N T |
2049 // U E E 2 2 2 2 R E I T C +- secondOp
2050 // N X X U S F F N X N 2 V |
2051 // C T T I I P P C T T P T -+
2052 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2053 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2054 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002055 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2056 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002057 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2058 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2059 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2060 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2061 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2062 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2063 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2064 };
2065
2066 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2067 [secondOp-Instruction::CastOpsBegin];
2068 switch (ElimCase) {
2069 case 0:
2070 // categorically disallowed
2071 return 0;
2072 case 1:
2073 // allowed, use first cast's opcode
2074 return firstOp;
2075 case 2:
2076 // allowed, use second cast's opcode
2077 return secondOp;
2078 case 3:
2079 // no-op cast in second op implies firstOp as long as the DestTy
2080 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00002081 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002082 return firstOp;
2083 return 0;
2084 case 4:
2085 // no-op cast in second op implies firstOp as long as the DestTy
2086 // is floating point
2087 if (DstTy->isFloatingPoint())
2088 return firstOp;
2089 return 0;
2090 case 5:
2091 // no-op cast in first op implies secondOp as long as the SrcTy
2092 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00002093 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002094 return secondOp;
2095 return 0;
2096 case 6:
2097 // no-op cast in first op implies secondOp as long as the SrcTy
2098 // is a floating point
2099 if (SrcTy->isFloatingPoint())
2100 return secondOp;
2101 return 0;
2102 case 7: {
2103 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002104 if (!IntPtrTy)
2105 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002106 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2107 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002108 if (MidSize >= PtrSize)
2109 return Instruction::BitCast;
2110 return 0;
2111 }
2112 case 8: {
2113 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2114 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2115 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002116 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2117 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002118 if (SrcSize == DstSize)
2119 return Instruction::BitCast;
2120 else if (SrcSize < DstSize)
2121 return firstOp;
2122 return secondOp;
2123 }
2124 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2125 return Instruction::ZExt;
2126 case 10:
2127 // fpext followed by ftrunc is allowed if the bit size returned to is
2128 // the same as the original, in which case its just a bitcast
2129 if (SrcTy == DstTy)
2130 return Instruction::BitCast;
2131 return 0; // If the types are not the same we can't eliminate it.
2132 case 11:
2133 // bitcast followed by ptrtoint is allowed as long as the bitcast
2134 // is a pointer to pointer cast.
2135 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
2136 return secondOp;
2137 return 0;
2138 case 12:
2139 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2140 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
2141 return firstOp;
2142 return 0;
2143 case 13: {
2144 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002145 if (!IntPtrTy)
2146 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002147 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2148 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2149 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002150 if (SrcSize <= PtrSize && SrcSize == DstSize)
2151 return Instruction::BitCast;
2152 return 0;
2153 }
2154 case 99:
2155 // cast combination can't happen (error in input). This is for all cases
2156 // where the MidTy is not the same for the two cast instructions.
2157 assert(!"Invalid Cast Combination");
2158 return 0;
2159 default:
2160 assert(!"Error in CastResults table!!!");
2161 return 0;
2162 }
2163 return 0;
2164}
2165
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002166CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002167 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002168 // Construct and return the appropriate CastInst subclass
2169 switch (op) {
2170 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2171 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2172 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2173 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2174 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2175 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2176 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2177 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2178 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2179 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2180 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2181 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2182 default:
2183 assert(!"Invalid opcode provided");
2184 }
2185 return 0;
2186}
2187
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002188CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002189 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002190 // Construct and return the appropriate CastInst subclass
2191 switch (op) {
2192 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2193 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2194 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2195 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2196 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2197 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2198 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2199 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2200 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2201 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2202 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2203 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2204 default:
2205 assert(!"Invalid opcode provided");
2206 }
2207 return 0;
2208}
2209
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002210CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002211 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002212 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002213 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002214 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2215 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002216}
2217
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002218CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002219 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002220 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002221 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002222 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2223 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002224}
2225
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002226CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002227 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002228 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002229 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002230 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2231 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002232}
2233
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002234CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002235 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002236 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002237 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002238 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2239 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002240}
2241
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002242CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002243 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002244 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002245 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002246 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2247 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002248}
2249
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002250CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002251 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002252 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002253 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002254 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2255 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002256}
2257
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002258CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002259 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002260 BasicBlock *InsertAtEnd) {
2261 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002262 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002263 "Invalid cast");
2264
Chris Lattner03c49532007-01-15 02:27:26 +00002265 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002266 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2267 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002268}
2269
2270/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002271CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002272 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002273 Instruction *InsertBefore) {
2274 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002275 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002276 "Invalid cast");
2277
Chris Lattner03c49532007-01-15 02:27:26 +00002278 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002279 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2280 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002281}
2282
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002283CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002284 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002285 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002286 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002287 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2288 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002289 Instruction::CastOps opcode =
2290 (SrcBits == DstBits ? Instruction::BitCast :
2291 (SrcBits > DstBits ? Instruction::Trunc :
2292 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002293 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002294}
2295
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002296CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002297 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002298 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002299 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2300 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002301 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2302 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002303 Instruction::CastOps opcode =
2304 (SrcBits == DstBits ? Instruction::BitCast :
2305 (SrcBits > DstBits ? Instruction::Trunc :
2306 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002307 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002308}
2309
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002310CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002311 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002312 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002313 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002314 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002315 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2316 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002317 Instruction::CastOps opcode =
2318 (SrcBits == DstBits ? Instruction::BitCast :
2319 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002320 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002321}
2322
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002323CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002324 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002325 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002326 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002327 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002328 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2329 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002330 Instruction::CastOps opcode =
2331 (SrcBits == DstBits ? Instruction::BitCast :
2332 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002333 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002334}
2335
Duncan Sands55e50902008-01-06 10:12:28 +00002336// Check whether it is valid to call getCastOpcode for these types.
2337// This routine must be kept in sync with getCastOpcode.
2338bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2339 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2340 return false;
2341
2342 if (SrcTy == DestTy)
2343 return true;
2344
2345 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002346 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2347 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002348
2349 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002350 if (DestTy->isInteger()) { // Casting to integral
2351 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002352 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002353 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002354 return true;
2355 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002356 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002357 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002358 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002359 return isa<PointerType>(SrcTy);
2360 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002361 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2362 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002363 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002364 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002365 return true;
2366 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002367 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002368 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002369 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002370 return false;
2371 }
2372 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002373 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002374 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002375 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002376 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002377 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002378 return DestPTy->getBitWidth() == SrcBits;
2379 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002380 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2381 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002382 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002383 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002384 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002385 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002386 return false;
2387 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002388 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002389 return false;
2390 }
2391}
2392
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393// Provide a way to get a "cast" where the cast opcode is inferred from the
2394// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002395// logic in the castIsValid function below. This axiom should hold:
2396// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2397// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002399// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002401CastInst::getCastOpcode(
2402 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002403 // Get the bit sizes, we'll need these
2404 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002405 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2406 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002407
Duncan Sands55e50902008-01-06 10:12:28 +00002408 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2409 "Only first class types are castable!");
2410
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002411 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002412 if (DestTy->isInteger()) { // Casting to integral
2413 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002414 if (DestBits < SrcBits)
2415 return Trunc; // int -> smaller int
2416 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002417 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002418 return SExt; // signed -> SEXT
2419 else
2420 return ZExt; // unsigned -> ZEXT
2421 } else {
2422 return BitCast; // Same size, No-op cast
2423 }
2424 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002425 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002426 return FPToSI; // FP -> sint
2427 else
2428 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002429 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002430 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002431 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002432 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002433 return BitCast; // Same size, no-op cast
2434 } else {
2435 assert(isa<PointerType>(SrcTy) &&
2436 "Casting from a value that is not first-class type");
2437 return PtrToInt; // ptr -> int
2438 }
2439 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002440 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002441 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002442 return SIToFP; // sint -> FP
2443 else
2444 return UIToFP; // uint -> FP
2445 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2446 if (DestBits < SrcBits) {
2447 return FPTrunc; // FP -> smaller FP
2448 } else if (DestBits > SrcBits) {
2449 return FPExt; // FP -> larger FP
2450 } else {
2451 return BitCast; // same size, no-op cast
2452 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002453 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002454 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002455 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002456 PTy = NULL;
2457 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002458 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002459 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002460 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002461 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2462 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002463 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002464 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002465 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002466 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002467 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002468 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002469 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002470 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471 }
2472 } else if (isa<PointerType>(DestTy)) {
2473 if (isa<PointerType>(SrcTy)) {
2474 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002475 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002476 return IntToPtr; // int -> ptr
2477 } else {
2478 assert(!"Casting pointer to other than pointer or int");
2479 }
2480 } else {
2481 assert(!"Casting to type that is not first-class");
2482 }
2483
2484 // If we fall through to here we probably hit an assertion cast above
2485 // and assertions are not turned on. Anything we return is an error, so
2486 // BitCast is as good a choice as any.
2487 return BitCast;
2488}
2489
2490//===----------------------------------------------------------------------===//
2491// CastInst SubClass Constructors
2492//===----------------------------------------------------------------------===//
2493
2494/// Check that the construction parameters for a CastInst are correct. This
2495/// could be broken out into the separate constructors but it is useful to have
2496/// it in one place and to eliminate the redundant code for getting the sizes
2497/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002498bool
2499CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002500
2501 // Check for type sanity on the arguments
2502 const Type *SrcTy = S->getType();
2503 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2504 return false;
2505
2506 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002507 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2508 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002509
2510 // Switch on the opcode provided
2511 switch (op) {
2512 default: return false; // This is an input error
2513 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002514 return SrcTy->isIntOrIntVector() &&
2515 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002517 return SrcTy->isIntOrIntVector() &&
2518 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002519 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002520 return SrcTy->isIntOrIntVector() &&
2521 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002523 return SrcTy->isFPOrFPVector() &&
2524 DstTy->isFPOrFPVector() &&
2525 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002526 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002527 return SrcTy->isFPOrFPVector() &&
2528 DstTy->isFPOrFPVector() &&
2529 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002531 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002532 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2533 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002534 return SVTy->getElementType()->isIntOrIntVector() &&
2535 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002536 SVTy->getNumElements() == DVTy->getNumElements();
2537 }
2538 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002539 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002542 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2543 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002544 return SVTy->getElementType()->isFPOrFPVector() &&
2545 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002546 SVTy->getNumElements() == DVTy->getNumElements();
2547 }
2548 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002549 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002551 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002552 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002553 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002554 case Instruction::BitCast:
2555 // BitCast implies a no-op cast of type only. No bits change.
2556 // However, you can't cast pointers to anything but pointers.
2557 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2558 return false;
2559
Duncan Sands55e50902008-01-06 10:12:28 +00002560 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561 // these cases, the cast is okay if the source and destination bit widths
2562 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002563 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564 }
2565}
2566
2567TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002568 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002570 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571}
2572
2573TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002574 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002575) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002576 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577}
2578
2579ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002580 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002581) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002582 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002583}
2584
2585ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002586 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002588 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002589}
2590SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002591 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002592) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002593 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594}
2595
Jeff Cohencc08c832006-12-02 02:22:01 +00002596SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002597 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002598) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002599 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600}
2601
2602FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002603 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002605 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606}
2607
2608FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002609 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002610) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002611 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612}
2613
2614FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002615 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002616) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002617 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002618}
2619
2620FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002621 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002622) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002623 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002624}
2625
2626UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002627 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002629 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002630}
2631
2632UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002633 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002634) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002635 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002636}
2637
2638SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002639 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002640) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002641 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642}
2643
2644SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002645 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002646) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002647 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648}
2649
2650FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002651 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002652) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002653 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002654}
2655
2656FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002657 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002658) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002659 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002660}
2661
2662FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002663 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002664) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002665 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002666}
2667
2668FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002669 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002670) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002671 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002672}
2673
2674PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002675 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002676) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002677 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002678}
2679
2680PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002681 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002682) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002683 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002684}
2685
2686IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002687 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002688) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002689 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002690}
2691
2692IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002693 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002694) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002695 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002696}
2697
2698BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002699 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002700) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002701 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002702}
2703
2704BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002705 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002706) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002707 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002708}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002709
2710//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002711// CmpInst Classes
2712//===----------------------------------------------------------------------===//
2713
Nate Begemand2195702008-05-12 19:01:56 +00002714CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002715 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002716 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002717 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002718 OperandTraits<CmpInst>::op_begin(this),
2719 OperandTraits<CmpInst>::operands(this),
2720 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002721 Op<0>() = LHS;
2722 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002723 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002724 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002725}
Gabor Greiff6caff662008-05-10 08:32:32 +00002726
Nate Begemand2195702008-05-12 19:01:56 +00002727CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002728 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002729 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002730 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002731 OperandTraits<CmpInst>::op_begin(this),
2732 OperandTraits<CmpInst>::operands(this),
2733 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002734 Op<0>() = LHS;
2735 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002736 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002737 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002738}
2739
2740CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002741CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002742 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002743 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002744 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002745 if (InsertBefore)
2746 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2747 S1, S2, Name);
2748 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002749 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002750 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002751 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002752
2753 if (InsertBefore)
2754 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2755 S1, S2, Name);
2756 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002757 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002758 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002759}
2760
2761CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002762CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002763 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002764 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002765 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2766 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002767 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002768 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2769 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002770}
2771
2772void CmpInst::swapOperands() {
2773 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2774 IC->swapOperands();
2775 else
2776 cast<FCmpInst>(this)->swapOperands();
2777}
2778
2779bool CmpInst::isCommutative() {
2780 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2781 return IC->isCommutative();
2782 return cast<FCmpInst>(this)->isCommutative();
2783}
2784
2785bool CmpInst::isEquality() {
2786 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2787 return IC->isEquality();
2788 return cast<FCmpInst>(this)->isEquality();
2789}
2790
2791
Dan Gohman4e724382008-05-31 02:47:54 +00002792CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002793 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002794 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002795 case ICMP_EQ: return ICMP_NE;
2796 case ICMP_NE: return ICMP_EQ;
2797 case ICMP_UGT: return ICMP_ULE;
2798 case ICMP_ULT: return ICMP_UGE;
2799 case ICMP_UGE: return ICMP_ULT;
2800 case ICMP_ULE: return ICMP_UGT;
2801 case ICMP_SGT: return ICMP_SLE;
2802 case ICMP_SLT: return ICMP_SGE;
2803 case ICMP_SGE: return ICMP_SLT;
2804 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002805
Dan Gohman4e724382008-05-31 02:47:54 +00002806 case FCMP_OEQ: return FCMP_UNE;
2807 case FCMP_ONE: return FCMP_UEQ;
2808 case FCMP_OGT: return FCMP_ULE;
2809 case FCMP_OLT: return FCMP_UGE;
2810 case FCMP_OGE: return FCMP_ULT;
2811 case FCMP_OLE: return FCMP_UGT;
2812 case FCMP_UEQ: return FCMP_ONE;
2813 case FCMP_UNE: return FCMP_OEQ;
2814 case FCMP_UGT: return FCMP_OLE;
2815 case FCMP_ULT: return FCMP_OGE;
2816 case FCMP_UGE: return FCMP_OLT;
2817 case FCMP_ULE: return FCMP_OGT;
2818 case FCMP_ORD: return FCMP_UNO;
2819 case FCMP_UNO: return FCMP_ORD;
2820 case FCMP_TRUE: return FCMP_FALSE;
2821 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002822 }
2823}
2824
Reid Spencer266e42b2006-12-23 06:05:41 +00002825ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2826 switch (pred) {
2827 default: assert(! "Unknown icmp predicate!");
2828 case ICMP_EQ: case ICMP_NE:
2829 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2830 return pred;
2831 case ICMP_UGT: return ICMP_SGT;
2832 case ICMP_ULT: return ICMP_SLT;
2833 case ICMP_UGE: return ICMP_SGE;
2834 case ICMP_ULE: return ICMP_SLE;
2835 }
2836}
2837
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002838ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2839 switch (pred) {
2840 default: assert(! "Unknown icmp predicate!");
2841 case ICMP_EQ: case ICMP_NE:
2842 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2843 return pred;
2844 case ICMP_SGT: return ICMP_UGT;
2845 case ICMP_SLT: return ICMP_ULT;
2846 case ICMP_SGE: return ICMP_UGE;
2847 case ICMP_SLE: return ICMP_ULE;
2848 }
2849}
2850
Reid Spencer0286bc12007-02-28 22:00:54 +00002851/// Initialize a set of values that all satisfy the condition with C.
2852///
2853ConstantRange
2854ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2855 APInt Lower(C);
2856 APInt Upper(C);
2857 uint32_t BitWidth = C.getBitWidth();
2858 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002859 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002860 case ICmpInst::ICMP_EQ: Upper++; break;
2861 case ICmpInst::ICMP_NE: Lower++; break;
2862 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2863 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2864 case ICmpInst::ICMP_UGT:
2865 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2866 break;
2867 case ICmpInst::ICMP_SGT:
2868 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2869 break;
2870 case ICmpInst::ICMP_ULE:
2871 Lower = APInt::getMinValue(BitWidth); Upper++;
2872 break;
2873 case ICmpInst::ICMP_SLE:
2874 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2875 break;
2876 case ICmpInst::ICMP_UGE:
2877 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2878 break;
2879 case ICmpInst::ICMP_SGE:
2880 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2881 break;
2882 }
2883 return ConstantRange(Lower, Upper);
2884}
2885
Dan Gohman4e724382008-05-31 02:47:54 +00002886CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002887 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002888 default: assert(!"Unknown cmp predicate!");
2889 case ICMP_EQ: case ICMP_NE:
2890 return pred;
2891 case ICMP_SGT: return ICMP_SLT;
2892 case ICMP_SLT: return ICMP_SGT;
2893 case ICMP_SGE: return ICMP_SLE;
2894 case ICMP_SLE: return ICMP_SGE;
2895 case ICMP_UGT: return ICMP_ULT;
2896 case ICMP_ULT: return ICMP_UGT;
2897 case ICMP_UGE: return ICMP_ULE;
2898 case ICMP_ULE: return ICMP_UGE;
2899
Reid Spencerd9436b62006-11-20 01:22:35 +00002900 case FCMP_FALSE: case FCMP_TRUE:
2901 case FCMP_OEQ: case FCMP_ONE:
2902 case FCMP_UEQ: case FCMP_UNE:
2903 case FCMP_ORD: case FCMP_UNO:
2904 return pred;
2905 case FCMP_OGT: return FCMP_OLT;
2906 case FCMP_OLT: return FCMP_OGT;
2907 case FCMP_OGE: return FCMP_OLE;
2908 case FCMP_OLE: return FCMP_OGE;
2909 case FCMP_UGT: return FCMP_ULT;
2910 case FCMP_ULT: return FCMP_UGT;
2911 case FCMP_UGE: return FCMP_ULE;
2912 case FCMP_ULE: return FCMP_UGE;
2913 }
2914}
2915
Reid Spencer266e42b2006-12-23 06:05:41 +00002916bool CmpInst::isUnsigned(unsigned short predicate) {
2917 switch (predicate) {
2918 default: return false;
2919 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2920 case ICmpInst::ICMP_UGE: return true;
2921 }
2922}
2923
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002924bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002925 switch (predicate) {
2926 default: return false;
2927 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2928 case ICmpInst::ICMP_SGE: return true;
2929 }
2930}
2931
2932bool CmpInst::isOrdered(unsigned short predicate) {
2933 switch (predicate) {
2934 default: return false;
2935 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2936 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2937 case FCmpInst::FCMP_ORD: return true;
2938 }
2939}
2940
2941bool CmpInst::isUnordered(unsigned short predicate) {
2942 switch (predicate) {
2943 default: return false;
2944 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2945 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2946 case FCmpInst::FCMP_UNO: return true;
2947 }
2948}
2949
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002950bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2951 switch(predicate) {
2952 default: return false;
2953 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2954 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2955 }
2956}
2957
2958bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2959 switch(predicate) {
2960 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2961 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2962 default: return false;
2963 }
2964}
2965
2966
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002967//===----------------------------------------------------------------------===//
2968// SwitchInst Implementation
2969//===----------------------------------------------------------------------===//
2970
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002971void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002972 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002973 ReservedSpace = 2+NumCases*2;
2974 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002975 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002976
Gabor Greif2d3024d2008-05-26 21:33:52 +00002977 OperandList[0] = Value;
2978 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002979}
2980
Chris Lattner2195fc42007-02-24 00:55:48 +00002981/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2982/// switch on and a default destination. The number of additional cases can
2983/// be specified here to make memory allocation more efficient. This
2984/// constructor can also autoinsert before another instruction.
2985SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2986 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002987 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2988 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002989 init(Value, Default, NumCases);
2990}
2991
2992/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2993/// switch on and a default destination. The number of additional cases can
2994/// be specified here to make memory allocation more efficient. This
2995/// constructor also autoinserts at the end of the specified BasicBlock.
2996SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2997 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002998 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2999 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00003000 init(Value, Default, NumCases);
3001}
3002
Misha Brukmanb1c93172005-04-21 23:48:37 +00003003SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00003004 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00003005 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003006 Use *OL = OperandList, *InOL = SI.OperandList;
3007 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003008 OL[i] = InOL[i];
3009 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003010 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003011 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003012}
3013
Gordon Henriksen14a55692007-12-10 02:14:30 +00003014SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003015 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003016}
3017
3018
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003019/// addCase - Add an entry to the switch instruction...
3020///
Chris Lattner47ac1872005-02-24 05:32:09 +00003021void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003022 unsigned OpNo = NumOperands;
3023 if (OpNo+2 > ReservedSpace)
3024 resizeOperands(0); // Get more space!
3025 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003026 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003027 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003028 OperandList[OpNo] = OnVal;
3029 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003030}
3031
3032/// removeCase - This method removes the specified successor from the switch
3033/// instruction. Note that this cannot be used to remove the default
3034/// destination (successor #0).
3035///
3036void SwitchInst::removeCase(unsigned idx) {
3037 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003038 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3039
3040 unsigned NumOps = getNumOperands();
3041 Use *OL = OperandList;
3042
3043 // Move everything after this operand down.
3044 //
3045 // FIXME: we could just swap with the end of the list, then erase. However,
3046 // client might not expect this to happen. The code as it is thrashes the
3047 // use/def lists, which is kinda lame.
3048 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3049 OL[i-2] = OL[i];
3050 OL[i-2+1] = OL[i+1];
3051 }
3052
3053 // Nuke the last value.
3054 OL[NumOps-2].set(0);
3055 OL[NumOps-2+1].set(0);
3056 NumOperands = NumOps-2;
3057}
3058
3059/// resizeOperands - resize operands - This adjusts the length of the operands
3060/// list according to the following behavior:
3061/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003062/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003063/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3064/// 3. If NumOps == NumOperands, trim the reserved space.
3065///
3066void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003067 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003068 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003069 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003070 } else if (NumOps*2 > NumOperands) {
3071 // No resize needed.
3072 if (ReservedSpace >= NumOps) return;
3073 } else if (NumOps == NumOperands) {
3074 if (ReservedSpace == NumOps) return;
3075 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003076 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003077 }
3078
3079 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003080 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003081 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003082 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003083 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003084 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003085 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003086 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003087}
3088
3089
3090BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3091 return getSuccessor(idx);
3092}
3093unsigned SwitchInst::getNumSuccessorsV() const {
3094 return getNumSuccessors();
3095}
3096void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3097 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003098}
Chris Lattnerf22be932004-10-15 23:52:53 +00003099
Chris Lattner3ed871f2009-10-27 19:13:16 +00003100//===----------------------------------------------------------------------===//
3101// SwitchInst Implementation
3102//===----------------------------------------------------------------------===//
3103
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003104void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Chris Lattner6747b4c2009-10-29 05:53:32 +00003105 assert(Address && isa<PointerType>(Address->getType()) &&
3106 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003107 ReservedSpace = 1+NumDests;
3108 NumOperands = 1;
3109 OperandList = allocHungoffUses(ReservedSpace);
3110
3111 OperandList[0] = Address;
3112}
3113
3114
3115/// resizeOperands - resize operands - This adjusts the length of the operands
3116/// list according to the following behavior:
3117/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3118/// of operation. This grows the number of ops by 2 times.
3119/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3120/// 3. If NumOps == NumOperands, trim the reserved space.
3121///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003122void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003123 unsigned e = getNumOperands();
3124 if (NumOps == 0) {
3125 NumOps = e*2;
3126 } else if (NumOps*2 > NumOperands) {
3127 // No resize needed.
3128 if (ReservedSpace >= NumOps) return;
3129 } else if (NumOps == NumOperands) {
3130 if (ReservedSpace == NumOps) return;
3131 } else {
3132 return;
3133 }
3134
3135 ReservedSpace = NumOps;
3136 Use *NewOps = allocHungoffUses(NumOps);
3137 Use *OldOps = OperandList;
3138 for (unsigned i = 0; i != e; ++i)
3139 NewOps[i] = OldOps[i];
3140 OperandList = NewOps;
3141 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3142}
3143
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003144IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3145 Instruction *InsertBefore)
3146: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003147 0, 0, InsertBefore) {
3148 init(Address, NumCases);
3149}
3150
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003151IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3152 BasicBlock *InsertAtEnd)
3153: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003154 0, 0, InsertAtEnd) {
3155 init(Address, NumCases);
3156}
3157
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003158IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3159 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003160 allocHungoffUses(IBI.getNumOperands()),
3161 IBI.getNumOperands()) {
3162 Use *OL = OperandList, *InOL = IBI.OperandList;
3163 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3164 OL[i] = InOL[i];
3165 SubclassOptionalData = IBI.SubclassOptionalData;
3166}
3167
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003168IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003169 dropHungoffUses(OperandList);
3170}
3171
3172/// addDestination - Add a destination.
3173///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003174void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003175 unsigned OpNo = NumOperands;
3176 if (OpNo+1 > ReservedSpace)
3177 resizeOperands(0); // Get more space!
3178 // Initialize some new operands.
3179 assert(OpNo < ReservedSpace && "Growing didn't work!");
3180 NumOperands = OpNo+1;
3181 OperandList[OpNo] = DestBB;
3182}
3183
3184/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003185/// indirectbr instruction.
3186void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003187 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3188
3189 unsigned NumOps = getNumOperands();
3190 Use *OL = OperandList;
3191
3192 // Replace this value with the last one.
3193 OL[idx+1] = OL[NumOps-1];
3194
3195 // Nuke the last value.
3196 OL[NumOps-1].set(0);
3197 NumOperands = NumOps-1;
3198}
3199
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003200BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003201 return getSuccessor(idx);
3202}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003203unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003204 return getNumSuccessors();
3205}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003206void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003207 setSuccessor(idx, B);
3208}
3209
3210//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003211// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003212//===----------------------------------------------------------------------===//
3213
Chris Lattnerf22be932004-10-15 23:52:53 +00003214// Define these methods here so vtables don't get emitted into every translation
3215// unit that uses these classes.
3216
Devang Patel11cf3f42009-10-27 22:16:29 +00003217GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3218 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003219}
3220
Devang Patel11cf3f42009-10-27 22:16:29 +00003221BinaryOperator *BinaryOperator::clone_impl() const {
3222 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003223}
3224
Devang Patel11cf3f42009-10-27 22:16:29 +00003225FCmpInst* FCmpInst::clone_impl() const {
3226 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003227}
3228
Devang Patel11cf3f42009-10-27 22:16:29 +00003229ICmpInst* ICmpInst::clone_impl() const {
3230 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003231}
3232
Devang Patel11cf3f42009-10-27 22:16:29 +00003233ExtractValueInst *ExtractValueInst::clone_impl() const {
3234 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003235}
3236
Devang Patel11cf3f42009-10-27 22:16:29 +00003237InsertValueInst *InsertValueInst::clone_impl() const {
3238 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003239}
3240
Devang Patel11cf3f42009-10-27 22:16:29 +00003241AllocaInst *AllocaInst::clone_impl() const {
3242 return new AllocaInst(getAllocatedType(),
3243 (Value*)getOperand(0),
3244 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003245}
3246
Devang Patel11cf3f42009-10-27 22:16:29 +00003247LoadInst *LoadInst::clone_impl() const {
3248 return new LoadInst(getOperand(0),
3249 Twine(), isVolatile(),
3250 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003251}
3252
Devang Patel11cf3f42009-10-27 22:16:29 +00003253StoreInst *StoreInst::clone_impl() const {
3254 return new StoreInst(getOperand(0), getOperand(1),
3255 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003256}
3257
Devang Patel11cf3f42009-10-27 22:16:29 +00003258TruncInst *TruncInst::clone_impl() const {
3259 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003260}
3261
Devang Patel11cf3f42009-10-27 22:16:29 +00003262ZExtInst *ZExtInst::clone_impl() const {
3263 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003264}
3265
Devang Patel11cf3f42009-10-27 22:16:29 +00003266SExtInst *SExtInst::clone_impl() const {
3267 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003268}
3269
Devang Patel11cf3f42009-10-27 22:16:29 +00003270FPTruncInst *FPTruncInst::clone_impl() const {
3271 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003272}
3273
Devang Patel11cf3f42009-10-27 22:16:29 +00003274FPExtInst *FPExtInst::clone_impl() const {
3275 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003276}
3277
Devang Patel11cf3f42009-10-27 22:16:29 +00003278UIToFPInst *UIToFPInst::clone_impl() const {
3279 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003280}
3281
Devang Patel11cf3f42009-10-27 22:16:29 +00003282SIToFPInst *SIToFPInst::clone_impl() const {
3283 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003284}
3285
Devang Patel11cf3f42009-10-27 22:16:29 +00003286FPToUIInst *FPToUIInst::clone_impl() const {
3287 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003288}
3289
Devang Patel11cf3f42009-10-27 22:16:29 +00003290FPToSIInst *FPToSIInst::clone_impl() const {
3291 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003292}
3293
Devang Patel11cf3f42009-10-27 22:16:29 +00003294PtrToIntInst *PtrToIntInst::clone_impl() const {
3295 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003296}
3297
Devang Patel11cf3f42009-10-27 22:16:29 +00003298IntToPtrInst *IntToPtrInst::clone_impl() const {
3299 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003300}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003301
Devang Patel11cf3f42009-10-27 22:16:29 +00003302BitCastInst *BitCastInst::clone_impl() const {
3303 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003304}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003305
Devang Patel11cf3f42009-10-27 22:16:29 +00003306CallInst *CallInst::clone_impl() const {
3307 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003308}
3309
Devang Patel11cf3f42009-10-27 22:16:29 +00003310SelectInst *SelectInst::clone_impl() const {
3311 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003312}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003313
Devang Patel11cf3f42009-10-27 22:16:29 +00003314VAArgInst *VAArgInst::clone_impl() const {
3315 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003316}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003317
Devang Patel11cf3f42009-10-27 22:16:29 +00003318ExtractElementInst *ExtractElementInst::clone_impl() const {
3319 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003320}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003321
Devang Patel11cf3f42009-10-27 22:16:29 +00003322InsertElementInst *InsertElementInst::clone_impl() const {
3323 return InsertElementInst::Create(getOperand(0),
3324 getOperand(1),
3325 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003326}
3327
Devang Patel11cf3f42009-10-27 22:16:29 +00003328ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3329 return new ShuffleVectorInst(getOperand(0),
3330 getOperand(1),
3331 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003332}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003333
Devang Patel11cf3f42009-10-27 22:16:29 +00003334PHINode *PHINode::clone_impl() const {
3335 return new PHINode(*this);
3336}
3337
3338ReturnInst *ReturnInst::clone_impl() const {
3339 return new(getNumOperands()) ReturnInst(*this);
3340}
3341
3342BranchInst *BranchInst::clone_impl() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003343 unsigned Ops(getNumOperands());
Devang Patel11cf3f42009-10-27 22:16:29 +00003344 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003345}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003346
Devang Patel11cf3f42009-10-27 22:16:29 +00003347SwitchInst *SwitchInst::clone_impl() const {
3348 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003349}
3350
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003351IndirectBrInst *IndirectBrInst::clone_impl() const {
3352 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003353}
3354
3355
Devang Patel11cf3f42009-10-27 22:16:29 +00003356InvokeInst *InvokeInst::clone_impl() const {
3357 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003358}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003359
Devang Patel11cf3f42009-10-27 22:16:29 +00003360UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003361 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003362 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003363}
3364
Devang Patel11cf3f42009-10-27 22:16:29 +00003365UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003366 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003367 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003368}