blob: b03ee93dbe751974c531d43f232d1a2601d5a265 [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 Gohman5476cfd2009-08-12 16:23:25 +00001775BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001776 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001777 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001778 return new BinaryOperator(Instruction::FSub,
1779 zero, Op,
1780 Op->getType(), Name, InsertBefore);
1781}
1782
Dan Gohman5476cfd2009-08-12 16:23:25 +00001783BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001784 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001785 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001786 return new BinaryOperator(Instruction::FSub,
1787 zero, Op,
1788 Op->getType(), Name, InsertAtEnd);
1789}
1790
Dan Gohman5476cfd2009-08-12 16:23:25 +00001791BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001792 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001793 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001794 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001795 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001796 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001797 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001798 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001799 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001800 }
1801
1802 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001803 Op->getType(), Name, InsertBefore);
1804}
1805
Dan Gohman5476cfd2009-08-12 16:23:25 +00001806BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001807 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001808 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001809 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001810 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001811 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001812 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001813 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001814 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001815 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001816 }
1817
1818 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001819 Op->getType(), Name, InsertAtEnd);
1820}
1821
1822
1823// isConstantAllOnes - Helper function for several functions below
1824static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001825 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1826 return CI->isAllOnesValue();
1827 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1828 return CV->isAllOnesValue();
1829 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001830}
1831
Owen Andersonbb2501b2009-07-13 22:18:28 +00001832bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001833 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1834 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001835 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1836 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001837 return false;
1838}
1839
Owen Andersonbb2501b2009-07-13 22:18:28 +00001840bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001841 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1842 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001843 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001844 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001845 return false;
1846}
1847
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001848bool BinaryOperator::isNot(const Value *V) {
1849 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1850 return (Bop->getOpcode() == Instruction::Xor &&
1851 (isConstantAllOnes(Bop->getOperand(1)) ||
1852 isConstantAllOnes(Bop->getOperand(0))));
1853 return false;
1854}
1855
Chris Lattner2c7d1772005-04-24 07:28:37 +00001856Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001857 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001858}
1859
Chris Lattner2c7d1772005-04-24 07:28:37 +00001860const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1861 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001862}
1863
Dan Gohmana5b96452009-06-04 22:49:04 +00001864Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001865 return cast<BinaryOperator>(BinOp)->getOperand(1);
1866}
1867
1868const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1869 return getFNegArgument(const_cast<Value*>(BinOp));
1870}
1871
Chris Lattner2c7d1772005-04-24 07:28:37 +00001872Value *BinaryOperator::getNotArgument(Value *BinOp) {
1873 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1874 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1875 Value *Op0 = BO->getOperand(0);
1876 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001877 if (isConstantAllOnes(Op0)) return Op1;
1878
1879 assert(isConstantAllOnes(Op1));
1880 return Op0;
1881}
1882
Chris Lattner2c7d1772005-04-24 07:28:37 +00001883const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1884 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001885}
1886
1887
1888// swapOperands - Exchange the two operands to this instruction. This
1889// instruction is safe to use on any binary instruction and does not
1890// modify the semantics of the instruction. If the instruction is
1891// order dependent (SetLT f.e.) the opcode is changed.
1892//
1893bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001894 if (!isCommutative())
1895 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001896 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001897 return false;
1898}
1899
Dan Gohman1b849082009-09-07 23:54:19 +00001900void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1901 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1902}
1903
1904void BinaryOperator::setHasNoSignedWrap(bool b) {
1905 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1906}
1907
1908void BinaryOperator::setIsExact(bool b) {
1909 cast<SDivOperator>(this)->setIsExact(b);
1910}
1911
Nick Lewycky28a5f252009-09-27 21:33:04 +00001912bool BinaryOperator::hasNoUnsignedWrap() const {
1913 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1914}
1915
1916bool BinaryOperator::hasNoSignedWrap() const {
1917 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1918}
1919
1920bool BinaryOperator::isExact() const {
1921 return cast<SDivOperator>(this)->isExact();
1922}
1923
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001924//===----------------------------------------------------------------------===//
1925// CastInst Class
1926//===----------------------------------------------------------------------===//
1927
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001928// Just determine if this cast only deals with integral->integral conversion.
1929bool CastInst::isIntegerCast() const {
1930 switch (getOpcode()) {
1931 default: return false;
1932 case Instruction::ZExt:
1933 case Instruction::SExt:
1934 case Instruction::Trunc:
1935 return true;
1936 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001937 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001938 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001939}
1940
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001941bool CastInst::isLosslessCast() const {
1942 // Only BitCast can be lossless, exit fast if we're not BitCast
1943 if (getOpcode() != Instruction::BitCast)
1944 return false;
1945
1946 // Identity cast is always lossless
1947 const Type* SrcTy = getOperand(0)->getType();
1948 const Type* DstTy = getType();
1949 if (SrcTy == DstTy)
1950 return true;
1951
Reid Spencer8d9336d2006-12-31 05:26:44 +00001952 // Pointer to pointer is always lossless.
1953 if (isa<PointerType>(SrcTy))
1954 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001955 return false; // Other types have no identity values
1956}
1957
1958/// This function determines if the CastInst does not require any bits to be
1959/// changed in order to effect the cast. Essentially, it identifies cases where
1960/// no code gen is necessary for the cast, hence the name no-op cast. For
1961/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001962/// # bitcast i32* %x to i8*
1963/// # bitcast <2 x i32> %x to <4 x i16>
1964/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001965/// @brief Determine if a cast is a no-op.
1966bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1967 switch (getOpcode()) {
1968 default:
1969 assert(!"Invalid CastOp");
1970 case Instruction::Trunc:
1971 case Instruction::ZExt:
1972 case Instruction::SExt:
1973 case Instruction::FPTrunc:
1974 case Instruction::FPExt:
1975 case Instruction::UIToFP:
1976 case Instruction::SIToFP:
1977 case Instruction::FPToUI:
1978 case Instruction::FPToSI:
1979 return false; // These always modify bits
1980 case Instruction::BitCast:
1981 return true; // BitCast never modifies bits.
1982 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001983 return IntPtrTy->getScalarSizeInBits() ==
1984 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001985 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001986 return IntPtrTy->getScalarSizeInBits() ==
1987 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001988 }
1989}
1990
1991/// This function determines if a pair of casts can be eliminated and what
1992/// opcode should be used in the elimination. This assumes that there are two
1993/// instructions like this:
1994/// * %F = firstOpcode SrcTy %x to MidTy
1995/// * %S = secondOpcode MidTy %F to DstTy
1996/// The function returns a resultOpcode so these two casts can be replaced with:
1997/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1998/// If no such cast is permited, the function returns 0.
1999unsigned CastInst::isEliminableCastPair(
2000 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
2001 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
2002{
2003 // Define the 144 possibilities for these two cast instructions. The values
2004 // in this matrix determine what to do in a given situation and select the
2005 // case in the switch below. The rows correspond to firstOp, the columns
2006 // correspond to secondOp. In looking at the table below, keep in mind
2007 // the following cast properties:
2008 //
2009 // Size Compare Source Destination
2010 // Operator Src ? Size Type Sign Type Sign
2011 // -------- ------------ ------------------- ---------------------
2012 // TRUNC > Integer Any Integral Any
2013 // ZEXT < Integral Unsigned Integer Any
2014 // SEXT < Integral Signed Integer Any
2015 // FPTOUI n/a FloatPt n/a Integral Unsigned
2016 // FPTOSI n/a FloatPt n/a Integral Signed
2017 // UITOFP n/a Integral Unsigned FloatPt n/a
2018 // SITOFP n/a Integral Signed FloatPt n/a
2019 // FPTRUNC > FloatPt n/a FloatPt n/a
2020 // FPEXT < FloatPt n/a FloatPt n/a
2021 // PTRTOINT n/a Pointer n/a Integral Unsigned
2022 // INTTOPTR n/a Integral Unsigned Pointer n/a
2023 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002024 //
2025 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002026 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2027 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002028 // of the produced value (we no longer know the top-part is all zeros).
2029 // Further this conversion is often much more expensive for typical hardware,
2030 // and causes issues when building libgcc. We disallow fptosi+sext for the
2031 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002032 const unsigned numCastOps =
2033 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2034 static const uint8_t CastResults[numCastOps][numCastOps] = {
2035 // T F F U S F F P I B -+
2036 // R Z S P P I I T P 2 N T |
2037 // U E E 2 2 2 2 R E I T C +- secondOp
2038 // N X X U S F F N X N 2 V |
2039 // C T T I I P P C T T P T -+
2040 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2041 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2042 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002043 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2044 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002045 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2046 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2047 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2048 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2049 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2050 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2051 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2052 };
2053
2054 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2055 [secondOp-Instruction::CastOpsBegin];
2056 switch (ElimCase) {
2057 case 0:
2058 // categorically disallowed
2059 return 0;
2060 case 1:
2061 // allowed, use first cast's opcode
2062 return firstOp;
2063 case 2:
2064 // allowed, use second cast's opcode
2065 return secondOp;
2066 case 3:
2067 // no-op cast in second op implies firstOp as long as the DestTy
2068 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00002069 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002070 return firstOp;
2071 return 0;
2072 case 4:
2073 // no-op cast in second op implies firstOp as long as the DestTy
2074 // is floating point
2075 if (DstTy->isFloatingPoint())
2076 return firstOp;
2077 return 0;
2078 case 5:
2079 // no-op cast in first op implies secondOp as long as the SrcTy
2080 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00002081 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002082 return secondOp;
2083 return 0;
2084 case 6:
2085 // no-op cast in first op implies secondOp as long as the SrcTy
2086 // is a floating point
2087 if (SrcTy->isFloatingPoint())
2088 return secondOp;
2089 return 0;
2090 case 7: {
2091 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002092 if (!IntPtrTy)
2093 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002094 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2095 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002096 if (MidSize >= PtrSize)
2097 return Instruction::BitCast;
2098 return 0;
2099 }
2100 case 8: {
2101 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2102 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2103 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002104 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2105 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002106 if (SrcSize == DstSize)
2107 return Instruction::BitCast;
2108 else if (SrcSize < DstSize)
2109 return firstOp;
2110 return secondOp;
2111 }
2112 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2113 return Instruction::ZExt;
2114 case 10:
2115 // fpext followed by ftrunc is allowed if the bit size returned to is
2116 // the same as the original, in which case its just a bitcast
2117 if (SrcTy == DstTy)
2118 return Instruction::BitCast;
2119 return 0; // If the types are not the same we can't eliminate it.
2120 case 11:
2121 // bitcast followed by ptrtoint is allowed as long as the bitcast
2122 // is a pointer to pointer cast.
2123 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
2124 return secondOp;
2125 return 0;
2126 case 12:
2127 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2128 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
2129 return firstOp;
2130 return 0;
2131 case 13: {
2132 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002133 if (!IntPtrTy)
2134 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002135 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2136 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2137 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002138 if (SrcSize <= PtrSize && SrcSize == DstSize)
2139 return Instruction::BitCast;
2140 return 0;
2141 }
2142 case 99:
2143 // cast combination can't happen (error in input). This is for all cases
2144 // where the MidTy is not the same for the two cast instructions.
2145 assert(!"Invalid Cast Combination");
2146 return 0;
2147 default:
2148 assert(!"Error in CastResults table!!!");
2149 return 0;
2150 }
2151 return 0;
2152}
2153
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002154CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002155 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002156 // Construct and return the appropriate CastInst subclass
2157 switch (op) {
2158 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2159 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2160 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2161 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2162 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2163 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2164 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2165 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2166 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2167 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2168 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2169 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2170 default:
2171 assert(!"Invalid opcode provided");
2172 }
2173 return 0;
2174}
2175
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002176CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002177 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002178 // Construct and return the appropriate CastInst subclass
2179 switch (op) {
2180 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2181 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2182 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2183 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2184 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2185 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2186 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2187 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2188 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2189 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2190 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2191 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2192 default:
2193 assert(!"Invalid opcode provided");
2194 }
2195 return 0;
2196}
2197
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002198CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002199 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002200 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002201 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002202 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2203 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002204}
2205
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002206CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002207 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002208 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002209 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002210 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2211 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002212}
2213
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002214CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002215 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002216 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002217 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002218 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2219 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002220}
2221
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002222CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002223 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002224 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002225 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002226 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2227 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002228}
2229
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002230CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002231 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002232 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002233 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002234 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2235 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002236}
2237
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002238CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002239 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002240 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002241 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002242 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2243 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002244}
2245
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002246CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002247 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002248 BasicBlock *InsertAtEnd) {
2249 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002250 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002251 "Invalid cast");
2252
Chris Lattner03c49532007-01-15 02:27:26 +00002253 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002254 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2255 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002256}
2257
2258/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002259CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002260 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002261 Instruction *InsertBefore) {
2262 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002263 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002264 "Invalid cast");
2265
Chris Lattner03c49532007-01-15 02:27:26 +00002266 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002267 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2268 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002269}
2270
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002271CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002272 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002273 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002274 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002275 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2276 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002277 Instruction::CastOps opcode =
2278 (SrcBits == DstBits ? Instruction::BitCast :
2279 (SrcBits > DstBits ? Instruction::Trunc :
2280 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002281 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002282}
2283
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002284CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002285 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002286 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002287 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2288 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002289 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2290 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002291 Instruction::CastOps opcode =
2292 (SrcBits == DstBits ? Instruction::BitCast :
2293 (SrcBits > DstBits ? Instruction::Trunc :
2294 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002295 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002296}
2297
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002298CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002299 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002300 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002301 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002302 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002303 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2304 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002305 Instruction::CastOps opcode =
2306 (SrcBits == DstBits ? Instruction::BitCast :
2307 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002308 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002309}
2310
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002311CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002312 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002313 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002314 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002315 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002316 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2317 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002318 Instruction::CastOps opcode =
2319 (SrcBits == DstBits ? Instruction::BitCast :
2320 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002321 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002322}
2323
Duncan Sands55e50902008-01-06 10:12:28 +00002324// Check whether it is valid to call getCastOpcode for these types.
2325// This routine must be kept in sync with getCastOpcode.
2326bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2327 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2328 return false;
2329
2330 if (SrcTy == DestTy)
2331 return true;
2332
2333 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002334 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2335 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002336
2337 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002338 if (DestTy->isInteger()) { // Casting to integral
2339 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002340 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002341 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002342 return true;
2343 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002344 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002345 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002346 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002347 return isa<PointerType>(SrcTy);
2348 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002349 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2350 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002351 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002352 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002353 return true;
2354 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002355 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002356 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002357 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002358 return false;
2359 }
2360 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002361 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002362 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002363 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002364 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002365 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002366 return DestPTy->getBitWidth() == SrcBits;
2367 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002368 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2369 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002370 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002371 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002372 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002373 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002374 return false;
2375 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002376 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002377 return false;
2378 }
2379}
2380
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002381// Provide a way to get a "cast" where the cast opcode is inferred from the
2382// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002383// logic in the castIsValid function below. This axiom should hold:
2384// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2385// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002386// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002387// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002388Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002389CastInst::getCastOpcode(
2390 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002391 // Get the bit sizes, we'll need these
2392 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002393 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2394 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002395
Duncan Sands55e50902008-01-06 10:12:28 +00002396 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2397 "Only first class types are castable!");
2398
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002399 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002400 if (DestTy->isInteger()) { // Casting to integral
2401 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402 if (DestBits < SrcBits)
2403 return Trunc; // int -> smaller int
2404 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002405 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002406 return SExt; // signed -> SEXT
2407 else
2408 return ZExt; // unsigned -> ZEXT
2409 } else {
2410 return BitCast; // Same size, No-op cast
2411 }
2412 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002413 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002414 return FPToSI; // FP -> sint
2415 else
2416 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002417 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002418 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002419 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002420 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002421 return BitCast; // Same size, no-op cast
2422 } else {
2423 assert(isa<PointerType>(SrcTy) &&
2424 "Casting from a value that is not first-class type");
2425 return PtrToInt; // ptr -> int
2426 }
2427 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002428 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002429 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002430 return SIToFP; // sint -> FP
2431 else
2432 return UIToFP; // uint -> FP
2433 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2434 if (DestBits < SrcBits) {
2435 return FPTrunc; // FP -> smaller FP
2436 } else if (DestBits > SrcBits) {
2437 return FPExt; // FP -> larger FP
2438 } else {
2439 return BitCast; // same size, no-op cast
2440 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002441 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002442 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002443 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002444 PTy = NULL;
2445 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002446 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002447 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002448 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002449 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2450 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002451 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002452 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002453 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002454 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002455 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002456 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002457 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002458 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459 }
2460 } else if (isa<PointerType>(DestTy)) {
2461 if (isa<PointerType>(SrcTy)) {
2462 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002463 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002464 return IntToPtr; // int -> ptr
2465 } else {
2466 assert(!"Casting pointer to other than pointer or int");
2467 }
2468 } else {
2469 assert(!"Casting to type that is not first-class");
2470 }
2471
2472 // If we fall through to here we probably hit an assertion cast above
2473 // and assertions are not turned on. Anything we return is an error, so
2474 // BitCast is as good a choice as any.
2475 return BitCast;
2476}
2477
2478//===----------------------------------------------------------------------===//
2479// CastInst SubClass Constructors
2480//===----------------------------------------------------------------------===//
2481
2482/// Check that the construction parameters for a CastInst are correct. This
2483/// could be broken out into the separate constructors but it is useful to have
2484/// it in one place and to eliminate the redundant code for getting the sizes
2485/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002486bool
2487CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002488
2489 // Check for type sanity on the arguments
2490 const Type *SrcTy = S->getType();
2491 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2492 return false;
2493
2494 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002495 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2496 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002497
2498 // Switch on the opcode provided
2499 switch (op) {
2500 default: return false; // This is an input error
2501 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002502 return SrcTy->isIntOrIntVector() &&
2503 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002504 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002505 return SrcTy->isIntOrIntVector() &&
2506 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002508 return SrcTy->isIntOrIntVector() &&
2509 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002510 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002511 return SrcTy->isFPOrFPVector() &&
2512 DstTy->isFPOrFPVector() &&
2513 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002514 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002515 return SrcTy->isFPOrFPVector() &&
2516 DstTy->isFPOrFPVector() &&
2517 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002518 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002519 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002520 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2521 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002522 return SVTy->getElementType()->isIntOrIntVector() &&
2523 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002524 SVTy->getNumElements() == DVTy->getNumElements();
2525 }
2526 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002527 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002529 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002530 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2531 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002532 return SVTy->getElementType()->isFPOrFPVector() &&
2533 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002534 SVTy->getNumElements() == DVTy->getNumElements();
2535 }
2536 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002537 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002539 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002541 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002542 case Instruction::BitCast:
2543 // BitCast implies a no-op cast of type only. No bits change.
2544 // However, you can't cast pointers to anything but pointers.
2545 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2546 return false;
2547
Duncan Sands55e50902008-01-06 10:12:28 +00002548 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002549 // these cases, the cast is okay if the source and destination bit widths
2550 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002551 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002552 }
2553}
2554
2555TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002556 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002557) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002558 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002559}
2560
2561TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002562 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002564 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002565}
2566
2567ZExtInst::ZExtInst(
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, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002570 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571}
2572
2573ZExtInst::ZExtInst(
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, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002576 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002577}
2578SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002579 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002581 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002582}
2583
Jeff Cohencc08c832006-12-02 02:22:01 +00002584SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002585 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002587 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002588}
2589
2590FPTruncInst::FPTruncInst(
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, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002593 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594}
2595
2596FPTruncInst::FPTruncInst(
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, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002599 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600}
2601
2602FPExtInst::FPExtInst(
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, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002605 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606}
2607
2608FPExtInst::FPExtInst(
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, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002611 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612}
2613
2614UIToFPInst::UIToFPInst(
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, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002617 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002618}
2619
2620UIToFPInst::UIToFPInst(
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, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002623 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002624}
2625
2626SIToFPInst::SIToFPInst(
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, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002629 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002630}
2631
2632SIToFPInst::SIToFPInst(
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, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002635 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002636}
2637
2638FPToUIInst::FPToUIInst(
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, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002641 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642}
2643
2644FPToUIInst::FPToUIInst(
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, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002647 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648}
2649
2650FPToSIInst::FPToSIInst(
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, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002653 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002654}
2655
2656FPToSIInst::FPToSIInst(
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, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002659 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002660}
2661
2662PtrToIntInst::PtrToIntInst(
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, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002665 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002666}
2667
2668PtrToIntInst::PtrToIntInst(
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, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002671 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002672}
2673
2674IntToPtrInst::IntToPtrInst(
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, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002677 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002678}
2679
2680IntToPtrInst::IntToPtrInst(
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, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002683 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002684}
2685
2686BitCastInst::BitCastInst(
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, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002689 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002690}
2691
2692BitCastInst::BitCastInst(
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, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002695 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002696}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002697
2698//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002699// CmpInst Classes
2700//===----------------------------------------------------------------------===//
2701
Nate Begemand2195702008-05-12 19:01:56 +00002702CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002703 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002704 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002705 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002706 OperandTraits<CmpInst>::op_begin(this),
2707 OperandTraits<CmpInst>::operands(this),
2708 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002709 Op<0>() = LHS;
2710 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002711 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002712 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002713}
Gabor Greiff6caff662008-05-10 08:32:32 +00002714
Nate Begemand2195702008-05-12 19:01:56 +00002715CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002716 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002717 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002718 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002719 OperandTraits<CmpInst>::op_begin(this),
2720 OperandTraits<CmpInst>::operands(this),
2721 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002722 Op<0>() = LHS;
2723 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002724 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002725 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002726}
2727
2728CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002729CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002730 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002731 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002732 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002733 if (InsertBefore)
2734 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2735 S1, S2, Name);
2736 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002737 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002738 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002739 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002740
2741 if (InsertBefore)
2742 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2743 S1, S2, Name);
2744 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002745 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002746 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002747}
2748
2749CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002750CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002751 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002752 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002753 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2754 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002755 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002756 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2757 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002758}
2759
2760void CmpInst::swapOperands() {
2761 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2762 IC->swapOperands();
2763 else
2764 cast<FCmpInst>(this)->swapOperands();
2765}
2766
2767bool CmpInst::isCommutative() {
2768 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2769 return IC->isCommutative();
2770 return cast<FCmpInst>(this)->isCommutative();
2771}
2772
2773bool CmpInst::isEquality() {
2774 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2775 return IC->isEquality();
2776 return cast<FCmpInst>(this)->isEquality();
2777}
2778
2779
Dan Gohman4e724382008-05-31 02:47:54 +00002780CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002781 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002782 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002783 case ICMP_EQ: return ICMP_NE;
2784 case ICMP_NE: return ICMP_EQ;
2785 case ICMP_UGT: return ICMP_ULE;
2786 case ICMP_ULT: return ICMP_UGE;
2787 case ICMP_UGE: return ICMP_ULT;
2788 case ICMP_ULE: return ICMP_UGT;
2789 case ICMP_SGT: return ICMP_SLE;
2790 case ICMP_SLT: return ICMP_SGE;
2791 case ICMP_SGE: return ICMP_SLT;
2792 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002793
Dan Gohman4e724382008-05-31 02:47:54 +00002794 case FCMP_OEQ: return FCMP_UNE;
2795 case FCMP_ONE: return FCMP_UEQ;
2796 case FCMP_OGT: return FCMP_ULE;
2797 case FCMP_OLT: return FCMP_UGE;
2798 case FCMP_OGE: return FCMP_ULT;
2799 case FCMP_OLE: return FCMP_UGT;
2800 case FCMP_UEQ: return FCMP_ONE;
2801 case FCMP_UNE: return FCMP_OEQ;
2802 case FCMP_UGT: return FCMP_OLE;
2803 case FCMP_ULT: return FCMP_OGE;
2804 case FCMP_UGE: return FCMP_OLT;
2805 case FCMP_ULE: return FCMP_OGT;
2806 case FCMP_ORD: return FCMP_UNO;
2807 case FCMP_UNO: return FCMP_ORD;
2808 case FCMP_TRUE: return FCMP_FALSE;
2809 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002810 }
2811}
2812
Reid Spencer266e42b2006-12-23 06:05:41 +00002813ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2814 switch (pred) {
2815 default: assert(! "Unknown icmp predicate!");
2816 case ICMP_EQ: case ICMP_NE:
2817 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2818 return pred;
2819 case ICMP_UGT: return ICMP_SGT;
2820 case ICMP_ULT: return ICMP_SLT;
2821 case ICMP_UGE: return ICMP_SGE;
2822 case ICMP_ULE: return ICMP_SLE;
2823 }
2824}
2825
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002826ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2827 switch (pred) {
2828 default: assert(! "Unknown icmp predicate!");
2829 case ICMP_EQ: case ICMP_NE:
2830 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2831 return pred;
2832 case ICMP_SGT: return ICMP_UGT;
2833 case ICMP_SLT: return ICMP_ULT;
2834 case ICMP_SGE: return ICMP_UGE;
2835 case ICMP_SLE: return ICMP_ULE;
2836 }
2837}
2838
Reid Spencer0286bc12007-02-28 22:00:54 +00002839/// Initialize a set of values that all satisfy the condition with C.
2840///
2841ConstantRange
2842ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2843 APInt Lower(C);
2844 APInt Upper(C);
2845 uint32_t BitWidth = C.getBitWidth();
2846 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002847 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002848 case ICmpInst::ICMP_EQ: Upper++; break;
2849 case ICmpInst::ICMP_NE: Lower++; break;
2850 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2851 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2852 case ICmpInst::ICMP_UGT:
2853 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2854 break;
2855 case ICmpInst::ICMP_SGT:
2856 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2857 break;
2858 case ICmpInst::ICMP_ULE:
2859 Lower = APInt::getMinValue(BitWidth); Upper++;
2860 break;
2861 case ICmpInst::ICMP_SLE:
2862 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2863 break;
2864 case ICmpInst::ICMP_UGE:
2865 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2866 break;
2867 case ICmpInst::ICMP_SGE:
2868 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2869 break;
2870 }
2871 return ConstantRange(Lower, Upper);
2872}
2873
Dan Gohman4e724382008-05-31 02:47:54 +00002874CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002875 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002876 default: assert(!"Unknown cmp predicate!");
2877 case ICMP_EQ: case ICMP_NE:
2878 return pred;
2879 case ICMP_SGT: return ICMP_SLT;
2880 case ICMP_SLT: return ICMP_SGT;
2881 case ICMP_SGE: return ICMP_SLE;
2882 case ICMP_SLE: return ICMP_SGE;
2883 case ICMP_UGT: return ICMP_ULT;
2884 case ICMP_ULT: return ICMP_UGT;
2885 case ICMP_UGE: return ICMP_ULE;
2886 case ICMP_ULE: return ICMP_UGE;
2887
Reid Spencerd9436b62006-11-20 01:22:35 +00002888 case FCMP_FALSE: case FCMP_TRUE:
2889 case FCMP_OEQ: case FCMP_ONE:
2890 case FCMP_UEQ: case FCMP_UNE:
2891 case FCMP_ORD: case FCMP_UNO:
2892 return pred;
2893 case FCMP_OGT: return FCMP_OLT;
2894 case FCMP_OLT: return FCMP_OGT;
2895 case FCMP_OGE: return FCMP_OLE;
2896 case FCMP_OLE: return FCMP_OGE;
2897 case FCMP_UGT: return FCMP_ULT;
2898 case FCMP_ULT: return FCMP_UGT;
2899 case FCMP_UGE: return FCMP_ULE;
2900 case FCMP_ULE: return FCMP_UGE;
2901 }
2902}
2903
Reid Spencer266e42b2006-12-23 06:05:41 +00002904bool CmpInst::isUnsigned(unsigned short predicate) {
2905 switch (predicate) {
2906 default: return false;
2907 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2908 case ICmpInst::ICMP_UGE: return true;
2909 }
2910}
2911
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002912bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002913 switch (predicate) {
2914 default: return false;
2915 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2916 case ICmpInst::ICMP_SGE: return true;
2917 }
2918}
2919
2920bool CmpInst::isOrdered(unsigned short predicate) {
2921 switch (predicate) {
2922 default: return false;
2923 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2924 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2925 case FCmpInst::FCMP_ORD: return true;
2926 }
2927}
2928
2929bool CmpInst::isUnordered(unsigned short predicate) {
2930 switch (predicate) {
2931 default: return false;
2932 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2933 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2934 case FCmpInst::FCMP_UNO: return true;
2935 }
2936}
2937
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002938bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2939 switch(predicate) {
2940 default: return false;
2941 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2942 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2943 }
2944}
2945
2946bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2947 switch(predicate) {
2948 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2949 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2950 default: return false;
2951 }
2952}
2953
2954
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002955//===----------------------------------------------------------------------===//
2956// SwitchInst Implementation
2957//===----------------------------------------------------------------------===//
2958
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002959void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002960 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002961 ReservedSpace = 2+NumCases*2;
2962 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002963 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002964
Gabor Greif2d3024d2008-05-26 21:33:52 +00002965 OperandList[0] = Value;
2966 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002967}
2968
Chris Lattner2195fc42007-02-24 00:55:48 +00002969/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2970/// switch on and a default destination. The number of additional cases can
2971/// be specified here to make memory allocation more efficient. This
2972/// constructor can also autoinsert before another instruction.
2973SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2974 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002975 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2976 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002977 init(Value, Default, NumCases);
2978}
2979
2980/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2981/// switch on and a default destination. The number of additional cases can
2982/// be specified here to make memory allocation more efficient. This
2983/// constructor also autoinserts at the end of the specified BasicBlock.
2984SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2985 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002986 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2987 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002988 init(Value, Default, NumCases);
2989}
2990
Misha Brukmanb1c93172005-04-21 23:48:37 +00002991SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002992 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002993 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002994 Use *OL = OperandList, *InOL = SI.OperandList;
2995 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002996 OL[i] = InOL[i];
2997 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002998 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002999 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003000}
3001
Gordon Henriksen14a55692007-12-10 02:14:30 +00003002SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003003 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003004}
3005
3006
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003007/// addCase - Add an entry to the switch instruction...
3008///
Chris Lattner47ac1872005-02-24 05:32:09 +00003009void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003010 unsigned OpNo = NumOperands;
3011 if (OpNo+2 > ReservedSpace)
3012 resizeOperands(0); // Get more space!
3013 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003014 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003015 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003016 OperandList[OpNo] = OnVal;
3017 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003018}
3019
3020/// removeCase - This method removes the specified successor from the switch
3021/// instruction. Note that this cannot be used to remove the default
3022/// destination (successor #0).
3023///
3024void SwitchInst::removeCase(unsigned idx) {
3025 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003026 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3027
3028 unsigned NumOps = getNumOperands();
3029 Use *OL = OperandList;
3030
3031 // Move everything after this operand down.
3032 //
3033 // FIXME: we could just swap with the end of the list, then erase. However,
3034 // client might not expect this to happen. The code as it is thrashes the
3035 // use/def lists, which is kinda lame.
3036 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3037 OL[i-2] = OL[i];
3038 OL[i-2+1] = OL[i+1];
3039 }
3040
3041 // Nuke the last value.
3042 OL[NumOps-2].set(0);
3043 OL[NumOps-2+1].set(0);
3044 NumOperands = NumOps-2;
3045}
3046
3047/// resizeOperands - resize operands - This adjusts the length of the operands
3048/// list according to the following behavior:
3049/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003050/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003051/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3052/// 3. If NumOps == NumOperands, trim the reserved space.
3053///
3054void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003055 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003056 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003057 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003058 } else if (NumOps*2 > NumOperands) {
3059 // No resize needed.
3060 if (ReservedSpace >= NumOps) return;
3061 } else if (NumOps == NumOperands) {
3062 if (ReservedSpace == NumOps) return;
3063 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003064 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003065 }
3066
3067 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003068 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003069 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003070 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003071 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003072 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003073 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003074 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003075}
3076
3077
3078BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3079 return getSuccessor(idx);
3080}
3081unsigned SwitchInst::getNumSuccessorsV() const {
3082 return getNumSuccessors();
3083}
3084void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3085 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003086}
Chris Lattnerf22be932004-10-15 23:52:53 +00003087
Chris Lattner3ed871f2009-10-27 19:13:16 +00003088//===----------------------------------------------------------------------===//
3089// SwitchInst Implementation
3090//===----------------------------------------------------------------------===//
3091
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003092void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Chris Lattner6747b4c2009-10-29 05:53:32 +00003093 assert(Address && isa<PointerType>(Address->getType()) &&
3094 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003095 ReservedSpace = 1+NumDests;
3096 NumOperands = 1;
3097 OperandList = allocHungoffUses(ReservedSpace);
3098
3099 OperandList[0] = Address;
3100}
3101
3102
3103/// resizeOperands - resize operands - This adjusts the length of the operands
3104/// list according to the following behavior:
3105/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3106/// of operation. This grows the number of ops by 2 times.
3107/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3108/// 3. If NumOps == NumOperands, trim the reserved space.
3109///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003110void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003111 unsigned e = getNumOperands();
3112 if (NumOps == 0) {
3113 NumOps = e*2;
3114 } else if (NumOps*2 > NumOperands) {
3115 // No resize needed.
3116 if (ReservedSpace >= NumOps) return;
3117 } else if (NumOps == NumOperands) {
3118 if (ReservedSpace == NumOps) return;
3119 } else {
3120 return;
3121 }
3122
3123 ReservedSpace = NumOps;
3124 Use *NewOps = allocHungoffUses(NumOps);
3125 Use *OldOps = OperandList;
3126 for (unsigned i = 0; i != e; ++i)
3127 NewOps[i] = OldOps[i];
3128 OperandList = NewOps;
3129 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3130}
3131
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003132IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3133 Instruction *InsertBefore)
3134: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003135 0, 0, InsertBefore) {
3136 init(Address, NumCases);
3137}
3138
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003139IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3140 BasicBlock *InsertAtEnd)
3141: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003142 0, 0, InsertAtEnd) {
3143 init(Address, NumCases);
3144}
3145
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003146IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3147 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003148 allocHungoffUses(IBI.getNumOperands()),
3149 IBI.getNumOperands()) {
3150 Use *OL = OperandList, *InOL = IBI.OperandList;
3151 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3152 OL[i] = InOL[i];
3153 SubclassOptionalData = IBI.SubclassOptionalData;
3154}
3155
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003156IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003157 dropHungoffUses(OperandList);
3158}
3159
3160/// addDestination - Add a destination.
3161///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003162void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003163 unsigned OpNo = NumOperands;
3164 if (OpNo+1 > ReservedSpace)
3165 resizeOperands(0); // Get more space!
3166 // Initialize some new operands.
3167 assert(OpNo < ReservedSpace && "Growing didn't work!");
3168 NumOperands = OpNo+1;
3169 OperandList[OpNo] = DestBB;
3170}
3171
3172/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003173/// indirectbr instruction.
3174void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003175 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3176
3177 unsigned NumOps = getNumOperands();
3178 Use *OL = OperandList;
3179
3180 // Replace this value with the last one.
3181 OL[idx+1] = OL[NumOps-1];
3182
3183 // Nuke the last value.
3184 OL[NumOps-1].set(0);
3185 NumOperands = NumOps-1;
3186}
3187
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003188BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003189 return getSuccessor(idx);
3190}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003191unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003192 return getNumSuccessors();
3193}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003194void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003195 setSuccessor(idx, B);
3196}
3197
3198//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003199// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003200//===----------------------------------------------------------------------===//
3201
Chris Lattnerf22be932004-10-15 23:52:53 +00003202// Define these methods here so vtables don't get emitted into every translation
3203// unit that uses these classes.
3204
Devang Patel11cf3f42009-10-27 22:16:29 +00003205GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3206 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003207}
3208
Devang Patel11cf3f42009-10-27 22:16:29 +00003209BinaryOperator *BinaryOperator::clone_impl() const {
3210 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003211}
3212
Devang Patel11cf3f42009-10-27 22:16:29 +00003213FCmpInst* FCmpInst::clone_impl() const {
3214 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003215}
3216
Devang Patel11cf3f42009-10-27 22:16:29 +00003217ICmpInst* ICmpInst::clone_impl() const {
3218 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003219}
3220
Devang Patel11cf3f42009-10-27 22:16:29 +00003221ExtractValueInst *ExtractValueInst::clone_impl() const {
3222 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003223}
3224
Devang Patel11cf3f42009-10-27 22:16:29 +00003225InsertValueInst *InsertValueInst::clone_impl() const {
3226 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003227}
3228
Devang Patel11cf3f42009-10-27 22:16:29 +00003229AllocaInst *AllocaInst::clone_impl() const {
3230 return new AllocaInst(getAllocatedType(),
3231 (Value*)getOperand(0),
3232 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003233}
3234
Devang Patel11cf3f42009-10-27 22:16:29 +00003235LoadInst *LoadInst::clone_impl() const {
3236 return new LoadInst(getOperand(0),
3237 Twine(), isVolatile(),
3238 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003239}
3240
Devang Patel11cf3f42009-10-27 22:16:29 +00003241StoreInst *StoreInst::clone_impl() const {
3242 return new StoreInst(getOperand(0), getOperand(1),
3243 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003244}
3245
Devang Patel11cf3f42009-10-27 22:16:29 +00003246TruncInst *TruncInst::clone_impl() const {
3247 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003248}
3249
Devang Patel11cf3f42009-10-27 22:16:29 +00003250ZExtInst *ZExtInst::clone_impl() const {
3251 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003252}
3253
Devang Patel11cf3f42009-10-27 22:16:29 +00003254SExtInst *SExtInst::clone_impl() const {
3255 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003256}
3257
Devang Patel11cf3f42009-10-27 22:16:29 +00003258FPTruncInst *FPTruncInst::clone_impl() const {
3259 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003260}
3261
Devang Patel11cf3f42009-10-27 22:16:29 +00003262FPExtInst *FPExtInst::clone_impl() const {
3263 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003264}
3265
Devang Patel11cf3f42009-10-27 22:16:29 +00003266UIToFPInst *UIToFPInst::clone_impl() const {
3267 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003268}
3269
Devang Patel11cf3f42009-10-27 22:16:29 +00003270SIToFPInst *SIToFPInst::clone_impl() const {
3271 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003272}
3273
Devang Patel11cf3f42009-10-27 22:16:29 +00003274FPToUIInst *FPToUIInst::clone_impl() const {
3275 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003276}
3277
Devang Patel11cf3f42009-10-27 22:16:29 +00003278FPToSIInst *FPToSIInst::clone_impl() const {
3279 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003280}
3281
Devang Patel11cf3f42009-10-27 22:16:29 +00003282PtrToIntInst *PtrToIntInst::clone_impl() const {
3283 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003284}
3285
Devang Patel11cf3f42009-10-27 22:16:29 +00003286IntToPtrInst *IntToPtrInst::clone_impl() const {
3287 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003288}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003289
Devang Patel11cf3f42009-10-27 22:16:29 +00003290BitCastInst *BitCastInst::clone_impl() const {
3291 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003292}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003293
Devang Patel11cf3f42009-10-27 22:16:29 +00003294CallInst *CallInst::clone_impl() const {
3295 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003296}
3297
Devang Patel11cf3f42009-10-27 22:16:29 +00003298SelectInst *SelectInst::clone_impl() const {
3299 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003300}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003301
Devang Patel11cf3f42009-10-27 22:16:29 +00003302VAArgInst *VAArgInst::clone_impl() const {
3303 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003304}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003305
Devang Patel11cf3f42009-10-27 22:16:29 +00003306ExtractElementInst *ExtractElementInst::clone_impl() const {
3307 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003308}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003309
Devang Patel11cf3f42009-10-27 22:16:29 +00003310InsertElementInst *InsertElementInst::clone_impl() const {
3311 return InsertElementInst::Create(getOperand(0),
3312 getOperand(1),
3313 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003314}
3315
Devang Patel11cf3f42009-10-27 22:16:29 +00003316ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3317 return new ShuffleVectorInst(getOperand(0),
3318 getOperand(1),
3319 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003320}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003321
Devang Patel11cf3f42009-10-27 22:16:29 +00003322PHINode *PHINode::clone_impl() const {
3323 return new PHINode(*this);
3324}
3325
3326ReturnInst *ReturnInst::clone_impl() const {
3327 return new(getNumOperands()) ReturnInst(*this);
3328}
3329
3330BranchInst *BranchInst::clone_impl() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003331 unsigned Ops(getNumOperands());
Devang Patel11cf3f42009-10-27 22:16:29 +00003332 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003333}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003334
Devang Patel11cf3f42009-10-27 22:16:29 +00003335SwitchInst *SwitchInst::clone_impl() const {
3336 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003337}
3338
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003339IndirectBrInst *IndirectBrInst::clone_impl() const {
3340 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003341}
3342
3343
Devang Patel11cf3f42009-10-27 22:16:29 +00003344InvokeInst *InvokeInst::clone_impl() const {
3345 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003346}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003347
Devang Patel11cf3f42009-10-27 22:16:29 +00003348UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003349 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003350 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003351}
3352
Devang Patel11cf3f42009-10-27 22:16:29 +00003353UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003354 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003355 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003356}