blob: 9817e4c78a82fbb7dc26099c50d03f196acc6587 [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 Hernandezc7d6a832009-10-17 00:00:19 +0000497 if (!MallocF)
498 // prototype malloc as "void *malloc(size_t)"
499 MallocF = cast<Function>(M->getOrInsertFunction("malloc", BPTy,
500 IntPtrTy, NULL));
501 if (!MallocF->doesNotAlias(0)) MallocF->setDoesNotAlias(0);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000502 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000503 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000504 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000505 if (InsertBefore) {
506 MCall = CallInst::Create(MallocF, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000507 Result = MCall;
508 if (Result->getType() != AllocPtrType)
509 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000510 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000511 } else {
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000512 MCall = CallInst::Create(MallocF, AllocSize, "malloccall");
513 Result = MCall;
514 if (Result->getType() != AllocPtrType) {
515 InsertAtEnd->getInstList().push_back(MCall);
516 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000517 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000518 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000519 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000520 MCall->setTailCall();
Victor Hernandezce997802009-11-06 21:43:21 +0000521 MCall->setCallingConv(MallocF->getCallingConv());
Daniel Dunbarb9189002009-09-11 22:07:31 +0000522 assert(MCall->getType() != Type::getVoidTy(BB->getContext()) &&
523 "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000524
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000525 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000526}
527
528/// CreateMalloc - Generate the IR for a call to malloc:
529/// 1. Compute the malloc call's argument as the specified type's size,
530/// possibly multiplied by the array size if the array size is not
531/// constant 1.
532/// 2. Call malloc with that argument.
533/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000534Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
535 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000536 Value *AllocSize, Value *ArraySize,
537 const Twine &Name) {
538 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000539 ArraySize, NULL, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000540}
541
542/// CreateMalloc - Generate the IR for a call to malloc:
543/// 1. Compute the malloc call's argument as the specified type's size,
544/// possibly multiplied by the array size if the array size is not
545/// constant 1.
546/// 2. Call malloc with that argument.
547/// 3. Bitcast the result of the malloc call to the specified type.
548/// Note: This function does not add the bitcast to the basic block, that is the
549/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000550Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
551 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000552 Value *AllocSize, Value *ArraySize,
553 Function *MallocF, const Twine &Name) {
554 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000555 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000556}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000557
Victor Hernandeze2971492009-10-24 04:23:03 +0000558static Instruction* createFree(Value* Source, Instruction *InsertBefore,
559 BasicBlock *InsertAtEnd) {
560 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
561 "createFree needs either InsertBefore or InsertAtEnd");
562 assert(isa<PointerType>(Source->getType()) &&
563 "Can not free something of nonpointer type!");
564
565 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
566 Module* M = BB->getParent()->getParent();
567
568 const Type *VoidTy = Type::getVoidTy(M->getContext());
569 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
570 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000571 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000572 CallInst* Result = NULL;
573 Value *PtrCast = Source;
574 if (InsertBefore) {
575 if (Source->getType() != IntPtrTy)
576 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
577 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
578 } else {
579 if (Source->getType() != IntPtrTy)
580 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
581 Result = CallInst::Create(FreeFunc, PtrCast, "");
582 }
583 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000584 if (Function *F = dyn_cast<Function>(FreeFunc))
585 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000586
587 return Result;
588}
589
590/// CreateFree - Generate the IR for a call to the builtin free function.
591void CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
592 createFree(Source, InsertBefore, NULL);
593}
594
595/// CreateFree - Generate the IR for a call to the builtin free function.
596/// Note: This function does not add the call to the basic block, that is the
597/// responsibility of the caller.
598Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
599 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
600 assert(FreeCall && "CreateFree did not create a CallInst");
601 return FreeCall;
602}
603
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000604//===----------------------------------------------------------------------===//
605// InvokeInst Implementation
606//===----------------------------------------------------------------------===//
607
608void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000609 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000610 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000611 Use *OL = OperandList;
612 OL[0] = Fn;
613 OL[1] = IfNormal;
614 OL[2] = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000615 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000616 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000617 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000618
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000619 assert(((NumArgs == FTy->getNumParams()) ||
620 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000621 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000622
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000623 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000624 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000625 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000626 "Invoking a function with a bad signature!");
627
Gabor Greif2d60e1e2009-09-03 02:02:59 +0000628 OL[i+3] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000629 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000630}
631
Misha Brukmanb1c93172005-04-21 23:48:37 +0000632InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000633 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000634 OperandTraits<InvokeInst>::op_end(this)
635 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000636 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000637 setAttributes(II.getAttributes());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000638 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000639 Use *OL = OperandList, *InOL = II.OperandList;
640 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000641 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000642 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000643}
644
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000645BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
646 return getSuccessor(idx);
647}
648unsigned InvokeInst::getNumSuccessorsV() const {
649 return getNumSuccessors();
650}
651void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
652 return setSuccessor(idx, B);
653}
654
Devang Patelba3fa6c2008-09-23 23:03:40 +0000655bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000656 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000657 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000658 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000659 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000660 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000661}
662
Devang Patel4c758ea2008-09-25 21:00:45 +0000663void InvokeInst::addAttribute(unsigned i, Attributes attr) {
664 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000665 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000666 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000667}
668
Devang Patel4c758ea2008-09-25 21:00:45 +0000669void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
670 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000671 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000672 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000673}
674
Duncan Sands5208d1a2007-11-28 17:07:01 +0000675
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000676//===----------------------------------------------------------------------===//
677// ReturnInst Implementation
678//===----------------------------------------------------------------------===//
679
Chris Lattner2195fc42007-02-24 00:55:48 +0000680ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000681 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000682 OperandTraits<ReturnInst>::op_end(this) -
683 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000684 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000685 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000686 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000687 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000688}
689
Owen Anderson55f1c092009-08-13 21:58:54 +0000690ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
691 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000692 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
693 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000694 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000695 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000696}
Owen Anderson55f1c092009-08-13 21:58:54 +0000697ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
698 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000699 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
700 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000701 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000702 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000703}
Owen Anderson55f1c092009-08-13 21:58:54 +0000704ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
705 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000706 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000707}
708
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000709unsigned ReturnInst::getNumSuccessorsV() const {
710 return getNumSuccessors();
711}
712
Devang Patelae682fb2008-02-26 17:56:20 +0000713/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
714/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000715void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000716 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000717}
718
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000719BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000720 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721 return 0;
722}
723
Devang Patel59643e52008-02-23 00:35:18 +0000724ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000725}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000726
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000727//===----------------------------------------------------------------------===//
728// UnwindInst Implementation
729//===----------------------------------------------------------------------===//
730
Owen Anderson55f1c092009-08-13 21:58:54 +0000731UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
732 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
733 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000734}
Owen Anderson55f1c092009-08-13 21:58:54 +0000735UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
736 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
737 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000738}
739
740
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000741unsigned UnwindInst::getNumSuccessorsV() const {
742 return getNumSuccessors();
743}
744
745void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000746 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000747}
748
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000749BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000750 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000751 return 0;
752}
753
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000754//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000755// UnreachableInst Implementation
756//===----------------------------------------------------------------------===//
757
Owen Anderson55f1c092009-08-13 21:58:54 +0000758UnreachableInst::UnreachableInst(LLVMContext &Context,
759 Instruction *InsertBefore)
760 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
761 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000762}
Owen Anderson55f1c092009-08-13 21:58:54 +0000763UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
764 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
765 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000766}
767
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000768unsigned UnreachableInst::getNumSuccessorsV() const {
769 return getNumSuccessors();
770}
771
772void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000773 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000774}
775
776BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000777 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000778 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000779}
780
781//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000782// BranchInst Implementation
783//===----------------------------------------------------------------------===//
784
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000785void BranchInst::AssertOK() {
786 if (isConditional())
Owen Anderson55f1c092009-08-13 21:58:54 +0000787 assert(getCondition()->getType() == Type::getInt1Ty(getContext()) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000788 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000789}
790
Chris Lattner2195fc42007-02-24 00:55:48 +0000791BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000792 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000793 OperandTraits<BranchInst>::op_end(this) - 1,
794 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000795 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000796 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000797}
798BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
799 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000800 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000801 OperandTraits<BranchInst>::op_end(this) - 3,
802 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000803 Op<-1>() = IfTrue;
804 Op<-2>() = IfFalse;
805 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000806#ifndef NDEBUG
807 AssertOK();
808#endif
809}
810
811BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000812 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000813 OperandTraits<BranchInst>::op_end(this) - 1,
814 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000815 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000816 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000817}
818
819BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
820 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000821 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000822 OperandTraits<BranchInst>::op_end(this) - 3,
823 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000824 Op<-1>() = IfTrue;
825 Op<-2>() = IfFalse;
826 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000827#ifndef NDEBUG
828 AssertOK();
829#endif
830}
831
832
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000833BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000834 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000835 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
836 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000837 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000838 if (BI.getNumOperands() != 1) {
839 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000840 Op<-3>() = BI.Op<-3>();
841 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000842 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000843 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000844}
845
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000846
847Use* Use::getPrefix() {
848 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
849 if (PotentialPrefix.getOpaqueValue())
850 return 0;
851
852 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
853}
854
855BranchInst::~BranchInst() {
856 if (NumOperands == 1) {
857 if (Use *Prefix = OperandList->getPrefix()) {
858 Op<-1>() = 0;
859 //
860 // mark OperandList to have a special value for scrutiny
861 // by baseclass destructors and operator delete
862 OperandList = Prefix;
863 } else {
864 NumOperands = 3;
865 OperandList = op_begin();
866 }
867 }
868}
869
870
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000871BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
872 return getSuccessor(idx);
873}
874unsigned BranchInst::getNumSuccessorsV() const {
875 return getNumSuccessors();
876}
877void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
878 setSuccessor(idx, B);
879}
880
881
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000882//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000883// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000884//===----------------------------------------------------------------------===//
885
Owen Andersonb6b25302009-07-14 23:09:55 +0000886static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000887 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000888 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000889 else {
890 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000891 "Passed basic block into allocation size parameter! Use other ctor");
Owen Anderson55f1c092009-08-13 21:58:54 +0000892 assert(Amt->getType() == Type::getInt32Ty(Context) &&
Victor Hernandeza3aaf852009-10-17 01:18:07 +0000893 "Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000894 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000895 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000896}
897
Victor Hernandez8acf2952009-10-23 21:09:37 +0000898AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
899 const Twine &Name, Instruction *InsertBefore)
900 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
901 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
902 setAlignment(0);
903 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
904 setName(Name);
905}
906
907AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
908 const Twine &Name, BasicBlock *InsertAtEnd)
909 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
910 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
911 setAlignment(0);
912 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
913 setName(Name);
914}
915
916AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
917 Instruction *InsertBefore)
918 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
919 getAISize(Ty->getContext(), 0), InsertBefore) {
920 setAlignment(0);
921 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
922 setName(Name);
923}
924
925AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
926 BasicBlock *InsertAtEnd)
927 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
928 getAISize(Ty->getContext(), 0), InsertAtEnd) {
929 setAlignment(0);
930 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
931 setName(Name);
932}
933
934AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
935 const Twine &Name, Instruction *InsertBefore)
936 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000937 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000938 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000939 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000940 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000941}
942
Victor Hernandez8acf2952009-10-23 21:09:37 +0000943AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
944 const Twine &Name, BasicBlock *InsertAtEnd)
945 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000946 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000947 setAlignment(Align);
Owen Anderson55f1c092009-08-13 21:58:54 +0000948 assert(Ty != Type::getVoidTy(Ty->getContext()) && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000949 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000950}
951
Gordon Henriksen14a55692007-12-10 02:14:30 +0000952// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000953AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000954}
955
Victor Hernandez8acf2952009-10-23 21:09:37 +0000956void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000957 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
958 SubclassData = Log2_32(Align) + 1;
959 assert(getAlignment() == Align && "Alignment representation error!");
960}
961
Victor Hernandez8acf2952009-10-23 21:09:37 +0000962bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000963 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
964 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000965 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000966}
967
Victor Hernandez8acf2952009-10-23 21:09:37 +0000968const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000969 return getType()->getElementType();
970}
971
Chris Lattner8b291e62008-11-26 02:54:17 +0000972/// isStaticAlloca - Return true if this alloca is in the entry block of the
973/// function and is a constant size. If so, the code generator will fold it
974/// into the prolog/epilog code, so it is basically free.
975bool AllocaInst::isStaticAlloca() const {
976 // Must be constant size.
977 if (!isa<ConstantInt>(getArraySize())) return false;
978
979 // Must be in the entry block.
980 const BasicBlock *Parent = getParent();
981 return Parent == &Parent->getParent()->front();
982}
983
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000984//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000985// LoadInst Implementation
986//===----------------------------------------------------------------------===//
987
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000988void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000989 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000990 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000991}
992
Daniel Dunbar4975db62009-07-25 04:41:11 +0000993LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000994 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000995 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000996 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000997 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000998 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000999 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001000}
1001
Daniel Dunbar4975db62009-07-25 04:41:11 +00001002LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001003 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001004 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001005 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001006 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001007 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001008 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001009}
1010
Daniel Dunbar4975db62009-07-25 04:41:11 +00001011LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001012 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001013 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001014 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +00001015 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001016 setAlignment(0);
1017 AssertOK();
1018 setName(Name);
1019}
1020
Daniel Dunbar4975db62009-07-25 04:41:11 +00001021LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001022 unsigned Align, Instruction *InsertBef)
1023 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1024 Load, Ptr, InsertBef) {
1025 setVolatile(isVolatile);
1026 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001027 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +00001028 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001029}
1030
Daniel Dunbar4975db62009-07-25 04:41:11 +00001031LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001032 unsigned Align, BasicBlock *InsertAE)
1033 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1034 Load, Ptr, InsertAE) {
1035 setVolatile(isVolatile);
1036 setAlignment(Align);
1037 AssertOK();
1038 setName(Name);
1039}
1040
Daniel Dunbar4975db62009-07-25 04:41:11 +00001041LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001042 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001043 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001044 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001045 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001046 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +00001047 AssertOK();
1048 setName(Name);
1049}
1050
Daniel Dunbar27096822009-08-11 18:11:15 +00001051
1052
1053LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1054 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1055 Load, Ptr, InsertBef) {
1056 setVolatile(false);
1057 setAlignment(0);
1058 AssertOK();
1059 if (Name && Name[0]) setName(Name);
1060}
1061
1062LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1063 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1064 Load, Ptr, InsertAE) {
1065 setVolatile(false);
1066 setAlignment(0);
1067 AssertOK();
1068 if (Name && Name[0]) setName(Name);
1069}
1070
1071LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1072 Instruction *InsertBef)
1073: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1074 Load, Ptr, InsertBef) {
1075 setVolatile(isVolatile);
1076 setAlignment(0);
1077 AssertOK();
1078 if (Name && Name[0]) setName(Name);
1079}
1080
1081LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1082 BasicBlock *InsertAE)
1083 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1084 Load, Ptr, InsertAE) {
1085 setVolatile(isVolatile);
1086 setAlignment(0);
1087 AssertOK();
1088 if (Name && Name[0]) setName(Name);
1089}
1090
Christopher Lamb84485702007-04-22 19:24:39 +00001091void LoadInst::setAlignment(unsigned Align) {
1092 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1093 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1094}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001095
1096//===----------------------------------------------------------------------===//
1097// StoreInst Implementation
1098//===----------------------------------------------------------------------===//
1099
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001100void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001101 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001102 assert(isa<PointerType>(getOperand(1)->getType()) &&
1103 "Ptr must have pointer type!");
1104 assert(getOperand(0)->getType() ==
1105 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001106 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001107}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001108
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001109
1110StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001111 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001112 OperandTraits<StoreInst>::op_begin(this),
1113 OperandTraits<StoreInst>::operands(this),
1114 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001115 Op<0>() = val;
1116 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001117 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001118 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001119 AssertOK();
1120}
1121
1122StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001123 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001124 OperandTraits<StoreInst>::op_begin(this),
1125 OperandTraits<StoreInst>::operands(this),
1126 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001127 Op<0>() = val;
1128 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001129 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001130 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001131 AssertOK();
1132}
1133
Misha Brukmanb1c93172005-04-21 23:48:37 +00001134StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001135 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001136 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001137 OperandTraits<StoreInst>::op_begin(this),
1138 OperandTraits<StoreInst>::operands(this),
1139 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001140 Op<0>() = val;
1141 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001142 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001143 setAlignment(0);
1144 AssertOK();
1145}
1146
1147StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1148 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001149 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001150 OperandTraits<StoreInst>::op_begin(this),
1151 OperandTraits<StoreInst>::operands(this),
1152 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001153 Op<0>() = val;
1154 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001155 setVolatile(isVolatile);
1156 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001157 AssertOK();
1158}
1159
Misha Brukmanb1c93172005-04-21 23:48:37 +00001160StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001161 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001162 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001163 OperandTraits<StoreInst>::op_begin(this),
1164 OperandTraits<StoreInst>::operands(this),
1165 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001166 Op<0>() = val;
1167 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001168 setVolatile(isVolatile);
1169 setAlignment(Align);
1170 AssertOK();
1171}
1172
1173StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001174 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001175 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001176 OperandTraits<StoreInst>::op_begin(this),
1177 OperandTraits<StoreInst>::operands(this),
1178 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001179 Op<0>() = val;
1180 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001181 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001182 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001183 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001184}
1185
Christopher Lamb84485702007-04-22 19:24:39 +00001186void StoreInst::setAlignment(unsigned Align) {
1187 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1188 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
1189}
1190
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001191//===----------------------------------------------------------------------===//
1192// GetElementPtrInst Implementation
1193//===----------------------------------------------------------------------===//
1194
Christopher Lambedf07882007-12-17 01:12:55 +00001195static unsigned retrieveAddrSpace(const Value *Val) {
1196 return cast<PointerType>(Val->getType())->getAddressSpace();
1197}
1198
Gabor Greif21ba1842008-06-06 20:28:12 +00001199void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001200 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001201 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1202 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001203 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001204
Chris Lattner79807c3d2007-01-31 19:47:18 +00001205 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001206 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001207
1208 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001209}
1210
Daniel Dunbar4975db62009-07-25 04:41:11 +00001211void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001212 assert(NumOperands == 2 && "NumOperands not initialized?");
1213 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001214 OL[0] = Ptr;
1215 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001216
1217 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001218}
1219
Gabor Greiff6caff662008-05-10 08:32:32 +00001220GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001221 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001222 OperandTraits<GetElementPtrInst>::op_end(this)
1223 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001224 GEPI.getNumOperands()) {
1225 Use *OL = OperandList;
1226 Use *GEPIOL = GEPI.OperandList;
1227 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001228 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001229 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001230}
1231
Chris Lattner82981202005-05-03 05:43:30 +00001232GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001233 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001234 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001235 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001236 GetElementPtr,
1237 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1238 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001239 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001240}
1241
1242GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001243 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001244 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001245 checkType(getIndexedType(Ptr->getType(),Idx)),
1246 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001247 GetElementPtr,
1248 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1249 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001250 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001251}
1252
Chris Lattner6090a422009-03-09 04:46:40 +00001253/// getIndexedType - Returns the type of the element that would be accessed with
1254/// a gep instruction with the specified parameters.
1255///
1256/// The Idxs pointer should point to a continuous piece of memory containing the
1257/// indices, either as Value* or uint64_t.
1258///
1259/// A null type is returned if the indices are invalid for the specified
1260/// pointer type.
1261///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001262template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001263static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1264 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001265 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1266 if (!PTy) return 0; // Type isn't a pointer type!
1267 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001268
Chris Lattner6090a422009-03-09 04:46:40 +00001269 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001270 if (NumIdx == 0)
1271 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001272
1273 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001274 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1275 // that contain opaque types) under the assumption that it will be resolved to
1276 // a sane type later.
1277 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001278 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001279
Dan Gohman1ecaf452008-05-31 00:58:22 +00001280 unsigned CurIdx = 1;
1281 for (; CurIdx != NumIdx; ++CurIdx) {
1282 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
1283 if (!CT || isa<PointerType>(CT)) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001284 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001285 if (!CT->indexValid(Index)) return 0;
1286 Agg = CT->getTypeAtIndex(Index);
1287
1288 // If the new type forwards to another type, then it is in the middle
1289 // of being refined to another type (and hence, may have dropped all
1290 // references to what it was using before). So, use the new forwarded
1291 // type.
1292 if (const Type *Ty = Agg->getForwardedType())
1293 Agg = Ty;
1294 }
1295 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001296}
1297
Matthijs Kooijman04468622008-07-29 08:46:11 +00001298const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1299 Value* const *Idxs,
1300 unsigned NumIdx) {
1301 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1302}
1303
1304const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1305 uint64_t const *Idxs,
1306 unsigned NumIdx) {
1307 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1308}
1309
Chris Lattner82981202005-05-03 05:43:30 +00001310const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1311 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1312 if (!PTy) return 0; // Type isn't a pointer type!
1313
1314 // Check the pointer index.
1315 if (!PTy->indexValid(Idx)) return 0;
1316
Chris Lattnerc2233332005-05-03 16:44:45 +00001317 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001318}
1319
Chris Lattner45f15572007-04-14 00:12:57 +00001320
1321/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1322/// zeros. If so, the result pointer and the first operand have the same
1323/// value, just potentially different types.
1324bool GetElementPtrInst::hasAllZeroIndices() const {
1325 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1326 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1327 if (!CI->isZero()) return false;
1328 } else {
1329 return false;
1330 }
1331 }
1332 return true;
1333}
1334
Chris Lattner27058292007-04-27 20:35:56 +00001335/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1336/// constant integers. If so, the result pointer and the first operand have
1337/// a constant offset between them.
1338bool GetElementPtrInst::hasAllConstantIndices() const {
1339 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1340 if (!isa<ConstantInt>(getOperand(i)))
1341 return false;
1342 }
1343 return true;
1344}
1345
Dan Gohman1b849082009-09-07 23:54:19 +00001346void GetElementPtrInst::setIsInBounds(bool B) {
1347 cast<GEPOperator>(this)->setIsInBounds(B);
1348}
Chris Lattner45f15572007-04-14 00:12:57 +00001349
Nick Lewycky28a5f252009-09-27 21:33:04 +00001350bool GetElementPtrInst::isInBounds() const {
1351 return cast<GEPOperator>(this)->isInBounds();
1352}
1353
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001354//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001355// ExtractElementInst Implementation
1356//===----------------------------------------------------------------------===//
1357
1358ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001359 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001360 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001361 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001362 ExtractElement,
1363 OperandTraits<ExtractElementInst>::op_begin(this),
1364 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001365 assert(isValidOperands(Val, Index) &&
1366 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001367 Op<0>() = Val;
1368 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001369 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001370}
1371
1372ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001373 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001374 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001375 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001376 ExtractElement,
1377 OperandTraits<ExtractElementInst>::op_begin(this),
1378 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001379 assert(isValidOperands(Val, Index) &&
1380 "Invalid extractelement instruction operands!");
1381
Gabor Greif2d3024d2008-05-26 21:33:52 +00001382 Op<0>() = Val;
1383 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001384 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001385}
1386
Chris Lattner65511ff2006-10-05 06:24:58 +00001387
Chris Lattner54865b32006-04-08 04:05:48 +00001388bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Owen Anderson55f1c092009-08-13 21:58:54 +00001389 if (!isa<VectorType>(Val->getType()) ||
1390 Index->getType() != Type::getInt32Ty(Val->getContext()))
Chris Lattner54865b32006-04-08 04:05:48 +00001391 return false;
1392 return true;
1393}
1394
1395
Robert Bocchino23004482006-01-10 19:05:34 +00001396//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001397// InsertElementInst Implementation
1398//===----------------------------------------------------------------------===//
1399
Chris Lattner54865b32006-04-08 04:05:48 +00001400InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001401 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001402 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001403 : Instruction(Vec->getType(), InsertElement,
1404 OperandTraits<InsertElementInst>::op_begin(this),
1405 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001406 assert(isValidOperands(Vec, Elt, Index) &&
1407 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001408 Op<0>() = Vec;
1409 Op<1>() = Elt;
1410 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001411 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001412}
1413
Chris Lattner54865b32006-04-08 04:05:48 +00001414InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001415 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001416 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001417 : Instruction(Vec->getType(), InsertElement,
1418 OperandTraits<InsertElementInst>::op_begin(this),
1419 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001420 assert(isValidOperands(Vec, Elt, Index) &&
1421 "Invalid insertelement instruction operands!");
1422
Gabor Greif2d3024d2008-05-26 21:33:52 +00001423 Op<0>() = Vec;
1424 Op<1>() = Elt;
1425 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001426 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001427}
1428
Chris Lattner54865b32006-04-08 04:05:48 +00001429bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1430 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001431 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001432 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001433
Reid Spencerd84d35b2007-02-15 02:26:10 +00001434 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001435 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001436
Owen Anderson55f1c092009-08-13 21:58:54 +00001437 if (Index->getType() != Type::getInt32Ty(Vec->getContext()))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001438 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001439 return true;
1440}
1441
1442
Robert Bocchinoca27f032006-01-17 20:07:22 +00001443//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001444// ShuffleVectorInst Implementation
1445//===----------------------------------------------------------------------===//
1446
1447ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001448 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001449 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001450: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001451 cast<VectorType>(Mask->getType())->getNumElements()),
1452 ShuffleVector,
1453 OperandTraits<ShuffleVectorInst>::op_begin(this),
1454 OperandTraits<ShuffleVectorInst>::operands(this),
1455 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001456 assert(isValidOperands(V1, V2, Mask) &&
1457 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001458 Op<0>() = V1;
1459 Op<1>() = V2;
1460 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001461 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001462}
1463
1464ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001465 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001466 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001467: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1468 cast<VectorType>(Mask->getType())->getNumElements()),
1469 ShuffleVector,
1470 OperandTraits<ShuffleVectorInst>::op_begin(this),
1471 OperandTraits<ShuffleVectorInst>::operands(this),
1472 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001473 assert(isValidOperands(V1, V2, Mask) &&
1474 "Invalid shuffle vector instruction operands!");
1475
Gabor Greif2d3024d2008-05-26 21:33:52 +00001476 Op<0>() = V1;
1477 Op<1>() = V2;
1478 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001479 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001480}
1481
Mon P Wang25f01062008-11-10 04:46:22 +00001482bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001483 const Value *Mask) {
Mon P Wang25f01062008-11-10 04:46:22 +00001484 if (!isa<VectorType>(V1->getType()) || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001485 return false;
1486
1487 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1488 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Owen Anderson55f1c092009-08-13 21:58:54 +00001489 MaskTy->getElementType() != Type::getInt32Ty(V1->getContext()))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001490 return false;
1491 return true;
1492}
1493
Chris Lattnerf724e342008-03-02 05:28:33 +00001494/// getMaskValue - Return the index from the shuffle mask for the specified
1495/// output result. This is either -1 if the element is undef or a number less
1496/// than 2*numelements.
1497int ShuffleVectorInst::getMaskValue(unsigned i) const {
1498 const Constant *Mask = cast<Constant>(getOperand(2));
1499 if (isa<UndefValue>(Mask)) return -1;
1500 if (isa<ConstantAggregateZero>(Mask)) return 0;
1501 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1502 assert(i < MaskCV->getNumOperands() && "Index out of range");
1503
1504 if (isa<UndefValue>(MaskCV->getOperand(i)))
1505 return -1;
1506 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1507}
1508
Dan Gohman12fce772008-05-15 19:50:34 +00001509//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001510// InsertValueInst Class
1511//===----------------------------------------------------------------------===//
1512
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001513void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001514 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001515 assert(NumOperands == 2 && "NumOperands not initialized?");
1516 Op<0>() = Agg;
1517 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001518
Dan Gohman1ecaf452008-05-31 00:58:22 +00001519 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001520 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001521}
1522
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001523void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001524 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001525 assert(NumOperands == 2 && "NumOperands not initialized?");
1526 Op<0>() = Agg;
1527 Op<1>() = Val;
1528
1529 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001530 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001531}
1532
1533InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001534 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001535 OperandTraits<InsertValueInst>::op_begin(this), 2),
1536 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001537 Op<0>() = IVI.getOperand(0);
1538 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001539 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001540}
1541
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001542InsertValueInst::InsertValueInst(Value *Agg,
1543 Value *Val,
1544 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001545 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001546 Instruction *InsertBefore)
1547 : Instruction(Agg->getType(), InsertValue,
1548 OperandTraits<InsertValueInst>::op_begin(this),
1549 2, InsertBefore) {
1550 init(Agg, Val, Idx, Name);
1551}
1552
1553InsertValueInst::InsertValueInst(Value *Agg,
1554 Value *Val,
1555 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001556 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001557 BasicBlock *InsertAtEnd)
1558 : Instruction(Agg->getType(), InsertValue,
1559 OperandTraits<InsertValueInst>::op_begin(this),
1560 2, InsertAtEnd) {
1561 init(Agg, Val, Idx, Name);
1562}
1563
Dan Gohman0752bff2008-05-23 00:36:11 +00001564//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001565// ExtractValueInst Class
1566//===----------------------------------------------------------------------===//
1567
Gabor Greifcbcc4952008-06-06 21:06:32 +00001568void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001569 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001570 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001571
Dan Gohman1ecaf452008-05-31 00:58:22 +00001572 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001573 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001574}
1575
Daniel Dunbar4975db62009-07-25 04:41:11 +00001576void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001577 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001578
1579 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001580 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001581}
1582
1583ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001584 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001585 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001586 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001587}
1588
Dan Gohman12fce772008-05-15 19:50:34 +00001589// getIndexedType - Returns the type of the element that would be extracted
1590// with an extractvalue instruction with the specified parameters.
1591//
1592// A null type is returned if the indices are invalid for the specified
1593// pointer type.
1594//
1595const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001596 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001597 unsigned NumIdx) {
1598 unsigned CurIdx = 0;
1599 for (; CurIdx != NumIdx; ++CurIdx) {
1600 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001601 if (!CT || isa<PointerType>(CT) || isa<VectorType>(CT)) return 0;
1602 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001603 if (!CT->indexValid(Index)) return 0;
1604 Agg = CT->getTypeAtIndex(Index);
1605
1606 // If the new type forwards to another type, then it is in the middle
1607 // of being refined to another type (and hence, may have dropped all
1608 // references to what it was using before). So, use the new forwarded
1609 // type.
1610 if (const Type *Ty = Agg->getForwardedType())
1611 Agg = Ty;
1612 }
1613 return CurIdx == NumIdx ? Agg : 0;
1614}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001615
Dan Gohman4a3125b2008-06-17 21:07:55 +00001616const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001617 unsigned Idx) {
1618 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001619}
1620
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001621//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001622// BinaryOperator Class
1623//===----------------------------------------------------------------------===//
1624
Dan Gohmana5b96452009-06-04 22:49:04 +00001625/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1626/// type is floating-point, to help provide compatibility with an older API.
1627///
1628static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1629 const Type *Ty) {
1630 // API compatibility: Adjust integer opcodes to floating-point opcodes.
1631 if (Ty->isFPOrFPVector()) {
1632 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1633 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1634 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1635 }
1636 return iType;
1637}
1638
Chris Lattner2195fc42007-02-24 00:55:48 +00001639BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001640 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001641 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001642 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001643 OperandTraits<BinaryOperator>::op_begin(this),
1644 OperandTraits<BinaryOperator>::operands(this),
1645 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001646 Op<0>() = S1;
1647 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001648 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001649 setName(Name);
1650}
1651
1652BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001653 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001654 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001655 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001656 OperandTraits<BinaryOperator>::op_begin(this),
1657 OperandTraits<BinaryOperator>::operands(this),
1658 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001659 Op<0>() = S1;
1660 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001661 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001662 setName(Name);
1663}
1664
1665
1666void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001667 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001668 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001669 assert(LHS->getType() == RHS->getType() &&
1670 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001671#ifndef NDEBUG
1672 switch (iType) {
1673 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001674 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001675 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001676 "Arithmetic operation should return same type as operands!");
Dan Gohmana5b96452009-06-04 22:49:04 +00001677 assert(getType()->isIntOrIntVector() &&
1678 "Tried to create an integer operation on a non-integer type!");
1679 break;
1680 case FAdd: case FSub:
1681 case FMul:
1682 assert(getType() == LHS->getType() &&
1683 "Arithmetic operation should return same type as operands!");
1684 assert(getType()->isFPOrFPVector() &&
1685 "Tried to create a floating-point operation on a "
1686 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001687 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001688 case UDiv:
1689 case SDiv:
1690 assert(getType() == LHS->getType() &&
1691 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001692 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1693 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001694 "Incorrect operand type (not integer) for S/UDIV");
1695 break;
1696 case FDiv:
1697 assert(getType() == LHS->getType() &&
1698 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001699 assert(getType()->isFPOrFPVector() &&
1700 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001701 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001702 case URem:
1703 case SRem:
1704 assert(getType() == LHS->getType() &&
1705 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001706 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1707 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001708 "Incorrect operand type (not integer) for S/UREM");
1709 break;
1710 case FRem:
1711 assert(getType() == LHS->getType() &&
1712 "Arithmetic operation should return same type as operands!");
Dan Gohman7889f2b2009-06-15 22:25:12 +00001713 assert(getType()->isFPOrFPVector() &&
1714 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001715 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001716 case Shl:
1717 case LShr:
1718 case AShr:
1719 assert(getType() == LHS->getType() &&
1720 "Shift operation should return same type as operands!");
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001721 assert((getType()->isInteger() ||
1722 (isa<VectorType>(getType()) &&
1723 cast<VectorType>(getType())->getElementType()->isInteger())) &&
1724 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001725 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001726 case And: case Or:
1727 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001728 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001729 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001730 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001731 (isa<VectorType>(getType()) &&
1732 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001733 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001734 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001735 default:
1736 break;
1737 }
1738#endif
1739}
1740
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001741BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001742 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001743 Instruction *InsertBefore) {
1744 assert(S1->getType() == S2->getType() &&
1745 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001746 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001747}
1748
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001749BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001750 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001751 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001752 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001753 InsertAtEnd->getInstList().push_back(Res);
1754 return Res;
1755}
1756
Dan Gohman5476cfd2009-08-12 16:23:25 +00001757BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001758 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001759 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001760 return new BinaryOperator(Instruction::Sub,
1761 zero, Op,
1762 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001763}
1764
Dan Gohman5476cfd2009-08-12 16:23:25 +00001765BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001766 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001767 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001768 return new BinaryOperator(Instruction::Sub,
1769 zero, Op,
1770 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001771}
1772
Dan Gohman5476cfd2009-08-12 16:23:25 +00001773BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001774 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001775 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001776 return new BinaryOperator(Instruction::FSub,
1777 zero, Op,
1778 Op->getType(), Name, InsertBefore);
1779}
1780
Dan Gohman5476cfd2009-08-12 16:23:25 +00001781BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001782 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001783 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001784 return new BinaryOperator(Instruction::FSub,
1785 zero, Op,
1786 Op->getType(), Name, InsertAtEnd);
1787}
1788
Dan Gohman5476cfd2009-08-12 16:23:25 +00001789BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001790 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001791 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001792 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001793 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001794 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001795 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001796 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001797 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001798 }
1799
1800 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001801 Op->getType(), Name, InsertBefore);
1802}
1803
Dan Gohman5476cfd2009-08-12 16:23:25 +00001804BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001805 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001806 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001807 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001808 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001809 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001810 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001811 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001812 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001813 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001814 }
1815
1816 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001817 Op->getType(), Name, InsertAtEnd);
1818}
1819
1820
1821// isConstantAllOnes - Helper function for several functions below
1822static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001823 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1824 return CI->isAllOnesValue();
1825 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1826 return CV->isAllOnesValue();
1827 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001828}
1829
Owen Andersonbb2501b2009-07-13 22:18:28 +00001830bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001831 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1832 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001833 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1834 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001835 return false;
1836}
1837
Owen Andersonbb2501b2009-07-13 22:18:28 +00001838bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001839 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1840 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001841 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001842 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001843 return false;
1844}
1845
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001846bool BinaryOperator::isNot(const Value *V) {
1847 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1848 return (Bop->getOpcode() == Instruction::Xor &&
1849 (isConstantAllOnes(Bop->getOperand(1)) ||
1850 isConstantAllOnes(Bop->getOperand(0))));
1851 return false;
1852}
1853
Chris Lattner2c7d1772005-04-24 07:28:37 +00001854Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001855 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001856}
1857
Chris Lattner2c7d1772005-04-24 07:28:37 +00001858const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1859 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001860}
1861
Dan Gohmana5b96452009-06-04 22:49:04 +00001862Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001863 return cast<BinaryOperator>(BinOp)->getOperand(1);
1864}
1865
1866const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1867 return getFNegArgument(const_cast<Value*>(BinOp));
1868}
1869
Chris Lattner2c7d1772005-04-24 07:28:37 +00001870Value *BinaryOperator::getNotArgument(Value *BinOp) {
1871 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1872 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1873 Value *Op0 = BO->getOperand(0);
1874 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001875 if (isConstantAllOnes(Op0)) return Op1;
1876
1877 assert(isConstantAllOnes(Op1));
1878 return Op0;
1879}
1880
Chris Lattner2c7d1772005-04-24 07:28:37 +00001881const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1882 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001883}
1884
1885
1886// swapOperands - Exchange the two operands to this instruction. This
1887// instruction is safe to use on any binary instruction and does not
1888// modify the semantics of the instruction. If the instruction is
1889// order dependent (SetLT f.e.) the opcode is changed.
1890//
1891bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001892 if (!isCommutative())
1893 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001894 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001895 return false;
1896}
1897
Dan Gohman1b849082009-09-07 23:54:19 +00001898void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1899 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1900}
1901
1902void BinaryOperator::setHasNoSignedWrap(bool b) {
1903 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1904}
1905
1906void BinaryOperator::setIsExact(bool b) {
1907 cast<SDivOperator>(this)->setIsExact(b);
1908}
1909
Nick Lewycky28a5f252009-09-27 21:33:04 +00001910bool BinaryOperator::hasNoUnsignedWrap() const {
1911 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1912}
1913
1914bool BinaryOperator::hasNoSignedWrap() const {
1915 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1916}
1917
1918bool BinaryOperator::isExact() const {
1919 return cast<SDivOperator>(this)->isExact();
1920}
1921
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001922//===----------------------------------------------------------------------===//
1923// CastInst Class
1924//===----------------------------------------------------------------------===//
1925
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001926// Just determine if this cast only deals with integral->integral conversion.
1927bool CastInst::isIntegerCast() const {
1928 switch (getOpcode()) {
1929 default: return false;
1930 case Instruction::ZExt:
1931 case Instruction::SExt:
1932 case Instruction::Trunc:
1933 return true;
1934 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001935 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001936 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001937}
1938
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001939bool CastInst::isLosslessCast() const {
1940 // Only BitCast can be lossless, exit fast if we're not BitCast
1941 if (getOpcode() != Instruction::BitCast)
1942 return false;
1943
1944 // Identity cast is always lossless
1945 const Type* SrcTy = getOperand(0)->getType();
1946 const Type* DstTy = getType();
1947 if (SrcTy == DstTy)
1948 return true;
1949
Reid Spencer8d9336d2006-12-31 05:26:44 +00001950 // Pointer to pointer is always lossless.
1951 if (isa<PointerType>(SrcTy))
1952 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001953 return false; // Other types have no identity values
1954}
1955
1956/// This function determines if the CastInst does not require any bits to be
1957/// changed in order to effect the cast. Essentially, it identifies cases where
1958/// no code gen is necessary for the cast, hence the name no-op cast. For
1959/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001960/// # bitcast i32* %x to i8*
1961/// # bitcast <2 x i32> %x to <4 x i16>
1962/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001963/// @brief Determine if a cast is a no-op.
1964bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1965 switch (getOpcode()) {
1966 default:
1967 assert(!"Invalid CastOp");
1968 case Instruction::Trunc:
1969 case Instruction::ZExt:
1970 case Instruction::SExt:
1971 case Instruction::FPTrunc:
1972 case Instruction::FPExt:
1973 case Instruction::UIToFP:
1974 case Instruction::SIToFP:
1975 case Instruction::FPToUI:
1976 case Instruction::FPToSI:
1977 return false; // These always modify bits
1978 case Instruction::BitCast:
1979 return true; // BitCast never modifies bits.
1980 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001981 return IntPtrTy->getScalarSizeInBits() ==
1982 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001983 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001984 return IntPtrTy->getScalarSizeInBits() ==
1985 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001986 }
1987}
1988
1989/// This function determines if a pair of casts can be eliminated and what
1990/// opcode should be used in the elimination. This assumes that there are two
1991/// instructions like this:
1992/// * %F = firstOpcode SrcTy %x to MidTy
1993/// * %S = secondOpcode MidTy %F to DstTy
1994/// The function returns a resultOpcode so these two casts can be replaced with:
1995/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1996/// If no such cast is permited, the function returns 0.
1997unsigned CastInst::isEliminableCastPair(
1998 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1999 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
2000{
2001 // Define the 144 possibilities for these two cast instructions. The values
2002 // in this matrix determine what to do in a given situation and select the
2003 // case in the switch below. The rows correspond to firstOp, the columns
2004 // correspond to secondOp. In looking at the table below, keep in mind
2005 // the following cast properties:
2006 //
2007 // Size Compare Source Destination
2008 // Operator Src ? Size Type Sign Type Sign
2009 // -------- ------------ ------------------- ---------------------
2010 // TRUNC > Integer Any Integral Any
2011 // ZEXT < Integral Unsigned Integer Any
2012 // SEXT < Integral Signed Integer Any
2013 // FPTOUI n/a FloatPt n/a Integral Unsigned
2014 // FPTOSI n/a FloatPt n/a Integral Signed
2015 // UITOFP n/a Integral Unsigned FloatPt n/a
2016 // SITOFP n/a Integral Signed FloatPt n/a
2017 // FPTRUNC > FloatPt n/a FloatPt n/a
2018 // FPEXT < FloatPt n/a FloatPt n/a
2019 // PTRTOINT n/a Pointer n/a Integral Unsigned
2020 // INTTOPTR n/a Integral Unsigned Pointer n/a
2021 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002022 //
2023 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002024 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2025 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002026 // of the produced value (we no longer know the top-part is all zeros).
2027 // Further this conversion is often much more expensive for typical hardware,
2028 // and causes issues when building libgcc. We disallow fptosi+sext for the
2029 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002030 const unsigned numCastOps =
2031 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2032 static const uint8_t CastResults[numCastOps][numCastOps] = {
2033 // T F F U S F F P I B -+
2034 // R Z S P P I I T P 2 N T |
2035 // U E E 2 2 2 2 R E I T C +- secondOp
2036 // N X X U S F F N X N 2 V |
2037 // C T T I I P P C T T P T -+
2038 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2039 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2040 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002041 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2042 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002043 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2044 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2045 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2046 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2047 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2048 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2049 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2050 };
2051
2052 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2053 [secondOp-Instruction::CastOpsBegin];
2054 switch (ElimCase) {
2055 case 0:
2056 // categorically disallowed
2057 return 0;
2058 case 1:
2059 // allowed, use first cast's opcode
2060 return firstOp;
2061 case 2:
2062 // allowed, use second cast's opcode
2063 return secondOp;
2064 case 3:
2065 // no-op cast in second op implies firstOp as long as the DestTy
2066 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00002067 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002068 return firstOp;
2069 return 0;
2070 case 4:
2071 // no-op cast in second op implies firstOp as long as the DestTy
2072 // is floating point
2073 if (DstTy->isFloatingPoint())
2074 return firstOp;
2075 return 0;
2076 case 5:
2077 // no-op cast in first op implies secondOp as long as the SrcTy
2078 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00002079 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002080 return secondOp;
2081 return 0;
2082 case 6:
2083 // no-op cast in first op implies secondOp as long as the SrcTy
2084 // is a floating point
2085 if (SrcTy->isFloatingPoint())
2086 return secondOp;
2087 return 0;
2088 case 7: {
2089 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002090 if (!IntPtrTy)
2091 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002092 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2093 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002094 if (MidSize >= PtrSize)
2095 return Instruction::BitCast;
2096 return 0;
2097 }
2098 case 8: {
2099 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2100 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2101 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002102 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2103 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002104 if (SrcSize == DstSize)
2105 return Instruction::BitCast;
2106 else if (SrcSize < DstSize)
2107 return firstOp;
2108 return secondOp;
2109 }
2110 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2111 return Instruction::ZExt;
2112 case 10:
2113 // fpext followed by ftrunc is allowed if the bit size returned to is
2114 // the same as the original, in which case its just a bitcast
2115 if (SrcTy == DstTy)
2116 return Instruction::BitCast;
2117 return 0; // If the types are not the same we can't eliminate it.
2118 case 11:
2119 // bitcast followed by ptrtoint is allowed as long as the bitcast
2120 // is a pointer to pointer cast.
2121 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
2122 return secondOp;
2123 return 0;
2124 case 12:
2125 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
2126 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
2127 return firstOp;
2128 return 0;
2129 case 13: {
2130 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002131 if (!IntPtrTy)
2132 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002133 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2134 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2135 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002136 if (SrcSize <= PtrSize && SrcSize == DstSize)
2137 return Instruction::BitCast;
2138 return 0;
2139 }
2140 case 99:
2141 // cast combination can't happen (error in input). This is for all cases
2142 // where the MidTy is not the same for the two cast instructions.
2143 assert(!"Invalid Cast Combination");
2144 return 0;
2145 default:
2146 assert(!"Error in CastResults table!!!");
2147 return 0;
2148 }
2149 return 0;
2150}
2151
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002152CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002153 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002154 // Construct and return the appropriate CastInst subclass
2155 switch (op) {
2156 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2157 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2158 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2159 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2160 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2161 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2162 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2163 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2164 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2165 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2166 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2167 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2168 default:
2169 assert(!"Invalid opcode provided");
2170 }
2171 return 0;
2172}
2173
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002174CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002175 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002176 // Construct and return the appropriate CastInst subclass
2177 switch (op) {
2178 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2179 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2180 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2181 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2182 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2183 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2184 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2185 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2186 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2187 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2188 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2189 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2190 default:
2191 assert(!"Invalid opcode provided");
2192 }
2193 return 0;
2194}
2195
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002196CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002197 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002198 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002199 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002200 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2201 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002202}
2203
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002204CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002205 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002206 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002207 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002208 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2209 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002210}
2211
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002212CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002213 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002214 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002215 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002216 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2217 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002218}
2219
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002220CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002221 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002222 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002223 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002224 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2225 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002226}
2227
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002228CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002229 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002230 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002231 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002232 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2233 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002234}
2235
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002236CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002237 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002238 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002239 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002240 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2241 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002242}
2243
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002244CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002245 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002246 BasicBlock *InsertAtEnd) {
2247 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002248 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002249 "Invalid cast");
2250
Chris Lattner03c49532007-01-15 02:27:26 +00002251 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002252 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2253 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002254}
2255
2256/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002257CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002258 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002259 Instruction *InsertBefore) {
2260 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00002261 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002262 "Invalid cast");
2263
Chris Lattner03c49532007-01-15 02:27:26 +00002264 if (Ty->isInteger())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002265 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2266 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002267}
2268
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002269CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002270 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002271 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00002272 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002273 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2274 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002275 Instruction::CastOps opcode =
2276 (SrcBits == DstBits ? Instruction::BitCast :
2277 (SrcBits > DstBits ? Instruction::Trunc :
2278 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002279 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002280}
2281
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002282CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002283 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002284 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002285 assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() &&
2286 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002287 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2288 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002289 Instruction::CastOps opcode =
2290 (SrcBits == DstBits ? Instruction::BitCast :
2291 (SrcBits > DstBits ? Instruction::Trunc :
2292 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002293 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002294}
2295
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002296CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002297 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002298 Instruction *InsertBefore) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002299 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002300 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002301 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2302 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002303 Instruction::CastOps opcode =
2304 (SrcBits == DstBits ? Instruction::BitCast :
2305 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002306 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002307}
2308
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002309CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002310 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002311 BasicBlock *InsertAtEnd) {
Dan Gohman7889f2b2009-06-15 22:25:12 +00002312 assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002313 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002314 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2315 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002316 Instruction::CastOps opcode =
2317 (SrcBits == DstBits ? Instruction::BitCast :
2318 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002319 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002320}
2321
Duncan Sands55e50902008-01-06 10:12:28 +00002322// Check whether it is valid to call getCastOpcode for these types.
2323// This routine must be kept in sync with getCastOpcode.
2324bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2325 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2326 return false;
2327
2328 if (SrcTy == DestTy)
2329 return true;
2330
2331 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002332 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2333 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002334
2335 // Run through the possibilities ...
Gabor Greif697e94c2008-05-15 10:04:30 +00002336 if (DestTy->isInteger()) { // Casting to integral
2337 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002338 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002339 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002340 return true;
2341 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002342 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002343 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002344 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002345 return isa<PointerType>(SrcTy);
2346 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002347 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
2348 if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002349 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002350 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002351 return true;
2352 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002353 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002354 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002355 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002356 return false;
2357 }
2358 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002359 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002360 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002361 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002362 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002363 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002364 return DestPTy->getBitWidth() == SrcBits;
2365 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002366 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
2367 if (isa<PointerType>(SrcTy)) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002368 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002369 } else if (SrcTy->isInteger()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002370 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002371 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002372 return false;
2373 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002374 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002375 return false;
2376 }
2377}
2378
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002379// Provide a way to get a "cast" where the cast opcode is inferred from the
2380// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002381// logic in the castIsValid function below. This axiom should hold:
2382// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2383// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002385// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002386Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002387CastInst::getCastOpcode(
2388 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002389 // Get the bit sizes, we'll need these
2390 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002391 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2392 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393
Duncan Sands55e50902008-01-06 10:12:28 +00002394 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2395 "Only first class types are castable!");
2396
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002397 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00002398 if (DestTy->isInteger()) { // Casting to integral
2399 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002400 if (DestBits < SrcBits)
2401 return Trunc; // int -> smaller int
2402 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002403 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002404 return SExt; // signed -> SEXT
2405 else
2406 return ZExt; // unsigned -> ZEXT
2407 } else {
2408 return BitCast; // Same size, No-op cast
2409 }
2410 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002411 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002412 return FPToSI; // FP -> sint
2413 else
2414 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002415 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002416 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002417 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002418 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002419 return BitCast; // Same size, no-op cast
2420 } else {
2421 assert(isa<PointerType>(SrcTy) &&
2422 "Casting from a value that is not first-class type");
2423 return PtrToInt; // ptr -> int
2424 }
2425 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002426 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002427 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428 return SIToFP; // sint -> FP
2429 else
2430 return UIToFP; // uint -> FP
2431 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2432 if (DestBits < SrcBits) {
2433 return FPTrunc; // FP -> smaller FP
2434 } else if (DestBits > SrcBits) {
2435 return FPExt; // FP -> larger FP
2436 } else {
2437 return BitCast; // same size, no-op cast
2438 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002439 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002440 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002441 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002442 PTy = NULL;
2443 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002445 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002446 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002447 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2448 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002449 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002450 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002451 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002452 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002454 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002455 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002456 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002457 }
2458 } else if (isa<PointerType>(DestTy)) {
2459 if (isa<PointerType>(SrcTy)) {
2460 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002461 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002462 return IntToPtr; // int -> ptr
2463 } else {
2464 assert(!"Casting pointer to other than pointer or int");
2465 }
2466 } else {
2467 assert(!"Casting to type that is not first-class");
2468 }
2469
2470 // If we fall through to here we probably hit an assertion cast above
2471 // and assertions are not turned on. Anything we return is an error, so
2472 // BitCast is as good a choice as any.
2473 return BitCast;
2474}
2475
2476//===----------------------------------------------------------------------===//
2477// CastInst SubClass Constructors
2478//===----------------------------------------------------------------------===//
2479
2480/// Check that the construction parameters for a CastInst are correct. This
2481/// could be broken out into the separate constructors but it is useful to have
2482/// it in one place and to eliminate the redundant code for getting the sizes
2483/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002484bool
2485CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002486
2487 // Check for type sanity on the arguments
2488 const Type *SrcTy = S->getType();
2489 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2490 return false;
2491
2492 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002493 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2494 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002495
2496 // Switch on the opcode provided
2497 switch (op) {
2498 default: return false; // This is an input error
2499 case Instruction::Trunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002500 return SrcTy->isIntOrIntVector() &&
2501 DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502 case Instruction::ZExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002503 return SrcTy->isIntOrIntVector() &&
2504 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002505 case Instruction::SExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002506 return SrcTy->isIntOrIntVector() &&
2507 DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002508 case Instruction::FPTrunc:
Dan Gohman550c9af2008-08-14 20:04:46 +00002509 return SrcTy->isFPOrFPVector() &&
2510 DstTy->isFPOrFPVector() &&
2511 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002512 case Instruction::FPExt:
Dan Gohman550c9af2008-08-14 20:04:46 +00002513 return SrcTy->isFPOrFPVector() &&
2514 DstTy->isFPOrFPVector() &&
2515 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002517 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002518 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2519 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002520 return SVTy->getElementType()->isIntOrIntVector() &&
2521 DVTy->getElementType()->isFPOrFPVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002522 SVTy->getNumElements() == DVTy->getNumElements();
2523 }
2524 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002525 return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002526 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002527 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002528 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2529 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Dan Gohman550c9af2008-08-14 20:04:46 +00002530 return SVTy->getElementType()->isFPOrFPVector() &&
2531 DVTy->getElementType()->isIntOrIntVector() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002532 SVTy->getNumElements() == DVTy->getNumElements();
2533 }
2534 }
Dan Gohman550c9af2008-08-14 20:04:46 +00002535 return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002536 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002537 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002539 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540 case Instruction::BitCast:
2541 // BitCast implies a no-op cast of type only. No bits change.
2542 // However, you can't cast pointers to anything but pointers.
2543 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2544 return false;
2545
Duncan Sands55e50902008-01-06 10:12:28 +00002546 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547 // these cases, the cast is okay if the source and destination bit widths
2548 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002549 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550 }
2551}
2552
2553TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002554 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002555) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002556 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002557}
2558
2559TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002560 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002561) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002562 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563}
2564
2565ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002566 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002567) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002568 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569}
2570
2571ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002572 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002573) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002574 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002575}
2576SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002577 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002578) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002579 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580}
2581
Jeff Cohencc08c832006-12-02 02:22:01 +00002582SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002583 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002584) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002585 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586}
2587
2588FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002589 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002590) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002591 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002592}
2593
2594FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002595 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002596) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002597 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002598}
2599
2600FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002601 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002602) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002603 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604}
2605
2606FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002607 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002608) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002609 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002610}
2611
2612UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002613 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002614) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002615 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002616}
2617
2618UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002619 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002620) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002621 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002622}
2623
2624SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002625 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002626) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002627 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628}
2629
2630SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002631 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002632) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002633 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002634}
2635
2636FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002637 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002638) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002639 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002640}
2641
2642FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002643 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002644) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002645 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002646}
2647
2648FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002649 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002650) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002651 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002652}
2653
2654FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002655 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002656) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002657 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002658}
2659
2660PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002661 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002662) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002663 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002664}
2665
2666PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002667 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002668) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002669 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002670}
2671
2672IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002673 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002674) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002675 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002676}
2677
2678IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002679 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002680) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002681 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002682}
2683
2684BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002685 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002686) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002687 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002688}
2689
2690BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002691 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002692) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002693 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002694}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002695
2696//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002697// CmpInst Classes
2698//===----------------------------------------------------------------------===//
2699
Nate Begemand2195702008-05-12 19:01:56 +00002700CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002701 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002702 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002703 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002704 OperandTraits<CmpInst>::op_begin(this),
2705 OperandTraits<CmpInst>::operands(this),
2706 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002707 Op<0>() = LHS;
2708 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002709 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002710 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002711}
Gabor Greiff6caff662008-05-10 08:32:32 +00002712
Nate Begemand2195702008-05-12 19:01:56 +00002713CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002714 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002715 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002716 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002717 OperandTraits<CmpInst>::op_begin(this),
2718 OperandTraits<CmpInst>::operands(this),
2719 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002720 Op<0>() = LHS;
2721 Op<1>() = RHS;
Reid Spencerd9436b62006-11-20 01:22:35 +00002722 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002723 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002724}
2725
2726CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002727CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002728 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002729 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002730 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002731 if (InsertBefore)
2732 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2733 S1, S2, Name);
2734 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002735 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002736 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002737 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002738
2739 if (InsertBefore)
2740 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2741 S1, S2, Name);
2742 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002743 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002744 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002745}
2746
2747CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002748CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002749 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002750 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002751 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2752 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002753 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002754 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2755 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002756}
2757
2758void CmpInst::swapOperands() {
2759 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2760 IC->swapOperands();
2761 else
2762 cast<FCmpInst>(this)->swapOperands();
2763}
2764
2765bool CmpInst::isCommutative() {
2766 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2767 return IC->isCommutative();
2768 return cast<FCmpInst>(this)->isCommutative();
2769}
2770
2771bool CmpInst::isEquality() {
2772 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2773 return IC->isEquality();
2774 return cast<FCmpInst>(this)->isEquality();
2775}
2776
2777
Dan Gohman4e724382008-05-31 02:47:54 +00002778CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002779 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002780 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002781 case ICMP_EQ: return ICMP_NE;
2782 case ICMP_NE: return ICMP_EQ;
2783 case ICMP_UGT: return ICMP_ULE;
2784 case ICMP_ULT: return ICMP_UGE;
2785 case ICMP_UGE: return ICMP_ULT;
2786 case ICMP_ULE: return ICMP_UGT;
2787 case ICMP_SGT: return ICMP_SLE;
2788 case ICMP_SLT: return ICMP_SGE;
2789 case ICMP_SGE: return ICMP_SLT;
2790 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002791
Dan Gohman4e724382008-05-31 02:47:54 +00002792 case FCMP_OEQ: return FCMP_UNE;
2793 case FCMP_ONE: return FCMP_UEQ;
2794 case FCMP_OGT: return FCMP_ULE;
2795 case FCMP_OLT: return FCMP_UGE;
2796 case FCMP_OGE: return FCMP_ULT;
2797 case FCMP_OLE: return FCMP_UGT;
2798 case FCMP_UEQ: return FCMP_ONE;
2799 case FCMP_UNE: return FCMP_OEQ;
2800 case FCMP_UGT: return FCMP_OLE;
2801 case FCMP_ULT: return FCMP_OGE;
2802 case FCMP_UGE: return FCMP_OLT;
2803 case FCMP_ULE: return FCMP_OGT;
2804 case FCMP_ORD: return FCMP_UNO;
2805 case FCMP_UNO: return FCMP_ORD;
2806 case FCMP_TRUE: return FCMP_FALSE;
2807 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002808 }
2809}
2810
Reid Spencer266e42b2006-12-23 06:05:41 +00002811ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2812 switch (pred) {
2813 default: assert(! "Unknown icmp predicate!");
2814 case ICMP_EQ: case ICMP_NE:
2815 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2816 return pred;
2817 case ICMP_UGT: return ICMP_SGT;
2818 case ICMP_ULT: return ICMP_SLT;
2819 case ICMP_UGE: return ICMP_SGE;
2820 case ICMP_ULE: return ICMP_SLE;
2821 }
2822}
2823
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002824ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2825 switch (pred) {
2826 default: assert(! "Unknown icmp predicate!");
2827 case ICMP_EQ: case ICMP_NE:
2828 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2829 return pred;
2830 case ICMP_SGT: return ICMP_UGT;
2831 case ICMP_SLT: return ICMP_ULT;
2832 case ICMP_SGE: return ICMP_UGE;
2833 case ICMP_SLE: return ICMP_ULE;
2834 }
2835}
2836
Reid Spencer0286bc12007-02-28 22:00:54 +00002837/// Initialize a set of values that all satisfy the condition with C.
2838///
2839ConstantRange
2840ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2841 APInt Lower(C);
2842 APInt Upper(C);
2843 uint32_t BitWidth = C.getBitWidth();
2844 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002845 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002846 case ICmpInst::ICMP_EQ: Upper++; break;
2847 case ICmpInst::ICMP_NE: Lower++; break;
2848 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2849 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2850 case ICmpInst::ICMP_UGT:
2851 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2852 break;
2853 case ICmpInst::ICMP_SGT:
2854 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2855 break;
2856 case ICmpInst::ICMP_ULE:
2857 Lower = APInt::getMinValue(BitWidth); Upper++;
2858 break;
2859 case ICmpInst::ICMP_SLE:
2860 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2861 break;
2862 case ICmpInst::ICMP_UGE:
2863 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2864 break;
2865 case ICmpInst::ICMP_SGE:
2866 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2867 break;
2868 }
2869 return ConstantRange(Lower, Upper);
2870}
2871
Dan Gohman4e724382008-05-31 02:47:54 +00002872CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002873 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002874 default: assert(!"Unknown cmp predicate!");
2875 case ICMP_EQ: case ICMP_NE:
2876 return pred;
2877 case ICMP_SGT: return ICMP_SLT;
2878 case ICMP_SLT: return ICMP_SGT;
2879 case ICMP_SGE: return ICMP_SLE;
2880 case ICMP_SLE: return ICMP_SGE;
2881 case ICMP_UGT: return ICMP_ULT;
2882 case ICMP_ULT: return ICMP_UGT;
2883 case ICMP_UGE: return ICMP_ULE;
2884 case ICMP_ULE: return ICMP_UGE;
2885
Reid Spencerd9436b62006-11-20 01:22:35 +00002886 case FCMP_FALSE: case FCMP_TRUE:
2887 case FCMP_OEQ: case FCMP_ONE:
2888 case FCMP_UEQ: case FCMP_UNE:
2889 case FCMP_ORD: case FCMP_UNO:
2890 return pred;
2891 case FCMP_OGT: return FCMP_OLT;
2892 case FCMP_OLT: return FCMP_OGT;
2893 case FCMP_OGE: return FCMP_OLE;
2894 case FCMP_OLE: return FCMP_OGE;
2895 case FCMP_UGT: return FCMP_ULT;
2896 case FCMP_ULT: return FCMP_UGT;
2897 case FCMP_UGE: return FCMP_ULE;
2898 case FCMP_ULE: return FCMP_UGE;
2899 }
2900}
2901
Reid Spencer266e42b2006-12-23 06:05:41 +00002902bool CmpInst::isUnsigned(unsigned short predicate) {
2903 switch (predicate) {
2904 default: return false;
2905 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2906 case ICmpInst::ICMP_UGE: return true;
2907 }
2908}
2909
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002910bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002911 switch (predicate) {
2912 default: return false;
2913 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2914 case ICmpInst::ICMP_SGE: return true;
2915 }
2916}
2917
2918bool CmpInst::isOrdered(unsigned short predicate) {
2919 switch (predicate) {
2920 default: return false;
2921 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2922 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2923 case FCmpInst::FCMP_ORD: return true;
2924 }
2925}
2926
2927bool CmpInst::isUnordered(unsigned short predicate) {
2928 switch (predicate) {
2929 default: return false;
2930 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2931 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2932 case FCmpInst::FCMP_UNO: return true;
2933 }
2934}
2935
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002936bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2937 switch(predicate) {
2938 default: return false;
2939 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2940 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2941 }
2942}
2943
2944bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2945 switch(predicate) {
2946 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2947 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2948 default: return false;
2949 }
2950}
2951
2952
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002953//===----------------------------------------------------------------------===//
2954// SwitchInst Implementation
2955//===----------------------------------------------------------------------===//
2956
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002957void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002958 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002959 ReservedSpace = 2+NumCases*2;
2960 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002961 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002962
Gabor Greif2d3024d2008-05-26 21:33:52 +00002963 OperandList[0] = Value;
2964 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002965}
2966
Chris Lattner2195fc42007-02-24 00:55:48 +00002967/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2968/// switch on and a default destination. The number of additional cases can
2969/// be specified here to make memory allocation more efficient. This
2970/// constructor can also autoinsert before another instruction.
2971SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2972 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002973 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2974 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002975 init(Value, Default, NumCases);
2976}
2977
2978/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2979/// switch on and a default destination. The number of additional cases can
2980/// be specified here to make memory allocation more efficient. This
2981/// constructor also autoinserts at the end of the specified BasicBlock.
2982SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2983 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002984 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2985 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002986 init(Value, Default, NumCases);
2987}
2988
Misha Brukmanb1c93172005-04-21 23:48:37 +00002989SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002990 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002991 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002992 Use *OL = OperandList, *InOL = SI.OperandList;
2993 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002994 OL[i] = InOL[i];
2995 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002996 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002997 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002998}
2999
Gordon Henriksen14a55692007-12-10 02:14:30 +00003000SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003001 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003002}
3003
3004
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003005/// addCase - Add an entry to the switch instruction...
3006///
Chris Lattner47ac1872005-02-24 05:32:09 +00003007void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003008 unsigned OpNo = NumOperands;
3009 if (OpNo+2 > ReservedSpace)
3010 resizeOperands(0); // Get more space!
3011 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003012 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003013 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003014 OperandList[OpNo] = OnVal;
3015 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003016}
3017
3018/// removeCase - This method removes the specified successor from the switch
3019/// instruction. Note that this cannot be used to remove the default
3020/// destination (successor #0).
3021///
3022void SwitchInst::removeCase(unsigned idx) {
3023 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003024 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3025
3026 unsigned NumOps = getNumOperands();
3027 Use *OL = OperandList;
3028
3029 // Move everything after this operand down.
3030 //
3031 // FIXME: we could just swap with the end of the list, then erase. However,
3032 // client might not expect this to happen. The code as it is thrashes the
3033 // use/def lists, which is kinda lame.
3034 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3035 OL[i-2] = OL[i];
3036 OL[i-2+1] = OL[i+1];
3037 }
3038
3039 // Nuke the last value.
3040 OL[NumOps-2].set(0);
3041 OL[NumOps-2+1].set(0);
3042 NumOperands = NumOps-2;
3043}
3044
3045/// resizeOperands - resize operands - This adjusts the length of the operands
3046/// list according to the following behavior:
3047/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003048/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003049/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3050/// 3. If NumOps == NumOperands, trim the reserved space.
3051///
3052void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003053 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003054 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003055 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003056 } else if (NumOps*2 > NumOperands) {
3057 // No resize needed.
3058 if (ReservedSpace >= NumOps) return;
3059 } else if (NumOps == NumOperands) {
3060 if (ReservedSpace == NumOps) return;
3061 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003062 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003063 }
3064
3065 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003066 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003067 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003068 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003069 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003070 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003071 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003072 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003073}
3074
3075
3076BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3077 return getSuccessor(idx);
3078}
3079unsigned SwitchInst::getNumSuccessorsV() const {
3080 return getNumSuccessors();
3081}
3082void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3083 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003084}
Chris Lattnerf22be932004-10-15 23:52:53 +00003085
Chris Lattner3ed871f2009-10-27 19:13:16 +00003086//===----------------------------------------------------------------------===//
3087// SwitchInst Implementation
3088//===----------------------------------------------------------------------===//
3089
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003090void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Chris Lattner6747b4c2009-10-29 05:53:32 +00003091 assert(Address && isa<PointerType>(Address->getType()) &&
3092 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003093 ReservedSpace = 1+NumDests;
3094 NumOperands = 1;
3095 OperandList = allocHungoffUses(ReservedSpace);
3096
3097 OperandList[0] = Address;
3098}
3099
3100
3101/// resizeOperands - resize operands - This adjusts the length of the operands
3102/// list according to the following behavior:
3103/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3104/// of operation. This grows the number of ops by 2 times.
3105/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3106/// 3. If NumOps == NumOperands, trim the reserved space.
3107///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003108void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003109 unsigned e = getNumOperands();
3110 if (NumOps == 0) {
3111 NumOps = e*2;
3112 } else if (NumOps*2 > NumOperands) {
3113 // No resize needed.
3114 if (ReservedSpace >= NumOps) return;
3115 } else if (NumOps == NumOperands) {
3116 if (ReservedSpace == NumOps) return;
3117 } else {
3118 return;
3119 }
3120
3121 ReservedSpace = NumOps;
3122 Use *NewOps = allocHungoffUses(NumOps);
3123 Use *OldOps = OperandList;
3124 for (unsigned i = 0; i != e; ++i)
3125 NewOps[i] = OldOps[i];
3126 OperandList = NewOps;
3127 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3128}
3129
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003130IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3131 Instruction *InsertBefore)
3132: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003133 0, 0, InsertBefore) {
3134 init(Address, NumCases);
3135}
3136
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003137IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3138 BasicBlock *InsertAtEnd)
3139: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003140 0, 0, InsertAtEnd) {
3141 init(Address, NumCases);
3142}
3143
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003144IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3145 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003146 allocHungoffUses(IBI.getNumOperands()),
3147 IBI.getNumOperands()) {
3148 Use *OL = OperandList, *InOL = IBI.OperandList;
3149 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3150 OL[i] = InOL[i];
3151 SubclassOptionalData = IBI.SubclassOptionalData;
3152}
3153
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003154IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003155 dropHungoffUses(OperandList);
3156}
3157
3158/// addDestination - Add a destination.
3159///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003160void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003161 unsigned OpNo = NumOperands;
3162 if (OpNo+1 > ReservedSpace)
3163 resizeOperands(0); // Get more space!
3164 // Initialize some new operands.
3165 assert(OpNo < ReservedSpace && "Growing didn't work!");
3166 NumOperands = OpNo+1;
3167 OperandList[OpNo] = DestBB;
3168}
3169
3170/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003171/// indirectbr instruction.
3172void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003173 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3174
3175 unsigned NumOps = getNumOperands();
3176 Use *OL = OperandList;
3177
3178 // Replace this value with the last one.
3179 OL[idx+1] = OL[NumOps-1];
3180
3181 // Nuke the last value.
3182 OL[NumOps-1].set(0);
3183 NumOperands = NumOps-1;
3184}
3185
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003186BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003187 return getSuccessor(idx);
3188}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003189unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003190 return getNumSuccessors();
3191}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003192void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003193 setSuccessor(idx, B);
3194}
3195
3196//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003197// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003198//===----------------------------------------------------------------------===//
3199
Chris Lattnerf22be932004-10-15 23:52:53 +00003200// Define these methods here so vtables don't get emitted into every translation
3201// unit that uses these classes.
3202
Devang Patel11cf3f42009-10-27 22:16:29 +00003203GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3204 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003205}
3206
Devang Patel11cf3f42009-10-27 22:16:29 +00003207BinaryOperator *BinaryOperator::clone_impl() const {
3208 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003209}
3210
Devang Patel11cf3f42009-10-27 22:16:29 +00003211FCmpInst* FCmpInst::clone_impl() const {
3212 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003213}
3214
Devang Patel11cf3f42009-10-27 22:16:29 +00003215ICmpInst* ICmpInst::clone_impl() const {
3216 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003217}
3218
Devang Patel11cf3f42009-10-27 22:16:29 +00003219ExtractValueInst *ExtractValueInst::clone_impl() const {
3220 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003221}
3222
Devang Patel11cf3f42009-10-27 22:16:29 +00003223InsertValueInst *InsertValueInst::clone_impl() const {
3224 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003225}
3226
Devang Patel11cf3f42009-10-27 22:16:29 +00003227AllocaInst *AllocaInst::clone_impl() const {
3228 return new AllocaInst(getAllocatedType(),
3229 (Value*)getOperand(0),
3230 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003231}
3232
Devang Patel11cf3f42009-10-27 22:16:29 +00003233LoadInst *LoadInst::clone_impl() const {
3234 return new LoadInst(getOperand(0),
3235 Twine(), isVolatile(),
3236 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003237}
3238
Devang Patel11cf3f42009-10-27 22:16:29 +00003239StoreInst *StoreInst::clone_impl() const {
3240 return new StoreInst(getOperand(0), getOperand(1),
3241 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003242}
3243
Devang Patel11cf3f42009-10-27 22:16:29 +00003244TruncInst *TruncInst::clone_impl() const {
3245 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003246}
3247
Devang Patel11cf3f42009-10-27 22:16:29 +00003248ZExtInst *ZExtInst::clone_impl() const {
3249 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003250}
3251
Devang Patel11cf3f42009-10-27 22:16:29 +00003252SExtInst *SExtInst::clone_impl() const {
3253 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003254}
3255
Devang Patel11cf3f42009-10-27 22:16:29 +00003256FPTruncInst *FPTruncInst::clone_impl() const {
3257 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003258}
3259
Devang Patel11cf3f42009-10-27 22:16:29 +00003260FPExtInst *FPExtInst::clone_impl() const {
3261 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003262}
3263
Devang Patel11cf3f42009-10-27 22:16:29 +00003264UIToFPInst *UIToFPInst::clone_impl() const {
3265 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003266}
3267
Devang Patel11cf3f42009-10-27 22:16:29 +00003268SIToFPInst *SIToFPInst::clone_impl() const {
3269 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003270}
3271
Devang Patel11cf3f42009-10-27 22:16:29 +00003272FPToUIInst *FPToUIInst::clone_impl() const {
3273 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003274}
3275
Devang Patel11cf3f42009-10-27 22:16:29 +00003276FPToSIInst *FPToSIInst::clone_impl() const {
3277 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003278}
3279
Devang Patel11cf3f42009-10-27 22:16:29 +00003280PtrToIntInst *PtrToIntInst::clone_impl() const {
3281 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003282}
3283
Devang Patel11cf3f42009-10-27 22:16:29 +00003284IntToPtrInst *IntToPtrInst::clone_impl() const {
3285 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003286}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003287
Devang Patel11cf3f42009-10-27 22:16:29 +00003288BitCastInst *BitCastInst::clone_impl() const {
3289 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003290}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003291
Devang Patel11cf3f42009-10-27 22:16:29 +00003292CallInst *CallInst::clone_impl() const {
3293 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003294}
3295
Devang Patel11cf3f42009-10-27 22:16:29 +00003296SelectInst *SelectInst::clone_impl() const {
3297 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003298}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003299
Devang Patel11cf3f42009-10-27 22:16:29 +00003300VAArgInst *VAArgInst::clone_impl() const {
3301 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003302}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003303
Devang Patel11cf3f42009-10-27 22:16:29 +00003304ExtractElementInst *ExtractElementInst::clone_impl() const {
3305 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003306}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003307
Devang Patel11cf3f42009-10-27 22:16:29 +00003308InsertElementInst *InsertElementInst::clone_impl() const {
3309 return InsertElementInst::Create(getOperand(0),
3310 getOperand(1),
3311 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003312}
3313
Devang Patel11cf3f42009-10-27 22:16:29 +00003314ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3315 return new ShuffleVectorInst(getOperand(0),
3316 getOperand(1),
3317 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003318}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003319
Devang Patel11cf3f42009-10-27 22:16:29 +00003320PHINode *PHINode::clone_impl() const {
3321 return new PHINode(*this);
3322}
3323
3324ReturnInst *ReturnInst::clone_impl() const {
3325 return new(getNumOperands()) ReturnInst(*this);
3326}
3327
3328BranchInst *BranchInst::clone_impl() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003329 unsigned Ops(getNumOperands());
Devang Patel11cf3f42009-10-27 22:16:29 +00003330 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003331}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003332
Devang Patel11cf3f42009-10-27 22:16:29 +00003333SwitchInst *SwitchInst::clone_impl() const {
3334 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003335}
3336
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003337IndirectBrInst *IndirectBrInst::clone_impl() const {
3338 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003339}
3340
3341
Devang Patel11cf3f42009-10-27 22:16:29 +00003342InvokeInst *InvokeInst::clone_impl() const {
3343 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003344}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003345
Devang Patel11cf3f42009-10-27 22:16:29 +00003346UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003347 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003348 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003349}
3350
Devang Patel11cf3f42009-10-27 22:16:29 +00003351UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003352 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003353 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003354}